blob: 676ae300c30e8f79ff9bdb6bc6a5bd192e179863 [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 Albert914449f2016-06-17 16:45:24 -070021 "strconv"
Dan Willemsen218f6562015-07-08 18:13:11 -070022 "strings"
23
Colin Cross635c3b02016-05-18 15:37:25 -070024 "android/soong/android"
Dan Willemsen218f6562015-07-08 18:13:11 -070025)
26
Dan Willemsenf2c27d72016-07-13 16:50:22 -070027type AndroidMkContext interface {
28 Target() android.Target
Colin Crossb916a382016-07-29 17:28:03 -070029 subAndroidMk(*android.AndroidMkData, interface{})
30}
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 Willemsena96ff642016-06-07 12:34:45 -070060 if c.Target().Os == android.Android && c.Properties.Sdk_version != "" {
61 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 }
Colin Crossca860ac2016-01-04 14:34:37 -080067 return nil
68 })
69
Colin Crossca860ac2016-01-04 14:34:37 -080070 for _, feature := range c.features {
Colin Crossb916a382016-07-29 17:28:03 -070071 c.subAndroidMk(&ret, feature)
Colin Crossca860ac2016-01-04 14:34:37 -080072 }
73
Colin Crossb916a382016-07-29 17:28:03 -070074 c.subAndroidMk(&ret, c.compiler)
75 c.subAndroidMk(&ret, c.linker)
76 c.subAndroidMk(&ret, c.installer)
Colin Crossca860ac2016-01-04 14:34:37 -080077
78 return ret, nil
79}
80
Colin Crossb916a382016-07-29 17:28:03 -070081func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
82 if !library.static() {
83 ctx.subAndroidMk(ret, &library.stripper)
Dan Willemsen394e9dc2016-09-14 15:04:48 -070084 ctx.subAndroidMk(ret, &library.relocationPacker)
Colin Crossb916a382016-07-29 17:28:03 -070085 }
86
Colin Crossca860ac2016-01-04 14:34:37 -080087 if library.static() {
Dan Willemsen218f6562015-07-08 18:13:11 -070088 ret.Class = "STATIC_LIBRARIES"
89 } else {
90 ret.Class = "SHARED_LIBRARIES"
91 }
Dan Willemsen7517ed02016-06-10 17:20:30 -070092
Colin Cross635c3b02016-05-18 15:37:25 -070093 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen3b1fffa2016-05-05 15:11:48 -070094 var exportedIncludes []string
Colin Crossca860ac2016-01-04 14:34:37 -080095 for _, flag := range library.exportedFlags() {
Dan Willemsen3b1fffa2016-05-05 15:11:48 -070096 if strings.HasPrefix(flag, "-I") {
Dan Willemsen97750522016-02-09 17:43:51 -080097 exportedIncludes = append(exportedIncludes, strings.TrimPrefix(flag, "-I"))
98 }
Dan Willemsen218f6562015-07-08 18:13:11 -070099 }
100 if len(exportedIncludes) > 0 {
Dan Willemsen97750522016-02-09 17:43:51 -0800101 fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DIRS :=", strings.Join(exportedIncludes, " "))
Dan Willemsen218f6562015-07-08 18:13:11 -0700102 }
103
Dan Willemsen1d577e22016-08-29 15:53:15 -0700104 fmt.Fprintln(w, "LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE)"+outputFile.Ext())
Dan Willemsen218f6562015-07-08 18:13:11 -0700105
Dan Willemsen97750522016-02-09 17:43:51 -0800106 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Dan Willemsen218f6562015-07-08 18:13:11 -0700107
Dan Willemsen97750522016-02-09 17:43:51 -0800108 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800109 })
Colin Crossb916a382016-07-29 17:28:03 -0700110
111 if !library.static() {
112 ctx.subAndroidMk(ret, library.baseInstaller)
113 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700114}
115
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700116func (object *objectLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsen97750522016-02-09 17:43:51 -0800117 ret.Custom = func(w io.Writer, name, prefix string) error {
Colin Crossca860ac2016-01-04 14:34:37 -0800118 out := ret.OutputFile.Path()
Dan Willemsen218f6562015-07-08 18:13:11 -0700119
Dan Willemsen72d39932016-07-08 23:23:48 -0700120 fmt.Fprintln(w, "\n$("+prefix+"OUT_INTERMEDIATE_LIBRARIES)/"+name+objectExtension+":", out.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800121 fmt.Fprintln(w, "\t$(copy-file-to-target)")
122
123 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700124 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700125}
126
Colin Crossb916a382016-07-29 17:28:03 -0700127func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross1e7d3702016-08-24 15:25:47 -0700128 ctx.subAndroidMk(ret, binary.baseInstaller)
Colin Crossb916a382016-07-29 17:28:03 -0700129 ctx.subAndroidMk(ret, &binary.stripper)
Dan Willemsen7517ed02016-06-10 17:20:30 -0700130
Dan Willemsen218f6562015-07-08 18:13:11 -0700131 ret.Class = "EXECUTABLES"
Colin Cross635c3b02016-05-18 15:37:25 -0700132 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800133 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Colin Crossb916a382016-07-29 17:28:03 -0700134 if Bool(binary.Properties.Static_executable) {
Colin Cross3854a602016-01-11 12:49:11 -0800135 fmt.Fprintln(w, "LOCAL_FORCE_STATIC_EXECUTABLE := true")
136 }
Dan Willemsen97750522016-02-09 17:43:51 -0800137 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800138 })
139}
140
Colin Crossb916a382016-07-29 17:28:03 -0700141func (benchmark *benchmarkDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
142 ctx.subAndroidMk(ret, benchmark.binaryDecorator)
Colin Crossb916a382016-07-29 17:28:03 -0700143}
144
145func (test *testBinary) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
146 ctx.subAndroidMk(ret, test.binaryDecorator)
Colin Crossb916a382016-07-29 17:28:03 -0700147 if Bool(test.Properties.Test_per_src) {
148 ret.SubName = "_" + test.binaryDecorator.Properties.Stem
Colin Crossa2344662016-03-24 13:14:12 -0700149 }
150}
151
Colin Crossb916a382016-07-29 17:28:03 -0700152func (test *testLibrary) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
153 ctx.subAndroidMk(ret, test.libraryDecorator)
154}
Dan Willemsene7932e82016-05-16 15:56:53 -0700155
Colin Crossb916a382016-07-29 17:28:03 -0700156func (library *toolchainLibraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
157 ret.Class = "STATIC_LIBRARIES"
Colin Cross635c3b02016-05-18 15:37:25 -0700158 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsene7932e82016-05-16 15:56:53 -0700159 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
Dan Willemsene7932e82016-05-16 15:56:53 -0700160 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
161
162 return nil
163 })
164}
165
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700166func (stripper *stripper) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
167 // Make only supports stripping target modules
168 if ctx.Target().Os != android.Android {
169 return
170 }
171
Dan Willemsen7517ed02016-06-10 17:20:30 -0700172 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
173 if stripper.StripProperties.Strip.None {
174 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false")
175 } else if stripper.StripProperties.Strip.Keep_symbols {
176 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := keep_symbols")
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700177 } else {
178 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := mini-debug-info")
Dan Willemsen7517ed02016-06-10 17:20:30 -0700179 }
180
181 return nil
182 })
183}
184
Dan Willemsen394e9dc2016-09-14 15:04:48 -0700185func (packer *relocationPacker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
186 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
187 if packer.Properties.PackingRelocations {
188 fmt.Fprintln(w, "LOCAL_PACK_MODULE_RELOCATIONS := true")
189 }
190 return nil
191 })
192}
193
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700194func (installer *baseInstaller) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsen03ce63e2016-09-29 12:17:49 -0700195 // Soong installation is only supported for host modules. Have Make
196 // installation trigger Soong installation.
197 if ctx.Target().Os.Class == android.Host {
198 ret.OutputFile = android.OptionalPathForPath(installer.path)
199 }
200
Colin Cross635c3b02016-05-18 15:37:25 -0700201 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Colin Crossa2344662016-03-24 13:14:12 -0700202 path := installer.path.RelPathString()
Colin Crossbf305de2016-03-29 17:32:06 -0700203 dir, file := filepath.Split(path)
204 stem := strings.TrimSuffix(file, filepath.Ext(file))
Dan Willemsen1d577e22016-08-29 15:53:15 -0700205 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+filepath.Ext(file))
Colin Crosse14388b2016-05-03 14:08:40 -0700206 fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(OUT_DIR)/"+filepath.Clean(dir))
Colin Crossbf305de2016-03-29 17:32:06 -0700207 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
Colin Cross3854a602016-01-11 12:49:11 -0800208 if len(installer.Properties.Symlinks) > 0 {
209 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS := "+strings.Join(installer.Properties.Symlinks, " "))
210 }
Colin Crossa2344662016-03-24 13:14:12 -0700211 return nil
212 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700213}
Dan Albert914449f2016-06-17 16:45:24 -0700214
Colin Crossb916a382016-07-29 17:28:03 -0700215func (c *stubDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Albert914449f2016-06-17 16:45:24 -0700216 ret.SubName = "." + strconv.Itoa(c.properties.ApiLevel)
Dan Albert705c84b2016-08-08 10:45:03 -0700217 ret.Class = "SHARED_LIBRARIES"
Dan Albert914449f2016-06-17 16:45:24 -0700218
Dan Albert914449f2016-06-17 16:45:24 -0700219 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Colin Crossb916a382016-07-29 17:28:03 -0700220 path, file := filepath.Split(c.installPath)
Dan Albert914449f2016-06-17 16:45:24 -0700221 stem := strings.TrimSuffix(file, filepath.Ext(file))
Dan Albertcdd4c242016-08-08 14:44:56 -0700222 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
223 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
Dan Albert914449f2016-06-17 16:45:24 -0700224 fmt.Fprintln(w, "LOCAL_MODULE_PATH := "+path)
225 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
226
227 // Prevent make from installing the libraries to obj/lib (since we have
228 // dozens of libraries with the same name, they'll clobber each other
229 // and the real versions of the libraries from the platform).
230 fmt.Fprintln(w, "LOCAL_COPY_TO_INTERMEDIATE_LIBRARIES := false")
231 return nil
232 })
233}