blob: b9d6777e4ee4393700cbef20110f9f2011c73c8e [file] [log] [blame]
Colin Cross7f64b6d2015-07-09 13:57:48 -07001// 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 Cross635c3b02016-05-18 15:37:25 -070015package android
Colin Cross7f64b6d2015-07-09 13:57:48 -070016
17import (
18 "fmt"
19 "reflect"
Dan Willemsen490fd492015-11-24 17:53:15 -080020 "runtime"
Colin Cross7f64b6d2015-07-09 13:57:48 -070021 "strings"
22
Simon Shieldse9e78a52017-08-23 12:05:33 +100023 "bliss/soong/android"
24
Colin Cross7f64b6d2015-07-09 13:57:48 -070025 "github.com/google/blueprint/proptools"
26)
27
28func init() {
Colin Cross1e676be2016-10-12 14:38:15 -070029 PreDepsMutators(func(ctx RegisterMutatorsContext) {
30 ctx.BottomUp("variable", variableMutator).Parallel()
31 })
Colin Cross7f64b6d2015-07-09 13:57:48 -070032}
33
34type variableProperties struct {
35 Product_variables struct {
Dan Willemsen47cf66b2015-09-16 16:48:54 -070036 Platform_sdk_version struct {
37 Asflags []string
Colin Crossceeff0f2017-05-08 13:43:00 -070038 Cflags []string
Dan Willemsen47cf66b2015-09-16 16:48:54 -070039 }
Colin Cross83163192015-12-01 16:31:16 -080040
41 // unbundled_build is a catch-all property to annotate modules that don't build in one or
42 // more unbundled branches, usually due to dependencies missing from the manifest.
43 Unbundled_build struct {
44 Enabled *bool `android:"arch_variant"`
45 } `android:"arch_variant"`
Dan Willemsen496e3952016-01-05 14:27:55 -080046
47 Brillo struct {
Dan Willemsen7b872832016-10-03 23:36:47 -070048 Cflags []string
Dan Willemsen496e3952016-01-05 14:27:55 -080049 Version_script *string `android:"arch_variant"`
50 } `android:"arch_variant"`
Dan Willemsen97cae012016-03-01 14:08:42 -080051
52 Malloc_not_svelte struct {
53 Cflags []string
54 }
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070055
56 Safestack struct {
57 Cflags []string `android:"arch_variant"`
58 } `android:"arch_variant"`
Dan Willemsena6f7d152016-07-12 14:57:40 -070059
Dan Willemsen1be35382016-07-25 17:42:18 -070060 Binder32bit struct {
61 Cflags []string
62 }
Dan Willemsenfcebcd52016-09-22 14:49:10 -070063
Colin Cross0e409a22017-05-02 10:58:50 -070064 Device_uses_hwc2 struct {
65 Cflags []string
66 }
67
Colin Cross95436422017-05-04 13:57:05 -070068 Override_rs_driver struct {
69 Cflags []string
70 }
71
Steven Morelande24bc202017-05-18 15:44:02 -070072 // treble is true when a build is a Treble compliant device. This is automatically set when
73 // a build is shipped with Android O, but can be overriden. This controls such things as
74 // the sepolicy split and enabling the Treble linker namespaces.
75 Treble struct {
76 Cflags []string
77 }
78
Colin Cross6bc59ef2016-12-08 09:46:35 -080079 // debuggable is true for eng and userdebug builds, and can be used to turn on additional
80 // debugging features that don't significantly impact runtime behavior. userdebug builds
81 // are used for dogfooding and performance testing, and should be as similar to user builds
82 // as possible.
Dan Willemsenfcebcd52016-09-22 14:49:10 -070083 Debuggable struct {
Dan Willemsen7b872832016-10-03 23:36:47 -070084 Cflags []string
85 Cppflags []string
Carmen Jackson6fe956b2017-04-14 14:38:47 -070086 Init_rc []string
Dan Willemsenfcebcd52016-09-22 14:49:10 -070087 }
Colin Cross6bc59ef2016-12-08 09:46:35 -080088
89 // eng is true for -eng builds, and can be used to turn on additionaly heavyweight debugging
90 // features.
91 Eng struct {
92 Cflags []string
93 Cppflags []string
94 }
Colin Crossb2123aa2017-05-05 13:37:11 -070095
96 Pdk struct {
97 Enabled *bool
98 }
Simon Shieldse9e78a52017-08-23 12:05:33 +100099
Simon Shields0c6b9a22017-09-04 21:09:44 +1000100 // include bliss variables
101 Bliss android.Product_variables
Colin Cross83163192015-12-01 16:31:16 -0800102 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -0700103}
104
105var zeroProductVariables variableProperties
106
Colin Cross485e5722015-08-27 13:28:01 -0700107type productVariables struct {
Dan Willemsen174978c2016-05-11 00:27:49 -0700108 // Suffix to add to generated Makefiles
109 Make_suffix *string `json:",omitempty"`
110
Dan Albert30c9d6e2017-03-28 14:54:55 -0700111 Platform_sdk_version *int `json:",omitempty"`
112 Platform_version_all_codenames []string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700113
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700114 DeviceName *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700115 DeviceArch *string `json:",omitempty"`
116 DeviceArchVariant *string `json:",omitempty"`
117 DeviceCpuVariant *string `json:",omitempty"`
118 DeviceAbi *[]string `json:",omitempty"`
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700119 DeviceUsesClang *bool `json:",omitempty"`
Dan Willemsend2ede872016-11-18 14:54:24 -0800120 DeviceVndkVersion *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -0700121
122 DeviceSecondaryArch *string `json:",omitempty"`
123 DeviceSecondaryArchVariant *string `json:",omitempty"`
124 DeviceSecondaryCpuVariant *string `json:",omitempty"`
125 DeviceSecondaryAbi *[]string `json:",omitempty"`
126
127 HostArch *string `json:",omitempty"`
128 HostSecondaryArch *string `json:",omitempty"`
Dan Willemsen490fd492015-11-24 17:53:15 -0800129
130 CrossHost *string `json:",omitempty"`
131 CrossHostArch *string `json:",omitempty"`
132 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -0800133
Dan Willemsenb5038162016-03-16 12:35:33 -0700134 Allow_missing_dependencies *bool `json:",omitempty"`
135 Unbundled_build *bool `json:",omitempty"`
136 Brillo *bool `json:",omitempty"`
137 Malloc_not_svelte *bool `json:",omitempty"`
Evgenii Stepanov8391efa2016-05-12 18:04:18 -0700138 Safestack *bool `json:",omitempty"`
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700139 HostStaticBinaries *bool `json:",omitempty"`
Dan Willemsen1be35382016-07-25 17:42:18 -0700140 Binder32bit *bool `json:",omitempty"`
Colin Cross9d45bb72016-08-29 16:14:13 -0700141 UseGoma *bool `json:",omitempty"`
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700142 Debuggable *bool `json:",omitempty"`
Colin Cross6bc59ef2016-12-08 09:46:35 -0800143 Eng *bool `json:",omitempty"`
Vishwath Mohan1b017a72017-01-19 13:54:55 -0800144 EnableCFI *bool `json:",omitempty"`
Colin Cross0e409a22017-05-02 10:58:50 -0700145 Device_uses_hwc2 *bool `json:",omitempty"`
Steven Morelande24bc202017-05-18 15:44:02 -0700146 Treble *bool `json:",omitempty"`
Colin Crossdab550d2017-05-25 10:18:24 -0700147 Pdk *bool `json:",omitempty"`
Colin Cross16b23492016-01-06 14:41:07 -0800148
Dan Willemsen4353bc42016-12-05 17:16:02 -0800149 VendorPath *string `json:",omitempty"`
150
Dan Willemsena03cf6d2016-09-26 15:45:04 -0700151 ClangTidy *bool `json:",omitempty"`
152 TidyChecks *string `json:",omitempty"`
153
Ryan Campbell469a18a2017-02-27 09:01:54 -0800154 NativeCoverage *bool `json:",omitempty"`
155 CoveragePaths *[]string `json:",omitempty"`
156 CoverageExcludePaths *[]string `json:",omitempty"`
Dan Willemsen581341d2017-02-09 16:16:31 -0800157
Colin Cross1e7d3702016-08-24 15:25:47 -0700158 DevicePrefer32BitExecutables *bool `json:",omitempty"`
159 HostPrefer32BitExecutables *bool `json:",omitempty"`
160
Colin Cross23ae82a2016-11-02 14:34:39 -0700161 SanitizeHost []string `json:",omitempty"`
162 SanitizeDevice []string `json:",omitempty"`
163 SanitizeDeviceArch []string `json:",omitempty"`
Hiroshi Yamauchie2a10632016-12-19 13:44:41 -0800164
165 ArtUseReadBarrier *bool `json:",omitempty"`
Jack He8cc71432016-12-08 15:45:07 -0800166
167 BtConfigIncludeDir *string `json:",omitempty"`
Colin Cross95436422017-05-04 13:57:05 -0700168
169 Override_rs_driver *string `json:",omitempty"`
Simon Shieldse9e78a52017-08-23 12:05:33 +1000170
Simon Shields0c6b9a22017-09-04 21:09:44 +1000171 // include Bliss variables
172 Bliss android.ProductVariables
Colin Crossa6bc19e2015-09-16 13:53:42 -0700173}
174
175func boolPtr(v bool) *bool {
176 return &v
Colin Cross485e5722015-08-27 13:28:01 -0700177}
178
Dan Willemsen47cf66b2015-09-16 16:48:54 -0700179func intPtr(v int) *int {
180 return &v
181}
182
Colin Cross4225f652015-09-17 14:33:42 -0700183func stringPtr(v string) *string {
184 return &v
185}
186
Colin Cross27385972015-09-18 10:57:10 -0700187func (v *productVariables) SetDefaultConfig() {
188 *v = productVariables{
Dan Albert22c0b412016-11-10 11:08:00 -0800189 Platform_sdk_version: intPtr(24),
Colin Cross4225f652015-09-17 14:33:42 -0700190 HostArch: stringPtr("x86_64"),
191 HostSecondaryArch: stringPtr("x86"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700192 DeviceName: stringPtr("flounder"),
Colin Cross4225f652015-09-17 14:33:42 -0700193 DeviceArch: stringPtr("arm64"),
Dan Willemsena91d1272016-03-29 22:06:05 -0700194 DeviceArchVariant: stringPtr("armv8-a"),
Colin Cross4225f652015-09-17 14:33:42 -0700195 DeviceCpuVariant: stringPtr("denver64"),
196 DeviceAbi: &[]string{"arm64-v8a"},
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700197 DeviceUsesClang: boolPtr(true),
Colin Cross4225f652015-09-17 14:33:42 -0700198 DeviceSecondaryArch: stringPtr("arm"),
199 DeviceSecondaryArchVariant: stringPtr("armv7-a-neon"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700200 DeviceSecondaryCpuVariant: stringPtr("denver"),
Colin Cross4225f652015-09-17 14:33:42 -0700201 DeviceSecondaryAbi: &[]string{"armeabi-v7a"},
Dan Willemsen97cae012016-03-01 14:08:42 -0800202 Malloc_not_svelte: boolPtr(false),
Evgenii Stepanov8391efa2016-05-12 18:04:18 -0700203 Safestack: boolPtr(false),
Colin Cross485e5722015-08-27 13:28:01 -0700204 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800205
206 if runtime.GOOS == "linux" {
207 v.CrossHost = stringPtr("windows")
208 v.CrossHostArch = stringPtr("x86")
Dan Willemsen07cd0512016-02-03 23:16:33 -0800209 v.CrossHostSecondaryArch = stringPtr("x86_64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800210 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700211}
212
Colin Cross635c3b02016-05-18 15:37:25 -0700213func variableMutator(mctx BottomUpMutatorContext) {
214 var module Module
Colin Cross7f64b6d2015-07-09 13:57:48 -0700215 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -0700216 if module, ok = mctx.Module().(Module); !ok {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700217 return
218 }
219
220 // TODO: depend on config variable, create variants, propagate variants up tree
221 a := module.base()
Colin Cross06a931b2015-10-28 17:23:31 -0700222 variableValues := reflect.ValueOf(&a.variableProperties.Product_variables).Elem()
Colin Cross7f64b6d2015-07-09 13:57:48 -0700223 zeroValues := reflect.ValueOf(zeroProductVariables.Product_variables)
Simon Shields3b50ea92017-09-04 21:09:10 +1000224 valStruct := reflect.ValueOf(mctx.Config().(Config).ProductVariables)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700225
Simon Shields3b50ea92017-09-04 21:09:10 +1000226 doVariableMutation(mctx, a, variableValues, zeroValues, valStruct)
227
228}
229
230func doVariableMutation(mctx BottomUpMutatorContext, a *ModuleBase, variableValues reflect.Value, zeroValues reflect.Value,
231 valStruct reflect.Value) {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700232 for i := 0; i < variableValues.NumField(); i++ {
233 variableValue := variableValues.Field(i)
234 zeroValue := zeroValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700235 name := variableValues.Type().Field(i).Name
236 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700237
Colin Crossa6bc19e2015-09-16 13:53:42 -0700238 // Check that the variable was set for the product
Simon Shields3b50ea92017-09-04 21:09:10 +1000239 val := valStruct.FieldByName(name)
240 if val.IsValid() && val.Kind() == reflect.Struct {
241 doVariableMutation(mctx, a, variableValue, zeroValue, val)
242 continue
243 } else if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
Colin Crossa6bc19e2015-09-16 13:53:42 -0700244 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700245 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700246
247 val = val.Elem()
248
249 // For bools, check that the value is true
250 if val.Kind() == reflect.Bool && val.Bool() == false {
251 continue
252 }
253
254 // Check if any properties were set for the module
255 if reflect.DeepEqual(variableValue.Interface(), zeroValue.Interface()) {
256 continue
257 }
258
259 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700260 }
261}
262
Colin Cross635c3b02016-05-18 15:37:25 -0700263func (a *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -0700264 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
265
Colin Cross7e0eaf12017-05-05 16:16:24 -0700266 printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700267
Colin Cross06a931b2015-10-28 17:23:31 -0700268 err := proptools.AppendMatchingProperties(a.generalProperties,
269 productVariablePropertyValue.Addr().Interface(), nil)
270 if err != nil {
271 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
272 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
273 } else {
274 panic(err)
275 }
276 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700277}
278
Colin Cross7e0eaf12017-05-05 16:16:24 -0700279func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string,
280 productVariablePropertyValue reflect.Value, i int, err error) {
281
282 field := productVariablePropertyValue.Type().Field(i).Name
283 property := prefix + "." + proptools.PropertyNameForField(field)
284 ctx.PropertyErrorf(property, "%s", err)
285}
286
287func printfIntoProperties(ctx BottomUpMutatorContext, prefix string,
288 productVariablePropertyValue reflect.Value, variableValue interface{}) {
289
Colin Cross7f64b6d2015-07-09 13:57:48 -0700290 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
291 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -0800292 kind := propertyValue.Kind()
293 if kind == reflect.Ptr {
294 if propertyValue.IsNil() {
295 continue
296 }
297 propertyValue = propertyValue.Elem()
298 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700299 switch propertyValue.Kind() {
300 case reflect.String:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700301 err := printfIntoProperty(propertyValue, variableValue)
302 if err != nil {
303 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
304 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700305 case reflect.Slice:
306 for j := 0; j < propertyValue.Len(); j++ {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700307 err := printfIntoProperty(propertyValue.Index(j), variableValue)
308 if err != nil {
309 printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
310 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700311 }
Colin Crossdd0dbe62015-12-02 15:24:38 -0800312 case reflect.Bool:
313 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700314 case reflect.Struct:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700315 printfIntoProperties(ctx, prefix, propertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700316 default:
317 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
318 }
319 }
320}
321
Colin Cross7e0eaf12017-05-05 16:16:24 -0700322func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700323 s := propertyValue.String()
Colin Cross7e0eaf12017-05-05 16:16:24 -0700324
325 count := strings.Count(s, "%")
326 if count == 0 {
327 return nil
328 }
329
330 if count > 1 {
331 return fmt.Errorf("product variable properties only support a single '%%'")
332 }
333
Colin Cross7f64b6d2015-07-09 13:57:48 -0700334 if strings.Contains(s, "%d") {
335 switch v := variableValue.(type) {
336 case int:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700337 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700338 case bool:
339 if v {
Colin Cross7e0eaf12017-05-05 16:16:24 -0700340 variableValue = 1
341 } else {
342 variableValue = 0
Colin Cross7f64b6d2015-07-09 13:57:48 -0700343 }
344 default:
Colin Cross7e0eaf12017-05-05 16:16:24 -0700345 return fmt.Errorf("unsupported type %T for %%d", variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700346 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700347 } else if strings.Contains(s, "%s") {
348 switch variableValue.(type) {
349 case string:
350 // Nothing
351 default:
352 return fmt.Errorf("unsupported type %T for %%s", variableValue)
353 }
354 } else {
355 return fmt.Errorf("unsupported %% in product variable property")
Colin Cross7f64b6d2015-07-09 13:57:48 -0700356 }
Colin Cross7e0eaf12017-05-05 16:16:24 -0700357
358 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue)))
359
360 return nil
Colin Cross7f64b6d2015-07-09 13:57:48 -0700361}