config.go 53 KB

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