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