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" |
Dan Willemsen | 174978c | 2016-05-11 00:27:49 -0700 | [diff] [blame] | 28 | "github.com/google/blueprint/proptools" |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func init() { |
Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 32 | RegisterSingletonType("androidmk", AndroidMkSingleton) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | type AndroidMkDataProvider interface { |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 36 | AndroidMk() (AndroidMkData, error) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 37 | BaseModuleName() string |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | type AndroidMkData struct { |
| 41 | Class string |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 42 | SubName string |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 43 | OutputFile OptionalPath |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 44 | Disabled bool |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 45 | |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 46 | Custom func(w io.Writer, name, prefix, moduleDir string) error |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 47 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 48 | Extra []func(w io.Writer, outputFile Path) error |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | func AndroidMkSingleton() blueprint.Singleton { |
| 52 | return &androidMkSingleton{} |
| 53 | } |
| 54 | |
| 55 | type androidMkSingleton struct{} |
| 56 | |
| 57 | func (c *androidMkSingleton) GenerateBuildActions(ctx blueprint.SingletonContext) { |
Dan Willemsen | 174978c | 2016-05-11 00:27:49 -0700 | [diff] [blame] | 58 | config := ctx.Config().(Config) |
| 59 | |
| 60 | if !config.EmbeddedInMake() { |
Dan Willemsen | 5ba07e8 | 2015-12-11 13:51:06 -0800 | [diff] [blame] | 61 | return |
| 62 | } |
| 63 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 64 | var androidMkModulesList []Module |
Colin Cross | 4f6e4e6 | 2016-01-11 12:55:55 -0800 | [diff] [blame] | 65 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 66 | ctx.VisitAllModules(func(module blueprint.Module) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 67 | if amod, ok := module.(Module); ok { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 68 | androidMkModulesList = append(androidMkModulesList, amod) |
| 69 | } |
Colin Cross | 4f6e4e6 | 2016-01-11 12:55:55 -0800 | [diff] [blame] | 70 | }) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 71 | |
Colin Cross | d779da4 | 2015-12-17 18:00:23 -0800 | [diff] [blame] | 72 | sort.Sort(AndroidModulesByName{androidMkModulesList, ctx}) |
| 73 | |
Dan Willemsen | 174978c | 2016-05-11 00:27:49 -0700 | [diff] [blame] | 74 | transMk := PathForOutput(ctx, "Android"+proptools.String(config.ProductVariables.Make_suffix)+".mk") |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 75 | if ctx.Failed() { |
| 76 | return |
| 77 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 78 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 79 | err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 80 | if err != nil { |
| 81 | ctx.Errorf(err.Error()) |
| 82 | } |
| 83 | |
| 84 | ctx.Build(pctx, blueprint.BuildParams{ |
| 85 | Rule: blueprint.Phony, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 86 | Outputs: []string{transMk.String()}, |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 87 | Optional: true, |
| 88 | }) |
| 89 | } |
| 90 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 91 | func translateAndroidMk(ctx blueprint.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 | |
| 143 | func translateAndroidMkModule(ctx blueprint.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 | |
Colin Cross | cc4f3e3 | 2016-11-23 15:41:09 -0800 | [diff] [blame] | 160 | data, err := provider.AndroidMk() |
| 161 | if err != nil { |
| 162 | return err |
| 163 | } |
| 164 | |
Dan Willemsen | 01a405a | 2016-06-13 17:19:03 -0700 | [diff] [blame] | 165 | // Make does not understand LinuxBionic |
| 166 | if amod.Os() == LinuxBionic { |
| 167 | return nil |
| 168 | } |
| 169 | |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 170 | if data.SubName != "" { |
Dan Albert | 6a04769 | 2016-07-18 17:24:47 -0700 | [diff] [blame] | 171 | name += data.SubName |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 174 | if data.Custom != nil { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 175 | prefix := "" |
Nan Zhang | 280802c | 2017-03-29 16:24:19 -0700 | [diff] [blame] | 176 | if amod.ArchSpecific() { |
| 177 | switch amod.Os().Class { |
| 178 | case Host: |
| 179 | prefix = "HOST_" |
| 180 | case HostCross: |
| 181 | prefix = "HOST_CROSS_" |
| 182 | case Device: |
| 183 | prefix = "TARGET_" |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 184 | |
Nan Zhang | 280802c | 2017-03-29 16:24:19 -0700 | [diff] [blame] | 185 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 186 | |
Nan Zhang | 280802c | 2017-03-29 16:24:19 -0700 | [diff] [blame] | 187 | config := ctx.Config().(Config) |
| 188 | if amod.Arch().ArchType != config.Targets[amod.Os().Class][0].Arch.ArchType { |
| 189 | prefix = "2ND_" + prefix |
| 190 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Nan Zhang | 6d34b30 | 2017-02-04 17:47:46 -0800 | [diff] [blame] | 193 | return data.Custom(w, name, prefix, filepath.Dir(ctx.BlueprintFile(mod))) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 196 | if data.Disabled { |
| 197 | return nil |
| 198 | } |
| 199 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 200 | if !data.OutputFile.Valid() { |
| 201 | return err |
| 202 | } |
| 203 | |
| 204 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
Colin Cross | 2630213 | 2016-05-13 12:28:46 -0700 | [diff] [blame] | 205 | fmt.Fprintln(w, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod))) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 206 | fmt.Fprintln(w, "LOCAL_MODULE :=", name) |
| 207 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", data.Class) |
Colin Cross | 2630213 | 2016-05-13 12:28:46 -0700 | [diff] [blame] | 208 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String()) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 209 | |
Chris Wolfe | 998306e | 2016-08-15 14:47:23 -0400 | [diff] [blame] | 210 | if len(amod.commonProperties.Required) > 0 { |
| 211 | fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES := "+strings.Join(amod.commonProperties.Required, " ")) |
| 212 | } |
| 213 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 214 | archStr := amod.Arch().ArchType.String() |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 215 | host := false |
| 216 | switch amod.Os().Class { |
| 217 | case Host: |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 218 | // Make cannot identify LOCAL_MODULE_HOST_ARCH:= common. |
| 219 | if archStr != "common" { |
| 220 | fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr) |
| 221 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 222 | host = true |
| 223 | case HostCross: |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 224 | // Make cannot identify LOCAL_MODULE_HOST_CROSS_ARCH:= common. |
| 225 | if archStr != "common" { |
| 226 | fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr) |
| 227 | } |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 228 | host = true |
| 229 | case Device: |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 230 | // Make cannot identify LOCAL_MODULE_TARGET_ARCH:= common. |
| 231 | if archStr != "common" { |
| 232 | fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr) |
| 233 | } |
Dan Willemsen | 0fda89f | 2016-06-01 15:25:32 -0700 | [diff] [blame] | 234 | |
| 235 | if len(amod.commonProperties.Logtags) > 0 { |
| 236 | fmt.Fprintln(w, "LOCAL_LOGTAGS_FILES := ", strings.Join(amod.commonProperties.Logtags, " ")) |
| 237 | } |
Dan Willemsen | 2277bcb | 2016-07-25 20:27:39 -0700 | [diff] [blame] | 238 | if len(amod.commonProperties.Init_rc) > 0 { |
| 239 | fmt.Fprintln(w, "LOCAL_INIT_RC := ", strings.Join(amod.commonProperties.Init_rc, " ")) |
| 240 | } |
Dan Willemsen | 7ed1ae3 | 2016-12-05 16:47:50 -0800 | [diff] [blame] | 241 | if amod.commonProperties.Proprietary { |
| 242 | fmt.Fprintln(w, "LOCAL_PROPRIETARY_MODULE := true") |
| 243 | } |
Dan Willemsen | aa118f9 | 2017-04-06 12:49:58 -0700 | [diff] [blame] | 244 | if amod.commonProperties.Vendor { |
| 245 | fmt.Fprintln(w, "LOCAL_VENDOR_MODULE := true") |
| 246 | } |
Dan Willemsen | efac4a8 | 2017-07-18 19:42:09 -0700 | [diff] [blame] | 247 | if amod.commonProperties.Owner != nil { |
| 248 | fmt.Fprintln(w, "LOCAL_MODULE_OWNER :=", *amod.commonProperties.Owner) |
Colin Cross | 55708f3 | 2017-03-20 13:23:34 -0700 | [diff] [blame] | 249 | } |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Colin Cross | a1ad8d1 | 2016-06-01 17:09:44 -0700 | [diff] [blame] | 252 | if host { |
| 253 | fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", amod.Os().String()) |
| 254 | fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true") |
| 255 | } |
| 256 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 257 | for _, extra := range data.Extra { |
| 258 | err = extra(w, data.OutputFile.Path()) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 259 | if err != nil { |
| 260 | return err |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
| 265 | |
| 266 | return err |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 267 | } |