blob: 23a97a6d99d973bb818ed7303e63e62e7c0aa92c [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 Willemsen1d31ec32015-09-22 16:56:09 -070059 Platform_sdk_version *int `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070060
Dan Willemsen1d31ec32015-09-22 16:56:09 -070061 DeviceName *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070062 DeviceArch *string `json:",omitempty"`
63 DeviceArchVariant *string `json:",omitempty"`
64 DeviceCpuVariant *string `json:",omitempty"`
65 DeviceAbi *[]string `json:",omitempty"`
Dan Willemsendd0e2c32015-10-20 14:29:35 -070066 DeviceUsesClang *bool `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070067
68 DeviceSecondaryArch *string `json:",omitempty"`
69 DeviceSecondaryArchVariant *string `json:",omitempty"`
70 DeviceSecondaryCpuVariant *string `json:",omitempty"`
71 DeviceSecondaryAbi *[]string `json:",omitempty"`
72
73 HostArch *string `json:",omitempty"`
74 HostSecondaryArch *string `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -070075}
76
77func boolPtr(v bool) *bool {
78 return &v
Colin Cross485e5722015-08-27 13:28:01 -070079}
80
Dan Willemsen47cf66b2015-09-16 16:48:54 -070081func intPtr(v int) *int {
82 return &v
83}
84
Colin Cross4225f652015-09-17 14:33:42 -070085func stringPtr(v string) *string {
86 return &v
87}
88
Colin Cross27385972015-09-18 10:57:10 -070089func (v *productVariables) SetDefaultConfig() {
90 *v = productVariables{
Dan Willemsen1d31ec32015-09-22 16:56:09 -070091 Device_uses_dlmalloc: boolPtr(true),
Colin Cross4225f652015-09-17 14:33:42 -070092 Platform_sdk_version: intPtr(22),
93 HostArch: stringPtr("x86_64"),
94 HostSecondaryArch: stringPtr("x86"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -070095 DeviceName: stringPtr("flounder"),
Colin Cross4225f652015-09-17 14:33:42 -070096 DeviceArch: stringPtr("arm64"),
97 DeviceCpuVariant: stringPtr("denver64"),
98 DeviceAbi: &[]string{"arm64-v8a"},
Dan Willemsendd0e2c32015-10-20 14:29:35 -070099 DeviceUsesClang: boolPtr(true),
Colin Cross4225f652015-09-17 14:33:42 -0700100 DeviceSecondaryArch: stringPtr("arm"),
101 DeviceSecondaryArchVariant: stringPtr("armv7-a-neon"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700102 DeviceSecondaryCpuVariant: stringPtr("denver"),
Colin Cross4225f652015-09-17 14:33:42 -0700103 DeviceSecondaryAbi: &[]string{"armeabi-v7a"},
Colin Cross485e5722015-08-27 13:28:01 -0700104 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700105}
106
107func VariableMutator(mctx blueprint.EarlyMutatorContext) {
108 var module AndroidModule
109 var ok bool
110 if module, ok = mctx.Module().(AndroidModule); !ok {
111 return
112 }
113
114 // TODO: depend on config variable, create variants, propagate variants up tree
115 a := module.base()
116 variableValues := reflect.ValueOf(a.variableProperties.Product_variables)
117 zeroValues := reflect.ValueOf(zeroProductVariables.Product_variables)
118
119 for i := 0; i < variableValues.NumField(); i++ {
120 variableValue := variableValues.Field(i)
121 zeroValue := zeroValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700122 name := variableValues.Type().Field(i).Name
123 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700124
Colin Crossa6bc19e2015-09-16 13:53:42 -0700125 // Check that the variable was set for the product
Colin Cross485e5722015-08-27 13:28:01 -0700126 val := reflect.ValueOf(mctx.Config().(Config).ProductVariables).FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -0700127 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
128 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700129 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700130
131 val = val.Elem()
132
133 // For bools, check that the value is true
134 if val.Kind() == reflect.Bool && val.Bool() == false {
135 continue
136 }
137
138 // Check if any properties were set for the module
139 if reflect.DeepEqual(variableValue.Interface(), zeroValue.Interface()) {
140 continue
141 }
142
143 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700144 }
145}
146
147func (a *AndroidModuleBase) setVariableProperties(ctx blueprint.EarlyMutatorContext,
148 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
149
150 generalPropertyValues := make([]reflect.Value, len(a.generalProperties))
151 for i := range a.generalProperties {
152 generalPropertyValues[i] = reflect.ValueOf(a.generalProperties[i]).Elem()
153 }
154
155 if variableValue != nil {
156 printfIntoProperties(productVariablePropertyValue, variableValue)
157 }
158
159 extendProperties(ctx, "", prefix, generalPropertyValues, productVariablePropertyValue, nil)
160}
161
162func 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
180func 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}