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" |
| 25 | |
| 26 | "android/soong" |
| 27 | |
| 28 | "github.com/google/blueprint" |
Dan Willemsen | 174978c | 2016-05-11 00:27:49 -0700 | [diff] [blame] | 29 | "github.com/google/blueprint/proptools" |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | func init() { |
| 33 | soong.RegisterSingletonType("androidmk", AndroidMkSingleton) |
| 34 | } |
| 35 | |
| 36 | type AndroidMkDataProvider interface { |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 37 | AndroidMk() (AndroidMkData, error) |
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 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 46 | Custom func(w io.Writer, name, prefix 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 | |
Dan Willemsen | 174978c | 2016-05-11 00:27:49 -0700 | [diff] [blame] | 64 | ctx.SetNinjaBuildDir(pctx, filepath.Join(config.buildDir, "..")) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 65 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 66 | var androidMkModulesList []Module |
Colin Cross | 4f6e4e6 | 2016-01-11 12:55:55 -0800 | [diff] [blame] | 67 | |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 68 | ctx.VisitAllModules(func(module blueprint.Module) { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 69 | if amod, ok := module.(Module); ok { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 70 | androidMkModulesList = append(androidMkModulesList, amod) |
| 71 | } |
Colin Cross | 4f6e4e6 | 2016-01-11 12:55:55 -0800 | [diff] [blame] | 72 | }) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 73 | |
Colin Cross | d779da4 | 2015-12-17 18:00:23 -0800 | [diff] [blame] | 74 | sort.Sort(AndroidModulesByName{androidMkModulesList, ctx}) |
| 75 | |
Dan Willemsen | 174978c | 2016-05-11 00:27:49 -0700 | [diff] [blame] | 76 | transMk := PathForOutput(ctx, "Android"+proptools.String(config.ProductVariables.Make_suffix)+".mk") |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 77 | if ctx.Failed() { |
| 78 | return |
| 79 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 80 | |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 81 | err := translateAndroidMk(ctx, transMk.String(), androidMkModulesList) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 82 | if err != nil { |
| 83 | ctx.Errorf(err.Error()) |
| 84 | } |
| 85 | |
| 86 | ctx.Build(pctx, blueprint.BuildParams{ |
| 87 | Rule: blueprint.Phony, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 88 | Outputs: []string{transMk.String()}, |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 89 | Optional: true, |
| 90 | }) |
| 91 | } |
| 92 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 93 | func translateAndroidMk(ctx blueprint.SingletonContext, mkFile string, mods []Module) error { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 94 | buf := &bytes.Buffer{} |
| 95 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 96 | fmt.Fprintln(buf, "LOCAL_MODULE_MAKEFILE := $(lastword $(MAKEFILE_LIST))") |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 97 | |
| 98 | for _, mod := range mods { |
| 99 | err := translateAndroidMkModule(ctx, buf, mod) |
| 100 | if err != nil { |
| 101 | os.Remove(mkFile) |
| 102 | return err |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Don't write to the file if it hasn't changed |
| 107 | if _, err := os.Stat(mkFile); !os.IsNotExist(err) { |
| 108 | if data, err := ioutil.ReadFile(mkFile); err == nil { |
| 109 | matches := buf.Len() == len(data) |
| 110 | |
| 111 | if matches { |
| 112 | for i, value := range buf.Bytes() { |
| 113 | if value != data[i] { |
| 114 | matches = false |
| 115 | break |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if matches { |
| 121 | return nil |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return ioutil.WriteFile(mkFile, buf.Bytes(), 0666) |
| 127 | } |
| 128 | |
| 129 | func translateAndroidMkModule(ctx blueprint.SingletonContext, w io.Writer, mod blueprint.Module) error { |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 130 | name := ctx.ModuleName(mod) |
| 131 | |
| 132 | provider, ok := mod.(AndroidMkDataProvider) |
| 133 | if !ok { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 134 | return nil |
| 135 | } |
| 136 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 137 | amod := mod.(Module).base() |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 138 | data, err := provider.AndroidMk() |
| 139 | if err != nil { |
| 140 | return err |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 143 | if !amod.Enabled() { |
| 144 | return err |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Colin Cross | a234466 | 2016-03-24 13:14:12 -0700 | [diff] [blame] | 147 | if data.SubName != "" { |
| 148 | name += "_" + data.SubName |
| 149 | } |
| 150 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 151 | hostCross := false |
| 152 | if amod.Host() && amod.HostType() != CurrentHostType() { |
| 153 | hostCross = true |
| 154 | } |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 155 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 156 | if data.Custom != nil { |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 157 | prefix := "" |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 158 | if amod.Host() { |
| 159 | if hostCross { |
| 160 | prefix = "HOST_CROSS_" |
| 161 | } else { |
| 162 | prefix = "HOST_" |
| 163 | } |
| 164 | if amod.Arch().ArchType != ctx.Config().(Config).HostArches[amod.HostType()][0].ArchType { |
| 165 | prefix = "2ND_" + prefix |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 166 | } |
| 167 | } else { |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 168 | prefix = "TARGET_" |
| 169 | if amod.Arch().ArchType != ctx.Config().(Config).DeviceArches[0].ArchType { |
| 170 | prefix = "2ND_" + prefix |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 174 | return data.Custom(w, name, prefix) |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 177 | if data.Disabled { |
| 178 | return nil |
| 179 | } |
| 180 | |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 181 | if !data.OutputFile.Valid() { |
| 182 | return err |
| 183 | } |
| 184 | |
| 185 | fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)") |
Colin Cross | 2630213 | 2016-05-13 12:28:46 -0700 | [diff] [blame] | 186 | fmt.Fprintln(w, "LOCAL_PATH :=", filepath.Dir(ctx.BlueprintFile(mod))) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 187 | fmt.Fprintln(w, "LOCAL_MODULE :=", name) |
| 188 | fmt.Fprintln(w, "LOCAL_MODULE_CLASS :=", data.Class) |
| 189 | fmt.Fprintln(w, "LOCAL_MULTILIB :=", amod.commonProperties.Compile_multilib) |
Colin Cross | 2630213 | 2016-05-13 12:28:46 -0700 | [diff] [blame] | 190 | fmt.Fprintln(w, "LOCAL_PREBUILT_MODULE_FILE :=", data.OutputFile.String()) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 191 | |
| 192 | archStr := amod.Arch().ArchType.String() |
| 193 | if amod.Host() { |
| 194 | if hostCross { |
| 195 | fmt.Fprintln(w, "LOCAL_MODULE_HOST_CROSS_ARCH :=", archStr) |
| 196 | } else { |
| 197 | fmt.Fprintln(w, "LOCAL_MODULE_HOST_ARCH :=", archStr) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 198 | } |
| 199 | fmt.Fprintln(w, "LOCAL_MODULE_HOST_OS :=", amod.HostType().String()) |
| 200 | fmt.Fprintln(w, "LOCAL_IS_HOST_MODULE := true") |
| 201 | } else { |
| 202 | fmt.Fprintln(w, "LOCAL_MODULE_TARGET_ARCH :=", archStr) |
| 203 | } |
| 204 | |
Colin Cross | ca860ac | 2016-01-04 14:34:37 -0800 | [diff] [blame] | 205 | for _, extra := range data.Extra { |
| 206 | err = extra(w, data.OutputFile.Path()) |
Dan Willemsen | 9775052 | 2016-02-09 17:43:51 -0800 | [diff] [blame] | 207 | if err != nil { |
| 208 | return err |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | fmt.Fprintln(w, "include $(BUILD_PREBUILT)") |
| 213 | |
| 214 | return err |
Dan Willemsen | 218f656 | 2015-07-08 18:13:11 -0700 | [diff] [blame] | 215 | } |