update.go 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386
  1. // Copyright (C) 2019 The Android Open Source Project
  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 sdk
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "reflect"
  20. "sort"
  21. "strings"
  22. "android/soong/apex"
  23. "android/soong/cc"
  24. "github.com/google/blueprint"
  25. "github.com/google/blueprint/proptools"
  26. "android/soong/android"
  27. )
  28. // Environment variables that affect the generated snapshot
  29. // ========================================================
  30. //
  31. // SOONG_SDK_SNAPSHOT_PREFER
  32. // By default every module in the generated snapshot has prefer: false. Building it
  33. // with SOONG_SDK_SNAPSHOT_PREFER=true will force them to use prefer: true.
  34. //
  35. // SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR
  36. // If set this specifies the Soong config var that can be used to control whether the prebuilt
  37. // modules from the generated snapshot or the original source modules. Values must be a colon
  38. // separated pair of strings, the first of which is the Soong config namespace, and the second
  39. // is the name of the variable within that namespace.
  40. //
  41. // The config namespace and var name are used to set the `use_source_config_var` property. That
  42. // in turn will cause the generated prebuilts to use the soong config variable to select whether
  43. // source or the prebuilt is used.
  44. // e.g. If an sdk snapshot is built using:
  45. // m SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR=acme:build_from_source sdkextensions-sdk
  46. // Then the resulting snapshot will include:
  47. // use_source_config_var: {
  48. // config_namespace: "acme",
  49. // var_name: "build_from_source",
  50. // }
  51. //
  52. // Assuming that the config variable is defined in .mk using something like:
  53. // $(call add_soong_config_namespace,acme)
  54. // $(call add_soong_config_var_value,acme,build_from_source,true)
  55. //
  56. // Then when the snapshot is unpacked in the repository it will have the following behavior:
  57. // m droid - will use the sdkextensions-sdk prebuilts if present. Otherwise, it will use the
  58. // sources.
  59. // m SOONG_CONFIG_acme_build_from_source=true droid - will use the sdkextensions-sdk
  60. // sources, if present. Otherwise, it will use the prebuilts.
  61. //
  62. // This is a temporary mechanism to control the prefer flags and will be removed once a more
  63. // maintainable solution has been implemented.
  64. // TODO(b/174997203): Remove when no longer necessary.
  65. //
  66. // SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE
  67. // This allows the target build release (i.e. the release version of the build within which
  68. // the snapshot will be used) of the snapshot to be specified. If unspecified then it defaults
  69. // to the current build release version. Otherwise, it must be the name of one of the build
  70. // releases defined in nameToBuildRelease, e.g. S, T, etc..
  71. //
  72. // The generated snapshot must only be used in the specified target release. If the target
  73. // build release is not the current build release then the generated Android.bp file not be
  74. // checked for compatibility.
  75. //
  76. // e.g. if setting SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE=S will cause the generated snapshot
  77. // to be compatible with S.
  78. //
  79. var pctx = android.NewPackageContext("android/soong/sdk")
  80. var (
  81. repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip",
  82. blueprint.RuleParams{
  83. Command: `${config.Zip2ZipCmd} -i $in -o $out -x META-INF/**/* "**/*:$destdir"`,
  84. CommandDeps: []string{
  85. "${config.Zip2ZipCmd}",
  86. },
  87. },
  88. "destdir")
  89. zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles",
  90. blueprint.RuleParams{
  91. Command: `${config.SoongZipCmd} -C $basedir -r $out.rsp -o $out`,
  92. CommandDeps: []string{
  93. "${config.SoongZipCmd}",
  94. },
  95. Rspfile: "$out.rsp",
  96. RspfileContent: "$in",
  97. },
  98. "basedir")
  99. mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips",
  100. blueprint.RuleParams{
  101. Command: `${config.MergeZipsCmd} -s $out $in`,
  102. CommandDeps: []string{
  103. "${config.MergeZipsCmd}",
  104. },
  105. })
  106. )
  107. const (
  108. soongSdkSnapshotVersionCurrent = "current"
  109. )
  110. type generatedContents struct {
  111. content strings.Builder
  112. indentLevel int
  113. }
  114. // generatedFile abstracts operations for writing contents into a file and emit a build rule
  115. // for the file.
  116. type generatedFile struct {
  117. generatedContents
  118. path android.OutputPath
  119. }
  120. func newGeneratedFile(ctx android.ModuleContext, path ...string) *generatedFile {
  121. return &generatedFile{
  122. path: android.PathForModuleOut(ctx, path...).OutputPath,
  123. }
  124. }
  125. func (gc *generatedContents) Indent() {
  126. gc.indentLevel++
  127. }
  128. func (gc *generatedContents) Dedent() {
  129. gc.indentLevel--
  130. }
  131. // IndentedPrintf will add spaces to indent the line to the appropriate level before printing the
  132. // arguments.
  133. func (gc *generatedContents) IndentedPrintf(format string, args ...interface{}) {
  134. _, _ = fmt.Fprintf(&(gc.content), strings.Repeat(" ", gc.indentLevel)+format, args...)
  135. }
  136. // UnindentedPrintf does not add spaces to indent the line to the appropriate level before printing
  137. // the arguments.
  138. func (gc *generatedContents) UnindentedPrintf(format string, args ...interface{}) {
  139. _, _ = fmt.Fprintf(&(gc.content), format, args...)
  140. }
  141. func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) {
  142. rb := android.NewRuleBuilder(pctx, ctx)
  143. content := gf.content.String()
  144. // ninja consumes newline characters in rspfile_content. Prevent it by
  145. // escaping the backslash in the newline character. The extra backslash
  146. // is removed when the rspfile is written to the actual script file
  147. content = strings.ReplaceAll(content, "\n", "\\n")
  148. rb.Command().
  149. Implicits(implicits).
  150. Text("echo -n").Text(proptools.ShellEscape(content)).
  151. // convert \\n to \n
  152. Text("| sed 's/\\\\n/\\n/g' >").Output(gf.path)
  153. rb.Command().
  154. Text("chmod a+x").Output(gf.path)
  155. rb.Build(gf.path.Base(), "Build "+gf.path.Base())
  156. }
  157. // Collect all the members.
  158. //
  159. // Updates the sdk module with a list of sdkMemberVariantDep instances and details as to which
  160. // multilibs (32/64/both) are used by this sdk variant.
  161. func (s *sdk) collectMembers(ctx android.ModuleContext) {
  162. s.multilibUsages = multilibNone
  163. ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
  164. tag := ctx.OtherModuleDependencyTag(child)
  165. if memberTag, ok := tag.(android.SdkMemberDependencyTag); ok {
  166. memberType := memberTag.SdkMemberType(child)
  167. // If a nil SdkMemberType was returned then this module should not be added to the sdk.
  168. if memberType == nil {
  169. return false
  170. }
  171. // Make sure that the resolved module is allowed in the member list property.
  172. if !memberType.IsInstance(child) {
  173. ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(child), memberType.SdkPropertyName())
  174. }
  175. // Keep track of which multilib variants are used by the sdk.
  176. s.multilibUsages = s.multilibUsages.addArchType(child.Target().Arch.ArchType)
  177. var exportedComponentsInfo android.ExportedComponentsInfo
  178. if ctx.OtherModuleHasProvider(child, android.ExportedComponentsInfoProvider) {
  179. exportedComponentsInfo = ctx.OtherModuleProvider(child, android.ExportedComponentsInfoProvider).(android.ExportedComponentsInfo)
  180. }
  181. var container android.SdkAware
  182. if parent != ctx.Module() {
  183. container = parent.(android.SdkAware)
  184. }
  185. export := memberTag.ExportMember()
  186. s.memberVariantDeps = append(s.memberVariantDeps, sdkMemberVariantDep{
  187. sdkVariant: s,
  188. memberType: memberType,
  189. variant: child.(android.SdkAware),
  190. container: container,
  191. export: export,
  192. exportedComponentsInfo: exportedComponentsInfo,
  193. })
  194. // Recurse down into the member's dependencies as it may have dependencies that need to be
  195. // automatically added to the sdk.
  196. return true
  197. }
  198. return false
  199. })
  200. }
  201. // groupMemberVariantsByMemberThenType groups the member variant dependencies so that all the
  202. // variants of each member are grouped together within an sdkMember instance.
  203. //
  204. // The sdkMember instances are then grouped into slices by member type. Within each such slice the
  205. // sdkMember instances appear in the order they were added as dependencies.
  206. //
  207. // Finally, the member type slices are concatenated together to form a single slice. The order in
  208. // which they are concatenated is the order in which the member types were registered in the
  209. // android.SdkMemberTypesRegistry.
  210. func (s *sdk) groupMemberVariantsByMemberThenType(ctx android.ModuleContext, targetBuildRelease *buildRelease, memberVariantDeps []sdkMemberVariantDep) []*sdkMember {
  211. byType := make(map[android.SdkMemberType][]*sdkMember)
  212. byName := make(map[string]*sdkMember)
  213. for _, memberVariantDep := range memberVariantDeps {
  214. memberType := memberVariantDep.memberType
  215. variant := memberVariantDep.variant
  216. name := ctx.OtherModuleName(variant)
  217. member := byName[name]
  218. if member == nil {
  219. member = &sdkMember{memberType: memberType, name: name}
  220. byName[name] = member
  221. byType[memberType] = append(byType[memberType], member)
  222. } else if member.memberType != memberType {
  223. // validate whether this is the same member type or and overriding member type
  224. if memberType.Overrides(member.memberType) {
  225. member.memberType = memberType
  226. } else if !member.memberType.Overrides(memberType) {
  227. ctx.ModuleErrorf("Incompatible member types %q %q", member.memberType, memberType)
  228. }
  229. }
  230. // Only append new variants to the list. This is needed because a member can be both
  231. // exported by the sdk and also be a transitive sdk member.
  232. member.variants = appendUniqueVariants(member.variants, variant)
  233. }
  234. var members []*sdkMember
  235. for _, memberListProperty := range s.memberTypeListProperties() {
  236. memberType := memberListProperty.memberType
  237. if !isMemberTypeSupportedByTargetBuildRelease(memberType, targetBuildRelease) {
  238. continue
  239. }
  240. membersOfType := byType[memberType]
  241. members = append(members, membersOfType...)
  242. }
  243. return members
  244. }
  245. // isMemberTypeSupportedByTargetBuildRelease returns true if the member type is supported by the
  246. // target build release.
  247. func isMemberTypeSupportedByTargetBuildRelease(memberType android.SdkMemberType, targetBuildRelease *buildRelease) bool {
  248. supportedByTargetBuildRelease := true
  249. supportedBuildReleases := memberType.SupportedBuildReleases()
  250. if supportedBuildReleases == "" {
  251. supportedBuildReleases = "S+"
  252. }
  253. set, err := parseBuildReleaseSet(supportedBuildReleases)
  254. if err != nil {
  255. panic(fmt.Errorf("member type %s has invalid supported build releases %q: %s",
  256. memberType.SdkPropertyName(), supportedBuildReleases, err))
  257. }
  258. if !set.contains(targetBuildRelease) {
  259. supportedByTargetBuildRelease = false
  260. }
  261. return supportedByTargetBuildRelease
  262. }
  263. func appendUniqueVariants(variants []android.SdkAware, newVariant android.SdkAware) []android.SdkAware {
  264. for _, v := range variants {
  265. if v == newVariant {
  266. return variants
  267. }
  268. }
  269. return append(variants, newVariant)
  270. }
  271. // BUILD_NUMBER_FILE is the name of the file in the snapshot zip that will contain the number of
  272. // the build from which the snapshot was produced.
  273. const BUILD_NUMBER_FILE = "snapshot-creation-build-number.txt"
  274. // SDK directory structure
  275. // <sdk_root>/
  276. // Android.bp : definition of a 'sdk' module is here. This is a hand-made one.
  277. // <api_ver>/ : below this directory are all auto-generated
  278. // Android.bp : definition of 'sdk_snapshot' module is here
  279. // aidl/
  280. // frameworks/base/core/..../IFoo.aidl : an exported AIDL file
  281. // java/
  282. // <module_name>.jar : the stub jar for a java library 'module_name'
  283. // include/
  284. // bionic/libc/include/stdlib.h : an exported header file
  285. // include_gen/
  286. // <module_name>/com/android/.../IFoo.h : a generated header file
  287. // <arch>/include/ : arch-specific exported headers
  288. // <arch>/include_gen/ : arch-specific generated headers
  289. // <arch>/lib/
  290. // libFoo.so : a stub library
  291. // buildSnapshot is the main function in this source file. It creates rules to copy
  292. // the contents (header files, stub libraries, etc) into the zip file.
  293. func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) {
  294. // Aggregate all the sdkMemberVariantDep instances from all the sdk variants.
  295. hasLicenses := false
  296. var memberVariantDeps []sdkMemberVariantDep
  297. for _, sdkVariant := range sdkVariants {
  298. memberVariantDeps = append(memberVariantDeps, sdkVariant.memberVariantDeps...)
  299. }
  300. // Filter out any sdkMemberVariantDep that is a component of another.
  301. memberVariantDeps = filterOutComponents(ctx, memberVariantDeps)
  302. // Record the names of all the members, both explicitly specified and implicitly
  303. // included.
  304. allMembersByName := make(map[string]struct{})
  305. exportedMembersByName := make(map[string]struct{})
  306. addMember := func(name string, export bool) {
  307. allMembersByName[name] = struct{}{}
  308. if export {
  309. exportedMembersByName[name] = struct{}{}
  310. }
  311. }
  312. for _, memberVariantDep := range memberVariantDeps {
  313. name := memberVariantDep.variant.Name()
  314. export := memberVariantDep.export
  315. addMember(name, export)
  316. // Add any components provided by the module.
  317. for _, component := range memberVariantDep.exportedComponentsInfo.Components {
  318. addMember(component, export)
  319. }
  320. if memberVariantDep.memberType == android.LicenseModuleSdkMemberType {
  321. hasLicenses = true
  322. }
  323. }
  324. snapshotDir := android.PathForModuleOut(ctx, "snapshot")
  325. bp := newGeneratedFile(ctx, "snapshot", "Android.bp")
  326. bpFile := &bpFile{
  327. modules: make(map[string]*bpModule),
  328. }
  329. config := ctx.Config()
  330. // Always add -current to the end
  331. snapshotFileSuffix := "-current"
  332. targetBuildReleaseEnv := config.GetenvWithDefault("SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE", buildReleaseCurrent.name)
  333. targetBuildRelease, err := nameToRelease(targetBuildReleaseEnv)
  334. if err != nil {
  335. ctx.ModuleErrorf("invalid SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE: %s", err)
  336. targetBuildRelease = buildReleaseCurrent
  337. }
  338. builder := &snapshotBuilder{
  339. ctx: ctx,
  340. sdk: s,
  341. snapshotDir: snapshotDir.OutputPath,
  342. copies: make(map[string]string),
  343. filesToZip: []android.Path{bp.path},
  344. bpFile: bpFile,
  345. prebuiltModules: make(map[string]*bpModule),
  346. allMembersByName: allMembersByName,
  347. exportedMembersByName: exportedMembersByName,
  348. targetBuildRelease: targetBuildRelease,
  349. }
  350. s.builderForTests = builder
  351. // If the sdk snapshot includes any license modules then add a package module which has a
  352. // default_applicable_licenses property. That will prevent the LSC license process from updating
  353. // the generated Android.bp file to add a package module that includes all licenses used by all
  354. // the modules in that package. That would be unnecessary as every module in the sdk should have
  355. // their own licenses property specified.
  356. if hasLicenses {
  357. pkg := bpFile.newModule("package")
  358. property := "default_applicable_licenses"
  359. pkg.AddCommentForProperty(property, `
  360. A default list here prevents the license LSC from adding its own list which would
  361. be unnecessary as every module in the sdk already has its own licenses property.
  362. `)
  363. pkg.AddProperty(property, []string{"Android-Apache-2.0"})
  364. bpFile.AddModule(pkg)
  365. }
  366. // Group the variants for each member module together and then group the members of each member
  367. // type together.
  368. members := s.groupMemberVariantsByMemberThenType(ctx, targetBuildRelease, memberVariantDeps)
  369. // Create the prebuilt modules for each of the member modules.
  370. traits := s.gatherTraits()
  371. for _, member := range members {
  372. memberType := member.memberType
  373. if !memberType.ArePrebuiltsRequired() {
  374. continue
  375. }
  376. name := member.name
  377. requiredTraits := traits[name]
  378. if requiredTraits == nil {
  379. requiredTraits = android.EmptySdkMemberTraitSet()
  380. }
  381. // Create the snapshot for the member.
  382. memberCtx := &memberContext{ctx, builder, memberType, name, requiredTraits}
  383. prebuiltModule := memberType.AddPrebuiltModule(memberCtx, member)
  384. s.createMemberSnapshot(memberCtx, member, prebuiltModule.(*bpModule))
  385. }
  386. // Create a transformer that will transform a module by replacing any references
  387. // to internal members with a unique module name and setting prefer: false.
  388. snapshotTransformer := snapshotTransformation{
  389. builder: builder,
  390. }
  391. for _, module := range builder.prebuiltOrder {
  392. // Prune any empty property sets.
  393. module = module.transform(pruneEmptySetTransformer{})
  394. // Transform the module module to make it suitable for use in the snapshot.
  395. module.transform(snapshotTransformer)
  396. bpFile.AddModule(module)
  397. }
  398. // generate Android.bp
  399. bp = newGeneratedFile(ctx, "snapshot", "Android.bp")
  400. generateBpContents(&bp.generatedContents, bpFile)
  401. contents := bp.content.String()
  402. // If the snapshot is being generated for the current build release then check the syntax to make
  403. // sure that it is compatible.
  404. if targetBuildRelease == buildReleaseCurrent {
  405. syntaxCheckSnapshotBpFile(ctx, contents)
  406. }
  407. bp.build(pctx, ctx, nil)
  408. // Copy the build number file into the snapshot.
  409. builder.CopyToSnapshot(ctx.Config().BuildNumberFile(ctx), BUILD_NUMBER_FILE)
  410. filesToZip := android.SortedUniquePaths(builder.filesToZip)
  411. // zip them all
  412. zipPath := fmt.Sprintf("%s%s.zip", ctx.ModuleName(), snapshotFileSuffix)
  413. outputZipFile := android.PathForModuleOut(ctx, zipPath).OutputPath
  414. outputDesc := "Building snapshot for " + ctx.ModuleName()
  415. // If there are no zips to merge then generate the output zip directly.
  416. // Otherwise, generate an intermediate zip file into which other zips can be
  417. // merged.
  418. var zipFile android.OutputPath
  419. var desc string
  420. if len(builder.zipsToMerge) == 0 {
  421. zipFile = outputZipFile
  422. desc = outputDesc
  423. } else {
  424. intermediatePath := fmt.Sprintf("%s%s.unmerged.zip", ctx.ModuleName(), snapshotFileSuffix)
  425. zipFile = android.PathForModuleOut(ctx, intermediatePath).OutputPath
  426. desc = "Building intermediate snapshot for " + ctx.ModuleName()
  427. }
  428. ctx.Build(pctx, android.BuildParams{
  429. Description: desc,
  430. Rule: zipFiles,
  431. Inputs: filesToZip,
  432. Output: zipFile,
  433. Args: map[string]string{
  434. "basedir": builder.snapshotDir.String(),
  435. },
  436. })
  437. if len(builder.zipsToMerge) != 0 {
  438. ctx.Build(pctx, android.BuildParams{
  439. Description: outputDesc,
  440. Rule: mergeZips,
  441. Input: zipFile,
  442. Inputs: android.SortedUniquePaths(builder.zipsToMerge),
  443. Output: outputZipFile,
  444. })
  445. }
  446. modules := s.generateInfoData(ctx, memberVariantDeps)
  447. // Output the modules information as pretty printed JSON.
  448. info := newGeneratedFile(ctx, fmt.Sprintf("%s%s.info", ctx.ModuleName(), snapshotFileSuffix))
  449. output, err := json.MarshalIndent(modules, "", " ")
  450. if err != nil {
  451. ctx.ModuleErrorf("error generating %q: %s", info, err)
  452. }
  453. builder.infoContents = string(output)
  454. info.generatedContents.UnindentedPrintf("%s", output)
  455. info.build(pctx, ctx, nil)
  456. infoPath := info.path
  457. installedInfo := ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), infoPath.Base(), infoPath)
  458. s.infoFile = android.OptionalPathForPath(installedInfo)
  459. // Install the zip, making sure that the info file has been installed as well.
  460. installedZip := ctx.InstallFile(android.PathForMainlineSdksInstall(ctx), outputZipFile.Base(), outputZipFile, installedInfo)
  461. s.snapshotFile = android.OptionalPathForPath(installedZip)
  462. }
  463. type moduleInfo struct {
  464. // The type of the module, e.g. java_sdk_library
  465. moduleType string
  466. // The name of the module.
  467. name string
  468. // A list of additional dependencies of the module.
  469. deps []string
  470. // Additional member specific properties.
  471. // These will be added into the generated JSON alongside the above properties.
  472. memberSpecific map[string]interface{}
  473. }
  474. func (m *moduleInfo) MarshalJSON() ([]byte, error) {
  475. buffer := bytes.Buffer{}
  476. separator := ""
  477. writeObjectPair := func(key string, value interface{}) {
  478. buffer.WriteString(fmt.Sprintf("%s%q: ", separator, key))
  479. b, err := json.Marshal(value)
  480. if err != nil {
  481. panic(err)
  482. }
  483. buffer.Write(b)
  484. separator = ","
  485. }
  486. buffer.WriteString("{")
  487. writeObjectPair("@type", m.moduleType)
  488. writeObjectPair("@name", m.name)
  489. if m.deps != nil {
  490. writeObjectPair("@deps", m.deps)
  491. }
  492. for _, k := range android.SortedStringKeys(m.memberSpecific) {
  493. v := m.memberSpecific[k]
  494. writeObjectPair(k, v)
  495. }
  496. buffer.WriteString("}")
  497. return buffer.Bytes(), nil
  498. }
  499. var _ json.Marshaler = (*moduleInfo)(nil)
  500. // generateInfoData creates a list of moduleInfo structures that will be marshalled into JSON.
  501. func (s *sdk) generateInfoData(ctx android.ModuleContext, memberVariantDeps []sdkMemberVariantDep) interface{} {
  502. modules := []*moduleInfo{}
  503. sdkInfo := moduleInfo{
  504. moduleType: "sdk",
  505. name: ctx.ModuleName(),
  506. memberSpecific: map[string]interface{}{},
  507. }
  508. modules = append(modules, &sdkInfo)
  509. name2Info := map[string]*moduleInfo{}
  510. getModuleInfo := func(module android.Module) *moduleInfo {
  511. name := module.Name()
  512. info := name2Info[name]
  513. if info == nil {
  514. moduleType := ctx.OtherModuleType(module)
  515. // Remove any suffix added when creating modules dynamically.
  516. moduleType = strings.Split(moduleType, "__")[0]
  517. info = &moduleInfo{
  518. moduleType: moduleType,
  519. name: name,
  520. }
  521. additionalSdkInfo := ctx.OtherModuleProvider(module, android.AdditionalSdkInfoProvider).(android.AdditionalSdkInfo)
  522. info.memberSpecific = additionalSdkInfo.Properties
  523. name2Info[name] = info
  524. }
  525. return info
  526. }
  527. for _, memberVariantDep := range memberVariantDeps {
  528. propertyName := memberVariantDep.memberType.SdkPropertyName()
  529. var list []string
  530. if v, ok := sdkInfo.memberSpecific[propertyName]; ok {
  531. list = v.([]string)
  532. }
  533. memberName := memberVariantDep.variant.Name()
  534. list = append(list, memberName)
  535. sdkInfo.memberSpecific[propertyName] = android.SortedUniqueStrings(list)
  536. if memberVariantDep.container != nil {
  537. containerInfo := getModuleInfo(memberVariantDep.container)
  538. containerInfo.deps = android.SortedUniqueStrings(append(containerInfo.deps, memberName))
  539. }
  540. // Make sure that the module info is created for each module.
  541. getModuleInfo(memberVariantDep.variant)
  542. }
  543. for _, memberName := range android.SortedStringKeys(name2Info) {
  544. info := name2Info[memberName]
  545. modules = append(modules, info)
  546. }
  547. return modules
  548. }
  549. // filterOutComponents removes any item from the deps list that is a component of another item in
  550. // the deps list, e.g. if the deps list contains "foo" and "foo.stubs" which is component of "foo"
  551. // then it will remove "foo.stubs" from the deps.
  552. func filterOutComponents(ctx android.ModuleContext, deps []sdkMemberVariantDep) []sdkMemberVariantDep {
  553. // Collate the set of components that all the modules added to the sdk provide.
  554. components := map[string]*sdkMemberVariantDep{}
  555. for i := range deps {
  556. dep := &deps[i]
  557. for _, c := range dep.exportedComponentsInfo.Components {
  558. components[c] = dep
  559. }
  560. }
  561. // If no module provides components then return the input deps unfiltered.
  562. if len(components) == 0 {
  563. return deps
  564. }
  565. filtered := make([]sdkMemberVariantDep, 0, len(deps))
  566. for _, dep := range deps {
  567. name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(dep.variant))
  568. if owner, ok := components[name]; ok {
  569. // This is a component of another module that is a member of the sdk.
  570. // If the component is exported but the owning module is not then the configuration is not
  571. // supported.
  572. if dep.export && !owner.export {
  573. ctx.ModuleErrorf("Module %s is internal to the SDK but provides component %s which is used outside the SDK")
  574. continue
  575. }
  576. // This module must not be added to the list of members of the sdk as that would result in a
  577. // duplicate module in the sdk snapshot.
  578. continue
  579. }
  580. filtered = append(filtered, dep)
  581. }
  582. return filtered
  583. }
  584. // Check the syntax of the generated Android.bp file contents and if they are
  585. // invalid then log an error with the contents (tagged with line numbers) and the
  586. // errors that were found so that it is easy to see where the problem lies.
  587. func syntaxCheckSnapshotBpFile(ctx android.ModuleContext, contents string) {
  588. errs := android.CheckBlueprintSyntax(ctx, "Android.bp", contents)
  589. if len(errs) != 0 {
  590. message := &strings.Builder{}
  591. _, _ = fmt.Fprint(message, `errors in generated Android.bp snapshot:
  592. Generated Android.bp contents
  593. ========================================================================
  594. `)
  595. for i, line := range strings.Split(contents, "\n") {
  596. _, _ = fmt.Fprintf(message, "%6d: %s\n", i+1, line)
  597. }
  598. _, _ = fmt.Fprint(message, `
  599. ========================================================================
  600. Errors found:
  601. `)
  602. for _, err := range errs {
  603. _, _ = fmt.Fprintf(message, "%s\n", err.Error())
  604. }
  605. ctx.ModuleErrorf("%s", message.String())
  606. }
  607. }
  608. func extractCommonProperties(ctx android.ModuleContext, extractor *commonValueExtractor, commonProperties interface{}, inputPropertiesSlice interface{}) {
  609. err := extractor.extractCommonProperties(commonProperties, inputPropertiesSlice)
  610. if err != nil {
  611. ctx.ModuleErrorf("error extracting common properties: %s", err)
  612. }
  613. }
  614. // snapshotModuleStaticProperties contains snapshot static (i.e. not dynamically generated) properties.
  615. type snapshotModuleStaticProperties struct {
  616. Compile_multilib string `android:"arch_variant"`
  617. }
  618. // combinedSnapshotModuleProperties are the properties that are associated with the snapshot module.
  619. type combinedSnapshotModuleProperties struct {
  620. // The sdk variant from which this information was collected.
  621. sdkVariant *sdk
  622. // Static snapshot module properties.
  623. staticProperties *snapshotModuleStaticProperties
  624. // The dynamically generated member list properties.
  625. dynamicProperties interface{}
  626. }
  627. // collateSnapshotModuleInfo collates all the snapshot module info from supplied sdk variants.
  628. func (s *sdk) collateSnapshotModuleInfo(ctx android.BaseModuleContext, sdkVariants []*sdk, memberVariantDeps []sdkMemberVariantDep) []*combinedSnapshotModuleProperties {
  629. sdkVariantToCombinedProperties := map[*sdk]*combinedSnapshotModuleProperties{}
  630. var list []*combinedSnapshotModuleProperties
  631. for _, sdkVariant := range sdkVariants {
  632. staticProperties := &snapshotModuleStaticProperties{
  633. Compile_multilib: sdkVariant.multilibUsages.String(),
  634. }
  635. dynamicProperties := s.dynamicSdkMemberTypes.createMemberTypeListProperties()
  636. combinedProperties := &combinedSnapshotModuleProperties{
  637. sdkVariant: sdkVariant,
  638. staticProperties: staticProperties,
  639. dynamicProperties: dynamicProperties,
  640. }
  641. sdkVariantToCombinedProperties[sdkVariant] = combinedProperties
  642. list = append(list, combinedProperties)
  643. }
  644. for _, memberVariantDep := range memberVariantDeps {
  645. // If the member dependency is internal then do not add the dependency to the snapshot member
  646. // list properties.
  647. if !memberVariantDep.export {
  648. continue
  649. }
  650. combined := sdkVariantToCombinedProperties[memberVariantDep.sdkVariant]
  651. memberListProperty := s.memberTypeListProperty(memberVariantDep.memberType)
  652. memberName := ctx.OtherModuleName(memberVariantDep.variant)
  653. if memberListProperty.getter == nil {
  654. continue
  655. }
  656. // Append the member to the appropriate list, if it is not already present in the list.
  657. memberList := memberListProperty.getter(combined.dynamicProperties)
  658. if !android.InList(memberName, memberList) {
  659. memberList = append(memberList, memberName)
  660. }
  661. memberListProperty.setter(combined.dynamicProperties, memberList)
  662. }
  663. return list
  664. }
  665. func (s *sdk) optimizeSnapshotModuleProperties(ctx android.ModuleContext, list []*combinedSnapshotModuleProperties) *combinedSnapshotModuleProperties {
  666. // Extract the dynamic properties and add them to a list of propertiesContainer.
  667. propertyContainers := []propertiesContainer{}
  668. for _, i := range list {
  669. propertyContainers = append(propertyContainers, sdkVariantPropertiesContainer{
  670. sdkVariant: i.sdkVariant,
  671. properties: i.dynamicProperties,
  672. })
  673. }
  674. // Extract the common members, removing them from the original properties.
  675. commonDynamicProperties := s.dynamicSdkMemberTypes.createMemberTypeListProperties()
  676. extractor := newCommonValueExtractor(commonDynamicProperties)
  677. extractCommonProperties(ctx, extractor, commonDynamicProperties, propertyContainers)
  678. // Extract the static properties and add them to a list of propertiesContainer.
  679. propertyContainers = []propertiesContainer{}
  680. for _, i := range list {
  681. propertyContainers = append(propertyContainers, sdkVariantPropertiesContainer{
  682. sdkVariant: i.sdkVariant,
  683. properties: i.staticProperties,
  684. })
  685. }
  686. commonStaticProperties := &snapshotModuleStaticProperties{}
  687. extractor = newCommonValueExtractor(commonStaticProperties)
  688. extractCommonProperties(ctx, extractor, &commonStaticProperties, propertyContainers)
  689. return &combinedSnapshotModuleProperties{
  690. sdkVariant: nil,
  691. staticProperties: commonStaticProperties,
  692. dynamicProperties: commonDynamicProperties,
  693. }
  694. }
  695. type propertyTag struct {
  696. name string
  697. }
  698. var _ android.BpPropertyTag = propertyTag{}
  699. // BpPropertyTag instances to add to a property that contains references to other sdk members.
  700. //
  701. // These will ensure that the referenced modules are available, if required.
  702. var requiredSdkMemberReferencePropertyTag = propertyTag{"requiredSdkMemberReferencePropertyTag"}
  703. var optionalSdkMemberReferencePropertyTag = propertyTag{"optionalSdkMemberReferencePropertyTag"}
  704. type snapshotTransformation struct {
  705. identityTransformation
  706. builder *snapshotBuilder
  707. }
  708. func (t snapshotTransformation) transformModule(module *bpModule) *bpModule {
  709. // If the module is an internal member then use a unique name for it.
  710. name := module.Name()
  711. module.setProperty("name", t.builder.snapshotSdkMemberName(name, true))
  712. return module
  713. }
  714. func (t snapshotTransformation) transformProperty(_ string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
  715. if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag {
  716. required := tag == requiredSdkMemberReferencePropertyTag
  717. return t.builder.snapshotSdkMemberNames(value.([]string), required), tag
  718. } else {
  719. return value, tag
  720. }
  721. }
  722. type pruneEmptySetTransformer struct {
  723. identityTransformation
  724. }
  725. var _ bpTransformer = (*pruneEmptySetTransformer)(nil)
  726. func (t pruneEmptySetTransformer) transformPropertySetAfterContents(_ string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
  727. if len(propertySet.properties) == 0 {
  728. return nil, nil
  729. } else {
  730. return propertySet, tag
  731. }
  732. }
  733. func generateBpContents(contents *generatedContents, bpFile *bpFile) {
  734. contents.IndentedPrintf("// This is auto-generated. DO NOT EDIT.\n")
  735. for _, bpModule := range bpFile.order {
  736. contents.IndentedPrintf("\n")
  737. contents.IndentedPrintf("%s {\n", bpModule.moduleType)
  738. outputPropertySet(contents, bpModule.bpPropertySet)
  739. contents.IndentedPrintf("}\n")
  740. }
  741. }
  742. func outputPropertySet(contents *generatedContents, set *bpPropertySet) {
  743. contents.Indent()
  744. addComment := func(name string) {
  745. if text, ok := set.comments[name]; ok {
  746. for _, line := range strings.Split(text, "\n") {
  747. contents.IndentedPrintf("// %s\n", line)
  748. }
  749. }
  750. }
  751. // Output the properties first, followed by the nested sets. This ensures a
  752. // consistent output irrespective of whether property sets are created before
  753. // or after the properties. This simplifies the creation of the module.
  754. for _, name := range set.order {
  755. value := set.getValue(name)
  756. // Do not write property sets in the properties phase.
  757. if _, ok := value.(*bpPropertySet); ok {
  758. continue
  759. }
  760. addComment(name)
  761. reflectValue := reflect.ValueOf(value)
  762. outputNamedValue(contents, name, reflectValue)
  763. }
  764. for _, name := range set.order {
  765. value := set.getValue(name)
  766. // Only write property sets in the sets phase.
  767. switch v := value.(type) {
  768. case *bpPropertySet:
  769. addComment(name)
  770. contents.IndentedPrintf("%s: {\n", name)
  771. outputPropertySet(contents, v)
  772. contents.IndentedPrintf("},\n")
  773. }
  774. }
  775. contents.Dedent()
  776. }
  777. // outputNamedValue outputs a value that has an associated name. The name will be indented, followed
  778. // by the value and then followed by a , and a newline.
  779. func outputNamedValue(contents *generatedContents, name string, value reflect.Value) {
  780. contents.IndentedPrintf("%s: ", name)
  781. outputUnnamedValue(contents, value)
  782. contents.UnindentedPrintf(",\n")
  783. }
  784. // outputUnnamedValue outputs a single value. The value is not indented and is not followed by
  785. // either a , or a newline. With multi-line values, e.g. slices, all but the first line will be
  786. // indented and all but the last line will end with a newline.
  787. func outputUnnamedValue(contents *generatedContents, value reflect.Value) {
  788. valueType := value.Type()
  789. switch valueType.Kind() {
  790. case reflect.Bool:
  791. contents.UnindentedPrintf("%t", value.Bool())
  792. case reflect.String:
  793. contents.UnindentedPrintf("%q", value)
  794. case reflect.Ptr:
  795. outputUnnamedValue(contents, value.Elem())
  796. case reflect.Slice:
  797. length := value.Len()
  798. if length == 0 {
  799. contents.UnindentedPrintf("[]")
  800. } else {
  801. firstValue := value.Index(0)
  802. if length == 1 && !multiLineValue(firstValue) {
  803. contents.UnindentedPrintf("[")
  804. outputUnnamedValue(contents, firstValue)
  805. contents.UnindentedPrintf("]")
  806. } else {
  807. contents.UnindentedPrintf("[\n")
  808. contents.Indent()
  809. for i := 0; i < length; i++ {
  810. itemValue := value.Index(i)
  811. contents.IndentedPrintf("")
  812. outputUnnamedValue(contents, itemValue)
  813. contents.UnindentedPrintf(",\n")
  814. }
  815. contents.Dedent()
  816. contents.IndentedPrintf("]")
  817. }
  818. }
  819. case reflect.Struct:
  820. // Avoid unlimited recursion by requiring every structure to implement android.BpPrintable.
  821. v := value.Interface()
  822. if _, ok := v.(android.BpPrintable); !ok {
  823. panic(fmt.Errorf("property value %#v of type %T does not implement android.BpPrintable", v, v))
  824. }
  825. contents.UnindentedPrintf("{\n")
  826. contents.Indent()
  827. for f := 0; f < valueType.NumField(); f++ {
  828. fieldType := valueType.Field(f)
  829. if fieldType.Anonymous {
  830. continue
  831. }
  832. fieldValue := value.Field(f)
  833. fieldName := fieldType.Name
  834. propertyName := proptools.PropertyNameForField(fieldName)
  835. outputNamedValue(contents, propertyName, fieldValue)
  836. }
  837. contents.Dedent()
  838. contents.IndentedPrintf("}")
  839. default:
  840. panic(fmt.Errorf("Unknown type: %T of value %#v", value, value))
  841. }
  842. }
  843. // multiLineValue returns true if the supplied value may require multiple lines in the output.
  844. func multiLineValue(value reflect.Value) bool {
  845. kind := value.Kind()
  846. return kind == reflect.Slice || kind == reflect.Struct
  847. }
  848. func (s *sdk) GetAndroidBpContentsForTests() string {
  849. contents := &generatedContents{}
  850. generateBpContents(contents, s.builderForTests.bpFile)
  851. return contents.content.String()
  852. }
  853. func (s *sdk) GetInfoContentsForTests() string {
  854. return s.builderForTests.infoContents
  855. }
  856. type snapshotBuilder struct {
  857. ctx android.ModuleContext
  858. sdk *sdk
  859. snapshotDir android.OutputPath
  860. bpFile *bpFile
  861. // Map from destination to source of each copy - used to eliminate duplicates and
  862. // detect conflicts.
  863. copies map[string]string
  864. filesToZip android.Paths
  865. zipsToMerge android.Paths
  866. // The path to an empty file.
  867. emptyFile android.WritablePath
  868. prebuiltModules map[string]*bpModule
  869. prebuiltOrder []*bpModule
  870. // The set of all members by name.
  871. allMembersByName map[string]struct{}
  872. // The set of exported members by name.
  873. exportedMembersByName map[string]struct{}
  874. // The target build release for which the snapshot is to be generated.
  875. targetBuildRelease *buildRelease
  876. // The contents of the .info file that describes the sdk contents.
  877. infoContents string
  878. }
  879. func (s *snapshotBuilder) CopyToSnapshot(src android.Path, dest string) {
  880. if existing, ok := s.copies[dest]; ok {
  881. if existing != src.String() {
  882. s.ctx.ModuleErrorf("conflicting copy, %s copied from both %s and %s", dest, existing, src)
  883. return
  884. }
  885. } else {
  886. path := s.snapshotDir.Join(s.ctx, dest)
  887. s.ctx.Build(pctx, android.BuildParams{
  888. Rule: android.Cp,
  889. Input: src,
  890. Output: path,
  891. })
  892. s.filesToZip = append(s.filesToZip, path)
  893. s.copies[dest] = src.String()
  894. }
  895. }
  896. func (s *snapshotBuilder) UnzipToSnapshot(zipPath android.Path, destDir string) {
  897. ctx := s.ctx
  898. // Repackage the zip file so that the entries are in the destDir directory.
  899. // This will allow the zip file to be merged into the snapshot.
  900. tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
  901. ctx.Build(pctx, android.BuildParams{
  902. Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
  903. Rule: repackageZip,
  904. Input: zipPath,
  905. Output: tmpZipPath,
  906. Args: map[string]string{
  907. "destdir": destDir,
  908. },
  909. })
  910. // Add the repackaged zip file to the files to merge.
  911. s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)
  912. }
  913. func (s *snapshotBuilder) EmptyFile() android.Path {
  914. if s.emptyFile == nil {
  915. ctx := s.ctx
  916. s.emptyFile = android.PathForModuleOut(ctx, "empty")
  917. s.ctx.Build(pctx, android.BuildParams{
  918. Rule: android.Touch,
  919. Output: s.emptyFile,
  920. })
  921. }
  922. return s.emptyFile
  923. }
  924. func (s *snapshotBuilder) AddPrebuiltModule(member android.SdkMember, moduleType string) android.BpModule {
  925. name := member.Name()
  926. if s.prebuiltModules[name] != nil {
  927. panic(fmt.Sprintf("Duplicate module detected, module %s has already been added", name))
  928. }
  929. m := s.bpFile.newModule(moduleType)
  930. m.AddProperty("name", name)
  931. variant := member.Variants()[0]
  932. if s.isInternalMember(name) {
  933. // An internal member is only referenced from the sdk snapshot which is in the
  934. // same package so can be marked as private.
  935. m.AddProperty("visibility", []string{"//visibility:private"})
  936. } else {
  937. // Extract visibility information from a member variant. All variants have the same
  938. // visibility so it doesn't matter which one is used.
  939. visibilityRules := android.EffectiveVisibilityRules(s.ctx, variant)
  940. // Add any additional visibility rules needed for the prebuilts to reference each other.
  941. err := visibilityRules.Widen(s.sdk.properties.Prebuilt_visibility)
  942. if err != nil {
  943. s.ctx.PropertyErrorf("prebuilt_visibility", "%s", err)
  944. }
  945. visibility := visibilityRules.Strings()
  946. if len(visibility) != 0 {
  947. m.AddProperty("visibility", visibility)
  948. }
  949. }
  950. // Where available copy apex_available properties from the member.
  951. if apexAware, ok := variant.(interface{ ApexAvailable() []string }); ok {
  952. apexAvailable := apexAware.ApexAvailable()
  953. if len(apexAvailable) == 0 {
  954. // //apex_available:platform is the default.
  955. apexAvailable = []string{android.AvailableToPlatform}
  956. }
  957. // Add in any baseline apex available settings.
  958. apexAvailable = append(apexAvailable, apex.BaselineApexAvailable(member.Name())...)
  959. // Remove duplicates and sort.
  960. apexAvailable = android.FirstUniqueStrings(apexAvailable)
  961. sort.Strings(apexAvailable)
  962. m.AddProperty("apex_available", apexAvailable)
  963. }
  964. // The licenses are the same for all variants.
  965. mctx := s.ctx
  966. licenseInfo := mctx.OtherModuleProvider(variant, android.LicenseInfoProvider).(android.LicenseInfo)
  967. if len(licenseInfo.Licenses) > 0 {
  968. m.AddPropertyWithTag("licenses", licenseInfo.Licenses, s.OptionalSdkMemberReferencePropertyTag())
  969. }
  970. deviceSupported := false
  971. hostSupported := false
  972. for _, variant := range member.Variants() {
  973. osClass := variant.Target().Os.Class
  974. if osClass == android.Host {
  975. hostSupported = true
  976. } else if osClass == android.Device {
  977. deviceSupported = true
  978. }
  979. }
  980. addHostDeviceSupportedProperties(deviceSupported, hostSupported, m)
  981. s.prebuiltModules[name] = m
  982. s.prebuiltOrder = append(s.prebuiltOrder, m)
  983. return m
  984. }
  985. func addHostDeviceSupportedProperties(deviceSupported bool, hostSupported bool, bpModule *bpModule) {
  986. // If neither device or host is supported then this module does not support either so will not
  987. // recognize the properties.
  988. if !deviceSupported && !hostSupported {
  989. return
  990. }
  991. if !deviceSupported {
  992. bpModule.AddProperty("device_supported", false)
  993. }
  994. if hostSupported {
  995. bpModule.AddProperty("host_supported", true)
  996. }
  997. }
  998. func (s *snapshotBuilder) SdkMemberReferencePropertyTag(required bool) android.BpPropertyTag {
  999. if required {
  1000. return requiredSdkMemberReferencePropertyTag
  1001. } else {
  1002. return optionalSdkMemberReferencePropertyTag
  1003. }
  1004. }
  1005. func (s *snapshotBuilder) OptionalSdkMemberReferencePropertyTag() android.BpPropertyTag {
  1006. return optionalSdkMemberReferencePropertyTag
  1007. }
  1008. // Get a name for sdk snapshot member. If the member is private then generate a snapshot specific
  1009. // name. As part of the processing this checks to make sure that any required members are part of
  1010. // the snapshot.
  1011. func (s *snapshotBuilder) snapshotSdkMemberName(name string, required bool) string {
  1012. if _, ok := s.allMembersByName[name]; !ok {
  1013. if required {
  1014. s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", name)
  1015. }
  1016. return name
  1017. }
  1018. if s.isInternalMember(name) {
  1019. return s.ctx.ModuleName() + "_" + name
  1020. } else {
  1021. return name
  1022. }
  1023. }
  1024. func (s *snapshotBuilder) snapshotSdkMemberNames(members []string, required bool) []string {
  1025. var references []string = nil
  1026. for _, m := range members {
  1027. references = append(references, s.snapshotSdkMemberName(m, required))
  1028. }
  1029. return references
  1030. }
  1031. func (s *snapshotBuilder) isInternalMember(memberName string) bool {
  1032. _, ok := s.exportedMembersByName[memberName]
  1033. return !ok
  1034. }
  1035. // Add the properties from the given SdkMemberProperties to the blueprint
  1036. // property set. This handles common properties in SdkMemberPropertiesBase and
  1037. // calls the member-specific AddToPropertySet for the rest.
  1038. func addSdkMemberPropertiesToSet(ctx *memberContext, memberProperties android.SdkMemberProperties, targetPropertySet android.BpPropertySet) {
  1039. if memberProperties.Base().Compile_multilib != "" {
  1040. targetPropertySet.AddProperty("compile_multilib", memberProperties.Base().Compile_multilib)
  1041. }
  1042. memberProperties.AddToPropertySet(ctx, targetPropertySet)
  1043. }
  1044. // sdkMemberVariantDep represents a dependency from an sdk variant onto a member variant.
  1045. type sdkMemberVariantDep struct {
  1046. // The sdk variant that depends (possibly indirectly) on the member variant.
  1047. sdkVariant *sdk
  1048. // The type of sdk member the variant is to be treated as.
  1049. memberType android.SdkMemberType
  1050. // The variant that is added to the sdk.
  1051. variant android.SdkAware
  1052. // The optional container of this member, i.e. the module that is depended upon by the sdk
  1053. // (possibly transitively) and whose dependency on this module is why it was added to the sdk.
  1054. // Is nil if this a direct dependency of the sdk.
  1055. container android.SdkAware
  1056. // True if the member should be exported, i.e. accessible, from outside the sdk.
  1057. export bool
  1058. // The names of additional component modules provided by the variant.
  1059. exportedComponentsInfo android.ExportedComponentsInfo
  1060. }
  1061. var _ android.SdkMember = (*sdkMember)(nil)
  1062. // sdkMember groups all the variants of a specific member module together along with the name of the
  1063. // module and the member type. This is used to generate the prebuilt modules for a specific member.
  1064. type sdkMember struct {
  1065. memberType android.SdkMemberType
  1066. name string
  1067. variants []android.SdkAware
  1068. }
  1069. func (m *sdkMember) Name() string {
  1070. return m.name
  1071. }
  1072. func (m *sdkMember) Variants() []android.SdkAware {
  1073. return m.variants
  1074. }
  1075. // Track usages of multilib variants.
  1076. type multilibUsage int
  1077. const (
  1078. multilibNone multilibUsage = 0
  1079. multilib32 multilibUsage = 1
  1080. multilib64 multilibUsage = 2
  1081. multilibBoth = multilib32 | multilib64
  1082. )
  1083. // Add the multilib that is used in the arch type.
  1084. func (m multilibUsage) addArchType(archType android.ArchType) multilibUsage {
  1085. multilib := archType.Multilib
  1086. switch multilib {
  1087. case "":
  1088. return m
  1089. case "lib32":
  1090. return m | multilib32
  1091. case "lib64":
  1092. return m | multilib64
  1093. default:
  1094. panic(fmt.Errorf("Unknown Multilib field in ArchType, expected 'lib32' or 'lib64', found %q", multilib))
  1095. }
  1096. }
  1097. func (m multilibUsage) String() string {
  1098. switch m {
  1099. case multilibNone:
  1100. return ""
  1101. case multilib32:
  1102. return "32"
  1103. case multilib64:
  1104. return "64"
  1105. case multilibBoth:
  1106. return "both"
  1107. default:
  1108. panic(fmt.Errorf("Unknown multilib value, found %b, expected one of %b, %b, %b or %b",
  1109. m, multilibNone, multilib32, multilib64, multilibBoth))
  1110. }
  1111. }
  1112. // TODO(187910671): BEGIN - Remove once modules do not have an APEX and default variant.
  1113. // variantCoordinate contains the coordinates used to identify a variant of an SDK member.
  1114. type variantCoordinate struct {
  1115. // osType identifies the OS target of a variant.
  1116. osType android.OsType
  1117. // archId identifies the architecture and whether it is for the native bridge.
  1118. archId archId
  1119. // image is the image variant name.
  1120. image string
  1121. // linkType is the link type name.
  1122. linkType string
  1123. }
  1124. func getVariantCoordinate(ctx *memberContext, variant android.Module) variantCoordinate {
  1125. linkType := ""
  1126. if len(ctx.MemberType().SupportedLinkages()) > 0 {
  1127. linkType = getLinkType(variant)
  1128. }
  1129. return variantCoordinate{
  1130. osType: variant.Target().Os,
  1131. archId: archIdFromTarget(variant.Target()),
  1132. image: variant.ImageVariation().Variation,
  1133. linkType: linkType,
  1134. }
  1135. }
  1136. // selectApexVariantsWhereAvailable filters the input list of variants by selecting the APEX
  1137. // specific variant for a specific variantCoordinate when there is both an APEX and default variant.
  1138. //
  1139. // There is a long-standing issue where a module that is added to an APEX has both an APEX and
  1140. // default/platform variant created even when the module does not require a platform variant. As a
  1141. // result an indirect dependency onto a module via the APEX will use the APEX variant, whereas a
  1142. // direct dependency onto the module will use the default/platform variant. That would result in a
  1143. // failure while attempting to optimize the properties for a member as it would have two variants
  1144. // when only one was expected.
  1145. //
  1146. // This function mitigates that problem by detecting when there are two variants that differ only
  1147. // by apex variant, where one is the default/platform variant and one is the APEX variant. In that
  1148. // case it picks the APEX variant. It picks the APEX variant because that is the behavior that would
  1149. // be expected
  1150. func selectApexVariantsWhereAvailable(ctx *memberContext, variants []android.SdkAware) []android.SdkAware {
  1151. moduleCtx := ctx.sdkMemberContext
  1152. // Group the variants by coordinates.
  1153. variantsByCoord := make(map[variantCoordinate][]android.SdkAware)
  1154. for _, variant := range variants {
  1155. coord := getVariantCoordinate(ctx, variant)
  1156. variantsByCoord[coord] = append(variantsByCoord[coord], variant)
  1157. }
  1158. toDiscard := make(map[android.SdkAware]struct{})
  1159. for coord, list := range variantsByCoord {
  1160. count := len(list)
  1161. if count == 1 {
  1162. continue
  1163. }
  1164. variantsByApex := make(map[string]android.SdkAware)
  1165. conflictDetected := false
  1166. for _, variant := range list {
  1167. apexInfo := moduleCtx.OtherModuleProvider(variant, android.ApexInfoProvider).(android.ApexInfo)
  1168. apexVariationName := apexInfo.ApexVariationName
  1169. // If there are two variants for a specific APEX variation then there is conflict.
  1170. if _, ok := variantsByApex[apexVariationName]; ok {
  1171. conflictDetected = true
  1172. break
  1173. }
  1174. variantsByApex[apexVariationName] = variant
  1175. }
  1176. // If there are more than 2 apex variations or one of the apex variations is not the
  1177. // default/platform variation then there is a conflict.
  1178. if len(variantsByApex) != 2 {
  1179. conflictDetected = true
  1180. } else if _, ok := variantsByApex[""]; !ok {
  1181. conflictDetected = true
  1182. }
  1183. // If there are no conflicts then add the default/platform variation to the list to remove.
  1184. if !conflictDetected {
  1185. toDiscard[variantsByApex[""]] = struct{}{}
  1186. continue
  1187. }
  1188. // There are duplicate variants at this coordinate and they are not the default and APEX variant
  1189. // so fail.
  1190. variantDescriptions := []string{}
  1191. for _, m := range list {
  1192. variantDescriptions = append(variantDescriptions, fmt.Sprintf(" %s", m.String()))
  1193. }
  1194. moduleCtx.ModuleErrorf("multiple conflicting variants detected for OsType{%s}, %s, Image{%s}, Link{%s}\n%s",
  1195. coord.osType, coord.archId.String(), coord.image, coord.linkType,
  1196. strings.Join(variantDescriptions, "\n"))
  1197. }
  1198. // If there are any variants to discard then remove them from the list of variants, while
  1199. // preserving the order.
  1200. if len(toDiscard) > 0 {
  1201. filtered := []android.SdkAware{}
  1202. for _, variant := range variants {
  1203. if _, ok := toDiscard[variant]; !ok {
  1204. filtered = append(filtered, variant)
  1205. }
  1206. }
  1207. variants = filtered
  1208. }
  1209. return variants
  1210. }
  1211. // TODO(187910671): END - Remove once modules do not have an APEX and default variant.
  1212. type baseInfo struct {
  1213. Properties android.SdkMemberProperties
  1214. }
  1215. func (b *baseInfo) optimizableProperties() interface{} {
  1216. return b.Properties
  1217. }
  1218. type osTypeSpecificInfo struct {
  1219. baseInfo
  1220. osType android.OsType
  1221. // The list of arch type specific info for this os type.
  1222. //
  1223. // Nil if there is one variant whose arch type is common
  1224. archInfos []*archTypeSpecificInfo
  1225. }
  1226. var _ propertiesContainer = (*osTypeSpecificInfo)(nil)
  1227. type variantPropertiesFactoryFunc func() android.SdkMemberProperties
  1228. // Create a new osTypeSpecificInfo for the specified os type and its properties
  1229. // structures populated with information from the variants.
  1230. func newOsTypeSpecificInfo(ctx android.SdkMemberContext, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, osTypeVariants []android.Module) *osTypeSpecificInfo {
  1231. osInfo := &osTypeSpecificInfo{
  1232. osType: osType,
  1233. }
  1234. osSpecificVariantPropertiesFactory := func() android.SdkMemberProperties {
  1235. properties := variantPropertiesFactory()
  1236. properties.Base().Os = osType
  1237. return properties
  1238. }
  1239. // Create a structure into which properties common across the architectures in
  1240. // this os type will be stored.
  1241. osInfo.Properties = osSpecificVariantPropertiesFactory()
  1242. // Group the variants by arch type.
  1243. var variantsByArchId = make(map[archId][]android.Module)
  1244. var archIds []archId
  1245. for _, variant := range osTypeVariants {
  1246. target := variant.Target()
  1247. id := archIdFromTarget(target)
  1248. if _, ok := variantsByArchId[id]; !ok {
  1249. archIds = append(archIds, id)
  1250. }
  1251. variantsByArchId[id] = append(variantsByArchId[id], variant)
  1252. }
  1253. if commonVariants, ok := variantsByArchId[commonArchId]; ok {
  1254. if len(osTypeVariants) != 1 {
  1255. variants := []string{}
  1256. for _, m := range osTypeVariants {
  1257. variants = append(variants, fmt.Sprintf(" %s", m.String()))
  1258. }
  1259. panic(fmt.Errorf("expected to only have 1 variant of %q when arch type is common but found %d\n%s",
  1260. ctx.Name(),
  1261. len(osTypeVariants),
  1262. strings.Join(variants, "\n")))
  1263. }
  1264. // A common arch type only has one variant and its properties should be treated
  1265. // as common to the os type.
  1266. osInfo.Properties.PopulateFromVariant(ctx, commonVariants[0])
  1267. } else {
  1268. // Create an arch specific info for each supported architecture type.
  1269. for _, id := range archIds {
  1270. archVariants := variantsByArchId[id]
  1271. archInfo := newArchSpecificInfo(ctx, id, osType, osSpecificVariantPropertiesFactory, archVariants)
  1272. osInfo.archInfos = append(osInfo.archInfos, archInfo)
  1273. }
  1274. }
  1275. return osInfo
  1276. }
  1277. func (osInfo *osTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
  1278. if len(osInfo.archInfos) == 0 {
  1279. pruner.pruneProperties(osInfo.Properties)
  1280. } else {
  1281. for _, archInfo := range osInfo.archInfos {
  1282. archInfo.pruneUnsupportedProperties(pruner)
  1283. }
  1284. }
  1285. }
  1286. // Optimize the properties by extracting common properties from arch type specific
  1287. // properties into os type specific properties.
  1288. func (osInfo *osTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
  1289. // Nothing to do if there is only a single common architecture.
  1290. if len(osInfo.archInfos) == 0 {
  1291. return
  1292. }
  1293. multilib := multilibNone
  1294. for _, archInfo := range osInfo.archInfos {
  1295. multilib = multilib.addArchType(archInfo.archId.archType)
  1296. // Optimize the arch properties first.
  1297. archInfo.optimizeProperties(ctx, commonValueExtractor)
  1298. }
  1299. extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, osInfo.Properties, osInfo.archInfos)
  1300. // Choose setting for compile_multilib that is appropriate for the arch variants supplied.
  1301. osInfo.Properties.Base().Compile_multilib = multilib.String()
  1302. }
  1303. // Add the properties for an os to a property set.
  1304. //
  1305. // Maps the properties related to the os variants through to an appropriate
  1306. // module structure that will produce equivalent set of variants when it is
  1307. // processed in a build.
  1308. func (osInfo *osTypeSpecificInfo) addToPropertySet(ctx *memberContext, bpModule android.BpModule, targetPropertySet android.BpPropertySet) {
  1309. var osPropertySet android.BpPropertySet
  1310. var archPropertySet android.BpPropertySet
  1311. var archOsPrefix string
  1312. if osInfo.Properties.Base().Os_count == 1 &&
  1313. (osInfo.osType.Class == android.Device || !ctx.memberType.IsHostOsDependent()) {
  1314. // There is only one OS type present in the variants and it shouldn't have a
  1315. // variant-specific target. The latter is the case if it's either for device
  1316. // where there is only one OS (android), or for host and the member type
  1317. // isn't host OS dependent.
  1318. // Create a structure that looks like:
  1319. // module_type {
  1320. // name: "...",
  1321. // ...
  1322. // <common properties>
  1323. // ...
  1324. // <single os type specific properties>
  1325. //
  1326. // arch: {
  1327. // <arch specific sections>
  1328. // }
  1329. //
  1330. osPropertySet = bpModule
  1331. archPropertySet = osPropertySet.AddPropertySet("arch")
  1332. // Arch specific properties need to be added to an arch specific section
  1333. // within arch.
  1334. archOsPrefix = ""
  1335. } else {
  1336. // Create a structure that looks like:
  1337. // module_type {
  1338. // name: "...",
  1339. // ...
  1340. // <common properties>
  1341. // ...
  1342. // target: {
  1343. // <arch independent os specific sections, e.g. android>
  1344. // ...
  1345. // <arch and os specific sections, e.g. android_x86>
  1346. // }
  1347. //
  1348. osType := osInfo.osType
  1349. osPropertySet = targetPropertySet.AddPropertySet(osType.Name)
  1350. archPropertySet = targetPropertySet
  1351. // Arch specific properties need to be added to an os and arch specific
  1352. // section prefixed with <os>_.
  1353. archOsPrefix = osType.Name + "_"
  1354. }
  1355. // Add the os specific but arch independent properties to the module.
  1356. addSdkMemberPropertiesToSet(ctx, osInfo.Properties, osPropertySet)
  1357. // Add arch (and possibly os) specific sections for each set of arch (and possibly
  1358. // os) specific properties.
  1359. //
  1360. // The archInfos list will be empty if the os contains variants for the common
  1361. // architecture.
  1362. for _, archInfo := range osInfo.archInfos {
  1363. archInfo.addToPropertySet(ctx, archPropertySet, archOsPrefix)
  1364. }
  1365. }
  1366. func (osInfo *osTypeSpecificInfo) isHostVariant() bool {
  1367. osClass := osInfo.osType.Class
  1368. return osClass == android.Host
  1369. }
  1370. var _ isHostVariant = (*osTypeSpecificInfo)(nil)
  1371. func (osInfo *osTypeSpecificInfo) String() string {
  1372. return fmt.Sprintf("OsType{%s}", osInfo.osType)
  1373. }
  1374. // archId encapsulates the information needed to identify a combination of arch type and native
  1375. // bridge support.
  1376. //
  1377. // Conceptually, native bridge support is a facet of an android.Target, not an android.Arch as it is
  1378. // essentially using one android.Arch to implement another. However, in terms of the handling of
  1379. // the variants native bridge is treated as part of the arch variation. See the ArchVariation method
  1380. // on android.Target.
  1381. //
  1382. // So, it makes sense when optimizing the variants to combine native bridge with the arch type.
  1383. type archId struct {
  1384. // The arch type of the variant's target.
  1385. archType android.ArchType
  1386. // True if the variants is for the native bridge, false otherwise.
  1387. nativeBridge bool
  1388. }
  1389. // propertyName returns the name of the property corresponding to use for this arch id.
  1390. func (i *archId) propertyName() string {
  1391. name := i.archType.Name
  1392. if i.nativeBridge {
  1393. // Note: This does not result in a valid property because there is no architecture specific
  1394. // native bridge property, only a generic "native_bridge" property. However, this will be used
  1395. // in error messages if there is an attempt to use this in a generated bp file.
  1396. name += "_native_bridge"
  1397. }
  1398. return name
  1399. }
  1400. func (i *archId) String() string {
  1401. return fmt.Sprintf("ArchType{%s}, NativeBridge{%t}", i.archType, i.nativeBridge)
  1402. }
  1403. // archIdFromTarget returns an archId initialized from information in the supplied target.
  1404. func archIdFromTarget(target android.Target) archId {
  1405. return archId{
  1406. archType: target.Arch.ArchType,
  1407. nativeBridge: target.NativeBridge == android.NativeBridgeEnabled,
  1408. }
  1409. }
  1410. // commonArchId is the archId for the common architecture.
  1411. var commonArchId = archId{archType: android.Common}
  1412. type archTypeSpecificInfo struct {
  1413. baseInfo
  1414. archId archId
  1415. osType android.OsType
  1416. imageVariantInfos []*imageVariantSpecificInfo
  1417. }
  1418. var _ propertiesContainer = (*archTypeSpecificInfo)(nil)
  1419. // Create a new archTypeSpecificInfo for the specified arch type and its properties
  1420. // structures populated with information from the variants.
  1421. func newArchSpecificInfo(ctx android.SdkMemberContext, archId archId, osType android.OsType, variantPropertiesFactory variantPropertiesFactoryFunc, archVariants []android.Module) *archTypeSpecificInfo {
  1422. // Create an arch specific info into which the variant properties can be copied.
  1423. archInfo := &archTypeSpecificInfo{archId: archId, osType: osType}
  1424. // Create the properties into which the arch type specific properties will be
  1425. // added.
  1426. archInfo.Properties = variantPropertiesFactory()
  1427. // if there are multiple supported link variants, we want to nest based on linkage even if there
  1428. // is only one variant, otherwise, if there is only one variant we can populate based on the arch
  1429. if len(archVariants) == 1 && len(ctx.MemberType().SupportedLinkages()) <= 1 {
  1430. archInfo.Properties.PopulateFromVariant(ctx, archVariants[0])
  1431. } else {
  1432. // Group the variants by image type.
  1433. variantsByImage := make(map[string][]android.Module)
  1434. for _, variant := range archVariants {
  1435. image := variant.ImageVariation().Variation
  1436. variantsByImage[image] = append(variantsByImage[image], variant)
  1437. }
  1438. // Create the image variant info in a fixed order.
  1439. for _, imageVariantName := range android.SortedStringKeys(variantsByImage) {
  1440. variants := variantsByImage[imageVariantName]
  1441. archInfo.imageVariantInfos = append(archInfo.imageVariantInfos, newImageVariantSpecificInfo(ctx, imageVariantName, variantPropertiesFactory, variants))
  1442. }
  1443. }
  1444. return archInfo
  1445. }
  1446. // Get the link type of the variant
  1447. //
  1448. // If the variant is not differentiated by link type then it returns "",
  1449. // otherwise it returns one of "static" or "shared".
  1450. func getLinkType(variant android.Module) string {
  1451. linkType := ""
  1452. if linkable, ok := variant.(cc.LinkableInterface); ok {
  1453. if linkable.Shared() && linkable.Static() {
  1454. panic(fmt.Errorf("expected variant %q to be either static or shared but was both", variant.String()))
  1455. } else if linkable.Shared() {
  1456. linkType = "shared"
  1457. } else if linkable.Static() {
  1458. linkType = "static"
  1459. } else {
  1460. panic(fmt.Errorf("expected variant %q to be either static or shared but was neither", variant.String()))
  1461. }
  1462. }
  1463. return linkType
  1464. }
  1465. func (archInfo *archTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
  1466. if len(archInfo.imageVariantInfos) == 0 {
  1467. pruner.pruneProperties(archInfo.Properties)
  1468. } else {
  1469. for _, imageVariantInfo := range archInfo.imageVariantInfos {
  1470. imageVariantInfo.pruneUnsupportedProperties(pruner)
  1471. }
  1472. }
  1473. }
  1474. // Optimize the properties by extracting common properties from link type specific
  1475. // properties into arch type specific properties.
  1476. func (archInfo *archTypeSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
  1477. if len(archInfo.imageVariantInfos) == 0 {
  1478. return
  1479. }
  1480. // Optimize the image variant properties first.
  1481. for _, imageVariantInfo := range archInfo.imageVariantInfos {
  1482. imageVariantInfo.optimizeProperties(ctx, commonValueExtractor)
  1483. }
  1484. extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, archInfo.Properties, archInfo.imageVariantInfos)
  1485. }
  1486. // Add the properties for an arch type to a property set.
  1487. func (archInfo *archTypeSpecificInfo) addToPropertySet(ctx *memberContext, archPropertySet android.BpPropertySet, archOsPrefix string) {
  1488. archPropertySuffix := archInfo.archId.propertyName()
  1489. propertySetName := archOsPrefix + archPropertySuffix
  1490. archTypePropertySet := archPropertySet.AddPropertySet(propertySetName)
  1491. // Enable the <os>_<arch> variant explicitly when we've disabled it by default on host.
  1492. if ctx.memberType.IsHostOsDependent() && archInfo.osType.Class == android.Host {
  1493. archTypePropertySet.AddProperty("enabled", true)
  1494. }
  1495. addSdkMemberPropertiesToSet(ctx, archInfo.Properties, archTypePropertySet)
  1496. for _, imageVariantInfo := range archInfo.imageVariantInfos {
  1497. imageVariantInfo.addToPropertySet(ctx, archTypePropertySet)
  1498. }
  1499. // If this is for a native bridge architecture then make sure that the property set does not
  1500. // contain any properties as providing native bridge specific properties is not currently
  1501. // supported.
  1502. if archInfo.archId.nativeBridge {
  1503. propertySetContents := getPropertySetContents(archTypePropertySet)
  1504. if propertySetContents != "" {
  1505. ctx.SdkModuleContext().ModuleErrorf("Architecture variant %q of sdk member %q has properties distinct from other variants; this is not yet supported. The properties are:\n%s",
  1506. propertySetName, ctx.name, propertySetContents)
  1507. }
  1508. }
  1509. }
  1510. // getPropertySetContents returns the string representation of the contents of a property set, after
  1511. // recursively pruning any empty nested property sets.
  1512. func getPropertySetContents(propertySet android.BpPropertySet) string {
  1513. set := propertySet.(*bpPropertySet)
  1514. set.transformContents(pruneEmptySetTransformer{})
  1515. if len(set.properties) != 0 {
  1516. contents := &generatedContents{}
  1517. contents.Indent()
  1518. outputPropertySet(contents, set)
  1519. setAsString := contents.content.String()
  1520. return setAsString
  1521. }
  1522. return ""
  1523. }
  1524. func (archInfo *archTypeSpecificInfo) String() string {
  1525. return archInfo.archId.String()
  1526. }
  1527. type imageVariantSpecificInfo struct {
  1528. baseInfo
  1529. imageVariant string
  1530. linkInfos []*linkTypeSpecificInfo
  1531. }
  1532. func newImageVariantSpecificInfo(ctx android.SdkMemberContext, imageVariant string, variantPropertiesFactory variantPropertiesFactoryFunc, imageVariants []android.Module) *imageVariantSpecificInfo {
  1533. // Create an image variant specific info into which the variant properties can be copied.
  1534. imageInfo := &imageVariantSpecificInfo{imageVariant: imageVariant}
  1535. // Create the properties into which the image variant specific properties will be added.
  1536. imageInfo.Properties = variantPropertiesFactory()
  1537. // if there are multiple supported link variants, we want to nest even if there is only one
  1538. // variant, otherwise, if there is only one variant we can populate based on the image
  1539. if len(imageVariants) == 1 && len(ctx.MemberType().SupportedLinkages()) <= 1 {
  1540. imageInfo.Properties.PopulateFromVariant(ctx, imageVariants[0])
  1541. } else {
  1542. // There is more than one variant for this image variant which must be differentiated by link
  1543. // type. Or there are multiple supported linkages and we need to nest based on link type.
  1544. for _, linkVariant := range imageVariants {
  1545. linkType := getLinkType(linkVariant)
  1546. if linkType == "" {
  1547. panic(fmt.Errorf("expected one arch specific variant as it is not identified by link type but found %d", len(imageVariants)))
  1548. } else {
  1549. linkInfo := newLinkSpecificInfo(ctx, linkType, variantPropertiesFactory, linkVariant)
  1550. imageInfo.linkInfos = append(imageInfo.linkInfos, linkInfo)
  1551. }
  1552. }
  1553. }
  1554. return imageInfo
  1555. }
  1556. func (imageInfo *imageVariantSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
  1557. if len(imageInfo.linkInfos) == 0 {
  1558. pruner.pruneProperties(imageInfo.Properties)
  1559. } else {
  1560. for _, linkInfo := range imageInfo.linkInfos {
  1561. linkInfo.pruneUnsupportedProperties(pruner)
  1562. }
  1563. }
  1564. }
  1565. // Optimize the properties by extracting common properties from link type specific
  1566. // properties into arch type specific properties.
  1567. func (imageInfo *imageVariantSpecificInfo) optimizeProperties(ctx *memberContext, commonValueExtractor *commonValueExtractor) {
  1568. if len(imageInfo.linkInfos) == 0 {
  1569. return
  1570. }
  1571. extractCommonProperties(ctx.sdkMemberContext, commonValueExtractor, imageInfo.Properties, imageInfo.linkInfos)
  1572. }
  1573. // Add the properties for an arch type to a property set.
  1574. func (imageInfo *imageVariantSpecificInfo) addToPropertySet(ctx *memberContext, propertySet android.BpPropertySet) {
  1575. if imageInfo.imageVariant != android.CoreVariation {
  1576. propertySet = propertySet.AddPropertySet(imageInfo.imageVariant)
  1577. }
  1578. addSdkMemberPropertiesToSet(ctx, imageInfo.Properties, propertySet)
  1579. usedLinkages := make(map[string]bool, len(imageInfo.linkInfos))
  1580. for _, linkInfo := range imageInfo.linkInfos {
  1581. usedLinkages[linkInfo.linkType] = true
  1582. linkInfo.addToPropertySet(ctx, propertySet)
  1583. }
  1584. // If not all supported linkages had existing variants, we need to disable the unsupported variant
  1585. if len(imageInfo.linkInfos) < len(ctx.MemberType().SupportedLinkages()) {
  1586. for _, l := range ctx.MemberType().SupportedLinkages() {
  1587. if _, ok := usedLinkages[l]; !ok {
  1588. otherLinkagePropertySet := propertySet.AddPropertySet(l)
  1589. otherLinkagePropertySet.AddProperty("enabled", false)
  1590. }
  1591. }
  1592. }
  1593. // If this is for a non-core image variant then make sure that the property set does not contain
  1594. // any properties as providing non-core image variant specific properties for prebuilts is not
  1595. // currently supported.
  1596. if imageInfo.imageVariant != android.CoreVariation {
  1597. propertySetContents := getPropertySetContents(propertySet)
  1598. if propertySetContents != "" {
  1599. ctx.SdkModuleContext().ModuleErrorf("Image variant %q of sdk member %q has properties distinct from other variants; this is not yet supported. The properties are:\n%s",
  1600. imageInfo.imageVariant, ctx.name, propertySetContents)
  1601. }
  1602. }
  1603. }
  1604. func (imageInfo *imageVariantSpecificInfo) String() string {
  1605. return imageInfo.imageVariant
  1606. }
  1607. type linkTypeSpecificInfo struct {
  1608. baseInfo
  1609. linkType string
  1610. }
  1611. var _ propertiesContainer = (*linkTypeSpecificInfo)(nil)
  1612. // Create a new linkTypeSpecificInfo for the specified link type and its properties
  1613. // structures populated with information from the variant.
  1614. func newLinkSpecificInfo(ctx android.SdkMemberContext, linkType string, variantPropertiesFactory variantPropertiesFactoryFunc, linkVariant android.Module) *linkTypeSpecificInfo {
  1615. linkInfo := &linkTypeSpecificInfo{
  1616. baseInfo: baseInfo{
  1617. // Create the properties into which the link type specific properties will be
  1618. // added.
  1619. Properties: variantPropertiesFactory(),
  1620. },
  1621. linkType: linkType,
  1622. }
  1623. linkInfo.Properties.PopulateFromVariant(ctx, linkVariant)
  1624. return linkInfo
  1625. }
  1626. func (l *linkTypeSpecificInfo) addToPropertySet(ctx *memberContext, propertySet android.BpPropertySet) {
  1627. linkPropertySet := propertySet.AddPropertySet(l.linkType)
  1628. addSdkMemberPropertiesToSet(ctx, l.Properties, linkPropertySet)
  1629. }
  1630. func (l *linkTypeSpecificInfo) pruneUnsupportedProperties(pruner *propertyPruner) {
  1631. pruner.pruneProperties(l.Properties)
  1632. }
  1633. func (l *linkTypeSpecificInfo) String() string {
  1634. return fmt.Sprintf("LinkType{%s}", l.linkType)
  1635. }
  1636. type memberContext struct {
  1637. sdkMemberContext android.ModuleContext
  1638. builder *snapshotBuilder
  1639. memberType android.SdkMemberType
  1640. name string
  1641. // The set of traits required of this member.
  1642. requiredTraits android.SdkMemberTraitSet
  1643. }
  1644. func (m *memberContext) SdkModuleContext() android.ModuleContext {
  1645. return m.sdkMemberContext
  1646. }
  1647. func (m *memberContext) SnapshotBuilder() android.SnapshotBuilder {
  1648. return m.builder
  1649. }
  1650. func (m *memberContext) MemberType() android.SdkMemberType {
  1651. return m.memberType
  1652. }
  1653. func (m *memberContext) Name() string {
  1654. return m.name
  1655. }
  1656. func (m *memberContext) RequiresTrait(trait android.SdkMemberTrait) bool {
  1657. return m.requiredTraits.Contains(trait)
  1658. }
  1659. func (m *memberContext) IsTargetBuildBeforeTiramisu() bool {
  1660. return m.builder.targetBuildRelease.EarlierThan(buildReleaseT)
  1661. }
  1662. var _ android.SdkMemberContext = (*memberContext)(nil)
  1663. func (s *sdk) createMemberSnapshot(ctx *memberContext, member *sdkMember, bpModule *bpModule) {
  1664. memberType := member.memberType
  1665. // Do not add the prefer property if the member snapshot module is a source module type.
  1666. moduleCtx := ctx.sdkMemberContext
  1667. config := moduleCtx.Config()
  1668. if !memberType.UsesSourceModuleTypeInSnapshot() {
  1669. // Set the prefer based on the environment variable. This is a temporary work around to allow a
  1670. // snapshot to be created that sets prefer: true.
  1671. // TODO(b/174997203): Remove once the ability to select the modules to prefer can be done
  1672. // dynamically at build time not at snapshot generation time.
  1673. prefer := config.IsEnvTrue("SOONG_SDK_SNAPSHOT_PREFER")
  1674. // Set prefer. Setting this to false is not strictly required as that is the default but it does
  1675. // provide a convenient hook to post-process the generated Android.bp file, e.g. in tests to
  1676. // check the behavior when a prebuilt is preferred. It also makes it explicit what the default
  1677. // behavior is for the module.
  1678. bpModule.insertAfter("name", "prefer", prefer)
  1679. configVar := config.Getenv("SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR")
  1680. if configVar != "" {
  1681. parts := strings.Split(configVar, ":")
  1682. cfp := android.ConfigVarProperties{
  1683. Config_namespace: proptools.StringPtr(parts[0]),
  1684. Var_name: proptools.StringPtr(parts[1]),
  1685. }
  1686. bpModule.insertAfter("prefer", "use_source_config_var", cfp)
  1687. }
  1688. }
  1689. variants := selectApexVariantsWhereAvailable(ctx, member.variants)
  1690. // Group the variants by os type.
  1691. variantsByOsType := make(map[android.OsType][]android.Module)
  1692. for _, variant := range variants {
  1693. osType := variant.Target().Os
  1694. variantsByOsType[osType] = append(variantsByOsType[osType], variant)
  1695. }
  1696. osCount := len(variantsByOsType)
  1697. variantPropertiesFactory := func() android.SdkMemberProperties {
  1698. properties := memberType.CreateVariantPropertiesStruct()
  1699. base := properties.Base()
  1700. base.Os_count = osCount
  1701. return properties
  1702. }
  1703. osTypeToInfo := make(map[android.OsType]*osTypeSpecificInfo)
  1704. // The set of properties that are common across all architectures and os types.
  1705. commonProperties := variantPropertiesFactory()
  1706. commonProperties.Base().Os = android.CommonOS
  1707. // Create a property pruner that will prune any properties unsupported by the target build
  1708. // release.
  1709. targetBuildRelease := ctx.builder.targetBuildRelease
  1710. unsupportedPropertyPruner := newPropertyPrunerByBuildRelease(commonProperties, targetBuildRelease)
  1711. // Create common value extractor that can be used to optimize the properties.
  1712. commonValueExtractor := newCommonValueExtractor(commonProperties)
  1713. // The list of property structures which are os type specific but common across
  1714. // architectures within that os type.
  1715. var osSpecificPropertiesContainers []*osTypeSpecificInfo
  1716. for osType, osTypeVariants := range variantsByOsType {
  1717. osInfo := newOsTypeSpecificInfo(ctx, osType, variantPropertiesFactory, osTypeVariants)
  1718. osTypeToInfo[osType] = osInfo
  1719. // Add the os specific properties to a list of os type specific yet architecture
  1720. // independent properties structs.
  1721. osSpecificPropertiesContainers = append(osSpecificPropertiesContainers, osInfo)
  1722. osInfo.pruneUnsupportedProperties(unsupportedPropertyPruner)
  1723. // Optimize the properties across all the variants for a specific os type.
  1724. osInfo.optimizeProperties(ctx, commonValueExtractor)
  1725. }
  1726. // Extract properties which are common across all architectures and os types.
  1727. extractCommonProperties(moduleCtx, commonValueExtractor, commonProperties, osSpecificPropertiesContainers)
  1728. // Add the common properties to the module.
  1729. addSdkMemberPropertiesToSet(ctx, commonProperties, bpModule)
  1730. // Create a target property set into which target specific properties can be
  1731. // added.
  1732. targetPropertySet := bpModule.AddPropertySet("target")
  1733. // If the member is host OS dependent and has host_supported then disable by
  1734. // default and enable each host OS variant explicitly. This avoids problems
  1735. // with implicitly enabled OS variants when the snapshot is used, which might
  1736. // be different from this run (e.g. different build OS).
  1737. if ctx.memberType.IsHostOsDependent() {
  1738. hostSupported := bpModule.getValue("host_supported") == true // Missing means false.
  1739. if hostSupported {
  1740. hostPropertySet := targetPropertySet.AddPropertySet("host")
  1741. hostPropertySet.AddProperty("enabled", false)
  1742. }
  1743. }
  1744. // Iterate over the os types in a fixed order.
  1745. for _, osType := range s.getPossibleOsTypes() {
  1746. osInfo := osTypeToInfo[osType]
  1747. if osInfo == nil {
  1748. continue
  1749. }
  1750. osInfo.addToPropertySet(ctx, bpModule, targetPropertySet)
  1751. }
  1752. }
  1753. // Compute the list of possible os types that this sdk could support.
  1754. func (s *sdk) getPossibleOsTypes() []android.OsType {
  1755. var osTypes []android.OsType
  1756. for _, osType := range android.OsTypeList() {
  1757. if s.DeviceSupported() {
  1758. if osType.Class == android.Device {
  1759. osTypes = append(osTypes, osType)
  1760. }
  1761. }
  1762. if s.HostSupported() {
  1763. if osType.Class == android.Host {
  1764. osTypes = append(osTypes, osType)
  1765. }
  1766. }
  1767. }
  1768. sort.SliceStable(osTypes, func(i, j int) bool { return osTypes[i].Name < osTypes[j].Name })
  1769. return osTypes
  1770. }
  1771. // Given a set of properties (struct value), return the value of the field within that
  1772. // struct (or one of its embedded structs).
  1773. type fieldAccessorFunc func(structValue reflect.Value) reflect.Value
  1774. // Checks the metadata to determine whether the property should be ignored for the
  1775. // purposes of common value extraction or not.
  1776. type extractorMetadataPredicate func(metadata propertiesContainer) bool
  1777. // Indicates whether optimizable properties are provided by a host variant or
  1778. // not.
  1779. type isHostVariant interface {
  1780. isHostVariant() bool
  1781. }
  1782. // A property that can be optimized by the commonValueExtractor.
  1783. type extractorProperty struct {
  1784. // The name of the field for this property. It is a "."-separated path for
  1785. // fields in non-anonymous substructs.
  1786. name string
  1787. // Filter that can use metadata associated with the properties being optimized
  1788. // to determine whether the field should be ignored during common value
  1789. // optimization.
  1790. filter extractorMetadataPredicate
  1791. // Retrieves the value on which common value optimization will be performed.
  1792. getter fieldAccessorFunc
  1793. // The empty value for the field.
  1794. emptyValue reflect.Value
  1795. // True if the property can support arch variants false otherwise.
  1796. archVariant bool
  1797. }
  1798. func (p extractorProperty) String() string {
  1799. return p.name
  1800. }
  1801. // Supports extracting common values from a number of instances of a properties
  1802. // structure into a separate common set of properties.
  1803. type commonValueExtractor struct {
  1804. // The properties that the extractor can optimize.
  1805. properties []extractorProperty
  1806. }
  1807. // Create a new common value extractor for the structure type for the supplied
  1808. // properties struct.
  1809. //
  1810. // The returned extractor can be used on any properties structure of the same type
  1811. // as the supplied set of properties.
  1812. func newCommonValueExtractor(propertiesStruct interface{}) *commonValueExtractor {
  1813. structType := getStructValue(reflect.ValueOf(propertiesStruct)).Type()
  1814. extractor := &commonValueExtractor{}
  1815. extractor.gatherFields(structType, nil, "")
  1816. return extractor
  1817. }
  1818. // Gather the fields from the supplied structure type from which common values will
  1819. // be extracted.
  1820. //
  1821. // This is recursive function. If it encounters a struct then it will recurse
  1822. // into it, passing in the accessor for the field and the struct name as prefix
  1823. // for the nested fields. That will then be used in the accessors for the fields
  1824. // in the embedded struct.
  1825. func (e *commonValueExtractor) gatherFields(structType reflect.Type, containingStructAccessor fieldAccessorFunc, namePrefix string) {
  1826. for f := 0; f < structType.NumField(); f++ {
  1827. field := structType.Field(f)
  1828. if field.PkgPath != "" {
  1829. // Ignore unexported fields.
  1830. continue
  1831. }
  1832. // Ignore fields whose value should be kept.
  1833. if proptools.HasTag(field, "sdk", "keep") {
  1834. continue
  1835. }
  1836. var filter extractorMetadataPredicate
  1837. // Add a filter
  1838. if proptools.HasTag(field, "sdk", "ignored-on-host") {
  1839. filter = func(metadata propertiesContainer) bool {
  1840. if m, ok := metadata.(isHostVariant); ok {
  1841. if m.isHostVariant() {
  1842. return false
  1843. }
  1844. }
  1845. return true
  1846. }
  1847. }
  1848. // Save a copy of the field index for use in the function.
  1849. fieldIndex := f
  1850. name := namePrefix + field.Name
  1851. fieldGetter := func(value reflect.Value) reflect.Value {
  1852. if containingStructAccessor != nil {
  1853. // This is an embedded structure so first access the field for the embedded
  1854. // structure.
  1855. value = containingStructAccessor(value)
  1856. }
  1857. // Skip through interface and pointer values to find the structure.
  1858. value = getStructValue(value)
  1859. defer func() {
  1860. if r := recover(); r != nil {
  1861. panic(fmt.Errorf("%s for fieldIndex %d of field %s of value %#v", r, fieldIndex, name, value.Interface()))
  1862. }
  1863. }()
  1864. // Return the field.
  1865. return value.Field(fieldIndex)
  1866. }
  1867. if field.Type.Kind() == reflect.Struct {
  1868. // Gather fields from the nested or embedded structure.
  1869. var subNamePrefix string
  1870. if field.Anonymous {
  1871. subNamePrefix = namePrefix
  1872. } else {
  1873. subNamePrefix = name + "."
  1874. }
  1875. e.gatherFields(field.Type, fieldGetter, subNamePrefix)
  1876. } else {
  1877. property := extractorProperty{
  1878. name,
  1879. filter,
  1880. fieldGetter,
  1881. reflect.Zero(field.Type),
  1882. proptools.HasTag(field, "android", "arch_variant"),
  1883. }
  1884. e.properties = append(e.properties, property)
  1885. }
  1886. }
  1887. }
  1888. func getStructValue(value reflect.Value) reflect.Value {
  1889. foundStruct:
  1890. for {
  1891. kind := value.Kind()
  1892. switch kind {
  1893. case reflect.Interface, reflect.Ptr:
  1894. value = value.Elem()
  1895. case reflect.Struct:
  1896. break foundStruct
  1897. default:
  1898. panic(fmt.Errorf("expecting struct, interface or pointer, found %v of kind %s", value, kind))
  1899. }
  1900. }
  1901. return value
  1902. }
  1903. // A container of properties to be optimized.
  1904. //
  1905. // Allows additional information to be associated with the properties, e.g. for
  1906. // filtering.
  1907. type propertiesContainer interface {
  1908. fmt.Stringer
  1909. // Get the properties that need optimizing.
  1910. optimizableProperties() interface{}
  1911. }
  1912. // A wrapper for sdk variant related properties to allow them to be optimized.
  1913. type sdkVariantPropertiesContainer struct {
  1914. sdkVariant *sdk
  1915. properties interface{}
  1916. }
  1917. func (c sdkVariantPropertiesContainer) optimizableProperties() interface{} {
  1918. return c.properties
  1919. }
  1920. func (c sdkVariantPropertiesContainer) String() string {
  1921. return c.sdkVariant.String()
  1922. }
  1923. // Extract common properties from a slice of property structures of the same type.
  1924. //
  1925. // All the property structures must be of the same type.
  1926. // commonProperties - must be a pointer to the structure into which common properties will be added.
  1927. // inputPropertiesSlice - must be a slice of propertiesContainer interfaces.
  1928. //
  1929. // Iterates over each exported field (capitalized name) and checks to see whether they
  1930. // have the same value (using DeepEquals) across all the input properties. If it does not then no
  1931. // change is made. Otherwise, the common value is stored in the field in the commonProperties
  1932. // and the field in each of the input properties structure is set to its default value. Nested
  1933. // structs are visited recursively and their non-struct fields are compared.
  1934. func (e *commonValueExtractor) extractCommonProperties(commonProperties interface{}, inputPropertiesSlice interface{}) error {
  1935. commonPropertiesValue := reflect.ValueOf(commonProperties)
  1936. commonStructValue := commonPropertiesValue.Elem()
  1937. sliceValue := reflect.ValueOf(inputPropertiesSlice)
  1938. for _, property := range e.properties {
  1939. fieldGetter := property.getter
  1940. filter := property.filter
  1941. if filter == nil {
  1942. filter = func(metadata propertiesContainer) bool {
  1943. return true
  1944. }
  1945. }
  1946. // Check to see if all the structures have the same value for the field. The commonValue
  1947. // is nil on entry to the loop and if it is nil on exit then there is no common value or
  1948. // all the values have been filtered out, otherwise it points to the common value.
  1949. var commonValue *reflect.Value
  1950. // Assume that all the values will be the same.
  1951. //
  1952. // While similar to this is not quite the same as commonValue == nil. If all the values
  1953. // have been filtered out then this will be false but commonValue == nil will be true.
  1954. valuesDiffer := false
  1955. for i := 0; i < sliceValue.Len(); i++ {
  1956. container := sliceValue.Index(i).Interface().(propertiesContainer)
  1957. itemValue := reflect.ValueOf(container.optimizableProperties())
  1958. fieldValue := fieldGetter(itemValue)
  1959. if !filter(container) {
  1960. expectedValue := property.emptyValue.Interface()
  1961. actualValue := fieldValue.Interface()
  1962. if !reflect.DeepEqual(expectedValue, actualValue) {
  1963. return fmt.Errorf("field %q is supposed to be ignored for %q but is set to %#v instead of %#v", property, container, actualValue, expectedValue)
  1964. }
  1965. continue
  1966. }
  1967. if commonValue == nil {
  1968. // Use the first value as the commonProperties value.
  1969. commonValue = &fieldValue
  1970. } else {
  1971. // If the value does not match the current common value then there is
  1972. // no value in common so break out.
  1973. if !reflect.DeepEqual(fieldValue.Interface(), commonValue.Interface()) {
  1974. commonValue = nil
  1975. valuesDiffer = true
  1976. break
  1977. }
  1978. }
  1979. }
  1980. // If the fields all have common value then store it in the common struct field
  1981. // and set the input struct's field to the empty value.
  1982. if commonValue != nil {
  1983. emptyValue := property.emptyValue
  1984. fieldGetter(commonStructValue).Set(*commonValue)
  1985. for i := 0; i < sliceValue.Len(); i++ {
  1986. container := sliceValue.Index(i).Interface().(propertiesContainer)
  1987. itemValue := reflect.ValueOf(container.optimizableProperties())
  1988. fieldValue := fieldGetter(itemValue)
  1989. fieldValue.Set(emptyValue)
  1990. }
  1991. }
  1992. if valuesDiffer && !property.archVariant {
  1993. // The values differ but the property does not support arch variants so it
  1994. // is an error.
  1995. var details strings.Builder
  1996. for i := 0; i < sliceValue.Len(); i++ {
  1997. container := sliceValue.Index(i).Interface().(propertiesContainer)
  1998. itemValue := reflect.ValueOf(container.optimizableProperties())
  1999. fieldValue := fieldGetter(itemValue)
  2000. _, _ = fmt.Fprintf(&details, "\n %q has value %q", container.String(), fieldValue.Interface())
  2001. }
  2002. return fmt.Errorf("field %q is not tagged as \"arch_variant\" but has arch specific properties:%s", property.String(), details.String())
  2003. }
  2004. }
  2005. return nil
  2006. }