Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 18 | "fmt" |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 19 | "io" |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 20 | "path/filepath" |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 21 | "strings" |
| 22 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 23 | "android/soong/android" |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 26 | type AndroidMkContext interface { |
| 27 | Target() android.Target |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 28 | subAndroidMk(*android.AndroidMkData, interface{}) |
| 29 | } |
| 30 | |
| 31 | type subAndroidMkProvider interface { |
| 32 | AndroidMk(AndroidMkContext, *android.AndroidMkData) |
| 33 | } |
| 34 | |
| 35 | func (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 Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 47 | func (c *Module) AndroidMk() (ret android.AndroidMkData, err error) { |
Colin Cross | bc6fb16 | 2016-05-24 15:39:04 -0700 | [diff] [blame] | 48 | if c.Properties.HideFromMake { |
| 49 | ret.Disabled = true |
| 50 | return ret, nil |
| 51 | } |
| 52 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 53 | ret.OutputFile = c.outputFile |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 54 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) (err error) { |
Colin Cross | 30d5f51 | 2016-05-03 18:02:42 -0700 | [diff] [blame] | 55 | fmt.Fprintln(w, "LOCAL_SANITIZE := never") |
Colin Cross | c99deeb | 2016-04-11 15:06:20 -0700 | [diff] [blame] | 56 | if len(c.Properties.AndroidMkSharedLibs) > 0 { |
| 57 | fmt.Fprintln(w, "LOCAL_SHARED_LIBRARIES := "+strings.Join(c.Properties.AndroidMkSharedLibs, " ")) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 58 | } |
Dan Willemsen | a96ff64 | 2016-06-07 12:34:45 -0700 | [diff] [blame] | 59 | 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 Willemsen | d2ede87 | 2016-11-18 14:54:24 -0800 | [diff] [blame] | 62 | } 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 Willemsen | a96ff64 | 2016-06-07 12:34:45 -0700 | [diff] [blame] | 65 | } else { |
| 66 | // These are already included in LOCAL_SHARED_LIBRARIES |
| 67 | fmt.Fprintln(w, "LOCAL_CXX_STL := none") |
| 68 | } |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 69 | return nil |
| 70 | }) |
| 71 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 72 | for _, feature := range c.features { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 73 | c.subAndroidMk(&ret, feature) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 76 | c.subAndroidMk(&ret, c.compiler) |
| 77 | c.subAndroidMk(&ret, c.linker) |
| 78 | c.subAndroidMk(&ret, c.installer) |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 79 | |
| 80 | return ret, nil |
| 81 | } |
| 82 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 83 | func (library *libraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 84 | if !library.static() { |
| 85 | ctx.subAndroidMk(ret, &library.stripper) |
Dan Willemsen | 394e9dc | 2016-09-14 15:04:48 -0700 | [diff] [blame] | 86 | ctx.subAndroidMk(ret, &library.relocationPacker) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 89 | if library.static() { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 90 | ret.Class = "STATIC_LIBRARIES" |
| 91 | } else { |
| 92 | ret.Class = "SHARED_LIBRARIES" |
| 93 | } |
Dan Willemsen | 7517ed0 | 2016-06-10 17:20:30 -0700 | [diff] [blame] | 94 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 95 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error { |
Dan Willemsen | 3b1fffa | 2016-05-05 15:11:48 -0700 | [diff] [blame] | 96 | var exportedIncludes []string |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 97 | for _, flag := range library.exportedFlags() { |
Dan Willemsen | 3b1fffa | 2016-05-05 15:11:48 -0700 | [diff] [blame] | 98 | if strings.HasPrefix(flag, "-I") { |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 99 | exportedIncludes = append(exportedIncludes, strings.TrimPrefix(flag, "-I")) |
| 100 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 101 | } |
| 102 | if len(exportedIncludes) > 0 { |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 103 | fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DIRS :=", strings.Join(exportedIncludes, " ")) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 104 | } |
Dan Willemsen | 847dcc7 | 2016-09-29 12:13:36 -0700 | [diff] [blame] | 105 | exportedIncludeDeps := library.exportedFlagsDeps() |
| 106 | if len(exportedIncludeDeps) > 0 { |
| 107 | fmt.Fprintln(w, "LOCAL_EXPORT_C_INCLUDE_DEPS :=", strings.Join(exportedIncludeDeps.Strings(), " ")) |
| 108 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 109 | |
Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 110 | fmt.Fprintln(w, "LOCAL_BUILT_MODULE_STEM := $(LOCAL_MODULE)"+outputFile.Ext()) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 111 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 112 | fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=") |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 113 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 114 | return nil |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 115 | }) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 116 | |
| 117 | if !library.static() { |
| 118 | ctx.subAndroidMk(ret, library.baseInstaller) |
| 119 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 122 | func (object *objectLinker) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 123 | ret.Custom = func(w io.Writer, name, prefix string) error { |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 124 | out := ret.OutputFile.Path() |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 125 | |
Dan Willemsen | 72d3993 | 2016-07-08 23:23:48 -0700 | [diff] [blame] | 126 | fmt.Fprintln(w, "\n$("+prefix+"OUT_INTERMEDIATE_LIBRARIES)/"+name+objectExtension+":", out.String()) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 127 | fmt.Fprintln(w, "\t$(copy-file-to-target)") |
| 128 | |
| 129 | return nil |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 130 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 133 | func (binary *binaryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 134 | ctx.subAndroidMk(ret, binary.baseInstaller) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 135 | ctx.subAndroidMk(ret, &binary.stripper) |
Dan Willemsen | 7517ed0 | 2016-06-10 17:20:30 -0700 | [diff] [blame] | 136 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 137 | ret.Class = "EXECUTABLES" |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 138 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error { |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 139 | fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=") |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 140 | if Bool(binary.Properties.Static_executable) { |
Colin Cross | 3854a60 | 2016-01-11 12:49:11 -0800 | [diff] [blame] | 141 | fmt.Fprintln(w, "LOCAL_FORCE_STATIC_EXECUTABLE := true") |
| 142 | } |
Colin Cross | 9b09f24 | 2016-12-07 13:37:42 -0800 | [diff] [blame^] | 143 | |
| 144 | if len(binary.symlinks) > 0 { |
| 145 | fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS := "+strings.Join(binary.symlinks, " ")) |
| 146 | } |
| 147 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 148 | return nil |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 149 | }) |
| 150 | } |
| 151 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 152 | func (benchmark *benchmarkDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 153 | ctx.subAndroidMk(ret, benchmark.binaryDecorator) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | func (test *testBinary) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 157 | ctx.subAndroidMk(ret, test.binaryDecorator) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 158 | if Bool(test.Properties.Test_per_src) { |
| 159 | ret.SubName = "_" + test.binaryDecorator.Properties.Stem |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 163 | func (test *testLibrary) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 164 | ctx.subAndroidMk(ret, test.libraryDecorator) |
| 165 | } |
Dan Willemsen | e7932e8 | 2016-05-16 15:56:53 -0700 | [diff] [blame] | 166 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 167 | func (library *toolchainLibraryDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
| 168 | ret.Class = "STATIC_LIBRARIES" |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 169 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error { |
Dan Willemsen | e7932e8 | 2016-05-16 15:56:53 -0700 | [diff] [blame] | 170 | fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext()) |
Dan Willemsen | e7932e8 | 2016-05-16 15:56:53 -0700 | [diff] [blame] | 171 | fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=") |
| 172 | |
| 173 | return nil |
| 174 | }) |
| 175 | } |
| 176 | |
Dan Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 177 | func (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 Willemsen | 7517ed0 | 2016-06-10 17:20:30 -0700 | [diff] [blame] | 183 | 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 Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 188 | } else { |
| 189 | fmt.Fprintln(w, "LOCAL_STRIP_MODULE := mini-debug-info") |
Dan Willemsen | 7517ed0 | 2016-06-10 17:20:30 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return nil |
| 193 | }) |
| 194 | } |
| 195 | |
Dan Willemsen | 394e9dc | 2016-09-14 15:04:48 -0700 | [diff] [blame] | 196 | func (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 Willemsen | f2c27d7 | 2016-07-13 16:50:22 -0700 | [diff] [blame] | 205 | func (installer *baseInstaller) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Dan Willemsen | 03ce63e | 2016-09-29 12:17:49 -0700 | [diff] [blame] | 206 | // 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 Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 212 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error { |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 213 | path := installer.path.RelPathString() |
Colin Cross | bf305de | 2016-03-29 17:32:06 -0700 | [diff] [blame] | 214 | dir, file := filepath.Split(path) |
| 215 | stem := strings.TrimSuffix(file, filepath.Ext(file)) |
Dan Willemsen | 1d577e2 | 2016-08-29 15:53:15 -0700 | [diff] [blame] | 216 | fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+filepath.Ext(file)) |
Colin Cross | e14388b | 2016-05-03 14:08:40 -0700 | [diff] [blame] | 217 | fmt.Fprintln(w, "LOCAL_MODULE_PATH := $(OUT_DIR)/"+filepath.Clean(dir)) |
Colin Cross | bf305de | 2016-03-29 17:32:06 -0700 | [diff] [blame] | 218 | fmt.Fprintln(w, "LOCAL_MODULE_STEM := "+stem) |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 219 | return nil |
| 220 | }) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 221 | } |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 222 | |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 223 | func (c *stubDecorator) AndroidMk(ctx AndroidMkContext, ret *android.AndroidMkData) { |
Dan Albert | fd86e9e | 2016-11-08 13:35:12 -0800 | [diff] [blame] | 224 | ret.SubName = "." + c.properties.ApiLevel |
Dan Albert | 705c84b | 2016-08-08 10:45:03 -0700 | [diff] [blame] | 225 | ret.Class = "SHARED_LIBRARIES" |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 226 | |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 227 | ret.Extra = append(ret.Extra, func(w io.Writer, outputFile android.Path) error { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 228 | path, file := filepath.Split(c.installPath) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 229 | stem := strings.TrimSuffix(file, filepath.Ext(file)) |
Dan Albert | cdd4c24 | 2016-08-08 14:44:56 -0700 | [diff] [blame] | 230 | fmt.Fprintln(w, "LOCAL_SYSTEM_SHARED_LIBRARIES :=") |
| 231 | fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX := "+outputFile.Ext()) |
Dan Albert | 914449f | 2016-06-17 16:45:24 -0700 | [diff] [blame] | 232 | 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 | } |