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