Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 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 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "reflect" |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 20 | "runtime" |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 21 | "strings" |
| 22 | |
Simon Shields | e9e78a5 | 2017-08-23 12:05:33 +1000 | [diff] [blame] | 23 | "bliss/soong/android" |
| 24 | |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 25 | "github.com/google/blueprint/proptools" |
| 26 | ) |
| 27 | |
| 28 | func init() { |
Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 29 | PreDepsMutators(func(ctx RegisterMutatorsContext) { |
| 30 | ctx.BottomUp("variable", variableMutator).Parallel() |
| 31 | }) |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type variableProperties struct { |
| 35 | Product_variables struct { |
Dan Willemsen | 47cf66b | 2015-09-16 16:48:54 -0700 | [diff] [blame] | 36 | Platform_sdk_version struct { |
| 37 | Asflags []string |
Colin Cross | ceeff0f | 2017-05-08 13:43:00 -0700 | [diff] [blame] | 38 | Cflags []string |
Dan Willemsen | 47cf66b | 2015-09-16 16:48:54 -0700 | [diff] [blame] | 39 | } |
Colin Cross | 8316319 | 2015-12-01 16:31:16 -0800 | [diff] [blame] | 40 | |
| 41 | // unbundled_build is a catch-all property to annotate modules that don't build in one or |
| 42 | // more unbundled branches, usually due to dependencies missing from the manifest. |
| 43 | Unbundled_build struct { |
| 44 | Enabled *bool `android:"arch_variant"` |
| 45 | } `android:"arch_variant"` |
Dan Willemsen | 496e395 | 2016-01-05 14:27:55 -0800 | [diff] [blame] | 46 | |
| 47 | Brillo struct { |
Dan Willemsen | 7b87283 | 2016-10-03 23:36:47 -0700 | [diff] [blame] | 48 | Cflags []string |
Dan Willemsen | 496e395 | 2016-01-05 14:27:55 -0800 | [diff] [blame] | 49 | Version_script *string `android:"arch_variant"` |
| 50 | } `android:"arch_variant"` |
Dan Willemsen | 97cae01 | 2016-03-01 14:08:42 -0800 | [diff] [blame] | 51 | |
| 52 | Malloc_not_svelte struct { |
| 53 | Cflags []string |
| 54 | } |
Evgenii Stepanov | 8391efa | 2016-05-12 18:04:18 -0700 | [diff] [blame] | 55 | |
| 56 | Safestack struct { |
| 57 | Cflags []string `android:"arch_variant"` |
| 58 | } `android:"arch_variant"` |
Dan Willemsen | a6f7d15 | 2016-07-12 14:57:40 -0700 | [diff] [blame] | 59 | |
Dan Willemsen | 1be3538 | 2016-07-25 17:42:18 -0700 | [diff] [blame] | 60 | Binder32bit struct { |
| 61 | Cflags []string |
| 62 | } |
Dan Willemsen | fcebcd5 | 2016-09-22 14:49:10 -0700 | [diff] [blame] | 63 | |
Colin Cross | 0e409a2 | 2017-05-02 10:58:50 -0700 | [diff] [blame] | 64 | Device_uses_hwc2 struct { |
| 65 | Cflags []string |
| 66 | } |
| 67 | |
Colin Cross | 9543642 | 2017-05-04 13:57:05 -0700 | [diff] [blame] | 68 | Override_rs_driver struct { |
| 69 | Cflags []string |
| 70 | } |
| 71 | |
Steven Moreland | e24bc20 | 2017-05-18 15:44:02 -0700 | [diff] [blame] | 72 | // treble is true when a build is a Treble compliant device. This is automatically set when |
| 73 | // a build is shipped with Android O, but can be overriden. This controls such things as |
| 74 | // the sepolicy split and enabling the Treble linker namespaces. |
| 75 | Treble struct { |
| 76 | Cflags []string |
| 77 | } |
| 78 | |
Colin Cross | 6bc59ef | 2016-12-08 09:46:35 -0800 | [diff] [blame] | 79 | // debuggable is true for eng and userdebug builds, and can be used to turn on additional |
| 80 | // debugging features that don't significantly impact runtime behavior. userdebug builds |
| 81 | // are used for dogfooding and performance testing, and should be as similar to user builds |
| 82 | // as possible. |
Dan Willemsen | fcebcd5 | 2016-09-22 14:49:10 -0700 | [diff] [blame] | 83 | Debuggable struct { |
Dan Willemsen | 7b87283 | 2016-10-03 23:36:47 -0700 | [diff] [blame] | 84 | Cflags []string |
| 85 | Cppflags []string |
Carmen Jackson | 6fe956b | 2017-04-14 14:38:47 -0700 | [diff] [blame] | 86 | Init_rc []string |
Dan Willemsen | fcebcd5 | 2016-09-22 14:49:10 -0700 | [diff] [blame] | 87 | } |
Colin Cross | 6bc59ef | 2016-12-08 09:46:35 -0800 | [diff] [blame] | 88 | |
| 89 | // eng is true for -eng builds, and can be used to turn on additionaly heavyweight debugging |
| 90 | // features. |
| 91 | Eng struct { |
| 92 | Cflags []string |
| 93 | Cppflags []string |
| 94 | } |
Colin Cross | b2123aa | 2017-05-05 13:37:11 -0700 | [diff] [blame] | 95 | |
| 96 | Pdk struct { |
| 97 | Enabled *bool |
| 98 | } |
Simon Shields | e9e78a5 | 2017-08-23 12:05:33 +1000 | [diff] [blame] | 99 | |
Simon Shields | 0c6b9a2 | 2017-09-04 21:09:44 +1000 | [diff] [blame] | 100 | // include bliss variables |
| 101 | Bliss android.Product_variables |
Colin Cross | 8316319 | 2015-12-01 16:31:16 -0800 | [diff] [blame] | 102 | } `android:"arch_variant"` |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | var zeroProductVariables variableProperties |
| 106 | |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 107 | type productVariables struct { |
Dan Willemsen | 174978c | 2016-05-11 00:27:49 -0700 | [diff] [blame] | 108 | // Suffix to add to generated Makefiles |
| 109 | Make_suffix *string `json:",omitempty"` |
| 110 | |
Dan Albert | 30c9d6e | 2017-03-28 14:54:55 -0700 | [diff] [blame] | 111 | Platform_sdk_version *int `json:",omitempty"` |
| 112 | Platform_version_all_codenames []string `json:",omitempty"` |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 113 | |
Dan Willemsen | 1d31ec3 | 2015-09-22 16:56:09 -0700 | [diff] [blame] | 114 | DeviceName *string `json:",omitempty"` |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 115 | DeviceArch *string `json:",omitempty"` |
| 116 | DeviceArchVariant *string `json:",omitempty"` |
| 117 | DeviceCpuVariant *string `json:",omitempty"` |
| 118 | DeviceAbi *[]string `json:",omitempty"` |
Dan Willemsen | dd0e2c3 | 2015-10-20 14:29:35 -0700 | [diff] [blame] | 119 | DeviceUsesClang *bool `json:",omitempty"` |
Dan Willemsen | d2ede87 | 2016-11-18 14:54:24 -0800 | [diff] [blame] | 120 | DeviceVndkVersion *string `json:",omitempty"` |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 121 | |
| 122 | DeviceSecondaryArch *string `json:",omitempty"` |
| 123 | DeviceSecondaryArchVariant *string `json:",omitempty"` |
| 124 | DeviceSecondaryCpuVariant *string `json:",omitempty"` |
| 125 | DeviceSecondaryAbi *[]string `json:",omitempty"` |
| 126 | |
| 127 | HostArch *string `json:",omitempty"` |
| 128 | HostSecondaryArch *string `json:",omitempty"` |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 129 | |
| 130 | CrossHost *string `json:",omitempty"` |
| 131 | CrossHostArch *string `json:",omitempty"` |
| 132 | CrossHostSecondaryArch *string `json:",omitempty"` |
Colin Cross | 8316319 | 2015-12-01 16:31:16 -0800 | [diff] [blame] | 133 | |
Dan Willemsen | b503816 | 2016-03-16 12:35:33 -0700 | [diff] [blame] | 134 | Allow_missing_dependencies *bool `json:",omitempty"` |
| 135 | Unbundled_build *bool `json:",omitempty"` |
| 136 | Brillo *bool `json:",omitempty"` |
| 137 | Malloc_not_svelte *bool `json:",omitempty"` |
Evgenii Stepanov | 8391efa | 2016-05-12 18:04:18 -0700 | [diff] [blame] | 138 | Safestack *bool `json:",omitempty"` |
Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 139 | HostStaticBinaries *bool `json:",omitempty"` |
Dan Willemsen | 1be3538 | 2016-07-25 17:42:18 -0700 | [diff] [blame] | 140 | Binder32bit *bool `json:",omitempty"` |
Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 141 | UseGoma *bool `json:",omitempty"` |
Dan Willemsen | fcebcd5 | 2016-09-22 14:49:10 -0700 | [diff] [blame] | 142 | Debuggable *bool `json:",omitempty"` |
Colin Cross | 6bc59ef | 2016-12-08 09:46:35 -0800 | [diff] [blame] | 143 | Eng *bool `json:",omitempty"` |
Vishwath Mohan | 1b017a7 | 2017-01-19 13:54:55 -0800 | [diff] [blame] | 144 | EnableCFI *bool `json:",omitempty"` |
Colin Cross | 0e409a2 | 2017-05-02 10:58:50 -0700 | [diff] [blame] | 145 | Device_uses_hwc2 *bool `json:",omitempty"` |
Steven Moreland | e24bc20 | 2017-05-18 15:44:02 -0700 | [diff] [blame] | 146 | Treble *bool `json:",omitempty"` |
Colin Cross | dab550d | 2017-05-25 10:18:24 -0700 | [diff] [blame] | 147 | Pdk *bool `json:",omitempty"` |
Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 148 | |
Dan Willemsen | 4353bc4 | 2016-12-05 17:16:02 -0800 | [diff] [blame] | 149 | VendorPath *string `json:",omitempty"` |
| 150 | |
Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 151 | ClangTidy *bool `json:",omitempty"` |
| 152 | TidyChecks *string `json:",omitempty"` |
| 153 | |
Ryan Campbell | 469a18a | 2017-02-27 09:01:54 -0800 | [diff] [blame] | 154 | NativeCoverage *bool `json:",omitempty"` |
| 155 | CoveragePaths *[]string `json:",omitempty"` |
| 156 | CoverageExcludePaths *[]string `json:",omitempty"` |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 157 | |
Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 158 | DevicePrefer32BitExecutables *bool `json:",omitempty"` |
| 159 | HostPrefer32BitExecutables *bool `json:",omitempty"` |
| 160 | |
Colin Cross | 23ae82a | 2016-11-02 14:34:39 -0700 | [diff] [blame] | 161 | SanitizeHost []string `json:",omitempty"` |
| 162 | SanitizeDevice []string `json:",omitempty"` |
| 163 | SanitizeDeviceArch []string `json:",omitempty"` |
Hiroshi Yamauchi | e2a1063 | 2016-12-19 13:44:41 -0800 | [diff] [blame] | 164 | |
| 165 | ArtUseReadBarrier *bool `json:",omitempty"` |
Jack He | 8cc7143 | 2016-12-08 15:45:07 -0800 | [diff] [blame] | 166 | |
| 167 | BtConfigIncludeDir *string `json:",omitempty"` |
Colin Cross | 9543642 | 2017-05-04 13:57:05 -0700 | [diff] [blame] | 168 | |
| 169 | Override_rs_driver *string `json:",omitempty"` |
Simon Shields | e9e78a5 | 2017-08-23 12:05:33 +1000 | [diff] [blame] | 170 | |
Simon Shields | 0c6b9a2 | 2017-09-04 21:09:44 +1000 | [diff] [blame] | 171 | // include Bliss variables |
| 172 | Bliss android.ProductVariables |
Colin Cross | a6bc19e | 2015-09-16 13:53:42 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | func boolPtr(v bool) *bool { |
| 176 | return &v |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Dan Willemsen | 47cf66b | 2015-09-16 16:48:54 -0700 | [diff] [blame] | 179 | func intPtr(v int) *int { |
| 180 | return &v |
| 181 | } |
| 182 | |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 183 | func stringPtr(v string) *string { |
| 184 | return &v |
| 185 | } |
| 186 | |
Colin Cross | 2738597 | 2015-09-18 10:57:10 -0700 | [diff] [blame] | 187 | func (v *productVariables) SetDefaultConfig() { |
| 188 | *v = productVariables{ |
Dan Albert | 22c0b41 | 2016-11-10 11:08:00 -0800 | [diff] [blame] | 189 | Platform_sdk_version: intPtr(24), |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 190 | HostArch: stringPtr("x86_64"), |
| 191 | HostSecondaryArch: stringPtr("x86"), |
Dan Willemsen | 1d31ec3 | 2015-09-22 16:56:09 -0700 | [diff] [blame] | 192 | DeviceName: stringPtr("flounder"), |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 193 | DeviceArch: stringPtr("arm64"), |
Dan Willemsen | a91d127 | 2016-03-29 22:06:05 -0700 | [diff] [blame] | 194 | DeviceArchVariant: stringPtr("armv8-a"), |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 195 | DeviceCpuVariant: stringPtr("denver64"), |
| 196 | DeviceAbi: &[]string{"arm64-v8a"}, |
Dan Willemsen | dd0e2c3 | 2015-10-20 14:29:35 -0700 | [diff] [blame] | 197 | DeviceUsesClang: boolPtr(true), |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 198 | DeviceSecondaryArch: stringPtr("arm"), |
| 199 | DeviceSecondaryArchVariant: stringPtr("armv7-a-neon"), |
Dan Willemsen | 1d31ec3 | 2015-09-22 16:56:09 -0700 | [diff] [blame] | 200 | DeviceSecondaryCpuVariant: stringPtr("denver"), |
Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 201 | DeviceSecondaryAbi: &[]string{"armeabi-v7a"}, |
Dan Willemsen | 97cae01 | 2016-03-01 14:08:42 -0800 | [diff] [blame] | 202 | Malloc_not_svelte: boolPtr(false), |
Evgenii Stepanov | 8391efa | 2016-05-12 18:04:18 -0700 | [diff] [blame] | 203 | Safestack: boolPtr(false), |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 204 | } |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 205 | |
| 206 | if runtime.GOOS == "linux" { |
| 207 | v.CrossHost = stringPtr("windows") |
| 208 | v.CrossHostArch = stringPtr("x86") |
Dan Willemsen | 07cd051 | 2016-02-03 23:16:33 -0800 | [diff] [blame] | 209 | v.CrossHostSecondaryArch = stringPtr("x86_64") |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 210 | } |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 213 | func variableMutator(mctx BottomUpMutatorContext) { |
| 214 | var module Module |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 215 | var ok bool |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 216 | if module, ok = mctx.Module().(Module); !ok { |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 217 | return |
| 218 | } |
| 219 | |
| 220 | // TODO: depend on config variable, create variants, propagate variants up tree |
| 221 | a := module.base() |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 222 | variableValues := reflect.ValueOf(&a.variableProperties.Product_variables).Elem() |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 223 | zeroValues := reflect.ValueOf(zeroProductVariables.Product_variables) |
Simon Shields | 3b50ea9 | 2017-09-04 21:09:10 +1000 | [diff] [blame] | 224 | valStruct := reflect.ValueOf(mctx.Config().(Config).ProductVariables) |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 225 | |
Simon Shields | 3b50ea9 | 2017-09-04 21:09:10 +1000 | [diff] [blame] | 226 | doVariableMutation(mctx, a, variableValues, zeroValues, valStruct) |
| 227 | |
| 228 | } |
| 229 | |
| 230 | func doVariableMutation(mctx BottomUpMutatorContext, a *ModuleBase, variableValues reflect.Value, zeroValues reflect.Value, |
| 231 | valStruct reflect.Value) { |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 232 | for i := 0; i < variableValues.NumField(); i++ { |
| 233 | variableValue := variableValues.Field(i) |
| 234 | zeroValue := zeroValues.Field(i) |
Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 235 | name := variableValues.Type().Field(i).Name |
| 236 | property := "product_variables." + proptools.PropertyNameForField(name) |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 237 | |
Colin Cross | a6bc19e | 2015-09-16 13:53:42 -0700 | [diff] [blame] | 238 | // Check that the variable was set for the product |
Simon Shields | 3b50ea9 | 2017-09-04 21:09:10 +1000 | [diff] [blame] | 239 | val := valStruct.FieldByName(name) |
| 240 | if val.IsValid() && val.Kind() == reflect.Struct { |
| 241 | doVariableMutation(mctx, a, variableValue, zeroValue, val) |
| 242 | continue |
| 243 | } else if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() { |
Colin Cross | a6bc19e | 2015-09-16 13:53:42 -0700 | [diff] [blame] | 244 | continue |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 245 | } |
Colin Cross | a6bc19e | 2015-09-16 13:53:42 -0700 | [diff] [blame] | 246 | |
| 247 | val = val.Elem() |
| 248 | |
| 249 | // For bools, check that the value is true |
| 250 | if val.Kind() == reflect.Bool && val.Bool() == false { |
| 251 | continue |
| 252 | } |
| 253 | |
| 254 | // Check if any properties were set for the module |
| 255 | if reflect.DeepEqual(variableValue.Interface(), zeroValue.Interface()) { |
| 256 | continue |
| 257 | } |
| 258 | |
| 259 | a.setVariableProperties(mctx, property, variableValue, val.Interface()) |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 263 | func (a *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext, |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 264 | prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) { |
| 265 | |
Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 266 | printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue) |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 267 | |
Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 268 | err := proptools.AppendMatchingProperties(a.generalProperties, |
| 269 | productVariablePropertyValue.Addr().Interface(), nil) |
| 270 | if err != nil { |
| 271 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { |
| 272 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) |
| 273 | } else { |
| 274 | panic(err) |
| 275 | } |
| 276 | } |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 279 | func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string, |
| 280 | productVariablePropertyValue reflect.Value, i int, err error) { |
| 281 | |
| 282 | field := productVariablePropertyValue.Type().Field(i).Name |
| 283 | property := prefix + "." + proptools.PropertyNameForField(field) |
| 284 | ctx.PropertyErrorf(property, "%s", err) |
| 285 | } |
| 286 | |
| 287 | func printfIntoProperties(ctx BottomUpMutatorContext, prefix string, |
| 288 | productVariablePropertyValue reflect.Value, variableValue interface{}) { |
| 289 | |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 290 | for i := 0; i < productVariablePropertyValue.NumField(); i++ { |
| 291 | propertyValue := productVariablePropertyValue.Field(i) |
Colin Cross | dd0dbe6 | 2015-12-02 15:24:38 -0800 | [diff] [blame] | 292 | kind := propertyValue.Kind() |
| 293 | if kind == reflect.Ptr { |
| 294 | if propertyValue.IsNil() { |
| 295 | continue |
| 296 | } |
| 297 | propertyValue = propertyValue.Elem() |
| 298 | } |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 299 | switch propertyValue.Kind() { |
| 300 | case reflect.String: |
Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 301 | err := printfIntoProperty(propertyValue, variableValue) |
| 302 | if err != nil { |
| 303 | printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err) |
| 304 | } |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 305 | case reflect.Slice: |
| 306 | for j := 0; j < propertyValue.Len(); j++ { |
Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 307 | err := printfIntoProperty(propertyValue.Index(j), variableValue) |
| 308 | if err != nil { |
| 309 | printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err) |
| 310 | } |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 311 | } |
Colin Cross | dd0dbe6 | 2015-12-02 15:24:38 -0800 | [diff] [blame] | 312 | case reflect.Bool: |
| 313 | // Nothing |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 314 | case reflect.Struct: |
Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 315 | printfIntoProperties(ctx, prefix, propertyValue, variableValue) |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 316 | default: |
| 317 | panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind())) |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 322 | func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error { |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 323 | s := propertyValue.String() |
Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 324 | |
| 325 | count := strings.Count(s, "%") |
| 326 | if count == 0 { |
| 327 | return nil |
| 328 | } |
| 329 | |
| 330 | if count > 1 { |
| 331 | return fmt.Errorf("product variable properties only support a single '%%'") |
| 332 | } |
| 333 | |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 334 | if strings.Contains(s, "%d") { |
| 335 | switch v := variableValue.(type) { |
| 336 | case int: |
Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 337 | // Nothing |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 338 | case bool: |
| 339 | if v { |
Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 340 | variableValue = 1 |
| 341 | } else { |
| 342 | variableValue = 0 |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 343 | } |
| 344 | default: |
Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 345 | return fmt.Errorf("unsupported type %T for %%d", variableValue) |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 346 | } |
Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 347 | } else if strings.Contains(s, "%s") { |
| 348 | switch variableValue.(type) { |
| 349 | case string: |
| 350 | // Nothing |
| 351 | default: |
| 352 | return fmt.Errorf("unsupported type %T for %%s", variableValue) |
| 353 | } |
| 354 | } else { |
| 355 | return fmt.Errorf("unsupported %% in product variable property") |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 356 | } |
Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 357 | |
| 358 | propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue))) |
| 359 | |
| 360 | return nil |
Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 361 | } |