blob: 0b5df07447cb99c96be157b38eec32b1f533853f [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
15package common
16
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 Cross6362e272015-10-29 15:25:03 -070027 RegisterBottomUpMutator("variable", variableMutator)
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"`
Colin Cross83163192015-12-01 16:31:16 -080053 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -070054}
55
56var zeroProductVariables variableProperties
57
Colin Cross485e5722015-08-27 13:28:01 -070058type productVariables struct {
Dan Willemsen174978c2016-05-11 00:27:49 -070059 // Suffix to add to generated Makefiles
60 Make_suffix *string `json:",omitempty"`
61
Dan Willemsen97cae012016-03-01 14:08:42 -080062 Platform_sdk_version *int `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070063
Dan Willemsen1d31ec32015-09-22 16:56:09 -070064 DeviceName *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070065 DeviceArch *string `json:",omitempty"`
66 DeviceArchVariant *string `json:",omitempty"`
67 DeviceCpuVariant *string `json:",omitempty"`
68 DeviceAbi *[]string `json:",omitempty"`
Dan Willemsendd0e2c32015-10-20 14:29:35 -070069 DeviceUsesClang *bool `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070070
71 DeviceSecondaryArch *string `json:",omitempty"`
72 DeviceSecondaryArchVariant *string `json:",omitempty"`
73 DeviceSecondaryCpuVariant *string `json:",omitempty"`
74 DeviceSecondaryAbi *[]string `json:",omitempty"`
75
76 HostArch *string `json:",omitempty"`
77 HostSecondaryArch *string `json:",omitempty"`
Dan Willemsen490fd492015-11-24 17:53:15 -080078
79 CrossHost *string `json:",omitempty"`
80 CrossHostArch *string `json:",omitempty"`
81 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -080082
Dan Willemsenb5038162016-03-16 12:35:33 -070083 Allow_missing_dependencies *bool `json:",omitempty"`
84 Unbundled_build *bool `json:",omitempty"`
85 Brillo *bool `json:",omitempty"`
86 Malloc_not_svelte *bool `json:",omitempty"`
Evgenii Stepanov8391efa2016-05-12 18:04:18 -070087 Safestack *bool `json:",omitempty"`
Colin Cross16b23492016-01-06 14:41:07 -080088
89 SanitizeHost *[]string `json:",omitempty"`
90 SanitizeDevice *[]string `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -070091}
92
93func boolPtr(v bool) *bool {
94 return &v
Colin Cross485e5722015-08-27 13:28:01 -070095}
96
Dan Willemsen47cf66b2015-09-16 16:48:54 -070097func intPtr(v int) *int {
98 return &v
99}
100
Colin Cross4225f652015-09-17 14:33:42 -0700101func stringPtr(v string) *string {
102 return &v
103}
104
Colin Cross27385972015-09-18 10:57:10 -0700105func (v *productVariables) SetDefaultConfig() {
106 *v = productVariables{
Colin Cross4225f652015-09-17 14:33:42 -0700107 Platform_sdk_version: intPtr(22),
108 HostArch: stringPtr("x86_64"),
109 HostSecondaryArch: stringPtr("x86"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700110 DeviceName: stringPtr("flounder"),
Colin Cross4225f652015-09-17 14:33:42 -0700111 DeviceArch: stringPtr("arm64"),
Dan Willemsena91d1272016-03-29 22:06:05 -0700112 DeviceArchVariant: stringPtr("armv8-a"),
Colin Cross4225f652015-09-17 14:33:42 -0700113 DeviceCpuVariant: stringPtr("denver64"),
114 DeviceAbi: &[]string{"arm64-v8a"},
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700115 DeviceUsesClang: boolPtr(true),
Colin Cross4225f652015-09-17 14:33:42 -0700116 DeviceSecondaryArch: stringPtr("arm"),
117 DeviceSecondaryArchVariant: stringPtr("armv7-a-neon"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700118 DeviceSecondaryCpuVariant: stringPtr("denver"),
Colin Cross4225f652015-09-17 14:33:42 -0700119 DeviceSecondaryAbi: &[]string{"armeabi-v7a"},
Dan Willemsen97cae012016-03-01 14:08:42 -0800120 Malloc_not_svelte: boolPtr(false),
Evgenii Stepanov8391efa2016-05-12 18:04:18 -0700121 Safestack: boolPtr(false),
Colin Cross485e5722015-08-27 13:28:01 -0700122 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800123
124 if runtime.GOOS == "linux" {
125 v.CrossHost = stringPtr("windows")
126 v.CrossHostArch = stringPtr("x86")
Dan Willemsen07cd0512016-02-03 23:16:33 -0800127 v.CrossHostSecondaryArch = stringPtr("x86_64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800128 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700129}
130
Colin Cross6362e272015-10-29 15:25:03 -0700131func variableMutator(mctx AndroidBottomUpMutatorContext) {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700132 var module AndroidModule
133 var ok bool
134 if module, ok = mctx.Module().(AndroidModule); !ok {
135 return
136 }
137
138 // TODO: depend on config variable, create variants, propagate variants up tree
139 a := module.base()
Colin Cross06a931b2015-10-28 17:23:31 -0700140 variableValues := reflect.ValueOf(&a.variableProperties.Product_variables).Elem()
Colin Cross7f64b6d2015-07-09 13:57:48 -0700141 zeroValues := reflect.ValueOf(zeroProductVariables.Product_variables)
142
143 for i := 0; i < variableValues.NumField(); i++ {
144 variableValue := variableValues.Field(i)
145 zeroValue := zeroValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700146 name := variableValues.Type().Field(i).Name
147 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700148
Colin Crossa6bc19e2015-09-16 13:53:42 -0700149 // Check that the variable was set for the product
Colin Cross485e5722015-08-27 13:28:01 -0700150 val := reflect.ValueOf(mctx.Config().(Config).ProductVariables).FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -0700151 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
152 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700153 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700154
155 val = val.Elem()
156
157 // For bools, check that the value is true
158 if val.Kind() == reflect.Bool && val.Bool() == false {
159 continue
160 }
161
162 // Check if any properties were set for the module
163 if reflect.DeepEqual(variableValue.Interface(), zeroValue.Interface()) {
164 continue
165 }
166
167 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700168 }
169}
170
Colin Cross6362e272015-10-29 15:25:03 -0700171func (a *AndroidModuleBase) setVariableProperties(ctx AndroidBottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -0700172 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
173
Colin Crossdd0dbe62015-12-02 15:24:38 -0800174 printfIntoProperties(productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700175
Colin Cross06a931b2015-10-28 17:23:31 -0700176 err := proptools.AppendMatchingProperties(a.generalProperties,
177 productVariablePropertyValue.Addr().Interface(), nil)
178 if err != nil {
179 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
180 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
181 } else {
182 panic(err)
183 }
184 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700185}
186
187func printfIntoProperties(productVariablePropertyValue reflect.Value, variableValue interface{}) {
188 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
189 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -0800190 kind := propertyValue.Kind()
191 if kind == reflect.Ptr {
192 if propertyValue.IsNil() {
193 continue
194 }
195 propertyValue = propertyValue.Elem()
196 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700197 switch propertyValue.Kind() {
198 case reflect.String:
199 printfIntoProperty(propertyValue, variableValue)
200 case reflect.Slice:
201 for j := 0; j < propertyValue.Len(); j++ {
202 printfIntoProperty(propertyValue.Index(j), variableValue)
203 }
Colin Crossdd0dbe62015-12-02 15:24:38 -0800204 case reflect.Bool:
205 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700206 case reflect.Struct:
207 printfIntoProperties(propertyValue, variableValue)
208 default:
209 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
210 }
211 }
212}
213
214func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) {
215 s := propertyValue.String()
216 // For now, we only support int formats
217 var i int
218 if strings.Contains(s, "%d") {
219 switch v := variableValue.(type) {
220 case int:
221 i = v
222 case bool:
223 if v {
224 i = 1
225 }
226 default:
227 panic(fmt.Errorf("unsupported type %T", variableValue))
228 }
229 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, i)))
230 }
231}