config.go 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  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. // This is the primary location to write and read all configuration values and
  16. // product variables necessary for soong_build's operation.
  17. import (
  18. "encoding/json"
  19. "errors"
  20. "fmt"
  21. "io/ioutil"
  22. "os"
  23. "path/filepath"
  24. "reflect"
  25. "runtime"
  26. "strconv"
  27. "strings"
  28. "sync"
  29. "github.com/google/blueprint"
  30. "github.com/google/blueprint/bootstrap"
  31. "github.com/google/blueprint/pathtools"
  32. "github.com/google/blueprint/proptools"
  33. "android/soong/android/soongconfig"
  34. "android/soong/bazel"
  35. "android/soong/remoteexec"
  36. "android/soong/starlark_fmt"
  37. )
  38. // Bool re-exports proptools.Bool for the android package.
  39. var Bool = proptools.Bool
  40. // String re-exports proptools.String for the android package.
  41. var String = proptools.String
  42. // StringDefault re-exports proptools.StringDefault for the android package.
  43. var StringDefault = proptools.StringDefault
  44. // FutureApiLevelInt is a placeholder constant for unreleased API levels.
  45. const FutureApiLevelInt = 10000
  46. // FutureApiLevel represents unreleased API levels.
  47. var FutureApiLevel = ApiLevel{
  48. value: "current",
  49. number: FutureApiLevelInt,
  50. isPreview: true,
  51. }
  52. // The product variables file name, containing product config from Kati.
  53. const productVariablesFileName = "soong.variables"
  54. // A Config object represents the entire build configuration for Android.
  55. type Config struct {
  56. *config
  57. }
  58. // SoongOutDir returns the build output directory for the configuration.
  59. func (c Config) SoongOutDir() string {
  60. return c.soongOutDir
  61. }
  62. func (c Config) OutDir() string {
  63. return c.outDir
  64. }
  65. func (c Config) RunGoTests() bool {
  66. return c.runGoTests
  67. }
  68. func (c Config) DebugCompilation() bool {
  69. return false // Never compile Go code in the main build for debugging
  70. }
  71. func (c Config) Subninjas() []string {
  72. return []string{}
  73. }
  74. func (c Config) PrimaryBuilderInvocations() []bootstrap.PrimaryBuilderInvocation {
  75. return []bootstrap.PrimaryBuilderInvocation{}
  76. }
  77. // A DeviceConfig object represents the configuration for a particular device
  78. // being built. For now there will only be one of these, but in the future there
  79. // may be multiple devices being built.
  80. type DeviceConfig struct {
  81. *deviceConfig
  82. }
  83. // VendorConfig represents the configuration for vendor-specific behavior.
  84. type VendorConfig soongconfig.SoongConfig
  85. // Definition of general build configuration for soong_build. Some of these
  86. // product configuration values are read from Kati-generated soong.variables.
  87. type config struct {
  88. // Options configurable with soong.variables
  89. productVariables productVariables
  90. // Only available on configs created by TestConfig
  91. TestProductVariables *productVariables
  92. // A specialized context object for Bazel/Soong mixed builds and migration
  93. // purposes.
  94. BazelContext BazelContext
  95. ProductVariablesFileName string
  96. // BuildOS stores the OsType for the OS that the build is running on.
  97. BuildOS OsType
  98. // BuildArch stores the ArchType for the CPU that the build is running on.
  99. BuildArch ArchType
  100. Targets map[OsType][]Target
  101. BuildOSTarget Target // the Target for tools run on the build machine
  102. BuildOSCommonTarget Target // the Target for common (java) tools run on the build machine
  103. AndroidCommonTarget Target // the Target for common modules for the Android device
  104. AndroidFirstDeviceTarget Target // the first Target for modules for the Android device
  105. // multilibConflicts for an ArchType is true if there is earlier configured
  106. // device architecture with the same multilib value.
  107. multilibConflicts map[ArchType]bool
  108. deviceConfig *deviceConfig
  109. outDir string // The output directory (usually out/)
  110. soongOutDir string
  111. moduleListFile string // the path to the file which lists blueprint files to parse.
  112. runGoTests bool
  113. env map[string]string
  114. envLock sync.Mutex
  115. envDeps map[string]string
  116. envFrozen bool
  117. // Changes behavior based on whether Kati runs after soong_build, or if soong_build
  118. // runs standalone.
  119. katiEnabled bool
  120. captureBuild bool // true for tests, saves build parameters for each module
  121. ignoreEnvironment bool // true for tests, returns empty from all Getenv calls
  122. fs pathtools.FileSystem
  123. mockBpList string
  124. runningAsBp2Build bool
  125. bp2buildPackageConfig bp2BuildConversionAllowlist
  126. Bp2buildSoongConfigDefinitions soongconfig.Bp2BuildSoongConfigDefinitions
  127. // If testAllowNonExistentPaths is true then PathForSource and PathForModuleSrc won't error
  128. // in tests when a path doesn't exist.
  129. TestAllowNonExistentPaths bool
  130. // The list of files that when changed, must invalidate soong_build to
  131. // regenerate build.ninja.
  132. ninjaFileDepsSet sync.Map
  133. OncePer
  134. }
  135. type deviceConfig struct {
  136. config *config
  137. OncePer
  138. }
  139. type jsonConfigurable interface {
  140. SetDefaultConfig()
  141. }
  142. func loadConfig(config *config) error {
  143. return loadFromConfigFile(&config.productVariables, absolutePath(config.ProductVariablesFileName))
  144. }
  145. // loadFromConfigFile loads and decodes configuration options from a JSON file
  146. // in the current working directory.
  147. func loadFromConfigFile(configurable *productVariables, filename string) error {
  148. // Try to open the file
  149. configFileReader, err := os.Open(filename)
  150. defer configFileReader.Close()
  151. if os.IsNotExist(err) {
  152. // Need to create a file, so that blueprint & ninja don't get in
  153. // a dependency tracking loop.
  154. // Make a file-configurable-options with defaults, write it out using
  155. // a json writer.
  156. configurable.SetDefaultConfig()
  157. err = saveToConfigFile(configurable, filename)
  158. if err != nil {
  159. return err
  160. }
  161. } else if err != nil {
  162. return fmt.Errorf("config file: could not open %s: %s", filename, err.Error())
  163. } else {
  164. // Make a decoder for it
  165. jsonDecoder := json.NewDecoder(configFileReader)
  166. err = jsonDecoder.Decode(configurable)
  167. if err != nil {
  168. return fmt.Errorf("config file: %s did not parse correctly: %s", filename, err.Error())
  169. }
  170. }
  171. if Bool(configurable.GcovCoverage) && Bool(configurable.ClangCoverage) {
  172. return fmt.Errorf("GcovCoverage and ClangCoverage cannot both be set")
  173. }
  174. configurable.Native_coverage = proptools.BoolPtr(
  175. Bool(configurable.GcovCoverage) ||
  176. Bool(configurable.ClangCoverage))
  177. // when Platform_sdk_final is true (or PLATFORM_VERSION_CODENAME is REL), use Platform_sdk_version;
  178. // if false (pre-released version, for example), use Platform_sdk_codename.
  179. if Bool(configurable.Platform_sdk_final) {
  180. if configurable.Platform_sdk_version != nil {
  181. configurable.Platform_sdk_version_or_codename =
  182. proptools.StringPtr(strconv.Itoa(*(configurable.Platform_sdk_version)))
  183. } else {
  184. return fmt.Errorf("Platform_sdk_version cannot be pointed by a NULL pointer")
  185. }
  186. } else {
  187. configurable.Platform_sdk_version_or_codename =
  188. proptools.StringPtr(String(configurable.Platform_sdk_codename))
  189. }
  190. return saveToBazelConfigFile(configurable, filepath.Dir(filename))
  191. }
  192. // atomically writes the config file in case two copies of soong_build are running simultaneously
  193. // (for example, docs generation and ninja manifest generation)
  194. func saveToConfigFile(config *productVariables, filename string) error {
  195. data, err := json.MarshalIndent(&config, "", " ")
  196. if err != nil {
  197. return fmt.Errorf("cannot marshal config data: %s", err.Error())
  198. }
  199. f, err := ioutil.TempFile(filepath.Dir(filename), "config")
  200. if err != nil {
  201. return fmt.Errorf("cannot create empty config file %s: %s", filename, err.Error())
  202. }
  203. defer os.Remove(f.Name())
  204. defer f.Close()
  205. _, err = f.Write(data)
  206. if err != nil {
  207. return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
  208. }
  209. _, err = f.WriteString("\n")
  210. if err != nil {
  211. return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
  212. }
  213. f.Close()
  214. os.Rename(f.Name(), filename)
  215. return nil
  216. }
  217. func saveToBazelConfigFile(config *productVariables, outDir string) error {
  218. dir := filepath.Join(outDir, bazel.SoongInjectionDirName, "product_config")
  219. err := createDirIfNonexistent(dir, os.ModePerm)
  220. if err != nil {
  221. return fmt.Errorf("Could not create dir %s: %s", dir, err)
  222. }
  223. nonArchVariantProductVariables := []string{}
  224. archVariantProductVariables := []string{}
  225. p := variableProperties{}
  226. t := reflect.TypeOf(p.Product_variables)
  227. for i := 0; i < t.NumField(); i++ {
  228. f := t.Field(i)
  229. nonArchVariantProductVariables = append(nonArchVariantProductVariables, strings.ToLower(f.Name))
  230. if proptools.HasTag(f, "android", "arch_variant") {
  231. archVariantProductVariables = append(archVariantProductVariables, strings.ToLower(f.Name))
  232. }
  233. }
  234. nonArchVariantProductVariablesJson := starlark_fmt.PrintStringList(nonArchVariantProductVariables, 0)
  235. if err != nil {
  236. return fmt.Errorf("cannot marshal product variable data: %s", err.Error())
  237. }
  238. archVariantProductVariablesJson := starlark_fmt.PrintStringList(archVariantProductVariables, 0)
  239. if err != nil {
  240. return fmt.Errorf("cannot marshal arch variant product variable data: %s", err.Error())
  241. }
  242. configJson, err := json.MarshalIndent(&config, "", " ")
  243. if err != nil {
  244. return fmt.Errorf("cannot marshal config data: %s", err.Error())
  245. }
  246. bzl := []string{
  247. bazel.GeneratedBazelFileWarning,
  248. fmt.Sprintf(`_product_vars = json.decode("""%s""")`, configJson),
  249. fmt.Sprintf(`_product_var_constraints = %s`, nonArchVariantProductVariablesJson),
  250. fmt.Sprintf(`_arch_variant_product_var_constraints = %s`, archVariantProductVariablesJson),
  251. "\n", `
  252. product_vars = _product_vars
  253. product_var_constraints = _product_var_constraints
  254. arch_variant_product_var_constraints = _arch_variant_product_var_constraints
  255. `,
  256. }
  257. err = ioutil.WriteFile(filepath.Join(dir, "product_variables.bzl"), []byte(strings.Join(bzl, "\n")), 0644)
  258. if err != nil {
  259. return fmt.Errorf("Could not write .bzl config file %s", err)
  260. }
  261. err = ioutil.WriteFile(filepath.Join(dir, "BUILD"), []byte(bazel.GeneratedBazelFileWarning), 0644)
  262. if err != nil {
  263. return fmt.Errorf("Could not write BUILD config file %s", err)
  264. }
  265. return nil
  266. }
  267. // NullConfig returns a mostly empty Config for use by standalone tools like dexpreopt_gen that
  268. // use the android package.
  269. func NullConfig(outDir, soongOutDir string) Config {
  270. return Config{
  271. config: &config{
  272. outDir: outDir,
  273. soongOutDir: soongOutDir,
  274. fs: pathtools.OsFs,
  275. },
  276. }
  277. }
  278. // TestConfig returns a Config object for testing.
  279. func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
  280. envCopy := make(map[string]string)
  281. for k, v := range env {
  282. envCopy[k] = v
  283. }
  284. // Copy the real PATH value to the test environment, it's needed by
  285. // NonHermeticHostSystemTool() used in x86_darwin_host.go
  286. envCopy["PATH"] = os.Getenv("PATH")
  287. config := &config{
  288. productVariables: productVariables{
  289. DeviceName: stringPtr("test_device"),
  290. DeviceProduct: stringPtr("test_product"),
  291. Platform_sdk_version: intPtr(30),
  292. Platform_sdk_codename: stringPtr("S"),
  293. Platform_base_sdk_extension_version: intPtr(1),
  294. Platform_version_active_codenames: []string{"S", "Tiramisu"},
  295. DeviceSystemSdkVersions: []string{"14", "15"},
  296. Platform_systemsdk_versions: []string{"29", "30"},
  297. AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
  298. AAPTPreferredConfig: stringPtr("xhdpi"),
  299. AAPTCharacteristics: stringPtr("nosdcard"),
  300. AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
  301. UncompressPrivAppDex: boolPtr(true),
  302. ShippingApiLevel: stringPtr("30"),
  303. },
  304. outDir: buildDir,
  305. soongOutDir: filepath.Join(buildDir, "soong"),
  306. captureBuild: true,
  307. env: envCopy,
  308. // Set testAllowNonExistentPaths so that test contexts don't need to specify every path
  309. // passed to PathForSource or PathForModuleSrc.
  310. TestAllowNonExistentPaths: true,
  311. BazelContext: noopBazelContext{},
  312. }
  313. config.deviceConfig = &deviceConfig{
  314. config: config,
  315. }
  316. config.TestProductVariables = &config.productVariables
  317. config.mockFileSystem(bp, fs)
  318. determineBuildOS(config)
  319. return Config{config}
  320. }
  321. func modifyTestConfigToSupportArchMutator(testConfig Config) {
  322. config := testConfig.config
  323. config.Targets = map[OsType][]Target{
  324. Android: []Target{
  325. {Android, Arch{ArchType: Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", "", false},
  326. {Android, Arch{ArchType: Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridgeDisabled, "", "", false},
  327. },
  328. config.BuildOS: []Target{
  329. {config.BuildOS, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
  330. {config.BuildOS, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
  331. },
  332. }
  333. if runtime.GOOS == "darwin" {
  334. config.Targets[config.BuildOS] = config.Targets[config.BuildOS][:1]
  335. }
  336. config.BuildOSTarget = config.Targets[config.BuildOS][0]
  337. config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
  338. config.AndroidCommonTarget = getCommonTargets(config.Targets[Android])[0]
  339. config.AndroidFirstDeviceTarget = firstTarget(config.Targets[Android], "lib64", "lib32")[0]
  340. config.TestProductVariables.DeviceArch = proptools.StringPtr("arm64")
  341. config.TestProductVariables.DeviceArchVariant = proptools.StringPtr("armv8-a")
  342. config.TestProductVariables.DeviceSecondaryArch = proptools.StringPtr("arm")
  343. config.TestProductVariables.DeviceSecondaryArchVariant = proptools.StringPtr("armv7-a-neon")
  344. }
  345. func modifyTestConfigForMusl(config Config) {
  346. delete(config.Targets, config.BuildOS)
  347. config.productVariables.HostMusl = boolPtr(true)
  348. determineBuildOS(config.config)
  349. config.Targets[config.BuildOS] = []Target{
  350. {config.BuildOS, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", false},
  351. {config.BuildOS, Arch{ArchType: X86}, NativeBridgeDisabled, "", "", false},
  352. }
  353. config.BuildOSTarget = config.Targets[config.BuildOS][0]
  354. config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
  355. }
  356. // TestArchConfig returns a Config object suitable for using for tests that
  357. // need to run the arch mutator.
  358. func TestArchConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
  359. testConfig := TestConfig(buildDir, env, bp, fs)
  360. modifyTestConfigToSupportArchMutator(testConfig)
  361. return testConfig
  362. }
  363. // ConfigForAdditionalRun is a config object which is "reset" for another
  364. // bootstrap run. Only per-run data is reset. Data which needs to persist across
  365. // multiple runs in the same program execution is carried over (such as Bazel
  366. // context or environment deps).
  367. func ConfigForAdditionalRun(c Config) (Config, error) {
  368. newConfig, err := NewConfig(c.moduleListFile, c.runGoTests, c.outDir, c.soongOutDir, c.env)
  369. if err != nil {
  370. return Config{}, err
  371. }
  372. newConfig.BazelContext = c.BazelContext
  373. newConfig.envDeps = c.envDeps
  374. return newConfig, nil
  375. }
  376. // NewConfig creates a new Config object. The srcDir argument specifies the path
  377. // to the root source directory. It also loads the config file, if found.
  378. func NewConfig(moduleListFile string, runGoTests bool, outDir, soongOutDir string, availableEnv map[string]string) (Config, error) {
  379. // Make a config with default options.
  380. config := &config{
  381. ProductVariablesFileName: filepath.Join(soongOutDir, productVariablesFileName),
  382. env: availableEnv,
  383. outDir: outDir,
  384. soongOutDir: soongOutDir,
  385. runGoTests: runGoTests,
  386. multilibConflicts: make(map[ArchType]bool),
  387. moduleListFile: moduleListFile,
  388. fs: pathtools.NewOsFs(absSrcDir),
  389. }
  390. config.deviceConfig = &deviceConfig{
  391. config: config,
  392. }
  393. // Soundness check of the build and source directories. This won't catch strange
  394. // configurations with symlinks, but at least checks the obvious case.
  395. absBuildDir, err := filepath.Abs(soongOutDir)
  396. if err != nil {
  397. return Config{}, err
  398. }
  399. absSrcDir, err := filepath.Abs(".")
  400. if err != nil {
  401. return Config{}, err
  402. }
  403. if strings.HasPrefix(absSrcDir, absBuildDir) {
  404. return Config{}, fmt.Errorf("Build dir must not contain source directory")
  405. }
  406. // Load any configurable options from the configuration file
  407. err = loadConfig(config)
  408. if err != nil {
  409. return Config{}, err
  410. }
  411. KatiEnabledMarkerFile := filepath.Join(soongOutDir, ".soong.kati_enabled")
  412. if _, err := os.Stat(absolutePath(KatiEnabledMarkerFile)); err == nil {
  413. config.katiEnabled = true
  414. }
  415. determineBuildOS(config)
  416. // Sets up the map of target OSes to the finer grained compilation targets
  417. // that are configured from the product variables.
  418. targets, err := decodeTargetProductVariables(config)
  419. if err != nil {
  420. return Config{}, err
  421. }
  422. // Make the CommonOS OsType available for all products.
  423. targets[CommonOS] = []Target{commonTargetMap[CommonOS.Name]}
  424. var archConfig []archConfig
  425. if config.NdkAbis() {
  426. archConfig = getNdkAbisConfig()
  427. } else if config.AmlAbis() {
  428. archConfig = getAmlAbisConfig()
  429. }
  430. if archConfig != nil {
  431. androidTargets, err := decodeAndroidArchSettings(archConfig)
  432. if err != nil {
  433. return Config{}, err
  434. }
  435. targets[Android] = androidTargets
  436. }
  437. multilib := make(map[string]bool)
  438. for _, target := range targets[Android] {
  439. if seen := multilib[target.Arch.ArchType.Multilib]; seen {
  440. config.multilibConflicts[target.Arch.ArchType] = true
  441. }
  442. multilib[target.Arch.ArchType.Multilib] = true
  443. }
  444. // Map of OS to compilation targets.
  445. config.Targets = targets
  446. // Compilation targets for host tools.
  447. config.BuildOSTarget = config.Targets[config.BuildOS][0]
  448. config.BuildOSCommonTarget = getCommonTargets(config.Targets[config.BuildOS])[0]
  449. // Compilation targets for Android.
  450. if len(config.Targets[Android]) > 0 {
  451. config.AndroidCommonTarget = getCommonTargets(config.Targets[Android])[0]
  452. config.AndroidFirstDeviceTarget = firstTarget(config.Targets[Android], "lib64", "lib32")[0]
  453. }
  454. config.BazelContext, err = NewBazelContext(config)
  455. config.bp2buildPackageConfig = bp2buildAllowlist
  456. return Config{config}, err
  457. }
  458. // mockFileSystem replaces all reads with accesses to the provided map of
  459. // filenames to contents stored as a byte slice.
  460. func (c *config) mockFileSystem(bp string, fs map[string][]byte) {
  461. mockFS := map[string][]byte{}
  462. if _, exists := mockFS["Android.bp"]; !exists {
  463. mockFS["Android.bp"] = []byte(bp)
  464. }
  465. for k, v := range fs {
  466. mockFS[k] = v
  467. }
  468. // no module list file specified; find every file named Blueprints or Android.bp
  469. pathsToParse := []string{}
  470. for candidate := range mockFS {
  471. base := filepath.Base(candidate)
  472. if base == "Android.bp" {
  473. pathsToParse = append(pathsToParse, candidate)
  474. }
  475. }
  476. if len(pathsToParse) < 1 {
  477. panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", mockFS))
  478. }
  479. mockFS[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n"))
  480. c.fs = pathtools.MockFs(mockFS)
  481. c.mockBpList = blueprint.MockModuleListFile
  482. }
  483. func (c *config) SetAllowMissingDependencies() {
  484. c.productVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
  485. }
  486. // BlueprintToolLocation returns the directory containing build system tools
  487. // from Blueprint, like soong_zip and merge_zips.
  488. func (c *config) HostToolDir() string {
  489. if c.KatiEnabled() {
  490. return filepath.Join(c.outDir, "host", c.PrebuiltOS(), "bin")
  491. } else {
  492. return filepath.Join(c.soongOutDir, "host", c.PrebuiltOS(), "bin")
  493. }
  494. }
  495. func (c *config) HostToolPath(ctx PathContext, tool string) Path {
  496. path := pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "bin", false, tool)
  497. return path
  498. }
  499. func (c *config) HostJNIToolPath(ctx PathContext, lib string) Path {
  500. ext := ".so"
  501. if runtime.GOOS == "darwin" {
  502. ext = ".dylib"
  503. }
  504. path := pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "lib64", false, lib+ext)
  505. return path
  506. }
  507. func (c *config) HostJavaToolPath(ctx PathContext, tool string) Path {
  508. path := pathForInstall(ctx, ctx.Config().BuildOS, ctx.Config().BuildArch, "framework", false, tool)
  509. return path
  510. }
  511. // PrebuiltOS returns the name of the host OS used in prebuilts directories.
  512. func (c *config) PrebuiltOS() string {
  513. switch runtime.GOOS {
  514. case "linux":
  515. return "linux-x86"
  516. case "darwin":
  517. return "darwin-x86"
  518. default:
  519. panic("Unknown GOOS")
  520. }
  521. }
  522. // GoRoot returns the path to the root directory of the Go toolchain.
  523. func (c *config) GoRoot() string {
  524. return fmt.Sprintf("prebuilts/go/%s", c.PrebuiltOS())
  525. }
  526. // PrebuiltBuildTool returns the path to a tool in the prebuilts directory containing
  527. // checked-in tools, like Kati, Ninja or Toybox, for the current host OS.
  528. func (c *config) PrebuiltBuildTool(ctx PathContext, tool string) Path {
  529. return PathForSource(ctx, "prebuilts/build-tools", c.PrebuiltOS(), "bin", tool)
  530. }
  531. // CpPreserveSymlinksFlags returns the host-specific flag for the cp(1) command
  532. // to preserve symlinks.
  533. func (c *config) CpPreserveSymlinksFlags() string {
  534. switch runtime.GOOS {
  535. case "darwin":
  536. return "-R"
  537. case "linux":
  538. return "-d"
  539. default:
  540. return ""
  541. }
  542. }
  543. func (c *config) Getenv(key string) string {
  544. var val string
  545. var exists bool
  546. c.envLock.Lock()
  547. defer c.envLock.Unlock()
  548. if c.envDeps == nil {
  549. c.envDeps = make(map[string]string)
  550. }
  551. if val, exists = c.envDeps[key]; !exists {
  552. if c.envFrozen {
  553. panic("Cannot access new environment variables after envdeps are frozen")
  554. }
  555. val, _ = c.env[key]
  556. c.envDeps[key] = val
  557. }
  558. return val
  559. }
  560. func (c *config) GetenvWithDefault(key string, defaultValue string) string {
  561. ret := c.Getenv(key)
  562. if ret == "" {
  563. return defaultValue
  564. }
  565. return ret
  566. }
  567. func (c *config) IsEnvTrue(key string) bool {
  568. value := c.Getenv(key)
  569. return value == "1" || value == "y" || value == "yes" || value == "on" || value == "true"
  570. }
  571. func (c *config) IsEnvFalse(key string) bool {
  572. value := c.Getenv(key)
  573. return value == "0" || value == "n" || value == "no" || value == "off" || value == "false"
  574. }
  575. // EnvDeps returns the environment variables this build depends on. The first
  576. // call to this function blocks future reads from the environment.
  577. func (c *config) EnvDeps() map[string]string {
  578. c.envLock.Lock()
  579. defer c.envLock.Unlock()
  580. c.envFrozen = true
  581. return c.envDeps
  582. }
  583. func (c *config) KatiEnabled() bool {
  584. return c.katiEnabled
  585. }
  586. func (c *config) BuildId() string {
  587. return String(c.productVariables.BuildId)
  588. }
  589. // BuildNumberFile returns the path to a text file containing metadata
  590. // representing the current build's number.
  591. //
  592. // Rules that want to reference the build number should read from this file
  593. // without depending on it. They will run whenever their other dependencies
  594. // require them to run and get the current build number. This ensures they don't
  595. // rebuild on every incremental build when the build number changes.
  596. func (c *config) BuildNumberFile(ctx PathContext) Path {
  597. return PathForOutput(ctx, String(c.productVariables.BuildNumberFile))
  598. }
  599. // DeviceName returns the name of the current device target.
  600. // TODO: take an AndroidModuleContext to select the device name for multi-device builds
  601. func (c *config) DeviceName() string {
  602. return *c.productVariables.DeviceName
  603. }
  604. // DeviceProduct returns the current product target. There could be multiple of
  605. // these per device type.
  606. //
  607. // NOTE: Do not base conditional logic on this value. It may break product
  608. // inheritance.
  609. func (c *config) DeviceProduct() string {
  610. return *c.productVariables.DeviceProduct
  611. }
  612. func (c *config) DeviceResourceOverlays() []string {
  613. return c.productVariables.DeviceResourceOverlays
  614. }
  615. func (c *config) ProductResourceOverlays() []string {
  616. return c.productVariables.ProductResourceOverlays
  617. }
  618. func (c *config) PlatformVersionName() string {
  619. return String(c.productVariables.Platform_version_name)
  620. }
  621. func (c *config) PlatformSdkVersion() ApiLevel {
  622. return uncheckedFinalApiLevel(*c.productVariables.Platform_sdk_version)
  623. }
  624. func (c *config) PlatformSdkCodename() string {
  625. return String(c.productVariables.Platform_sdk_codename)
  626. }
  627. func (c *config) PlatformSdkExtensionVersion() int {
  628. return *c.productVariables.Platform_sdk_extension_version
  629. }
  630. func (c *config) PlatformBaseSdkExtensionVersion() int {
  631. return *c.productVariables.Platform_base_sdk_extension_version
  632. }
  633. func (c *config) PlatformSecurityPatch() string {
  634. return String(c.productVariables.Platform_security_patch)
  635. }
  636. func (c *config) PlatformPreviewSdkVersion() string {
  637. return String(c.productVariables.Platform_preview_sdk_version)
  638. }
  639. func (c *config) PlatformMinSupportedTargetSdkVersion() string {
  640. return String(c.productVariables.Platform_min_supported_target_sdk_version)
  641. }
  642. func (c *config) PlatformBaseOS() string {
  643. return String(c.productVariables.Platform_base_os)
  644. }
  645. func (c *config) PlatformVersionLastStable() string {
  646. return String(c.productVariables.Platform_version_last_stable)
  647. }
  648. func (c *config) MinSupportedSdkVersion() ApiLevel {
  649. return uncheckedFinalApiLevel(19)
  650. }
  651. func (c *config) FinalApiLevels() []ApiLevel {
  652. var levels []ApiLevel
  653. for i := 1; i <= c.PlatformSdkVersion().FinalOrFutureInt(); i++ {
  654. levels = append(levels, uncheckedFinalApiLevel(i))
  655. }
  656. return levels
  657. }
  658. func (c *config) PreviewApiLevels() []ApiLevel {
  659. var levels []ApiLevel
  660. for i, codename := range c.PlatformVersionActiveCodenames() {
  661. levels = append(levels, ApiLevel{
  662. value: codename,
  663. number: i,
  664. isPreview: true,
  665. })
  666. }
  667. return levels
  668. }
  669. func (c *config) LatestPreviewApiLevel() ApiLevel {
  670. level := NoneApiLevel
  671. for _, l := range c.PreviewApiLevels() {
  672. if l.GreaterThan(level) {
  673. level = l
  674. }
  675. }
  676. return level
  677. }
  678. func (c *config) AllSupportedApiLevels() []ApiLevel {
  679. var levels []ApiLevel
  680. levels = append(levels, c.FinalApiLevels()...)
  681. return append(levels, c.PreviewApiLevels()...)
  682. }
  683. // DefaultAppTargetSdk returns the API level that platform apps are targeting.
  684. // This converts a codename to the exact ApiLevel it represents.
  685. func (c *config) DefaultAppTargetSdk(ctx EarlyModuleContext) ApiLevel {
  686. if Bool(c.productVariables.Platform_sdk_final) {
  687. return c.PlatformSdkVersion()
  688. }
  689. codename := c.PlatformSdkCodename()
  690. if codename == "" {
  691. return NoneApiLevel
  692. }
  693. if codename == "REL" {
  694. panic("Platform_sdk_codename should not be REL when Platform_sdk_final is true")
  695. }
  696. return ApiLevelOrPanic(ctx, codename)
  697. }
  698. func (c *config) AppsDefaultVersionName() string {
  699. return String(c.productVariables.AppsDefaultVersionName)
  700. }
  701. // Codenames that are active in the current lunch target.
  702. func (c *config) PlatformVersionActiveCodenames() []string {
  703. return c.productVariables.Platform_version_active_codenames
  704. }
  705. func (c *config) ProductAAPTConfig() []string {
  706. return c.productVariables.AAPTConfig
  707. }
  708. func (c *config) ProductAAPTPreferredConfig() string {
  709. return String(c.productVariables.AAPTPreferredConfig)
  710. }
  711. func (c *config) ProductAAPTCharacteristics() string {
  712. return String(c.productVariables.AAPTCharacteristics)
  713. }
  714. func (c *config) ProductAAPTPrebuiltDPI() []string {
  715. return c.productVariables.AAPTPrebuiltDPI
  716. }
  717. func (c *config) DefaultAppCertificateDir(ctx PathContext) SourcePath {
  718. defaultCert := String(c.productVariables.DefaultAppCertificate)
  719. if defaultCert != "" {
  720. return PathForSource(ctx, filepath.Dir(defaultCert))
  721. }
  722. return PathForSource(ctx, "build/make/target/product/security")
  723. }
  724. func (c *config) DefaultAppCertificate(ctx PathContext) (pem, key SourcePath) {
  725. defaultCert := String(c.productVariables.DefaultAppCertificate)
  726. if defaultCert != "" {
  727. return PathForSource(ctx, defaultCert+".x509.pem"), PathForSource(ctx, defaultCert+".pk8")
  728. }
  729. defaultDir := c.DefaultAppCertificateDir(ctx)
  730. return defaultDir.Join(ctx, "testkey.x509.pem"), defaultDir.Join(ctx, "testkey.pk8")
  731. }
  732. func (c *config) ApexKeyDir(ctx ModuleContext) SourcePath {
  733. // TODO(b/121224311): define another variable such as TARGET_APEX_KEY_OVERRIDE
  734. defaultCert := String(c.productVariables.DefaultAppCertificate)
  735. if defaultCert == "" || filepath.Dir(defaultCert) == "build/make/target/product/security" {
  736. // When defaultCert is unset or is set to the testkeys path, use the APEX keys
  737. // that is under the module dir
  738. return pathForModuleSrc(ctx)
  739. }
  740. // If not, APEX keys are under the specified directory
  741. return PathForSource(ctx, filepath.Dir(defaultCert))
  742. }
  743. // AllowMissingDependencies configures Blueprint/Soong to not fail when modules
  744. // are configured to depend on non-existent modules. Note that this does not
  745. // affect missing input dependencies at the Ninja level.
  746. func (c *config) AllowMissingDependencies() bool {
  747. return Bool(c.productVariables.Allow_missing_dependencies)
  748. }
  749. // Returns true if a full platform source tree cannot be assumed.
  750. func (c *config) UnbundledBuild() bool {
  751. return Bool(c.productVariables.Unbundled_build)
  752. }
  753. // Returns true if building apps that aren't bundled with the platform.
  754. // UnbundledBuild() is always true when this is true.
  755. func (c *config) UnbundledBuildApps() bool {
  756. return len(c.productVariables.Unbundled_build_apps) > 0
  757. }
  758. // Returns true if building image that aren't bundled with the platform.
  759. // UnbundledBuild() is always true when this is true.
  760. func (c *config) UnbundledBuildImage() bool {
  761. return Bool(c.productVariables.Unbundled_build_image)
  762. }
  763. // Returns true if building modules against prebuilt SDKs.
  764. func (c *config) AlwaysUsePrebuiltSdks() bool {
  765. return Bool(c.productVariables.Always_use_prebuilt_sdks)
  766. }
  767. func (c *config) MinimizeJavaDebugInfo() bool {
  768. return Bool(c.productVariables.MinimizeJavaDebugInfo) && !Bool(c.productVariables.Eng)
  769. }
  770. func (c *config) Debuggable() bool {
  771. return Bool(c.productVariables.Debuggable)
  772. }
  773. func (c *config) Eng() bool {
  774. return Bool(c.productVariables.Eng)
  775. }
  776. // DevicePrimaryArchType returns the ArchType for the first configured device architecture, or
  777. // Common if there are no device architectures.
  778. func (c *config) DevicePrimaryArchType() ArchType {
  779. if androidTargets := c.Targets[Android]; len(androidTargets) > 0 {
  780. return androidTargets[0].Arch.ArchType
  781. }
  782. return Common
  783. }
  784. func (c *config) SanitizeHost() []string {
  785. return append([]string(nil), c.productVariables.SanitizeHost...)
  786. }
  787. func (c *config) SanitizeDevice() []string {
  788. return append([]string(nil), c.productVariables.SanitizeDevice...)
  789. }
  790. func (c *config) SanitizeDeviceDiag() []string {
  791. return append([]string(nil), c.productVariables.SanitizeDeviceDiag...)
  792. }
  793. func (c *config) SanitizeDeviceArch() []string {
  794. return append([]string(nil), c.productVariables.SanitizeDeviceArch...)
  795. }
  796. func (c *config) EnableCFI() bool {
  797. if c.productVariables.EnableCFI == nil {
  798. return true
  799. }
  800. return *c.productVariables.EnableCFI
  801. }
  802. func (c *config) DisableScudo() bool {
  803. return Bool(c.productVariables.DisableScudo)
  804. }
  805. func (c *config) Android64() bool {
  806. for _, t := range c.Targets[Android] {
  807. if t.Arch.ArchType.Multilib == "lib64" {
  808. return true
  809. }
  810. }
  811. return false
  812. }
  813. func (c *config) UseGoma() bool {
  814. return Bool(c.productVariables.UseGoma)
  815. }
  816. func (c *config) UseRBE() bool {
  817. return Bool(c.productVariables.UseRBE)
  818. }
  819. func (c *config) UseRBEJAVAC() bool {
  820. return Bool(c.productVariables.UseRBEJAVAC)
  821. }
  822. func (c *config) UseRBER8() bool {
  823. return Bool(c.productVariables.UseRBER8)
  824. }
  825. func (c *config) UseRBED8() bool {
  826. return Bool(c.productVariables.UseRBED8)
  827. }
  828. func (c *config) UseRemoteBuild() bool {
  829. return c.UseGoma() || c.UseRBE()
  830. }
  831. func (c *config) RunErrorProne() bool {
  832. return c.IsEnvTrue("RUN_ERROR_PRONE")
  833. }
  834. // XrefCorpusName returns the Kythe cross-reference corpus name.
  835. func (c *config) XrefCorpusName() string {
  836. return c.Getenv("XREF_CORPUS")
  837. }
  838. // XrefCuEncoding returns the compilation unit encoding to use for Kythe code
  839. // xrefs. Can be 'json' (default), 'proto' or 'all'.
  840. func (c *config) XrefCuEncoding() string {
  841. if enc := c.Getenv("KYTHE_KZIP_ENCODING"); enc != "" {
  842. return enc
  843. }
  844. return "json"
  845. }
  846. // XrefCuJavaSourceMax returns the maximum number of the Java source files
  847. // in a single compilation unit
  848. const xrefJavaSourceFileMaxDefault = "1000"
  849. func (c Config) XrefCuJavaSourceMax() string {
  850. v := c.Getenv("KYTHE_JAVA_SOURCE_BATCH_SIZE")
  851. if v == "" {
  852. return xrefJavaSourceFileMaxDefault
  853. }
  854. if _, err := strconv.ParseUint(v, 0, 0); err != nil {
  855. fmt.Fprintf(os.Stderr,
  856. "bad KYTHE_JAVA_SOURCE_BATCH_SIZE value: %s, will use %s",
  857. err, xrefJavaSourceFileMaxDefault)
  858. return xrefJavaSourceFileMaxDefault
  859. }
  860. return v
  861. }
  862. func (c *config) EmitXrefRules() bool {
  863. return c.XrefCorpusName() != ""
  864. }
  865. func (c *config) ClangTidy() bool {
  866. return Bool(c.productVariables.ClangTidy)
  867. }
  868. func (c *config) TidyChecks() string {
  869. if c.productVariables.TidyChecks == nil {
  870. return ""
  871. }
  872. return *c.productVariables.TidyChecks
  873. }
  874. func (c *config) LibartImgHostBaseAddress() string {
  875. return "0x60000000"
  876. }
  877. func (c *config) LibartImgDeviceBaseAddress() string {
  878. return "0x70000000"
  879. }
  880. func (c *config) ArtUseReadBarrier() bool {
  881. return Bool(c.productVariables.ArtUseReadBarrier)
  882. }
  883. // Enforce Runtime Resource Overlays for a module. RROs supersede static RROs,
  884. // but some modules still depend on it.
  885. //
  886. // More info: https://source.android.com/devices/architecture/rros
  887. func (c *config) EnforceRROForModule(name string) bool {
  888. enforceList := c.productVariables.EnforceRROTargets
  889. if len(enforceList) > 0 {
  890. if InList("*", enforceList) {
  891. return true
  892. }
  893. return InList(name, enforceList)
  894. }
  895. return false
  896. }
  897. func (c *config) EnforceRROExcludedOverlay(path string) bool {
  898. excluded := c.productVariables.EnforceRROExcludedOverlays
  899. if len(excluded) > 0 {
  900. return HasAnyPrefix(path, excluded)
  901. }
  902. return false
  903. }
  904. func (c *config) ExportedNamespaces() []string {
  905. return append([]string(nil), c.productVariables.NamespacesToExport...)
  906. }
  907. func (c *config) HostStaticBinaries() bool {
  908. return Bool(c.productVariables.HostStaticBinaries)
  909. }
  910. func (c *config) UncompressPrivAppDex() bool {
  911. return Bool(c.productVariables.UncompressPrivAppDex)
  912. }
  913. func (c *config) ModulesLoadedByPrivilegedModules() []string {
  914. return c.productVariables.ModulesLoadedByPrivilegedModules
  915. }
  916. // DexpreoptGlobalConfigPath returns the path to the dexpreopt.config file in
  917. // the output directory, if it was created during the product configuration
  918. // phase by Kati.
  919. func (c *config) DexpreoptGlobalConfigPath(ctx PathContext) OptionalPath {
  920. if c.productVariables.DexpreoptGlobalConfig == nil {
  921. return OptionalPathForPath(nil)
  922. }
  923. return OptionalPathForPath(
  924. pathForBuildToolDep(ctx, *c.productVariables.DexpreoptGlobalConfig))
  925. }
  926. // DexpreoptGlobalConfig returns the raw byte contents of the dexpreopt global
  927. // configuration. Since the configuration file was created by Kati during
  928. // product configuration (externally of soong_build), it's not tracked, so we
  929. // also manually add a Ninja file dependency on the configuration file to the
  930. // rule that creates the main build.ninja file. This ensures that build.ninja is
  931. // regenerated correctly if dexpreopt.config changes.
  932. func (c *config) DexpreoptGlobalConfig(ctx PathContext) ([]byte, error) {
  933. path := c.DexpreoptGlobalConfigPath(ctx)
  934. if !path.Valid() {
  935. return nil, nil
  936. }
  937. ctx.AddNinjaFileDeps(path.String())
  938. return ioutil.ReadFile(absolutePath(path.String()))
  939. }
  940. func (c *deviceConfig) WithDexpreopt() bool {
  941. return c.config.productVariables.WithDexpreopt
  942. }
  943. func (c *config) FrameworksBaseDirExists(ctx PathContext) bool {
  944. return ExistentPathForSource(ctx, "frameworks", "base", "Android.bp").Valid()
  945. }
  946. func (c *config) VndkSnapshotBuildArtifacts() bool {
  947. return Bool(c.productVariables.VndkSnapshotBuildArtifacts)
  948. }
  949. func (c *config) HasMultilibConflict(arch ArchType) bool {
  950. return c.multilibConflicts[arch]
  951. }
  952. func (c *config) PrebuiltHiddenApiDir(ctx PathContext) string {
  953. return String(c.productVariables.PrebuiltHiddenApiDir)
  954. }
  955. func (c *deviceConfig) Arches() []Arch {
  956. var arches []Arch
  957. for _, target := range c.config.Targets[Android] {
  958. arches = append(arches, target.Arch)
  959. }
  960. return arches
  961. }
  962. func (c *deviceConfig) BinderBitness() string {
  963. is32BitBinder := c.config.productVariables.Binder32bit
  964. if is32BitBinder != nil && *is32BitBinder {
  965. return "32"
  966. }
  967. return "64"
  968. }
  969. func (c *deviceConfig) VendorPath() string {
  970. if c.config.productVariables.VendorPath != nil {
  971. return *c.config.productVariables.VendorPath
  972. }
  973. return "vendor"
  974. }
  975. func (c *deviceConfig) VndkVersion() string {
  976. return String(c.config.productVariables.DeviceVndkVersion)
  977. }
  978. func (c *deviceConfig) RecoverySnapshotVersion() string {
  979. return String(c.config.productVariables.RecoverySnapshotVersion)
  980. }
  981. func (c *deviceConfig) CurrentApiLevelForVendorModules() string {
  982. return StringDefault(c.config.productVariables.DeviceCurrentApiLevelForVendorModules, "current")
  983. }
  984. func (c *deviceConfig) PlatformVndkVersion() string {
  985. return String(c.config.productVariables.Platform_vndk_version)
  986. }
  987. func (c *deviceConfig) ProductVndkVersion() string {
  988. return String(c.config.productVariables.ProductVndkVersion)
  989. }
  990. func (c *deviceConfig) ExtraVndkVersions() []string {
  991. return c.config.productVariables.ExtraVndkVersions
  992. }
  993. func (c *deviceConfig) VndkUseCoreVariant() bool {
  994. return Bool(c.config.productVariables.VndkUseCoreVariant)
  995. }
  996. func (c *deviceConfig) SystemSdkVersions() []string {
  997. return c.config.productVariables.DeviceSystemSdkVersions
  998. }
  999. func (c *deviceConfig) PlatformSystemSdkVersions() []string {
  1000. return c.config.productVariables.Platform_systemsdk_versions
  1001. }
  1002. func (c *deviceConfig) OdmPath() string {
  1003. if c.config.productVariables.OdmPath != nil {
  1004. return *c.config.productVariables.OdmPath
  1005. }
  1006. return "odm"
  1007. }
  1008. func (c *deviceConfig) ProductPath() string {
  1009. if c.config.productVariables.ProductPath != nil {
  1010. return *c.config.productVariables.ProductPath
  1011. }
  1012. return "product"
  1013. }
  1014. func (c *deviceConfig) SystemExtPath() string {
  1015. if c.config.productVariables.SystemExtPath != nil {
  1016. return *c.config.productVariables.SystemExtPath
  1017. }
  1018. return "system_ext"
  1019. }
  1020. func (c *deviceConfig) BtConfigIncludeDir() string {
  1021. return String(c.config.productVariables.BtConfigIncludeDir)
  1022. }
  1023. func (c *deviceConfig) DeviceKernelHeaderDirs() []string {
  1024. return c.config.productVariables.DeviceKernelHeaders
  1025. }
  1026. // JavaCoverageEnabledForPath returns whether Java code coverage is enabled for
  1027. // path. Coverage is enabled by default when the product variable
  1028. // JavaCoveragePaths is empty. If JavaCoveragePaths is not empty, coverage is
  1029. // enabled for any path which is part of this variable (and not part of the
  1030. // JavaCoverageExcludePaths product variable). Value "*" in JavaCoveragePaths
  1031. // represents any path.
  1032. func (c *deviceConfig) JavaCoverageEnabledForPath(path string) bool {
  1033. coverage := false
  1034. if len(c.config.productVariables.JavaCoveragePaths) == 0 ||
  1035. InList("*", c.config.productVariables.JavaCoveragePaths) ||
  1036. HasAnyPrefix(path, c.config.productVariables.JavaCoveragePaths) {
  1037. coverage = true
  1038. }
  1039. if coverage && len(c.config.productVariables.JavaCoverageExcludePaths) > 0 {
  1040. if HasAnyPrefix(path, c.config.productVariables.JavaCoverageExcludePaths) {
  1041. coverage = false
  1042. }
  1043. }
  1044. return coverage
  1045. }
  1046. // Returns true if gcov or clang coverage is enabled.
  1047. func (c *deviceConfig) NativeCoverageEnabled() bool {
  1048. return Bool(c.config.productVariables.GcovCoverage) ||
  1049. Bool(c.config.productVariables.ClangCoverage)
  1050. }
  1051. func (c *deviceConfig) ClangCoverageEnabled() bool {
  1052. return Bool(c.config.productVariables.ClangCoverage)
  1053. }
  1054. func (c *deviceConfig) ClangCoverageContinuousMode() bool {
  1055. return Bool(c.config.productVariables.ClangCoverageContinuousMode)
  1056. }
  1057. func (c *deviceConfig) GcovCoverageEnabled() bool {
  1058. return Bool(c.config.productVariables.GcovCoverage)
  1059. }
  1060. // NativeCoverageEnabledForPath returns whether (GCOV- or Clang-based) native
  1061. // code coverage is enabled for path. By default, coverage is not enabled for a
  1062. // given path unless it is part of the NativeCoveragePaths product variable (and
  1063. // not part of the NativeCoverageExcludePaths product variable). Value "*" in
  1064. // NativeCoveragePaths represents any path.
  1065. func (c *deviceConfig) NativeCoverageEnabledForPath(path string) bool {
  1066. coverage := false
  1067. if len(c.config.productVariables.NativeCoveragePaths) > 0 {
  1068. if InList("*", c.config.productVariables.NativeCoveragePaths) || HasAnyPrefix(path, c.config.productVariables.NativeCoveragePaths) {
  1069. coverage = true
  1070. }
  1071. }
  1072. if coverage && len(c.config.productVariables.NativeCoverageExcludePaths) > 0 {
  1073. if HasAnyPrefix(path, c.config.productVariables.NativeCoverageExcludePaths) {
  1074. coverage = false
  1075. }
  1076. }
  1077. return coverage
  1078. }
  1079. func (c *deviceConfig) AfdoAdditionalProfileDirs() []string {
  1080. return c.config.productVariables.AfdoAdditionalProfileDirs
  1081. }
  1082. func (c *deviceConfig) PgoAdditionalProfileDirs() []string {
  1083. return c.config.productVariables.PgoAdditionalProfileDirs
  1084. }
  1085. func (c *deviceConfig) VendorSepolicyDirs() []string {
  1086. return c.config.productVariables.BoardVendorSepolicyDirs
  1087. }
  1088. func (c *deviceConfig) OdmSepolicyDirs() []string {
  1089. return c.config.productVariables.BoardOdmSepolicyDirs
  1090. }
  1091. func (c *deviceConfig) SystemExtPublicSepolicyDirs() []string {
  1092. return c.config.productVariables.SystemExtPublicSepolicyDirs
  1093. }
  1094. func (c *deviceConfig) SystemExtPrivateSepolicyDirs() []string {
  1095. return c.config.productVariables.SystemExtPrivateSepolicyDirs
  1096. }
  1097. func (c *deviceConfig) SepolicyM4Defs() []string {
  1098. return c.config.productVariables.BoardSepolicyM4Defs
  1099. }
  1100. func (c *deviceConfig) OverrideManifestPackageNameFor(name string) (manifestName string, overridden bool) {
  1101. return findOverrideValue(c.config.productVariables.ManifestPackageNameOverrides, name,
  1102. "invalid override rule %q in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES should be <module_name>:<manifest_name>")
  1103. }
  1104. func (c *deviceConfig) OverrideCertificateFor(name string) (certificatePath string, overridden bool) {
  1105. return findOverrideValue(c.config.productVariables.CertificateOverrides, name,
  1106. "invalid override rule %q in PRODUCT_CERTIFICATE_OVERRIDES should be <module_name>:<certificate_module_name>")
  1107. }
  1108. func (c *deviceConfig) OverridePackageNameFor(name string) string {
  1109. newName, overridden := findOverrideValue(
  1110. c.config.productVariables.PackageNameOverrides,
  1111. name,
  1112. "invalid override rule %q in PRODUCT_PACKAGE_NAME_OVERRIDES should be <module_name>:<package_name>")
  1113. if overridden {
  1114. return newName
  1115. }
  1116. return name
  1117. }
  1118. func findOverrideValue(overrides []string, name string, errorMsg string) (newValue string, overridden bool) {
  1119. if overrides == nil || len(overrides) == 0 {
  1120. return "", false
  1121. }
  1122. for _, o := range overrides {
  1123. split := strings.Split(o, ":")
  1124. if len(split) != 2 {
  1125. // This shouldn't happen as this is first checked in make, but just in case.
  1126. panic(fmt.Errorf(errorMsg, o))
  1127. }
  1128. if matchPattern(split[0], name) {
  1129. return substPattern(split[0], split[1], name), true
  1130. }
  1131. }
  1132. return "", false
  1133. }
  1134. func (c *deviceConfig) ApexGlobalMinSdkVersionOverride() string {
  1135. return String(c.config.productVariables.ApexGlobalMinSdkVersionOverride)
  1136. }
  1137. func (c *config) IntegerOverflowDisabledForPath(path string) bool {
  1138. if len(c.productVariables.IntegerOverflowExcludePaths) == 0 {
  1139. return false
  1140. }
  1141. return HasAnyPrefix(path, c.productVariables.IntegerOverflowExcludePaths)
  1142. }
  1143. func (c *config) CFIDisabledForPath(path string) bool {
  1144. if len(c.productVariables.CFIExcludePaths) == 0 {
  1145. return false
  1146. }
  1147. return HasAnyPrefix(path, c.productVariables.CFIExcludePaths)
  1148. }
  1149. func (c *config) CFIEnabledForPath(path string) bool {
  1150. if len(c.productVariables.CFIIncludePaths) == 0 {
  1151. return false
  1152. }
  1153. return HasAnyPrefix(path, c.productVariables.CFIIncludePaths) && !c.CFIDisabledForPath(path)
  1154. }
  1155. func (c *config) MemtagHeapDisabledForPath(path string) bool {
  1156. if len(c.productVariables.MemtagHeapExcludePaths) == 0 {
  1157. return false
  1158. }
  1159. return HasAnyPrefix(path, c.productVariables.MemtagHeapExcludePaths)
  1160. }
  1161. func (c *config) MemtagHeapAsyncEnabledForPath(path string) bool {
  1162. if len(c.productVariables.MemtagHeapAsyncIncludePaths) == 0 {
  1163. return false
  1164. }
  1165. return HasAnyPrefix(path, c.productVariables.MemtagHeapAsyncIncludePaths) && !c.MemtagHeapDisabledForPath(path)
  1166. }
  1167. func (c *config) MemtagHeapSyncEnabledForPath(path string) bool {
  1168. if len(c.productVariables.MemtagHeapSyncIncludePaths) == 0 {
  1169. return false
  1170. }
  1171. return HasAnyPrefix(path, c.productVariables.MemtagHeapSyncIncludePaths) && !c.MemtagHeapDisabledForPath(path)
  1172. }
  1173. func (c *config) VendorConfig(name string) VendorConfig {
  1174. return soongconfig.Config(c.productVariables.VendorVars[name])
  1175. }
  1176. func (c *config) NdkAbis() bool {
  1177. return Bool(c.productVariables.Ndk_abis)
  1178. }
  1179. func (c *config) AmlAbis() bool {
  1180. return Bool(c.productVariables.Aml_abis)
  1181. }
  1182. func (c *config) FlattenApex() bool {
  1183. return Bool(c.productVariables.Flatten_apex)
  1184. }
  1185. func (c *config) ForceApexSymlinkOptimization() bool {
  1186. return Bool(c.productVariables.ForceApexSymlinkOptimization)
  1187. }
  1188. func (c *config) CompressedApex() bool {
  1189. return Bool(c.productVariables.CompressedApex)
  1190. }
  1191. func (c *config) EnforceSystemCertificate() bool {
  1192. return Bool(c.productVariables.EnforceSystemCertificate)
  1193. }
  1194. func (c *config) EnforceSystemCertificateAllowList() []string {
  1195. return c.productVariables.EnforceSystemCertificateAllowList
  1196. }
  1197. func (c *config) EnforceProductPartitionInterface() bool {
  1198. return Bool(c.productVariables.EnforceProductPartitionInterface)
  1199. }
  1200. func (c *config) EnforceInterPartitionJavaSdkLibrary() bool {
  1201. return Bool(c.productVariables.EnforceInterPartitionJavaSdkLibrary)
  1202. }
  1203. func (c *config) InterPartitionJavaLibraryAllowList() []string {
  1204. return c.productVariables.InterPartitionJavaLibraryAllowList
  1205. }
  1206. func (c *config) InstallExtraFlattenedApexes() bool {
  1207. return Bool(c.productVariables.InstallExtraFlattenedApexes)
  1208. }
  1209. func (c *config) ProductHiddenAPIStubs() []string {
  1210. return c.productVariables.ProductHiddenAPIStubs
  1211. }
  1212. func (c *config) ProductHiddenAPIStubsSystem() []string {
  1213. return c.productVariables.ProductHiddenAPIStubsSystem
  1214. }
  1215. func (c *config) ProductHiddenAPIStubsTest() []string {
  1216. return c.productVariables.ProductHiddenAPIStubsTest
  1217. }
  1218. func (c *deviceConfig) TargetFSConfigGen() []string {
  1219. return c.config.productVariables.TargetFSConfigGen
  1220. }
  1221. func (c *config) ProductPublicSepolicyDirs() []string {
  1222. return c.productVariables.ProductPublicSepolicyDirs
  1223. }
  1224. func (c *config) ProductPrivateSepolicyDirs() []string {
  1225. return c.productVariables.ProductPrivateSepolicyDirs
  1226. }
  1227. func (c *config) MissingUsesLibraries() []string {
  1228. return c.productVariables.MissingUsesLibraries
  1229. }
  1230. func (c *deviceConfig) DeviceArch() string {
  1231. return String(c.config.productVariables.DeviceArch)
  1232. }
  1233. func (c *deviceConfig) DeviceArchVariant() string {
  1234. return String(c.config.productVariables.DeviceArchVariant)
  1235. }
  1236. func (c *deviceConfig) DeviceSecondaryArch() string {
  1237. return String(c.config.productVariables.DeviceSecondaryArch)
  1238. }
  1239. func (c *deviceConfig) DeviceSecondaryArchVariant() string {
  1240. return String(c.config.productVariables.DeviceSecondaryArchVariant)
  1241. }
  1242. func (c *deviceConfig) BoardUsesRecoveryAsBoot() bool {
  1243. return Bool(c.config.productVariables.BoardUsesRecoveryAsBoot)
  1244. }
  1245. func (c *deviceConfig) BoardKernelBinaries() []string {
  1246. return c.config.productVariables.BoardKernelBinaries
  1247. }
  1248. func (c *deviceConfig) BoardKernelModuleInterfaceVersions() []string {
  1249. return c.config.productVariables.BoardKernelModuleInterfaceVersions
  1250. }
  1251. func (c *deviceConfig) BoardMoveRecoveryResourcesToVendorBoot() bool {
  1252. return Bool(c.config.productVariables.BoardMoveRecoveryResourcesToVendorBoot)
  1253. }
  1254. func (c *deviceConfig) PlatformSepolicyVersion() string {
  1255. return String(c.config.productVariables.PlatformSepolicyVersion)
  1256. }
  1257. func (c *deviceConfig) TotSepolicyVersion() string {
  1258. return String(c.config.productVariables.TotSepolicyVersion)
  1259. }
  1260. func (c *deviceConfig) PlatformSepolicyCompatVersions() []string {
  1261. return c.config.productVariables.PlatformSepolicyCompatVersions
  1262. }
  1263. func (c *deviceConfig) BoardSepolicyVers() string {
  1264. if ver := String(c.config.productVariables.BoardSepolicyVers); ver != "" {
  1265. return ver
  1266. }
  1267. return c.PlatformSepolicyVersion()
  1268. }
  1269. func (c *deviceConfig) BoardPlatVendorPolicy() []string {
  1270. return c.config.productVariables.BoardPlatVendorPolicy
  1271. }
  1272. func (c *deviceConfig) BoardReqdMaskPolicy() []string {
  1273. return c.config.productVariables.BoardReqdMaskPolicy
  1274. }
  1275. func (c *deviceConfig) BoardSystemExtPublicPrebuiltDirs() []string {
  1276. return c.config.productVariables.BoardSystemExtPublicPrebuiltDirs
  1277. }
  1278. func (c *deviceConfig) BoardSystemExtPrivatePrebuiltDirs() []string {
  1279. return c.config.productVariables.BoardSystemExtPrivatePrebuiltDirs
  1280. }
  1281. func (c *deviceConfig) BoardProductPublicPrebuiltDirs() []string {
  1282. return c.config.productVariables.BoardProductPublicPrebuiltDirs
  1283. }
  1284. func (c *deviceConfig) BoardProductPrivatePrebuiltDirs() []string {
  1285. return c.config.productVariables.BoardProductPrivatePrebuiltDirs
  1286. }
  1287. func (c *deviceConfig) SystemExtSepolicyPrebuiltApiDir() string {
  1288. return String(c.config.productVariables.SystemExtSepolicyPrebuiltApiDir)
  1289. }
  1290. func (c *deviceConfig) ProductSepolicyPrebuiltApiDir() string {
  1291. return String(c.config.productVariables.ProductSepolicyPrebuiltApiDir)
  1292. }
  1293. func (c *deviceConfig) IsPartnerTrebleSepolicyTestEnabled() bool {
  1294. return c.SystemExtSepolicyPrebuiltApiDir() != "" || c.ProductSepolicyPrebuiltApiDir() != ""
  1295. }
  1296. func (c *deviceConfig) DirectedVendorSnapshot() bool {
  1297. return c.config.productVariables.DirectedVendorSnapshot
  1298. }
  1299. func (c *deviceConfig) VendorSnapshotModules() map[string]bool {
  1300. return c.config.productVariables.VendorSnapshotModules
  1301. }
  1302. func (c *deviceConfig) DirectedRecoverySnapshot() bool {
  1303. return c.config.productVariables.DirectedRecoverySnapshot
  1304. }
  1305. func (c *deviceConfig) RecoverySnapshotModules() map[string]bool {
  1306. return c.config.productVariables.RecoverySnapshotModules
  1307. }
  1308. func createDirsMap(previous map[string]bool, dirs []string) (map[string]bool, error) {
  1309. var ret = make(map[string]bool)
  1310. for _, dir := range dirs {
  1311. clean := filepath.Clean(dir)
  1312. if previous[clean] || ret[clean] {
  1313. return nil, fmt.Errorf("Duplicate entry %s", dir)
  1314. }
  1315. ret[clean] = true
  1316. }
  1317. return ret, nil
  1318. }
  1319. func (c *deviceConfig) createDirsMapOnce(onceKey OnceKey, previous map[string]bool, dirs []string) map[string]bool {
  1320. dirMap := c.Once(onceKey, func() interface{} {
  1321. ret, err := createDirsMap(previous, dirs)
  1322. if err != nil {
  1323. panic(fmt.Errorf("%s: %w", onceKey.key, err))
  1324. }
  1325. return ret
  1326. })
  1327. if dirMap == nil {
  1328. return nil
  1329. }
  1330. return dirMap.(map[string]bool)
  1331. }
  1332. var vendorSnapshotDirsExcludedKey = NewOnceKey("VendorSnapshotDirsExcludedMap")
  1333. func (c *deviceConfig) VendorSnapshotDirsExcludedMap() map[string]bool {
  1334. return c.createDirsMapOnce(vendorSnapshotDirsExcludedKey, nil,
  1335. c.config.productVariables.VendorSnapshotDirsExcluded)
  1336. }
  1337. var vendorSnapshotDirsIncludedKey = NewOnceKey("VendorSnapshotDirsIncludedMap")
  1338. func (c *deviceConfig) VendorSnapshotDirsIncludedMap() map[string]bool {
  1339. excludedMap := c.VendorSnapshotDirsExcludedMap()
  1340. return c.createDirsMapOnce(vendorSnapshotDirsIncludedKey, excludedMap,
  1341. c.config.productVariables.VendorSnapshotDirsIncluded)
  1342. }
  1343. var recoverySnapshotDirsExcludedKey = NewOnceKey("RecoverySnapshotDirsExcludedMap")
  1344. func (c *deviceConfig) RecoverySnapshotDirsExcludedMap() map[string]bool {
  1345. return c.createDirsMapOnce(recoverySnapshotDirsExcludedKey, nil,
  1346. c.config.productVariables.RecoverySnapshotDirsExcluded)
  1347. }
  1348. var recoverySnapshotDirsIncludedKey = NewOnceKey("RecoverySnapshotDirsIncludedMap")
  1349. func (c *deviceConfig) RecoverySnapshotDirsIncludedMap() map[string]bool {
  1350. excludedMap := c.RecoverySnapshotDirsExcludedMap()
  1351. return c.createDirsMapOnce(recoverySnapshotDirsIncludedKey, excludedMap,
  1352. c.config.productVariables.RecoverySnapshotDirsIncluded)
  1353. }
  1354. func (c *deviceConfig) HostFakeSnapshotEnabled() bool {
  1355. return c.config.productVariables.HostFakeSnapshotEnabled
  1356. }
  1357. func (c *deviceConfig) ShippingApiLevel() ApiLevel {
  1358. if c.config.productVariables.ShippingApiLevel == nil {
  1359. return NoneApiLevel
  1360. }
  1361. apiLevel, _ := strconv.Atoi(*c.config.productVariables.ShippingApiLevel)
  1362. return uncheckedFinalApiLevel(apiLevel)
  1363. }
  1364. func (c *deviceConfig) BuildBrokenEnforceSyspropOwner() bool {
  1365. return c.config.productVariables.BuildBrokenEnforceSyspropOwner
  1366. }
  1367. func (c *deviceConfig) BuildBrokenTrebleSyspropNeverallow() bool {
  1368. return c.config.productVariables.BuildBrokenTrebleSyspropNeverallow
  1369. }
  1370. func (c *deviceConfig) BuildDebugfsRestrictionsEnabled() bool {
  1371. return c.config.productVariables.BuildDebugfsRestrictionsEnabled
  1372. }
  1373. func (c *deviceConfig) BuildBrokenVendorPropertyNamespace() bool {
  1374. return c.config.productVariables.BuildBrokenVendorPropertyNamespace
  1375. }
  1376. func (c *deviceConfig) BuildBrokenInputDir(name string) bool {
  1377. return InList(name, c.config.productVariables.BuildBrokenInputDirModules)
  1378. }
  1379. func (c *deviceConfig) RequiresInsecureExecmemForSwiftshader() bool {
  1380. return c.config.productVariables.RequiresInsecureExecmemForSwiftshader
  1381. }
  1382. func (c *config) SelinuxIgnoreNeverallows() bool {
  1383. return c.productVariables.SelinuxIgnoreNeverallows
  1384. }
  1385. func (c *deviceConfig) SepolicySplit() bool {
  1386. return c.config.productVariables.SepolicySplit
  1387. }
  1388. func (c *deviceConfig) SepolicyFreezeTestExtraDirs() []string {
  1389. return c.config.productVariables.SepolicyFreezeTestExtraDirs
  1390. }
  1391. func (c *deviceConfig) SepolicyFreezeTestExtraPrebuiltDirs() []string {
  1392. return c.config.productVariables.SepolicyFreezeTestExtraPrebuiltDirs
  1393. }
  1394. func (c *deviceConfig) GenerateAidlNdkPlatformBackend() bool {
  1395. return c.config.productVariables.GenerateAidlNdkPlatformBackend
  1396. }
  1397. // The ConfiguredJarList struct provides methods for handling a list of (apex, jar) pairs.
  1398. // Such lists are used in the build system for things like bootclasspath jars or system server jars.
  1399. // The apex part is either an apex name, or a special names "platform" or "system_ext". Jar is a
  1400. // module name. The pairs come from Make product variables as a list of colon-separated strings.
  1401. //
  1402. // Examples:
  1403. // - "com.android.art:core-oj"
  1404. // - "platform:framework"
  1405. // - "system_ext:foo"
  1406. //
  1407. type ConfiguredJarList struct {
  1408. // A list of apex components, which can be an apex name,
  1409. // or special names like "platform" or "system_ext".
  1410. apexes []string
  1411. // A list of jar module name components.
  1412. jars []string
  1413. }
  1414. // Len returns the length of the list of jars.
  1415. func (l *ConfiguredJarList) Len() int {
  1416. return len(l.jars)
  1417. }
  1418. // Jar returns the idx-th jar component of (apex, jar) pairs.
  1419. func (l *ConfiguredJarList) Jar(idx int) string {
  1420. return l.jars[idx]
  1421. }
  1422. // Apex returns the idx-th apex component of (apex, jar) pairs.
  1423. func (l *ConfiguredJarList) Apex(idx int) string {
  1424. return l.apexes[idx]
  1425. }
  1426. // ContainsJar returns true if the (apex, jar) pairs contains a pair with the
  1427. // given jar module name.
  1428. func (l *ConfiguredJarList) ContainsJar(jar string) bool {
  1429. return InList(jar, l.jars)
  1430. }
  1431. // If the list contains the given (apex, jar) pair.
  1432. func (l *ConfiguredJarList) containsApexJarPair(apex, jar string) bool {
  1433. for i := 0; i < l.Len(); i++ {
  1434. if apex == l.apexes[i] && jar == l.jars[i] {
  1435. return true
  1436. }
  1437. }
  1438. return false
  1439. }
  1440. // ApexOfJar returns the apex component of the first pair with the given jar name on the list, or
  1441. // an empty string if not found.
  1442. func (l *ConfiguredJarList) ApexOfJar(jar string) string {
  1443. if idx := IndexList(jar, l.jars); idx != -1 {
  1444. return l.Apex(IndexList(jar, l.jars))
  1445. }
  1446. return ""
  1447. }
  1448. // IndexOfJar returns the first pair with the given jar name on the list, or -1
  1449. // if not found.
  1450. func (l *ConfiguredJarList) IndexOfJar(jar string) int {
  1451. return IndexList(jar, l.jars)
  1452. }
  1453. func copyAndAppend(list []string, item string) []string {
  1454. // Create the result list to be 1 longer than the input.
  1455. result := make([]string, len(list)+1)
  1456. // Copy the whole input list into the result.
  1457. count := copy(result, list)
  1458. // Insert the extra item at the end.
  1459. result[count] = item
  1460. return result
  1461. }
  1462. // Append an (apex, jar) pair to the list.
  1463. func (l *ConfiguredJarList) Append(apex string, jar string) ConfiguredJarList {
  1464. // Create a copy of the backing arrays before appending to avoid sharing backing
  1465. // arrays that are mutated across instances.
  1466. apexes := copyAndAppend(l.apexes, apex)
  1467. jars := copyAndAppend(l.jars, jar)
  1468. return ConfiguredJarList{apexes, jars}
  1469. }
  1470. // Append a list of (apex, jar) pairs to the list.
  1471. func (l *ConfiguredJarList) AppendList(other *ConfiguredJarList) ConfiguredJarList {
  1472. apexes := make([]string, 0, l.Len()+other.Len())
  1473. jars := make([]string, 0, l.Len()+other.Len())
  1474. apexes = append(apexes, l.apexes...)
  1475. jars = append(jars, l.jars...)
  1476. apexes = append(apexes, other.apexes...)
  1477. jars = append(jars, other.jars...)
  1478. return ConfiguredJarList{apexes, jars}
  1479. }
  1480. // RemoveList filters out a list of (apex, jar) pairs from the receiving list of pairs.
  1481. func (l *ConfiguredJarList) RemoveList(list ConfiguredJarList) ConfiguredJarList {
  1482. apexes := make([]string, 0, l.Len())
  1483. jars := make([]string, 0, l.Len())
  1484. for i, jar := range l.jars {
  1485. apex := l.apexes[i]
  1486. if !list.containsApexJarPair(apex, jar) {
  1487. apexes = append(apexes, apex)
  1488. jars = append(jars, jar)
  1489. }
  1490. }
  1491. return ConfiguredJarList{apexes, jars}
  1492. }
  1493. // Filter keeps the entries if a jar appears in the given list of jars to keep. Returns a new list
  1494. // and any remaining jars that are not on this list.
  1495. func (l *ConfiguredJarList) Filter(jarsToKeep []string) (ConfiguredJarList, []string) {
  1496. var apexes []string
  1497. var jars []string
  1498. for i, jar := range l.jars {
  1499. if InList(jar, jarsToKeep) {
  1500. apexes = append(apexes, l.apexes[i])
  1501. jars = append(jars, jar)
  1502. }
  1503. }
  1504. return ConfiguredJarList{apexes, jars}, RemoveListFromList(jarsToKeep, jars)
  1505. }
  1506. // CopyOfJars returns a copy of the list of strings containing jar module name
  1507. // components.
  1508. func (l *ConfiguredJarList) CopyOfJars() []string {
  1509. return CopyOf(l.jars)
  1510. }
  1511. // CopyOfApexJarPairs returns a copy of the list of strings with colon-separated
  1512. // (apex, jar) pairs.
  1513. func (l *ConfiguredJarList) CopyOfApexJarPairs() []string {
  1514. pairs := make([]string, 0, l.Len())
  1515. for i, jar := range l.jars {
  1516. apex := l.apexes[i]
  1517. pairs = append(pairs, apex+":"+jar)
  1518. }
  1519. return pairs
  1520. }
  1521. // BuildPaths returns a list of build paths based on the given directory prefix.
  1522. func (l *ConfiguredJarList) BuildPaths(ctx PathContext, dir OutputPath) WritablePaths {
  1523. paths := make(WritablePaths, l.Len())
  1524. for i, jar := range l.jars {
  1525. paths[i] = dir.Join(ctx, ModuleStem(jar)+".jar")
  1526. }
  1527. return paths
  1528. }
  1529. // BuildPathsByModule returns a map from module name to build paths based on the given directory
  1530. // prefix.
  1531. func (l *ConfiguredJarList) BuildPathsByModule(ctx PathContext, dir OutputPath) map[string]WritablePath {
  1532. paths := map[string]WritablePath{}
  1533. for _, jar := range l.jars {
  1534. paths[jar] = dir.Join(ctx, ModuleStem(jar)+".jar")
  1535. }
  1536. return paths
  1537. }
  1538. // UnmarshalJSON converts JSON configuration from raw bytes into a
  1539. // ConfiguredJarList structure.
  1540. func (l *ConfiguredJarList) UnmarshalJSON(b []byte) error {
  1541. // Try and unmarshal into a []string each item of which contains a pair
  1542. // <apex>:<jar>.
  1543. var list []string
  1544. err := json.Unmarshal(b, &list)
  1545. if err != nil {
  1546. // Did not work so return
  1547. return err
  1548. }
  1549. apexes, jars, err := splitListOfPairsIntoPairOfLists(list)
  1550. if err != nil {
  1551. return err
  1552. }
  1553. l.apexes = apexes
  1554. l.jars = jars
  1555. return nil
  1556. }
  1557. func (l *ConfiguredJarList) MarshalJSON() ([]byte, error) {
  1558. if len(l.apexes) != len(l.jars) {
  1559. return nil, errors.New(fmt.Sprintf("Inconsistent ConfiguredJarList: apexes: %q, jars: %q", l.apexes, l.jars))
  1560. }
  1561. list := make([]string, 0, len(l.apexes))
  1562. for i := 0; i < len(l.apexes); i++ {
  1563. list = append(list, l.apexes[i]+":"+l.jars[i])
  1564. }
  1565. return json.Marshal(list)
  1566. }
  1567. // ModuleStem hardcodes the stem of framework-minus-apex to return "framework".
  1568. //
  1569. // TODO(b/139391334): hard coded until we find a good way to query the stem of a
  1570. // module before any other mutators are run.
  1571. func ModuleStem(module string) string {
  1572. if module == "framework-minus-apex" {
  1573. return "framework"
  1574. }
  1575. return module
  1576. }
  1577. // DevicePaths computes the on-device paths for the list of (apex, jar) pairs,
  1578. // based on the operating system.
  1579. func (l *ConfiguredJarList) DevicePaths(cfg Config, ostype OsType) []string {
  1580. paths := make([]string, l.Len())
  1581. for i, jar := range l.jars {
  1582. apex := l.apexes[i]
  1583. name := ModuleStem(jar) + ".jar"
  1584. var subdir string
  1585. if apex == "platform" {
  1586. subdir = "system/framework"
  1587. } else if apex == "system_ext" {
  1588. subdir = "system_ext/framework"
  1589. } else {
  1590. subdir = filepath.Join("apex", apex, "javalib")
  1591. }
  1592. if ostype.Class == Host {
  1593. paths[i] = filepath.Join(cfg.Getenv("OUT_DIR"), "host", cfg.PrebuiltOS(), subdir, name)
  1594. } else {
  1595. paths[i] = filepath.Join("/", subdir, name)
  1596. }
  1597. }
  1598. return paths
  1599. }
  1600. func (l *ConfiguredJarList) String() string {
  1601. var pairs []string
  1602. for i := 0; i < l.Len(); i++ {
  1603. pairs = append(pairs, l.apexes[i]+":"+l.jars[i])
  1604. }
  1605. return strings.Join(pairs, ",")
  1606. }
  1607. func splitListOfPairsIntoPairOfLists(list []string) ([]string, []string, error) {
  1608. // Now we need to populate this list by splitting each item in the slice of
  1609. // pairs and appending them to the appropriate list of apexes or jars.
  1610. apexes := make([]string, len(list))
  1611. jars := make([]string, len(list))
  1612. for i, apexjar := range list {
  1613. apex, jar, err := splitConfiguredJarPair(apexjar)
  1614. if err != nil {
  1615. return nil, nil, err
  1616. }
  1617. apexes[i] = apex
  1618. jars[i] = jar
  1619. }
  1620. return apexes, jars, nil
  1621. }
  1622. // Expected format for apexJarValue = <apex name>:<jar name>
  1623. func splitConfiguredJarPair(str string) (string, string, error) {
  1624. pair := strings.SplitN(str, ":", 2)
  1625. if len(pair) == 2 {
  1626. apex := pair[0]
  1627. jar := pair[1]
  1628. if apex == "" {
  1629. return apex, jar, fmt.Errorf("invalid apex '%s' in <apex>:<jar> pair '%s', expected format: <apex>:<jar>", apex, str)
  1630. }
  1631. return apex, jar, nil
  1632. } else {
  1633. return "error-apex", "error-jar", fmt.Errorf("malformed (apex, jar) pair: '%s', expected format: <apex>:<jar>", str)
  1634. }
  1635. }
  1636. // CreateTestConfiguredJarList is a function to create ConfiguredJarList for tests.
  1637. func CreateTestConfiguredJarList(list []string) ConfiguredJarList {
  1638. // Create the ConfiguredJarList in as similar way as it is created at runtime by marshalling to
  1639. // a json list of strings and then unmarshalling into a ConfiguredJarList instance.
  1640. b, err := json.Marshal(list)
  1641. if err != nil {
  1642. panic(err)
  1643. }
  1644. var jarList ConfiguredJarList
  1645. err = json.Unmarshal(b, &jarList)
  1646. if err != nil {
  1647. panic(err)
  1648. }
  1649. return jarList
  1650. }
  1651. // EmptyConfiguredJarList returns an empty jar list.
  1652. func EmptyConfiguredJarList() ConfiguredJarList {
  1653. return ConfiguredJarList{}
  1654. }
  1655. var earlyBootJarsKey = NewOnceKey("earlyBootJars")
  1656. func (c *config) BootJars() []string {
  1657. return c.Once(earlyBootJarsKey, func() interface{} {
  1658. list := c.productVariables.BootJars.CopyOfJars()
  1659. return append(list, c.productVariables.ApexBootJars.CopyOfJars()...)
  1660. }).([]string)
  1661. }
  1662. func (c *config) NonApexBootJars() ConfiguredJarList {
  1663. return c.productVariables.BootJars
  1664. }
  1665. func (c *config) ApexBootJars() ConfiguredJarList {
  1666. return c.productVariables.ApexBootJars
  1667. }
  1668. func (c *config) RBEWrapper() string {
  1669. return c.GetenvWithDefault("RBE_WRAPPER", remoteexec.DefaultWrapperPath)
  1670. }
  1671. // UseHostMusl returns true if the host target has been configured to build against musl libc.
  1672. func (c *config) UseHostMusl() bool {
  1673. return Bool(c.productVariables.HostMusl)
  1674. }