blob: 4f76dc9e311bbe365b371b539bd1a60afb9c643a [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
28}
29
Colin Cross635c3b02016-05-18 15:37:25 -070030func (c *Module) AndroidMk() (ret android.AndroidMkData, err error) {
Colin Crossbc6fb162016-05-24 15:39:04 -070031 if c.Properties.HideFromMake {
32 ret.Disabled = true
33 return ret, nil
34 }
35
Colin Crossca860ac2016-01-04 14:34:37 -080036 ret.OutputFile = c.outputFile
Colin Cross635c3b02016-05-18 15:37:25 -070037 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) (err error) {
Colin Cross30d5f512016-05-03 18:02:42 -070038 fmt.Fprintln(w, "LOCAL_SANITIZE := never")
Colin Crossc99deeb2016-04-11 15:06:20 -070039 if len(c.Properties.AndroidMkSharedLibs) > 0 {
40 fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES := "+strings.Join(c.Properties.AndroidMkSharedLibs, " "))
Colin Crossca860ac2016-01-04 14:34:37 -080041 }
Dan Willemsena96ff642016-06-07 12:34:45 -070042 if c.Target().Os == android.Android && c.Properties.Sdk_version != "" {
43 fmt.Fprintln(w, "LOCAL_SDK_VERSION := "+c.Properties.Sdk_version)
44 fmt.Fprintln(w, "LOCAL_NDK_STL_VARIANT := none")
45 } else {
46 // These are already included in LOCAL_SHARED_LIBRARIES
47 fmt.Fprintln(w, "LOCAL_CXX_STL := none")
48 }
Colin Crossca860ac2016-01-04 14:34:37 -080049 return nil
50 })
51
52 callSubAndroidMk := func(obj interface{}) {
53 if obj != nil {
54 if androidmk, ok := obj.(interface {
Dan Willemsenf2c27d72016-07-13 16:50:22 -070055 AndroidMk(AndroidMkContext, *android.AndroidMkData)
Colin Crossca860ac2016-01-04 14:34:37 -080056 }); ok {
Dan Willemsenf2c27d72016-07-13 16:50:22 -070057 androidmk.AndroidMk(c, &ret)
Colin Crossca860ac2016-01-04 14:34:37 -080058 }
59 }
60 }
61
62 for _, feature := range c.features {
63 callSubAndroidMk(feature)
64 }
65
66 callSubAndroidMk(c.compiler)
67 callSubAndroidMk(c.linker)
Colin Crossc99deeb2016-04-11 15:06:20 -070068 if c.linker.installable() {
69 callSubAndroidMk(c.installer)
70 }
Colin Crossca860ac2016-01-04 14:34:37 -080071
72 return ret, nil
73}
74
Dan Willemsenf2c27d72016-07-13 16:50:22 -070075func (library *baseLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Crossca860ac2016-01-04 14:34:37 -080076 if library.static() {
Dan Willemsen218f6562015-07-08 18:13:11 -070077 ret.Class = "STATIC_LIBRARIES"
78 } else {
79 ret.Class = "SHARED_LIBRARIES"
80 }
Colin Crossca860ac2016-01-04 14:34:37 -080081}
82
Dan Willemsenf2c27d72016-07-13 16:50:22 -070083func (library *libraryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
84 library.baseLinker.AndroidMk(ctx, ret)
Colin Crossca860ac2016-01-04 14:34:37 -080085
Colin Cross7b5c22b2016-07-07 11:16:20 -070086 if !library.static() {
Dan Willemsenf2c27d72016-07-13 16:50:22 -070087 library.stripper.AndroidMk(ctx, ret)
Colin Cross7b5c22b2016-07-07 11:16:20 -070088 }
Dan Willemsen7517ed02016-06-10 17:20:30 -070089
Colin Cross635c3b02016-05-18 15:37:25 -070090 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen3b1fffa2016-05-05 15:11:48 -070091 var exportedIncludes []string
Colin Crossca860ac2016-01-04 14:34:37 -080092 for _, flag := range library.exportedFlags() {
Dan Willemsen3b1fffa2016-05-05 15:11:48 -070093 if strings.HasPrefix(flag, "-I") {
Dan Willemsen97750522016-02-09 17:43:51 -080094 exportedIncludes = append(exportedIncludes, strings.TrimPrefix(flag, "-I"))
95 }
Dan Willemsen218f6562015-07-08 18:13:11 -070096 }
97 if len(exportedIncludes) > 0 {
Dan Willemsen97750522016-02-09 17:43:51 -080098 fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DIRS :=", strings.Join(exportedIncludes, " "))
Dan Willemsen218f6562015-07-08 18:13:11 -070099 }
100
Colin Crossca860ac2016-01-04 14:34:37 -0800101 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
Dan Willemsen648c8ae2016-07-21 16:42:14 -0700102 fmt.Fprintln(w, "LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE)$(LOCAL_MODULE_SUFFIX)")
Dan Willemsen218f6562015-07-08 18:13:11 -0700103
Dan Willemsen97750522016-02-09 17:43:51 -0800104 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Dan Willemsen218f6562015-07-08 18:13:11 -0700105
Dan Willemsen97750522016-02-09 17:43:51 -0800106 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800107 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700108}
109
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700110func (object *objectLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Dan Willemsen97750522016-02-09 17:43:51 -0800111 ret.Custom = func(w io.Writer, name, prefix string) error {
Colin Crossca860ac2016-01-04 14:34:37 -0800112 out := ret.OutputFile.Path()
Dan Willemsen218f6562015-07-08 18:13:11 -0700113
Dan Willemsen72d39932016-07-08 23:23:48 -0700114 fmt.Fprintln(w, "\n$("+prefix+"OUT_INTERMEDIATE_LIBRARIES)/"+name+objectExtension+":", out.String())
Dan Willemsen97750522016-02-09 17:43:51 -0800115 fmt.Fprintln(w, "\t$(copy-file-to-target)")
116
117 return nil
Dan Willemsen218f6562015-07-08 18:13:11 -0700118 }
Dan Willemsen218f6562015-07-08 18:13:11 -0700119}
120
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700121func (binary *binaryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
122 binary.stripper.AndroidMk(ctx, ret)
Dan Willemsen7517ed02016-06-10 17:20:30 -0700123
Dan Willemsen218f6562015-07-08 18:13:11 -0700124 ret.Class = "EXECUTABLES"
Colin Cross635c3b02016-05-18 15:37:25 -0700125 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsen97750522016-02-09 17:43:51 -0800126 fmt.Fprintln(w, "LOCAL_CXX_STL := none")
127 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
Colin Cross3854a602016-01-11 12:49:11 -0800128 if binary.static() {
129 fmt.Fprintln(w, "LOCAL_FORCE_STATIC_EXECUTABLE := true")
130 }
Dan Willemsen97750522016-02-09 17:43:51 -0800131 return nil
Colin Crossca860ac2016-01-04 14:34:37 -0800132 })
133}
134
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700135func (test *testBinaryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
136 test.binaryLinker.AndroidMk(ctx, ret)
Colin Crossc7a38dc2016-07-12 13:13:09 -0700137 if Bool(test.testLinker.Properties.Test_per_src) {
Dan Albert6a047692016-07-18 17:24:47 -0700138 ret.SubName = "_" + test.binaryLinker.Properties.Stem
Colin Crossa2344662016-03-24 13:14:12 -0700139 }
140}
141
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700142func (library *toolchainLibraryLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
143 library.baseLinker.AndroidMk(ctx, ret)
Dan Willemsene7932e82016-05-16 15:56:53 -0700144
Colin Cross635c3b02016-05-18 15:37:25 -0700145 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Dan Willemsene7932e82016-05-16 15:56:53 -0700146 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext())
147 fmt.Fprintln(w, "LOCAL_CXX_STL := none")
148 fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=")
149
150 return nil
151 })
152}
153
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700154func (stripper *stripper) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
155 // Make only supports stripping target modules
156 if ctx.Target().Os != android.Android {
157 return
158 }
159
Dan Willemsen7517ed02016-06-10 17:20:30 -0700160 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
161 if stripper.StripProperties.Strip.None {
162 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := false")
163 } else if stripper.StripProperties.Strip.Keep_symbols {
164 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := keep_symbols")
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700165 } else {
166 fmt.Fprintln(w, "LOCAL_STRIP_MODULE := mini-debug-info")
Dan Willemsen7517ed02016-06-10 17:20:30 -0700167 }
168
169 return nil
170 })
171}
172
Dan Willemsenf2c27d72016-07-13 16:50:22 -0700173func (installer *baseInstaller) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) {
Colin Cross635c3b02016-05-18 15:37:25 -0700174 ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error {
Colin Crossa2344662016-03-24 13:14:12 -0700175 path := installer.path.RelPathString()
Colin Crossbf305de2016-03-29 17:32:06 -0700176 dir, file := filepath.Split(path)
177 stem := strings.TrimSuffix(file, filepath.Ext(file))
Colin Crosse14388b2016-05-03 14:08:40 -0700178 fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(OUT_DIR)/"+filepath.Clean(dir))
Colin Crossbf305de2016-03-29 17:32:06 -0700179 fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem)
Colin Cross3854a602016-01-11 12:49:11 -0800180 if len(installer.Properties.Symlinks) > 0 {
181 fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS := "+strings.Join(installer.Properties.Symlinks, " "))
182 }
Colin Crossa2344662016-03-24 13:14:12 -0700183 return nil
184 })
Dan Willemsen218f6562015-07-08 18:13:11 -0700185}