update.go 81 KB

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