update.go 81 KB

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