blob: 29d6c7de1d15a151cc13168a6ef3d9dbf1d67c66 [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
Colin Cross7f64b6d2015-07-09 13:57:48 -070023 "github.com/google/blueprint/proptools"
24)
25
26func init() {
Colin Crosse8a67a72016-08-07 21:17:54 -070027 RegisterBottomUpMutator("variable", variableMutator).Parallel()
Colin Cross7f64b6d2015-07-09 13:57:48 -070028}
29
30type variableProperties struct {
31 Product_variables struct {
Dan Willemsen47cf66b2015-09-16 16:48:54 -070032 Platform_sdk_version struct {
33 Asflags []string
34 }
Colin Cross83163192015-12-01 16:31:16 -080035
36 // unbundled_build is a catch-all property to annotate modules that don't build in one or
37 // more unbundled branches, usually due to dependencies missing from the manifest.
38 Unbundled_build struct {
39 Enabled *bool `android:"arch_variant"`
40 } `android:"arch_variant"`
Dan Willemsen496e3952016-01-05 14:27:55 -080041
42 Brillo struct {
43 Version_script *string `android:"arch_variant"`
44 } `android:"arch_variant"`
Dan Willemsen97cae012016-03-01 14:08:42 -080045
46 Malloc_not_svelte struct {
47 Cflags []string
48 }
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070049
50 Safestack struct {
51 Cflags []string `android:"arch_variant"`
52 } `android:"arch_variant"`
Dan Willemsena6f7d152016-07-12 14:57:40 -070053
54 Cpusets struct {
55 Cflags []string
56 }
57
58 Schedboost struct {
59 Cflags []string
60 }
Dan Willemsen1be35382016-07-25 17:42:18 -070061
62 Binder32bit struct {
63 Cflags []string
64 }
Dan Willemsenfcebcd52016-09-22 14:49:10 -070065
66 Debuggable struct {
67 Cflags []string
68 }
Colin Cross83163192015-12-01 16:31:16 -080069 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -070070}
71
72var zeroProductVariables variableProperties
73
Colin Cross485e5722015-08-27 13:28:01 -070074type productVariables struct {
Dan Willemsen174978c2016-05-11 00:27:49 -070075 // Suffix to add to generated Makefiles
76 Make_suffix *string `json:",omitempty"`
77
Dan Willemsen97cae012016-03-01 14:08:42 -080078 Platform_sdk_version *int `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070079
Dan Willemsen1d31ec32015-09-22 16:56:09 -070080 DeviceName *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070081 DeviceArch *string `json:",omitempty"`
82 DeviceArchVariant *string `json:",omitempty"`
83 DeviceCpuVariant *string `json:",omitempty"`
84 DeviceAbi *[]string `json:",omitempty"`
Dan Willemsendd0e2c32015-10-20 14:29:35 -070085 DeviceUsesClang *bool `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070086
87 DeviceSecondaryArch *string `json:",omitempty"`
88 DeviceSecondaryArchVariant *string `json:",omitempty"`
89 DeviceSecondaryCpuVariant *string `json:",omitempty"`
90 DeviceSecondaryAbi *[]string `json:",omitempty"`
91
92 HostArch *string `json:",omitempty"`
93 HostSecondaryArch *string `json:",omitempty"`
Dan Willemsen490fd492015-11-24 17:53:15 -080094
95 CrossHost *string `json:",omitempty"`
96 CrossHostArch *string `json:",omitempty"`
97 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -080098
Dan Willemsenb5038162016-03-16 12:35:33 -070099 Allow_missing_dependencies *bool `json:",omitempty"`
100 Unbundled_build *bool `json:",omitempty"`
101 Brillo *bool `json:",omitempty"`
102 Malloc_not_svelte *bool `json:",omitempty"`
Evgenii Stepanov8391efa2016-05-12 18:04:18 -0700103 Safestack *bool `json:",omitempty"`
Dan Willemsen36cff8b2016-05-17 16:35:02 -0700104 HostStaticBinaries *bool `json:",omitempty"`
Dan Willemsena6f7d152016-07-12 14:57:40 -0700105 Cpusets *bool `json:",omitempty"`
106 Schedboost *bool `json:",omitempty"`
Dan Willemsen1be35382016-07-25 17:42:18 -0700107 Binder32bit *bool `json:",omitempty"`
Colin Cross9d45bb72016-08-29 16:14:13 -0700108 UseGoma *bool `json:",omitempty"`
Dan Willemsenfcebcd52016-09-22 14:49:10 -0700109 Debuggable *bool `json:",omitempty"`
Colin Cross16b23492016-01-06 14:41:07 -0800110
Colin Cross1e7d3702016-08-24 15:25:47 -0700111 DevicePrefer32BitExecutables *bool `json:",omitempty"`
112 HostPrefer32BitExecutables *bool `json:",omitempty"`
113
Colin Cross16b23492016-01-06 14:41:07 -0800114 SanitizeHost *[]string `json:",omitempty"`
115 SanitizeDevice *[]string `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -0700116}
117
118func boolPtr(v bool) *bool {
119 return &v
Colin Cross485e5722015-08-27 13:28:01 -0700120}
121
Dan Willemsen47cf66b2015-09-16 16:48:54 -0700122func intPtr(v int) *int {
123 return &v
124}
125
Colin Cross4225f652015-09-17 14:33:42 -0700126func stringPtr(v string) *string {
127 return &v
128}
129
Colin Cross27385972015-09-18 10:57:10 -0700130func (v *productVariables) SetDefaultConfig() {
131 *v = productVariables{
Dan Willemsen5951c8a2016-07-19 19:08:14 -0700132 Platform_sdk_version: intPtr(23),
Colin Cross4225f652015-09-17 14:33:42 -0700133 HostArch: stringPtr("x86_64"),
134 HostSecondaryArch: stringPtr("x86"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700135 DeviceName: stringPtr("flounder"),
Colin Cross4225f652015-09-17 14:33:42 -0700136 DeviceArch: stringPtr("arm64"),
Dan Willemsena91d1272016-03-29 22:06:05 -0700137 DeviceArchVariant: stringPtr("armv8-a"),
Colin Cross4225f652015-09-17 14:33:42 -0700138 DeviceCpuVariant: stringPtr("denver64"),
139 DeviceAbi: &[]string{"arm64-v8a"},
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700140 DeviceUsesClang: boolPtr(true),
Colin Cross4225f652015-09-17 14:33:42 -0700141 DeviceSecondaryArch: stringPtr("arm"),
142 DeviceSecondaryArchVariant: stringPtr("armv7-a-neon"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700143 DeviceSecondaryCpuVariant: stringPtr("denver"),
Colin Cross4225f652015-09-17 14:33:42 -0700144 DeviceSecondaryAbi: &[]string{"armeabi-v7a"},
Dan Willemsen97cae012016-03-01 14:08:42 -0800145 Malloc_not_svelte: boolPtr(false),
Evgenii Stepanov8391efa2016-05-12 18:04:18 -0700146 Safestack: boolPtr(false),
Colin Cross485e5722015-08-27 13:28:01 -0700147 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800148
149 if runtime.GOOS == "linux" {
150 v.CrossHost = stringPtr("windows")
151 v.CrossHostArch = stringPtr("x86")
Dan Willemsen07cd0512016-02-03 23:16:33 -0800152 v.CrossHostSecondaryArch = stringPtr("x86_64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800153 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700154}
155
Colin Cross635c3b02016-05-18 15:37:25 -0700156func variableMutator(mctx BottomUpMutatorContext) {
157 var module Module
Colin Cross7f64b6d2015-07-09 13:57:48 -0700158 var ok bool
Colin Cross635c3b02016-05-18 15:37:25 -0700159 if module, ok = mctx.Module().(Module); !ok {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700160 return
161 }
162
163 // TODO: depend on config variable, create variants, propagate variants up tree
164 a := module.base()
Colin Cross06a931b2015-10-28 17:23:31 -0700165 variableValues := reflect.ValueOf(&a.variableProperties.Product_variables).Elem()
Colin Cross7f64b6d2015-07-09 13:57:48 -0700166 zeroValues := reflect.ValueOf(zeroProductVariables.Product_variables)
167
168 for i := 0; i < variableValues.NumField(); i++ {
169 variableValue := variableValues.Field(i)
170 zeroValue := zeroValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700171 name := variableValues.Type().Field(i).Name
172 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700173
Colin Crossa6bc19e2015-09-16 13:53:42 -0700174 // Check that the variable was set for the product
Colin Cross485e5722015-08-27 13:28:01 -0700175 val := reflect.ValueOf(mctx.Config().(Config).ProductVariables).FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -0700176 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
177 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700178 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700179
180 val = val.Elem()
181
182 // For bools, check that the value is true
183 if val.Kind() == reflect.Bool && val.Bool() == false {
184 continue
185 }
186
187 // Check if any properties were set for the module
188 if reflect.DeepEqual(variableValue.Interface(), zeroValue.Interface()) {
189 continue
190 }
191
192 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700193 }
194}
195
Colin Cross635c3b02016-05-18 15:37:25 -0700196func (a *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -0700197 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
198
Colin Crossdd0dbe62015-12-02 15:24:38 -0800199 printfIntoProperties(productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700200
Colin Cross06a931b2015-10-28 17:23:31 -0700201 err := proptools.AppendMatchingProperties(a.generalProperties,
202 productVariablePropertyValue.Addr().Interface(), nil)
203 if err != nil {
204 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
205 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
206 } else {
207 panic(err)
208 }
209 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700210}
211
212func printfIntoProperties(productVariablePropertyValue reflect.Value, variableValue interface{}) {
213 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
214 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -0800215 kind := propertyValue.Kind()
216 if kind == reflect.Ptr {
217 if propertyValue.IsNil() {
218 continue
219 }
220 propertyValue = propertyValue.Elem()
221 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700222 switch propertyValue.Kind() {
223 case reflect.String:
224 printfIntoProperty(propertyValue, variableValue)
225 case reflect.Slice:
226 for j := 0; j < propertyValue.Len(); j++ {
227 printfIntoProperty(propertyValue.Index(j), variableValue)
228 }
Colin Crossdd0dbe62015-12-02 15:24:38 -0800229 case reflect.Bool:
230 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700231 case reflect.Struct:
232 printfIntoProperties(propertyValue, variableValue)
233 default:
234 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
235 }
236 }
237}
238
239func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) {
240 s := propertyValue.String()
241 // For now, we only support int formats
242 var i int
243 if strings.Contains(s, "%d") {
244 switch v := variableValue.(type) {
245 case int:
246 i = v
247 case bool:
248 if v {
249 i = 1
250 }
251 default:
252 panic(fmt.Errorf("unsupported type %T", variableValue))
253 }
254 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, i)))
255 }
256}