variable.go 41 KB

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