variable.go 42 KB

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