Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -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 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "reflect" |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 20 | "runtime" |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 21 | "strings" |
| 22 | |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
| 24 | ) |
| 25 | |
| 26 | func init() { |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 27 | RegisterBottomUpMutator("variable", variableMutator) |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | type variableProperties struct { |
| 31 | Product_variables struct { |
Dan Willemsen | 47cf66b | 2015-09-16 16:48:54 -0700 | [diff] [blame] | 32 | Platform_sdk_version struct { |
| 33 | Asflags []string |
| 34 | } |
Colin Cross | 8316319 | 2015-12-01 16:31:16 -0800 | [diff] [blame] | 35 | |
| 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 Willemsen | 496e395 | 2016-01-05 14:27:55 -0800 | [diff] [blame] | 41 | |
| 42 | Brillo struct { |
| 43 | Version_script *string `android:"arch_variant"` |
| 44 | } `android:"arch_variant"` |
Dan Willemsen | 97cae01 | 2016-03-01 14:08:42 -0800 | [diff] [blame] | 45 | |
| 46 | Malloc_not_svelte struct { |
| 47 | Cflags []string |
| 48 | } |
Evgenii Stepanov | 8391efa | 2016-05-12 18:04:18 -0700 | [diff] [blame] | 49 | |
| 50 | Safestack struct { |
| 51 | Cflags []string `android:"arch_variant"` |
| 52 | } `android:"arch_variant"` |
Dan Willemsen | a6f7d15 | 2016-07-12 14:57:40 -0700 | [diff] [blame] | 53 | |
| 54 | Cpusets struct { |
| 55 | Cflags []string |
| 56 | } |
| 57 | |
| 58 | Schedboost struct { |
| 59 | Cflags []string |
| 60 | } |
Colin Cross | 8316319 | 2015-12-01 16:31:16 -0800 | [diff] [blame] | 61 | } `android:"arch_variant"` |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | var zeroProductVariables variableProperties |
| 65 | |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 66 | type productVariables struct { |
Dan Willemsen | 174978c | 2016-05-11 00:27:49 -0700 | [diff] [blame] | 67 | // Suffix to add to generated Makefiles |
| 68 | Make_suffix *string `json:",omitempty"` |
| 69 | |
Dan Willemsen | 97cae01 | 2016-03-01 14:08:42 -0800 | [diff] [blame] | 70 | Platform_sdk_version *int `json:",omitempty"` |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 71 | |
Dan Willemsen | 1d31ec3 | 2015-09-22 16:56:09 -0700 | [diff] [blame] | 72 | DeviceName *string `json:",omitempty"` |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 73 | DeviceArch *string `json:",omitempty"` |
| 74 | DeviceArchVariant *string `json:",omitempty"` |
| 75 | DeviceCpuVariant *string `json:",omitempty"` |
| 76 | DeviceAbi *[]string `json:",omitempty"` |
Dan Willemsen | dd0e2c3 | 2015-10-20 14:29:35 -0700 | [diff] [blame] | 77 | DeviceUsesClang *bool `json:",omitempty"` |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 78 | |
| 79 | DeviceSecondaryArch *string `json:",omitempty"` |
| 80 | DeviceSecondaryArchVariant *string `json:",omitempty"` |
| 81 | DeviceSecondaryCpuVariant *string `json:",omitempty"` |
| 82 | DeviceSecondaryAbi *[]string `json:",omitempty"` |
| 83 | |
| 84 | HostArch *string `json:",omitempty"` |
| 85 | HostSecondaryArch *string `json:",omitempty"` |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 86 | |
| 87 | CrossHost *string `json:",omitempty"` |
| 88 | CrossHostArch *string `json:",omitempty"` |
| 89 | CrossHostSecondaryArch *string `json:",omitempty"` |
Colin Cross | 8316319 | 2015-12-01 16:31:16 -0800 | [diff] [blame] | 90 | |
Dan Willemsen | b503816 | 2016-03-16 12:35:33 -0700 | [diff] [blame] | 91 | Allow_missing_dependencies *bool `json:",omitempty"` |
| 92 | Unbundled_build *bool `json:",omitempty"` |
| 93 | Brillo *bool `json:",omitempty"` |
| 94 | Malloc_not_svelte *bool `json:",omitempty"` |
Evgenii Stepanov | 8391efa | 2016-05-12 18:04:18 -0700 | [diff] [blame] | 95 | Safestack *bool `json:",omitempty"` |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 96 | HostStaticBinaries *bool `json:",omitempty"` |
Dan Willemsen | a6f7d15 | 2016-07-12 14:57:40 -0700 | [diff] [blame] | 97 | Cpusets *bool `json:",omitempty"` |
| 98 | Schedboost *bool `json:",omitempty"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 99 | |
| 100 | SanitizeHost *[]string `json:",omitempty"` |
| 101 | SanitizeDevice *[]string `json:",omitempty"` |
Colin Cross | a6bc19e | 2015-09-16 13:53:42 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | func boolPtr(v bool) *bool { |
| 105 | return &v |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Dan Willemsen | 47cf66b | 2015-09-16 16:48:54 -0700 | [diff] [blame] | 108 | func intPtr(v int) *int { |
| 109 | return &v |
| 110 | } |
| 111 | |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 112 | func stringPtr(v string) *string { |
| 113 | return &v |
| 114 | } |
| 115 | |
Colin Cross | 2738597 | 2015-09-18 10:57:10 -0700 | [diff] [blame] | 116 | func (v *productVariables) SetDefaultConfig() { |
| 117 | *v = productVariables{ |
Dan Willemsen | 5951c8a | 2016-07-19 19:08:14 -0700 | [diff] [blame^] | 118 | Platform_sdk_version: intPtr(23), |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 119 | HostArch: stringPtr("x86_64"), |
| 120 | HostSecondaryArch: stringPtr("x86"), |
Dan Willemsen | 1d31ec3 | 2015-09-22 16:56:09 -0700 | [diff] [blame] | 121 | DeviceName: stringPtr("flounder"), |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 122 | DeviceArch: stringPtr("arm64"), |
Dan Willemsen | a91d127 | 2016-03-29 22:06:05 -0700 | [diff] [blame] | 123 | DeviceArchVariant: stringPtr("armv8-a"), |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 124 | DeviceCpuVariant: stringPtr("denver64"), |
| 125 | DeviceAbi: &[]string{"arm64-v8a"}, |
Dan Willemsen | dd0e2c3 | 2015-10-20 14:29:35 -0700 | [diff] [blame] | 126 | DeviceUsesClang: boolPtr(true), |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 127 | DeviceSecondaryArch: stringPtr("arm"), |
| 128 | DeviceSecondaryArchVariant: stringPtr("armv7-a-neon"), |
Dan Willemsen | 1d31ec3 | 2015-09-22 16:56:09 -0700 | [diff] [blame] | 129 | DeviceSecondaryCpuVariant: stringPtr("denver"), |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 130 | DeviceSecondaryAbi: &[]string{"armeabi-v7a"}, |
Dan Willemsen | 97cae01 | 2016-03-01 14:08:42 -0800 | [diff] [blame] | 131 | Malloc_not_svelte: boolPtr(false), |
Evgenii Stepanov | 8391efa | 2016-05-12 18:04:18 -0700 | [diff] [blame] | 132 | Safestack: boolPtr(false), |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 133 | } |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 134 | |
| 135 | if runtime.GOOS == "linux" { |
| 136 | v.CrossHost = stringPtr("windows") |
| 137 | v.CrossHostArch = stringPtr("x86") |
Dan Willemsen | 07cd051 | 2016-02-03 23:16:33 -0800 | [diff] [blame] | 138 | v.CrossHostSecondaryArch = stringPtr("x86_64") |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 139 | } |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 142 | func variableMutator(mctx BottomUpMutatorContext) { |
| 143 | var module Module |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 144 | var ok bool |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 145 | if module, ok = mctx.Module().(Module); !ok { |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 146 | return |
| 147 | } |
| 148 | |
| 149 | // TODO: depend on config variable, create variants, propagate variants up tree |
| 150 | a := module.base() |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 151 | variableValues := reflect.ValueOf(&a.variableProperties.Product_variables).Elem() |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 152 | zeroValues := reflect.ValueOf(zeroProductVariables.Product_variables) |
| 153 | |
| 154 | for i := 0; i < variableValues.NumField(); i++ { |
| 155 | variableValue := variableValues.Field(i) |
| 156 | zeroValue := zeroValues.Field(i) |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 157 | name := variableValues.Type().Field(i).Name |
| 158 | property := "product_variables." + proptools.PropertyNameForField(name) |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 159 | |
Colin Cross | a6bc19e | 2015-09-16 13:53:42 -0700 | [diff] [blame] | 160 | // Check that the variable was set for the product |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 161 | val := reflect.ValueOf(mctx.Config().(Config).ProductVariables).FieldByName(name) |
Colin Cross | a6bc19e | 2015-09-16 13:53:42 -0700 | [diff] [blame] | 162 | if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() { |
| 163 | continue |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 164 | } |
Colin Cross | a6bc19e | 2015-09-16 13:53:42 -0700 | [diff] [blame] | 165 | |
| 166 | val = val.Elem() |
| 167 | |
| 168 | // For bools, check that the value is true |
| 169 | if val.Kind() == reflect.Bool && val.Bool() == false { |
| 170 | continue |
| 171 | } |
| 172 | |
| 173 | // Check if any properties were set for the module |
| 174 | if reflect.DeepEqual(variableValue.Interface(), zeroValue.Interface()) { |
| 175 | continue |
| 176 | } |
| 177 | |
| 178 | a.setVariableProperties(mctx, property, variableValue, val.Interface()) |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 182 | func (a *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext, |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 183 | prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) { |
| 184 | |
Colin Cross | dd0dbe6 | 2015-12-02 15:24:38 -0800 | [diff] [blame] | 185 | printfIntoProperties(productVariablePropertyValue, variableValue) |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 186 | |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 187 | err := proptools.AppendMatchingProperties(a.generalProperties, |
| 188 | productVariablePropertyValue.Addr().Interface(), nil) |
| 189 | if err != nil { |
| 190 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 191 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 192 | } else { |
| 193 | panic(err) |
| 194 | } |
| 195 | } |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | func printfIntoProperties(productVariablePropertyValue reflect.Value, variableValue interface{}) { |
| 199 | for i := 0; i < productVariablePropertyValue.NumField(); i++ { |
| 200 | propertyValue := productVariablePropertyValue.Field(i) |
Colin Cross | dd0dbe6 | 2015-12-02 15:24:38 -0800 | [diff] [blame] | 201 | kind := propertyValue.Kind() |
| 202 | if kind == reflect.Ptr { |
| 203 | if propertyValue.IsNil() { |
| 204 | continue |
| 205 | } |
| 206 | propertyValue = propertyValue.Elem() |
| 207 | } |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 208 | switch propertyValue.Kind() { |
| 209 | case reflect.String: |
| 210 | printfIntoProperty(propertyValue, variableValue) |
| 211 | case reflect.Slice: |
| 212 | for j := 0; j < propertyValue.Len(); j++ { |
| 213 | printfIntoProperty(propertyValue.Index(j), variableValue) |
| 214 | } |
Colin Cross | dd0dbe6 | 2015-12-02 15:24:38 -0800 | [diff] [blame] | 215 | case reflect.Bool: |
| 216 | // Nothing |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 217 | case reflect.Struct: |
| 218 | printfIntoProperties(propertyValue, variableValue) |
| 219 | default: |
| 220 | panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind())) |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) { |
| 226 | s := propertyValue.String() |
| 227 | // For now, we only support int formats |
| 228 | var i int |
| 229 | if strings.Contains(s, "%d") { |
| 230 | switch v := variableValue.(type) { |
| 231 | case int: |
| 232 | i = v |
| 233 | case bool: |
| 234 | if v { |
| 235 | i = 1 |
| 236 | } |
| 237 | default: |
| 238 | panic(fmt.Errorf("unsupported type %T", variableValue)) |
| 239 | } |
| 240 | propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, i))) |
| 241 | } |
| 242 | } |