blob: 7acc244b2145e65de0d4cae3915276b130babb8c [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")
Dan Willemsend2ede872016-11-18 14:54:24 -080062 } else if c.Target().Os == android.Android && c.Properties.Use_vndk {
63 fmt.Fprintln(w, "LOCAL_USE_VNDK := true")
64 fmt.Fprintln(w, "LOCAL_NDK_STL_VARIANT := none")
Dan Willemsena96ff642016-06-07 12:34:45 -070065 } else {
66 // These are already included in LOCAL_SHARED_LIBRARIES
67 fmt.Fprintln(w, "LOCAL_CXX_STL := none")
68 }
Colin Crossca860ac2016-01-04 14:34:37 -080069 return nil
70 })
71
Colin Crossca860ac2016-01-04 14:34:37 -080072 for _, feature := range c.features {
Colin Crossb916a382016-07-29 17:28:03 -070073 c.subAndroidMk(&ret, feature)
Colin Crossca860ac2016-01-04 14:34:37 -080074 }
75
Colin Crossb916a382016-07-29 17:28:03 -070076 c.subAndroidMk(&ret, c.compiler)
77 c.subAndroidMk(&ret, c.linker)
78 c.subAndroidMk(&ret, c.installer)
Colin Crossca860ac2016-01-04 14:34:37 -080079
80 return ret, nil
81}
82
Colin Crossb916a382016-07-29 17:28:03 -070083func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsend5781342017-02-15 16:15:21 -080084 writeExportedIncludes := func(w io.Writer) {
Dan Willemsen3b1fffa2016-05-05 15:11:48 -070085 var exportedIncludes []string
Colin Crossca860ac2016-01-04 14:34:37 -080086 for _, flag := range library.exportedFlags() {
Dan Willemsen3b1fffa2016-05-05 15:11:48 -070087 if strings.HasPrefix(flag, "-I") {
Dan Willemsen97750522016-02-09 17:43:51 -080088 exportedIncludes = append(exportedIncludes, strings.TrimPrefix(flag, "-I"))
89 }
Dan Willemsen218f6562015-07-08 18:13:11 -070090 }
91 if len(exportedIncludes) > 0 {
Dan Willemsen97750522016-02-09 17:43:51 -080092 fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DIRS :=", strings.Join(exportedIncludes, " "))
Dan Willemsen218f6562015-07-08 18:13:11 -070093 }
Dan Willemsen847dcc72016-09-29 12:13:36 -070094 exportedIncludeDeps := library.exportedFlagsDeps()
95 if len(exportedIncludeDeps) > 0 {
96 fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DEPS :=", strings.Join(exportedIncludeDeps.Strings(), " "))
97 }
Dan Willemsend5781342017-02-15 16:15:21 -080098 }
99
100 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")
129 }
130
131 writeExportedIncludes(w)
132 fmt.Fprintln(w, "include $(BUILD_HEADER_LIBRARY)")
133
134 return nil
135 }
136
137 return
138 }
139
140 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
141 writeExportedIncludes(w)
Dan Willemsen218f6562015-07-08 18:13:11 -0700142
Dan Willemsen1d577e22016-08-29 15:53:15 -0700143 fmt.Fprintln(w, "LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE)"+outputFile.Ext())
Dan Willemsen218f6562015-07-08 18:13:11 -0700144
Dan Willemsen97750522016-02-09 17:43:51 -0800145 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Dan Willemsen218f6562015-07-08 18:13:11 -0700146
Dan Willemsen581341d2017-02-09 16:16:31 -0800147 if library.coverageOutputFile.Valid() {
148 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", library.coverageOutputFile.String())
149 }
150
Dan Willemsen97750522016-02-09 17:43:51 -0800151 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800152 })
Colin Crossb916a382016-07-29 17:28:03 -0700153
Colin Cross4ac44802017-02-15 14:06:12 -0800154 if library.shared() {
Colin Crossb916a382016-07-29 17:28:03 -0700155 ctx.subAndroidMk(ret, library.baseInstaller)
156 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700157}
158
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700159func (object *objectLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Nan Zhang6d34b302017-02-04 17:47:46 -0800160 ret.Custom = func(w io.Writer, name, prefix, moduleDir string) error {
Colin Crossca860ac2016-01-04 14:34:37 -0800161 out := ret.OutputFile.Path()
Dan Willemsen218f6562015-07-08 18:13:11 -0700162
Dan Willemsen72d39932016-07-08 23:23:48 -0700163 fmt.Fprintln(w, "\n$("+prefix+"OUT_INTERMEDIATE_LIBRARIES)/"+name+objectExtension+":", out.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800164 fmt.Fprintln(w, "\t$(copy-file-to-target)")
165
166 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700167 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700168}
169
Colin Crossb916a382016-07-29 17:28:03 -0700170func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross1e7d3702016-08-24 15:25:47 -0700171 ctx.subAndroidMk(ret, binary.baseInstaller)
Colin Crossb916a382016-07-29 17:28:03 -0700172 ctx.subAndroidMk(ret, &binary.stripper)
Dan Willemsen7517ed02016-06-10 17:20:30 -0700173
Dan Willemsen218f6562015-07-08 18:13:11 -0700174 ret.Class = "EXECUTABLES"
Colin Cross635c3b02016-05-18 15:37:25 -0700175 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800176 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Colin Crossb916a382016-07-29 17:28:03 -0700177 if Bool(binary.Properties.Static_executable) {
Colin Cross3854a602016-01-11 12:49:11 -0800178 fmt.Fprintln(w, "LOCAL_FORCE_STATIC_EXECUTABLE := true")
179 }
Colin Cross9b09f242016-12-07 13:37:42 -0800180
181 if len(binary.symlinks) > 0 {
182 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS := "+strings.Join(binary.symlinks, " "))
183 }
184
Dan Willemsen581341d2017-02-09 16:16:31 -0800185 if binary.coverageOutputFile.Valid() {
186 fmt.Fprintln(w, "LOCAL_PREBUILT_COVERAGE_ARCHIVE :=", binary.coverageOutputFile.String())
187 }
Dan Willemsen97750522016-02-09 17:43:51 -0800188 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800189 })
190}
191
Colin Crossb916a382016-07-29 17:28:03 -0700192func (benchmark *benchmarkDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
193 ctx.subAndroidMk(ret, benchmark.binaryDecorator)
Colin Crossb916a382016-07-29 17:28:03 -0700194}
195
196func (test *testBinary) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
197 ctx.subAndroidMk(ret, test.binaryDecorator)
Dan Willemsen0fe72532017-01-17 13:25:49 -0800198 ret.Class = "NATIVE_TESTS"
Colin Crossb916a382016-07-29 17:28:03 -0700199 if Bool(test.Properties.Test_per_src) {
200 ret.SubName = "_" + test.binaryDecorator.Properties.Stem
Colin Crossa2344662016-03-24 13:14:12 -0700201 }
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800202
Colin Crossa929db02017-03-27 16:27:50 -0700203 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
204 if len(test.Properties.Test_suites) > 0 {
205 fmt.Fprintln(w, "LOCAL_COMPATIBILITY_SUITES :=",
206 strings.Join(test.Properties.Test_suites, " "))
207 }
208 return nil
209 })
210
Colin Crossfaeb7aa2017-02-01 14:12:44 -0800211 var testFiles []string
212 for _, d := range test.data {
213 rel := d.Rel()
214 path := d.String()
215 if !strings.HasSuffix(path, rel) {
216 panic(fmt.Errorf("path %q does not end with %q", path, rel))
217 }
218 path = strings.TrimSuffix(path, rel)
219 testFiles = append(testFiles, path+":"+rel)
220 }
221 if len(testFiles) > 0 {
222 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
223 fmt.Fprintln(w, "LOCAL_TEST_DATA := "+strings.Join(testFiles, " "))
224 return nil
225 })
226 }
Colin Crossa2344662016-03-24 13:14:12 -0700227}
228
Colin Crossb916a382016-07-29 17:28:03 -0700229func (test *testLibrary) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
230 ctx.subAndroidMk(ret, test.libraryDecorator)
231}
Dan Willemsene7932e82016-05-16 15:56:53 -0700232
Colin Crossb916a382016-07-29 17:28:03 -0700233func (library *toolchainLibraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
234 ret.Class = "STATIC_LIBRARIES"
Colin Cross635c3b02016-05-18 15:37:25 -0700235 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsene7932e82016-05-16 15:56:53 -0700236 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
Dan Willemsene7932e82016-05-16 15:56:53 -0700237 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
238
239 return nil
240 })
241}
242
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700243func (stripper *stripper) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
244 // Make only supports stripping target modules
245 if ctx.Target().Os != android.Android {
246 return
247 }
248
Dan Willemsen7517ed02016-06-10 17:20:30 -0700249 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
250 if stripper.StripProperties.Strip.None {
251 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false")
252 } else if stripper.StripProperties.Strip.Keep_symbols {
253 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := keep_symbols")
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700254 } else {
255 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := mini-debug-info")
Dan Willemsen7517ed02016-06-10 17:20:30 -0700256 }
257
258 return nil
259 })
260}
261
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700262func (packer *relocationPacker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
263 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
264 if packer.Properties.PackingRelocations {
265 fmt.Fprintln(w, "LOCAL_PACK_MODULE_RELOCATIONS := true")
266 }
267 return nil
268 })
269}
270
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700271func (installer *baseInstaller) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsen03ce63e2016-09-29 12:17:49 -0700272 // Soong installation is only supported for host modules. Have Make
273 // installation trigger Soong installation.
274 if ctx.Target().Os.Class == android.Host {
275 ret.OutputFile = android.OptionalPathForPath(installer.path)
276 }
277
Colin Cross635c3b02016-05-18 15:37:25 -0700278 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Colin Crossa2344662016-03-24 13:14:12 -0700279 path := installer.path.RelPathString()
Colin Crossbf305de2016-03-29 17:32:06 -0700280 dir, file := filepath.Split(path)
281 stem := strings.TrimSuffix(file, filepath.Ext(file))
Dan Willemsen1d577e22016-08-29 15:53:15 -0700282 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+filepath.Ext(file))
Colin Crosse14388b2016-05-03 14:08:40 -0700283 fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(OUT_DIR)/"+filepath.Clean(dir))
Colin Crossbf305de2016-03-29 17:32:06 -0700284 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
Colin Crossa2344662016-03-24 13:14:12 -0700285 return nil
286 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700287}
Dan Albert914449f2016-06-17 16:45:24 -0700288
Colin Crossb916a382016-07-29 17:28:03 -0700289func (c *stubDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800290 ret.SubName = "." + c.properties.ApiLevel
Dan Albert705c84b2016-08-08 10:45:03 -0700291 ret.Class = "SHARED_LIBRARIES"
Dan Albert914449f2016-06-17 16:45:24 -0700292
Dan Albert914449f2016-06-17 16:45:24 -0700293 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Colin Crossb916a382016-07-29 17:28:03 -0700294 path, file := filepath.Split(c.installPath)
Dan Albert914449f2016-06-17 16:45:24 -0700295 stem := strings.TrimSuffix(file, filepath.Ext(file))
Dan Albertcdd4c242016-08-08 14:44:56 -0700296 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
297 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
Dan Albert914449f2016-06-17 16:45:24 -0700298 fmt.Fprintln(w, "LOCAL_MODULE_PATH := "+path)
299 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
300
301 // Prevent make from installing the libraries to obj/lib (since we have
302 // dozens of libraries with the same name, they'll clobber each other
303 // and the real versions of the libraries from the platform).
304 fmt.Fprintln(w, "LOCAL_COPY_TO_INTERMEDIATE_LIBRARIES := false")
305 return nil
306 })
307}