variable.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. "android/soong/android/soongconfig"
  21. "android/soong/bazel"
  22. "github.com/google/blueprint/proptools"
  23. )
  24. func init() {
  25. registerVariableBuildComponents(InitRegistrationContext)
  26. }
  27. func registerVariableBuildComponents(ctx RegistrationContext) {
  28. ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
  29. ctx.BottomUp("variable", VariableMutator).Parallel()
  30. })
  31. }
  32. var PrepareForTestWithVariables = FixtureRegisterWithContext(registerVariableBuildComponents)
  33. type variableProperties struct {
  34. Product_variables struct {
  35. Platform_sdk_version struct {
  36. Asflags []string
  37. Cflags []string
  38. Cmd *string
  39. }
  40. Platform_sdk_version_or_codename struct {
  41. Java_resource_dirs []string
  42. }
  43. Platform_sdk_extension_version struct {
  44. Cmd *string
  45. }
  46. Platform_version_name struct {
  47. Base_dir *string
  48. }
  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"`
  54. // similar to `Unbundled_build`, but `Always_use_prebuilt_sdks` means that it uses prebuilt
  55. // sdk specifically.
  56. Always_use_prebuilt_sdks struct {
  57. Enabled *bool `android:"arch_variant"`
  58. } `android:"arch_variant"`
  59. Malloc_not_svelte struct {
  60. Cflags []string `android:"arch_variant"`
  61. Shared_libs []string `android:"arch_variant"`
  62. Whole_static_libs []string `android:"arch_variant"`
  63. Exclude_static_libs []string `android:"arch_variant"`
  64. Srcs []string `android:"arch_variant"`
  65. Header_libs []string `android:"arch_variant"`
  66. } `android:"arch_variant"`
  67. Malloc_zero_contents struct {
  68. Cflags []string `android:"arch_variant"`
  69. } `android:"arch_variant"`
  70. Malloc_pattern_fill_contents struct {
  71. Cflags []string `android:"arch_variant"`
  72. } `android:"arch_variant"`
  73. Safestack struct {
  74. Cflags []string `android:"arch_variant"`
  75. } `android:"arch_variant"`
  76. Binder32bit struct {
  77. Cflags []string
  78. }
  79. Override_rs_driver struct {
  80. Cflags []string
  81. }
  82. // treble_linker_namespaces is true when the system/vendor linker namespace separation is
  83. // enabled.
  84. Treble_linker_namespaces struct {
  85. Cflags []string
  86. }
  87. // enforce_vintf_manifest is true when a device is required to have a vintf manifest.
  88. Enforce_vintf_manifest struct {
  89. Cflags []string
  90. }
  91. Build_from_text_stub struct {
  92. Static_libs []string
  93. Exclude_static_libs []string
  94. }
  95. // debuggable is true for eng and userdebug builds, and can be used to turn on additional
  96. // debugging features that don't significantly impact runtime behavior. userdebug builds
  97. // are used for dogfooding and performance testing, and should be as similar to user builds
  98. // as possible.
  99. Debuggable struct {
  100. Cflags []string
  101. Cppflags []string
  102. Init_rc []string
  103. Required []string
  104. Host_required []string
  105. Target_required []string
  106. Strip struct {
  107. All *bool
  108. Keep_symbols *bool
  109. Keep_symbols_and_debug_frame *bool
  110. }
  111. Static_libs []string
  112. Whole_static_libs []string
  113. Shared_libs []string
  114. Cmdline []string
  115. Srcs []string
  116. Exclude_srcs []string
  117. }
  118. // eng is true for -eng builds, and can be used to turn on additional heavyweight debugging
  119. // features.
  120. Eng struct {
  121. Cflags []string
  122. Cppflags []string
  123. Lto struct {
  124. Never *bool
  125. }
  126. Sanitize struct {
  127. Address *bool
  128. }
  129. Optimize struct {
  130. Enabled *bool
  131. }
  132. }
  133. Pdk struct {
  134. Enabled *bool `android:"arch_variant"`
  135. } `android:"arch_variant"`
  136. Uml struct {
  137. Cppflags []string
  138. }
  139. Arc struct {
  140. Cflags []string `android:"arch_variant"`
  141. Exclude_srcs []string `android:"arch_variant"`
  142. Header_libs []string `android:"arch_variant"`
  143. Include_dirs []string `android:"arch_variant"`
  144. Shared_libs []string `android:"arch_variant"`
  145. Static_libs []string `android:"arch_variant"`
  146. Srcs []string `android:"arch_variant"`
  147. Whole_static_libs []string `android:"arch_variant"`
  148. } `android:"arch_variant"`
  149. Native_coverage struct {
  150. Src *string `android:"arch_variant"`
  151. Srcs []string `android:"arch_variant"`
  152. Exclude_srcs []string `android:"arch_variant"`
  153. } `android:"arch_variant"`
  154. } `android:"arch_variant"`
  155. }
  156. var defaultProductVariables interface{} = variableProperties{}
  157. type productVariables struct {
  158. // Suffix to add to generated Makefiles
  159. Make_suffix *string `json:",omitempty"`
  160. BuildId *string `json:",omitempty"`
  161. BuildNumberFile *string `json:",omitempty"`
  162. Platform_version_name *string `json:",omitempty"`
  163. Platform_sdk_version *int `json:",omitempty"`
  164. Platform_sdk_codename *string `json:",omitempty"`
  165. Platform_sdk_version_or_codename *string `json:",omitempty"`
  166. Platform_sdk_final *bool `json:",omitempty"`
  167. Platform_sdk_extension_version *int `json:",omitempty"`
  168. Platform_base_sdk_extension_version *int `json:",omitempty"`
  169. Platform_version_active_codenames []string `json:",omitempty"`
  170. Platform_version_all_preview_codenames []string `json:",omitempty"`
  171. Platform_vndk_version *string `json:",omitempty"`
  172. Platform_systemsdk_versions []string `json:",omitempty"`
  173. Platform_security_patch *string `json:",omitempty"`
  174. Platform_preview_sdk_version *string `json:",omitempty"`
  175. Platform_min_supported_target_sdk_version *string `json:",omitempty"`
  176. Platform_base_os *string `json:",omitempty"`
  177. Platform_version_last_stable *string `json:",omitempty"`
  178. Platform_version_known_codenames *string `json:",omitempty"`
  179. DeviceName *string `json:",omitempty"`
  180. DeviceProduct *string `json:",omitempty"`
  181. DeviceArch *string `json:",omitempty"`
  182. DeviceArchVariant *string `json:",omitempty"`
  183. DeviceCpuVariant *string `json:",omitempty"`
  184. DeviceAbi []string `json:",omitempty"`
  185. DeviceVndkVersion *string `json:",omitempty"`
  186. DeviceCurrentApiLevelForVendorModules *string `json:",omitempty"`
  187. DeviceSystemSdkVersions []string `json:",omitempty"`
  188. DeviceMaxPageSizeSupported *string `json:",omitempty"`
  189. RecoverySnapshotVersion *string `json:",omitempty"`
  190. DeviceSecondaryArch *string `json:",omitempty"`
  191. DeviceSecondaryArchVariant *string `json:",omitempty"`
  192. DeviceSecondaryCpuVariant *string `json:",omitempty"`
  193. DeviceSecondaryAbi []string `json:",omitempty"`
  194. NativeBridgeArch *string `json:",omitempty"`
  195. NativeBridgeArchVariant *string `json:",omitempty"`
  196. NativeBridgeCpuVariant *string `json:",omitempty"`
  197. NativeBridgeAbi []string `json:",omitempty"`
  198. NativeBridgeRelativePath *string `json:",omitempty"`
  199. NativeBridgeSecondaryArch *string `json:",omitempty"`
  200. NativeBridgeSecondaryArchVariant *string `json:",omitempty"`
  201. NativeBridgeSecondaryCpuVariant *string `json:",omitempty"`
  202. NativeBridgeSecondaryAbi []string `json:",omitempty"`
  203. NativeBridgeSecondaryRelativePath *string `json:",omitempty"`
  204. HostArch *string `json:",omitempty"`
  205. HostSecondaryArch *string `json:",omitempty"`
  206. HostMusl *bool `json:",omitempty"`
  207. CrossHost *string `json:",omitempty"`
  208. CrossHostArch *string `json:",omitempty"`
  209. CrossHostSecondaryArch *string `json:",omitempty"`
  210. DeviceResourceOverlays []string `json:",omitempty"`
  211. ProductResourceOverlays []string `json:",omitempty"`
  212. EnforceRROTargets []string `json:",omitempty"`
  213. EnforceRROExcludedOverlays []string `json:",omitempty"`
  214. AAPTCharacteristics *string `json:",omitempty"`
  215. AAPTConfig []string `json:",omitempty"`
  216. AAPTPreferredConfig *string `json:",omitempty"`
  217. AAPTPrebuiltDPI []string `json:",omitempty"`
  218. DefaultAppCertificate *string `json:",omitempty"`
  219. MainlineSepolicyDevCertificates *string `json:",omitempty"`
  220. AppsDefaultVersionName *string `json:",omitempty"`
  221. Allow_missing_dependencies *bool `json:",omitempty"`
  222. Unbundled_build *bool `json:",omitempty"`
  223. Unbundled_build_apps []string `json:",omitempty"`
  224. Unbundled_build_image *bool `json:",omitempty"`
  225. Always_use_prebuilt_sdks *bool `json:",omitempty"`
  226. Skip_boot_jars_check *bool `json:",omitempty"`
  227. Malloc_not_svelte *bool `json:",omitempty"`
  228. Malloc_zero_contents *bool `json:",omitempty"`
  229. Malloc_pattern_fill_contents *bool `json:",omitempty"`
  230. Safestack *bool `json:",omitempty"`
  231. HostStaticBinaries *bool `json:",omitempty"`
  232. Binder32bit *bool `json:",omitempty"`
  233. UseGoma *bool `json:",omitempty"`
  234. UseRBE *bool `json:",omitempty"`
  235. UseRBEJAVAC *bool `json:",omitempty"`
  236. UseRBER8 *bool `json:",omitempty"`
  237. UseRBED8 *bool `json:",omitempty"`
  238. Debuggable *bool `json:",omitempty"`
  239. Eng *bool `json:",omitempty"`
  240. Treble_linker_namespaces *bool `json:",omitempty"`
  241. Enforce_vintf_manifest *bool `json:",omitempty"`
  242. Uml *bool `json:",omitempty"`
  243. Arc *bool `json:",omitempty"`
  244. MinimizeJavaDebugInfo *bool `json:",omitempty"`
  245. Build_from_text_stub *bool `json:",omitempty"`
  246. Check_elf_files *bool `json:",omitempty"`
  247. UncompressPrivAppDex *bool `json:",omitempty"`
  248. ModulesLoadedByPrivilegedModules []string `json:",omitempty"`
  249. BootJars ConfiguredJarList `json:",omitempty"`
  250. ApexBootJars ConfiguredJarList `json:",omitempty"`
  251. IntegerOverflowExcludePaths []string `json:",omitempty"`
  252. EnableCFI *bool `json:",omitempty"`
  253. CFIExcludePaths []string `json:",omitempty"`
  254. CFIIncludePaths []string `json:",omitempty"`
  255. DisableScudo *bool `json:",omitempty"`
  256. MemtagHeapExcludePaths []string `json:",omitempty"`
  257. MemtagHeapAsyncIncludePaths []string `json:",omitempty"`
  258. MemtagHeapSyncIncludePaths []string `json:",omitempty"`
  259. HWASanIncludePaths []string `json:",omitempty"`
  260. VendorPath *string `json:",omitempty"`
  261. OdmPath *string `json:",omitempty"`
  262. ProductPath *string `json:",omitempty"`
  263. SystemExtPath *string `json:",omitempty"`
  264. ClangTidy *bool `json:",omitempty"`
  265. TidyChecks *string `json:",omitempty"`
  266. JavaCoveragePaths []string `json:",omitempty"`
  267. JavaCoverageExcludePaths []string `json:",omitempty"`
  268. GcovCoverage *bool `json:",omitempty"`
  269. ClangCoverage *bool `json:",omitempty"`
  270. NativeCoveragePaths []string `json:",omitempty"`
  271. NativeCoverageExcludePaths []string `json:",omitempty"`
  272. ClangCoverageContinuousMode *bool `json:",omitempty"`
  273. // Set by NewConfig
  274. Native_coverage *bool `json:",omitempty"`
  275. SanitizeHost []string `json:",omitempty"`
  276. SanitizeDevice []string `json:",omitempty"`
  277. SanitizeDeviceDiag []string `json:",omitempty"`
  278. SanitizeDeviceArch []string `json:",omitempty"`
  279. ArtUseReadBarrier *bool `json:",omitempty"`
  280. BtConfigIncludeDir *string `json:",omitempty"`
  281. Override_rs_driver *string `json:",omitempty"`
  282. DeviceKernelHeaders []string `json:",omitempty"`
  283. ExtraVndkVersions []string `json:",omitempty"`
  284. NamespacesToExport []string `json:",omitempty"`
  285. PgoAdditionalProfileDirs []string `json:",omitempty"`
  286. VndkUseCoreVariant *bool `json:",omitempty"`
  287. VndkSnapshotBuildArtifacts *bool `json:",omitempty"`
  288. DirectedVendorSnapshot bool `json:",omitempty"`
  289. VendorSnapshotModules map[string]bool `json:",omitempty"`
  290. DirectedRecoverySnapshot bool `json:",omitempty"`
  291. RecoverySnapshotModules map[string]bool `json:",omitempty"`
  292. VendorSnapshotDirsIncluded []string `json:",omitempty"`
  293. VendorSnapshotDirsExcluded []string `json:",omitempty"`
  294. RecoverySnapshotDirsExcluded []string `json:",omitempty"`
  295. RecoverySnapshotDirsIncluded []string `json:",omitempty"`
  296. HostFakeSnapshotEnabled bool `json:",omitempty"`
  297. MultitreeUpdateMeta bool `json:",omitempty"`
  298. BoardVendorSepolicyDirs []string `json:",omitempty"`
  299. BoardOdmSepolicyDirs []string `json:",omitempty"`
  300. BoardReqdMaskPolicy []string `json:",omitempty"`
  301. BoardPlatVendorPolicy []string `json:",omitempty"`
  302. BoardSystemExtPublicPrebuiltDirs []string `json:",omitempty"`
  303. BoardSystemExtPrivatePrebuiltDirs []string `json:",omitempty"`
  304. BoardProductPublicPrebuiltDirs []string `json:",omitempty"`
  305. BoardProductPrivatePrebuiltDirs []string `json:",omitempty"`
  306. SystemExtPublicSepolicyDirs []string `json:",omitempty"`
  307. SystemExtPrivateSepolicyDirs []string `json:",omitempty"`
  308. BoardSepolicyM4Defs []string `json:",omitempty"`
  309. BoardSepolicyVers *string `json:",omitempty"`
  310. PlatformSepolicyVersion *string `json:",omitempty"`
  311. TotSepolicyVersion *string `json:",omitempty"`
  312. SystemExtSepolicyPrebuiltApiDir *string `json:",omitempty"`
  313. ProductSepolicyPrebuiltApiDir *string `json:",omitempty"`
  314. PlatformSepolicyCompatVersions []string `json:",omitempty"`
  315. VendorVars map[string]map[string]string `json:",omitempty"`
  316. Ndk_abis *bool `json:",omitempty"`
  317. TrimmedApex *bool `json:",omitempty"`
  318. ForceApexSymlinkOptimization *bool `json:",omitempty"`
  319. CompressedApex *bool `json:",omitempty"`
  320. Aml_abis *bool `json:",omitempty"`
  321. DexpreoptGlobalConfig *string `json:",omitempty"`
  322. WithDexpreopt bool `json:",omitempty"`
  323. ManifestPackageNameOverrides []string `json:",omitempty"`
  324. CertificateOverrides []string `json:",omitempty"`
  325. PackageNameOverrides []string `json:",omitempty"`
  326. ApexGlobalMinSdkVersionOverride *string `json:",omitempty"`
  327. EnforceSystemCertificate *bool `json:",omitempty"`
  328. EnforceSystemCertificateAllowList []string `json:",omitempty"`
  329. ProductHiddenAPIStubs []string `json:",omitempty"`
  330. ProductHiddenAPIStubsSystem []string `json:",omitempty"`
  331. ProductHiddenAPIStubsTest []string `json:",omitempty"`
  332. ProductPublicSepolicyDirs []string `json:",omitempty"`
  333. ProductPrivateSepolicyDirs []string `json:",omitempty"`
  334. ProductVndkVersion *string `json:",omitempty"`
  335. TargetFSConfigGen []string `json:",omitempty"`
  336. EnforceProductPartitionInterface *bool `json:",omitempty"`
  337. EnforceInterPartitionJavaSdkLibrary *bool `json:",omitempty"`
  338. InterPartitionJavaLibraryAllowList []string `json:",omitempty"`
  339. BoardUsesRecoveryAsBoot *bool `json:",omitempty"`
  340. BoardKernelBinaries []string `json:",omitempty"`
  341. BoardKernelModuleInterfaceVersions []string `json:",omitempty"`
  342. BoardMoveRecoveryResourcesToVendorBoot *bool `json:",omitempty"`
  343. PrebuiltHiddenApiDir *string `json:",omitempty"`
  344. ShippingApiLevel *string `json:",omitempty"`
  345. BuildBrokenPluginValidation []string `json:",omitempty"`
  346. BuildBrokenClangAsFlags bool `json:",omitempty"`
  347. BuildBrokenClangCFlags bool `json:",omitempty"`
  348. BuildBrokenClangProperty bool `json:",omitempty"`
  349. GenruleSandboxing *bool `json:",omitempty"`
  350. BuildBrokenEnforceSyspropOwner bool `json:",omitempty"`
  351. BuildBrokenTrebleSyspropNeverallow bool `json:",omitempty"`
  352. BuildBrokenUsesSoongPython2Modules bool `json:",omitempty"`
  353. BuildBrokenVendorPropertyNamespace bool `json:",omitempty"`
  354. BuildBrokenInputDirModules []string `json:",omitempty"`
  355. BuildWarningBadOptionalUsesLibsAllowlist []string `json:",omitempty"`
  356. BuildDebugfsRestrictionsEnabled bool `json:",omitempty"`
  357. RequiresInsecureExecmemForSwiftshader bool `json:",omitempty"`
  358. SelinuxIgnoreNeverallows bool `json:",omitempty"`
  359. SepolicyFreezeTestExtraDirs []string `json:",omitempty"`
  360. SepolicyFreezeTestExtraPrebuiltDirs []string `json:",omitempty"`
  361. GenerateAidlNdkPlatformBackend bool `json:",omitempty"`
  362. IgnorePrefer32OnDevice bool `json:",omitempty"`
  363. IncludeTags []string `json:",omitempty"`
  364. SourceRootDirs []string `json:",omitempty"`
  365. AfdoProfiles []string `json:",omitempty"`
  366. ProductManufacturer string `json:",omitempty"`
  367. ProductBrand string `json:",omitempty"`
  368. BuildVersionTags []string `json:",omitempty"`
  369. ReleaseVersion string `json:",omitempty"`
  370. ReleaseDeviceConfigValueSets []string `json:",omitempty"`
  371. }
  372. func boolPtr(v bool) *bool {
  373. return &v
  374. }
  375. func intPtr(v int) *int {
  376. return &v
  377. }
  378. func stringPtr(v string) *string {
  379. return &v
  380. }
  381. func (v *productVariables) SetDefaultConfig() {
  382. *v = productVariables{
  383. BuildNumberFile: stringPtr("build_number.txt"),
  384. Platform_version_name: stringPtr("S"),
  385. Platform_base_sdk_extension_version: intPtr(30),
  386. Platform_sdk_version: intPtr(30),
  387. Platform_sdk_codename: stringPtr("S"),
  388. Platform_sdk_final: boolPtr(false),
  389. Platform_version_active_codenames: []string{"S"},
  390. Platform_version_all_preview_codenames: []string{"S"},
  391. Platform_vndk_version: stringPtr("S"),
  392. HostArch: stringPtr("x86_64"),
  393. HostSecondaryArch: stringPtr("x86"),
  394. DeviceName: stringPtr("generic_arm64"),
  395. DeviceProduct: stringPtr("aosp_arm-eng"),
  396. DeviceArch: stringPtr("arm64"),
  397. DeviceArchVariant: stringPtr("armv8-a"),
  398. DeviceCpuVariant: stringPtr("generic"),
  399. DeviceAbi: []string{"arm64-v8a"},
  400. DeviceSecondaryArch: stringPtr("arm"),
  401. DeviceSecondaryArchVariant: stringPtr("armv8-a"),
  402. DeviceSecondaryCpuVariant: stringPtr("generic"),
  403. DeviceSecondaryAbi: []string{"armeabi-v7a", "armeabi"},
  404. DeviceMaxPageSizeSupported: stringPtr("4096"),
  405. AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
  406. AAPTPreferredConfig: stringPtr("xhdpi"),
  407. AAPTCharacteristics: stringPtr("nosdcard"),
  408. AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
  409. Malloc_not_svelte: boolPtr(true),
  410. Malloc_zero_contents: boolPtr(true),
  411. Malloc_pattern_fill_contents: boolPtr(false),
  412. Safestack: boolPtr(false),
  413. TrimmedApex: boolPtr(false),
  414. Build_from_text_stub: boolPtr(false),
  415. BootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
  416. ApexBootJars: ConfiguredJarList{apexes: []string{}, jars: []string{}},
  417. }
  418. if runtime.GOOS == "linux" {
  419. v.CrossHost = stringPtr("windows")
  420. v.CrossHostArch = stringPtr("x86")
  421. v.CrossHostSecondaryArch = stringPtr("x86_64")
  422. }
  423. }
  424. // ProductConfigContext requires the access to the Module to get product config properties.
  425. type ProductConfigContext interface {
  426. Module() Module
  427. }
  428. // ProductConfigOrSoongConfigProperty represents either a soong config variable + its value
  429. // or a product config variable. You can get both a ConfigurationAxis and a SelectKey from it
  430. // for use in bazel attributes. ProductVariableProperties() will return a map from properties ->
  431. // this interface -> property structs for use in bp2build converters
  432. type ProductConfigOrSoongConfigProperty interface {
  433. // Name of the product variable or soong config variable
  434. Name() string
  435. // AlwaysEmit returns true for soong config variables but false for product variables. This
  436. // is intended to indicate if we need to always emit empty lists in the select statements.
  437. AlwaysEmit() bool
  438. // ConfigurationAxis returns the bazel.ConfigurationAxis that represents this variable. The
  439. // configuration axis will change depending on the variable and whether it's arch/os variant
  440. // as well.
  441. ConfigurationAxis() bazel.ConfigurationAxis
  442. // SelectKey returns a string that represents the key of a select branch, however, it is not
  443. // actually the real label written out to the build file.
  444. // this.ConfigurationAxis().SelectKey(this.SelectKey()) will give the actual label.
  445. SelectKey() string
  446. }
  447. // ProductConfigProperty represents a product config variable, and if it is arch-variant or not.
  448. type ProductConfigProperty struct {
  449. // The name of the product variable, e.g. "safestack", "malloc_not_svelte",
  450. // "board"
  451. name string
  452. arch string
  453. }
  454. func (p ProductConfigProperty) Name() string {
  455. return p.name
  456. }
  457. func (p ProductConfigProperty) AlwaysEmit() bool {
  458. return false
  459. }
  460. func (p ProductConfigProperty) ConfigurationAxis() bazel.ConfigurationAxis {
  461. return bazel.ProductVariableConfigurationAxis(p.arch != "", p.name+"__"+p.arch)
  462. }
  463. func (p ProductConfigProperty) SelectKey() string {
  464. if p.arch == "" {
  465. return strings.ToLower(p.name)
  466. } else {
  467. return strings.ToLower(p.name + "-" + p.arch)
  468. }
  469. }
  470. // SoongConfigProperty represents a soong config variable, its value if it's a string variable,
  471. // and if it's dependent on the OS or not
  472. type SoongConfigProperty struct {
  473. name string
  474. namespace string
  475. // Can be an empty string for bool/value soong config variables
  476. value string
  477. // If there is a target: field inside a soong config property struct, the os that it selects
  478. // on will be represented here.
  479. os string
  480. }
  481. func (p SoongConfigProperty) Name() string {
  482. return p.name
  483. }
  484. func (p SoongConfigProperty) AlwaysEmit() bool {
  485. return true
  486. }
  487. func (p SoongConfigProperty) ConfigurationAxis() bazel.ConfigurationAxis {
  488. return bazel.ProductVariableConfigurationAxis(false, p.namespace+"__"+p.name+"__"+p.os)
  489. }
  490. // SelectKey returns the literal string that represents this variable in a BUILD
  491. // select statement.
  492. func (p SoongConfigProperty) SelectKey() string {
  493. // p.value being conditions_default can happen with or without a desired os. When not using
  494. // an os, we want to emit literally just //conditions:default in the select statement, but
  495. // when using an os, we want to emit namespace__name__conditions_default__os, so that
  496. // the branch is only taken if the variable is not set, and we're on the desired os.
  497. // ConfigurationAxis#SelectKey will map the conditions_default result of this function to
  498. // //conditions:default.
  499. if p.value == bazel.ConditionsDefaultConfigKey && p.os == "" {
  500. return bazel.ConditionsDefaultConfigKey
  501. }
  502. parts := []string{p.namespace, p.name}
  503. if p.value != "" && p.value != bazel.ConditionsDefaultSelectKey {
  504. parts = append(parts, p.value)
  505. }
  506. if p.os != "" {
  507. parts = append(parts, p.os)
  508. }
  509. // e.g. acme__feature1, android__board__soc_a, my_namespace__my_variables__my_value__my_os
  510. return strings.ToLower(strings.Join(parts, "__"))
  511. }
  512. // ProductConfigProperties is a map of maps to group property values according
  513. // their property name and the product config variable they're set under.
  514. //
  515. // The outer map key is the name of the property, like "cflags".
  516. //
  517. // The inner map key is a ProductConfigProperty, which is a struct of product
  518. // variable name, namespace, and the "full configuration" of the product
  519. // variable.
  520. //
  521. // e.g. product variable name: board, namespace: acme, full config: vendor_chip_foo
  522. //
  523. // The value of the map is the interface{} representing the value of the
  524. // property, like ["-DDEFINES"] for cflags.
  525. type ProductConfigProperties map[string]map[ProductConfigOrSoongConfigProperty]interface{}
  526. // ProductVariableProperties returns a ProductConfigProperties containing only the properties which
  527. // have been set for the given module.
  528. func ProductVariableProperties(ctx ArchVariantContext, module Module) ProductConfigProperties {
  529. moduleBase := module.base()
  530. productConfigProperties := ProductConfigProperties{}
  531. if moduleBase.variableProperties != nil {
  532. productVariablesProperty := proptools.FieldNameForProperty("product_variables")
  533. for /* axis */ _, configToProps := range moduleBase.GetArchVariantProperties(ctx, moduleBase.variableProperties) {
  534. for config, props := range configToProps {
  535. variableValues := reflect.ValueOf(props).Elem().FieldByName(productVariablesProperty)
  536. productConfigProperties.AddProductConfigProperties(variableValues, config)
  537. }
  538. }
  539. }
  540. if m, ok := module.(Bazelable); ok && m.namespacedVariableProps() != nil {
  541. for namespace, namespacedVariableProps := range m.namespacedVariableProps() {
  542. for _, namespacedVariableProp := range namespacedVariableProps {
  543. variableValues := reflect.ValueOf(namespacedVariableProp).Elem().FieldByName(soongconfig.SoongConfigProperty)
  544. productConfigProperties.AddSoongConfigProperties(namespace, variableValues)
  545. }
  546. }
  547. }
  548. return productConfigProperties
  549. }
  550. func (p *ProductConfigProperties) AddProductConfigProperty(
  551. propertyName, productVariableName, arch string, propertyValue interface{}) {
  552. productConfigProp := ProductConfigProperty{
  553. name: productVariableName, // e.g. size, feature1, feature2, FEATURE3, board
  554. arch: arch, // e.g. "", x86, arm64
  555. }
  556. p.AddEitherProperty(propertyName, productConfigProp, propertyValue)
  557. }
  558. func (p *ProductConfigProperties) AddSoongConfigProperty(
  559. propertyName, namespace, variableName, value, os string, propertyValue interface{}) {
  560. soongConfigProp := SoongConfigProperty{
  561. namespace: namespace,
  562. name: variableName, // e.g. size, feature1, feature2, FEATURE3, board
  563. value: value,
  564. os: os, // e.g. android, linux_x86
  565. }
  566. p.AddEitherProperty(propertyName, soongConfigProp, propertyValue)
  567. }
  568. func (p *ProductConfigProperties) AddEitherProperty(
  569. propertyName string, key ProductConfigOrSoongConfigProperty, propertyValue interface{}) {
  570. if (*p)[propertyName] == nil {
  571. (*p)[propertyName] = make(map[ProductConfigOrSoongConfigProperty]interface{})
  572. }
  573. if existing, ok := (*p)[propertyName][key]; ok {
  574. switch dst := existing.(type) {
  575. case []string:
  576. src, ok := propertyValue.([]string)
  577. if !ok {
  578. panic("Conflicting types")
  579. }
  580. dst = append(dst, src...)
  581. (*p)[propertyName][key] = dst
  582. default:
  583. panic(fmt.Errorf("TODO: handle merging value %#v", existing))
  584. }
  585. } else {
  586. (*p)[propertyName][key] = propertyValue
  587. }
  588. }
  589. var (
  590. conditionsDefaultField string = proptools.FieldNameForProperty(bazel.ConditionsDefaultConfigKey)
  591. )
  592. // maybeExtractConfigVarProp attempts to read this value as a config var struct
  593. // wrapped by interfaces and ptrs. If it's not the right type, the second return
  594. // value is false.
  595. func maybeExtractConfigVarProp(v reflect.Value) (reflect.Value, bool) {
  596. if v.Kind() == reflect.Interface {
  597. // The conditions_default value can be either
  598. // 1) an ptr to an interface of a struct (bool config variables and product variables)
  599. // 2) an interface of 1) (config variables with nested structs, like string vars)
  600. v = v.Elem()
  601. }
  602. if v.Kind() != reflect.Ptr {
  603. return v, false
  604. }
  605. v = reflect.Indirect(v)
  606. if v.Kind() == reflect.Interface {
  607. // Extract the struct from the interface
  608. v = v.Elem()
  609. }
  610. if !v.IsValid() {
  611. return v, false
  612. }
  613. if v.Kind() != reflect.Struct {
  614. return v, false
  615. }
  616. return v, true
  617. }
  618. func (productConfigProperties *ProductConfigProperties) AddProductConfigProperties(variableValues reflect.Value, arch string) {
  619. // Example of product_variables:
  620. //
  621. // product_variables: {
  622. // malloc_not_svelte: {
  623. // shared_libs: ["malloc_not_svelte_shared_lib"],
  624. // whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
  625. // exclude_static_libs: [
  626. // "malloc_not_svelte_static_lib_excludes",
  627. // "malloc_not_svelte_whole_static_lib_excludes",
  628. // ],
  629. // },
  630. // },
  631. for i := 0; i < variableValues.NumField(); i++ {
  632. // e.g. Platform_sdk_version, Unbundled_build, Malloc_not_svelte, etc.
  633. productVariableName := variableValues.Type().Field(i).Name
  634. variableValue := variableValues.Field(i)
  635. // Check if any properties were set for the module
  636. if variableValue.IsZero() {
  637. // e.g. feature1: {}, malloc_not_svelte: {}
  638. continue
  639. }
  640. for j := 0; j < variableValue.NumField(); j++ {
  641. property := variableValue.Field(j)
  642. // e.g. Asflags, Cflags, Enabled, etc.
  643. propertyName := variableValue.Type().Field(j).Name
  644. if property.Kind() != reflect.Interface {
  645. productConfigProperties.AddProductConfigProperty(propertyName, productVariableName, arch, property.Interface())
  646. }
  647. }
  648. }
  649. }
  650. func (productConfigProperties *ProductConfigProperties) AddSoongConfigProperties(namespace string, soongConfigVariablesStruct reflect.Value) {
  651. //
  652. // Example of soong_config_variables:
  653. //
  654. // soong_config_variables: {
  655. // feature1: {
  656. // conditions_default: {
  657. // ...
  658. // },
  659. // cflags: ...
  660. // },
  661. // feature2: {
  662. // cflags: ...
  663. // conditions_default: {
  664. // ...
  665. // },
  666. // },
  667. // board: {
  668. // soc_a: {
  669. // ...
  670. // },
  671. // soc_b: {
  672. // ...
  673. // },
  674. // soc_c: {},
  675. // conditions_default: {
  676. // ...
  677. // },
  678. // },
  679. // }
  680. for i := 0; i < soongConfigVariablesStruct.NumField(); i++ {
  681. // e.g. feature1, feature2, board
  682. variableName := soongConfigVariablesStruct.Type().Field(i).Name
  683. variableStruct := soongConfigVariablesStruct.Field(i)
  684. // Check if any properties were set for the module
  685. if variableStruct.IsZero() {
  686. // e.g. feature1: {}
  687. continue
  688. }
  689. // Unlike product variables, config variables require a few more
  690. // indirections to extract the struct from the reflect.Value.
  691. if v, ok := maybeExtractConfigVarProp(variableStruct); ok {
  692. variableStruct = v
  693. } else if !v.IsValid() {
  694. // Skip invalid variables which may not used, else leads to panic
  695. continue
  696. }
  697. for j := 0; j < variableStruct.NumField(); j++ {
  698. propertyOrStruct := variableStruct.Field(j)
  699. // propertyOrValueName can either be:
  700. // - A property, like: Asflags, Cflags, Enabled, etc.
  701. // - A soong config string variable's value, like soc_a, soc_b, soc_c in the example above
  702. // - "conditions_default"
  703. propertyOrValueName := variableStruct.Type().Field(j).Name
  704. // If the property wasn't set, no need to pass it along
  705. if propertyOrStruct.IsZero() {
  706. continue
  707. }
  708. if v, ok := maybeExtractConfigVarProp(propertyOrStruct); ok {
  709. // The field is a struct, which is used by:
  710. // 1) soong_config_string_variables
  711. //
  712. // soc_a: {
  713. // cflags: ...,
  714. // }
  715. //
  716. // soc_b: {
  717. // cflags: ...,
  718. // }
  719. //
  720. // 2) conditions_default structs for all soong config variable types.
  721. //
  722. // conditions_default: {
  723. // cflags: ...,
  724. // static_libs: ...
  725. // }
  726. //
  727. // This means that propertyOrValueName is either conditions_default, or a soong
  728. // config string variable's value.
  729. field := v
  730. // Iterate over fields of this struct prop.
  731. for k := 0; k < field.NumField(); k++ {
  732. // For product variables, zero values are irrelevant; however, for soong config variables,
  733. // empty values are relevant because there can also be a conditions default which is not
  734. // applied for empty variables.
  735. if field.Field(k).IsZero() && namespace == "" {
  736. continue
  737. }
  738. propertyName := field.Type().Field(k).Name
  739. if propertyName == "Target" {
  740. productConfigProperties.AddSoongConfigPropertiesFromTargetStruct(namespace, variableName, proptools.PropertyNameForField(propertyOrValueName), field.Field(k))
  741. } else if propertyName == "Arch" || propertyName == "Multilib" {
  742. panic("Arch/Multilib are not currently supported in soong config variable structs")
  743. } else {
  744. productConfigProperties.AddSoongConfigProperty(propertyName, namespace, variableName, proptools.PropertyNameForField(propertyOrValueName), "", field.Field(k).Interface())
  745. }
  746. }
  747. } else if propertyOrStruct.Kind() != reflect.Interface {
  748. // If not an interface, then this is not a conditions_default or
  749. // a struct prop. That is, this is a bool/value config variable.
  750. if propertyOrValueName == "Target" {
  751. productConfigProperties.AddSoongConfigPropertiesFromTargetStruct(namespace, variableName, "", propertyOrStruct)
  752. } else if propertyOrValueName == "Arch" || propertyOrValueName == "Multilib" {
  753. panic("Arch/Multilib are not currently supported in soong config variable structs")
  754. } else {
  755. productConfigProperties.AddSoongConfigProperty(propertyOrValueName, namespace, variableName, "", "", propertyOrStruct.Interface())
  756. }
  757. }
  758. }
  759. }
  760. }
  761. func (productConfigProperties *ProductConfigProperties) AddSoongConfigPropertiesFromTargetStruct(namespace, soongConfigVariableName string, soongConfigVariableValue string, targetStruct reflect.Value) {
  762. // targetStruct will be a struct with fields like "android", "host", "arm", "x86",
  763. // "android_arm", etc. The values of each of those fields will be a regular property struct.
  764. for i := 0; i < targetStruct.NumField(); i++ {
  765. targetFieldName := targetStruct.Type().Field(i).Name
  766. archOrOsSpecificStruct := targetStruct.Field(i)
  767. for j := 0; j < archOrOsSpecificStruct.NumField(); j++ {
  768. property := archOrOsSpecificStruct.Field(j)
  769. // e.g. Asflags, Cflags, Enabled, etc.
  770. propertyName := archOrOsSpecificStruct.Type().Field(j).Name
  771. if targetFieldName == "Android" {
  772. productConfigProperties.AddSoongConfigProperty(propertyName, namespace, soongConfigVariableName, soongConfigVariableValue, "android", property.Interface())
  773. } else if targetFieldName == "Host" {
  774. for _, os := range osTypeList {
  775. if os.Class == Host {
  776. productConfigProperties.AddSoongConfigProperty(propertyName, namespace, soongConfigVariableName, soongConfigVariableValue, os.Name, property.Interface())
  777. }
  778. }
  779. } else {
  780. // One problem with supporting additional fields is that if multiple branches of
  781. // "target" overlap, we don't want them to be in the same select statement (aka
  782. // configuration axis). "android" and "host" are disjoint, so it's ok that we only
  783. // have 2 axes right now. (soongConfigVariables and soongConfigVariablesPlusOs)
  784. panic("TODO: support other target types in soong config variable structs: " + targetFieldName)
  785. }
  786. }
  787. }
  788. }
  789. func VariableMutator(mctx BottomUpMutatorContext) {
  790. var module Module
  791. var ok bool
  792. if module, ok = mctx.Module().(Module); !ok {
  793. return
  794. }
  795. // TODO: depend on config variable, create variants, propagate variants up tree
  796. a := module.base()
  797. if a.variableProperties == nil {
  798. return
  799. }
  800. variableValues := reflect.ValueOf(a.variableProperties).Elem().FieldByName("Product_variables")
  801. productVariables := reflect.ValueOf(mctx.Config().productVariables)
  802. for i := 0; i < variableValues.NumField(); i++ {
  803. variableValue := variableValues.Field(i)
  804. name := variableValues.Type().Field(i).Name
  805. property := "product_variables." + proptools.PropertyNameForField(name)
  806. // Check that the variable was set for the product
  807. val := productVariables.FieldByName(name)
  808. if !val.IsValid() || val.Kind() != reflect.Ptr || val.IsNil() {
  809. continue
  810. }
  811. val = val.Elem()
  812. // For bools, check that the value is true
  813. if val.Kind() == reflect.Bool && val.Bool() == false {
  814. continue
  815. }
  816. // Check if any properties were set for the module
  817. if variableValue.IsZero() {
  818. continue
  819. }
  820. a.setVariableProperties(mctx, property, variableValue, val.Interface())
  821. }
  822. }
  823. func (m *ModuleBase) setVariableProperties(ctx BottomUpMutatorContext,
  824. prefix string, productVariablePropertyValue reflect.Value, variableValue interface{}) {
  825. printfIntoProperties(ctx, prefix, productVariablePropertyValue, variableValue)
  826. err := proptools.AppendMatchingProperties(m.GetProperties(),
  827. productVariablePropertyValue.Addr().Interface(), nil)
  828. if err != nil {
  829. if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
  830. ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
  831. } else {
  832. panic(err)
  833. }
  834. }
  835. }
  836. func printfIntoPropertiesError(ctx BottomUpMutatorContext, prefix string,
  837. productVariablePropertyValue reflect.Value, i int, err error) {
  838. field := productVariablePropertyValue.Type().Field(i).Name
  839. property := prefix + "." + proptools.PropertyNameForField(field)
  840. ctx.PropertyErrorf(property, "%s", err)
  841. }
  842. func printfIntoProperties(ctx BottomUpMutatorContext, prefix string,
  843. productVariablePropertyValue reflect.Value, variableValue interface{}) {
  844. for i := 0; i < productVariablePropertyValue.NumField(); i++ {
  845. propertyValue := productVariablePropertyValue.Field(i)
  846. kind := propertyValue.Kind()
  847. if kind == reflect.Ptr {
  848. if propertyValue.IsNil() {
  849. continue
  850. }
  851. propertyValue = propertyValue.Elem()
  852. }
  853. switch propertyValue.Kind() {
  854. case reflect.String:
  855. err := printfIntoProperty(propertyValue, variableValue)
  856. if err != nil {
  857. printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
  858. }
  859. case reflect.Slice:
  860. for j := 0; j < propertyValue.Len(); j++ {
  861. err := printfIntoProperty(propertyValue.Index(j), variableValue)
  862. if err != nil {
  863. printfIntoPropertiesError(ctx, prefix, productVariablePropertyValue, i, err)
  864. }
  865. }
  866. case reflect.Bool:
  867. // Nothing
  868. case reflect.Struct:
  869. printfIntoProperties(ctx, prefix, propertyValue, variableValue)
  870. default:
  871. panic(fmt.Errorf("unsupported field kind %q", propertyValue.Kind()))
  872. }
  873. }
  874. }
  875. func printfIntoProperty(propertyValue reflect.Value, variableValue interface{}) error {
  876. s := propertyValue.String()
  877. count := strings.Count(s, "%")
  878. if count == 0 {
  879. return nil
  880. }
  881. if count > 1 {
  882. return fmt.Errorf("product variable properties only support a single '%%'")
  883. }
  884. if strings.Contains(s, "%d") {
  885. switch v := variableValue.(type) {
  886. case int:
  887. // Nothing
  888. case bool:
  889. if v {
  890. variableValue = 1
  891. } else {
  892. variableValue = 0
  893. }
  894. default:
  895. return fmt.Errorf("unsupported type %T for %%d", variableValue)
  896. }
  897. } else if strings.Contains(s, "%s") {
  898. switch variableValue.(type) {
  899. case string:
  900. // Nothing
  901. default:
  902. return fmt.Errorf("unsupported type %T for %%s", variableValue)
  903. }
  904. } else {
  905. return fmt.Errorf("unsupported %% in product variable property")
  906. }
  907. propertyValue.Set(reflect.ValueOf(fmt.Sprintf(s, variableValue)))
  908. return nil
  909. }
  910. var variablePropTypeMap OncePer
  911. // sliceToTypeArray takes a slice of property structs and returns a reflection created array containing the
  912. // reflect.Types of each property struct. The result can be used as a key in a map.
  913. func sliceToTypeArray(s []interface{}) interface{} {
  914. // Create an array using reflection whose length is the length of the input slice
  915. ret := reflect.New(reflect.ArrayOf(len(s), reflect.TypeOf(reflect.TypeOf(0)))).Elem()
  916. for i, e := range s {
  917. ret.Index(i).Set(reflect.ValueOf(reflect.TypeOf(e)))
  918. }
  919. return ret.Interface()
  920. }
  921. func initProductVariableModule(m Module) {
  922. base := m.base()
  923. // Allow tests to override the default product variables
  924. if base.variableProperties == nil {
  925. base.variableProperties = defaultProductVariables
  926. }
  927. // Filter the product variables properties to the ones that exist on this module
  928. base.variableProperties = createVariableProperties(m.GetProperties(), base.variableProperties)
  929. if base.variableProperties != nil {
  930. m.AddProperties(base.variableProperties)
  931. }
  932. }
  933. // createVariableProperties takes the list of property structs for a module and returns a property struct that
  934. // contains the product variable properties that exist in the property structs, or nil if there are none. It
  935. // caches the result.
  936. func createVariableProperties(moduleTypeProps []interface{}, productVariables interface{}) interface{} {
  937. // Convert the moduleTypeProps to an array of reflect.Types that can be used as a key in the OncePer.
  938. key := sliceToTypeArray(moduleTypeProps)
  939. // Use the variablePropTypeMap OncePer to cache the result for each set of property struct types.
  940. typ, _ := variablePropTypeMap.Once(NewCustomOnceKey(key), func() interface{} {
  941. // Compute the filtered property struct type.
  942. return createVariablePropertiesType(moduleTypeProps, productVariables)
  943. }).(reflect.Type)
  944. if typ == nil {
  945. return nil
  946. }
  947. // Create a new pointer to a filtered property struct.
  948. return reflect.New(typ).Interface()
  949. }
  950. // createVariablePropertiesType creates a new type that contains only the product variable properties that exist in
  951. // a list of property structs.
  952. func createVariablePropertiesType(moduleTypeProps []interface{}, productVariables interface{}) reflect.Type {
  953. typ, _ := proptools.FilterPropertyStruct(reflect.TypeOf(productVariables),
  954. func(field reflect.StructField, prefix string) (bool, reflect.StructField) {
  955. // Filter function, returns true if the field should be in the resulting struct
  956. if prefix == "" {
  957. // Keep the top level Product_variables field
  958. return true, field
  959. }
  960. _, rest := splitPrefix(prefix)
  961. if rest == "" {
  962. // Keep the 2nd level field (i.e. Product_variables.Eng)
  963. return true, field
  964. }
  965. // Strip off the first 2 levels of the prefix
  966. _, prefix = splitPrefix(rest)
  967. for _, p := range moduleTypeProps {
  968. if fieldExistsByNameRecursive(reflect.TypeOf(p).Elem(), prefix, field.Name) {
  969. // Keep any fields that exist in one of the property structs
  970. return true, field
  971. }
  972. }
  973. return false, field
  974. })
  975. return typ
  976. }
  977. func splitPrefix(prefix string) (first, rest string) {
  978. index := strings.IndexByte(prefix, '.')
  979. if index == -1 {
  980. return prefix, ""
  981. }
  982. return prefix[:index], prefix[index+1:]
  983. }
  984. func fieldExistsByNameRecursive(t reflect.Type, prefix, name string) bool {
  985. if t.Kind() != reflect.Struct {
  986. panic(fmt.Errorf("fieldExistsByNameRecursive can only be called on a reflect.Struct"))
  987. }
  988. if prefix != "" {
  989. split := strings.SplitN(prefix, ".", 2)
  990. firstPrefix := split[0]
  991. rest := ""
  992. if len(split) > 1 {
  993. rest = split[1]
  994. }
  995. f, exists := t.FieldByName(firstPrefix)
  996. if !exists {
  997. return false
  998. }
  999. ft := f.Type
  1000. if ft.Kind() == reflect.Ptr {
  1001. ft = ft.Elem()
  1002. }
  1003. if ft.Kind() != reflect.Struct {
  1004. panic(fmt.Errorf("field %q in %q is not a struct", firstPrefix, t))
  1005. }
  1006. return fieldExistsByNameRecursive(ft, rest, name)
  1007. } else {
  1008. _, exists := t.FieldByName(name)
  1009. return exists
  1010. }
  1011. }