blob: fa7ce425358bb63e34618e6e8ddb3d7a67e98816 [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{})
29}
30
31type subAndroidMkProvider interface {
32 AndroidMk(AndroidMkContext, *android.AndroidMkData)
33}
34
35func (c *Module) subAndroidMk(data *android.AndroidMkData, obj interface{}) {
36 if c.subAndroidMkOnce == nil {
37 c.subAndroidMkOnce = make(map[subAndroidMkProvider]bool)
38 }
39 if androidmk, ok := obj.(subAndroidMkProvider); ok {
40 if !c.subAndroidMkOnce[androidmk] {
41 c.subAndroidMkOnce[androidmk] = true
42 androidmk.AndroidMk(c, data)
43 }
44 }
Dan Willemsenf2c27d72016-07-13 16:50:22 -070045}
46
Colin Cross635c3b02016-05-18 15:37:25 -070047func (c *Module) AndroidMk() (ret android.AndroidMkData, err error) {
Colin Crossbc6fb162016-05-24 15:39:04 -070048 if c.Properties.HideFromMake {
49 ret.Disabled = true
50 return ret, nil
51 }
52
Colin Crossca860ac2016-01-04 14:34:37 -080053 ret.OutputFile = c.outputFile
Colin Cross635c3b02016-05-18 15:37:25 -070054 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) (err error) {
Colin Cross30d5f512016-05-03 18:02:42 -070055 fmt.Fprintln(w, "LOCAL_SANITIZE := never")
Colin Crossc99deeb2016-04-11 15:06:20 -070056 if len(c.Properties.AndroidMkSharedLibs) > 0 {
57 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES := "+strings.Join(c.Properties.AndroidMkSharedLibs, " "))
Colin Crossca860ac2016-01-04 14:34:37 -080058 }
Dan Willemsena96ff642016-06-07 12:34:45 -070059 if c.Target().Os == android.Android && c.Properties.Sdk_version != "" {
60 fmt.Fprintln(w, "LOCAL_SDK_VERSION := "+c.Properties.Sdk_version)
61 fmt.Fprintln(w, "LOCAL_NDK_STL_VARIANT := none")
62 } else {
63 // These are already included in LOCAL_SHARED_LIBRARIES
64 fmt.Fprintln(w, "LOCAL_CXX_STL := none")
65 }
Colin Crossca860ac2016-01-04 14:34:37 -080066 return nil
67 })
68
Colin Crossca860ac2016-01-04 14:34:37 -080069 for _, feature := range c.features {
Colin Crossb916a382016-07-29 17:28:03 -070070 c.subAndroidMk(&ret, feature)
Colin Crossca860ac2016-01-04 14:34:37 -080071 }
72
Colin Crossb916a382016-07-29 17:28:03 -070073 c.subAndroidMk(&ret, c.compiler)
74 c.subAndroidMk(&ret, c.linker)
75 c.subAndroidMk(&ret, c.installer)
Colin Crossca860ac2016-01-04 14:34:37 -080076
77 return ret, nil
78}
79
Dan Willemsen58539402017-03-19 13:44:32 -070080func (library *libraryDecorator) androidMkWriteExportedFlags(w io.Writer) {
81 exportedFlags := library.exportedFlags()
82 if len(exportedFlags) > 0 {
83 fmt.Fprintln(w, "LOCAL_EXPORT_CFLAGS :=", strings.Join(exportedFlags, " "))
Dan Willemsend5781342017-02-15 16:15:21 -080084 }
Dan Willemsen58539402017-03-19 13:44:32 -070085 exportedFlagsDeps := library.exportedFlagsDeps()
86 if len(exportedFlagsDeps) > 0 {
87 fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DEPS :=", strings.Join(exportedFlagsDeps.Strings(), " "))
88 }
89}
Dan Willemsend5781342017-02-15 16:15:21 -080090
Dan Willemsen58539402017-03-19 13:44:32 -070091func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsend5781342017-02-15 16:15:21 -080092 if library.static() {
93 ret.Class = "STATIC_LIBRARIES"
94 } else if library.shared() {
95 ctx.subAndroidMk(ret, &library.stripper)
96 ctx.subAndroidMk(ret, &library.relocationPacker)
97
98 ret.Class = "SHARED_LIBRARIES"
99 } else if library.header() {
100 ret.Custom = func(w io.Writer, name, prefix, moduleDir string) error {
101 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
102 fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
103 fmt.Fprintln(w, "LOCAL_MODULE :=", name)
104
105 archStr := ctx.Target().Arch.ArchType.String()
106 var host bool
107 switch ctx.Target().Os.Class {
108 case android.Host:
109 fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH := ", archStr)
110 host = true
111 case android.HostCross:
112 fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH := ", archStr)
113 host = true
114 case android.Device:
115 fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH := ", archStr)
116 }
117
118 if host {
119 fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", ctx.Target().Os.String())
120 fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true")
121 }
122
Dan Willemsen58539402017-03-19 13:44:32 -0700123 library.androidMkWriteExportedFlags(w)
Dan Willemsend5781342017-02-15 16:15:21 -0800124 fmt.Fprintln(w, "include $(BUILD_HEADER_LIBRARY)")
125
126 return nil
127 }
128
129 return
130 }
131
132 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen58539402017-03-19 13:44:32 -0700133 library.androidMkWriteExportedFlags(w)
Dan Willemsen218f6562015-07-08 18:13:11 -0700134
Dan Willemsen1d577e22016-08-29 15:53:15 -0700135 fmt.Fprintln(w, "LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE)"+outputFile.Ext())
Dan Willemsen218f6562015-07-08 18:13:11 -0700136
Dan Willemsen97750522016-02-09 17:43:51 -0800137 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Dan Willemsen218f6562015-07-08 18:13:11 -0700138
Dan Willemsen581341d2017-02-09 16:16:31 -0800139 if library.coverageOutputFile.Valid() {
140 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", library.coverageOutputFile.String())
141 }
142
Dan Willemsen97750522016-02-09 17:43:51 -0800143 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800144 })
Colin Crossb916a382016-07-29 17:28:03 -0700145
Colin Cross4ac44802017-02-15 14:06:12 -0800146 if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700147 ctx.subAndroidMk(ret, library.baseInstaller)
148 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700149}
150
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700151func (object *objectLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Nan Zhang6d34b302017-02-04 17:47:46 -0800152 ret.Custom = func(w io.Writer, name, prefix, moduleDir string) error {
Colin Crossca860ac2016-01-04 14:34:37 -0800153 out := ret.OutputFile.Path()
Dan Willemsen218f6562015-07-08 18:13:11 -0700154
Dan Willemsen72d39932016-07-08 23:23:48 -0700155 fmt.Fprintln(w, "\n$("+prefix+"OUT_INTERMEDIATE_LIBRARIES)/"+name+objectExtension+":", out.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800156 fmt.Fprintln(w, "\t$(copy-file-to-target)")
157
158 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700159 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700160}
161
Colin Crossb916a382016-07-29 17:28:03 -0700162func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross1e7d3702016-08-24 15:25:47 -0700163 ctx.subAndroidMk(ret, binary.baseInstaller)
Colin Crossb916a382016-07-29 17:28:03 -0700164 ctx.subAndroidMk(ret, &binary.stripper)
Dan Willemsen7517ed02016-06-10 17:20:30 -0700165
Dan Willemsen218f6562015-07-08 18:13:11 -0700166 ret.Class = "EXECUTABLES"
Colin Cross635c3b02016-05-18 15:37:25 -0700167 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800168 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Colin Crossb916a382016-07-29 17:28:03 -0700169 if Bool(binary.Properties.Static_executable) {
Colin Cross3854a602016-01-11 12:49:11 -0800170 fmt.Fprintln(w, "LOCAL_FORCE_STATIC_EXECUTABLE := true")
171 }
Colin Cross9b09f242016-12-07 13:37:42 -0800172
173 if len(binary.symlinks) > 0 {
174 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS := "+strings.Join(binary.symlinks, " "))
175 }
176
Dan Willemsen581341d2017-02-09 16:16:31 -0800177 if binary.coverageOutputFile.Valid() {
178 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", binary.coverageOutputFile.String())
179 }
Dan Willemsen97750522016-02-09 17:43:51 -0800180 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800181 })
182}
183
Colin Crossb916a382016-07-29 17:28:03 -0700184func (benchmark *benchmarkDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
185 ctx.subAndroidMk(ret, benchmark.binaryDecorator)
Colin Crossb916a382016-07-29 17:28:03 -0700186}
187
188func (test *testBinary) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
189 ctx.subAndroidMk(ret, test.binaryDecorator)
Dan Willemsen0fe72532017-01-17 13:25:49 -0800190 ret.Class = "NATIVE_TESTS"
Colin Crossb916a382016-07-29 17:28:03 -0700191 if Bool(test.Properties.Test_per_src) {
192 ret.SubName = "_" + test.binaryDecorator.Properties.Stem
Colin Crossa2344662016-03-24 13:14:12 -0700193 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800194
Colin Crossa929db02017-03-27 16:27:50 -0700195 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
196 if len(test.Properties.Test_suites) > 0 {
Dan Shi4df566d2017-04-03 12:29:25 -0700197 fmt.Fprintln(w, "LOCAL_COMPATIBILITY_SUITE :=",
Colin Crossa929db02017-03-27 16:27:50 -0700198 strings.Join(test.Properties.Test_suites, " "))
199 }
200 return nil
201 })
202
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800203 var testFiles []string
204 for _, d := range test.data {
205 rel := d.Rel()
206 path := d.String()
207 if !strings.HasSuffix(path, rel) {
208 panic(fmt.Errorf("path %q does not end with %q", path, rel))
209 }
210 path = strings.TrimSuffix(path, rel)
211 testFiles = append(testFiles, path+":"+rel)
212 }
213 if len(testFiles) > 0 {
214 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
215 fmt.Fprintln(w, "LOCAL_TEST_DATA := "+strings.Join(testFiles, " "))
216 return nil
217 })
218 }
Colin Crossa2344662016-03-24 13:14:12 -0700219}
220
Colin Crossb916a382016-07-29 17:28:03 -0700221func (test *testLibrary) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
222 ctx.subAndroidMk(ret, test.libraryDecorator)
223}
Dan Willemsene7932e82016-05-16 15:56:53 -0700224
Colin Crossb916a382016-07-29 17:28:03 -0700225func (library *toolchainLibraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
226 ret.Class = "STATIC_LIBRARIES"
Colin Cross635c3b02016-05-18 15:37:25 -0700227 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsene7932e82016-05-16 15:56:53 -0700228 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
Dan Willemsene7932e82016-05-16 15:56:53 -0700229 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
230
231 return nil
232 })
233}
234
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700235func (stripper *stripper) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
236 // Make only supports stripping target modules
237 if ctx.Target().Os != android.Android {
238 return
239 }
240
Dan Willemsen7517ed02016-06-10 17:20:30 -0700241 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
242 if stripper.StripProperties.Strip.None {
243 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false")
244 } else if stripper.StripProperties.Strip.Keep_symbols {
245 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := keep_symbols")
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700246 } else {
247 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := mini-debug-info")
Dan Willemsen7517ed02016-06-10 17:20:30 -0700248 }
249
250 return nil
251 })
252}
253
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700254func (packer *relocationPacker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
255 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
256 if packer.Properties.PackingRelocations {
257 fmt.Fprintln(w, "LOCAL_PACK_MODULE_RELOCATIONS := true")
258 }
259 return nil
260 })
261}
262
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700263func (installer *baseInstaller) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsen03ce63e2016-09-29 12:17:49 -0700264 // Soong installation is only supported for host modules. Have Make
265 // installation trigger Soong installation.
266 if ctx.Target().Os.Class == android.Host {
267 ret.OutputFile = android.OptionalPathForPath(installer.path)
268 }
269
Colin Cross635c3b02016-05-18 15:37:25 -0700270 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Colin Crossa2344662016-03-24 13:14:12 -0700271 path := installer.path.RelPathString()
Colin Crossbf305de2016-03-29 17:32:06 -0700272 dir, file := filepath.Split(path)
273 stem := strings.TrimSuffix(file, filepath.Ext(file))
Dan Willemsen1d577e22016-08-29 15:53:15 -0700274 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+filepath.Ext(file))
Colin Crosse14388b2016-05-03 14:08:40 -0700275 fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(OUT_DIR)/"+filepath.Clean(dir))
Colin Crossbf305de2016-03-29 17:32:06 -0700276 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
Colin Crossa2344662016-03-24 13:14:12 -0700277 return nil
278 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700279}
Dan Albert914449f2016-06-17 16:45:24 -0700280
Colin Crossb916a382016-07-29 17:28:03 -0700281func (c *stubDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsen01a90592017-04-07 15:21:13 -0700282 ret.SubName = ndkLibrarySuffix + "." + c.properties.ApiLevel
Dan Albert705c84b2016-08-08 10:45:03 -0700283 ret.Class = "SHARED_LIBRARIES"
Dan Albert914449f2016-06-17 16:45:24 -0700284
Dan Albert914449f2016-06-17 16:45:24 -0700285 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Colin Crossb916a382016-07-29 17:28:03 -0700286 path, file := filepath.Split(c.installPath)
Dan Albert914449f2016-06-17 16:45:24 -0700287 stem := strings.TrimSuffix(file, filepath.Ext(file))
Dan Albertcdd4c242016-08-08 14:44:56 -0700288 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
289 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
Dan Albert914449f2016-06-17 16:45:24 -0700290 fmt.Fprintln(w, "LOCAL_MODULE_PATH := "+path)
291 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
Dan Willemsen866810d2017-04-04 14:13:45 -0700292 fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
Dan Albert914449f2016-06-17 16:45:24 -0700293
294 // Prevent make from installing the libraries to obj/lib (since we have
295 // dozens of libraries with the same name, they'll clobber each other
296 // and the real versions of the libraries from the platform).
297 fmt.Fprintln(w, "LOCAL_COPY_TO_INTERMEDIATE_LIBRARIES := false")
298 return nil
299 })
300}
Dan Willemsenb916b802017-03-19 13:44:32 -0700301
302func (c *llndkStubDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
303 ret.Class = "SHARED_LIBRARIES"
Dan Willemsen01a90592017-04-07 15:21:13 -0700304 ret.SubName = llndkLibrarySuffix
Dan Willemsenb916b802017-03-19 13:44:32 -0700305
306 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
307 c.libraryDecorator.androidMkWriteExportedFlags(w)
308
309 fmt.Fprintln(w, "LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE)"+outputFile.Ext())
310 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false")
311 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
312 fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true")
313 fmt.Fprintln(w, "LOCAL_NO_NOTICE_FILE := true")
314
315 return nil
316 })
317}