update.go 76 KB

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