blob: 7f8a6e620c700ffc62833311422ac3bf661713af [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 }
Colin Cross83163192015-12-01 16:31:16 -080049 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -070050}
51
52var zeroProductVariables variableProperties
53
Colin Cross485e5722015-08-27 13:28:01 -070054type productVariables struct {
Dan Willemsen174978c2016-05-11 00:27:49 -070055 // Suffix to add to generated Makefiles
56 Make_suffix *string `json:",omitempty"`
57
Dan Willemsen97cae012016-03-01 14:08:42 -080058 Platform_sdk_version *int `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070059
Dan Willemsen1d31ec32015-09-22 16:56:09 -070060 DeviceName *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070061 DeviceArch *string `json:",omitempty"`
62 DeviceArchVariant *string `json:",omitempty"`
63 DeviceCpuVariant *string `json:",omitempty"`
64 DeviceAbi *[]string `json:",omitempty"`
Dan Willemsendd0e2c32015-10-20 14:29:35 -070065 DeviceUsesClang *bool `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070066
67 DeviceSecondaryArch *string `json:",omitempty"`
68 DeviceSecondaryArchVariant *string `json:",omitempty"`
69 DeviceSecondaryCpuVariant *string `json:",omitempty"`
70 DeviceSecondaryAbi *[]string `json:",omitempty"`
71
72 HostArch *string `json:",omitempty"`
73 HostSecondaryArch *string `json:",omitempty"`
Dan Willemsen490fd492015-11-24 17:53:15 -080074
75 CrossHost *string `json:",omitempty"`
76 CrossHostArch *string `json:",omitempty"`
77 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -080078
Dan Willemsenb5038162016-03-16 12:35:33 -070079 Allow_missing_dependencies *bool `json:",omitempty"`
80 Unbundled_build *bool `json:",omitempty"`
81 Brillo *bool `json:",omitempty"`
82 Malloc_not_svelte *bool `json:",omitempty"`
Colin Cross16b23492016-01-06 14:41:07 -080083
84 SanitizeHost *[]string `json:",omitempty"`
85 SanitizeDevice *[]string `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -070086}
87
88func boolPtr(v bool) *bool {
89 return &v
Colin Cross485e5722015-08-27 13:28:01 -070090}
91
Dan Willemsen47cf66b2015-09-16 16:48:54 -070092func intPtr(v int) *int {
93 return &v
94}
95
Colin Cross4225f652015-09-17 14:33:42 -070096func stringPtr(v string) *string {
97 return &v
98}
99
Colin Cross27385972015-09-18 10:57:10 -0700100func (v *productVariables) SetDefaultConfig() {
101 *v = productVariables{
Colin Cross4225f652015-09-17 14:33:42 -0700102 Platform_sdk_version: intPtr(22),
103 HostArch: stringPtr("x86_64"),
104 HostSecondaryArch: stringPtr("x86"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700105 DeviceName: stringPtr("flounder"),
Colin Cross4225f652015-09-17 14:33:42 -0700106 DeviceArch: stringPtr("arm64"),
Dan Willemsena91d1272016-03-29 22:06:05 -0700107 DeviceArchVariant: stringPtr("armv8-a"),
Colin Cross4225f652015-09-17 14:33:42 -0700108 DeviceCpuVariant: stringPtr("denver64"),
109 DeviceAbi: &[]string{"arm64-v8a"},
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700110 DeviceUsesClang: boolPtr(true),
Colin Cross4225f652015-09-17 14:33:42 -0700111 DeviceSecondaryArch: stringPtr("arm"),
112 DeviceSecondaryArchVariant: stringPtr("armv7-a-neon"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700113 DeviceSecondaryCpuVariant: stringPtr("denver"),
Colin Cross4225f652015-09-17 14:33:42 -0700114 DeviceSecondaryAbi: &[]string{"armeabi-v7a"},
Dan Willemsen97cae012016-03-01 14:08:42 -0800115 Malloc_not_svelte: boolPtr(false),
Colin Cross485e5722015-08-27 13:28:01 -0700116 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800117
118 if runtime.GOOS == "linux" {
119 v.CrossHost = stringPtr("windows")
120 v.CrossHostArch = stringPtr("x86")
Dan Willemsen07cd0512016-02-03 23:16:33 -0800121 v.CrossHostSecondaryArch = stringPtr("x86_64")
Dan Willemsen490fd492015-11-24 17:53:15 -0800122 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700123}
124
Colin Cross6362e272015-10-29 15:25:03 -0700125func variableMutator(mctx AndroidBottomUpMutatorContext) {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700126 var module AndroidModule
127 var ok bool
128 if module, ok = mctx.Module().(AndroidModule); !ok {
129 return
130 }
131
132 // TODO: depend on config variable, create variants, propagate variants up tree
133 a := module.base()
Colin Cross06a931b2015-10-28 17:23:31 -0700134 variableValues := reflect.ValueOf(&a.variableProperties.Product_variables).Elem()
Colin Cross7f64b6d2015-07-09 13:57:48 -0700135 zeroValues := reflect.ValueOf(zeroProductVariables.Product_variables)
136
137 for i := 0; i < variableValues.NumField(); i++ {
138 variableValue := variableValues.Field(i)
139 zeroValue := zeroValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700140 name := variableValues.Type().Field(i).Name
141 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700142
Colin Crossa6bc19e2015-09-16 13:53:42 -0700143 // Check that the variable was set for the product
Colin Cross485e5722015-08-27 13:28:01 -0700144 val := reflect.ValueOf(mctx.Config().(Config).ProductVariables).FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -0700145 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
146 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700147 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700148
149 val = val.Elem()
150
151 // For bools, check that the value is true
152 if val.Kind() == reflect.Bool && val.Bool() == false {
153 continue
154 }
155
156 // Check if any properties were set for the module
157 if reflect.DeepEqual(variableValue.Interface(), zeroValue.Interface()) {
158 continue
159 }
160
161 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700162 }
163}
164
Colin Cross6362e272015-10-29 15:25:03 -0700165func (a *AndroidModuleBase) setVariableProperties(ctx AndroidBottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -0700166 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
167
Colin Crossdd0dbe62015-12-02 15:24:38 -0800168 printfIntoProperties(productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700169
Colin Cross06a931b2015-10-28 17:23:31 -0700170 err := proptools.AppendMatchingProperties(a.generalProperties,
171 productVariablePropertyValue.Addr().Interface(), nil)
172 if err != nil {
173 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
174 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
175 } else {
176 panic(err)
177 }
178 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700179}
180
181func printfIntoProperties(productVariablePropertyValue reflect.Value, variableValue interface{}) {
182 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
183 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -0800184 kind := propertyValue.Kind()
185 if kind == reflect.Ptr {
186 if propertyValue.IsNil() {
187 continue
188 }
189 propertyValue = propertyValue.Elem()
190 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700191 switch propertyValue.Kind() {
192 case reflect.String:
193 printfIntoProperty(propertyValue, variableValue)
194 case reflect.Slice:
195 for j := 0; j < propertyValue.Len(); j++ {
196 printfIntoProperty(propertyValue.Index(j), variableValue)
197 }
Colin Crossdd0dbe62015-12-02 15:24:38 -0800198 case reflect.Bool:
199 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700200 case reflect.Struct:
201 printfIntoProperties(propertyValue, variableValue)
202 default:
203 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
204 }
205 }
206}
207
208func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) {
209 s := propertyValue.String()
210 // For now, we only support int formats
211 var i int
212 if strings.Contains(s, "%d") {
213 switch v := variableValue.(type) {
214 case int:
215 i = v
216 case bool:
217 if v {
218 i = 1
219 }
220 default:
221 panic(fmt.Errorf("unsupported type %T", variableValue))
222 }
223 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, i)))
224 }
225}