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