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 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "bytes" |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 19 | "fmt" |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 20 | "io" |
| 21 | "io/ioutil" |
| 22 | "os" |
| 23 | "path/filepath" |
| 24 | "sort" |
Dan Willemsen | 0fda89f | 2016-06-01 15:25:32 -0700 | [diff] [blame] | 25 | "strings" |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 26 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 27 | "github.com/google/blueprint" |
| 28 | ) |
| 29 | |
| 30 | func init() { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 31 | RegisterSingletonType("androidmk", AndroidMkSingleton) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type AndroidMkDataProvider interface { |
Colin Cross | a18e9cf | 2017-08-10 17:00:19 -0700 | [diff] [blame] | 35 | AndroidMk() AndroidMkData |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 36 | BaseModuleName() string |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | type AndroidMkData struct { |
| 40 | Class string |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 41 | SubName string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 42 | OutputFile OptionalPath |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 43 | Disabled bool |
Colin Cross | 5349941 | 2017-09-07 13:20:25 -0700 | [diff] [blame] | 44 | Include string |
Colin Cross | 9243010 | 2017-10-09 14:59:32 -0700 | [diff] [blame] | 45 | Required []string |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 46 | |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 47 | Custom func(w io.Writer, name, prefix, moduleDir string, data AndroidMkData) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 48 | |
Colin Cross | 27a4b05 | 2017-08-10 16:32:23 -0700 | [diff] [blame] | 49 | Extra []AndroidMkExtraFunc |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 50 | |
| 51 | preamble bytes.Buffer |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Colin Cross | 27a4b05 | 2017-08-10 16:32:23 -0700 | [diff] [blame] | 54 | type AndroidMkExtraFunc func(w io.Writer, outputFile Path) |
| 55 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 56 | func AndroidMkSingleton() Singleton { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 57 | return &androidMkSingleton{} |
| 58 | } |
| 59 | |
| 60 | type androidMkSingleton struct{} |
| 61 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 62 | func (c *androidMkSingleton) GenerateBuildActions(ctx SingletonContext) { |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 63 | if !ctx.Config().EmbeddedInMake() { |
Dan Willemsen | 5ba07e8 | 2015-12-11 13:51:06 -0800 | [diff] [blame] | 64 | return |
| 65 | } |
| 66 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 67 | var androidMkModulesList []Module |
Colin Cross | 4f6e4e6 | 2016-01-11 12:55:55 -0800 | [diff] [blame] | 68 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 69 | ctx.VisitAllModules(func(module Module) { |
| 70 | androidMkModulesList = append(androidMkModulesList, module) |
Colin Cross | 4f6e4e6 | 2016-01-11 12:55:55 -0800 | [diff] [blame] | 71 | }) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 72 | |
Colin Cross | d779da4 | 2015-12-17 18:00:23 -0800 | [diff] [blame] | 73 | sort.Sort(AndroidModulesByName{androidMkModulesList, ctx}) |
| 74 | |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 75 | transMk := PathForOutput(ctx, "Android"+String(ctx.Config().ProductVariables.Make_suffix)+".mk") |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 76 | if ctx.Failed() { |
| 77 | return |
| 78 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 79 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 80 | err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 81 | if err != nil { |
| 82 | ctx.Errorf(err.Error()) |
| 83 | } |
| 84 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 85 | ctx.Build(pctx, BuildParams{ |
| 86 | Rule: blueprint.Phony, |
| 87 | Output: transMk, |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 88 | }) |
| 89 | } |
| 90 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 91 | func translateAndroidMk(ctx SingletonContext, mkFile string, mods []Module) error { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 92 | buf := &bytes.Buffer{} |
| 93 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 94 | fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))") |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 95 | |
Dan Willemsen | 70e17fa | 2016-07-25 16:00:20 -0700 | [diff] [blame] | 96 | type_stats := make(map[string]int) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 97 | for _, mod := range mods { |
| 98 | err := translateAndroidMkModule(ctx, buf, mod) |
| 99 | if err != nil { |
| 100 | os.Remove(mkFile) |
| 101 | return err |
| 102 | } |
Dan Willemsen | 70e17fa | 2016-07-25 16:00:20 -0700 | [diff] [blame] | 103 | |
| 104 | if ctx.PrimaryModule(mod) == mod { |
| 105 | type_stats[ctx.ModuleType(mod)] += 1 |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | keys := []string{} |
| 110 | fmt.Fprintln(buf, "\nSTATS.SOONG_MODULE_TYPE :=") |
| 111 | for k := range type_stats { |
| 112 | keys = append(keys, k) |
| 113 | } |
| 114 | sort.Strings(keys) |
| 115 | for _, mod_type := range keys { |
| 116 | fmt.Fprintln(buf, "STATS.SOONG_MODULE_TYPE +=", mod_type) |
| 117 | fmt.Fprintf(buf, "STATS.SOONG_MODULE_TYPE.%s := %d\n", mod_type, type_stats[mod_type]) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // Don't write to the file if it hasn't changed |
| 121 | if _, err := os.Stat(mkFile); !os.IsNotExist(err) { |
| 122 | if data, err := ioutil.ReadFile(mkFile); err == nil { |
| 123 | matches := buf.Len() == len(data) |
| 124 | |
| 125 | if matches { |
| 126 | for i, value := range buf.Bytes() { |
| 127 | if value != data[i] { |
| 128 | matches = false |
| 129 | break |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if matches { |
| 135 | return nil |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return ioutil.WriteFile(mkFile, buf.Bytes(), 0666) |
| 141 | } |
| 142 | |
Colin Cross | 0875c52 | 2017-11-28 17:34:01 -0800 | [diff] [blame] | 143 | func translateAndroidMkModule(ctx SingletonContext, w io.Writer, mod blueprint.Module) error { |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 144 | provider, ok := mod.(AndroidMkDataProvider) |
| 145 | if !ok { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 146 | return nil |
| 147 | } |
| 148 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 149 | name := provider.BaseModuleName() |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 150 | amod := mod.(Module).base() |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 151 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 152 | if !amod.Enabled() { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 153 | return nil |
| 154 | } |
| 155 | |
| 156 | if amod.commonProperties.SkipInstall { |
| 157 | return nil |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 160 | if !amod.commonProperties.NamespaceExportedToMake { |
| 161 | // TODO(jeffrygaston) do we want to validate that there are no modules being |
| 162 | // exported to Kati that depend on this module? |
| 163 | return nil |
| 164 | } |
| 165 | |
Colin Cross | 91825d2 | 2017-08-10 16:59:47 -0700 | [diff] [blame] | 166 | data := provider.AndroidMk() |
Colin Cross | cc4f3e3 | 2016-11-23 15:41:09 -0800 | [diff] [blame] | 167 | |
Colin Cross | 5349941 | 2017-09-07 13:20:25 -0700 | [diff] [blame] | 168 | if data.Include == "" { |
| 169 | data.Include = "$(BUILD_PREBUILT)" |
| 170 | } |
| 171 | |
Colin Cross | 9243010 | 2017-10-09 14:59:32 -0700 | [diff] [blame] | 172 | data.Required = amod.commonProperties.Required |
| 173 | |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 174 | // Make does not understand LinuxBionic |
| 175 | if amod.Os() == LinuxBionic { |
| 176 | return nil |
| 177 | } |
| 178 | |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 179 | prefix := "" |
| 180 | if amod.ArchSpecific() { |
| 181 | switch amod.Os().Class { |
| 182 | case Host: |
| 183 | prefix = "HOST_" |
| 184 | case HostCross: |
| 185 | prefix = "HOST_CROSS_" |
| 186 | case Device: |
| 187 | prefix = "TARGET_" |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 188 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 191 | if amod.Arch().ArchType != ctx.Config().Targets[amod.Os().Class][0].Arch.ArchType { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 192 | prefix = "2ND_" + prefix |
| 193 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 196 | fmt.Fprintln(&data.preamble, "\ninclude $(CLEAR_VARS)") |
| 197 | fmt.Fprintln(&data.preamble, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod))) |
| 198 | fmt.Fprintln(&data.preamble, "LOCAL_MODULE :=", name+data.SubName) |
| 199 | fmt.Fprintln(&data.preamble, "LOCAL_MODULE_CLASS :=", data.Class) |
| 200 | fmt.Fprintln(&data.preamble, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String()) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 201 | |
Colin Cross | 9243010 | 2017-10-09 14:59:32 -0700 | [diff] [blame] | 202 | if len(data.Required) > 0 { |
| 203 | fmt.Fprintln(&data.preamble, "LOCAL_REQUIRED_MODULES := "+strings.Join(data.Required, " ")) |
Chris Wolfe | 998306e | 2016-08-15 14:47:23 -0400 | [diff] [blame] | 204 | } |
| 205 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 206 | archStr := amod.Arch().ArchType.String() |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 207 | host := false |
| 208 | switch amod.Os().Class { |
| 209 | case Host: |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 210 | // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common. |
| 211 | if archStr != "common" { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 212 | fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_ARCH :=", archStr) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 213 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 214 | host = true |
| 215 | case HostCross: |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 216 | // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common. |
| 217 | if archStr != "common" { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 218 | fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 219 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 220 | host = true |
| 221 | case Device: |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 222 | // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common. |
| 223 | if archStr != "common" { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 224 | fmt.Fprintln(&data.preamble, "LOCAL_MODULE_TARGET_ARCH :=", archStr) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 225 | } |
Dan Willemsen | 0fda89f | 2016-06-01 15:25:32 -0700 | [diff] [blame] | 226 | |
Dan Willemsen | 2277bcb | 2016-07-25 20:27:39 -0700 | [diff] [blame] | 227 | if len(amod.commonProperties.Init_rc) > 0 { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 228 | fmt.Fprintln(&data.preamble, "LOCAL_INIT_RC := ", strings.Join(amod.commonProperties.Init_rc, " ")) |
Dan Willemsen | 2277bcb | 2016-07-25 20:27:39 -0700 | [diff] [blame] | 229 | } |
Colin Cross | 7d716ba | 2017-11-01 10:38:29 -0700 | [diff] [blame] | 230 | if Bool(amod.commonProperties.Proprietary) { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 231 | fmt.Fprintln(&data.preamble, "LOCAL_PROPRIETARY_MODULE := true") |
Dan Willemsen | 7ed1ae3 | 2016-12-05 16:47:50 -0800 | [diff] [blame] | 232 | } |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 233 | if Bool(amod.commonProperties.Vendor) || Bool(amod.commonProperties.Soc_specific) { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 234 | fmt.Fprintln(&data.preamble, "LOCAL_VENDOR_MODULE := true") |
Dan Willemsen | aa118f9 | 2017-04-06 12:49:58 -0700 | [diff] [blame] | 235 | } |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 236 | if Bool(amod.commonProperties.Device_specific) { |
| 237 | fmt.Fprintln(&data.preamble, "LOCAL_ODM_MODULE := true") |
| 238 | } |
| 239 | if Bool(amod.commonProperties.Product_specific) { |
| 240 | fmt.Fprintln(&data.preamble, "LOCAL_OEM_MODULE := true") |
| 241 | } |
Dan Willemsen | efac4a8 | 2017-07-18 19:42:09 -0700 | [diff] [blame] | 242 | if amod.commonProperties.Owner != nil { |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 243 | fmt.Fprintln(&data.preamble, "LOCAL_MODULE_OWNER :=", *amod.commonProperties.Owner) |
Colin Cross | 55708f3 | 2017-03-20 13:23:34 -0700 | [diff] [blame] | 244 | } |
Colin Cross | 5aac362 | 2017-08-31 15:07:09 -0700 | [diff] [blame] | 245 | if amod.commonProperties.Notice != nil { |
Colin Cross | aa76858 | 2017-09-05 15:54:27 -0700 | [diff] [blame] | 246 | fmt.Fprintln(&data.preamble, "LOCAL_NOTICE_FILE :=", "$(LOCAL_PATH)/"+*amod.commonProperties.Notice) |
Colin Cross | 5aac362 | 2017-08-31 15:07:09 -0700 | [diff] [blame] | 247 | } |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 250 | if host { |
Dan Willemsen | 866b563 | 2017-09-22 12:28:24 -0700 | [diff] [blame] | 251 | makeOs := amod.Os().String() |
| 252 | if amod.Os() == Linux || amod.Os() == LinuxBionic { |
| 253 | makeOs = "linux" |
| 254 | } |
| 255 | fmt.Fprintln(&data.preamble, "LOCAL_MODULE_HOST_OS :=", makeOs) |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 256 | fmt.Fprintln(&data.preamble, "LOCAL_IS_HOST_MODULE := true") |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Colin Cross | 0f86d18 | 2017-08-10 17:07:28 -0700 | [diff] [blame] | 259 | blueprintDir := filepath.Dir(ctx.BlueprintFile(mod)) |
| 260 | |
| 261 | if data.Custom != nil { |
| 262 | data.Custom(w, name, prefix, blueprintDir, data) |
| 263 | } else { |
| 264 | WriteAndroidMkData(w, data) |
| 265 | } |
| 266 | |
| 267 | return nil |
| 268 | } |
| 269 | |
| 270 | func WriteAndroidMkData(w io.Writer, data AndroidMkData) { |
| 271 | if data.Disabled { |
| 272 | return |
| 273 | } |
| 274 | |
| 275 | if !data.OutputFile.Valid() { |
| 276 | return |
| 277 | } |
| 278 | |
| 279 | w.Write(data.preamble.Bytes()) |
| 280 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 281 | for _, extra := range data.Extra { |
Colin Cross | 27a4b05 | 2017-08-10 16:32:23 -0700 | [diff] [blame] | 282 | extra(w, data.OutputFile.Path()) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Colin Cross | 5349941 | 2017-09-07 13:20:25 -0700 | [diff] [blame] | 285 | fmt.Fprintln(w, "include "+data.Include) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 286 | } |