| 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 |  | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" | 
|  | 24 | ) | 
|  | 25 |  | 
|  | 26 | func init() { | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 27 | PreDepsMutators(func(ctx RegisterMutatorsContext) { | 
|  | 28 | ctx.BottomUp("variable", variableMutator).Parallel() | 
|  | 29 | }) | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 30 | } | 
|  | 31 |  | 
|  | 32 | type variableProperties struct { | 
|  | 33 | Product_variables struct { | 
| Dan Willemsen | 47cf66b | 2015-09-16 16:48:54 -0700 | [diff] [blame] | 34 | Platform_sdk_version struct { | 
|  | 35 | Asflags []string | 
| Colin Cross | ceeff0f | 2017-05-08 13:43:00 -0700 | [diff] [blame] | 36 | Cflags  []string | 
| Dan Willemsen | 47cf66b | 2015-09-16 16:48:54 -0700 | [diff] [blame] | 37 | } | 
| Colin Cross | 8316319 | 2015-12-01 16:31:16 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | // unbundled_build is a catch-all property to annotate modules that don't build in one or | 
|  | 40 | // more unbundled branches, usually due to dependencies missing from the manifest. | 
|  | 41 | Unbundled_build struct { | 
|  | 42 | Enabled *bool `android:"arch_variant"` | 
|  | 43 | } `android:"arch_variant"` | 
| Dan Willemsen | 496e395 | 2016-01-05 14:27:55 -0800 | [diff] [blame] | 44 |  | 
| Dan Willemsen | 97cae01 | 2016-03-01 14:08:42 -0800 | [diff] [blame] | 45 | Malloc_not_svelte struct { | 
|  | 46 | Cflags []string | 
|  | 47 | } | 
| Evgenii Stepanov | 8391efa | 2016-05-12 18:04:18 -0700 | [diff] [blame] | 48 |  | 
|  | 49 | Safestack struct { | 
|  | 50 | Cflags []string `android:"arch_variant"` | 
|  | 51 | } `android:"arch_variant"` | 
| Dan Willemsen | a6f7d15 | 2016-07-12 14:57:40 -0700 | [diff] [blame] | 52 |  | 
| Dan Willemsen | 1be3538 | 2016-07-25 17:42:18 -0700 | [diff] [blame] | 53 | Binder32bit struct { | 
|  | 54 | Cflags []string | 
|  | 55 | } | 
| Dan Willemsen | fcebcd5 | 2016-09-22 14:49:10 -0700 | [diff] [blame] | 56 |  | 
| Colin Cross | 0e409a2 | 2017-05-02 10:58:50 -0700 | [diff] [blame] | 57 | Device_uses_hwc2 struct { | 
|  | 58 | Cflags []string | 
|  | 59 | } | 
|  | 60 |  | 
| Colin Cross | 9543642 | 2017-05-04 13:57:05 -0700 | [diff] [blame] | 61 | Override_rs_driver struct { | 
|  | 62 | Cflags []string | 
|  | 63 | } | 
|  | 64 |  | 
| Steven Moreland | c2b9f06 | 2017-12-13 14:06:20 -0800 | [diff] [blame] | 65 | // treble_linker_namespaces is true when the system/vendor linker namespace separation is | 
|  | 66 | // enabled. | 
|  | 67 | Treble_linker_namespaces struct { | 
|  | 68 | Cflags []string | 
|  | 69 | } | 
| Steven Moreland | c2b9f06 | 2017-12-13 14:06:20 -0800 | [diff] [blame] | 70 | // enforce_vintf_manifest is true when a device is required to have a vintf manifest. | 
|  | 71 | Enforce_vintf_manifest struct { | 
|  | 72 | Cflags []string | 
|  | 73 | } | 
|  | 74 |  | 
| Colin Cross | 6bc59ef | 2016-12-08 09:46:35 -0800 | [diff] [blame] | 75 | // debuggable is true for eng and userdebug builds, and can be used to turn on additional | 
|  | 76 | // debugging features that don't significantly impact runtime behavior.  userdebug builds | 
|  | 77 | // are used for dogfooding and performance testing, and should be as similar to user builds | 
|  | 78 | // as possible. | 
| Dan Willemsen | fcebcd5 | 2016-09-22 14:49:10 -0700 | [diff] [blame] | 79 | Debuggable struct { | 
| Dan Willemsen | 7b87283 | 2016-10-03 23:36:47 -0700 | [diff] [blame] | 80 | Cflags   []string | 
|  | 81 | Cppflags []string | 
| Carmen Jackson | 6fe956b | 2017-04-14 14:38:47 -0700 | [diff] [blame] | 82 | Init_rc  []string | 
| Dan Willemsen | fcebcd5 | 2016-09-22 14:49:10 -0700 | [diff] [blame] | 83 | } | 
| Colin Cross | 6bc59ef | 2016-12-08 09:46:35 -0800 | [diff] [blame] | 84 |  | 
|  | 85 | // eng is true for -eng builds, and can be used to turn on additionaly heavyweight debugging | 
|  | 86 | // features. | 
|  | 87 | Eng struct { | 
|  | 88 | Cflags   []string | 
|  | 89 | Cppflags []string | 
|  | 90 | } | 
| Colin Cross | b2123aa | 2017-05-05 13:37:11 -0700 | [diff] [blame] | 91 |  | 
|  | 92 | Pdk struct { | 
| Colin Cross | 99967a7 | 2017-10-17 13:55:02 -0700 | [diff] [blame] | 93 | Enabled *bool `android:"arch_variant"` | 
|  | 94 | } `android:"arch_variant"` | 
| Dmitry Shmidt | dba5419 | 2017-08-23 14:58:37 -0700 | [diff] [blame] | 95 |  | 
|  | 96 | Uml struct { | 
|  | 97 | Cppflags []string | 
|  | 98 | } | 
| Colin Cross | 8316319 | 2015-12-01 16:31:16 -0800 | [diff] [blame] | 99 | } `android:"arch_variant"` | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 100 | } | 
|  | 101 |  | 
|  | 102 | var zeroProductVariables variableProperties | 
|  | 103 |  | 
| Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 104 | type productVariables struct { | 
| Dan Willemsen | 174978c | 2016-05-11 00:27:49 -0700 | [diff] [blame] | 105 | // Suffix to add to generated Makefiles | 
|  | 106 | Make_suffix *string `json:",omitempty"` | 
|  | 107 |  | 
| Nan Zhang | 581fd21 | 2018-01-10 16:06:12 -0800 | [diff] [blame] | 108 | BuildId             *string `json:",omitempty"` | 
|  | 109 | BuildNumberFromFile *string `json:",omitempty"` | 
|  | 110 | DateFromFile        *string `json:",omitempty"` | 
|  | 111 |  | 
| Dan Albert | 31384de | 2017-07-28 12:39:46 -0700 | [diff] [blame] | 112 | Platform_sdk_version              *int     `json:",omitempty"` | 
| Colin Cross | 595a406 | 2017-08-31 12:30:37 -0700 | [diff] [blame] | 113 | Platform_sdk_final                *bool    `json:",omitempty"` | 
| Dan Albert | 31384de | 2017-07-28 12:39:46 -0700 | [diff] [blame] | 114 | Platform_version_active_codenames []string `json:",omitempty"` | 
|  | 115 | Platform_version_future_codenames []string `json:",omitempty"` | 
| Justin Yun | 8fe1212 | 2017-12-07 17:18:15 +0900 | [diff] [blame] | 116 | Platform_vndk_version             *string  `json:",omitempty"` | 
| Jiyong Park | 1a5d7b1 | 2018-01-15 15:05:10 +0900 | [diff] [blame] | 117 | Platform_systemsdk_versions       []string `json:",omitempty"` | 
| Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 118 |  | 
| Jiyong Park | 1a5d7b1 | 2018-01-15 15:05:10 +0900 | [diff] [blame] | 119 | DeviceName              *string   `json:",omitempty"` | 
|  | 120 | DeviceArch              *string   `json:",omitempty"` | 
|  | 121 | DeviceArchVariant       *string   `json:",omitempty"` | 
|  | 122 | DeviceCpuVariant        *string   `json:",omitempty"` | 
|  | 123 | DeviceAbi               *[]string `json:",omitempty"` | 
|  | 124 | DeviceVndkVersion       *string   `json:",omitempty"` | 
|  | 125 | DeviceSystemSdkVersions *[]string `json:",omitempty"` | 
| Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 126 |  | 
|  | 127 | DeviceSecondaryArch        *string   `json:",omitempty"` | 
|  | 128 | DeviceSecondaryArchVariant *string   `json:",omitempty"` | 
|  | 129 | DeviceSecondaryCpuVariant  *string   `json:",omitempty"` | 
|  | 130 | DeviceSecondaryAbi         *[]string `json:",omitempty"` | 
|  | 131 |  | 
|  | 132 | HostArch          *string `json:",omitempty"` | 
|  | 133 | HostSecondaryArch *string `json:",omitempty"` | 
| Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 134 |  | 
|  | 135 | CrossHost              *string `json:",omitempty"` | 
|  | 136 | CrossHostArch          *string `json:",omitempty"` | 
|  | 137 | CrossHostSecondaryArch *string `json:",omitempty"` | 
| Colin Cross | 8316319 | 2015-12-01 16:31:16 -0800 | [diff] [blame] | 138 |  | 
| Colin Cross | face4e4 | 2017-10-30 17:32:15 -0700 | [diff] [blame] | 139 | ResourceOverlays           *[]string `json:",omitempty"` | 
|  | 140 | EnforceRROTargets          *[]string `json:",omitempty"` | 
|  | 141 | EnforceRROExcludedOverlays *[]string `json:",omitempty"` | 
|  | 142 |  | 
|  | 143 | AAPTCharacteristics *string   `json:",omitempty"` | 
|  | 144 | AAPTConfig          *[]string `json:",omitempty"` | 
|  | 145 | AAPTPreferredConfig *string   `json:",omitempty"` | 
|  | 146 | AAPTPrebuiltDPI     *[]string `json:",omitempty"` | 
|  | 147 |  | 
| Colin Cross | 61ae0b7 | 2017-12-01 17:16:02 -0800 | [diff] [blame] | 148 | DefaultAppCertificate *string `json:",omitempty"` | 
|  | 149 |  | 
| Colin Cross | face4e4 | 2017-10-30 17:32:15 -0700 | [diff] [blame] | 150 | AppsDefaultVersionName *string `json:",omitempty"` | 
|  | 151 |  | 
| Dan Willemsen | b503816 | 2016-03-16 12:35:33 -0700 | [diff] [blame] | 152 | Allow_missing_dependencies *bool `json:",omitempty"` | 
|  | 153 | Unbundled_build            *bool `json:",omitempty"` | 
| Dan Willemsen | b503816 | 2016-03-16 12:35:33 -0700 | [diff] [blame] | 154 | Malloc_not_svelte          *bool `json:",omitempty"` | 
| Evgenii Stepanov | 8391efa | 2016-05-12 18:04:18 -0700 | [diff] [blame] | 155 | Safestack                  *bool `json:",omitempty"` | 
| Dan Willemsen | 36cff8b | 2016-05-17 16:35:02 -0700 | [diff] [blame] | 156 | HostStaticBinaries         *bool `json:",omitempty"` | 
| Dan Willemsen | 1be3538 | 2016-07-25 17:42:18 -0700 | [diff] [blame] | 157 | Binder32bit                *bool `json:",omitempty"` | 
| Colin Cross | 9d45bb7 | 2016-08-29 16:14:13 -0700 | [diff] [blame] | 158 | UseGoma                    *bool `json:",omitempty"` | 
| Dan Willemsen | fcebcd5 | 2016-09-22 14:49:10 -0700 | [diff] [blame] | 159 | Debuggable                 *bool `json:",omitempty"` | 
| Colin Cross | 6bc59ef | 2016-12-08 09:46:35 -0800 | [diff] [blame] | 160 | Eng                        *bool `json:",omitempty"` | 
| Colin Cross | 0e409a2 | 2017-05-02 10:58:50 -0700 | [diff] [blame] | 161 | Device_uses_hwc2           *bool `json:",omitempty"` | 
| Steven Moreland | c2b9f06 | 2017-12-13 14:06:20 -0800 | [diff] [blame] | 162 | Treble_linker_namespaces   *bool `json:",omitempty"` | 
|  | 163 | Sepolicy_split             *bool `json:",omitempty"` | 
|  | 164 | Enforce_vintf_manifest     *bool `json:",omitempty"` | 
| Colin Cross | dab550d | 2017-05-25 10:18:24 -0700 | [diff] [blame] | 165 | Pdk                        *bool `json:",omitempty"` | 
| Dmitry Shmidt | dba5419 | 2017-08-23 14:58:37 -0700 | [diff] [blame] | 166 | Uml                        *bool `json:",omitempty"` | 
| Colin Cross | 126a25c | 2017-10-31 13:55:34 -0700 | [diff] [blame] | 167 | MinimizeJavaDebugInfo      *bool `json:",omitempty"` | 
| Colin Cross | 16b2349 | 2016-01-06 14:41:07 -0800 | [diff] [blame] | 168 |  | 
| Ivan Lozano | 5f59553 | 2017-07-13 14:46:05 -0700 | [diff] [blame] | 169 | IntegerOverflowExcludePaths *[]string `json:",omitempty"` | 
|  | 170 |  | 
| Vishwath Mohan | 1fa3ac5 | 2017-10-31 02:26:14 -0700 | [diff] [blame] | 171 | EnableCFI       *bool     `json:",omitempty"` | 
|  | 172 | CFIExcludePaths *[]string `json:",omitempty"` | 
|  | 173 | CFIIncludePaths *[]string `json:",omitempty"` | 
|  | 174 |  | 
| Jaekyun Seok | 5cfbfbb | 2018-01-10 19:00:15 +0900 | [diff] [blame] | 175 | VendorPath  *string `json:",omitempty"` | 
|  | 176 | OdmPath     *string `json:",omitempty"` | 
|  | 177 | ProductPath *string `json:",omitempty"` | 
| Dan Willemsen | 4353bc4 | 2016-12-05 17:16:02 -0800 | [diff] [blame] | 178 |  | 
| Dan Willemsen | a03cf6d | 2016-09-26 15:45:04 -0700 | [diff] [blame] | 179 | ClangTidy  *bool   `json:",omitempty"` | 
|  | 180 | TidyChecks *string `json:",omitempty"` | 
|  | 181 |  | 
| Ryan Campbell | 469a18a | 2017-02-27 09:01:54 -0800 | [diff] [blame] | 182 | NativeCoverage       *bool     `json:",omitempty"` | 
|  | 183 | CoveragePaths        *[]string `json:",omitempty"` | 
|  | 184 | CoverageExcludePaths *[]string `json:",omitempty"` | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 185 |  | 
| Colin Cross | 1e7d370 | 2016-08-24 15:25:47 -0700 | [diff] [blame] | 186 | DevicePrefer32BitExecutables *bool `json:",omitempty"` | 
|  | 187 | HostPrefer32BitExecutables   *bool `json:",omitempty"` | 
|  | 188 |  | 
| Colin Cross | 23ae82a | 2016-11-02 14:34:39 -0700 | [diff] [blame] | 189 | SanitizeHost       []string `json:",omitempty"` | 
|  | 190 | SanitizeDevice     []string `json:",omitempty"` | 
| Ivan Lozano | 0c3a1ef | 2017-06-28 09:10:48 -0700 | [diff] [blame] | 191 | SanitizeDeviceDiag []string `json:",omitempty"` | 
| Colin Cross | 23ae82a | 2016-11-02 14:34:39 -0700 | [diff] [blame] | 192 | SanitizeDeviceArch []string `json:",omitempty"` | 
| Hiroshi Yamauchi | e2a1063 | 2016-12-19 13:44:41 -0800 | [diff] [blame] | 193 |  | 
|  | 194 | ArtUseReadBarrier *bool `json:",omitempty"` | 
| Jack He | 8cc7143 | 2016-12-08 15:45:07 -0800 | [diff] [blame] | 195 |  | 
|  | 196 | BtConfigIncludeDir *string `json:",omitempty"` | 
| Colin Cross | 9543642 | 2017-05-04 13:57:05 -0700 | [diff] [blame] | 197 |  | 
|  | 198 | Override_rs_driver *string `json:",omitempty"` | 
| Jiyong Park | d773eb3 | 2017-07-03 13:18:12 +0900 | [diff] [blame] | 199 |  | 
|  | 200 | DeviceKernelHeaders []string `json:",omitempty"` | 
| Jayant Chowdhary | 219139d | 2017-11-27 14:52:21 -0800 | [diff] [blame] | 201 | DistDir             *string  `json:",omitempty"` | 
| Justin Yun | 7154928 | 2017-11-17 12:10:28 +0900 | [diff] [blame] | 202 |  | 
|  | 203 | ExtraVndkVersions []string `json:",omitempty"` | 
| Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 204 |  | 
|  | 205 | NamespacesToExport []string `json:",omitempty"` | 
| Pirama Arumuga Nainar | 4954080 | 2018-01-29 23:11:42 -0800 | [diff] [blame] | 206 |  | 
|  | 207 | PgoAdditionalProfileDirs []string `json:",omitempty"` | 
| Colin Cross | a6bc19e | 2015-09-16 13:53:42 -0700 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
|  | 210 | func boolPtr(v bool) *bool { | 
|  | 211 | return &v | 
| Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 212 | } | 
|  | 213 |  | 
| Dan Willemsen | 47cf66b | 2015-09-16 16:48:54 -0700 | [diff] [blame] | 214 | func intPtr(v int) *int { | 
|  | 215 | return &v | 
|  | 216 | } | 
|  | 217 |  | 
| Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 218 | func stringPtr(v string) *string { | 
|  | 219 | return &v | 
|  | 220 | } | 
|  | 221 |  | 
| Colin Cross | 2738597 | 2015-09-18 10:57:10 -0700 | [diff] [blame] | 222 | func (v *productVariables) SetDefaultConfig() { | 
|  | 223 | *v = productVariables{ | 
| Dan Willemsen | cbf9e82 | 2017-11-13 14:34:56 -0800 | [diff] [blame] | 224 | Platform_sdk_version:              intPtr(26), | 
|  | 225 | Platform_version_active_codenames: []string{"P"}, | 
|  | 226 | Platform_version_future_codenames: []string{"P"}, | 
|  | 227 |  | 
| Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 228 | HostArch:                   stringPtr("x86_64"), | 
|  | 229 | HostSecondaryArch:          stringPtr("x86"), | 
| Dan Willemsen | cbf9e82 | 2017-11-13 14:34:56 -0800 | [diff] [blame] | 230 | DeviceName:                 stringPtr("generic_arm64"), | 
| Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 231 | DeviceArch:                 stringPtr("arm64"), | 
| Dan Willemsen | a91d127 | 2016-03-29 22:06:05 -0700 | [diff] [blame] | 232 | DeviceArchVariant:          stringPtr("armv8-a"), | 
| Dan Willemsen | cbf9e82 | 2017-11-13 14:34:56 -0800 | [diff] [blame] | 233 | DeviceCpuVariant:           stringPtr("generic"), | 
| Colin Cross | 4225f65 | 2015-09-17 14:33:42 -0700 | [diff] [blame] | 234 | DeviceAbi:                  &[]string{"arm64-v8a"}, | 
|  | 235 | DeviceSecondaryArch:        stringPtr("arm"), | 
| Dan Willemsen | cbf9e82 | 2017-11-13 14:34:56 -0800 | [diff] [blame] | 236 | DeviceSecondaryArchVariant: stringPtr("armv8-a"), | 
|  | 237 | DeviceSecondaryCpuVariant:  stringPtr("generic"), | 
|  | 238 | DeviceSecondaryAbi:         &[]string{"armeabi-v7a", "armeabi"}, | 
| Colin Cross | face4e4 | 2017-10-30 17:32:15 -0700 | [diff] [blame] | 239 |  | 
|  | 240 | AAPTConfig:          &[]string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"}, | 
|  | 241 | AAPTPreferredConfig: stringPtr("xhdpi"), | 
|  | 242 | AAPTCharacteristics: stringPtr("nosdcard"), | 
|  | 243 | AAPTPrebuiltDPI:     &[]string{"xhdpi", "xxhdpi"}, | 
|  | 244 |  | 
| Dan Willemsen | cbf9e82 | 2017-11-13 14:34:56 -0800 | [diff] [blame] | 245 | Malloc_not_svelte: boolPtr(true), | 
| Colin Cross | face4e4 | 2017-10-30 17:32:15 -0700 | [diff] [blame] | 246 | Safestack:         boolPtr(false), | 
| Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 247 | } | 
| Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 248 |  | 
|  | 249 | if runtime.GOOS == "linux" { | 
|  | 250 | v.CrossHost = stringPtr("windows") | 
|  | 251 | v.CrossHostArch = stringPtr("x86") | 
| Dan Willemsen | 07cd051 | 2016-02-03 23:16:33 -0800 | [diff] [blame] | 252 | v.CrossHostSecondaryArch = stringPtr("x86_64") | 
| Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 253 | } | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 254 | } | 
|  | 255 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 256 | func variableMutator(mctx BottomUpMutatorContext) { | 
|  | 257 | var module Module | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 258 | var ok bool | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 259 | if module, ok = mctx.Module().(Module); !ok { | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 260 | return | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | // TODO: depend on config variable, create variants, propagate variants up tree | 
|  | 264 | a := module.base() | 
| Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 265 | variableValues := reflect.ValueOf(&a.variableProperties.Product_variables).Elem() | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 266 | zeroValues := reflect.ValueOf(zeroProductVariables.Product_variables) | 
|  | 267 |  | 
|  | 268 | for i := 0; i < variableValues.NumField(); i++ { | 
|  | 269 | variableValue := variableValues.Field(i) | 
|  | 270 | zeroValue := zeroValues.Field(i) | 
| Colin Cross | 485e572 | 2015-08-27 13:28:01 -0700 | [diff] [blame] | 271 | name := variableValues.Type().Field(i).Name | 
|  | 272 | property := "product_variables." + proptools.PropertyNameForField(name) | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 273 |  | 
| Colin Cross | a6bc19e | 2015-09-16 13:53:42 -0700 | [diff] [blame] | 274 | // Check that the variable was set for the product | 
| Dan Willemsen | 45133ac | 2018-03-09 21:22:06 -0800 | [diff] [blame^] | 275 | val := reflect.ValueOf(mctx.Config().productVariables).FieldByName(name) | 
| Colin Cross | a6bc19e | 2015-09-16 13:53:42 -0700 | [diff] [blame] | 276 | if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() { | 
|  | 277 | continue | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 278 | } | 
| Colin Cross | a6bc19e | 2015-09-16 13:53:42 -0700 | [diff] [blame] | 279 |  | 
|  | 280 | val = val.Elem() | 
|  | 281 |  | 
|  | 282 | // For bools, check that the value is true | 
|  | 283 | if val.Kind() == reflect.Bool && val.Bool() == false { | 
|  | 284 | continue | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | // Check if any properties were set for the module | 
|  | 288 | if reflect.DeepEqual(variableValue.Interface(), zeroValue.Interface()) { | 
|  | 289 | continue | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | a.setVariableProperties(mctx, property, variableValue, val.Interface()) | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 293 | } | 
|  | 294 | } | 
|  | 295 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 296 | func (a *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext, | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 297 | prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) { | 
|  | 298 |  | 
| Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 299 | printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue) | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 300 |  | 
| Colin Cross | 06a931b | 2015-10-28 17:23:31 -0700 | [diff] [blame] | 301 | err := proptools.AppendMatchingProperties(a.generalProperties, | 
|  | 302 | productVariablePropertyValue.Addr().Interface(), nil) | 
|  | 303 | if err != nil { | 
|  | 304 | if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok { | 
|  | 305 | ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error()) | 
|  | 306 | } else { | 
|  | 307 | panic(err) | 
|  | 308 | } | 
|  | 309 | } | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 310 | } | 
|  | 311 |  | 
| Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 312 | func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string, | 
|  | 313 | productVariablePropertyValue reflect.Value, i int, err error) { | 
|  | 314 |  | 
|  | 315 | field := productVariablePropertyValue.Type().Field(i).Name | 
|  | 316 | property := prefix + "." + proptools.PropertyNameForField(field) | 
|  | 317 | ctx.PropertyErrorf(property, "%s", err) | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | func printfIntoProperties(ctx BottomUpMutatorContext, prefix string, | 
|  | 321 | productVariablePropertyValue reflect.Value, variableValue interface{}) { | 
|  | 322 |  | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 323 | for i := 0; i < productVariablePropertyValue.NumField(); i++ { | 
|  | 324 | propertyValue := productVariablePropertyValue.Field(i) | 
| Colin Cross | dd0dbe6 | 2015-12-02 15:24:38 -0800 | [diff] [blame] | 325 | kind := propertyValue.Kind() | 
|  | 326 | if kind == reflect.Ptr { | 
|  | 327 | if propertyValue.IsNil() { | 
|  | 328 | continue | 
|  | 329 | } | 
|  | 330 | propertyValue = propertyValue.Elem() | 
|  | 331 | } | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 332 | switch propertyValue.Kind() { | 
|  | 333 | case reflect.String: | 
| Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 334 | err := printfIntoProperty(propertyValue, variableValue) | 
|  | 335 | if err != nil { | 
|  | 336 | printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err) | 
|  | 337 | } | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 338 | case reflect.Slice: | 
|  | 339 | for j := 0; j < propertyValue.Len(); j++ { | 
| Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 340 | err := printfIntoProperty(propertyValue.Index(j), variableValue) | 
|  | 341 | if err != nil { | 
|  | 342 | printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err) | 
|  | 343 | } | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 344 | } | 
| Colin Cross | dd0dbe6 | 2015-12-02 15:24:38 -0800 | [diff] [blame] | 345 | case reflect.Bool: | 
|  | 346 | // Nothing | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 347 | case reflect.Struct: | 
| Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 348 | printfIntoProperties(ctx, prefix, propertyValue, variableValue) | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 349 | default: | 
|  | 350 | panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind())) | 
|  | 351 | } | 
|  | 352 | } | 
|  | 353 | } | 
|  | 354 |  | 
| Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 355 | func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error { | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 356 | s := propertyValue.String() | 
| Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 357 |  | 
|  | 358 | count := strings.Count(s, "%") | 
|  | 359 | if count == 0 { | 
|  | 360 | return nil | 
|  | 361 | } | 
|  | 362 |  | 
|  | 363 | if count > 1 { | 
|  | 364 | return fmt.Errorf("product variable properties only support a single '%%'") | 
|  | 365 | } | 
|  | 366 |  | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 367 | if strings.Contains(s, "%d") { | 
|  | 368 | switch v := variableValue.(type) { | 
|  | 369 | case int: | 
| Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 370 | // Nothing | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 371 | case bool: | 
|  | 372 | if v { | 
| Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 373 | variableValue = 1 | 
|  | 374 | } else { | 
|  | 375 | variableValue = 0 | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 376 | } | 
|  | 377 | default: | 
| Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 378 | return fmt.Errorf("unsupported type %T for %%d", variableValue) | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 379 | } | 
| Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 380 | } else if strings.Contains(s, "%s") { | 
|  | 381 | switch variableValue.(type) { | 
|  | 382 | case string: | 
|  | 383 | // Nothing | 
|  | 384 | default: | 
|  | 385 | return fmt.Errorf("unsupported type %T for %%s", variableValue) | 
|  | 386 | } | 
|  | 387 | } else { | 
|  | 388 | return fmt.Errorf("unsupported %% in product variable property") | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 389 | } | 
| Colin Cross | 7e0eaf1 | 2017-05-05 16:16:24 -0700 | [diff] [blame] | 390 |  | 
|  | 391 | propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue))) | 
|  | 392 |  | 
|  | 393 | return nil | 
| Colin Cross | 7f64b6d | 2015-07-09 13:57:48 -0700 | [diff] [blame] | 394 | } |