blob: 70e1f474c58a10ce23d46520d870a7f2008fbb2b [file] [log] [blame]
Dan Willemsen218f6562015-07-08 18:13:11 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
Dan Willemsen97750522016-02-09 17:43:51 -080018 "fmt"
Dan Willemsen218f6562015-07-08 18:13:11 -070019 "io"
Colin Crossa2344662016-03-24 13:14:12 -070020 "path/filepath"
Dan Willemsen218f6562015-07-08 18:13:11 -070021 "strings"
22
Colin Cross635c3b02016-05-18 15:37:25 -070023 "android/soong/android"
Dan Willemsen218f6562015-07-08 18:13:11 -070024)
25
Dan Willemsenf2c27d72016-07-13 16:50:22 -070026type AndroidMkContext interface {
27 Target() android.Target
28}
29
Colin Cross635c3b02016-05-18 15:37:25 -070030func (c *Module) AndroidMk() (ret android.AndroidMkData, err error) {
Colin Crossbc6fb162016-05-24 15:39:04 -070031 if c.Properties.HideFromMake {
32 ret.Disabled = true
33 return ret, nil
34 }
35
Colin Crossca860ac2016-01-04 14:34:37 -080036 ret.OutputFile = c.outputFile
Colin Cross635c3b02016-05-18 15:37:25 -070037 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) (err error) {
Colin Cross30d5f512016-05-03 18:02:42 -070038 fmt.Fprintln(w, "LOCAL_SANITIZE := never")
Colin Crossc99deeb2016-04-11 15:06:20 -070039 if len(c.Properties.AndroidMkSharedLibs) > 0 {
40 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES := "+strings.Join(c.Properties.AndroidMkSharedLibs, " "))
Colin Crossca860ac2016-01-04 14:34:37 -080041 }
Dan Willemsena96ff642016-06-07 12:34:45 -070042 if c.Target().Os == android.Android && c.Properties.Sdk_version != "" {
43 fmt.Fprintln(w, "LOCAL_SDK_VERSION := "+c.Properties.Sdk_version)
44 fmt.Fprintln(w, "LOCAL_NDK_STL_VARIANT := none")
45 } else {
46 // These are already included in LOCAL_SHARED_LIBRARIES
47 fmt.Fprintln(w, "LOCAL_CXX_STL := none")
48 }
Colin Crossca860ac2016-01-04 14:34:37 -080049 return nil
50 })
51
52 callSubAndroidMk := func(obj interface{}) {
53 if obj != nil {
54 if androidmk, ok := obj.(interface {
Dan Willemsenf2c27d72016-07-13 16:50:22 -070055 AndroidMk(AndroidMkContext, *android.AndroidMkData)
Colin Crossca860ac2016-01-04 14:34:37 -080056 }); ok {
Dan Willemsenf2c27d72016-07-13 16:50:22 -070057 androidmk.AndroidMk(c, &ret)
Colin Crossca860ac2016-01-04 14:34:37 -080058 }
59 }
60 }
61
62 for _, feature := range c.features {
63 callSubAndroidMk(feature)
64 }
65
66 callSubAndroidMk(c.compiler)
67 callSubAndroidMk(c.linker)
Colin Crossc99deeb2016-04-11 15:06:20 -070068 if c.linker.installable() {
69 callSubAndroidMk(c.installer)
70 }
Colin Crossca860ac2016-01-04 14:34:37 -080071
72 return ret, nil
73}
74
Dan Willemsenf2c27d72016-07-13 16:50:22 -070075func (library *baseLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Crossca860ac2016-01-04 14:34:37 -080076 if library.static() {
Dan Willemsen218f6562015-07-08 18:13:11 -070077 ret.Class = "STATIC_LIBRARIES"
78 } else {
79 ret.Class = "SHARED_LIBRARIES"
80 }
Colin Crossca860ac2016-01-04 14:34:37 -080081}
82
Dan Willemsenf2c27d72016-07-13 16:50:22 -070083func (library *libraryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
84 library.baseLinker.AndroidMk(ctx, ret)
Colin Crossca860ac2016-01-04 14:34:37 -080085
Colin Cross7b5c22b2016-07-07 11:16:20 -070086 if !library.static() {
Dan Willemsenf2c27d72016-07-13 16:50:22 -070087 library.stripper.AndroidMk(ctx, ret)
Colin Cross7b5c22b2016-07-07 11:16:20 -070088 }
Dan Willemsen7517ed02016-06-10 17:20:30 -070089
Colin Cross635c3b02016-05-18 15:37:25 -070090 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen3b1fffa2016-05-05 15:11:48 -070091 var exportedIncludes []string
Colin Crossca860ac2016-01-04 14:34:37 -080092 for _, flag := range library.exportedFlags() {
Dan Willemsen3b1fffa2016-05-05 15:11:48 -070093 if strings.HasPrefix(flag, "-I") {
Dan Willemsen97750522016-02-09 17:43:51 -080094 exportedIncludes = append(exportedIncludes, strings.TrimPrefix(flag, "-I"))
95 }
Dan Willemsen218f6562015-07-08 18:13:11 -070096 }
97 if len(exportedIncludes) > 0 {
Dan Willemsen97750522016-02-09 17:43:51 -080098 fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DIRS :=", strings.Join(exportedIncludes, " "))
Dan Willemsen218f6562015-07-08 18:13:11 -070099 }
100
Colin Crossca860ac2016-01-04 14:34:37 -0800101 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
Dan Willemsen218f6562015-07-08 18:13:11 -0700102
Dan Willemsen97750522016-02-09 17:43:51 -0800103 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Dan Willemsen218f6562015-07-08 18:13:11 -0700104
Dan Willemsen97750522016-02-09 17:43:51 -0800105 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800106 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700107}
108
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700109func (object *objectLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsen97750522016-02-09 17:43:51 -0800110 ret.Custom = func(w io.Writer, name, prefix string) error {
Colin Crossca860ac2016-01-04 14:34:37 -0800111 out := ret.OutputFile.Path()
Dan Willemsen218f6562015-07-08 18:13:11 -0700112
Dan Willemsen72d39932016-07-08 23:23:48 -0700113 fmt.Fprintln(w, "\n$("+prefix+"OUT_INTERMEDIATE_LIBRARIES)/"+name+objectExtension+":", out.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800114 fmt.Fprintln(w, "\t$(copy-file-to-target)")
115
116 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700117 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700118}
119
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700120func (binary *binaryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
121 binary.stripper.AndroidMk(ctx, ret)
Dan Willemsen7517ed02016-06-10 17:20:30 -0700122
Dan Willemsen218f6562015-07-08 18:13:11 -0700123 ret.Class = "EXECUTABLES"
Colin Cross635c3b02016-05-18 15:37:25 -0700124 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800125 fmt.Fprintln(w, "LOCAL_CXX_STL := none")
126 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Colin Cross3854a602016-01-11 12:49:11 -0800127 if binary.static() {
128 fmt.Fprintln(w, "LOCAL_FORCE_STATIC_EXECUTABLE := true")
129 }
Dan Willemsen97750522016-02-09 17:43:51 -0800130 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800131 })
132}
133
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700134func (test *testBinaryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
135 test.binaryLinker.AndroidMk(ctx, ret)
Colin Crossc7a38dc2016-07-12 13:13:09 -0700136 if Bool(test.testLinker.Properties.Test_per_src) {
Dan Albert6a047692016-07-18 17:24:47 -0700137 ret.SubName = "_" + test.binaryLinker.Properties.Stem
Colin Crossa2344662016-03-24 13:14:12 -0700138 }
139}
140
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700141func (library *toolchainLibraryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
142 library.baseLinker.AndroidMk(ctx, ret)
Dan Willemsene7932e82016-05-16 15:56:53 -0700143
Colin Cross635c3b02016-05-18 15:37:25 -0700144 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsene7932e82016-05-16 15:56:53 -0700145 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
146 fmt.Fprintln(w, "LOCAL_CXX_STL := none")
147 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
148
149 return nil
150 })
151}
152
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700153func (stripper *stripper) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
154 // Make only supports stripping target modules
155 if ctx.Target().Os != android.Android {
156 return
157 }
158
Dan Willemsen7517ed02016-06-10 17:20:30 -0700159 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
160 if stripper.StripProperties.Strip.None {
161 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false")
162 } else if stripper.StripProperties.Strip.Keep_symbols {
163 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := keep_symbols")
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700164 } else {
165 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := mini-debug-info")
Dan Willemsen7517ed02016-06-10 17:20:30 -0700166 }
167
168 return nil
169 })
170}
171
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700172func (installer *baseInstaller) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross635c3b02016-05-18 15:37:25 -0700173 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Colin Crossa2344662016-03-24 13:14:12 -0700174 path := installer.path.RelPathString()
Colin Crossbf305de2016-03-29 17:32:06 -0700175 dir, file := filepath.Split(path)
176 stem := strings.TrimSuffix(file, filepath.Ext(file))
Colin Crosse14388b2016-05-03 14:08:40 -0700177 fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(OUT_DIR)/"+filepath.Clean(dir))
Colin Crossbf305de2016-03-29 17:32:06 -0700178 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
Colin Cross3854a602016-01-11 12:49:11 -0800179 if len(installer.Properties.Symlinks) > 0 {
180 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS := "+strings.Join(installer.Properties.Symlinks, " "))
181 }
Colin Crossa2344662016-03-24 13:14:12 -0700182 return nil
183 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700184}