blob: df44a4c211e35a4d044ee0c5d5f187b0500b8bc7 [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
29}
30
Colin Cross635c3b02016-05-18 15:37:25 -070031func (c *Module) AndroidMk() (ret android.AndroidMkData, err error) {
Colin Crossbc6fb162016-05-24 15:39:04 -070032 if c.Properties.HideFromMake {
33 ret.Disabled = true
34 return ret, nil
35 }
36
Colin Crossca860ac2016-01-04 14:34:37 -080037 ret.OutputFile = c.outputFile
Colin Cross635c3b02016-05-18 15:37:25 -070038 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) (err error) {
Colin Cross30d5f512016-05-03 18:02:42 -070039 fmt.Fprintln(w, "LOCAL_SANITIZE := never")
Colin Crossc99deeb2016-04-11 15:06:20 -070040 if len(c.Properties.AndroidMkSharedLibs) > 0 {
41 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES := "+strings.Join(c.Properties.AndroidMkSharedLibs, " "))
Colin Crossca860ac2016-01-04 14:34:37 -080042 }
Dan Willemsena96ff642016-06-07 12:34:45 -070043 if c.Target().Os == android.Android && c.Properties.Sdk_version != "" {
44 fmt.Fprintln(w, "LOCAL_SDK_VERSION := "+c.Properties.Sdk_version)
45 fmt.Fprintln(w, "LOCAL_NDK_STL_VARIANT := none")
46 } else {
47 // These are already included in LOCAL_SHARED_LIBRARIES
48 fmt.Fprintln(w, "LOCAL_CXX_STL := none")
49 }
Colin Crossca860ac2016-01-04 14:34:37 -080050 return nil
51 })
52
53 callSubAndroidMk := func(obj interface{}) {
54 if obj != nil {
55 if androidmk, ok := obj.(interface {
Dan Willemsenf2c27d72016-07-13 16:50:22 -070056 AndroidMk(AndroidMkContext, *android.AndroidMkData)
Colin Crossca860ac2016-01-04 14:34:37 -080057 }); ok {
Dan Willemsenf2c27d72016-07-13 16:50:22 -070058 androidmk.AndroidMk(c, &ret)
Colin Crossca860ac2016-01-04 14:34:37 -080059 }
60 }
61 }
62
63 for _, feature := range c.features {
64 callSubAndroidMk(feature)
65 }
66
67 callSubAndroidMk(c.compiler)
68 callSubAndroidMk(c.linker)
Colin Crossc99deeb2016-04-11 15:06:20 -070069 if c.linker.installable() {
70 callSubAndroidMk(c.installer)
71 }
Colin Crossca860ac2016-01-04 14:34:37 -080072
73 return ret, nil
74}
75
Dan Willemsenf2c27d72016-07-13 16:50:22 -070076func (library *baseLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Crossca860ac2016-01-04 14:34:37 -080077 if library.static() {
Dan Willemsen218f6562015-07-08 18:13:11 -070078 ret.Class = "STATIC_LIBRARIES"
79 } else {
80 ret.Class = "SHARED_LIBRARIES"
81 }
Colin Crossca860ac2016-01-04 14:34:37 -080082}
83
Dan Willemsenf2c27d72016-07-13 16:50:22 -070084func (library *libraryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
85 library.baseLinker.AndroidMk(ctx, ret)
Colin Crossca860ac2016-01-04 14:34:37 -080086
Colin Cross7b5c22b2016-07-07 11:16:20 -070087 if !library.static() {
Dan Willemsenf2c27d72016-07-13 16:50:22 -070088 library.stripper.AndroidMk(ctx, ret)
Colin Cross7b5c22b2016-07-07 11:16:20 -070089 }
Dan Willemsen7517ed02016-06-10 17:20:30 -070090
Colin Cross635c3b02016-05-18 15:37:25 -070091 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen3b1fffa2016-05-05 15:11:48 -070092 var exportedIncludes []string
Colin Crossca860ac2016-01-04 14:34:37 -080093 for _, flag := range library.exportedFlags() {
Dan Willemsen3b1fffa2016-05-05 15:11:48 -070094 if strings.HasPrefix(flag, "-I") {
Dan Willemsen97750522016-02-09 17:43:51 -080095 exportedIncludes = append(exportedIncludes, strings.TrimPrefix(flag, "-I"))
96 }
Dan Willemsen218f6562015-07-08 18:13:11 -070097 }
98 if len(exportedIncludes) > 0 {
Dan Willemsen97750522016-02-09 17:43:51 -080099 fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DIRS :=", strings.Join(exportedIncludes, " "))
Dan Willemsen218f6562015-07-08 18:13:11 -0700100 }
101
Colin Crossca860ac2016-01-04 14:34:37 -0800102 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
Dan Willemsen648c8ae2016-07-21 16:42:14 -0700103 fmt.Fprintln(w, "LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE)$(LOCAL_MODULE_SUFFIX)")
Dan Willemsen218f6562015-07-08 18:13:11 -0700104
Dan Willemsen97750522016-02-09 17:43:51 -0800105 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Dan Willemsen218f6562015-07-08 18:13:11 -0700106
Dan Willemsen97750522016-02-09 17:43:51 -0800107 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800108 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700109}
110
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700111func (object *objectLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsen97750522016-02-09 17:43:51 -0800112 ret.Custom = func(w io.Writer, name, prefix string) error {
Colin Crossca860ac2016-01-04 14:34:37 -0800113 out := ret.OutputFile.Path()
Dan Willemsen218f6562015-07-08 18:13:11 -0700114
Dan Willemsen72d39932016-07-08 23:23:48 -0700115 fmt.Fprintln(w, "\n$("+prefix+"OUT_INTERMEDIATE_LIBRARIES)/"+name+objectExtension+":", out.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800116 fmt.Fprintln(w, "\t$(copy-file-to-target)")
117
118 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700119 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700120}
121
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700122func (binary *binaryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
123 binary.stripper.AndroidMk(ctx, ret)
Dan Willemsen7517ed02016-06-10 17:20:30 -0700124
Dan Willemsen218f6562015-07-08 18:13:11 -0700125 ret.Class = "EXECUTABLES"
Colin Cross635c3b02016-05-18 15:37:25 -0700126 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800127 fmt.Fprintln(w, "LOCAL_CXX_STL := none")
128 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Colin Cross3854a602016-01-11 12:49:11 -0800129 if binary.static() {
130 fmt.Fprintln(w, "LOCAL_FORCE_STATIC_EXECUTABLE := true")
131 }
Dan Willemsen97750522016-02-09 17:43:51 -0800132 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800133 })
134}
135
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700136func (test *testBinaryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
137 test.binaryLinker.AndroidMk(ctx, ret)
Colin Crossc7a38dc2016-07-12 13:13:09 -0700138 if Bool(test.testLinker.Properties.Test_per_src) {
Dan Albert6a047692016-07-18 17:24:47 -0700139 ret.SubName = "_" + test.binaryLinker.Properties.Stem
Colin Crossa2344662016-03-24 13:14:12 -0700140 }
141}
142
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700143func (library *toolchainLibraryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
144 library.baseLinker.AndroidMk(ctx, ret)
Dan Willemsene7932e82016-05-16 15:56:53 -0700145
Colin Cross635c3b02016-05-18 15:37:25 -0700146 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsene7932e82016-05-16 15:56:53 -0700147 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
148 fmt.Fprintln(w, "LOCAL_CXX_STL := none")
149 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
150
151 return nil
152 })
153}
154
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700155func (stripper *stripper) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
156 // Make only supports stripping target modules
157 if ctx.Target().Os != android.Android {
158 return
159 }
160
Dan Willemsen7517ed02016-06-10 17:20:30 -0700161 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
162 if stripper.StripProperties.Strip.None {
163 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false")
164 } else if stripper.StripProperties.Strip.Keep_symbols {
165 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := keep_symbols")
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700166 } else {
167 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := mini-debug-info")
Dan Willemsen7517ed02016-06-10 17:20:30 -0700168 }
169
170 return nil
171 })
172}
173
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700174func (installer *baseInstaller) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross635c3b02016-05-18 15:37:25 -0700175 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Colin Crossa2344662016-03-24 13:14:12 -0700176 path := installer.path.RelPathString()
Colin Crossbf305de2016-03-29 17:32:06 -0700177 dir, file := filepath.Split(path)
178 stem := strings.TrimSuffix(file, filepath.Ext(file))
Colin Crosse14388b2016-05-03 14:08:40 -0700179 fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(OUT_DIR)/"+filepath.Clean(dir))
Colin Crossbf305de2016-03-29 17:32:06 -0700180 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
Colin Cross3854a602016-01-11 12:49:11 -0800181 if len(installer.Properties.Symlinks) > 0 {
182 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS := "+strings.Join(installer.Properties.Symlinks, " "))
183 }
Colin Crossa2344662016-03-24 13:14:12 -0700184 return nil
185 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700186}
Dan Albert914449f2016-06-17 16:45:24 -0700187
188func (c *stubCompiler) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
189 ret.SubName = "." + strconv.Itoa(c.properties.ApiLevel)
190}
191
192func (installer *stubInstaller) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
193 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
194 path, file := filepath.Split(installer.installPath)
195 stem := strings.TrimSuffix(file, filepath.Ext(file))
196 fmt.Fprintln(w, "LOCAL_MODULE_PATH := "+path)
197 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
198
199 // Prevent make from installing the libraries to obj/lib (since we have
200 // dozens of libraries with the same name, they'll clobber each other
201 // and the real versions of the libraries from the platform).
202 fmt.Fprintln(w, "LOCAL_COPY_TO_INTERMEDIATE_LIBRARIES := false")
203 return nil
204 })
205}