blob: 380babcbc5a7b2ff83a93d7dbbe18d914a246800 [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) {
84 if !library.static() {
85 ctx.subAndroidMk(ret, &library.stripper)
Dan Willemsen394e9dc2016-09-14 15:04:48 -070086 ctx.subAndroidMk(ret, &library.relocationPacker)
Colin Crossb916a382016-07-29 17:28:03 -070087 }
88
Colin Crossca860ac2016-01-04 14:34:37 -080089 if library.static() {
Dan Willemsen218f6562015-07-08 18:13:11 -070090 ret.Class = "STATIC_LIBRARIES"
91 } else {
92 ret.Class = "SHARED_LIBRARIES"
93 }
Dan Willemsen7517ed02016-06-10 17:20:30 -070094
Colin Cross635c3b02016-05-18 15:37:25 -070095 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen3b1fffa2016-05-05 15:11:48 -070096 var exportedIncludes []string
Colin Crossca860ac2016-01-04 14:34:37 -080097 for _, flag := range library.exportedFlags() {
Dan Willemsen3b1fffa2016-05-05 15:11:48 -070098 if strings.HasPrefix(flag, "-I") {
Dan Willemsen97750522016-02-09 17:43:51 -080099 exportedIncludes = append(exportedIncludes, strings.TrimPrefix(flag, "-I"))
100 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700101 }
102 if len(exportedIncludes) > 0 {
Dan Willemsen97750522016-02-09 17:43:51 -0800103 fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DIRS :=", strings.Join(exportedIncludes, " "))
Dan Willemsen218f6562015-07-08 18:13:11 -0700104 }
Dan Willemsen847dcc72016-09-29 12:13:36 -0700105 exportedIncludeDeps := library.exportedFlagsDeps()
106 if len(exportedIncludeDeps) > 0 {
107 fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DEPS :=", strings.Join(exportedIncludeDeps.Strings(), " "))
108 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700109
Dan Willemsen1d577e22016-08-29 15:53:15 -0700110 fmt.Fprintln(w, "LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE)"+outputFile.Ext())
Dan Willemsen218f6562015-07-08 18:13:11 -0700111
Dan Willemsen97750522016-02-09 17:43:51 -0800112 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Dan Willemsen218f6562015-07-08 18:13:11 -0700113
Dan Willemsen97750522016-02-09 17:43:51 -0800114 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800115 })
Colin Crossb916a382016-07-29 17:28:03 -0700116
117 if !library.static() {
118 ctx.subAndroidMk(ret, library.baseInstaller)
119 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700120}
121
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700122func (object *objectLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsen97750522016-02-09 17:43:51 -0800123 ret.Custom = func(w io.Writer, name, prefix string) error {
Colin Crossca860ac2016-01-04 14:34:37 -0800124 out := ret.OutputFile.Path()
Dan Willemsen218f6562015-07-08 18:13:11 -0700125
Dan Willemsen72d39932016-07-08 23:23:48 -0700126 fmt.Fprintln(w, "\n$("+prefix+"OUT_INTERMEDIATE_LIBRARIES)/"+name+objectExtension+":", out.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800127 fmt.Fprintln(w, "\t$(copy-file-to-target)")
128
129 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700130 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700131}
132
Colin Crossb916a382016-07-29 17:28:03 -0700133func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross1e7d3702016-08-24 15:25:47 -0700134 ctx.subAndroidMk(ret, binary.baseInstaller)
Colin Crossb916a382016-07-29 17:28:03 -0700135 ctx.subAndroidMk(ret, &binary.stripper)
Dan Willemsen7517ed02016-06-10 17:20:30 -0700136
Dan Willemsen218f6562015-07-08 18:13:11 -0700137 ret.Class = "EXECUTABLES"
Colin Cross635c3b02016-05-18 15:37:25 -0700138 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800139 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Colin Crossb916a382016-07-29 17:28:03 -0700140 if Bool(binary.Properties.Static_executable) {
Colin Cross3854a602016-01-11 12:49:11 -0800141 fmt.Fprintln(w, "LOCAL_FORCE_STATIC_EXECUTABLE := true")
142 }
Colin Cross9b09f242016-12-07 13:37:42 -0800143
144 if len(binary.symlinks) > 0 {
145 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS := "+strings.Join(binary.symlinks, " "))
146 }
147
Dan Willemsen97750522016-02-09 17:43:51 -0800148 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800149 })
150}
151
Colin Crossb916a382016-07-29 17:28:03 -0700152func (benchmark *benchmarkDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
153 ctx.subAndroidMk(ret, benchmark.binaryDecorator)
Colin Crossb916a382016-07-29 17:28:03 -0700154}
155
156func (test *testBinary) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
157 ctx.subAndroidMk(ret, test.binaryDecorator)
Colin Crossb916a382016-07-29 17:28:03 -0700158 if Bool(test.Properties.Test_per_src) {
159 ret.SubName = "_" + test.binaryDecorator.Properties.Stem
Colin Crossa2344662016-03-24 13:14:12 -0700160 }
161}
162
Colin Crossb916a382016-07-29 17:28:03 -0700163func (test *testLibrary) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
164 ctx.subAndroidMk(ret, test.libraryDecorator)
165}
Dan Willemsene7932e82016-05-16 15:56:53 -0700166
Colin Crossb916a382016-07-29 17:28:03 -0700167func (library *toolchainLibraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
168 ret.Class = "STATIC_LIBRARIES"
Colin Cross635c3b02016-05-18 15:37:25 -0700169 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsene7932e82016-05-16 15:56:53 -0700170 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
Dan Willemsene7932e82016-05-16 15:56:53 -0700171 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
172
173 return nil
174 })
175}
176
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700177func (stripper *stripper) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
178 // Make only supports stripping target modules
179 if ctx.Target().Os != android.Android {
180 return
181 }
182
Dan Willemsen7517ed02016-06-10 17:20:30 -0700183 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
184 if stripper.StripProperties.Strip.None {
185 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false")
186 } else if stripper.StripProperties.Strip.Keep_symbols {
187 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := keep_symbols")
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700188 } else {
189 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := mini-debug-info")
Dan Willemsen7517ed02016-06-10 17:20:30 -0700190 }
191
192 return nil
193 })
194}
195
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700196func (packer *relocationPacker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
197 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
198 if packer.Properties.PackingRelocations {
199 fmt.Fprintln(w, "LOCAL_PACK_MODULE_RELOCATIONS := true")
200 }
201 return nil
202 })
203}
204
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700205func (installer *baseInstaller) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsen03ce63e2016-09-29 12:17:49 -0700206 // Soong installation is only supported for host modules. Have Make
207 // installation trigger Soong installation.
208 if ctx.Target().Os.Class == android.Host {
209 ret.OutputFile = android.OptionalPathForPath(installer.path)
210 }
211
Colin Cross635c3b02016-05-18 15:37:25 -0700212 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Colin Crossa2344662016-03-24 13:14:12 -0700213 path := installer.path.RelPathString()
Colin Crossbf305de2016-03-29 17:32:06 -0700214 dir, file := filepath.Split(path)
215 stem := strings.TrimSuffix(file, filepath.Ext(file))
Dan Willemsen1d577e22016-08-29 15:53:15 -0700216 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+filepath.Ext(file))
Colin Crosse14388b2016-05-03 14:08:40 -0700217 fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(OUT_DIR)/"+filepath.Clean(dir))
Colin Crossbf305de2016-03-29 17:32:06 -0700218 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
Colin Crossa2344662016-03-24 13:14:12 -0700219 return nil
220 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700221}
Dan Albert914449f2016-06-17 16:45:24 -0700222
Colin Crossb916a382016-07-29 17:28:03 -0700223func (c *stubDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Albertfd86e9e2016-11-08 13:35:12 -0800224 ret.SubName = "." + c.properties.ApiLevel
Dan Albert705c84b2016-08-08 10:45:03 -0700225 ret.Class = "SHARED_LIBRARIES"
Dan Albert914449f2016-06-17 16:45:24 -0700226
Dan Albert914449f2016-06-17 16:45:24 -0700227 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Colin Crossb916a382016-07-29 17:28:03 -0700228 path, file := filepath.Split(c.installPath)
Dan Albert914449f2016-06-17 16:45:24 -0700229 stem := strings.TrimSuffix(file, filepath.Ext(file))
Dan Albertcdd4c242016-08-08 14:44:56 -0700230 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
231 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
Dan Albert914449f2016-06-17 16:45:24 -0700232 fmt.Fprintln(w, "LOCAL_MODULE_PATH := "+path)
233 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
234
235 // Prevent make from installing the libraries to obj/lib (since we have
236 // dozens of libraries with the same name, they'll clobber each other
237 // and the real versions of the libraries from the platform).
238 fmt.Fprintln(w, "LOCAL_COPY_TO_INTERMEDIATE_LIBRARIES := false")
239 return nil
240 })
241}