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