arch.go 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  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/proptools"
  24. )
  25. const COMMON_VARIANT = "common"
  26. var (
  27. archTypeList []ArchType
  28. Arm = newArch("arm", "lib32")
  29. Arm64 = newArch("arm64", "lib64")
  30. X86 = newArch("x86", "lib32")
  31. X86_64 = newArch("x86_64", "lib64")
  32. Common = ArchType{
  33. Name: COMMON_VARIANT,
  34. }
  35. )
  36. var archTypeMap = map[string]ArchType{
  37. "arm": Arm,
  38. "arm64": Arm64,
  39. "x86": X86,
  40. "x86_64": X86_64,
  41. }
  42. /*
  43. Example blueprints file containing all variant property groups, with comment listing what type
  44. of variants get properties in that group:
  45. module {
  46. arch: {
  47. arm: {
  48. // Host or device variants with arm architecture
  49. },
  50. arm64: {
  51. // Host or device variants with arm64 architecture
  52. },
  53. x86: {
  54. // Host or device variants with x86 architecture
  55. },
  56. x86_64: {
  57. // Host or device variants with x86_64 architecture
  58. },
  59. },
  60. multilib: {
  61. lib32: {
  62. // Host or device variants for 32-bit architectures
  63. },
  64. lib64: {
  65. // Host or device variants for 64-bit architectures
  66. },
  67. },
  68. target: {
  69. android: {
  70. // Device variants
  71. },
  72. host: {
  73. // Host variants
  74. },
  75. linux_glibc: {
  76. // Linux host variants
  77. },
  78. darwin: {
  79. // Darwin host variants
  80. },
  81. windows: {
  82. // Windows host variants
  83. },
  84. not_windows: {
  85. // Non-windows host variants
  86. },
  87. },
  88. }
  89. */
  90. var archVariants = map[ArchType][]string{
  91. Arm: {
  92. "armv7-a",
  93. "armv7-a-neon",
  94. "armv8-a",
  95. "armv8-2a",
  96. "cortex-a7",
  97. "cortex-a8",
  98. "cortex-a9",
  99. "cortex-a15",
  100. "cortex-a53",
  101. "cortex-a53-a57",
  102. "cortex-a55",
  103. "cortex-a72",
  104. "cortex-a73",
  105. "cortex-a75",
  106. "cortex-a76",
  107. "krait",
  108. "kryo",
  109. "kryo385",
  110. "exynos-m1",
  111. "exynos-m2",
  112. },
  113. Arm64: {
  114. "armv8_a",
  115. "armv8_2a",
  116. "cortex-a53",
  117. "cortex-a55",
  118. "cortex-a72",
  119. "cortex-a73",
  120. "cortex-a75",
  121. "cortex-a76",
  122. "kryo",
  123. "kryo385",
  124. "exynos-m1",
  125. "exynos-m2",
  126. },
  127. X86: {
  128. "amberlake",
  129. "atom",
  130. "broadwell",
  131. "haswell",
  132. "icelake",
  133. "ivybridge",
  134. "kabylake",
  135. "sandybridge",
  136. "silvermont",
  137. "skylake",
  138. "stoneyridge",
  139. "tigerlake",
  140. "whiskeylake",
  141. "x86_64",
  142. },
  143. X86_64: {
  144. "amberlake",
  145. "broadwell",
  146. "haswell",
  147. "icelake",
  148. "ivybridge",
  149. "kabylake",
  150. "sandybridge",
  151. "silvermont",
  152. "skylake",
  153. "stoneyridge",
  154. "tigerlake",
  155. "whiskeylake",
  156. },
  157. }
  158. var archFeatures = map[ArchType][]string{
  159. Arm: {
  160. "neon",
  161. },
  162. X86: {
  163. "ssse3",
  164. "sse4",
  165. "sse4_1",
  166. "sse4_2",
  167. "aes_ni",
  168. "avx",
  169. "avx2",
  170. "avx512",
  171. "popcnt",
  172. "movbe",
  173. },
  174. X86_64: {
  175. "ssse3",
  176. "sse4",
  177. "sse4_1",
  178. "sse4_2",
  179. "aes_ni",
  180. "avx",
  181. "avx2",
  182. "avx512",
  183. "popcnt",
  184. },
  185. }
  186. var archFeatureMap = map[ArchType]map[string][]string{
  187. Arm: {
  188. "armv7-a-neon": {
  189. "neon",
  190. },
  191. "armv8-a": {
  192. "neon",
  193. },
  194. "armv8-2a": {
  195. "neon",
  196. },
  197. },
  198. X86: {
  199. "amberlake": {
  200. "ssse3",
  201. "sse4",
  202. "sse4_1",
  203. "sse4_2",
  204. "avx",
  205. "avx2",
  206. "aes_ni",
  207. "popcnt",
  208. },
  209. "atom": {
  210. "ssse3",
  211. "movbe",
  212. },
  213. "broadwell": {
  214. "ssse3",
  215. "sse4",
  216. "sse4_1",
  217. "sse4_2",
  218. "avx",
  219. "avx2",
  220. "aes_ni",
  221. "popcnt",
  222. },
  223. "haswell": {
  224. "ssse3",
  225. "sse4",
  226. "sse4_1",
  227. "sse4_2",
  228. "aes_ni",
  229. "avx",
  230. "popcnt",
  231. "movbe",
  232. },
  233. "icelake": {
  234. "ssse3",
  235. "sse4",
  236. "sse4_1",
  237. "sse4_2",
  238. "avx",
  239. "avx2",
  240. "avx512",
  241. "aes_ni",
  242. "popcnt",
  243. },
  244. "ivybridge": {
  245. "ssse3",
  246. "sse4",
  247. "sse4_1",
  248. "sse4_2",
  249. "aes_ni",
  250. "avx",
  251. "popcnt",
  252. },
  253. "kabylake": {
  254. "ssse3",
  255. "sse4",
  256. "sse4_1",
  257. "sse4_2",
  258. "avx",
  259. "avx2",
  260. "aes_ni",
  261. "popcnt",
  262. },
  263. "sandybridge": {
  264. "ssse3",
  265. "sse4",
  266. "sse4_1",
  267. "sse4_2",
  268. "popcnt",
  269. },
  270. "silvermont": {
  271. "ssse3",
  272. "sse4",
  273. "sse4_1",
  274. "sse4_2",
  275. "aes_ni",
  276. "popcnt",
  277. "movbe",
  278. },
  279. "skylake": {
  280. "ssse3",
  281. "sse4",
  282. "sse4_1",
  283. "sse4_2",
  284. "avx",
  285. "avx2",
  286. "avx512",
  287. "aes_ni",
  288. "popcnt",
  289. },
  290. "stoneyridge": {
  291. "ssse3",
  292. "sse4",
  293. "sse4_1",
  294. "sse4_2",
  295. "aes_ni",
  296. "avx",
  297. "avx2",
  298. "popcnt",
  299. "movbe",
  300. },
  301. "tigerlake": {
  302. "ssse3",
  303. "sse4",
  304. "sse4_1",
  305. "sse4_2",
  306. "avx",
  307. "avx2",
  308. "avx512",
  309. "aes_ni",
  310. "popcnt",
  311. },
  312. "whiskeylake": {
  313. "ssse3",
  314. "sse4",
  315. "sse4_1",
  316. "sse4_2",
  317. "avx",
  318. "avx2",
  319. "avx512",
  320. "aes_ni",
  321. "popcnt",
  322. },
  323. "x86_64": {
  324. "ssse3",
  325. "sse4",
  326. "sse4_1",
  327. "sse4_2",
  328. "popcnt",
  329. },
  330. },
  331. X86_64: {
  332. "amberlake": {
  333. "ssse3",
  334. "sse4",
  335. "sse4_1",
  336. "sse4_2",
  337. "avx",
  338. "avx2",
  339. "aes_ni",
  340. "popcnt",
  341. },
  342. "broadwell": {
  343. "ssse3",
  344. "sse4",
  345. "sse4_1",
  346. "sse4_2",
  347. "avx",
  348. "avx2",
  349. "aes_ni",
  350. "popcnt",
  351. },
  352. "haswell": {
  353. "ssse3",
  354. "sse4",
  355. "sse4_1",
  356. "sse4_2",
  357. "aes_ni",
  358. "avx",
  359. "popcnt",
  360. },
  361. "icelake": {
  362. "ssse3",
  363. "sse4",
  364. "sse4_1",
  365. "sse4_2",
  366. "avx",
  367. "avx2",
  368. "avx512",
  369. "aes_ni",
  370. "popcnt",
  371. },
  372. "ivybridge": {
  373. "ssse3",
  374. "sse4",
  375. "sse4_1",
  376. "sse4_2",
  377. "aes_ni",
  378. "avx",
  379. "popcnt",
  380. },
  381. "kabylake": {
  382. "ssse3",
  383. "sse4",
  384. "sse4_1",
  385. "sse4_2",
  386. "avx",
  387. "avx2",
  388. "aes_ni",
  389. "popcnt",
  390. },
  391. "sandybridge": {
  392. "ssse3",
  393. "sse4",
  394. "sse4_1",
  395. "sse4_2",
  396. "popcnt",
  397. },
  398. "silvermont": {
  399. "ssse3",
  400. "sse4",
  401. "sse4_1",
  402. "sse4_2",
  403. "aes_ni",
  404. "popcnt",
  405. },
  406. "skylake": {
  407. "ssse3",
  408. "sse4",
  409. "sse4_1",
  410. "sse4_2",
  411. "avx",
  412. "avx2",
  413. "avx512",
  414. "aes_ni",
  415. "popcnt",
  416. },
  417. "stoneyridge": {
  418. "ssse3",
  419. "sse4",
  420. "sse4_1",
  421. "sse4_2",
  422. "aes_ni",
  423. "avx",
  424. "avx2",
  425. "popcnt",
  426. },
  427. "tigerlake": {
  428. "ssse3",
  429. "sse4",
  430. "sse4_1",
  431. "sse4_2",
  432. "avx",
  433. "avx2",
  434. "avx512",
  435. "aes_ni",
  436. "popcnt",
  437. },
  438. "whiskeylake": {
  439. "ssse3",
  440. "sse4",
  441. "sse4_1",
  442. "sse4_2",
  443. "avx",
  444. "avx2",
  445. "avx512",
  446. "aes_ni",
  447. "popcnt",
  448. },
  449. },
  450. }
  451. var defaultArchFeatureMap = map[OsType]map[ArchType][]string{}
  452. func RegisterDefaultArchVariantFeatures(os OsType, arch ArchType, features ...string) {
  453. checkCalledFromInit()
  454. for _, feature := range features {
  455. if !InList(feature, archFeatures[arch]) {
  456. panic(fmt.Errorf("Invalid feature %q for arch %q variant \"\"", feature, arch))
  457. }
  458. }
  459. if defaultArchFeatureMap[os] == nil {
  460. defaultArchFeatureMap[os] = make(map[ArchType][]string)
  461. }
  462. defaultArchFeatureMap[os][arch] = features
  463. }
  464. // An Arch indicates a single CPU architecture.
  465. type Arch struct {
  466. ArchType ArchType
  467. ArchVariant string
  468. CpuVariant string
  469. Abi []string
  470. ArchFeatures []string
  471. }
  472. func (a Arch) String() string {
  473. s := a.ArchType.String()
  474. if a.ArchVariant != "" {
  475. s += "_" + a.ArchVariant
  476. }
  477. if a.CpuVariant != "" {
  478. s += "_" + a.CpuVariant
  479. }
  480. return s
  481. }
  482. type ArchType struct {
  483. Name string
  484. Field string
  485. Multilib string
  486. }
  487. func newArch(name, multilib string) ArchType {
  488. archType := ArchType{
  489. Name: name,
  490. Field: proptools.FieldNameForProperty(name),
  491. Multilib: multilib,
  492. }
  493. archTypeList = append(archTypeList, archType)
  494. return archType
  495. }
  496. func ArchTypeList() []ArchType {
  497. return append([]ArchType(nil), archTypeList...)
  498. }
  499. func (a ArchType) String() string {
  500. return a.Name
  501. }
  502. var _ encoding.TextMarshaler = ArchType{}
  503. func (a ArchType) MarshalText() ([]byte, error) {
  504. return []byte(strconv.Quote(a.String())), nil
  505. }
  506. var _ encoding.TextUnmarshaler = &ArchType{}
  507. func (a *ArchType) UnmarshalText(text []byte) error {
  508. if u, ok := archTypeMap[string(text)]; ok {
  509. *a = u
  510. return nil
  511. }
  512. return fmt.Errorf("unknown ArchType %q", text)
  513. }
  514. var BuildOs = func() OsType {
  515. switch runtime.GOOS {
  516. case "linux":
  517. return Linux
  518. case "darwin":
  519. return Darwin
  520. default:
  521. panic(fmt.Sprintf("unsupported OS: %s", runtime.GOOS))
  522. }
  523. }()
  524. var (
  525. osTypeList []OsType
  526. commonTargetMap = make(map[string]Target)
  527. NoOsType OsType
  528. Linux = NewOsType("linux_glibc", Host, false)
  529. Darwin = NewOsType("darwin", Host, false)
  530. LinuxBionic = NewOsType("linux_bionic", Host, false)
  531. Windows = NewOsType("windows", HostCross, true)
  532. Android = NewOsType("android", Device, false)
  533. Fuchsia = NewOsType("fuchsia", Device, false)
  534. // A pseudo OSType for a common os variant, which is OSType agnostic and which
  535. // has dependencies on all the OS variants.
  536. CommonOS = NewOsType("common_os", Generic, false)
  537. osArchTypeMap = map[OsType][]ArchType{
  538. Linux: []ArchType{X86, X86_64},
  539. LinuxBionic: []ArchType{X86_64},
  540. Darwin: []ArchType{X86_64},
  541. Windows: []ArchType{X86, X86_64},
  542. Android: []ArchType{Arm, Arm64, X86, X86_64},
  543. Fuchsia: []ArchType{Arm64, X86_64},
  544. }
  545. )
  546. type OsType struct {
  547. Name, Field string
  548. Class OsClass
  549. DefaultDisabled bool
  550. }
  551. type OsClass int
  552. const (
  553. Generic OsClass = iota
  554. Device
  555. Host
  556. HostCross
  557. )
  558. func (class OsClass) String() string {
  559. switch class {
  560. case Generic:
  561. return "generic"
  562. case Device:
  563. return "device"
  564. case Host:
  565. return "host"
  566. case HostCross:
  567. return "host cross"
  568. default:
  569. panic(fmt.Errorf("unknown class %d", class))
  570. }
  571. }
  572. func (os OsType) String() string {
  573. return os.Name
  574. }
  575. func (os OsType) Bionic() bool {
  576. return os == Android || os == LinuxBionic
  577. }
  578. func (os OsType) Linux() bool {
  579. return os == Android || os == Linux || os == LinuxBionic
  580. }
  581. func NewOsType(name string, class OsClass, defDisabled bool) OsType {
  582. os := OsType{
  583. Name: name,
  584. Field: strings.Title(name),
  585. Class: class,
  586. DefaultDisabled: defDisabled,
  587. }
  588. osTypeList = append(osTypeList, os)
  589. if _, found := commonTargetMap[name]; found {
  590. panic(fmt.Errorf("Found Os type duplicate during OsType registration: %q", name))
  591. } else {
  592. commonTargetMap[name] = Target{Os: os, Arch: Arch{ArchType: Common}}
  593. }
  594. return os
  595. }
  596. func osByName(name string) OsType {
  597. for _, os := range osTypeList {
  598. if os.Name == name {
  599. return os
  600. }
  601. }
  602. return NoOsType
  603. }
  604. type NativeBridgeSupport bool
  605. const (
  606. NativeBridgeDisabled NativeBridgeSupport = false
  607. NativeBridgeEnabled NativeBridgeSupport = true
  608. )
  609. type Target struct {
  610. Os OsType
  611. Arch Arch
  612. NativeBridge NativeBridgeSupport
  613. NativeBridgeHostArchName string
  614. NativeBridgeRelativePath string
  615. }
  616. func (target Target) String() string {
  617. return target.OsVariation() + "_" + target.ArchVariation()
  618. }
  619. func (target Target) OsVariation() string {
  620. return target.Os.String()
  621. }
  622. func (target Target) ArchVariation() string {
  623. var variation string
  624. if target.NativeBridge {
  625. variation = "native_bridge_"
  626. }
  627. variation += target.Arch.String()
  628. return variation
  629. }
  630. func (target Target) Variations() []blueprint.Variation {
  631. return []blueprint.Variation{
  632. {Mutator: "os", Variation: target.OsVariation()},
  633. {Mutator: "arch", Variation: target.ArchVariation()},
  634. }
  635. }
  636. func osMutator(mctx BottomUpMutatorContext) {
  637. var module Module
  638. var ok bool
  639. if module, ok = mctx.Module().(Module); !ok {
  640. return
  641. }
  642. base := module.base()
  643. if !base.ArchSpecific() {
  644. return
  645. }
  646. osClasses := base.OsClassSupported()
  647. var moduleOSList []OsType
  648. for _, os := range osTypeList {
  649. supportedClass := false
  650. for _, osClass := range osClasses {
  651. if os.Class == osClass {
  652. supportedClass = true
  653. }
  654. }
  655. if !supportedClass {
  656. continue
  657. }
  658. if len(mctx.Config().Targets[os]) == 0 {
  659. continue
  660. }
  661. moduleOSList = append(moduleOSList, os)
  662. }
  663. if len(moduleOSList) == 0 {
  664. base.Disable()
  665. return
  666. }
  667. osNames := make([]string, len(moduleOSList))
  668. for i, os := range moduleOSList {
  669. osNames[i] = os.String()
  670. }
  671. createCommonOSVariant := base.commonProperties.CreateCommonOSVariant
  672. if createCommonOSVariant {
  673. // A CommonOS variant was requested so add it to the list of OS's variants to
  674. // create. It needs to be added to the end because it needs to depend on the
  675. // the other variants in the list returned by CreateVariations(...) and inter
  676. // variant dependencies can only be created from a later variant in that list to
  677. // an earlier one. That is because variants are always processed in the order in
  678. // which they are returned from CreateVariations(...).
  679. osNames = append(osNames, CommonOS.Name)
  680. moduleOSList = append(moduleOSList, CommonOS)
  681. }
  682. modules := mctx.CreateVariations(osNames...)
  683. for i, m := range modules {
  684. m.base().commonProperties.CompileOS = moduleOSList[i]
  685. m.base().setOSProperties(mctx)
  686. }
  687. if createCommonOSVariant {
  688. // A CommonOS variant was requested so add dependencies from it (the last one in
  689. // the list) to the OS type specific variants.
  690. last := len(modules) - 1
  691. commonOSVariant := modules[last]
  692. commonOSVariant.base().commonProperties.CommonOSVariant = true
  693. for _, module := range modules[0:last] {
  694. // Ignore modules that are enabled. Note, this will only avoid adding
  695. // dependencies on OsType variants that are explicitly disabled in their
  696. // properties. The CommonOS variant will still depend on disabled variants
  697. // if they are disabled afterwards, e.g. in archMutator if
  698. if module.Enabled() {
  699. mctx.AddInterVariantDependency(commonOsToOsSpecificVariantTag, commonOSVariant, module)
  700. }
  701. }
  702. }
  703. }
  704. // Identifies the dependency from CommonOS variant to the os specific variants.
  705. type commonOSTag struct{ blueprint.BaseDependencyTag }
  706. var commonOsToOsSpecificVariantTag = commonOSTag{}
  707. // Get the OsType specific variants for the current CommonOS variant.
  708. //
  709. // The returned list will only contain enabled OsType specific variants of the
  710. // module referenced in the supplied context. An empty list is returned if there
  711. // are no enabled variants or the supplied context is not for an CommonOS
  712. // variant.
  713. func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
  714. var variants []Module
  715. mctx.VisitDirectDeps(func(m Module) {
  716. if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
  717. if m.Enabled() {
  718. variants = append(variants, m)
  719. }
  720. }
  721. })
  722. return variants
  723. }
  724. // archMutator splits a module into a variant for each Target requested by the module. Target selection
  725. // for a module is in three levels, OsClass, mulitlib, and then Target.
  726. // OsClass selection is determined by:
  727. // - The HostOrDeviceSupported value passed in to InitAndroidArchModule by the module type factory, which selects
  728. // whether the module type can compile for host, device or both.
  729. // - The host_supported and device_supported properties on the module.
  730. // If host is supported for the module, the Host and HostCross OsClasses are selected. If device is supported
  731. // for the module, the Device OsClass is selected.
  732. // Within each selected OsClass, the multilib selection is determined by:
  733. // - The compile_multilib property if it set (which may be overridden by target.android.compile_multilib or
  734. // target.host.compile_multilib).
  735. // - The default multilib passed to InitAndroidArchModule if compile_multilib was not set.
  736. // Valid multilib values include:
  737. // "both": compile for all Targets supported by the OsClass (generally x86_64 and x86, or arm64 and arm).
  738. // "first": compile for only a single preferred Target supported by the OsClass. This is generally x86_64 or arm64,
  739. // but may be arm for a 32-bit only build or a build with TARGET_PREFER_32_BIT=true set.
  740. // "32": compile for only a single 32-bit Target supported by the OsClass.
  741. // "64": compile for only a single 64-bit Target supported by the OsClass.
  742. // "common": compile a for a single Target that will work on all Targets suported by the OsClass (for example Java).
  743. //
  744. // Once the list of Targets is determined, the module is split into a variant for each Target.
  745. //
  746. // Modules can be initialized with InitAndroidMultiTargetsArchModule, in which case they will be split by OsClass,
  747. // but will have a common Target that is expected to handle all other selected Targets via ctx.MultiTargets().
  748. func archMutator(mctx BottomUpMutatorContext) {
  749. var module Module
  750. var ok bool
  751. if module, ok = mctx.Module().(Module); !ok {
  752. return
  753. }
  754. base := module.base()
  755. if !base.ArchSpecific() {
  756. return
  757. }
  758. os := base.commonProperties.CompileOS
  759. if os == CommonOS {
  760. // Make sure that the target related properties are initialized for the
  761. // CommonOS variant.
  762. addTargetProperties(module, commonTargetMap[os.Name], nil, true)
  763. // Do not create arch specific variants for the CommonOS variant.
  764. return
  765. }
  766. osTargets := mctx.Config().Targets[os]
  767. image := base.commonProperties.ImageVariation
  768. // Filter NativeBridge targets unless they are explicitly supported
  769. // Skip creating native bridge variants for vendor modules
  770. if os == Android &&
  771. !(Bool(base.commonProperties.Native_bridge_supported) && image == CoreVariation) {
  772. var targets []Target
  773. for _, t := range osTargets {
  774. if !t.NativeBridge {
  775. targets = append(targets, t)
  776. }
  777. }
  778. osTargets = targets
  779. }
  780. // only the primary arch in the ramdisk / recovery partition
  781. if os == Android && (module.InstallInRecovery() || module.InstallInRamdisk()) {
  782. osTargets = []Target{osTargets[0]}
  783. }
  784. prefer32 := false
  785. if base.prefer32 != nil {
  786. prefer32 = base.prefer32(mctx, base, os.Class)
  787. }
  788. multilib, extraMultilib := decodeMultilib(base, os.Class)
  789. targets, err := decodeMultilibTargets(multilib, osTargets, prefer32)
  790. if err != nil {
  791. mctx.ModuleErrorf("%s", err.Error())
  792. }
  793. var multiTargets []Target
  794. if extraMultilib != "" {
  795. multiTargets, err = decodeMultilibTargets(extraMultilib, osTargets, prefer32)
  796. if err != nil {
  797. mctx.ModuleErrorf("%s", err.Error())
  798. }
  799. }
  800. if image == RecoveryVariation {
  801. primaryArch := mctx.Config().DevicePrimaryArchType()
  802. targets = filterToArch(targets, primaryArch)
  803. multiTargets = filterToArch(multiTargets, primaryArch)
  804. }
  805. if len(targets) == 0 {
  806. base.Disable()
  807. return
  808. }
  809. targetNames := make([]string, len(targets))
  810. for i, target := range targets {
  811. targetNames[i] = target.ArchVariation()
  812. }
  813. modules := mctx.CreateVariations(targetNames...)
  814. for i, m := range modules {
  815. addTargetProperties(m, targets[i], multiTargets, i == 0)
  816. m.(Module).base().setArchProperties(mctx)
  817. }
  818. }
  819. func addTargetProperties(m Module, target Target, multiTargets []Target, primaryTarget bool) {
  820. m.base().commonProperties.CompileTarget = target
  821. m.base().commonProperties.CompileMultiTargets = multiTargets
  822. m.base().commonProperties.CompilePrimary = primaryTarget
  823. }
  824. func decodeMultilib(base *ModuleBase, class OsClass) (multilib, extraMultilib string) {
  825. switch class {
  826. case Device:
  827. multilib = String(base.commonProperties.Target.Android.Compile_multilib)
  828. case Host, HostCross:
  829. multilib = String(base.commonProperties.Target.Host.Compile_multilib)
  830. }
  831. if multilib == "" {
  832. multilib = String(base.commonProperties.Compile_multilib)
  833. }
  834. if multilib == "" {
  835. multilib = base.commonProperties.Default_multilib
  836. }
  837. if base.commonProperties.UseTargetVariants {
  838. return multilib, ""
  839. } else {
  840. // For app modules a single arch variant will be created per OS class which is expected to handle all the
  841. // selected arches. Return the common-type as multilib and any Android.bp provided multilib as extraMultilib
  842. if multilib == base.commonProperties.Default_multilib {
  843. multilib = "first"
  844. }
  845. return base.commonProperties.Default_multilib, multilib
  846. }
  847. }
  848. func filterToArch(targets []Target, arch ArchType) []Target {
  849. for i := 0; i < len(targets); i++ {
  850. if targets[i].Arch.ArchType != arch {
  851. targets = append(targets[:i], targets[i+1:]...)
  852. i--
  853. }
  854. }
  855. return targets
  856. }
  857. type archPropTypeDesc struct {
  858. arch, multilib, target reflect.Type
  859. }
  860. type archPropRoot struct {
  861. Arch, Multilib, Target interface{}
  862. }
  863. // createArchPropTypeDesc takes a reflect.Type that is either a struct or a pointer to a struct, and
  864. // returns lists of reflect.Types that contains the arch-variant properties inside structs for each
  865. // arch, multilib and target property.
  866. func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc {
  867. // Each property struct shard will be nested many times under the runtime generated arch struct,
  868. // which can hit the limit of 64kB for the name of runtime generated structs. They are nested
  869. // 97 times now, which may grow in the future, plus there is some overhead for the containing
  870. // type. This number may need to be reduced if too many are added, but reducing it too far
  871. // could cause problems if a single deeply nested property no longer fits in the name.
  872. const maxArchTypeNameSize = 500
  873. propShards, _ := proptools.FilterPropertyStructSharded(props, maxArchTypeNameSize, filterArchStruct)
  874. if len(propShards) == 0 {
  875. return nil
  876. }
  877. var ret []archPropTypeDesc
  878. for _, props := range propShards {
  879. variantFields := func(names []string) []reflect.StructField {
  880. ret := make([]reflect.StructField, len(names))
  881. for i, name := range names {
  882. ret[i].Name = name
  883. ret[i].Type = props
  884. }
  885. return ret
  886. }
  887. archFields := make([]reflect.StructField, len(archTypeList))
  888. for i, arch := range archTypeList {
  889. variants := []string{}
  890. for _, archVariant := range archVariants[arch] {
  891. archVariant := variantReplacer.Replace(archVariant)
  892. variants = append(variants, proptools.FieldNameForProperty(archVariant))
  893. }
  894. for _, feature := range archFeatures[arch] {
  895. feature := variantReplacer.Replace(feature)
  896. variants = append(variants, proptools.FieldNameForProperty(feature))
  897. }
  898. fields := variantFields(variants)
  899. fields = append([]reflect.StructField{{
  900. Name: "BlueprintEmbed",
  901. Type: props,
  902. Anonymous: true,
  903. }}, fields...)
  904. archFields[i] = reflect.StructField{
  905. Name: arch.Field,
  906. Type: reflect.StructOf(fields),
  907. }
  908. }
  909. archType := reflect.StructOf(archFields)
  910. multilibType := reflect.StructOf(variantFields([]string{"Lib32", "Lib64"}))
  911. targets := []string{
  912. "Host",
  913. "Android64",
  914. "Android32",
  915. "Bionic",
  916. "Linux",
  917. "Not_windows",
  918. "Arm_on_x86",
  919. "Arm_on_x86_64",
  920. }
  921. for _, os := range osTypeList {
  922. targets = append(targets, os.Field)
  923. for _, archType := range osArchTypeMap[os] {
  924. targets = append(targets, os.Field+"_"+archType.Name)
  925. if os.Linux() {
  926. target := "Linux_" + archType.Name
  927. if !InList(target, targets) {
  928. targets = append(targets, target)
  929. }
  930. }
  931. if os.Bionic() {
  932. target := "Bionic_" + archType.Name
  933. if !InList(target, targets) {
  934. targets = append(targets, target)
  935. }
  936. }
  937. }
  938. }
  939. targetType := reflect.StructOf(variantFields(targets))
  940. ret = append(ret, archPropTypeDesc{
  941. arch: reflect.PtrTo(archType),
  942. multilib: reflect.PtrTo(multilibType),
  943. target: reflect.PtrTo(targetType),
  944. })
  945. }
  946. return ret
  947. }
  948. func filterArchStruct(field reflect.StructField, prefix string) (bool, reflect.StructField) {
  949. if proptools.HasTag(field, "android", "arch_variant") {
  950. // The arch_variant field isn't necessary past this point
  951. // Instead of wasting space, just remove it. Go also has a
  952. // 16-bit limit on structure name length. The name is constructed
  953. // based on the Go source representation of the structure, so
  954. // the tag names count towards that length.
  955. androidTag := field.Tag.Get("android")
  956. values := strings.Split(androidTag, ",")
  957. if string(field.Tag) != `android:"`+strings.Join(values, ",")+`"` {
  958. panic(fmt.Errorf("unexpected tag format %q", field.Tag))
  959. }
  960. // these tags don't need to be present in the runtime generated struct type.
  961. values = RemoveListFromList(values, []string{"arch_variant", "variant_prepend", "path"})
  962. if len(values) > 0 {
  963. panic(fmt.Errorf("unknown tags %q in field %q", values, prefix+field.Name))
  964. }
  965. field.Tag = ""
  966. return true, field
  967. }
  968. return false, field
  969. }
  970. var archPropTypeMap OncePer
  971. func InitArchModule(m Module) {
  972. base := m.base()
  973. base.generalProperties = m.GetProperties()
  974. for _, properties := range base.generalProperties {
  975. propertiesValue := reflect.ValueOf(properties)
  976. t := propertiesValue.Type()
  977. if propertiesValue.Kind() != reflect.Ptr {
  978. panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
  979. propertiesValue.Interface()))
  980. }
  981. propertiesValue = propertiesValue.Elem()
  982. if propertiesValue.Kind() != reflect.Struct {
  983. panic(fmt.Errorf("properties must be a pointer to a struct, got %T",
  984. propertiesValue.Interface()))
  985. }
  986. archPropTypes := archPropTypeMap.Once(NewCustomOnceKey(t), func() interface{} {
  987. return createArchPropTypeDesc(t)
  988. }).([]archPropTypeDesc)
  989. var archProperties []interface{}
  990. for _, t := range archPropTypes {
  991. archProperties = append(archProperties, &archPropRoot{
  992. Arch: reflect.Zero(t.arch).Interface(),
  993. Multilib: reflect.Zero(t.multilib).Interface(),
  994. Target: reflect.Zero(t.target).Interface(),
  995. })
  996. }
  997. base.archProperties = append(base.archProperties, archProperties)
  998. m.AddProperties(archProperties...)
  999. }
  1000. base.customizableProperties = m.GetProperties()
  1001. }
  1002. var variantReplacer = strings.NewReplacer("-", "_", ".", "_")
  1003. func (m *ModuleBase) appendProperties(ctx BottomUpMutatorContext,
  1004. dst interface{}, src reflect.Value, field, srcPrefix string) reflect.Value {
  1005. if src.Kind() == reflect.Ptr {
  1006. if src.IsNil() {
  1007. return src
  1008. }
  1009. src = src.Elem()
  1010. }
  1011. src = src.FieldByName(field)
  1012. if !src.IsValid() {
  1013. ctx.ModuleErrorf("field %q does not exist", srcPrefix)
  1014. return src
  1015. }
  1016. ret := src
  1017. if src.Kind() == reflect.Struct {
  1018. src = src.FieldByName("BlueprintEmbed")
  1019. }
  1020. order := func(property string,
  1021. dstField, srcField reflect.StructField,
  1022. dstValue, srcValue interface{}) (proptools.Order, error) {
  1023. if proptools.HasTag(dstField, "android", "variant_prepend") {
  1024. return proptools.Prepend, nil
  1025. } else {
  1026. return proptools.Append, nil
  1027. }
  1028. }
  1029. err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, order)
  1030. if err != nil {
  1031. if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
  1032. ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
  1033. } else {
  1034. panic(err)
  1035. }
  1036. }
  1037. return ret
  1038. }
  1039. // Rewrite the module's properties structs to contain os-specific values.
  1040. func (m *ModuleBase) setOSProperties(ctx BottomUpMutatorContext) {
  1041. os := m.commonProperties.CompileOS
  1042. for i := range m.generalProperties {
  1043. genProps := m.generalProperties[i]
  1044. if m.archProperties[i] == nil {
  1045. continue
  1046. }
  1047. for _, archProperties := range m.archProperties[i] {
  1048. archPropValues := reflect.ValueOf(archProperties).Elem()
  1049. targetProp := archPropValues.FieldByName("Target").Elem()
  1050. // Handle host-specific properties in the form:
  1051. // target: {
  1052. // host: {
  1053. // key: value,
  1054. // },
  1055. // },
  1056. if os.Class == Host || os.Class == HostCross {
  1057. field := "Host"
  1058. prefix := "target.host"
  1059. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1060. }
  1061. // Handle target OS generalities of the form:
  1062. // target: {
  1063. // bionic: {
  1064. // key: value,
  1065. // },
  1066. // }
  1067. if os.Linux() {
  1068. field := "Linux"
  1069. prefix := "target.linux"
  1070. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1071. }
  1072. if os.Bionic() {
  1073. field := "Bionic"
  1074. prefix := "target.bionic"
  1075. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1076. }
  1077. // Handle target OS properties in the form:
  1078. // target: {
  1079. // linux_glibc: {
  1080. // key: value,
  1081. // },
  1082. // not_windows: {
  1083. // key: value,
  1084. // },
  1085. // android {
  1086. // key: value,
  1087. // },
  1088. // },
  1089. field := os.Field
  1090. prefix := "target." + os.Name
  1091. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1092. if (os.Class == Host || os.Class == HostCross) && os != Windows {
  1093. field := "Not_windows"
  1094. prefix := "target.not_windows"
  1095. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1096. }
  1097. // Handle 64-bit device properties in the form:
  1098. // target {
  1099. // android64 {
  1100. // key: value,
  1101. // },
  1102. // android32 {
  1103. // key: value,
  1104. // },
  1105. // },
  1106. // WARNING: this is probably not what you want to use in your blueprints file, it selects
  1107. // options for all targets on a device that supports 64-bit binaries, not just the targets
  1108. // that are being compiled for 64-bit. Its expected use case is binaries like linker and
  1109. // debuggerd that need to know when they are a 32-bit process running on a 64-bit device
  1110. if os.Class == Device {
  1111. if ctx.Config().Android64() {
  1112. field := "Android64"
  1113. prefix := "target.android64"
  1114. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1115. } else {
  1116. field := "Android32"
  1117. prefix := "target.android32"
  1118. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1119. }
  1120. }
  1121. }
  1122. }
  1123. }
  1124. // Rewrite the module's properties structs to contain arch-specific values.
  1125. func (m *ModuleBase) setArchProperties(ctx BottomUpMutatorContext) {
  1126. arch := m.Arch()
  1127. os := m.Os()
  1128. for i := range m.generalProperties {
  1129. genProps := m.generalProperties[i]
  1130. if m.archProperties[i] == nil {
  1131. continue
  1132. }
  1133. for _, archProperties := range m.archProperties[i] {
  1134. archPropValues := reflect.ValueOf(archProperties).Elem()
  1135. archProp := archPropValues.FieldByName("Arch").Elem()
  1136. multilibProp := archPropValues.FieldByName("Multilib").Elem()
  1137. targetProp := archPropValues.FieldByName("Target").Elem()
  1138. // Handle arch-specific properties in the form:
  1139. // arch: {
  1140. // arm64: {
  1141. // key: value,
  1142. // },
  1143. // },
  1144. t := arch.ArchType
  1145. if arch.ArchType != Common {
  1146. field := proptools.FieldNameForProperty(t.Name)
  1147. prefix := "arch." + t.Name
  1148. archStruct := m.appendProperties(ctx, genProps, archProp, field, prefix)
  1149. // Handle arch-variant-specific properties in the form:
  1150. // arch: {
  1151. // variant: {
  1152. // key: value,
  1153. // },
  1154. // },
  1155. v := variantReplacer.Replace(arch.ArchVariant)
  1156. if v != "" {
  1157. field := proptools.FieldNameForProperty(v)
  1158. prefix := "arch." + t.Name + "." + v
  1159. m.appendProperties(ctx, genProps, archStruct, field, prefix)
  1160. }
  1161. // Handle cpu-variant-specific properties in the form:
  1162. // arch: {
  1163. // variant: {
  1164. // key: value,
  1165. // },
  1166. // },
  1167. if arch.CpuVariant != arch.ArchVariant {
  1168. c := variantReplacer.Replace(arch.CpuVariant)
  1169. if c != "" {
  1170. field := proptools.FieldNameForProperty(c)
  1171. prefix := "arch." + t.Name + "." + c
  1172. m.appendProperties(ctx, genProps, archStruct, field, prefix)
  1173. }
  1174. }
  1175. // Handle arch-feature-specific properties in the form:
  1176. // arch: {
  1177. // feature: {
  1178. // key: value,
  1179. // },
  1180. // },
  1181. for _, feature := range arch.ArchFeatures {
  1182. field := proptools.FieldNameForProperty(feature)
  1183. prefix := "arch." + t.Name + "." + feature
  1184. m.appendProperties(ctx, genProps, archStruct, field, prefix)
  1185. }
  1186. // Handle multilib-specific properties in the form:
  1187. // multilib: {
  1188. // lib32: {
  1189. // key: value,
  1190. // },
  1191. // },
  1192. field = proptools.FieldNameForProperty(t.Multilib)
  1193. prefix = "multilib." + t.Multilib
  1194. m.appendProperties(ctx, genProps, multilibProp, field, prefix)
  1195. }
  1196. // Handle combined OS-feature and arch specific properties in the form:
  1197. // target: {
  1198. // bionic_x86: {
  1199. // key: value,
  1200. // },
  1201. // }
  1202. if os.Linux() && arch.ArchType != Common {
  1203. field := "Linux_" + arch.ArchType.Name
  1204. prefix := "target.linux_" + arch.ArchType.Name
  1205. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1206. }
  1207. if os.Bionic() && arch.ArchType != Common {
  1208. field := "Bionic_" + t.Name
  1209. prefix := "target.bionic_" + t.Name
  1210. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1211. }
  1212. // Handle combined OS and arch specific properties in the form:
  1213. // target: {
  1214. // linux_glibc_x86: {
  1215. // key: value,
  1216. // },
  1217. // linux_glibc_arm: {
  1218. // key: value,
  1219. // },
  1220. // android_arm {
  1221. // key: value,
  1222. // },
  1223. // android_x86 {
  1224. // key: value,
  1225. // },
  1226. // },
  1227. if arch.ArchType != Common {
  1228. field := os.Field + "_" + t.Name
  1229. prefix := "target." + os.Name + "_" + t.Name
  1230. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1231. }
  1232. // Handle arm on x86 properties in the form:
  1233. // target {
  1234. // arm_on_x86 {
  1235. // key: value,
  1236. // },
  1237. // arm_on_x86_64 {
  1238. // key: value,
  1239. // },
  1240. // },
  1241. // TODO(ccross): is this still necessary with native bridge?
  1242. if os.Class == Device {
  1243. if (arch.ArchType == X86 && (hasArmAbi(arch) ||
  1244. hasArmAndroidArch(ctx.Config().Targets[Android]))) ||
  1245. (arch.ArchType == Arm &&
  1246. hasX86AndroidArch(ctx.Config().Targets[Android])) {
  1247. field := "Arm_on_x86"
  1248. prefix := "target.arm_on_x86"
  1249. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1250. }
  1251. if (arch.ArchType == X86_64 && (hasArmAbi(arch) ||
  1252. hasArmAndroidArch(ctx.Config().Targets[Android]))) ||
  1253. (arch.ArchType == Arm &&
  1254. hasX8664AndroidArch(ctx.Config().Targets[Android])) {
  1255. field := "Arm_on_x86_64"
  1256. prefix := "target.arm_on_x86_64"
  1257. m.appendProperties(ctx, genProps, targetProp, field, prefix)
  1258. }
  1259. }
  1260. }
  1261. }
  1262. }
  1263. func forEachInterface(v reflect.Value, f func(reflect.Value)) {
  1264. switch v.Kind() {
  1265. case reflect.Interface:
  1266. f(v)
  1267. case reflect.Struct:
  1268. for i := 0; i < v.NumField(); i++ {
  1269. forEachInterface(v.Field(i), f)
  1270. }
  1271. case reflect.Ptr:
  1272. forEachInterface(v.Elem(), f)
  1273. default:
  1274. panic(fmt.Errorf("Unsupported kind %s", v.Kind()))
  1275. }
  1276. }
  1277. // Convert the arch product variables into a list of targets for each os class structs
  1278. func decodeTargetProductVariables(config *config) (map[OsType][]Target, error) {
  1279. variables := config.productVariables
  1280. targets := make(map[OsType][]Target)
  1281. var targetErr error
  1282. addTarget := func(os OsType, archName string, archVariant, cpuVariant *string, abi []string,
  1283. nativeBridgeEnabled NativeBridgeSupport, nativeBridgeHostArchName *string,
  1284. nativeBridgeRelativePath *string) {
  1285. if targetErr != nil {
  1286. return
  1287. }
  1288. arch, err := decodeArch(os, archName, archVariant, cpuVariant, abi)
  1289. if err != nil {
  1290. targetErr = err
  1291. return
  1292. }
  1293. nativeBridgeRelativePathStr := String(nativeBridgeRelativePath)
  1294. nativeBridgeHostArchNameStr := String(nativeBridgeHostArchName)
  1295. // Use guest arch as relative install path by default
  1296. if nativeBridgeEnabled && nativeBridgeRelativePathStr == "" {
  1297. nativeBridgeRelativePathStr = arch.ArchType.String()
  1298. }
  1299. targets[os] = append(targets[os],
  1300. Target{
  1301. Os: os,
  1302. Arch: arch,
  1303. NativeBridge: nativeBridgeEnabled,
  1304. NativeBridgeHostArchName: nativeBridgeHostArchNameStr,
  1305. NativeBridgeRelativePath: nativeBridgeRelativePathStr,
  1306. })
  1307. }
  1308. if variables.HostArch == nil {
  1309. return nil, fmt.Errorf("No host primary architecture set")
  1310. }
  1311. addTarget(BuildOs, *variables.HostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
  1312. if variables.HostSecondaryArch != nil && *variables.HostSecondaryArch != "" {
  1313. addTarget(BuildOs, *variables.HostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
  1314. }
  1315. if Bool(config.Host_bionic) {
  1316. addTarget(LinuxBionic, "x86_64", nil, nil, nil, NativeBridgeDisabled, nil, nil)
  1317. }
  1318. if String(variables.CrossHost) != "" {
  1319. crossHostOs := osByName(*variables.CrossHost)
  1320. if crossHostOs == NoOsType {
  1321. return nil, fmt.Errorf("Unknown cross host OS %q", *variables.CrossHost)
  1322. }
  1323. if String(variables.CrossHostArch) == "" {
  1324. return nil, fmt.Errorf("No cross-host primary architecture set")
  1325. }
  1326. addTarget(crossHostOs, *variables.CrossHostArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
  1327. if variables.CrossHostSecondaryArch != nil && *variables.CrossHostSecondaryArch != "" {
  1328. addTarget(crossHostOs, *variables.CrossHostSecondaryArch, nil, nil, nil, NativeBridgeDisabled, nil, nil)
  1329. }
  1330. }
  1331. if variables.DeviceArch != nil && *variables.DeviceArch != "" {
  1332. var target = Android
  1333. if Bool(variables.Fuchsia) {
  1334. target = Fuchsia
  1335. }
  1336. addTarget(target, *variables.DeviceArch, variables.DeviceArchVariant,
  1337. variables.DeviceCpuVariant, variables.DeviceAbi, NativeBridgeDisabled, nil, nil)
  1338. if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" {
  1339. addTarget(Android, *variables.DeviceSecondaryArch,
  1340. variables.DeviceSecondaryArchVariant, variables.DeviceSecondaryCpuVariant,
  1341. variables.DeviceSecondaryAbi, NativeBridgeDisabled, nil, nil)
  1342. }
  1343. if variables.NativeBridgeArch != nil && *variables.NativeBridgeArch != "" {
  1344. addTarget(Android, *variables.NativeBridgeArch,
  1345. variables.NativeBridgeArchVariant, variables.NativeBridgeCpuVariant,
  1346. variables.NativeBridgeAbi, NativeBridgeEnabled, variables.DeviceArch,
  1347. variables.NativeBridgeRelativePath)
  1348. }
  1349. if variables.DeviceSecondaryArch != nil && *variables.DeviceSecondaryArch != "" &&
  1350. variables.NativeBridgeSecondaryArch != nil && *variables.NativeBridgeSecondaryArch != "" {
  1351. addTarget(Android, *variables.NativeBridgeSecondaryArch,
  1352. variables.NativeBridgeSecondaryArchVariant,
  1353. variables.NativeBridgeSecondaryCpuVariant,
  1354. variables.NativeBridgeSecondaryAbi,
  1355. NativeBridgeEnabled,
  1356. variables.DeviceSecondaryArch,
  1357. variables.NativeBridgeSecondaryRelativePath)
  1358. }
  1359. }
  1360. if targetErr != nil {
  1361. return nil, targetErr
  1362. }
  1363. return targets, nil
  1364. }
  1365. // hasArmAbi returns true if arch has at least one arm ABI
  1366. func hasArmAbi(arch Arch) bool {
  1367. return PrefixInList(arch.Abi, "arm")
  1368. }
  1369. // hasArmArch returns true if targets has at least non-native_bridge arm Android arch
  1370. func hasArmAndroidArch(targets []Target) bool {
  1371. for _, target := range targets {
  1372. if target.Os == Android && target.Arch.ArchType == Arm && target.NativeBridge == NativeBridgeDisabled {
  1373. return true
  1374. }
  1375. }
  1376. return false
  1377. }
  1378. // hasX86Arch returns true if targets has at least x86 Android arch
  1379. func hasX86AndroidArch(targets []Target) bool {
  1380. for _, target := range targets {
  1381. if target.Os == Android && target.Arch.ArchType == X86 {
  1382. return true
  1383. }
  1384. }
  1385. return false
  1386. }
  1387. // hasX8664Arch returns true if targets has at least x86_64 Android arch
  1388. func hasX8664AndroidArch(targets []Target) bool {
  1389. for _, target := range targets {
  1390. if target.Os == Android && target.Arch.ArchType == X86_64 {
  1391. return true
  1392. }
  1393. }
  1394. return false
  1395. }
  1396. type archConfig struct {
  1397. arch string
  1398. archVariant string
  1399. cpuVariant string
  1400. abi []string
  1401. }
  1402. func getMegaDeviceConfig() []archConfig {
  1403. return []archConfig{
  1404. {"arm", "armv7-a", "generic", []string{"armeabi-v7a"}},
  1405. {"arm", "armv7-a-neon", "generic", []string{"armeabi-v7a"}},
  1406. {"arm", "armv7-a-neon", "cortex-a7", []string{"armeabi-v7a"}},
  1407. {"arm", "armv7-a-neon", "cortex-a8", []string{"armeabi-v7a"}},
  1408. {"arm", "armv7-a-neon", "cortex-a9", []string{"armeabi-v7a"}},
  1409. {"arm", "armv7-a-neon", "cortex-a15", []string{"armeabi-v7a"}},
  1410. {"arm", "armv7-a-neon", "cortex-a53", []string{"armeabi-v7a"}},
  1411. {"arm", "armv7-a-neon", "cortex-a53.a57", []string{"armeabi-v7a"}},
  1412. {"arm", "armv7-a-neon", "cortex-a72", []string{"armeabi-v7a"}},
  1413. {"arm", "armv7-a-neon", "cortex-a73", []string{"armeabi-v7a"}},
  1414. {"arm", "armv7-a-neon", "cortex-a75", []string{"armeabi-v7a"}},
  1415. {"arm", "armv7-a-neon", "cortex-a76", []string{"armeabi-v7a"}},
  1416. {"arm", "armv7-a-neon", "krait", []string{"armeabi-v7a"}},
  1417. {"arm", "armv7-a-neon", "kryo", []string{"armeabi-v7a"}},
  1418. {"arm", "armv7-a-neon", "kryo385", []string{"armeabi-v7a"}},
  1419. {"arm", "armv7-a-neon", "exynos-m1", []string{"armeabi-v7a"}},
  1420. {"arm", "armv7-a-neon", "exynos-m2", []string{"armeabi-v7a"}},
  1421. {"arm64", "armv8-a", "cortex-a53", []string{"arm64-v8a"}},
  1422. {"arm64", "armv8-a", "cortex-a72", []string{"arm64-v8a"}},
  1423. {"arm64", "armv8-a", "cortex-a73", []string{"arm64-v8a"}},
  1424. {"arm64", "armv8-a", "kryo", []string{"arm64-v8a"}},
  1425. {"arm64", "armv8-a", "exynos-m1", []string{"arm64-v8a"}},
  1426. {"arm64", "armv8-a", "exynos-m2", []string{"arm64-v8a"}},
  1427. {"arm64", "armv8-2a", "cortex-a75", []string{"arm64-v8a"}},
  1428. {"arm64", "armv8-2a", "cortex-a76", []string{"arm64-v8a"}},
  1429. {"arm64", "armv8-2a", "kryo385", []string{"arm64-v8a"}},
  1430. {"x86", "", "", []string{"x86"}},
  1431. {"x86", "atom", "", []string{"x86"}},
  1432. {"x86", "haswell", "", []string{"x86"}},
  1433. {"x86", "ivybridge", "", []string{"x86"}},
  1434. {"x86", "sandybridge", "", []string{"x86"}},
  1435. {"x86", "silvermont", "", []string{"x86"}},
  1436. {"x86", "stoneyridge", "", []string{"x86"}},
  1437. {"x86", "x86_64", "", []string{"x86"}},
  1438. {"x86_64", "", "", []string{"x86_64"}},
  1439. {"x86_64", "haswell", "", []string{"x86_64"}},
  1440. {"x86_64", "ivybridge", "", []string{"x86_64"}},
  1441. {"x86_64", "sandybridge", "", []string{"x86_64"}},
  1442. {"x86_64", "silvermont", "", []string{"x86_64"}},
  1443. {"x86_64", "stoneyridge", "", []string{"x86_64"}},
  1444. }
  1445. }
  1446. func getNdkAbisConfig() []archConfig {
  1447. return []archConfig{
  1448. {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
  1449. {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
  1450. {"x86", "", "", []string{"x86"}},
  1451. {"x86_64", "", "", []string{"x86_64"}},
  1452. }
  1453. }
  1454. func getAmlAbisConfig() []archConfig {
  1455. return []archConfig{
  1456. {"arm", "armv7-a", "", []string{"armeabi-v7a"}},
  1457. {"arm64", "armv8-a", "", []string{"arm64-v8a"}},
  1458. {"x86", "", "", []string{"x86"}},
  1459. {"x86_64", "", "", []string{"x86_64"}},
  1460. }
  1461. }
  1462. func decodeArchSettings(os OsType, archConfigs []archConfig) ([]Target, error) {
  1463. var ret []Target
  1464. for _, config := range archConfigs {
  1465. arch, err := decodeArch(os, config.arch, &config.archVariant,
  1466. &config.cpuVariant, config.abi)
  1467. if err != nil {
  1468. return nil, err
  1469. }
  1470. ret = append(ret, Target{
  1471. Os: Android,
  1472. Arch: arch,
  1473. })
  1474. }
  1475. return ret, nil
  1476. }
  1477. // Convert a set of strings from product variables into a single Arch struct
  1478. func decodeArch(os OsType, arch string, archVariant, cpuVariant *string, abi []string) (Arch, error) {
  1479. stringPtr := func(p *string) string {
  1480. if p != nil {
  1481. return *p
  1482. }
  1483. return ""
  1484. }
  1485. archType, ok := archTypeMap[arch]
  1486. if !ok {
  1487. return Arch{}, fmt.Errorf("unknown arch %q", arch)
  1488. }
  1489. a := Arch{
  1490. ArchType: archType,
  1491. ArchVariant: stringPtr(archVariant),
  1492. CpuVariant: stringPtr(cpuVariant),
  1493. Abi: abi,
  1494. }
  1495. if a.ArchVariant == a.ArchType.Name || a.ArchVariant == "generic" {
  1496. a.ArchVariant = ""
  1497. }
  1498. if a.CpuVariant == a.ArchType.Name || a.CpuVariant == "generic" {
  1499. a.CpuVariant = ""
  1500. }
  1501. for i := 0; i < len(a.Abi); i++ {
  1502. if a.Abi[i] == "" {
  1503. a.Abi = append(a.Abi[:i], a.Abi[i+1:]...)
  1504. i--
  1505. }
  1506. }
  1507. if a.ArchVariant == "" {
  1508. if featureMap, ok := defaultArchFeatureMap[os]; ok {
  1509. a.ArchFeatures = featureMap[archType]
  1510. }
  1511. } else {
  1512. if featureMap, ok := archFeatureMap[archType]; ok {
  1513. a.ArchFeatures = featureMap[a.ArchVariant]
  1514. }
  1515. }
  1516. return a, nil
  1517. }
  1518. func filterMultilibTargets(targets []Target, multilib string) []Target {
  1519. var ret []Target
  1520. for _, t := range targets {
  1521. if t.Arch.ArchType.Multilib == multilib {
  1522. ret = append(ret, t)
  1523. }
  1524. }
  1525. return ret
  1526. }
  1527. // Return the set of Os specific common architecture targets for each Os in a list of
  1528. // targets.
  1529. func getCommonTargets(targets []Target) []Target {
  1530. var ret []Target
  1531. set := make(map[string]bool)
  1532. for _, t := range targets {
  1533. if _, found := set[t.Os.String()]; !found {
  1534. set[t.Os.String()] = true
  1535. ret = append(ret, commonTargetMap[t.Os.String()])
  1536. }
  1537. }
  1538. return ret
  1539. }
  1540. func firstTarget(targets []Target, filters ...string) []Target {
  1541. for _, filter := range filters {
  1542. buildTargets := filterMultilibTargets(targets, filter)
  1543. if len(buildTargets) > 0 {
  1544. return buildTargets[:1]
  1545. }
  1546. }
  1547. return nil
  1548. }
  1549. // Use the module multilib setting to select one or more targets from a target list
  1550. func decodeMultilibTargets(multilib string, targets []Target, prefer32 bool) ([]Target, error) {
  1551. buildTargets := []Target{}
  1552. switch multilib {
  1553. case "common":
  1554. buildTargets = getCommonTargets(targets)
  1555. case "common_first":
  1556. buildTargets = getCommonTargets(targets)
  1557. if prefer32 {
  1558. buildTargets = append(buildTargets, firstTarget(targets, "lib32", "lib64")...)
  1559. } else {
  1560. buildTargets = append(buildTargets, firstTarget(targets, "lib64", "lib32")...)
  1561. }
  1562. case "both":
  1563. if prefer32 {
  1564. buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
  1565. buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
  1566. } else {
  1567. buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib64")...)
  1568. buildTargets = append(buildTargets, filterMultilibTargets(targets, "lib32")...)
  1569. }
  1570. case "32":
  1571. buildTargets = filterMultilibTargets(targets, "lib32")
  1572. case "64":
  1573. buildTargets = filterMultilibTargets(targets, "lib64")
  1574. case "first":
  1575. if prefer32 {
  1576. buildTargets = firstTarget(targets, "lib32", "lib64")
  1577. } else {
  1578. buildTargets = firstTarget(targets, "lib64", "lib32")
  1579. }
  1580. case "prefer32":
  1581. buildTargets = filterMultilibTargets(targets, "lib32")
  1582. if len(buildTargets) == 0 {
  1583. buildTargets = filterMultilibTargets(targets, "lib64")
  1584. }
  1585. default:
  1586. return nil, fmt.Errorf(`compile_multilib must be "both", "first", "32", "64", or "prefer32" found %q`,
  1587. multilib)
  1588. }
  1589. return buildTargets, nil
  1590. }