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