blob: 59a1db89f5e64ac5d37ef4e60215c1a904ce497d [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
Colin Crossb916a382016-07-29 17:28:03 -070028 subAndroidMk(*android.AndroidMkData, interface{})
Dan Willemsen4416e5d2017-04-06 12:43:22 -070029 vndk() bool
Colin Crossb916a382016-07-29 17:28:03 -070030}
31
32type subAndroidMkProvider interface {
33 AndroidMk(AndroidMkContext, *android.AndroidMkData)
34}
35
36func (c *Module) subAndroidMk(data *android.AndroidMkData, obj interface{}) {
37 if c.subAndroidMkOnce == nil {
38 c.subAndroidMkOnce = make(map[subAndroidMkProvider]bool)
39 }
40 if androidmk, ok := obj.(subAndroidMkProvider); ok {
41 if !c.subAndroidMkOnce[androidmk] {
42 c.subAndroidMkOnce[androidmk] = true
43 androidmk.AndroidMk(c, data)
44 }
45 }
Dan Willemsenf2c27d72016-07-13 16:50:22 -070046}
47
Colin Cross635c3b02016-05-18 15:37:25 -070048func (c *Module) AndroidMk() (ret android.AndroidMkData, err error) {
Colin Crossbc6fb162016-05-24 15:39:04 -070049 if c.Properties.HideFromMake {
50 ret.Disabled = true
51 return ret, nil
52 }
53
Colin Crossca860ac2016-01-04 14:34:37 -080054 ret.OutputFile = c.outputFile
Colin Cross635c3b02016-05-18 15:37:25 -070055 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) (err error) {
Colin Cross30d5f512016-05-03 18:02:42 -070056 fmt.Fprintln(w, "LOCAL_SANITIZE := never")
Colin Crossc99deeb2016-04-11 15:06:20 -070057 if len(c.Properties.AndroidMkSharedLibs) > 0 {
58 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES := "+strings.Join(c.Properties.AndroidMkSharedLibs, " "))
Colin Crossca860ac2016-01-04 14:34:37 -080059 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -070060 if c.Target().Os == android.Android && c.Properties.Sdk_version != "" && !c.vndk() {
Dan Willemsena96ff642016-06-07 12:34:45 -070061 fmt.Fprintln(w, "LOCAL_SDK_VERSION := "+c.Properties.Sdk_version)
62 fmt.Fprintln(w, "LOCAL_NDK_STL_VARIANT := none")
63 } else {
64 // These are already included in LOCAL_SHARED_LIBRARIES
65 fmt.Fprintln(w, "LOCAL_CXX_STL := none")
66 }
Dan Willemsen4416e5d2017-04-06 12:43:22 -070067 if c.vndk() {
68 fmt.Fprintln(w, "LOCAL_USE_VNDK := true")
69 }
Colin Crossca860ac2016-01-04 14:34:37 -080070 return nil
71 })
72
Colin Crossca860ac2016-01-04 14:34:37 -080073 for _, feature := range c.features {
Colin Crossb916a382016-07-29 17:28:03 -070074 c.subAndroidMk(&ret, feature)
Colin Crossca860ac2016-01-04 14:34:37 -080075 }
76
Colin Crossb916a382016-07-29 17:28:03 -070077 c.subAndroidMk(&ret, c.compiler)
78 c.subAndroidMk(&ret, c.linker)
79 c.subAndroidMk(&ret, c.installer)
Colin Crossca860ac2016-01-04 14:34:37 -080080
Dan Willemsen4416e5d2017-04-06 12:43:22 -070081 if c.vndk() {
82 ret.SubName += ".vendor"
83 }
84
Colin Crossca860ac2016-01-04 14:34:37 -080085 return ret, nil
86}
87
Dan Willemsen58539402017-03-19 13:44:32 -070088func (library *libraryDecorator) androidMkWriteExportedFlags(w io.Writer) {
89 exportedFlags := library.exportedFlags()
90 if len(exportedFlags) > 0 {
91 fmt.Fprintln(w, "LOCAL_EXPORT_CFLAGS :=", strings.Join(exportedFlags, " "))
Dan Willemsend5781342017-02-15 16:15:21 -080092 }
Dan Willemsen58539402017-03-19 13:44:32 -070093 exportedFlagsDeps := library.exportedFlagsDeps()
94 if len(exportedFlagsDeps) > 0 {
95 fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DEPS :=", strings.Join(exportedFlagsDeps.Strings(), " "))
96 }
97}
Dan Willemsend5781342017-02-15 16:15:21 -080098
Dan Willemsen58539402017-03-19 13:44:32 -070099func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsend5781342017-02-15 16:15:21 -0800100 if library.static() {
101 ret.Class = "STATIC_LIBRARIES"
102 } else if library.shared() {
103 ctx.subAndroidMk(ret, &library.stripper)
104 ctx.subAndroidMk(ret, &library.relocationPacker)
105
106 ret.Class = "SHARED_LIBRARIES"
107 } else if library.header() {
108 ret.Custom = func(w io.Writer, name, prefix, moduleDir string) error {
109 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
110 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
111 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
112
113 archStr := ctx.Target().Arch.ArchType.String()
114 var host bool
115 switch ctx.Target().Os.Class {
116 case android.Host:
117 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH := ", archStr)
118 host = true
119 case android.HostCross:
120 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH := ", archStr)
121 host = true
122 case android.Device:
123 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH := ", archStr)
124 }
125
126 if host {
127 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", ctx.Target().Os.String())
128 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700129 } else if ctx.vndk() {
130 fmt.Fprintln(w, "LOCAL_USE_VNDK := true")
Dan Willemsend5781342017-02-15 16:15:21 -0800131 }
132
Dan Willemsen58539402017-03-19 13:44:32 -0700133 library.androidMkWriteExportedFlags(w)
Dan Willemsend5781342017-02-15 16:15:21 -0800134 fmt.Fprintln(w, "include $(BUILD_HEADER_LIBRARY)")
135
136 return nil
137 }
138
139 return
140 }
141
142 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen58539402017-03-19 13:44:32 -0700143 library.androidMkWriteExportedFlags(w)
Dan Willemsen218f6562015-07-08 18:13:11 -0700144
Dan Willemsen1d577e22016-08-29 15:53:15 -0700145 fmt.Fprintln(w, "LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE)"+outputFile.Ext())
Dan Willemsen218f6562015-07-08 18:13:11 -0700146
Dan Willemsen97750522016-02-09 17:43:51 -0800147 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Dan Willemsen218f6562015-07-08 18:13:11 -0700148
Dan Willemsen581341d2017-02-09 16:16:31 -0800149 if library.coverageOutputFile.Valid() {
150 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", library.coverageOutputFile.String())
151 }
152
Dan Willemsen97750522016-02-09 17:43:51 -0800153 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800154 })
Colin Crossb916a382016-07-29 17:28:03 -0700155
Colin Cross4ac44802017-02-15 14:06:12 -0800156 if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700157 ctx.subAndroidMk(ret, library.baseInstaller)
158 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700159}
160
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700161func (object *objectLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Nan Zhang6d34b302017-02-04 17:47:46 -0800162 ret.Custom = func(w io.Writer, name, prefix, moduleDir string) error {
Colin Crossca860ac2016-01-04 14:34:37 -0800163 out := ret.OutputFile.Path()
Dan Willemsen218f6562015-07-08 18:13:11 -0700164
Dan Willemsen72d39932016-07-08 23:23:48 -0700165 fmt.Fprintln(w, "\n$("+prefix+"OUT_INTERMEDIATE_LIBRARIES)/"+name+objectExtension+":", out.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800166 fmt.Fprintln(w, "\t$(copy-file-to-target)")
167
168 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700169 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700170}
171
Colin Crossb916a382016-07-29 17:28:03 -0700172func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross1e7d3702016-08-24 15:25:47 -0700173 ctx.subAndroidMk(ret, binary.baseInstaller)
Colin Crossb916a382016-07-29 17:28:03 -0700174 ctx.subAndroidMk(ret, &binary.stripper)
Dan Willemsen7517ed02016-06-10 17:20:30 -0700175
Dan Willemsen218f6562015-07-08 18:13:11 -0700176 ret.Class = "EXECUTABLES"
Colin Cross635c3b02016-05-18 15:37:25 -0700177 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800178 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Colin Crossb916a382016-07-29 17:28:03 -0700179 if Bool(binary.Properties.Static_executable) {
Colin Cross3854a602016-01-11 12:49:11 -0800180 fmt.Fprintln(w, "LOCAL_FORCE_STATIC_EXECUTABLE := true")
181 }
Colin Cross9b09f242016-12-07 13:37:42 -0800182
183 if len(binary.symlinks) > 0 {
184 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS := "+strings.Join(binary.symlinks, " "))
185 }
186
Dan Willemsen581341d2017-02-09 16:16:31 -0800187 if binary.coverageOutputFile.Valid() {
188 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", binary.coverageOutputFile.String())
189 }
Dan Willemsen97750522016-02-09 17:43:51 -0800190 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800191 })
192}
193
Colin Crossb916a382016-07-29 17:28:03 -0700194func (benchmark *benchmarkDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
195 ctx.subAndroidMk(ret, benchmark.binaryDecorator)
Colin Crossb916a382016-07-29 17:28:03 -0700196}
197
198func (test *testBinary) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
199 ctx.subAndroidMk(ret, test.binaryDecorator)
Dan Willemsen0fe72532017-01-17 13:25:49 -0800200 ret.Class = "NATIVE_TESTS"
Colin Crossb916a382016-07-29 17:28:03 -0700201 if Bool(test.Properties.Test_per_src) {
202 ret.SubName = "_" + test.binaryDecorator.Properties.Stem
Colin Crossa2344662016-03-24 13:14:12 -0700203 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800204
Colin Crossa929db02017-03-27 16:27:50 -0700205 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
206 if len(test.Properties.Test_suites) > 0 {
Dan Shi4df566d2017-04-03 12:29:25 -0700207 fmt.Fprintln(w, "LOCAL_COMPATIBILITY_SUITE :=",
Colin Crossa929db02017-03-27 16:27:50 -0700208 strings.Join(test.Properties.Test_suites, " "))
209 }
210 return nil
211 })
212
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800213 var testFiles []string
214 for _, d := range test.data {
215 rel := d.Rel()
216 path := d.String()
217 if !strings.HasSuffix(path, rel) {
218 panic(fmt.Errorf("path %q does not end with %q", path, rel))
219 }
220 path = strings.TrimSuffix(path, rel)
221 testFiles = append(testFiles, path+":"+rel)
222 }
223 if len(testFiles) > 0 {
224 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
225 fmt.Fprintln(w, "LOCAL_TEST_DATA := "+strings.Join(testFiles, " "))
226 return nil
227 })
228 }
Colin Crossa2344662016-03-24 13:14:12 -0700229}
230
Colin Crossb916a382016-07-29 17:28:03 -0700231func (test *testLibrary) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
232 ctx.subAndroidMk(ret, test.libraryDecorator)
233}
Dan Willemsene7932e82016-05-16 15:56:53 -0700234
Colin Crossb916a382016-07-29 17:28:03 -0700235func (library *toolchainLibraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
236 ret.Class = "STATIC_LIBRARIES"
Colin Cross635c3b02016-05-18 15:37:25 -0700237 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsene7932e82016-05-16 15:56:53 -0700238 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
Dan Willemsene7932e82016-05-16 15:56:53 -0700239 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
240
241 return nil
242 })
243}
244
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700245func (stripper *stripper) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
246 // Make only supports stripping target modules
247 if ctx.Target().Os != android.Android {
248 return
249 }
250
Dan Willemsen7517ed02016-06-10 17:20:30 -0700251 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
252 if stripper.StripProperties.Strip.None {
253 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false")
254 } else if stripper.StripProperties.Strip.Keep_symbols {
255 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := keep_symbols")
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700256 } else {
257 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := mini-debug-info")
Dan Willemsen7517ed02016-06-10 17:20:30 -0700258 }
259
260 return nil
261 })
262}
263
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700264func (packer *relocationPacker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
265 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
266 if packer.Properties.PackingRelocations {
267 fmt.Fprintln(w, "LOCAL_PACK_MODULE_RELOCATIONS := true")
268 }
269 return nil
270 })
271}
272
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700273func (installer *baseInstaller) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsen03ce63e2016-09-29 12:17:49 -0700274 // Soong installation is only supported for host modules. Have Make
275 // installation trigger Soong installation.
276 if ctx.Target().Os.Class == android.Host {
277 ret.OutputFile = android.OptionalPathForPath(installer.path)
278 }
279
Colin Cross635c3b02016-05-18 15:37:25 -0700280 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Colin Crossa2344662016-03-24 13:14:12 -0700281 path := installer.path.RelPathString()
Colin Crossbf305de2016-03-29 17:32:06 -0700282 dir, file := filepath.Split(path)
283 stem := strings.TrimSuffix(file, filepath.Ext(file))
Dan Willemsen1d577e22016-08-29 15:53:15 -0700284 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+filepath.Ext(file))
Colin Crosse14388b2016-05-03 14:08:40 -0700285 fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(OUT_DIR)/"+filepath.Clean(dir))
Colin Crossbf305de2016-03-29 17:32:06 -0700286 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
Colin Crossa2344662016-03-24 13:14:12 -0700287 return nil
288 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700289}
Dan Albert914449f2016-06-17 16:45:24 -0700290
Colin Crossb916a382016-07-29 17:28:03 -0700291func (c *stubDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsen01a90592017-04-07 15:21:13 -0700292 ret.SubName = ndkLibrarySuffix + "." + c.properties.ApiLevel
Dan Albert705c84b2016-08-08 10:45:03 -0700293 ret.Class = "SHARED_LIBRARIES"
Dan Albert914449f2016-06-17 16:45:24 -0700294
Dan Albert914449f2016-06-17 16:45:24 -0700295 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Colin Crossb916a382016-07-29 17:28:03 -0700296 path, file := filepath.Split(c.installPath)
Dan Albert914449f2016-06-17 16:45:24 -0700297 stem := strings.TrimSuffix(file, filepath.Ext(file))
Dan Albertcdd4c242016-08-08 14:44:56 -0700298 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
299 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
Dan Albert914449f2016-06-17 16:45:24 -0700300 fmt.Fprintln(w, "LOCAL_MODULE_PATH := "+path)
301 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
Dan Willemsen866810d2017-04-04 14:13:45 -0700302 fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
Dan Albert914449f2016-06-17 16:45:24 -0700303
304 // Prevent make from installing the libraries to obj/lib (since we have
305 // dozens of libraries with the same name, they'll clobber each other
306 // and the real versions of the libraries from the platform).
307 fmt.Fprintln(w, "LOCAL_COPY_TO_INTERMEDIATE_LIBRARIES := false")
308 return nil
309 })
310}
Dan Willemsenb916b802017-03-19 13:44:32 -0700311
312func (c *llndkStubDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
313 ret.Class = "SHARED_LIBRARIES"
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700314 ret.SubName = ".vendor"
Dan Willemsenb916b802017-03-19 13:44:32 -0700315
316 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
317 c.libraryDecorator.androidMkWriteExportedFlags(w)
318
319 fmt.Fprintln(w, "LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE)"+outputFile.Ext())
320 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false")
321 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
322 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
323 fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
Dan Willemsen4416e5d2017-04-06 12:43:22 -0700324 fmt.Fprintln(w, "LOCAL_USE_VNDK := true")
Dan Willemsenb916b802017-03-19 13:44:32 -0700325
326 return nil
327 })
328}