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