Home | History | Annotate | Download | only in common
      1 // 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 
     15 package common
     16 
     17 import (
     18 	"fmt"
     19 	"reflect"
     20 	"runtime"
     21 	"strings"
     22 
     23 	"github.com/google/blueprint/proptools"
     24 )
     25 
     26 func init() {
     27 	RegisterBottomUpMutator("variable", variableMutator)
     28 }
     29 
     30 type variableProperties struct {
     31 	Product_variables struct {
     32 		Platform_sdk_version struct {
     33 			Asflags []string
     34 		}
     35 
     36 		// unbundled_build is a catch-all property to annotate modules that don't build in one or
     37 		// more unbundled branches, usually due to dependencies missing from the manifest.
     38 		Unbundled_build struct {
     39 			Enabled *bool `android:"arch_variant"`
     40 		} `android:"arch_variant"`
     41 
     42 		Brillo struct {
     43 			Version_script *string `android:"arch_variant"`
     44 		} `android:"arch_variant"`
     45 
     46 		Malloc_not_svelte struct {
     47 			Cflags []string
     48 		}
     49 	} `android:"arch_variant"`
     50 }
     51 
     52 var zeroProductVariables variableProperties
     53 
     54 type productVariables struct {
     55 	Platform_sdk_version *int `json:",omitempty"`
     56 
     57 	DeviceName        *string   `json:",omitempty"`
     58 	DeviceArch        *string   `json:",omitempty"`
     59 	DeviceArchVariant *string   `json:",omitempty"`
     60 	DeviceCpuVariant  *string   `json:",omitempty"`
     61 	DeviceAbi         *[]string `json:",omitempty"`
     62 	DeviceUsesClang   *bool     `json:",omitempty"`
     63 
     64 	DeviceSecondaryArch        *string   `json:",omitempty"`
     65 	DeviceSecondaryArchVariant *string   `json:",omitempty"`
     66 	DeviceSecondaryCpuVariant  *string   `json:",omitempty"`
     67 	DeviceSecondaryAbi         *[]string `json:",omitempty"`
     68 
     69 	HostArch          *string `json:",omitempty"`
     70 	HostSecondaryArch *string `json:",omitempty"`
     71 
     72 	CrossHost              *string `json:",omitempty"`
     73 	CrossHostArch          *string `json:",omitempty"`
     74 	CrossHostSecondaryArch *string `json:",omitempty"`
     75 
     76 	Allow_missing_dependencies *bool `json:",omitempty"`
     77 	Unbundled_build            *bool `json:",omitempty"`
     78 	Brillo                     *bool `json:",omitempty"`
     79 	Malloc_not_svelte          *bool `json:",omitempty"`
     80 }
     81 
     82 func boolPtr(v bool) *bool {
     83 	return &v
     84 }
     85 
     86 func intPtr(v int) *int {
     87 	return &v
     88 }
     89 
     90 func stringPtr(v string) *string {
     91 	return &v
     92 }
     93 
     94 func (v *productVariables) SetDefaultConfig() {
     95 	*v = productVariables{
     96 		Platform_sdk_version:       intPtr(22),
     97 		HostArch:                   stringPtr("x86_64"),
     98 		HostSecondaryArch:          stringPtr("x86"),
     99 		DeviceName:                 stringPtr("flounder"),
    100 		DeviceArch:                 stringPtr("arm64"),
    101 		DeviceCpuVariant:           stringPtr("denver64"),
    102 		DeviceAbi:                  &[]string{"arm64-v8a"},
    103 		DeviceUsesClang:            boolPtr(true),
    104 		DeviceSecondaryArch:        stringPtr("arm"),
    105 		DeviceSecondaryArchVariant: stringPtr("armv7-a-neon"),
    106 		DeviceSecondaryCpuVariant:  stringPtr("denver"),
    107 		DeviceSecondaryAbi:         &[]string{"armeabi-v7a"},
    108 		Malloc_not_svelte:          boolPtr(false),
    109 	}
    110 
    111 	if runtime.GOOS == "linux" {
    112 		v.CrossHost = stringPtr("windows")
    113 		v.CrossHostArch = stringPtr("x86")
    114 		v.CrossHostSecondaryArch = stringPtr("x86_64")
    115 	}
    116 }
    117 
    118 func variableMutator(mctx AndroidBottomUpMutatorContext) {
    119 	var module AndroidModule
    120 	var ok bool
    121 	if module, ok = mctx.Module().(AndroidModule); !ok {
    122 		return
    123 	}
    124 
    125 	// TODO: depend on config variable, create variants, propagate variants up tree
    126 	a := module.base()
    127 	variableValues := reflect.ValueOf(&a.variableProperties.Product_variables).Elem()
    128 	zeroValues := reflect.ValueOf(zeroProductVariables.Product_variables)
    129 
    130 	for i := 0; i < variableValues.NumField(); i++ {
    131 		variableValue := variableValues.Field(i)
    132 		zeroValue := zeroValues.Field(i)
    133 		name := variableValues.Type().Field(i).Name
    134 		property := "product_variables." + proptools.PropertyNameForField(name)
    135 
    136 		// Check that the variable was set for the product
    137 		val := reflect.ValueOf(mctx.Config().(Config).ProductVariables).FieldByName(name)
    138 		if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
    139 			continue
    140 		}
    141 
    142 		val = val.Elem()
    143 
    144 		// For bools, check that the value is true
    145 		if val.Kind() == reflect.Bool && val.Bool() == false {
    146 			continue
    147 		}
    148 
    149 		// Check if any properties were set for the module
    150 		if reflect.DeepEqual(variableValue.Interface(), zeroValue.Interface()) {
    151 			continue
    152 		}
    153 
    154 		a.setVariableProperties(mctx, property, variableValue, val.Interface())
    155 	}
    156 }
    157 
    158 func (a *AndroidModuleBase) setVariableProperties(ctx AndroidBottomUpMutatorContext,
    159 	prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
    160 
    161 	printfIntoProperties(productVariablePropertyValue, variableValue)
    162 
    163 	err := proptools.AppendMatchingProperties(a.generalProperties,
    164 		productVariablePropertyValue.Addr().Interface(), nil)
    165 	if err != nil {
    166 		if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
    167 			ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
    168 		} else {
    169 			panic(err)
    170 		}
    171 	}
    172 }
    173 
    174 func printfIntoProperties(productVariablePropertyValue reflect.Value, variableValue interface{}) {
    175 	for i := 0; i < productVariablePropertyValue.NumField(); i++ {
    176 		propertyValue := productVariablePropertyValue.Field(i)
    177 		kind := propertyValue.Kind()
    178 		if kind == reflect.Ptr {
    179 			if propertyValue.IsNil() {
    180 				continue
    181 			}
    182 			propertyValue = propertyValue.Elem()
    183 		}
    184 		switch propertyValue.Kind() {
    185 		case reflect.String:
    186 			printfIntoProperty(propertyValue, variableValue)
    187 		case reflect.Slice:
    188 			for j := 0; j < propertyValue.Len(); j++ {
    189 				printfIntoProperty(propertyValue.Index(j), variableValue)
    190 			}
    191 		case reflect.Bool:
    192 			// Nothing
    193 		case reflect.Struct:
    194 			printfIntoProperties(propertyValue, variableValue)
    195 		default:
    196 			panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
    197 		}
    198 	}
    199 }
    200 
    201 func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) {
    202 	s := propertyValue.String()
    203 	// For now, we only support int formats
    204 	var i int
    205 	if strings.Contains(s, "%d") {
    206 		switch v := variableValue.(type) {
    207 		case int:
    208 			i = v
    209 		case bool:
    210 			if v {
    211 				i = 1
    212 			}
    213 		default:
    214 			panic(fmt.Errorf("unsupported type %T", variableValue))
    215 		}
    216 		propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, i)))
    217 	}
    218 }
    219