blob: 2c0063c6380e3e9d92ef464c3915f88efffbc6df [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 Cross83163192015-12-01 16:31:16 -080048
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"`
Dan Willemsen496e3952016-01-05 14:27:55 -080054
55 Brillo struct {
56 Version_script *string `android:"arch_variant"`
57 } `android:"arch_variant"`
Colin Cross83163192015-12-01 16:31:16 -080058 } `android:"arch_variant"`
Colin Cross7f64b6d2015-07-09 13:57:48 -070059}
60
61var zeroProductVariables variableProperties
62
Colin Cross485e5722015-08-27 13:28:01 -070063type productVariables struct {
Colin Crossa6bc19e2015-09-16 13:53:42 -070064 Device_uses_jemalloc *bool `json:",omitempty"`
65 Device_uses_dlmalloc *bool `json:",omitempty"`
66 Dlmalloc_alignment *int `json:",omitempty"`
Dan Willemsen1d31ec32015-09-22 16:56:09 -070067 Platform_sdk_version *int `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070068
Dan Willemsen1d31ec32015-09-22 16:56:09 -070069 DeviceName *string `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070070 DeviceArch *string `json:",omitempty"`
71 DeviceArchVariant *string `json:",omitempty"`
72 DeviceCpuVariant *string `json:",omitempty"`
73 DeviceAbi *[]string `json:",omitempty"`
Dan Willemsendd0e2c32015-10-20 14:29:35 -070074 DeviceUsesClang *bool `json:",omitempty"`
Colin Cross4225f652015-09-17 14:33:42 -070075
76 DeviceSecondaryArch *string `json:",omitempty"`
77 DeviceSecondaryArchVariant *string `json:",omitempty"`
78 DeviceSecondaryCpuVariant *string `json:",omitempty"`
79 DeviceSecondaryAbi *[]string `json:",omitempty"`
80
81 HostArch *string `json:",omitempty"`
82 HostSecondaryArch *string `json:",omitempty"`
Dan Willemsen490fd492015-11-24 17:53:15 -080083
84 CrossHost *string `json:",omitempty"`
85 CrossHostArch *string `json:",omitempty"`
86 CrossHostSecondaryArch *string `json:",omitempty"`
Colin Cross83163192015-12-01 16:31:16 -080087
88 Unbundled_build *bool `json:",omitempty"`
Colin Cross7b66f152015-12-15 16:07:43 -080089 Brillo *bool `json:",omitempty"`
Colin Crossa6bc19e2015-09-16 13:53:42 -070090}
91
92func boolPtr(v bool) *bool {
93 return &v
Colin Cross485e5722015-08-27 13:28:01 -070094}
95
Dan Willemsen47cf66b2015-09-16 16:48:54 -070096func intPtr(v int) *int {
97 return &v
98}
99
Colin Cross4225f652015-09-17 14:33:42 -0700100func stringPtr(v string) *string {
101 return &v
102}
103
Colin Cross27385972015-09-18 10:57:10 -0700104func (v *productVariables) SetDefaultConfig() {
105 *v = productVariables{
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700106 Device_uses_dlmalloc: boolPtr(true),
Colin Cross4225f652015-09-17 14:33:42 -0700107 Platform_sdk_version: intPtr(22),
108 HostArch: stringPtr("x86_64"),
109 HostSecondaryArch: stringPtr("x86"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700110 DeviceName: stringPtr("flounder"),
Colin Cross4225f652015-09-17 14:33:42 -0700111 DeviceArch: stringPtr("arm64"),
112 DeviceCpuVariant: stringPtr("denver64"),
113 DeviceAbi: &[]string{"arm64-v8a"},
Dan Willemsendd0e2c32015-10-20 14:29:35 -0700114 DeviceUsesClang: boolPtr(true),
Colin Cross4225f652015-09-17 14:33:42 -0700115 DeviceSecondaryArch: stringPtr("arm"),
116 DeviceSecondaryArchVariant: stringPtr("armv7-a-neon"),
Dan Willemsen1d31ec32015-09-22 16:56:09 -0700117 DeviceSecondaryCpuVariant: stringPtr("denver"),
Colin Cross4225f652015-09-17 14:33:42 -0700118 DeviceSecondaryAbi: &[]string{"armeabi-v7a"},
Colin Cross485e5722015-08-27 13:28:01 -0700119 }
Dan Willemsen490fd492015-11-24 17:53:15 -0800120
121 if runtime.GOOS == "linux" {
122 v.CrossHost = stringPtr("windows")
123 v.CrossHostArch = stringPtr("x86")
124 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700125}
126
Colin Cross6362e272015-10-29 15:25:03 -0700127func variableMutator(mctx AndroidBottomUpMutatorContext) {
Colin Cross7f64b6d2015-07-09 13:57:48 -0700128 var module AndroidModule
129 var ok bool
130 if module, ok = mctx.Module().(AndroidModule); !ok {
131 return
132 }
133
134 // TODO: depend on config variable, create variants, propagate variants up tree
135 a := module.base()
Colin Cross06a931b2015-10-28 17:23:31 -0700136 variableValues := reflect.ValueOf(&a.variableProperties.Product_variables).Elem()
Colin Cross7f64b6d2015-07-09 13:57:48 -0700137 zeroValues := reflect.ValueOf(zeroProductVariables.Product_variables)
138
139 for i := 0; i < variableValues.NumField(); i++ {
140 variableValue := variableValues.Field(i)
141 zeroValue := zeroValues.Field(i)
Colin Cross485e5722015-08-27 13:28:01 -0700142 name := variableValues.Type().Field(i).Name
143 property := "product_variables." + proptools.PropertyNameForField(name)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700144
Colin Crossa6bc19e2015-09-16 13:53:42 -0700145 // Check that the variable was set for the product
Colin Cross485e5722015-08-27 13:28:01 -0700146 val := reflect.ValueOf(mctx.Config().(Config).ProductVariables).FieldByName(name)
Colin Crossa6bc19e2015-09-16 13:53:42 -0700147 if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
148 continue
Colin Cross7f64b6d2015-07-09 13:57:48 -0700149 }
Colin Crossa6bc19e2015-09-16 13:53:42 -0700150
151 val = val.Elem()
152
153 // For bools, check that the value is true
154 if val.Kind() == reflect.Bool && val.Bool() == false {
155 continue
156 }
157
158 // Check if any properties were set for the module
159 if reflect.DeepEqual(variableValue.Interface(), zeroValue.Interface()) {
160 continue
161 }
162
163 a.setVariableProperties(mctx, property, variableValue, val.Interface())
Colin Cross7f64b6d2015-07-09 13:57:48 -0700164 }
165}
166
Colin Cross6362e272015-10-29 15:25:03 -0700167func (a *AndroidModuleBase) setVariableProperties(ctx AndroidBottomUpMutatorContext,
Colin Cross7f64b6d2015-07-09 13:57:48 -0700168 prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
169
Colin Crossdd0dbe62015-12-02 15:24:38 -0800170 printfIntoProperties(productVariablePropertyValue, variableValue)
Colin Cross7f64b6d2015-07-09 13:57:48 -0700171
Colin Cross06a931b2015-10-28 17:23:31 -0700172 err := proptools.AppendMatchingProperties(a.generalProperties,
173 productVariablePropertyValue.Addr().Interface(), nil)
174 if err != nil {
175 if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
176 ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
177 } else {
178 panic(err)
179 }
180 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700181}
182
183func printfIntoProperties(productVariablePropertyValue reflect.Value, variableValue interface{}) {
184 for i := 0; i < productVariablePropertyValue.NumField(); i++ {
185 propertyValue := productVariablePropertyValue.Field(i)
Colin Crossdd0dbe62015-12-02 15:24:38 -0800186 kind := propertyValue.Kind()
187 if kind == reflect.Ptr {
188 if propertyValue.IsNil() {
189 continue
190 }
191 propertyValue = propertyValue.Elem()
192 }
Colin Cross7f64b6d2015-07-09 13:57:48 -0700193 switch propertyValue.Kind() {
194 case reflect.String:
195 printfIntoProperty(propertyValue, variableValue)
196 case reflect.Slice:
197 for j := 0; j < propertyValue.Len(); j++ {
198 printfIntoProperty(propertyValue.Index(j), variableValue)
199 }
Colin Crossdd0dbe62015-12-02 15:24:38 -0800200 case reflect.Bool:
201 // Nothing
Colin Cross7f64b6d2015-07-09 13:57:48 -0700202 case reflect.Struct:
203 printfIntoProperties(propertyValue, variableValue)
204 default:
205 panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
206 }
207 }
208}
209
210func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) {
211 s := propertyValue.String()
212 // For now, we only support int formats
213 var i int
214 if strings.Contains(s, "%d") {
215 switch v := variableValue.(type) {
216 case int:
217 i = v
218 case bool:
219 if v {
220 i = 1
221 }
222 default:
223 panic(fmt.Errorf("unsupported type %T", variableValue))
224 }
225 propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, i)))
226 }
227}