arch.go 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package android
  15. import (
  16. "encoding"
  17. "fmt"
  18. "reflect"
  19. "runtime"
  20. "strconv"
  21. "strings"
  22. "github.com/google/blueprint"
  23. "github.com/google/blueprint/bootstrap"
  24. "github.com/google/blueprint/proptools"
  25. )
  26. /*
  27. Example blueprints file containing all variant property groups, with comment listing what type
  28. of variants get properties in that group:
  29. module {
  30. arch: {
  31. arm: {
  32. // Host or device variants with arm architecture
  33. },
  34. arm64: {
  35. // Host or device variants with arm64 architecture
  36. },
  37. x86: {
  38. // Host or device variants with x86 architecture
  39. },
  40. x86_64: {
  41. // Host or device variants with x86_64 architecture
  42. },
  43. },
  44. multilib: {
  45. lib32: {
  46. // Host or device variants for 32-bit architectures
  47. },
  48. lib64: {
  49. // Host or device variants for 64-bit architectures
  50. },
  51. },
  52. target: {
  53. android: {
  54. // Device variants (implies Bionic)
  55. },
  56. host: {
  57. // Host variants
  58. },
  59. bionic: {
  60. // Bionic (device and host) variants
  61. },
  62. linux_bionic: {
  63. // Bionic host variants
  64. },
  65. linux: {
  66. // Bionic (device and host) and Linux glibc variants
  67. },
  68. linux_glibc: {
  69. // Linux host variants (using non-Bionic libc)
  70. },
  71. darwin: {
  72. // Darwin host variants
  73. },
  74. windows: {
  75. // Windows host variants
  76. },
  77. not_windows: {
  78. // Non-windows host variants
  79. },
  80. android_arm: {
  81. // Any <os>_<arch> combination restricts to that os and arch
  82. },
  83. },
  84. }
  85. */
  86. // An Arch indicates a single CPU architecture.
  87. type Arch struct {
  88. // The type of the architecture (arm, arm64, x86, or x86_64).
  89. ArchType ArchType
  90. // The variant of the architecture, for example "armv7-a" or "armv7-a-neon" for arm.
  91. ArchVariant string
  92. // The variant of the CPU, for example "cortex-a53" for arm64.
  93. CpuVariant string
  94. // The list of Android app ABIs supported by the CPU architecture, for example "arm64-v8a".
  95. Abi []string
  96. // The list of arch-specific features supported by the CPU architecture, for example "neon".
  97. ArchFeatures []string
  98. }
  99. // String returns the Arch as a string. The value is used as the name of the variant created
  100. // by archMutator.
  101. func (a Arch) String() string {
  102. s := a.ArchType.String()
  103. if a.ArchVariant != "" {
  104. s += "_" + a.ArchVariant
  105. }
  106. if a.CpuVariant != "" {
  107. s += "_" + a.CpuVariant
  108. }
  109. return s
  110. }
  111. // ArchType is used to define the 4 supported architecture types (arm, arm64, x86, x86_64), as
  112. // well as the "common" architecture used for modules that support multiple architectures, for
  113. // example Java modules.
  114. type ArchType struct {
  115. // Name is the name of the architecture type, "arm", "arm64", "x86", or "x86_64".
  116. Name string
  117. // Field is the name of the field used in properties that refer to the architecture, e.g. "Arm64".
  118. Field string
  119. // Multilib is either "lib32" or "lib64" for 32-bit or 64-bit architectures.
  120. Multilib string
  121. }
  122. // String returns the name of the ArchType.
  123. func (a ArchType) String() string {
  124. return a.Name
  125. }
  126. const COMMON_VARIANT = "common"
  127. var (
  128. archTypeList []ArchType
  129. Arm = newArch("arm", "lib32")
  130. Arm64 = newArch("arm64", "lib64")
  131. X86 = newArch("x86", "lib32")
  132. X86_64 = newArch("x86_64", "lib64")
  133. Common = ArchType{
  134. Name: COMMON_VARIANT,
  135. }
  136. )
  137. var archTypeMap = map[string]ArchType{}
  138. func newArch(name, multilib string) ArchType {
  139. archType := ArchType{
  140. Name: name,
  141. Field: proptools.FieldNameForProperty(name),
  142. Multilib: multilib,
  143. }
  144. archTypeList = append(archTypeList, archType)
  145. archTypeMap[name] = archType
  146. return archType
  147. }
  148. // ArchTypeList returns the 4 supported ArchTypes for arm, arm64, x86 and x86_64.
  149. func ArchTypeList() []ArchType {
  150. return append([]ArchType(nil), archTypeList...)
  151. }
  152. // MarshalText allows an ArchType to be serialized through any encoder that supports
  153. // encoding.TextMarshaler.
  154. func (a ArchType) MarshalText() ([]byte, error) {
  155. return []byte(strconv.Quote(a.String())), nil
  156. }
  157. var _ encoding.TextMarshaler = ArchType{}
  158. // UnmarshalText allows an ArchType to be deserialized through any decoder that supports
  159. // encoding.TextUnmarshaler.
  160. func (a *ArchType) UnmarshalText(text []byte) error {
  161. if u, ok := archTypeMap[string(text)]; ok {
  162. *a = u
  163. return nil
  164. }
  165. return fmt.Errorf("unknown ArchType %q", text)
  166. }
  167. var _ encoding.TextUnmarshaler = &ArchType{}
  168. // OsClass is an enum that describes whether a variant of a module runs on the host, on the device,
  169. // or is generic.
  170. type OsClass int
  171. const (
  172. // Generic is used for variants of modules that are not OS-specific.
  173. Generic OsClass = iota
  174. // Device is used for variants of modules that run on the device.
  175. Device
  176. // Host is used for variants of modules that run on the host.
  177. Host
  178. )
  179. // String returns the OsClass as a string.
  180. func (class OsClass) String() string {
  181. switch class {
  182. case Generic:
  183. return "generic"
  184. case Device:
  185. return "device"
  186. case Host:
  187. return "host"
  188. default:
  189. panic(fmt.Errorf("unknown class %d", class))
  190. }
  191. }
  192. // OsType describes an OS variant of a module.
  193. type OsType struct {
  194. // Name is the name of the OS. It is also used as the name of the property in Android.bp
  195. // files.
  196. Name string
  197. // Field is the name of the OS converted to an exported field name, i.e. with the first
  198. // character capitalized.
  199. Field string
  200. // Class is the OsClass of the OS.
  201. Class OsClass
  202. // DefaultDisabled is set when the module variants for the OS should not be created unless
  203. // the module explicitly requests them. This is used to limit Windows cross compilation to
  204. // only modules that need it.
  205. DefaultDisabled bool
  206. }
  207. // String returns the name of the OsType.
  208. func (os OsType) String() string {
  209. return os.Name
  210. }
  211. // Bionic returns true if the OS uses the Bionic libc runtime, i.e. if the OS is Android or
  212. // is Linux with Bionic.
  213. func (os OsType) Bionic() bool {
  214. return os == Android || os == LinuxBionic
  215. }
  216. // Linux returns true if the OS uses the Linux kernel, i.e. if the OS is Android or is Linux
  217. // with or without the Bionic libc runtime.
  218. func (os OsType) Linux() bool {
  219. return os == Android || os == Linux || os == LinuxBionic
  220. }
  221. // newOsType constructs an OsType and adds it to the global lists.
  222. func newOsType(name string, class OsClass, defDisabled bool, archTypes ...ArchType) OsType {
  223. checkCalledFromInit()
  224. os := OsType{
  225. Name: name,
  226. Field: proptools.FieldNameForProperty(name),
  227. Class: class,
  228. DefaultDisabled: defDisabled,
  229. }
  230. OsTypeList = append(OsTypeList, os)
  231. if _, found := commonTargetMap[name]; found {
  232. panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
  233. } else {
  234. commonTargetMap[name] = Target{Os: os, Arch: CommonArch}
  235. }
  236. osArchTypeMap[os] = archTypes
  237. return os
  238. }
  239. // osByName returns the OsType that has the given name, or NoOsType if none match.
  240. func osByName(name string) OsType {
  241. for _, os := range OsTypeList {
  242. if os.Name == name {
  243. return os
  244. }
  245. }
  246. return NoOsType
  247. }
  248. // BuildOs returns the OsType for the OS that the build is running on.
  249. var BuildOs = func() OsType {
  250. switch runtime.GOOS {
  251. case "linux":
  252. return Linux
  253. case "darwin":
  254. return Darwin
  255. default:
  256. panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
  257. }
  258. }()
  259. // BuildArch returns the ArchType for the CPU that the build is running on.
  260. var BuildArch = func() ArchType {
  261. switch runtime.GOARCH {
  262. case "amd64":
  263. return X86_64
  264. default:
  265. panic(fmt.Sprintf("unsupported Arch: %s", runtime.GOARCH))
  266. }
  267. }()
  268. var (
  269. // OsTypeList contains a list of all the supported OsTypes, including ones not supported
  270. // by the current build host or the target device.
  271. OsTypeList []OsType
  272. // commonTargetMap maps names of OsTypes to the corresponding common Target, i.e. the
  273. // Target with the same OsType and the common ArchType.
  274. commonTargetMap = make(map[string]Target)
  275. // osArchTypeMap maps OsTypes to the list of supported ArchTypes for that OS.
  276. osArchTypeMap = map[OsType][]ArchType{}
  277. // NoOsType is a placeholder for when no OS is needed.
  278. NoOsType OsType
  279. // Linux is the OS for the Linux kernel plus the glibc runtime.
  280. Linux = newOsType("linux_glibc", Host, false, X86, X86_64)
  281. // Darwin is the OS for MacOS/Darwin host machines.
  282. Darwin = newOsType("darwin", Host, false, X86_64)
  283. // LinuxBionic is the OS for the Linux kernel plus the Bionic libc runtime, but without the
  284. // rest of Android.
  285. LinuxBionic = newOsType("linux_bionic", Host, false, Arm64, X86_64)
  286. // Windows the OS for Windows host machines.
  287. Windows = newOsType("windows", Host, true, X86, X86_64)
  288. // Android is the OS for target devices that run all of Android, including the Linux kernel
  289. // and the Bionic libc runtime.
  290. Android = newOsType("android", Device, false, Arm, Arm64, X86, X86_64)
  291. // Fuchsia is the OS for target devices that run Fuchsia.
  292. Fuchsia = newOsType("fuchsia", Device, false, Arm64, X86_64)
  293. // CommonOS is a pseudo OSType for a common OS variant, which is OsType agnostic and which
  294. // has dependencies on all the OS variants.
  295. CommonOS = newOsType("common_os", Generic, false)
  296. // CommonArch is the Arch for all modules that are os-specific but not arch specific,
  297. // for example most Java modules.
  298. CommonArch = Arch{ArchType: Common}
  299. )
  300. // Target specifies the OS and architecture that a module is being compiled for.
  301. type Target struct {
  302. // Os the OS that the module is being compiled for (e.g. "linux_glibc", "android").
  303. Os OsType
  304. // Arch is the architecture that the module is being compiled for.
  305. Arch Arch
  306. // NativeBridge is NativeBridgeEnabled if the architecture is supported using NativeBridge
  307. // (i.e. arm on x86) for this device.
  308. NativeBridge NativeBridgeSupport
  309. // NativeBridgeHostArchName is the name of the real architecture that is used to implement
  310. // the NativeBridge architecture. For example, for arm on x86 this would be "x86".
  311. NativeBridgeHostArchName string
  312. // NativeBridgeRelativePath is the name of the subdirectory that will contain NativeBridge
  313. // libraries and binaries.
  314. NativeBridgeRelativePath string
  315. // HostCross is true when the target cannot run natively on the current build host.
  316. // For example, linux_glibc_x86 returns true on a regular x86/i686/Linux machines, but returns false
  317. // on Mac (different OS), or on 64-bit only i686/Linux machines (unsupported arch).
  318. HostCross bool
  319. }
  320. // NativeBridgeSupport is an enum that specifies if a Target supports NativeBridge.
  321. type NativeBridgeSupport bool
  322. const (
  323. NativeBridgeDisabled NativeBridgeSupport = false
  324. NativeBridgeEnabled NativeBridgeSupport = true
  325. )
  326. // String returns the OS and arch variations used for the Target.
  327. func (target Target) String() string {
  328. return target.OsVariation() + "_" + target.ArchVariation()
  329. }
  330. // OsVariation returns the name of the variation used by the osMutator for the Target.
  331. func (target Target) OsVariation() string {
  332. return target.Os.String()
  333. }
  334. // ArchVariation returns the name of the variation used by the archMutator for the Target.
  335. func (target Target) ArchVariation() string {
  336. var variation string
  337. if target.NativeBridge {
  338. variation = "native_bridge_"
  339. }
  340. variation += target.Arch.String()
  341. return variation
  342. }
  343. // Variations returns a list of blueprint.Variations for the osMutator and archMutator for the
  344. // Target.
  345. func (target Target) Variations() []blueprint.Variation {
  346. return []blueprint.Variation{
  347. {Mutator: "os", Variation: target.OsVariation()},
  348. {Mutator: "arch", Variation: target.ArchVariation()},
  349. }
  350. }
  351. // osMutator splits an arch-specific module into a variant for each OS that is enabled for the
  352. // module. It uses the HostOrDevice value passed to InitAndroidArchModule and the
  353. // device_supported and host_supported properties to determine which OsTypes are enabled for this
  354. // module, then searches through the Targets to determine which have enabled Targets for this
  355. // module.
  356. func osMutator(bpctx blueprint.BottomUpMutatorContext) {
  357. var module Module
  358. var ok bool
  359. if module, ok = bpctx.Module().(Module); !ok {
  360. // The module is not a Soong module, it is a Blueprint module.
  361. if bootstrap.IsBootstrapModule(bpctx.Module()) {
  362. // Bootstrap Go modules are always the build OS or linux bionic.
  363. config := bpctx.Config().(Config)
  364. osNames := []string{config.BuildOSTarget.OsVariation()}
  365. for _, hostCrossTarget := range config.Targets[LinuxBionic] {
  366. if hostCrossTarget.Arch.ArchType == config.BuildOSTarget.Arch.ArchType {
  367. osNames = append(osNames, hostCrossTarget.OsVariation())
  368. }
  369. }
  370. osNames = FirstUniqueStrings(osNames)
  371. bpctx.CreateVariations(osNames...)
  372. }
  373. return
  374. }
  375. // Bootstrap Go module support above requires this mutator to be a
  376. // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
  377. // filters out non-Soong modules. Now that we've handled them, create a
  378. // normal android.BottomUpMutatorContext.
  379. mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
  380. base := module.base()
  381. // Nothing to do for modules that are not architecture specific (e.g. a genrule).
  382. if !base.ArchSpecific() {
  383. return
  384. }
  385. // Collect a list of OSTypes supported by this module based on the HostOrDevice value
  386. // passed to InitAndroidArchModule and the device_supported and host_supported properties.
  387. var moduleOSList []OsType
  388. for _, os := range OsTypeList {
  389. for _, t := range mctx.Config().Targets[os] {
  390. if base.supportsTarget(t) {
  391. moduleOSList = append(moduleOSList, os)
  392. break
  393. }
  394. }
  395. }
  396. // If there are no supported OSes then disable the module.
  397. if len(moduleOSList) == 0 {
  398. base.Disable()
  399. return
  400. }
  401. // Convert the list of supported OsTypes to the variation names.
  402. osNames := make([]string, len(moduleOSList))
  403. for i, os := range moduleOSList {
  404. osNames[i] = os.String()
  405. }
  406. createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
  407. if createCommonOSVariant {
  408. // A CommonOS variant was requested so add it to the list of OS variants to
  409. // create. It needs to be added to the end because it needs to depend on the
  410. // the other variants in the list returned by CreateVariations(...) and inter
  411. // variant dependencies can only be created from a later variant in that list to
  412. // an earlier one. That is because variants are always processed in the order in
  413. // which they are returned from CreateVariations(...).
  414. osNames = append(osNames, CommonOS.Name)
  415. moduleOSList = append(moduleOSList, CommonOS)
  416. }
  417. // Create the variations, annotate each one with which OS it was created for, and
  418. // squash the appropriate OS-specific properties into the top level properties.
  419. modules := mctx.CreateVariations(osNames...)
  420. for i, m := range modules {
  421. m.base().commonProperties.CompileOS = moduleOSList[i]
  422. m.base().setOSProperties(mctx)
  423. }
  424. if createCommonOSVariant {
  425. // A CommonOS variant was requested so add dependencies from it (the last one in
  426. // the list) to the OS type specific variants.
  427. last := len(modules) - 1
  428. commonOSVariant := modules[last]
  429. commonOSVariant.base().commonProperties.CommonOSVariant = true
  430. for _, module := range modules[0:last] {
  431. // Ignore modules that are enabled. Note, this will only avoid adding
  432. // dependencies on OsType variants that are explicitly disabled in their
  433. // properties. The CommonOS variant will still depend on disabled variants
  434. // if they are disabled afterwards, e.g. in archMutator if
  435. if module.Enabled() {
  436. mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
  437. }
  438. }
  439. }
  440. }
  441. type archDepTag struct {
  442. blueprint.BaseDependencyTag
  443. name string
  444. }
  445. // Identifies the dependency from CommonOS variant to the os specific variants.
  446. var commonOsToOsSpecificVariantTag = archDepTag{name: "common os to os specific"}
  447. // Get the OsType specific variants for the current CommonOS variant.
  448. //
  449. // The returned list will only contain enabled OsType specific variants of the
  450. // module referenced in the supplied context. An empty list is returned if there
  451. // are no enabled variants or the supplied context is not for an CommonOS
  452. // variant.
  453. func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
  454. var variants []Module
  455. mctx.VisitDirectDeps(func(m Module) {
  456. if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
  457. if m.Enabled() {
  458. variants = append(variants, m)
  459. }
  460. }
  461. })
  462. return variants
  463. }
  464. // archMutator splits a module into a variant for each Target requested by the module. Target selection
  465. // for a module is in three levels, OsClass, multilib, and then Target.
  466. // OsClass selection is determined by:
  467. // - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
  468. // whether the module type can compile for host, device or both.
  469. // - The host_supported and device_supported properties on the module.
  470. // If host is supported for the module, the Host and HostCross OsClasses are selected. If device is supported
  471. // for the module, the Device OsClass is selected.
  472. // Within each selected OsClass, the multilib selection is determined by:
  473. // - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
  474. // target.host.compile_multilib).
  475. // - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
  476. // Valid multilib values include:
  477. // "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
  478. // "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64,
  479. // but may be arm for a 32-bit only build.
  480. // "32": compile for only a single 32-bit Target supported by the OsClass.
  481. // "64": compile for only a single 64-bit Target supported by the OsClass.
  482. // "common": compile a for a single Target that will work on all Targets supported by the OsClass (for example Java).
  483. // "common_first": compile a for a Target that will work on all Targets supported by the OsClass
  484. // (same as "common"), plus a second Target for the preferred Target supported by the OsClass
  485. // (same as "first"). This is used for java_binary that produces a common .jar and a wrapper
  486. // executable script.
  487. //
  488. // Once the list of Targets is determined, the module is split into a variant for each Target.
  489. //
  490. // Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
  491. // but will have a common Target that is expected to handle all other selected Targets via ctx.MultiTargets().
  492. func archMutator(bpctx blueprint.BottomUpMutatorContext) {
  493. var module Module
  494. var ok bool
  495. if module, ok = bpctx.Module().(Module); !ok {
  496. if bootstrap.IsBootstrapModule(bpctx.Module()) {
  497. // Bootstrap Go modules are always the build architecture.
  498. bpctx.CreateVariations(bpctx.Config().(Config).BuildOSTarget.ArchVariation())
  499. }
  500. return
  501. }
  502. // Bootstrap Go module support above requires this mutator to be a
  503. // blueprint.BottomUpMutatorContext because android.BottomUpMutatorContext
  504. // filters out non-Soong modules. Now that we've handled them, create a
  505. // normal android.BottomUpMutatorContext.
  506. mctx := bottomUpMutatorContextFactory(bpctx, module, false, false)
  507. base := module.base()
  508. if !base.ArchSpecific() {
  509. return
  510. }
  511. os := base.commonProperties.CompileOS
  512. if os == CommonOS {
  513. // Make sure that the target related properties are initialized for the
  514. // CommonOS variant.
  515. addTargetProperties(module, commonTargetMap[os.Name], nil, true)
  516. // Do not create arch specific variants for the CommonOS variant.
  517. return
  518. }
  519. osTargets := mctx.Config().Targets[os]
  520. image := base.commonProperties.ImageVariation
  521. // Filter NativeBridge targets unless they are explicitly supported.
  522. // Skip creating native bridge variants for non-core modules.
  523. if os == Android &&
  524. !(Bool(base.commonProperties.Native_bridge_supported) && image == CoreVariation) {
  525. var targets []Target
  526. for _, t := range osTargets {
  527. if !t.NativeBridge {
  528. targets = append(targets, t)
  529. }
  530. }
  531. osTargets = targets
  532. }
  533. // only the primary arch in the ramdisk / vendor_ramdisk / recovery partition
  534. if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk() || module.InstallInVendorRamdisk()) {
  535. osTargets = []Target{osTargets[0]}
  536. }
  537. // Some modules want compile_multilib: "first" to mean 32-bit, not 64-bit.
  538. // This is used for HOST_PREFER_32_BIT=true support for Art modules.
  539. prefer32 := false
  540. if base.prefer32 != nil {
  541. prefer32 = base.prefer32(mctx, base, os)
  542. }
  543. if os == Windows {
  544. // Windows builds always prefer 32-bit
  545. prefer32 = true
  546. }
  547. // Determine the multilib selection for this module.
  548. multilib, extraMultilib := decodeMultilib(base, os.Class)
  549. // Convert the multilib selection into a list of Targets.
  550. targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
  551. if err != nil {
  552. mctx.ModuleErrorf("%s", err.Error())
  553. }
  554. // If the module is using extraMultilib, decode the extraMultilib selection into
  555. // a separate list of Targets.
  556. var multiTargets []Target
  557. if extraMultilib != "" {
  558. multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
  559. if err != nil {
  560. mctx.ModuleErrorf("%s", err.Error())
  561. }
  562. }
  563. // Recovery is always the primary architecture, filter out any other architectures.
  564. // Common arch is also allowed
  565. if image == RecoveryVariation {
  566. primaryArch := mctx.Config().DevicePrimaryArchType()
  567. targets = filterToArch(targets, primaryArch, Common)
  568. multiTargets = filterToArch(multiTargets, primaryArch, Common)
  569. }
  570. // If there are no supported targets disable the module.
  571. if len(targets) == 0 {
  572. base.Disable()
  573. return
  574. }
  575. // Convert the targets into a list of arch variation names.
  576. targetNames := make([]string, len(targets))
  577. for i, target := range targets {
  578. targetNames[i] = target.ArchVariation()
  579. }
  580. // Create the variations, annotate each one with which Target it was created for, and
  581. // squash the appropriate arch-specific properties into the top level properties.
  582. modules := mctx.CreateVariations(targetNames...)
  583. for i, m := range modules {
  584. addTargetProperties(m, targets[i], multiTargets, i == 0)
  585. m.base().setArchProperties(mctx)
  586. }
  587. }
  588. // addTargetProperties annotates a variant with the Target is is being compiled for, the list
  589. // of additional Targets it is supporting (if any), and whether it is the primary Target for
  590. // the module.
  591. func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
  592. m.base().commonProperties.CompileTarget = target
  593. m.base().commonProperties.CompileMultiTargets = multiTargets
  594. m.base().commonProperties.CompilePrimary = primaryTarget
  595. }
  596. // decodeMultilib returns the appropriate compile_multilib property for the module, or the default
  597. // multilib from the factory's call to InitAndroidArchModule if none was set. For modules that
  598. // called InitAndroidMultiTargetsArchModule it always returns "common" for multilib, and returns
  599. // the actual multilib in extraMultilib.
  600. func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
  601. // First check the "android.compile_multilib" or "host.compile_multilib" properties.
  602. switch class {
  603. case Device:
  604. multilib = String(base.commonProperties.Target.Android.Compile_multilib)
  605. case Host:
  606. multilib = String(base.commonProperties.Target.Host.Compile_multilib)
  607. }
  608. // If those aren't set, try the "compile_multilib" property.
  609. if multilib == "" {
  610. multilib = String(base.commonProperties.Compile_multilib)
  611. }
  612. // If that wasn't set, use the default multilib set by the factory.
  613. if multilib == "" {
  614. multilib = base.commonProperties.Default_multilib
  615. }
  616. if base.commonProperties.UseTargetVariants {
  617. return multilib, ""
  618. } else {
  619. // For app modules a single arch variant will be created per OS class which is expected to handle all the
  620. // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
  621. if multilib == base.commonProperties.Default_multilib {
  622. multilib = "first"
  623. }
  624. return base.commonProperties.Default_multilib, multilib
  625. }
  626. }
  627. // filterToArch takes a list of Targets and an ArchType, and returns a modified list that contains
  628. // only Targets that have the specified ArchTypes.
  629. func filterToArch(targets []Target, archs ...ArchType) []Target {
  630. for i := 0; i < len(targets); i++ {
  631. found := false
  632. for _, arch := range archs {
  633. if targets[i].Arch.ArchType == arch {
  634. found = true
  635. break
  636. }
  637. }
  638. if !found {
  639. targets = append(targets[:i], targets[i+1:]...)
  640. i--
  641. }
  642. }
  643. return targets
  644. }
  645. // archPropRoot is a struct type used as the top level of the arch-specific properties. It
  646. // contains the "arch", "multilib", and "target" property structs. It is used to split up the
  647. // property structs to limit how much is allocated when a single arch-specific property group is
  648. // used. The types are interface{} because they will hold instances of runtime-created types.
  649. type archPropRoot struct {
  650. Arch, Multilib, Target interface{}
  651. }
  652. // archPropTypeDesc holds the runtime-created types for the property structs to instantiate to
  653. // create an archPropRoot property struct.
  654. type archPropTypeDesc struct {
  655. arch, multilib, target reflect.Type
  656. }
  657. // createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
  658. // returns lists of reflect.Types that contains the arch-variant properties inside structs for each
  659. // arch, multilib and target property.
  660. //
  661. // This is a relatively expensive operation, so the results are cached in the global
  662. // archPropTypeMap. It is constructed entirely based on compile-time data, so there is no need
  663. // to isolate the results between multiple tests running in parallel.
  664. func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
  665. // Each property struct shard will be nested many times under the runtime generated arch struct,
  666. // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
  667. // 97 times now, which may grow in the future, plus there is some overhead for the containing
  668. // type. This number may need to be reduced if too many are added, but reducing it too far
  669. // could cause problems if a single deeply nested property no longer fits in the name.
  670. const maxArchTypeNameSize = 500
  671. // Convert the type to a new set of types that contains only the arch-specific properties
  672. // (those that are tagged with `android:"arch_specific"`), and sharded into multiple types
  673. // to keep the runtime-generated names under the limit.
  674. propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
  675. // If the type has no arch-specific properties there is nothing to do.
  676. if len(propShards) == 0 {
  677. return nil
  678. }
  679. var ret []archPropTypeDesc
  680. for _, props := range propShards {
  681. // variantFields takes a list of variant property field names and returns a list the
  682. // StructFields with the names and the type of the current shard.
  683. variantFields := func(names []string) []reflect.StructField {
  684. ret := make([]reflect.StructField, len(names))
  685. for i, name := range names {
  686. ret[i].Name = name
  687. ret[i].Type = props
  688. }
  689. return ret
  690. }
  691. // Create a type that contains the properties in this shard repeated for each
  692. // architecture, architecture variant, and architecture feature.
  693. archFields := make([]reflect.StructField, len(archTypeList))
  694. for i, arch := range archTypeList {
  695. var variants []string
  696. for _, archVariant := range archVariants[arch] {
  697. archVariant := variantReplacer.Replace(archVariant)
  698. variants = append(variants, proptools.FieldNameForProperty(archVariant))
  699. }
  700. for _, feature := range archFeatures[arch] {
  701. feature := variantReplacer.Replace(feature)
  702. variants = append(variants, proptools.FieldNameForProperty(feature))
  703. }
  704. // Create the StructFields for each architecture variant architecture feature
  705. // (e.g. "arch.arm.cortex-a53" or "arch.arm.neon").
  706. fields := variantFields(variants)
  707. // Create the StructField for the architecture itself (e.g. "arch.arm"). The special
  708. // "BlueprintEmbed" name is used by Blueprint to put the properties in the
  709. // parent struct.
  710. fields = append([]reflect.StructField{{
  711. Name: "BlueprintEmbed",
  712. Type: props,
  713. Anonymous: true,
  714. }}, fields...)
  715. archFields[i] = reflect.StructField{
  716. Name: arch.Field,
  717. Type: reflect.StructOf(fields),
  718. }
  719. }
  720. // Create the type of the "arch" property struct for this shard.
  721. archType := reflect.StructOf(archFields)
  722. // Create the type for the "multilib" property struct for this shard, containing the
  723. // "multilib.lib32" and "multilib.lib64" property structs.
  724. multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
  725. // Start with a list of the special targets
  726. targets := []string{
  727. "Host",
  728. "Android64",
  729. "Android32",
  730. "Bionic",
  731. "Linux",
  732. "Not_windows",
  733. "Arm_on_x86",
  734. "Arm_on_x86_64",
  735. "Native_bridge",
  736. }
  737. for _, os := range OsTypeList {
  738. // Add all the OSes.
  739. targets = append(targets, os.Field)
  740. // Add the OS/Arch combinations, e.g. "android_arm64".
  741. for _, archType := range osArchTypeMap[os] {
  742. targets = append(targets, os.Field+"_"+archType.Name)
  743. // Also add the special "linux_<arch>" and "bionic_<arch>" property structs.
  744. if os.Linux() {
  745. target := "Linux_" + archType.Name
  746. if !InList(target, targets) {
  747. targets = append(targets, target)
  748. }
  749. }
  750. if os.Bionic() {
  751. target := "Bionic_" + archType.Name
  752. if !InList(target, targets) {
  753. targets = append(targets, target)
  754. }
  755. }
  756. }
  757. }
  758. // Create the type for the "target" property struct for this shard.
  759. targetType := reflect.StructOf(variantFields(targets))
  760. // Return a descriptor of the 3 runtime-created types.
  761. ret = append(ret, archPropTypeDesc{
  762. arch: reflect.PtrTo(archType),
  763. multilib: reflect.PtrTo(multilibType),
  764. target: reflect.PtrTo(targetType),
  765. })
  766. }
  767. return ret
  768. }
  769. // variantReplacer converts architecture variant or architecture feature names into names that
  770. // are valid for an Android.bp file.
  771. var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
  772. // filterArchStruct returns true if the given field is an architecture specific property.
  773. func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
  774. if proptools.HasTag(field, "android", "arch_variant") {
  775. // The arch_variant field isn't necessary past this point
  776. // Instead of wasting space, just remove it. Go also has a
  777. // 16-bit limit on structure name length. The name is constructed
  778. // based on the Go source representation of the structure, so
  779. // the tag names count towards that length.
  780. androidTag := field.Tag.Get("android")
  781. values := strings.Split(androidTag, ",")
  782. if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
  783. panic(fmt.Errorf("unexpected tag format %q", field.Tag))
  784. }
  785. // these tags don't need to be present in the runtime generated struct type.
  786. values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
  787. if len(values) > 0 {
  788. panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
  789. }
  790. field.Tag = ""
  791. return true, field
  792. }
  793. return false, field
  794. }
  795. // archPropTypeMap contains a cache of the results of createArchPropTypeDesc for each type. It is
  796. // shared across all Contexts, but is constructed based only on compile-time information so there
  797. // is no risk of contaminating one Context with data from another.
  798. var archPropTypeMap OncePer
  799. // initArchModule adds the architecture-specific property structs to a Module.
  800. func initArchModule(m Module) {
  801. base := m.base()
  802. // Store the original list of top level property structs
  803. base.generalProperties = m.GetProperties()
  804. for _, properties := range base.generalProperties {
  805. propertiesValue := reflect.ValueOf(properties)
  806. t := propertiesValue.Type()
  807. if propertiesValue.Kind() != reflect.Ptr {
  808. panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
  809. propertiesValue.Interface()))
  810. }
  811. propertiesValue = propertiesValue.Elem()
  812. if propertiesValue.Kind() != reflect.Struct {
  813. panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
  814. propertiesValue.Interface()))
  815. }
  816. // Get or create the arch-specific property struct types for this property struct type.
  817. archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
  818. return createArchPropTypeDesc(t)
  819. }).([]archPropTypeDesc)
  820. // Instantiate one of each arch-specific property struct type and add it to the
  821. // properties for the Module.
  822. var archProperties []interface{}
  823. for _, t := range archPropTypes {
  824. archProperties = append(archProperties, &archPropRoot{
  825. Arch: reflect.Zero(t.arch).Interface(),
  826. Multilib: reflect.Zero(t.multilib).Interface(),
  827. Target: reflect.Zero(t.target).Interface(),
  828. })
  829. }
  830. base.archProperties = append(base.archProperties, archProperties)
  831. m.AddProperties(archProperties...)
  832. }
  833. // Update the list of properties that can be set by a defaults module or a call to
  834. // AppendMatchingProperties or PrependMatchingProperties.
  835. base.customizableProperties = m.GetProperties()
  836. }
  837. // appendProperties squashes properties from the given field of the given src property struct
  838. // into the dst property struct. Returns the reflect.Value of the field in the src property
  839. // struct to be used for further appendProperties calls on fields of that property struct.
  840. func (m *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
  841. dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
  842. // Step into non-nil pointers to structs in the src value.
  843. if src.Kind() == reflect.Ptr {
  844. if src.IsNil() {
  845. return src
  846. }
  847. src = src.Elem()
  848. }
  849. // Find the requested field in the src struct.
  850. src = src.FieldByName(field)
  851. if !src.IsValid() {
  852. ctx.ModuleErrorf("field %q does not exist", srcPrefix)
  853. return src
  854. }
  855. // Save the value of the field in the src struct to return.
  856. ret := src
  857. // If the value of the field is a struct (as opposed to a pointer to a struct) then step
  858. // into the BlueprintEmbed field.
  859. if src.Kind() == reflect.Struct {
  860. src = src.FieldByName("BlueprintEmbed")
  861. }
  862. // order checks the `android:"variant_prepend"` tag to handle properties where the
  863. // arch-specific value needs to come before the generic value, for example for lists of
  864. // include directories.
  865. order := func(property string,
  866. dstField, srcField reflect.StructField,
  867. dstValue, srcValue interface{}) (proptools.Order, error) {
  868. if proptools.HasTag(dstField, "android", "variant_prepend") {
  869. return proptools.Prepend, nil
  870. } else {
  871. return proptools.Append, nil
  872. }
  873. }
  874. // Squash the located property struct into the destination property struct.
  875. err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
  876. if err != nil {
  877. if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
  878. ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
  879. } else {
  880. panic(err)
  881. }
  882. }
  883. return ret
  884. }
  885. // Squash the appropriate OS-specific property structs into the matching top level property structs
  886. // based on the CompileOS value that was annotated on the variant.
  887. func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
  888. os := m.commonProperties.CompileOS
  889. for i := range m.generalProperties {
  890. genProps := m.generalProperties[i]
  891. if m.archProperties[i] == nil {
  892. continue
  893. }
  894. for _, archProperties := range m.archProperties[i] {
  895. archPropValues := reflect.ValueOf(archProperties).Elem()
  896. targetProp := archPropValues.FieldByName("Target").Elem()
  897. // Handle host-specific properties in the form:
  898. // target: {
  899. // host: {
  900. // key: value,
  901. // },
  902. // },
  903. if os.Class == Host {
  904. field := "Host"
  905. prefix := "target.host"
  906. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  907. }
  908. // Handle target OS generalities of the form:
  909. // target: {
  910. // bionic: {
  911. // key: value,
  912. // },
  913. // }
  914. if os.Linux() {
  915. field := "Linux"
  916. prefix := "target.linux"
  917. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  918. }
  919. if os.Bionic() {
  920. field := "Bionic"
  921. prefix := "target.bionic"
  922. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  923. }
  924. // Handle target OS properties in the form:
  925. // target: {
  926. // linux_glibc: {
  927. // key: value,
  928. // },
  929. // not_windows: {
  930. // key: value,
  931. // },
  932. // android {
  933. // key: value,
  934. // },
  935. // },
  936. field := os.Field
  937. prefix := "target." + os.Name
  938. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  939. if os.Class == Host && os != Windows {
  940. field := "Not_windows"
  941. prefix := "target.not_windows"
  942. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  943. }
  944. // Handle 64-bit device properties in the form:
  945. // target {
  946. // android64 {
  947. // key: value,
  948. // },
  949. // android32 {
  950. // key: value,
  951. // },
  952. // },
  953. // WARNING: this is probably not what you want to use in your blueprints file, it selects
  954. // options for all targets on a device that supports 64-bit binaries, not just the targets
  955. // that are being compiled for 64-bit. Its expected use case is binaries like linker and
  956. // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
  957. if os.Class == Device {
  958. if ctx.Config().Android64() {
  959. field := "Android64"
  960. prefix := "target.android64"
  961. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  962. } else {
  963. field := "Android32"
  964. prefix := "target.android32"
  965. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  966. }
  967. }
  968. }
  969. }
  970. }
  971. // Squash the appropriate arch-specific property structs into the matching top level property
  972. // structs based on the CompileTarget value that was annotated on the variant.
  973. func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
  974. arch := m.Arch()
  975. os := m.Os()
  976. for i := range m.generalProperties {
  977. genProps := m.generalProperties[i]
  978. if m.archProperties[i] == nil {
  979. continue
  980. }
  981. for _, archProperties := range m.archProperties[i] {
  982. archPropValues := reflect.ValueOf(archProperties).Elem()
  983. archProp := archPropValues.FieldByName("Arch").Elem()
  984. multilibProp := archPropValues.FieldByName("Multilib").Elem()
  985. targetProp := archPropValues.FieldByName("Target").Elem()
  986. // Handle arch-specific properties in the form:
  987. // arch: {
  988. // arm64: {
  989. // key: value,
  990. // },
  991. // },
  992. t := arch.ArchType
  993. if arch.ArchType != Common {
  994. field := proptools.FieldNameForProperty(t.Name)
  995. prefix := "arch." + t.Name
  996. archStruct := m.appendProperties(ctx, genProps, archProp, field, prefix)
  997. // Handle arch-variant-specific properties in the form:
  998. // arch: {
  999. // variant: {
  1000. // key: value,
  1001. // },
  1002. // },
  1003. v := variantReplacer.Replace(arch.ArchVariant)
  1004. if v != "" {
  1005. field := proptools.FieldNameForProperty(v)
  1006. prefix := "arch." + t.Name + "." + v
  1007. m.appendProperties(ctx, genProps, archStruct, field, prefix)
  1008. }
  1009. // Handle cpu-variant-specific properties in the form:
  1010. // arch: {
  1011. // variant: {
  1012. // key: value,
  1013. // },
  1014. // },
  1015. if arch.CpuVariant != arch.ArchVariant {
  1016. c := variantReplacer.Replace(arch.CpuVariant)
  1017. if c != "" {
  1018. field := proptools.FieldNameForProperty(c)
  1019. prefix := "arch." + t.Name + "." + c
  1020. m.appendProperties(ctx, genProps, archStruct, field, prefix)
  1021. }
  1022. }
  1023. // Handle arch-feature-specific properties in the form:
  1024. // arch: {
  1025. // feature: {
  1026. // key: value,
  1027. // },
  1028. // },
  1029. for _, feature := range arch.ArchFeatures {
  1030. field := proptools.FieldNameForProperty(feature)
  1031. prefix := "arch." + t.Name + "." + feature
  1032. m.appendProperties(ctx, genProps, archStruct, field, prefix)
  1033. }
  1034. // Handle multilib-specific properties in the form:
  1035. // multilib: {
  1036. // lib32: {
  1037. // key: value,
  1038. // },
  1039. // },
  1040. field = proptools.FieldNameForProperty(t.Multilib)
  1041. prefix = "multilib." + t.Multilib
  1042. m.appendProperties(ctx, genProps, multilibProp, field, prefix)
  1043. }
  1044. // Handle combined OS-feature and arch specific properties in the form:
  1045. // target: {
  1046. // bionic_x86: {
  1047. // key: value,
  1048. // },
  1049. // }
  1050. if os.Linux() && arch.ArchType != Common {
  1051. field := "Linux_" + arch.ArchType.Name
  1052. prefix := "target.linux_" + arch.ArchType.Name
  1053. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1054. }
  1055. if os.Bionic() && arch.ArchType != Common {
  1056. field := "Bionic_" + t.Name
  1057. prefix := "target.bionic_" + t.Name
  1058. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1059. }
  1060. // Handle combined OS and arch specific properties in the form:
  1061. // target: {
  1062. // linux_glibc_x86: {
  1063. // key: value,
  1064. // },
  1065. // linux_glibc_arm: {
  1066. // key: value,
  1067. // },
  1068. // android_arm {
  1069. // key: value,
  1070. // },
  1071. // android_x86 {
  1072. // key: value,
  1073. // },
  1074. // },
  1075. if arch.ArchType != Common {
  1076. field := os.Field + "_" + t.Name
  1077. prefix := "target." + os.Name + "_" + t.Name
  1078. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1079. }
  1080. // Handle arm on x86 properties in the form:
  1081. // target {
  1082. // arm_on_x86 {
  1083. // key: value,
  1084. // },
  1085. // arm_on_x86_64 {
  1086. // key: value,
  1087. // },
  1088. // },
  1089. if os.Class == Device {
  1090. if arch.ArchType == X86 && (hasArmAbi(arch) ||
  1091. hasArmAndroidArch(ctx.Config().Targets[Android])) {
  1092. field := "Arm_on_x86"
  1093. prefix := "target.arm_on_x86"
  1094. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1095. }
  1096. if arch.ArchType == X86_64 && (hasArmAbi(arch) ||
  1097. hasArmAndroidArch(ctx.Config().Targets[Android])) {
  1098. field := "Arm_on_x86_64"
  1099. prefix := "target.arm_on_x86_64"
  1100. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1101. }
  1102. if os == Android && m.Target().NativeBridge == NativeBridgeEnabled {
  1103. field := "Native_bridge"
  1104. prefix := "target.native_bridge"
  1105. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1106. }
  1107. }
  1108. }
  1109. }
  1110. }
  1111. // Convert the arch product variables into a list of targets for each OsType.
  1112. func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
  1113. variables := config.productVariables
  1114. targets := make(map[OsType][]Target)
  1115. var targetErr error
  1116. addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string,
  1117. nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string,
  1118. nativeBridgeRelativePath *string) {
  1119. if targetErr != nil {
  1120. return
  1121. }
  1122. arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
  1123. if err != nil {
  1124. targetErr = err
  1125. return
  1126. }
  1127. nativeBridgeRelativePathStr := String(nativeBridgeRelativePath)
  1128. nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName)
  1129. // Use guest arch as relative install path by default
  1130. if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
  1131. nativeBridgeRelativePathStr = arch.ArchType.String()
  1132. }
  1133. // A target is considered as HostCross if it's a host target which can't run natively on
  1134. // the currently configured build machine (either because the OS is different or because of
  1135. // the unsupported arch)
  1136. hostCross := false
  1137. if os.Class == Host {
  1138. var osSupported bool
  1139. if os == BuildOs {
  1140. osSupported = true
  1141. } else if BuildOs.Linux() && os.Linux() {
  1142. // LinuxBionic and Linux are compatible
  1143. osSupported = true
  1144. } else {
  1145. osSupported = false
  1146. }
  1147. var archSupported bool
  1148. if arch.ArchType == Common {
  1149. archSupported = true
  1150. } else if arch.ArchType.Name == *variables.HostArch {
  1151. archSupported = true
  1152. } else if variables.HostSecondaryArch != nil && arch.ArchType.Name == *variables.HostSecondaryArch {
  1153. archSupported = true
  1154. } else {
  1155. archSupported = false
  1156. }
  1157. if !osSupported || !archSupported {
  1158. hostCross = true
  1159. }
  1160. }
  1161. targets[os] = append(targets[os],
  1162. Target{
  1163. Os: os,
  1164. Arch: arch,
  1165. NativeBridge: nativeBridgeEnabled,
  1166. NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
  1167. NativeBridgeRelativePath: nativeBridgeRelativePathStr,
  1168. HostCross: hostCross,
  1169. })
  1170. }
  1171. if variables.HostArch == nil {
  1172. return nil, fmt.Errorf("No host primary architecture set")
  1173. }
  1174. // The primary host target, which must always exist.
  1175. addTarget(BuildOs, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
  1176. // An optional secondary host target.
  1177. if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
  1178. addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
  1179. }
  1180. // Optional cross-compiled host targets, generally Windows.
  1181. if String(variables.CrossHost) != "" {
  1182. crossHostOs := osByName(*variables.CrossHost)
  1183. if crossHostOs == NoOsType {
  1184. return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
  1185. }
  1186. if String(variables.CrossHostArch) == "" {
  1187. return nil, fmt.Errorf("No cross-host primary architecture set")
  1188. }
  1189. // The primary cross-compiled host target.
  1190. addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
  1191. // An optional secondary cross-compiled host target.
  1192. if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
  1193. addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
  1194. }
  1195. }
  1196. // Optional device targets
  1197. if variables.DeviceArch != nil && *variables.DeviceArch != "" {
  1198. var target = Android
  1199. if Bool(variables.Fuchsia) {
  1200. target = Fuchsia
  1201. }
  1202. // The primary device target.
  1203. addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant,
  1204. variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
  1205. // An optional secondary device target.
  1206. if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
  1207. addTarget(Android, *variables.DeviceSecondaryArch,
  1208. variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
  1209. variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil)
  1210. }
  1211. // An optional NativeBridge device target.
  1212. if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
  1213. addTarget(Android, *variables.NativeBridgeArch,
  1214. variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant,
  1215. variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch,
  1216. variables.NativeBridgeRelativePath)
  1217. }
  1218. // An optional secondary NativeBridge device target.
  1219. if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
  1220. variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
  1221. addTarget(Android, *variables.NativeBridgeSecondaryArch,
  1222. variables.NativeBridgeSecondaryArchVariant,
  1223. variables.NativeBridgeSecondaryCpuVariant,
  1224. variables.NativeBridgeSecondaryAbi,
  1225. NativeBridgeEnabled,
  1226. variables.DeviceSecondaryArch,
  1227. variables.NativeBridgeSecondaryRelativePath)
  1228. }
  1229. }
  1230. if targetErr != nil {
  1231. return nil, targetErr
  1232. }
  1233. return targets, nil
  1234. }
  1235. // hasArmAbi returns true if arch has at least one arm ABI
  1236. func hasArmAbi(arch Arch) bool {
  1237. return PrefixInList(arch.Abi, "arm")
  1238. }
  1239. // hasArmArch returns true if targets has at least non-native_bridge arm Android arch
  1240. func hasArmAndroidArch(targets []Target) bool {
  1241. for _, target := range targets {
  1242. if target.Os == Android && target.Arch.ArchType == Arm {
  1243. return true
  1244. }
  1245. }
  1246. return false
  1247. }
  1248. // archConfig describes a built-in configuration.
  1249. type archConfig struct {
  1250. arch string
  1251. archVariant string
  1252. cpuVariant string
  1253. abi []string
  1254. }
  1255. // getNdkAbisConfig returns a list of archConfigs for the ABIs supported by the NDK.
  1256. func getNdkAbisConfig() []archConfig {
  1257. return []archConfig{
  1258. {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
  1259. {"arm64", "armv8-a-branchprot", "", []string{"arm64-v8a"}},
  1260. {"x86", "", "", []string{"x86"}},
  1261. {"x86_64", "", "", []string{"x86_64"}},
  1262. }
  1263. }
  1264. // getAmlAbisConfig returns a list of archConfigs for the ABIs supported by mainline modules.
  1265. func getAmlAbisConfig() []archConfig {
  1266. return []archConfig{
  1267. {"arm", "armv7-a-neon", "", []string{"armeabi-v7a"}},
  1268. {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
  1269. {"x86", "", "", []string{"x86"}},
  1270. {"x86_64", "", "", []string{"x86_64"}},
  1271. }
  1272. }
  1273. // decodeArchSettings converts a list of archConfigs into a list of Targets for the given OsType.
  1274. func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
  1275. var ret []Target
  1276. for _, config := range archConfigs {
  1277. arch, err := decodeArch(os, config.arch, &config.archVariant,
  1278. &config.cpuVariant, config.abi)
  1279. if err != nil {
  1280. return nil, err
  1281. }
  1282. ret = append(ret, Target{
  1283. Os: Android,
  1284. Arch: arch,
  1285. })
  1286. }
  1287. return ret, nil
  1288. }
  1289. // decodeArch converts a set of strings from product variables into an Arch struct.
  1290. func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
  1291. // Verify the arch is valid
  1292. archType, ok := archTypeMap[arch]
  1293. if !ok {
  1294. return Arch{}, fmt.Errorf("unknown arch %q", arch)
  1295. }
  1296. a := Arch{
  1297. ArchType: archType,
  1298. ArchVariant: String(archVariant),
  1299. CpuVariant: String(cpuVariant),
  1300. Abi: abi,
  1301. }
  1302. // Convert generic arch variants into the empty string.
  1303. if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
  1304. a.ArchVariant = ""
  1305. }
  1306. // Convert generic CPU variants into the empty string.
  1307. if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
  1308. a.CpuVariant = ""
  1309. }
  1310. // Filter empty ABIs out of the list.
  1311. for i := 0; i < len(a.Abi); i++ {
  1312. if a.Abi[i] == "" {
  1313. a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
  1314. i--
  1315. }
  1316. }
  1317. if a.ArchVariant == "" {
  1318. // Set ArchFeatures from the default arch features.
  1319. if featureMap, ok := defaultArchFeatureMap[os]; ok {
  1320. a.ArchFeatures = featureMap[archType]
  1321. }
  1322. } else {
  1323. // Set ArchFeatures from the arch type.
  1324. if featureMap, ok := archFeatureMap[archType]; ok {
  1325. a.ArchFeatures = featureMap[a.ArchVariant]
  1326. }
  1327. }
  1328. return a, nil
  1329. }
  1330. // filterMultilibTargets takes a list of Targets and a multilib value and returns a new list of
  1331. // Targets containing only those that have the given multilib value.
  1332. func filterMultilibTargets(targets []Target, multilib string) []Target {
  1333. var ret []Target
  1334. for _, t := range targets {
  1335. if t.Arch.ArchType.Multilib == multilib {
  1336. ret = append(ret, t)
  1337. }
  1338. }
  1339. return ret
  1340. }
  1341. // getCommonTargets returns the set of Os specific common architecture targets for each Os in a list
  1342. // of targets.
  1343. func getCommonTargets(targets []Target) []Target {
  1344. var ret []Target
  1345. set := make(map[string]bool)
  1346. for _, t := range targets {
  1347. if _, found := set[t.Os.String()]; !found {
  1348. set[t.Os.String()] = true
  1349. ret = append(ret, commonTargetMap[t.Os.String()])
  1350. }
  1351. }
  1352. return ret
  1353. }
  1354. // firstTarget takes a list of Targets and a list of multilib values and returns a list of Targets
  1355. // that contains zero or one Target for each OsType, selecting the one that matches the earliest
  1356. // filter.
  1357. func firstTarget(targets []Target, filters ...string) []Target {
  1358. // find the first target from each OS
  1359. var ret []Target
  1360. hasHost := false
  1361. set := make(map[OsType]bool)
  1362. for _, filter := range filters {
  1363. buildTargets := filterMultilibTargets(targets, filter)
  1364. for _, t := range buildTargets {
  1365. if _, found := set[t.Os]; !found {
  1366. hasHost = hasHost || (t.Os.Class == Host)
  1367. set[t.Os] = true
  1368. ret = append(ret, t)
  1369. }
  1370. }
  1371. }
  1372. return ret
  1373. }
  1374. // decodeMultilibTargets uses the module's multilib setting to select one or more targets from a
  1375. // list of Targets.
  1376. func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
  1377. var buildTargets []Target
  1378. switch multilib {
  1379. case "common":
  1380. buildTargets = getCommonTargets(targets)
  1381. case "common_first":
  1382. buildTargets = getCommonTargets(targets)
  1383. if prefer32 {
  1384. buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
  1385. } else {
  1386. buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
  1387. }
  1388. case "both":
  1389. if prefer32 {
  1390. buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
  1391. buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
  1392. } else {
  1393. buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
  1394. buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
  1395. }
  1396. case "32":
  1397. buildTargets = filterMultilibTargets(targets, "lib32")
  1398. case "64":
  1399. buildTargets = filterMultilibTargets(targets, "lib64")
  1400. case "first":
  1401. if prefer32 {
  1402. buildTargets = firstTarget(targets, "lib32", "lib64")
  1403. } else {
  1404. buildTargets = firstTarget(targets, "lib64", "lib32")
  1405. }
  1406. case "first_prefer32":
  1407. buildTargets = firstTarget(targets, "lib32", "lib64")
  1408. case "prefer32":
  1409. buildTargets = filterMultilibTargets(targets, "lib32")
  1410. if len(buildTargets) == 0 {
  1411. buildTargets = filterMultilibTargets(targets, "lib64")
  1412. }
  1413. default:
  1414. return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", "prefer32" or "first_prefer32" found %q`,
  1415. multilib)
  1416. }
  1417. return buildTargets, nil
  1418. }