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