variable.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. package android
  15. import (
  16. "fmt"
  17. "reflect"
  18. "runtime"
  19. "strings"
  20. "github.com/google/blueprint/proptools"
  21. )
  22. func init() {
  23. registerVariableBuildComponents(InitRegistrationContext)
  24. }
  25. func registerVariableBuildComponents(ctx RegistrationContext) {
  26. ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
  27. ctx.BottomUp("variable", VariableMutator).Parallel()
  28. })
  29. }
  30. var PrepareForTestWithVariables = FixtureRegisterWithContext(registerVariableBuildComponents)
  31. type variableProperties struct {
  32. Product_variables struct {
  33. Platform_sdk_version struct {
  34. Asflags []string
  35. Cflags []string
  36. Cmd *string
  37. }
  38. Platform_sdk_version_or_codename struct {
  39. Java_resource_dirs []string
  40. }
  41. Platform_sdk_extension_version struct {
  42. Cmd *string
  43. }
  44. Platform_version_name struct {
  45. Base_dir *string
  46. }
  47. // unbundled_build is a catch-all property to annotate modules that don't build in one or
  48. // more unbundled branches, usually due to dependencies missing from the manifest.
  49. Unbundled_build struct {
  50. Enabled *bool `android:"arch_variant"`
  51. } `android:"arch_variant"`
  52. Malloc_not_svelte struct {
  53. Cflags []string `android:"arch_variant"`
  54. Shared_libs []string `android:"arch_variant"`
  55. Whole_static_libs []string `android:"arch_variant"`
  56. Exclude_static_libs []string `android:"arch_variant"`
  57. Srcs []string `android:"arch_variant"`
  58. Header_libs []string `android:"arch_variant"`
  59. } `android:"arch_variant"`
  60. Malloc_zero_contents struct {
  61. Cflags []string `android:"arch_variant"`
  62. } `android:"arch_variant"`
  63. Malloc_pattern_fill_contents struct {
  64. Cflags []string `android:"arch_variant"`
  65. } `android:"arch_variant"`
  66. Safestack struct {
  67. Cflags []string `android:"arch_variant"`
  68. } `android:"arch_variant"`
  69. Binder32bit struct {
  70. Cflags []string
  71. }
  72. Override_rs_driver struct {
  73. Cflags []string
  74. }
  75. // treble_linker_namespaces is true when the system/vendor linker namespace separation is
  76. // enabled.
  77. Treble_linker_namespaces struct {
  78. Cflags []string
  79. }
  80. // enforce_vintf_manifest is true when a device is required to have a vintf manifest.
  81. Enforce_vintf_manifest struct {
  82. Cflags []string
  83. }
  84. // debuggable is true for eng and userdebug builds, and can be used to turn on additional
  85. // debugging features that don't significantly impact runtime behavior. userdebug builds
  86. // are used for dogfooding and performance testing, and should be as similar to user builds
  87. // as possible.
  88. Debuggable struct {
  89. Cflags []string
  90. Cppflags []string
  91. Init_rc []string
  92. Required []string
  93. Host_required []string
  94. Target_required []string
  95. Strip struct {
  96. All *bool
  97. Keep_symbols *bool
  98. Keep_symbols_and_debug_frame *bool
  99. }
  100. Static_libs []string
  101. Whole_static_libs []string
  102. Shared_libs []string
  103. Cmdline []string
  104. }
  105. // eng is true for -eng builds, and can be used to turn on additionaly heavyweight debugging
  106. // features.
  107. Eng struct {
  108. Cflags []string
  109. Cppflags []string
  110. Lto struct {
  111. Never *bool
  112. }
  113. Sanitize struct {
  114. Address *bool
  115. }
  116. Optimize struct {
  117. Enabled *bool
  118. }
  119. }
  120. Pdk struct {
  121. Enabled *bool `android:"arch_variant"`
  122. } `android:"arch_variant"`
  123. Uml struct {
  124. Cppflags []string
  125. }
  126. Arc struct {
  127. Cflags []string `android:"arch_variant"`
  128. Exclude_srcs []string `android:"arch_variant"`
  129. Header_libs []string `android:"arch_variant"`
  130. Include_dirs []string `android:"arch_variant"`
  131. Shared_libs []string `android:"arch_variant"`
  132. Static_libs []string `android:"arch_variant"`
  133. Srcs []string `android:"arch_variant"`
  134. Whole_static_libs []string `android:"arch_variant"`
  135. } `android:"arch_variant"`
  136. Flatten_apex struct {
  137. Enabled *bool
  138. }
  139. Native_coverage struct {
  140. Src *string `android:"arch_variant"`
  141. Srcs []string `android:"arch_variant"`
  142. Exclude_srcs []string `android:"arch_variant"`
  143. } `android:"arch_variant"`
  144. } `android:"arch_variant"`
  145. }
  146. var defaultProductVariables interface{} = variableProperties{}
  147. type productVariables struct {
  148. // Suffix to add to generated Makefiles
  149. Make_suffix *string `json:",omitempty"`
  150. BuildId *string `json:",omitempty"`
  151. BuildNumberFile *string `json:",omitempty"`
  152. Platform_version_name *string `json:",omitempty"`
  153. Platform_sdk_version *int `json:",omitempty"`
  154. Platform_sdk_codename *string `json:",omitempty"`
  155. Platform_sdk_version_or_codename *string `json:",omitempty"`
  156. Platform_sdk_final *bool `json:",omitempty"`
  157. Platform_sdk_extension_version *int `json:",omitempty"`
  158. Platform_version_active_codenames []string `json:",omitempty"`
  159. Platform_vndk_version *string `json:",omitempty"`
  160. Platform_systemsdk_versions []string `json:",omitempty"`
  161. Platform_security_patch *string `json:",omitempty"`
  162. Platform_preview_sdk_version *string `json:",omitempty"`
  163. Platform_min_supported_target_sdk_version *string `json:",omitempty"`
  164. Platform_base_os *string `json:",omitempty"`
  165. DeviceName *string `json:",omitempty"`
  166. DeviceArch *string `json:",omitempty"`
  167. DeviceArchVariant *string `json:",omitempty"`
  168. DeviceCpuVariant *string `json:",omitempty"`
  169. DeviceAbi []string `json:",omitempty"`
  170. DeviceVndkVersion *string `json:",omitempty"`
  171. DeviceCurrentApiLevelForVendorModules *string `json:",omitempty"`
  172. DeviceSystemSdkVersions []string `json:",omitempty"`
  173. RecoverySnapshotVersion *string `json:",omitempty"`
  174. DeviceSecondaryArch *string `json:",omitempty"`
  175. DeviceSecondaryArchVariant *string `json:",omitempty"`
  176. DeviceSecondaryCpuVariant *string `json:",omitempty"`
  177. DeviceSecondaryAbi []string `json:",omitempty"`
  178. NativeBridgeArch *string `json:",omitempty"`
  179. NativeBridgeArchVariant *string `json:",omitempty"`
  180. NativeBridgeCpuVariant *string `json:",omitempty"`
  181. NativeBridgeAbi []string `json:",omitempty"`
  182. NativeBridgeRelativePath *string `json:",omitempty"`
  183. NativeBridgeSecondaryArch *string `json:",omitempty"`
  184. NativeBridgeSecondaryArchVariant *string `json:",omitempty"`
  185. NativeBridgeSecondaryCpuVariant *string `json:",omitempty"`
  186. NativeBridgeSecondaryAbi []string `json:",omitempty"`
  187. NativeBridgeSecondaryRelativePath *string `json:",omitempty"`
  188. HostArch *string `json:",omitempty"`
  189. HostSecondaryArch *string `json:",omitempty"`
  190. HostMusl *bool `json:",omitempty"`
  191. CrossHost *string `json:",omitempty"`
  192. CrossHostArch *string `json:",omitempty"`
  193. CrossHostSecondaryArch *string `json:",omitempty"`
  194. DeviceResourceOverlays []string `json:",omitempty"`
  195. ProductResourceOverlays []string `json:",omitempty"`
  196. EnforceRROTargets []string `json:",omitempty"`
  197. EnforceRROExcludedOverlays []string `json:",omitempty"`
  198. AAPTCharacteristics *string `json:",omitempty"`
  199. AAPTConfig []string `json:",omitempty"`
  200. AAPTPreferredConfig *string `json:",omitempty"`
  201. AAPTPrebuiltDPI []string `json:",omitempty"`
  202. DefaultAppCertificate *string `json:",omitempty"`
  203. AppsDefaultVersionName *string `json:",omitempty"`
  204. Allow_missing_dependencies *bool `json:",omitempty"`
  205. Unbundled_build *bool `json:",omitempty"`
  206. Unbundled_build_apps *bool `json:",omitempty"`
  207. Unbundled_build_image *bool `json:",omitempty"`
  208. Always_use_prebuilt_sdks *bool `json:",omitempty"`
  209. Skip_boot_jars_check *bool `json:",omitempty"`
  210. Malloc_not_svelte *bool `json:",omitempty"`
  211. Malloc_zero_contents *bool `json:",omitempty"`
  212. Malloc_pattern_fill_contents *bool `json:",omitempty"`
  213. Safestack *bool `json:",omitempty"`
  214. HostStaticBinaries *bool `json:",omitempty"`
  215. Binder32bit *bool `json:",omitempty"`
  216. UseGoma *bool `json:",omitempty"`
  217. UseRBE *bool `json:",omitempty"`
  218. UseRBEJAVAC *bool `json:",omitempty"`
  219. UseRBER8 *bool `json:",omitempty"`
  220. UseRBED8 *bool `json:",omitempty"`
  221. Debuggable *bool `json:",omitempty"`
  222. Eng *bool `json:",omitempty"`
  223. Treble_linker_namespaces *bool `json:",omitempty"`
  224. Enforce_vintf_manifest *bool `json:",omitempty"`
  225. Uml *bool `json:",omitempty"`
  226. Arc *bool `json:",omitempty"`
  227. MinimizeJavaDebugInfo *bool `json:",omitempty"`
  228. Check_elf_files *bool `json:",omitempty"`
  229. UncompressPrivAppDex *bool `json:",omitempty"`
  230. ModulesLoadedByPrivilegedModules []string `json:",omitempty"`
  231. BootJars ConfiguredJarList `json:",omitempty"`
  232. ApexBootJars ConfiguredJarList `json:",omitempty"`
  233. IntegerOverflowExcludePaths []string `json:",omitempty"`
  234. EnableCFI *bool `json:",omitempty"`
  235. CFIExcludePaths []string `json:",omitempty"`
  236. CFIIncludePaths []string `json:",omitempty"`
  237. DisableScudo *bool `json:",omitempty"`
  238. MemtagHeapExcludePaths []string `json:",omitempty"`
  239. MemtagHeapAsyncIncludePaths []string `json:",omitempty"`
  240. MemtagHeapSyncIncludePaths []string `json:",omitempty"`
  241. VendorPath *string `json:",omitempty"`
  242. OdmPath *string `json:",omitempty"`
  243. ProductPath *string `json:",omitempty"`
  244. SystemExtPath *string `json:",omitempty"`
  245. ClangTidy *bool `json:",omitempty"`
  246. TidyChecks *string `json:",omitempty"`
  247. SamplingPGO *bool `json:",omitempty"`
  248. JavaCoveragePaths []string `json:",omitempty"`
  249. JavaCoverageExcludePaths []string `json:",omitempty"`
  250. GcovCoverage *bool `json:",omitempty"`
  251. ClangCoverage *bool `json:",omitempty"`
  252. NativeCoveragePaths []string `json:",omitempty"`
  253. NativeCoverageExcludePaths []string `json:",omitempty"`
  254. // Set by NewConfig
  255. Native_coverage *bool `json:",omitempty"`
  256. SanitizeHost []string `json:",omitempty"`
  257. SanitizeDevice []string `json:",omitempty"`
  258. SanitizeDeviceDiag []string `json:",omitempty"`
  259. SanitizeDeviceArch []string `json:",omitempty"`
  260. ArtUseReadBarrier *bool `json:",omitempty"`
  261. BtConfigIncludeDir *string `json:",omitempty"`
  262. Override_rs_driver *string `json:",omitempty"`
  263. DeviceKernelHeaders []string `json:",omitempty"`
  264. ExtraVndkVersions []string `json:",omitempty"`
  265. NamespacesToExport []string `json:",omitempty"`
  266. PgoAdditionalProfileDirs []string `json:",omitempty"`
  267. VndkUseCoreVariant *bool `json:",omitempty"`
  268. VndkSnapshotBuildArtifacts *bool `json:",omitempty"`
  269. DirectedVendorSnapshot bool `json:",omitempty"`
  270. VendorSnapshotModules map[string]bool `json:",omitempty"`
  271. DirectedRecoverySnapshot bool `json:",omitempty"`
  272. RecoverySnapshotModules map[string]bool `json:",omitempty"`
  273. VendorSnapshotDirsIncluded []string `json:",omitempty"`
  274. VendorSnapshotDirsExcluded []string `json:",omitempty"`
  275. RecoverySnapshotDirsExcluded []string `json:",omitempty"`
  276. RecoverySnapshotDirsIncluded []string `json:",omitempty"`
  277. HostFakeSnapshotEnabled bool `json:",omitempty"`
  278. BoardVendorSepolicyDirs []string `json:",omitempty"`
  279. BoardOdmSepolicyDirs []string `json:",omitempty"`
  280. BoardReqdMaskPolicy []string `json:",omitempty"`
  281. SystemExtPublicSepolicyDirs []string `json:",omitempty"`
  282. SystemExtPrivateSepolicyDirs []string `json:",omitempty"`
  283. BoardSepolicyM4Defs []string `json:",omitempty"`
  284. BoardSepolicyVers *string `json:",omitempty"`
  285. PlatformSepolicyVersion *string `json:",omitempty"`
  286. TotSepolicyVersion *string `json:",omitempty"`
  287. VendorVars map[string]map[string]string `json:",omitempty"`
  288. Ndk_abis *bool `json:",omitempty"`
  289. Flatten_apex *bool `json:",omitempty"`
  290. ForceApexSymlinkOptimization *bool `json:",omitempty"`
  291. CompressedApex *bool `json:",omitempty"`
  292. Aml_abis *bool `json:",omitempty"`
  293. DexpreoptGlobalConfig *string `json:",omitempty"`
  294. WithDexpreopt bool `json:",omitempty"`
  295. ManifestPackageNameOverrides []string `json:",omitempty"`
  296. CertificateOverrides []string `json:",omitempty"`
  297. PackageNameOverrides []string `json:",omitempty"`
  298. EnforceSystemCertificate *bool `json:",omitempty"`
  299. EnforceSystemCertificateAllowList []string `json:",omitempty"`
  300. ProductHiddenAPIStubs []string `json:",omitempty"`
  301. ProductHiddenAPIStubsSystem []string `json:",omitempty"`
  302. ProductHiddenAPIStubsTest []string `json:",omitempty"`
  303. ProductPublicSepolicyDirs []string `json:",omitempty"`
  304. ProductPrivateSepolicyDirs []string `json:",omitempty"`
  305. ProductVndkVersion *string `json:",omitempty"`
  306. TargetFSConfigGen []string `json:",omitempty"`
  307. MissingUsesLibraries []string `json:",omitempty"`
  308. EnforceProductPartitionInterface *bool `json:",omitempty"`
  309. EnforceInterPartitionJavaSdkLibrary *bool `json:",omitempty"`
  310. InterPartitionJavaLibraryAllowList []string `json:",omitempty"`
  311. InstallExtraFlattenedApexes *bool `json:",omitempty"`
  312. BoardUsesRecoveryAsBoot *bool `json:",omitempty"`
  313. BoardKernelBinaries []string `json:",omitempty"`
  314. BoardKernelModuleInterfaceVersions []string `json:",omitempty"`
  315. BoardMoveRecoveryResourcesToVendorBoot *bool `json:",omitempty"`
  316. PrebuiltHiddenApiDir *string `json:",omitempty"`
  317. ShippingApiLevel *string `json:",omitempty"`
  318. BuildBrokenEnforceSyspropOwner bool `json:",omitempty"`
  319. BuildBrokenTrebleSyspropNeverallow bool `json:",omitempty"`
  320. BuildBrokenVendorPropertyNamespace bool `json:",omitempty"`
  321. BuildDebugfsRestrictionsEnabled bool `json:",omitempty"`
  322. RequiresInsecureExecmemForSwiftshader bool `json:",omitempty"`
  323. SelinuxIgnoreNeverallows bool `json:",omitempty"`
  324. SepolicySplit bool `json:",omitempty"`
  325. SepolicyFreezeTestExtraDirs []string `json:",omitempty"`
  326. SepolicyFreezeTestExtraPrebuiltDirs []string `json:",omitempty"`
  327. }
  328. func boolPtr(v bool) *bool {
  329. return &v
  330. }
  331. func intPtr(v int) *int {
  332. return &v
  333. }
  334. func stringPtr(v string) *string {
  335. return &v
  336. }
  337. func (v *productVariables) SetDefaultConfig() {
  338. *v = productVariables{
  339. BuildNumberFile: stringPtr("build_number.txt"),
  340. Platform_version_name: stringPtr("S"),
  341. Platform_sdk_version: intPtr(30),
  342. Platform_sdk_codename: stringPtr("S"),
  343. Platform_sdk_final: boolPtr(false),
  344. Platform_version_active_codenames: []string{"S"},
  345. Platform_vndk_version: stringPtr("S"),
  346. HostArch: stringPtr("x86_64"),
  347. HostSecondaryArch: stringPtr("x86"),
  348. DeviceName: stringPtr("generic_arm64"),
  349. DeviceArch: stringPtr("arm64"),
  350. DeviceArchVariant: stringPtr("armv8-a"),
  351. DeviceCpuVariant: stringPtr("generic"),
  352. DeviceAbi: []string{"arm64-v8a"},
  353. DeviceSecondaryArch: stringPtr("arm"),
  354. DeviceSecondaryArchVariant: stringPtr("armv8-a"),
  355. DeviceSecondaryCpuVariant: stringPtr("generic"),
  356. DeviceSecondaryAbi: []string{"armeabi-v7a", "armeabi"},
  357. AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
  358. AAPTPreferredConfig: stringPtr("xhdpi"),
  359. AAPTCharacteristics: stringPtr("nosdcard"),
  360. AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
  361. Malloc_not_svelte: boolPtr(true),
  362. Malloc_zero_contents: boolPtr(true),
  363. Malloc_pattern_fill_contents: boolPtr(false),
  364. Safestack: boolPtr(false),
  365. BootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
  366. ApexBootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
  367. }
  368. if runtime.GOOS == "linux" {
  369. v.CrossHost = stringPtr("windows")
  370. v.CrossHostArch = stringPtr("x86")
  371. v.CrossHostSecondaryArch = stringPtr("x86_64")
  372. }
  373. }
  374. // ProductConfigContext requires the access to the Module to get product config properties.
  375. type ProductConfigContext interface {
  376. Module() Module
  377. }
  378. // ProductConfigProperty contains the information for a single property (may be a struct) paired
  379. // with the appropriate ProductConfigVariable.
  380. type ProductConfigProperty struct {
  381. ProductConfigVariable string
  382. FullConfig string
  383. Property interface{}
  384. }
  385. // ProductConfigProperties is a map of property name to a slice of ProductConfigProperty such that
  386. // all it all product variable-specific versions of a property are easily accessed together
  387. type ProductConfigProperties map[string]map[string]ProductConfigProperty
  388. // ProductVariableProperties returns a ProductConfigProperties containing only the properties which
  389. // have been set for the module in the given context.
  390. func ProductVariableProperties(ctx BaseMutatorContext) ProductConfigProperties {
  391. module := ctx.Module()
  392. moduleBase := module.base()
  393. productConfigProperties := ProductConfigProperties{}
  394. if moduleBase.variableProperties == nil {
  395. return productConfigProperties
  396. }
  397. productVariableValues(moduleBase.variableProperties, "", &productConfigProperties)
  398. for _, configToProps := range moduleBase.GetArchVariantProperties(ctx, moduleBase.variableProperties) {
  399. for config, props := range configToProps {
  400. // GetArchVariantProperties is creating an instance of the requested type
  401. // and productVariablesValues expects an interface, so no need to cast
  402. productVariableValues(props, config, &productConfigProperties)
  403. }
  404. }
  405. return productConfigProperties
  406. }
  407. func productVariableValues(variableProps interface{}, suffix string, productConfigProperties *ProductConfigProperties) {
  408. if suffix != "" {
  409. suffix = "-" + suffix
  410. }
  411. variableValues := reflect.ValueOf(variableProps).Elem().FieldByName("Product_variables")
  412. for i := 0; i < variableValues.NumField(); i++ {
  413. variableValue := variableValues.Field(i)
  414. // Check if any properties were set for the module
  415. if variableValue.IsZero() {
  416. continue
  417. }
  418. // e.g. Platform_sdk_version, Unbundled_build, Malloc_not_svelte, etc.
  419. productVariableName := variableValues.Type().Field(i).Name
  420. for j := 0; j < variableValue.NumField(); j++ {
  421. property := variableValue.Field(j)
  422. // If the property wasn't set, no need to pass it along
  423. if property.IsZero() {
  424. continue
  425. }
  426. // e.g. Asflags, Cflags, Enabled, etc.
  427. propertyName := variableValue.Type().Field(j).Name
  428. if (*productConfigProperties)[propertyName] == nil {
  429. (*productConfigProperties)[propertyName] = make(map[string]ProductConfigProperty)
  430. }
  431. config := productVariableName + suffix
  432. (*productConfigProperties)[propertyName][config] = ProductConfigProperty{
  433. ProductConfigVariable: productVariableName,
  434. FullConfig: config,
  435. Property: property.Interface(),
  436. }
  437. }
  438. }
  439. }
  440. func VariableMutator(mctx BottomUpMutatorContext) {
  441. var module Module
  442. var ok bool
  443. if module, ok = mctx.Module().(Module); !ok {
  444. return
  445. }
  446. // TODO: depend on config variable, create variants, propagate variants up tree
  447. a := module.base()
  448. if a.variableProperties == nil {
  449. return
  450. }
  451. variableValues := reflect.ValueOf(a.variableProperties).Elem().FieldByName("Product_variables")
  452. productVariables := reflect.ValueOf(mctx.Config().productVariables)
  453. for i := 0; i < variableValues.NumField(); i++ {
  454. variableValue := variableValues.Field(i)
  455. name := variableValues.Type().Field(i).Name
  456. property := "product_variables." + proptools.PropertyNameForField(name)
  457. // Check that the variable was set for the product
  458. val := productVariables.FieldByName(name)
  459. if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
  460. continue
  461. }
  462. val = val.Elem()
  463. // For bools, check that the value is true
  464. if val.Kind() == reflect.Bool && val.Bool() == false {
  465. continue
  466. }
  467. // Check if any properties were set for the module
  468. if variableValue.IsZero() {
  469. continue
  470. }
  471. a.setVariableProperties(mctx, property, variableValue, val.Interface())
  472. }
  473. }
  474. func (m *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
  475. prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
  476. printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue)
  477. err := proptools.AppendMatchingProperties(m.generalProperties,
  478. productVariablePropertyValue.Addr().Interface(), nil)
  479. if err != nil {
  480. if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
  481. ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
  482. } else {
  483. panic(err)
  484. }
  485. }
  486. }
  487. func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string,
  488. productVariablePropertyValue reflect.Value, i int, err error) {
  489. field := productVariablePropertyValue.Type().Field(i).Name
  490. property := prefix + "." + proptools.PropertyNameForField(field)
  491. ctx.PropertyErrorf(property, "%s", err)
  492. }
  493. func printfIntoProperties(ctx BottomUpMutatorContext, prefix string,
  494. productVariablePropertyValue reflect.Value, variableValue interface{}) {
  495. for i := 0; i < productVariablePropertyValue.NumField(); i++ {
  496. propertyValue := productVariablePropertyValue.Field(i)
  497. kind := propertyValue.Kind()
  498. if kind == reflect.Ptr {
  499. if propertyValue.IsNil() {
  500. continue
  501. }
  502. propertyValue = propertyValue.Elem()
  503. }
  504. switch propertyValue.Kind() {
  505. case reflect.String:
  506. err := printfIntoProperty(propertyValue, variableValue)
  507. if err != nil {
  508. printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
  509. }
  510. case reflect.Slice:
  511. for j := 0; j < propertyValue.Len(); j++ {
  512. err := printfIntoProperty(propertyValue.Index(j), variableValue)
  513. if err != nil {
  514. printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
  515. }
  516. }
  517. case reflect.Bool:
  518. // Nothing
  519. case reflect.Struct:
  520. printfIntoProperties(ctx, prefix, propertyValue, variableValue)
  521. default:
  522. panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
  523. }
  524. }
  525. }
  526. func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error {
  527. s := propertyValue.String()
  528. count := strings.Count(s, "%")
  529. if count == 0 {
  530. return nil
  531. }
  532. if count > 1 {
  533. return fmt.Errorf("product variable properties only support a single '%%'")
  534. }
  535. if strings.Contains(s, "%d") {
  536. switch v := variableValue.(type) {
  537. case int:
  538. // Nothing
  539. case bool:
  540. if v {
  541. variableValue = 1
  542. } else {
  543. variableValue = 0
  544. }
  545. default:
  546. return fmt.Errorf("unsupported type %T for %%d", variableValue)
  547. }
  548. } else if strings.Contains(s, "%s") {
  549. switch variableValue.(type) {
  550. case string:
  551. // Nothing
  552. default:
  553. return fmt.Errorf("unsupported type %T for %%s", variableValue)
  554. }
  555. } else {
  556. return fmt.Errorf("unsupported %% in product variable property")
  557. }
  558. propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue)))
  559. return nil
  560. }
  561. var variablePropTypeMap OncePer
  562. // sliceToTypeArray takes a slice of property structs and returns a reflection created array containing the
  563. // reflect.Types of each property struct. The result can be used as a key in a map.
  564. func sliceToTypeArray(s []interface{}) interface{} {
  565. // Create an array using reflection whose length is the length of the input slice
  566. ret := reflect.New(reflect.ArrayOf(len(s), reflect.TypeOf(reflect.TypeOf(0)))).Elem()
  567. for i, e := range s {
  568. ret.Index(i).Set(reflect.ValueOf(reflect.TypeOf(e)))
  569. }
  570. return ret.Interface()
  571. }
  572. func initProductVariableModule(m Module) {
  573. base := m.base()
  574. // Allow tests to override the default product variables
  575. if base.variableProperties == nil {
  576. base.variableProperties = defaultProductVariables
  577. }
  578. // Filter the product variables properties to the ones that exist on this module
  579. base.variableProperties = createVariableProperties(m.GetProperties(), base.variableProperties)
  580. if base.variableProperties != nil {
  581. m.AddProperties(base.variableProperties)
  582. }
  583. }
  584. // createVariableProperties takes the list of property structs for a module and returns a property struct that
  585. // contains the product variable properties that exist in the property structs, or nil if there are none. It
  586. // caches the result.
  587. func createVariableProperties(moduleTypeProps []interface{}, productVariables interface{}) interface{} {
  588. // Convert the moduleTypeProps to an array of reflect.Types that can be used as a key in the OncePer.
  589. key := sliceToTypeArray(moduleTypeProps)
  590. // Use the variablePropTypeMap OncePer to cache the result for each set of property struct types.
  591. typ, _ := variablePropTypeMap.Once(NewCustomOnceKey(key), func() interface{} {
  592. // Compute the filtered property struct type.
  593. return createVariablePropertiesType(moduleTypeProps, productVariables)
  594. }).(reflect.Type)
  595. if typ == nil {
  596. return nil
  597. }
  598. // Create a new pointer to a filtered property struct.
  599. return reflect.New(typ).Interface()
  600. }
  601. // createVariablePropertiesType creates a new type that contains only the product variable properties that exist in
  602. // a list of property structs.
  603. func createVariablePropertiesType(moduleTypeProps []interface{}, productVariables interface{}) reflect.Type {
  604. typ, _ := proptools.FilterPropertyStruct(reflect.TypeOf(productVariables),
  605. func(field reflect.StructField, prefix string) (bool, reflect.StructField) {
  606. // Filter function, returns true if the field should be in the resulting struct
  607. if prefix == "" {
  608. // Keep the top level Product_variables field
  609. return true, field
  610. }
  611. _, rest := splitPrefix(prefix)
  612. if rest == "" {
  613. // Keep the 2nd level field (i.e. Product_variables.Eng)
  614. return true, field
  615. }
  616. // Strip off the first 2 levels of the prefix
  617. _, prefix = splitPrefix(rest)
  618. for _, p := range moduleTypeProps {
  619. if fieldExistsByNameRecursive(reflect.TypeOf(p).Elem(), prefix, field.Name) {
  620. // Keep any fields that exist in one of the property structs
  621. return true, field
  622. }
  623. }
  624. return false, field
  625. })
  626. return typ
  627. }
  628. func splitPrefix(prefix string) (first, rest string) {
  629. index := strings.IndexByte(prefix, '.')
  630. if index == -1 {
  631. return prefix, ""
  632. }
  633. return prefix[:index], prefix[index+1:]
  634. }
  635. func fieldExistsByNameRecursive(t reflect.Type, prefix, name string) bool {
  636. if t.Kind() != reflect.Struct {
  637. panic(fmt.Errorf("fieldExistsByNameRecursive can only be called on a reflect.Struct"))
  638. }
  639. if prefix != "" {
  640. split := strings.SplitN(prefix, ".", 2)
  641. firstPrefix := split[0]
  642. rest := ""
  643. if len(split) > 1 {
  644. rest = split[1]
  645. }
  646. f, exists := t.FieldByName(firstPrefix)
  647. if !exists {
  648. return false
  649. }
  650. ft := f.Type
  651. if ft.Kind() == reflect.Ptr {
  652. ft = ft.Elem()
  653. }
  654. if ft.Kind() != reflect.Struct {
  655. panic(fmt.Errorf("field %q in %q is not a struct", firstPrefix, t))
  656. }
  657. return fieldExistsByNameRecursive(ft, rest, name)
  658. } else {
  659. _, exists := t.FieldByName(name)
  660. return exists
  661. }
  662. }