config.go 60 KB

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