blob: c899bb414215e78b079d69594140f9233a3c9cc6 [file] [log] [blame]
Colin Cross7f64b6d2015-07-09 13:57:48 -07001// 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
15package common
16
17import (
18 "fmt"
19 "reflect"
Dan Willemsen490fd492015-11-24 17:53:15 -080020 "runtime"
Colin Cross7f64b6d2015-07-09 13:57:48 -070021 "strings"
22
Colin Cross7f64b6d2015-07-09 13:57:48 -070023 "github.com/google/blueprint/proptools"
24)
25
26func init() {
Colin Cross6362e272015-10-29 15:25:03 -070027 RegisterBottomUpMutator("variable", variableMutator)
Colin Cross7f64b6d2015-07-09 13:57:48 -070028}
29
30type variableProperties struct {
31 Product_variables struct {
Colin Cross7f64b6d2015-07-09 13:57:48 -070032 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 }
Dan Willemsen47cf66b2015-09-16 16:48:54 -070042 Platform_sdk_version struct {
43 Asflags []string
44 }
Colin Cross83163192015-12-01 16:31:16 -080045
46 // unbundled_build is a catch-all property to annotate modules that don't build in one or
47 // more unbundled branches, usually due to dependencies missing from the manifest.
48 Unbundled_build struct {
49 Enabled *bool `android:"arch_variant"`
50 } `android:"arch_variant"`
Dan Willemsen496e3952016-01-05 14:27:55 -080051
52 Brillo struct {
53 Version_script *string `android:"arch_variant"`
54 } `android:"arch_variant"`
Colin Cross83163192015-12-01 16:31:16 -080055 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -070056}
57
58var zeroProductVariables variableProperties
59
Colin Cross485e5722015-08-27 13:28:01 -070060type productVariables struct {
Colin Crossa6bc19e2015-09-16 13:53:42 -070061 Device_uses_jemalloc *bool `json:",omitempty"`
62 Device_uses_dlmalloc *bool `json:",omitempty"`
Dan Willemsen1d31ec32015-09-22 16:56:09 -070063 Platform_sdk_version *int `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070064
Dan Willemsen1d31ec32015-09-22 16:56:09 -070065 DeviceName *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070066 DeviceArch *string `json:",omitempty"`
67 DeviceArchVariant *string `json:",omitempty"`
68 DeviceCpuVariant *string `json:",omitempty"`
69 DeviceAbi *[]string `json:",omitempty"`
Dan Willemsendd0e2c32015-10-20 14:29:35 -070070 DeviceUsesClang *bool `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070071
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 Willemsen490fd492015-11-24 17:53:15 -080079
80 CrossHost *string `json:",omitempty"`
81 CrossHostArch *string `json:",omitempty"`
82 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -080083
84 Unbundled_build *bool `json:",omitempty"`
Colin Cross7b66f152015-12-15 16:07:43 -080085 Brillo *bool `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -070086}
87
88func boolPtr(v bool) *bool {
89 return &v
Colin Cross485e5722015-08-27 13:28:01 -070090}
91
Dan Willemsen47cf66b2015-09-16 16:48:54 -070092func intPtr(v int) *int {
93 return &v
94}
95
Colin Cross4225f652015-09-17 14:33:42 -070096func stringPtr(v string) *string {
97 return &v
98}
99
Colin Cross27385972015-09-18 10:57:10 -0700100func (v *productVariables) SetDefaultConfig() {
101 *v = productVariables{
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700102 Device_uses_dlmalloc: boolPtr(true),
Colin Cross4225f652015-09-17 14:33:42 -0700103 Platform_sdk_version: intPtr(22),
104 HostArch: stringPtr("x86_64"),
105 HostSecondaryArch: stringPtr("x86"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700106 DeviceName: stringPtr("flounder"),
Colin Cross4225f652015-09-17 14:33:42 -0700107 DeviceArch: stringPtr("arm64"),
108 DeviceCpuVariant: stringPtr("denver64"),
109 DeviceAbi: &[]string{"arm64-v8a"},
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700110 DeviceUsesClang: boolPtr(true),
Colin Cross4225f652015-09-17 14:33:42 -0700111 DeviceSecondaryArch: stringPtr("arm"),
112 DeviceSecondaryArchVariant: stringPtr("armv7-a-neon"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700113 DeviceSecondaryCpuVariant: stringPtr("denver"),
Colin Cross4225f652015-09-17 14:33:42 -0700114 DeviceSecondaryAbi: &[]string{"armeabi-v7a"},
Colin Cross485e5722015-08-27 13:28:01 -0700115 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800116
117 if runtime.GOOS == "linux" {
118 v.CrossHost = stringPtr("windows")
119 v.CrossHostArch = stringPtr("x86")
120 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700121}
122
Colin Cross6362e272015-10-29 15:25:03 -0700123func variableMutator(mctx AndroidBottomUpMutatorContext) {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700124 var module AndroidModule
125 var ok bool
126 if module, ok = mctx.Module().(AndroidModule); !ok {
127 return
128 }
129
130 // TODO: depend on config variable, create variants, propagate variants up tree
131 a := module.base()
Colin Cross06a931b2015-10-28 17:23:31 -0700132 variableValues := reflect.ValueOf(&a.variableProperties.Product_variables).Elem()
Colin Cross7f64b6d2015-07-09 13:57:48 -0700133 zeroValues := reflect.ValueOf(zeroProductVariables.Product_variables)
134
135 for i := 0; i < variableValues.NumField(); i++ {
136 variableValue := variableValues.Field(i)
137 zeroValue := zeroValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700138 name := variableValues.Type().Field(i).Name
139 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700140
Colin Crossa6bc19e2015-09-16 13:53:42 -0700141 // Check that the variable was set for the product
Colin Cross485e5722015-08-27 13:28:01 -0700142 val := reflect.ValueOf(mctx.Config().(Config).ProductVariables).FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -0700143 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
144 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700145 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700146
147 val = val.Elem()
148
149 // For bools, check that the value is true
150 if val.Kind() == reflect.Bool && val.Bool() == false {
151 continue
152 }
153
154 // Check if any properties were set for the module
155 if reflect.DeepEqual(variableValue.Interface(), zeroValue.Interface()) {
156 continue
157 }
158
159 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700160 }
161}
162
Colin Cross6362e272015-10-29 15:25:03 -0700163func (a *AndroidModuleBase) setVariableProperties(ctx AndroidBottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -0700164 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
165
Colin Crossdd0dbe62015-12-02 15:24:38 -0800166 printfIntoProperties(productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700167
Colin Cross06a931b2015-10-28 17:23:31 -0700168 err := proptools.AppendMatchingProperties(a.generalProperties,
169 productVariablePropertyValue.Addr().Interface(), nil)
170 if err != nil {
171 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
172 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
173 } else {
174 panic(err)
175 }
176 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700177}
178
179func printfIntoProperties(productVariablePropertyValue reflect.Value, variableValue interface{}) {
180 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
181 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -0800182 kind := propertyValue.Kind()
183 if kind == reflect.Ptr {
184 if propertyValue.IsNil() {
185 continue
186 }
187 propertyValue = propertyValue.Elem()
188 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700189 switch propertyValue.Kind() {
190 case reflect.String:
191 printfIntoProperty(propertyValue, variableValue)
192 case reflect.Slice:
193 for j := 0; j < propertyValue.Len(); j++ {
194 printfIntoProperty(propertyValue.Index(j), variableValue)
195 }
Colin Crossdd0dbe62015-12-02 15:24:38 -0800196 case reflect.Bool:
197 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700198 case reflect.Struct:
199 printfIntoProperties(propertyValue, variableValue)
200 default:
201 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
202 }
203 }
204}
205
206func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) {
207 s := propertyValue.String()
208 // For now, we only support int formats
209 var i int
210 if strings.Contains(s, "%d") {
211 switch v := variableValue.(type) {
212 case int:
213 i = v
214 case bool:
215 if v {
216 i = 1
217 }
218 default:
219 panic(fmt.Errorf("unsupported type %T", variableValue))
220 }
221 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, i)))
222 }
223}