blob: 5b9092f3f6aba6ed5053d0ff95636a004c8f2522 [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 }
42 Dlmalloc_alignment struct {
43 Cflags []string
44 }
Dan Willemsen47cf66b2015-09-16 16:48:54 -070045 Platform_sdk_version struct {
46 Asflags []string
47 }
Colin Cross7f64b6d2015-07-09 13:57:48 -070048 }
49}
50
51var zeroProductVariables variableProperties
52
Colin Cross485e5722015-08-27 13:28:01 -070053type productVariables struct {
Colin Crossa6bc19e2015-09-16 13:53:42 -070054 Device_uses_jemalloc *bool `json:",omitempty"`
55 Device_uses_dlmalloc *bool `json:",omitempty"`
56 Dlmalloc_alignment *int `json:",omitempty"`
Dan Willemsen1d31ec32015-09-22 16:56:09 -070057 Platform_sdk_version *int `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070058
Dan Willemsen1d31ec32015-09-22 16:56:09 -070059 DeviceName *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070060 DeviceArch *string `json:",omitempty"`
61 DeviceArchVariant *string `json:",omitempty"`
62 DeviceCpuVariant *string `json:",omitempty"`
63 DeviceAbi *[]string `json:",omitempty"`
Dan Willemsendd0e2c32015-10-20 14:29:35 -070064 DeviceUsesClang *bool `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070065
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"`
Dan Willemsen490fd492015-11-24 17:53:15 -080073
74 CrossHost *string `json:",omitempty"`
75 CrossHostArch *string `json:",omitempty"`
76 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -070077}
78
79func boolPtr(v bool) *bool {
80 return &v
Colin Cross485e5722015-08-27 13:28:01 -070081}
82
Dan Willemsen47cf66b2015-09-16 16:48:54 -070083func intPtr(v int) *int {
84 return &v
85}
86
Colin Cross4225f652015-09-17 14:33:42 -070087func stringPtr(v string) *string {
88 return &v
89}
90
Colin Cross27385972015-09-18 10:57:10 -070091func (v *productVariables) SetDefaultConfig() {
92 *v = productVariables{
Dan Willemsen1d31ec32015-09-22 16:56:09 -070093 Device_uses_dlmalloc: boolPtr(true),
Colin Cross4225f652015-09-17 14:33:42 -070094 Platform_sdk_version: intPtr(22),
95 HostArch: stringPtr("x86_64"),
96 HostSecondaryArch: stringPtr("x86"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -070097 DeviceName: stringPtr("flounder"),
Colin Cross4225f652015-09-17 14:33:42 -070098 DeviceArch: stringPtr("arm64"),
99 DeviceCpuVariant: stringPtr("denver64"),
100 DeviceAbi: &[]string{"arm64-v8a"},
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700101 DeviceUsesClang: boolPtr(true),
Colin Cross4225f652015-09-17 14:33:42 -0700102 DeviceSecondaryArch: stringPtr("arm"),
103 DeviceSecondaryArchVariant: stringPtr("armv7-a-neon"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700104 DeviceSecondaryCpuVariant: stringPtr("denver"),
Colin Cross4225f652015-09-17 14:33:42 -0700105 DeviceSecondaryAbi: &[]string{"armeabi-v7a"},
Colin Cross485e5722015-08-27 13:28:01 -0700106 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800107
108 if runtime.GOOS == "linux" {
109 v.CrossHost = stringPtr("windows")
110 v.CrossHostArch = stringPtr("x86")
111 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700112}
113
Colin Cross6362e272015-10-29 15:25:03 -0700114func variableMutator(mctx AndroidBottomUpMutatorContext) {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700115 var module AndroidModule
116 var ok bool
117 if module, ok = mctx.Module().(AndroidModule); !ok {
118 return
119 }
120
121 // TODO: depend on config variable, create variants, propagate variants up tree
122 a := module.base()
Colin Cross06a931b2015-10-28 17:23:31 -0700123 variableValues := reflect.ValueOf(&a.variableProperties.Product_variables).Elem()
Colin Cross7f64b6d2015-07-09 13:57:48 -0700124 zeroValues := reflect.ValueOf(zeroProductVariables.Product_variables)
125
126 for i := 0; i < variableValues.NumField(); i++ {
127 variableValue := variableValues.Field(i)
128 zeroValue := zeroValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700129 name := variableValues.Type().Field(i).Name
130 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700131
Colin Crossa6bc19e2015-09-16 13:53:42 -0700132 // Check that the variable was set for the product
Colin Cross485e5722015-08-27 13:28:01 -0700133 val := reflect.ValueOf(mctx.Config().(Config).ProductVariables).FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -0700134 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
135 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700136 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700137
138 val = val.Elem()
139
140 // For bools, check that the value is true
141 if val.Kind() == reflect.Bool && val.Bool() == false {
142 continue
143 }
144
145 // Check if any properties were set for the module
146 if reflect.DeepEqual(variableValue.Interface(), zeroValue.Interface()) {
147 continue
148 }
149
150 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700151 }
152}
153
Colin Cross6362e272015-10-29 15:25:03 -0700154func (a *AndroidModuleBase) setVariableProperties(ctx AndroidBottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -0700155 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
156
Colin Cross7f64b6d2015-07-09 13:57:48 -0700157 if variableValue != nil {
158 printfIntoProperties(productVariablePropertyValue, variableValue)
159 }
160
Colin Cross06a931b2015-10-28 17:23:31 -0700161 err := proptools.AppendMatchingProperties(a.generalProperties,
162 productVariablePropertyValue.Addr().Interface(), nil)
163 if err != nil {
164 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
165 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
166 } else {
167 panic(err)
168 }
169 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700170}
171
172func printfIntoProperties(productVariablePropertyValue reflect.Value, variableValue interface{}) {
173 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
174 propertyValue := productVariablePropertyValue.Field(i)
175 switch propertyValue.Kind() {
176 case reflect.String:
177 printfIntoProperty(propertyValue, variableValue)
178 case reflect.Slice:
179 for j := 0; j < propertyValue.Len(); j++ {
180 printfIntoProperty(propertyValue.Index(j), variableValue)
181 }
182 case reflect.Struct:
183 printfIntoProperties(propertyValue, variableValue)
184 default:
185 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
186 }
187 }
188}
189
190func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) {
191 s := propertyValue.String()
192 // For now, we only support int formats
193 var i int
194 if strings.Contains(s, "%d") {
195 switch v := variableValue.(type) {
196 case int:
197 i = v
198 case bool:
199 if v {
200 i = 1
201 }
202 default:
203 panic(fmt.Errorf("unsupported type %T", variableValue))
204 }
205 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, i)))
206 }
207}