blob: 5116a9512bffce65fade304418926e52c032e3cf [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"
20 "strings"
21
22 "android/soong"
23
24 "github.com/google/blueprint"
25 "github.com/google/blueprint/proptools"
26)
27
28func init() {
29 soong.RegisterEarlyMutator("variable", VariableMutator)
30}
31
32type variableProperties struct {
33 Product_variables struct {
Colin Cross7f64b6d2015-07-09 13:57:48 -070034 Device_uses_dlmalloc struct {
35 Cflags []string
36 Srcs []string
37 }
38 Device_uses_jemalloc struct {
39 Cflags []string
40 Srcs []string
41 Whole_static_libs []string
42 Include_dirs []string
43 }
44 Dlmalloc_alignment struct {
45 Cflags []string
46 }
Dan Willemsen47cf66b2015-09-16 16:48:54 -070047 Platform_sdk_version struct {
48 Asflags []string
49 }
Colin Cross7f64b6d2015-07-09 13:57:48 -070050 }
51}
52
53var zeroProductVariables variableProperties
54
Colin Cross485e5722015-08-27 13:28:01 -070055type productVariables struct {
Colin Crossa6bc19e2015-09-16 13:53:42 -070056 Device_uses_jemalloc *bool `json:",omitempty"`
57 Device_uses_dlmalloc *bool `json:",omitempty"`
58 Dlmalloc_alignment *int `json:",omitempty"`
Dan Willemsen47cf66b2015-09-16 16:48:54 -070059 Platform_sdk_version *int
Colin Cross4225f652015-09-17 14:33:42 -070060
61 DeviceArch *string `json:",omitempty"`
62 DeviceArchVariant *string `json:",omitempty"`
63 DeviceCpuVariant *string `json:",omitempty"`
64 DeviceAbi *[]string `json:",omitempty"`
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"`
Colin Crossa6bc19e2015-09-16 13:53:42 -070073}
74
75func boolPtr(v bool) *bool {
76 return &v
Colin Cross485e5722015-08-27 13:28:01 -070077}
78
Dan Willemsen47cf66b2015-09-16 16:48:54 -070079func intPtr(v int) *int {
80 return &v
81}
82
Colin Cross4225f652015-09-17 14:33:42 -070083func stringPtr(v string) *string {
84 return &v
85}
86
Colin Cross485e5722015-08-27 13:28:01 -070087func (productVariables) DefaultConfig() jsonConfigurable {
88 v := productVariables{
Colin Cross4225f652015-09-17 14:33:42 -070089 Device_uses_jemalloc: boolPtr(true),
90 Platform_sdk_version: intPtr(22),
91 HostArch: stringPtr("x86_64"),
92 HostSecondaryArch: stringPtr("x86"),
93 DeviceArch: stringPtr("arm64"),
94 DeviceCpuVariant: stringPtr("denver64"),
95 DeviceAbi: &[]string{"arm64-v8a"},
96 DeviceSecondaryArch: stringPtr("arm"),
97 DeviceSecondaryArchVariant: stringPtr("armv7-a-neon"),
98 DeviceSecondaryCpuVariant: stringPtr("cortex-a15"),
99 DeviceSecondaryAbi: &[]string{"armeabi-v7a"},
Colin Cross485e5722015-08-27 13:28:01 -0700100 }
101 return v
Colin Cross7f64b6d2015-07-09 13:57:48 -0700102}
103
104func VariableMutator(mctx blueprint.EarlyMutatorContext) {
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()
113 variableValues := reflect.ValueOf(a.variableProperties.Product_variables)
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 Cross485e5722015-08-27 13:28:01 -0700119 name := variableValues.Type().Field(i).Name
120 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700121
Colin Crossa6bc19e2015-09-16 13:53:42 -0700122 // Check that the variable was set for the product
Colin Cross485e5722015-08-27 13:28:01 -0700123 val := reflect.ValueOf(mctx.Config().(Config).ProductVariables).FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -0700124 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
125 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700126 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700127
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 Cross7f64b6d2015-07-09 13:57:48 -0700141 }
142}
143
144func (a *AndroidModuleBase) setVariableProperties(ctx blueprint.EarlyMutatorContext,
145 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
146
147 generalPropertyValues := make([]reflect.Value, len(a.generalProperties))
148 for i := range a.generalProperties {
149 generalPropertyValues[i] = reflect.ValueOf(a.generalProperties[i]).Elem()
150 }
151
152 if variableValue != nil {
153 printfIntoProperties(productVariablePropertyValue, variableValue)
154 }
155
156 extendProperties(ctx, "", prefix, generalPropertyValues, productVariablePropertyValue, nil)
157}
158
159func printfIntoProperties(productVariablePropertyValue reflect.Value, variableValue interface{}) {
160 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
161 propertyValue := productVariablePropertyValue.Field(i)
162 switch propertyValue.Kind() {
163 case reflect.String:
164 printfIntoProperty(propertyValue, variableValue)
165 case reflect.Slice:
166 for j := 0; j < propertyValue.Len(); j++ {
167 printfIntoProperty(propertyValue.Index(j), variableValue)
168 }
169 case reflect.Struct:
170 printfIntoProperties(propertyValue, variableValue)
171 default:
172 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
173 }
174 }
175}
176
177func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) {
178 s := propertyValue.String()
179 // For now, we only support int formats
180 var i int
181 if strings.Contains(s, "%d") {
182 switch v := variableValue.(type) {
183 case int:
184 i = v
185 case bool:
186 if v {
187 i = 1
188 }
189 default:
190 panic(fmt.Errorf("unsupported type %T", variableValue))
191 }
192 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, i)))
193 }
194}