config.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package android
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "runtime"
  22. "strconv"
  23. "strings"
  24. "sync"
  25. "github.com/google/blueprint"
  26. "github.com/google/blueprint/bootstrap"
  27. "github.com/google/blueprint/pathtools"
  28. "github.com/google/blueprint/proptools"
  29. "android/soong/android/soongconfig"
  30. )
  31. var Bool = proptools.Bool
  32. var String = proptools.String
  33. const FutureApiLevel = 10000
  34. // The configuration file name
  35. const configFileName = "soong.config"
  36. const productVariablesFileName = "soong.variables"
  37. // A FileConfigurableOptions contains options which can be configured by the
  38. // config file. These will be included in the config struct.
  39. type FileConfigurableOptions struct {
  40. Mega_device *bool `json:",omitempty"`
  41. Host_bionic *bool `json:",omitempty"`
  42. }
  43. func (f *FileConfigurableOptions) SetDefaultConfig() {
  44. *f = FileConfigurableOptions{}
  45. }
  46. // A Config object represents the entire build configuration for Android.
  47. type Config struct {
  48. *config
  49. }
  50. func (c Config) BuildDir() string {
  51. return c.buildDir
  52. }
  53. // A DeviceConfig object represents the configuration for a particular device being built. For
  54. // now there will only be one of these, but in the future there may be multiple devices being
  55. // built
  56. type DeviceConfig struct {
  57. *deviceConfig
  58. }
  59. type VendorConfig soongconfig.SoongConfig
  60. type config struct {
  61. FileConfigurableOptions
  62. productVariables productVariables
  63. // Only available on configs created by TestConfig
  64. TestProductVariables *productVariables
  65. PrimaryBuilder string
  66. ConfigFileName string
  67. ProductVariablesFileName string
  68. Targets map[OsType][]Target
  69. BuildOSTarget Target // the Target for tools run on the build machine
  70. BuildOSCommonTarget Target // the Target for common (java) tools run on the build machine
  71. AndroidCommonTarget Target // the Target for common modules for the Android device
  72. // multilibConflicts for an ArchType is true if there is earlier configured device architecture with the same
  73. // multilib value.
  74. multilibConflicts map[ArchType]bool
  75. deviceConfig *deviceConfig
  76. srcDir string // the path of the root source directory
  77. buildDir string // the path of the build output directory
  78. env map[string]string
  79. envLock sync.Mutex
  80. envDeps map[string]string
  81. envFrozen bool
  82. inMake bool
  83. captureBuild bool // true for tests, saves build parameters for each module
  84. ignoreEnvironment bool // true for tests, returns empty from all Getenv calls
  85. stopBefore bootstrap.StopBefore
  86. fs pathtools.FileSystem
  87. mockBpList string
  88. OncePer
  89. }
  90. type deviceConfig struct {
  91. config *config
  92. OncePer
  93. }
  94. type jsonConfigurable interface {
  95. SetDefaultConfig()
  96. }
  97. func loadConfig(config *config) error {
  98. err := loadFromConfigFile(&config.FileConfigurableOptions, absolutePath(config.ConfigFileName))
  99. if err != nil {
  100. return err
  101. }
  102. return loadFromConfigFile(&config.productVariables, absolutePath(config.ProductVariablesFileName))
  103. }
  104. // loads configuration options from a JSON file in the cwd.
  105. func loadFromConfigFile(configurable jsonConfigurable, filename string) error {
  106. // Try to open the file
  107. configFileReader, err := os.Open(filename)
  108. defer configFileReader.Close()
  109. if os.IsNotExist(err) {
  110. // Need to create a file, so that blueprint & ninja don't get in
  111. // a dependency tracking loop.
  112. // Make a file-configurable-options with defaults, write it out using
  113. // a json writer.
  114. configurable.SetDefaultConfig()
  115. err = saveToConfigFile(configurable, filename)
  116. if err != nil {
  117. return err
  118. }
  119. } else if err != nil {
  120. return fmt.Errorf("config file: could not open %s: %s", filename, err.Error())
  121. } else {
  122. // Make a decoder for it
  123. jsonDecoder := json.NewDecoder(configFileReader)
  124. err = jsonDecoder.Decode(configurable)
  125. if err != nil {
  126. return fmt.Errorf("config file: %s did not parse correctly: %s", filename, err.Error())
  127. }
  128. }
  129. // No error
  130. return nil
  131. }
  132. // atomically writes the config file in case two copies of soong_build are running simultaneously
  133. // (for example, docs generation and ninja manifest generation)
  134. func saveToConfigFile(config jsonConfigurable, filename string) error {
  135. data, err := json.MarshalIndent(&config, "", " ")
  136. if err != nil {
  137. return fmt.Errorf("cannot marshal config data: %s", err.Error())
  138. }
  139. f, err := ioutil.TempFile(filepath.Dir(filename), "config")
  140. if err != nil {
  141. return fmt.Errorf("cannot create empty config file %s: %s\n", filename, err.Error())
  142. }
  143. defer os.Remove(f.Name())
  144. defer f.Close()
  145. _, err = f.Write(data)
  146. if err != nil {
  147. return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
  148. }
  149. _, err = f.WriteString("\n")
  150. if err != nil {
  151. return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
  152. }
  153. f.Close()
  154. os.Rename(f.Name(), filename)
  155. return nil
  156. }
  157. // NullConfig returns a mostly empty Config for use by standalone tools like dexpreopt_gen that
  158. // use the android package.
  159. func NullConfig(buildDir string) Config {
  160. return Config{
  161. config: &config{
  162. buildDir: buildDir,
  163. fs: pathtools.OsFs,
  164. },
  165. }
  166. }
  167. // TestConfig returns a Config object suitable for using for tests
  168. func TestConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
  169. envCopy := make(map[string]string)
  170. for k, v := range env {
  171. envCopy[k] = v
  172. }
  173. // Copy the real PATH value to the test environment, it's needed by HostSystemTool() used in x86_darwin_host.go
  174. envCopy["PATH"] = originalEnv["PATH"]
  175. config := &config{
  176. productVariables: productVariables{
  177. DeviceName: stringPtr("test_device"),
  178. Platform_sdk_version: intPtr(30),
  179. DeviceSystemSdkVersions: []string{"14", "15"},
  180. Platform_systemsdk_versions: []string{"29", "30"},
  181. AAPTConfig: []string{"normal", "large", "xlarge", "hdpi", "xhdpi", "xxhdpi"},
  182. AAPTPreferredConfig: stringPtr("xhdpi"),
  183. AAPTCharacteristics: stringPtr("nosdcard"),
  184. AAPTPrebuiltDPI: []string{"xhdpi", "xxhdpi"},
  185. UncompressPrivAppDex: boolPtr(true),
  186. },
  187. buildDir: buildDir,
  188. captureBuild: true,
  189. env: envCopy,
  190. }
  191. config.deviceConfig = &deviceConfig{
  192. config: config,
  193. }
  194. config.TestProductVariables = &config.productVariables
  195. config.mockFileSystem(bp, fs)
  196. if err := config.fromEnv(); err != nil {
  197. panic(err)
  198. }
  199. return Config{config}
  200. }
  201. func TestArchConfigNativeBridge(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
  202. testConfig := TestArchConfig(buildDir, env, bp, fs)
  203. config := testConfig.config
  204. config.Targets[Android] = []Target{
  205. {Android, Arch{ArchType: X86_64, ArchVariant: "silvermont", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", ""},
  206. {Android, Arch{ArchType: X86, ArchVariant: "silvermont", Abi: []string{"armeabi-v7a"}}, NativeBridgeDisabled, "", ""},
  207. {Android, Arch{ArchType: Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridgeEnabled, "x86_64", "arm64"},
  208. {Android, Arch{ArchType: Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridgeEnabled, "x86", "arm"},
  209. }
  210. return testConfig
  211. }
  212. func TestArchConfigFuchsia(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
  213. testConfig := TestConfig(buildDir, env, bp, fs)
  214. config := testConfig.config
  215. config.Targets = map[OsType][]Target{
  216. Fuchsia: []Target{
  217. {Fuchsia, Arch{ArchType: Arm64, ArchVariant: "", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", ""},
  218. },
  219. BuildOs: []Target{
  220. {BuildOs, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", ""},
  221. },
  222. }
  223. return testConfig
  224. }
  225. // TestConfig returns a Config object suitable for using for tests that need to run the arch mutator
  226. func TestArchConfig(buildDir string, env map[string]string, bp string, fs map[string][]byte) Config {
  227. testConfig := TestConfig(buildDir, env, bp, fs)
  228. config := testConfig.config
  229. config.Targets = map[OsType][]Target{
  230. Android: []Target{
  231. {Android, Arch{ArchType: Arm64, ArchVariant: "armv8-a", Abi: []string{"arm64-v8a"}}, NativeBridgeDisabled, "", ""},
  232. {Android, Arch{ArchType: Arm, ArchVariant: "armv7-a-neon", Abi: []string{"armeabi-v7a"}}, NativeBridgeDisabled, "", ""},
  233. },
  234. BuildOs: []Target{
  235. {BuildOs, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", ""},
  236. {BuildOs, Arch{ArchType: X86}, NativeBridgeDisabled, "", ""},
  237. },
  238. }
  239. if runtime.GOOS == "darwin" {
  240. config.Targets[BuildOs] = config.Targets[BuildOs][:1]
  241. }
  242. config.BuildOSTarget = config.Targets[BuildOs][0]
  243. config.BuildOSCommonTarget = getCommonTargets(config.Targets[BuildOs])[0]
  244. config.AndroidCommonTarget = getCommonTargets(config.Targets[Android])[0]
  245. config.TestProductVariables.DeviceArch = proptools.StringPtr("arm64")
  246. config.TestProductVariables.DeviceArchVariant = proptools.StringPtr("armv8-a")
  247. config.TestProductVariables.DeviceSecondaryArch = proptools.StringPtr("arm")
  248. config.TestProductVariables.DeviceSecondaryArchVariant = proptools.StringPtr("armv7-a-neon")
  249. return testConfig
  250. }
  251. // New creates a new Config object. The srcDir argument specifies the path to
  252. // the root source directory. It also loads the config file, if found.
  253. func NewConfig(srcDir, buildDir string) (Config, error) {
  254. // Make a config with default options
  255. config := &config{
  256. ConfigFileName: filepath.Join(buildDir, configFileName),
  257. ProductVariablesFileName: filepath.Join(buildDir, productVariablesFileName),
  258. env: originalEnv,
  259. srcDir: srcDir,
  260. buildDir: buildDir,
  261. multilibConflicts: make(map[ArchType]bool),
  262. fs: pathtools.NewOsFs(absSrcDir),
  263. }
  264. config.deviceConfig = &deviceConfig{
  265. config: config,
  266. }
  267. // Sanity check the build and source directories. This won't catch strange
  268. // configurations with symlinks, but at least checks the obvious cases.
  269. absBuildDir, err := filepath.Abs(buildDir)
  270. if err != nil {
  271. return Config{}, err
  272. }
  273. absSrcDir, err := filepath.Abs(srcDir)
  274. if err != nil {
  275. return Config{}, err
  276. }
  277. if strings.HasPrefix(absSrcDir, absBuildDir) {
  278. return Config{}, fmt.Errorf("Build dir must not contain source directory")
  279. }
  280. // Load any configurable options from the configuration file
  281. err = loadConfig(config)
  282. if err != nil {
  283. return Config{}, err
  284. }
  285. inMakeFile := filepath.Join(buildDir, ".soong.in_make")
  286. if _, err := os.Stat(absolutePath(inMakeFile)); err == nil {
  287. config.inMake = true
  288. }
  289. targets, err := decodeTargetProductVariables(config)
  290. if err != nil {
  291. return Config{}, err
  292. }
  293. // Make the CommonOS OsType available for all products.
  294. targets[CommonOS] = []Target{commonTargetMap[CommonOS.Name]}
  295. var archConfig []archConfig
  296. if Bool(config.Mega_device) {
  297. archConfig = getMegaDeviceConfig()
  298. } else if config.NdkAbis() {
  299. archConfig = getNdkAbisConfig()
  300. } else if config.AmlAbis() {
  301. archConfig = getAmlAbisConfig()
  302. }
  303. if archConfig != nil {
  304. androidTargets, err := decodeArchSettings(Android, archConfig)
  305. if err != nil {
  306. return Config{}, err
  307. }
  308. targets[Android] = androidTargets
  309. }
  310. multilib := make(map[string]bool)
  311. for _, target := range targets[Android] {
  312. if seen := multilib[target.Arch.ArchType.Multilib]; seen {
  313. config.multilibConflicts[target.Arch.ArchType] = true
  314. }
  315. multilib[target.Arch.ArchType.Multilib] = true
  316. }
  317. config.Targets = targets
  318. config.BuildOSTarget = config.Targets[BuildOs][0]
  319. config.BuildOSCommonTarget = getCommonTargets(config.Targets[BuildOs])[0]
  320. if len(config.Targets[Android]) > 0 {
  321. config.AndroidCommonTarget = getCommonTargets(config.Targets[Android])[0]
  322. }
  323. if err := config.fromEnv(); err != nil {
  324. return Config{}, err
  325. }
  326. return Config{config}, nil
  327. }
  328. var TestConfigOsFs = map[string][]byte{}
  329. // mockFileSystem replaces all reads with accesses to the provided map of
  330. // filenames to contents stored as a byte slice.
  331. func (c *config) mockFileSystem(bp string, fs map[string][]byte) {
  332. mockFS := map[string][]byte{}
  333. if _, exists := mockFS["Android.bp"]; !exists {
  334. mockFS["Android.bp"] = []byte(bp)
  335. }
  336. for k, v := range fs {
  337. mockFS[k] = v
  338. }
  339. // no module list file specified; find every file named Blueprints or Android.bp
  340. pathsToParse := []string{}
  341. for candidate := range mockFS {
  342. base := filepath.Base(candidate)
  343. if base == "Blueprints" || base == "Android.bp" {
  344. pathsToParse = append(pathsToParse, candidate)
  345. }
  346. }
  347. if len(pathsToParse) < 1 {
  348. panic(fmt.Sprintf("No Blueprint or Android.bp files found in mock filesystem: %v\n", mockFS))
  349. }
  350. mockFS[blueprint.MockModuleListFile] = []byte(strings.Join(pathsToParse, "\n"))
  351. c.fs = pathtools.MockFs(mockFS)
  352. c.mockBpList = blueprint.MockModuleListFile
  353. }
  354. func (c *config) fromEnv() error {
  355. switch c.Getenv("EXPERIMENTAL_JAVA_LANGUAGE_LEVEL_9") {
  356. case "", "true":
  357. // Do nothing
  358. default:
  359. return fmt.Errorf("The environment variable EXPERIMENTAL_JAVA_LANGUAGE_LEVEL_9 is no longer supported. Java language level 9 is now the global default.")
  360. }
  361. return nil
  362. }
  363. func (c *config) StopBefore() bootstrap.StopBefore {
  364. return c.stopBefore
  365. }
  366. func (c *config) SetStopBefore(stopBefore bootstrap.StopBefore) {
  367. c.stopBefore = stopBefore
  368. }
  369. var _ bootstrap.ConfigStopBefore = (*config)(nil)
  370. func (c *config) BlueprintToolLocation() string {
  371. return filepath.Join(c.buildDir, "host", c.PrebuiltOS(), "bin")
  372. }
  373. var _ bootstrap.ConfigBlueprintToolLocation = (*config)(nil)
  374. func (c *config) HostToolPath(ctx PathContext, tool string) Path {
  375. return PathForOutput(ctx, "host", c.PrebuiltOS(), "bin", tool)
  376. }
  377. func (c *config) HostJNIToolPath(ctx PathContext, path string) Path {
  378. ext := ".so"
  379. if runtime.GOOS == "darwin" {
  380. ext = ".dylib"
  381. }
  382. return PathForOutput(ctx, "host", c.PrebuiltOS(), "lib64", path+ext)
  383. }
  384. func (c *config) HostJavaToolPath(ctx PathContext, path string) Path {
  385. return PathForOutput(ctx, "host", c.PrebuiltOS(), "framework", path)
  386. }
  387. // HostSystemTool looks for non-hermetic tools from the system we're running on.
  388. // Generally shouldn't be used, but useful to find the XCode SDK, etc.
  389. func (c *config) HostSystemTool(name string) string {
  390. for _, dir := range filepath.SplitList(c.Getenv("PATH")) {
  391. path := filepath.Join(dir, name)
  392. if s, err := os.Stat(path); err != nil {
  393. continue
  394. } else if m := s.Mode(); !s.IsDir() && m&0111 != 0 {
  395. return path
  396. }
  397. }
  398. return name
  399. }
  400. // PrebuiltOS returns the name of the host OS used in prebuilts directories
  401. func (c *config) PrebuiltOS() string {
  402. switch runtime.GOOS {
  403. case "linux":
  404. return "linux-x86"
  405. case "darwin":
  406. return "darwin-x86"
  407. default:
  408. panic("Unknown GOOS")
  409. }
  410. }
  411. // GoRoot returns the path to the root directory of the Go toolchain.
  412. func (c *config) GoRoot() string {
  413. return fmt.Sprintf("%s/prebuilts/go/%s", c.srcDir, c.PrebuiltOS())
  414. }
  415. func (c *config) PrebuiltBuildTool(ctx PathContext, tool string) Path {
  416. return PathForSource(ctx, "prebuilts/build-tools", c.PrebuiltOS(), "bin", tool)
  417. }
  418. func (c *config) CpPreserveSymlinksFlags() string {
  419. switch runtime.GOOS {
  420. case "darwin":
  421. return "-R"
  422. case "linux":
  423. return "-d"
  424. default:
  425. return ""
  426. }
  427. }
  428. func (c *config) Getenv(key string) string {
  429. var val string
  430. var exists bool
  431. c.envLock.Lock()
  432. defer c.envLock.Unlock()
  433. if c.envDeps == nil {
  434. c.envDeps = make(map[string]string)
  435. }
  436. if val, exists = c.envDeps[key]; !exists {
  437. if c.envFrozen {
  438. panic("Cannot access new environment variables after envdeps are frozen")
  439. }
  440. val, _ = c.env[key]
  441. c.envDeps[key] = val
  442. }
  443. return val
  444. }
  445. func (c *config) GetenvWithDefault(key string, defaultValue string) string {
  446. ret := c.Getenv(key)
  447. if ret == "" {
  448. return defaultValue
  449. }
  450. return ret
  451. }
  452. func (c *config) IsEnvTrue(key string) bool {
  453. value := c.Getenv(key)
  454. return value == "1" || value == "y" || value == "yes" || value == "on" || value == "true"
  455. }
  456. func (c *config) IsEnvFalse(key string) bool {
  457. value := c.Getenv(key)
  458. return value == "0" || value == "n" || value == "no" || value == "off" || value == "false"
  459. }
  460. func (c *config) EnvDeps() map[string]string {
  461. c.envLock.Lock()
  462. defer c.envLock.Unlock()
  463. c.envFrozen = true
  464. return c.envDeps
  465. }
  466. func (c *config) EmbeddedInMake() bool {
  467. return c.inMake
  468. }
  469. func (c *config) BuildId() string {
  470. return String(c.productVariables.BuildId)
  471. }
  472. func (c *config) BuildNumberFile(ctx PathContext) Path {
  473. return PathForOutput(ctx, String(c.productVariables.BuildNumberFile))
  474. }
  475. // DeviceName returns the name of the current device target
  476. // TODO: take an AndroidModuleContext to select the device name for multi-device builds
  477. func (c *config) DeviceName() string {
  478. return *c.productVariables.DeviceName
  479. }
  480. func (c *config) DeviceResourceOverlays() []string {
  481. return c.productVariables.DeviceResourceOverlays
  482. }
  483. func (c *config) ProductResourceOverlays() []string {
  484. return c.productVariables.ProductResourceOverlays
  485. }
  486. func (c *config) PlatformVersionName() string {
  487. return String(c.productVariables.Platform_version_name)
  488. }
  489. func (c *config) PlatformSdkVersionInt() int {
  490. return *c.productVariables.Platform_sdk_version
  491. }
  492. func (c *config) PlatformSdkVersion() string {
  493. return strconv.Itoa(c.PlatformSdkVersionInt())
  494. }
  495. func (c *config) PlatformSdkCodename() string {
  496. return String(c.productVariables.Platform_sdk_codename)
  497. }
  498. func (c *config) PlatformSecurityPatch() string {
  499. return String(c.productVariables.Platform_security_patch)
  500. }
  501. func (c *config) PlatformPreviewSdkVersion() string {
  502. return String(c.productVariables.Platform_preview_sdk_version)
  503. }
  504. func (c *config) PlatformMinSupportedTargetSdkVersion() string {
  505. return String(c.productVariables.Platform_min_supported_target_sdk_version)
  506. }
  507. func (c *config) PlatformBaseOS() string {
  508. return String(c.productVariables.Platform_base_os)
  509. }
  510. func (c *config) MinSupportedSdkVersion() int {
  511. return 16
  512. }
  513. func (c *config) DefaultAppTargetSdkInt() int {
  514. if Bool(c.productVariables.Platform_sdk_final) {
  515. return c.PlatformSdkVersionInt()
  516. } else {
  517. return FutureApiLevel
  518. }
  519. }
  520. func (c *config) DefaultAppTargetSdk() string {
  521. if Bool(c.productVariables.Platform_sdk_final) {
  522. return c.PlatformSdkVersion()
  523. } else {
  524. return c.PlatformSdkCodename()
  525. }
  526. }
  527. func (c *config) AppsDefaultVersionName() string {
  528. return String(c.productVariables.AppsDefaultVersionName)
  529. }
  530. // Codenames that are active in the current lunch target.
  531. func (c *config) PlatformVersionActiveCodenames() []string {
  532. return c.productVariables.Platform_version_active_codenames
  533. }
  534. // Codenames that are available in the branch but not included in the current
  535. // lunch target.
  536. func (c *config) PlatformVersionFutureCodenames() []string {
  537. return c.productVariables.Platform_version_future_codenames
  538. }
  539. // All possible codenames in the current branch. NB: Not named AllCodenames
  540. // because "all" has historically meant "active" in make, and still does in
  541. // build.prop.
  542. func (c *config) PlatformVersionCombinedCodenames() []string {
  543. combined := []string{}
  544. combined = append(combined, c.PlatformVersionActiveCodenames()...)
  545. combined = append(combined, c.PlatformVersionFutureCodenames()...)
  546. return combined
  547. }
  548. func (c *config) ProductAAPTConfig() []string {
  549. return c.productVariables.AAPTConfig
  550. }
  551. func (c *config) ProductAAPTPreferredConfig() string {
  552. return String(c.productVariables.AAPTPreferredConfig)
  553. }
  554. func (c *config) ProductAAPTCharacteristics() string {
  555. return String(c.productVariables.AAPTCharacteristics)
  556. }
  557. func (c *config) ProductAAPTPrebuiltDPI() []string {
  558. return c.productVariables.AAPTPrebuiltDPI
  559. }
  560. func (c *config) DefaultAppCertificateDir(ctx PathContext) SourcePath {
  561. defaultCert := String(c.productVariables.DefaultAppCertificate)
  562. if defaultCert != "" {
  563. return PathForSource(ctx, filepath.Dir(defaultCert))
  564. } else {
  565. return PathForSource(ctx, "build/make/target/product/security")
  566. }
  567. }
  568. func (c *config) DefaultAppCertificate(ctx PathContext) (pem, key SourcePath) {
  569. defaultCert := String(c.productVariables.DefaultAppCertificate)
  570. if defaultCert != "" {
  571. return PathForSource(ctx, defaultCert+".x509.pem"), PathForSource(ctx, defaultCert+".pk8")
  572. } else {
  573. defaultDir := c.DefaultAppCertificateDir(ctx)
  574. return defaultDir.Join(ctx, "testkey.x509.pem"), defaultDir.Join(ctx, "testkey.pk8")
  575. }
  576. }
  577. func (c *config) ApexKeyDir(ctx ModuleContext) SourcePath {
  578. // TODO(b/121224311): define another variable such as TARGET_APEX_KEY_OVERRIDE
  579. defaultCert := String(c.productVariables.DefaultAppCertificate)
  580. if defaultCert == "" || filepath.Dir(defaultCert) == "build/make/target/product/security" {
  581. // When defaultCert is unset or is set to the testkeys path, use the APEX keys
  582. // that is under the module dir
  583. return pathForModuleSrc(ctx)
  584. } else {
  585. // If not, APEX keys are under the specified directory
  586. return PathForSource(ctx, filepath.Dir(defaultCert))
  587. }
  588. }
  589. func (c *config) AllowMissingDependencies() bool {
  590. return Bool(c.productVariables.Allow_missing_dependencies)
  591. }
  592. func (c *config) UnbundledBuild() bool {
  593. return Bool(c.productVariables.Unbundled_build)
  594. }
  595. func (c *config) UnbundledBuildUsePrebuiltSdks() bool {
  596. return Bool(c.productVariables.Unbundled_build) && !Bool(c.productVariables.Unbundled_build_sdks_from_source)
  597. }
  598. func (c *config) Fuchsia() bool {
  599. return Bool(c.productVariables.Fuchsia)
  600. }
  601. func (c *config) IsPdkBuild() bool {
  602. return Bool(c.productVariables.Pdk)
  603. }
  604. func (c *config) MinimizeJavaDebugInfo() bool {
  605. return Bool(c.productVariables.MinimizeJavaDebugInfo) && !Bool(c.productVariables.Eng)
  606. }
  607. func (c *config) Debuggable() bool {
  608. return Bool(c.productVariables.Debuggable)
  609. }
  610. func (c *config) Eng() bool {
  611. return Bool(c.productVariables.Eng)
  612. }
  613. func (c *config) DevicePrefer32BitApps() bool {
  614. return Bool(c.productVariables.DevicePrefer32BitApps)
  615. }
  616. func (c *config) DevicePrefer32BitExecutables() bool {
  617. return Bool(c.productVariables.DevicePrefer32BitExecutables)
  618. }
  619. func (c *config) DevicePrimaryArchType() ArchType {
  620. return c.Targets[Android][0].Arch.ArchType
  621. }
  622. func (c *config) SkipMegaDeviceInstall(path string) bool {
  623. return Bool(c.Mega_device) &&
  624. strings.HasPrefix(path, filepath.Join(c.buildDir, "target", "product"))
  625. }
  626. func (c *config) SanitizeHost() []string {
  627. return append([]string(nil), c.productVariables.SanitizeHost...)
  628. }
  629. func (c *config) SanitizeDevice() []string {
  630. return append([]string(nil), c.productVariables.SanitizeDevice...)
  631. }
  632. func (c *config) SanitizeDeviceDiag() []string {
  633. return append([]string(nil), c.productVariables.SanitizeDeviceDiag...)
  634. }
  635. func (c *config) SanitizeDeviceArch() []string {
  636. return append([]string(nil), c.productVariables.SanitizeDeviceArch...)
  637. }
  638. func (c *config) EnableCFI() bool {
  639. if c.productVariables.EnableCFI == nil {
  640. return true
  641. } else {
  642. return *c.productVariables.EnableCFI
  643. }
  644. }
  645. func (c *config) DisableScudo() bool {
  646. return Bool(c.productVariables.DisableScudo)
  647. }
  648. func (c *config) Android64() bool {
  649. for _, t := range c.Targets[Android] {
  650. if t.Arch.ArchType.Multilib == "lib64" {
  651. return true
  652. }
  653. }
  654. return false
  655. }
  656. func (c *config) UseGoma() bool {
  657. return Bool(c.productVariables.UseGoma)
  658. }
  659. func (c *config) UseRBE() bool {
  660. return Bool(c.productVariables.UseRBE)
  661. }
  662. func (c *config) UseRBEJAVAC() bool {
  663. return Bool(c.productVariables.UseRBEJAVAC)
  664. }
  665. func (c *config) UseRBER8() bool {
  666. return Bool(c.productVariables.UseRBER8)
  667. }
  668. func (c *config) UseRBED8() bool {
  669. return Bool(c.productVariables.UseRBED8)
  670. }
  671. func (c *config) UseRemoteBuild() bool {
  672. return c.UseGoma() || c.UseRBE()
  673. }
  674. func (c *config) RunErrorProne() bool {
  675. return c.IsEnvTrue("RUN_ERROR_PRONE")
  676. }
  677. func (c *config) XrefCorpusName() string {
  678. return c.Getenv("XREF_CORPUS")
  679. }
  680. // Returns Compilation Unit encoding to use. Can be 'json' (default), 'proto' or 'all'.
  681. func (c *config) XrefCuEncoding() string {
  682. if enc := c.Getenv("KYTHE_KZIP_ENCODING"); enc != "" {
  683. return enc
  684. }
  685. return "json"
  686. }
  687. func (c *config) EmitXrefRules() bool {
  688. return c.XrefCorpusName() != ""
  689. }
  690. func (c *config) ClangTidy() bool {
  691. return Bool(c.productVariables.ClangTidy)
  692. }
  693. func (c *config) TidyChecks() string {
  694. if c.productVariables.TidyChecks == nil {
  695. return ""
  696. }
  697. return *c.productVariables.TidyChecks
  698. }
  699. func (c *config) LibartImgHostBaseAddress() string {
  700. return "0x60000000"
  701. }
  702. func (c *config) LibartImgDeviceBaseAddress() string {
  703. return "0x70000000"
  704. }
  705. func (c *config) ArtUseReadBarrier() bool {
  706. return Bool(c.productVariables.ArtUseReadBarrier)
  707. }
  708. func (c *config) EnforceRROForModule(name string) bool {
  709. enforceList := c.productVariables.EnforceRROTargets
  710. if enforceList != nil {
  711. if InList("*", enforceList) {
  712. return true
  713. }
  714. return InList(name, enforceList)
  715. }
  716. return false
  717. }
  718. func (c *config) EnforceRROExcludedOverlay(path string) bool {
  719. excluded := c.productVariables.EnforceRROExcludedOverlays
  720. if excluded != nil {
  721. return HasAnyPrefix(path, excluded)
  722. }
  723. return false
  724. }
  725. func (c *config) ExportedNamespaces() []string {
  726. return append([]string(nil), c.productVariables.NamespacesToExport...)
  727. }
  728. func (c *config) HostStaticBinaries() bool {
  729. return Bool(c.productVariables.HostStaticBinaries)
  730. }
  731. func (c *config) UncompressPrivAppDex() bool {
  732. return Bool(c.productVariables.UncompressPrivAppDex)
  733. }
  734. func (c *config) ModulesLoadedByPrivilegedModules() []string {
  735. return c.productVariables.ModulesLoadedByPrivilegedModules
  736. }
  737. // Expected format for apexJarValue = <apex name>:<jar name>
  738. func SplitApexJarPair(apexJarValue string) (string, string) {
  739. var apexJarPair []string = strings.SplitN(apexJarValue, ":", 2)
  740. if apexJarPair == nil || len(apexJarPair) != 2 {
  741. panic(fmt.Errorf("malformed apexJarValue: %q, expected format: <apex>:<jar>",
  742. apexJarValue))
  743. }
  744. return apexJarPair[0], apexJarPair[1]
  745. }
  746. func (c *config) BootJars() []string {
  747. jars := c.productVariables.BootJars
  748. for _, p := range c.productVariables.UpdatableBootJars {
  749. _, jar := SplitApexJarPair(p)
  750. jars = append(jars, jar)
  751. }
  752. return jars
  753. }
  754. func (c *config) DexpreoptGlobalConfig(ctx PathContext) ([]byte, error) {
  755. if c.productVariables.DexpreoptGlobalConfig == nil {
  756. return nil, nil
  757. }
  758. path := absolutePath(*c.productVariables.DexpreoptGlobalConfig)
  759. ctx.AddNinjaFileDeps(path)
  760. return ioutil.ReadFile(path)
  761. }
  762. func (c *config) FrameworksBaseDirExists(ctx PathContext) bool {
  763. return ExistentPathForSource(ctx, "frameworks", "base").Valid()
  764. }
  765. func (c *config) VndkSnapshotBuildArtifacts() bool {
  766. return Bool(c.productVariables.VndkSnapshotBuildArtifacts)
  767. }
  768. func (c *config) HasMultilibConflict(arch ArchType) bool {
  769. return c.multilibConflicts[arch]
  770. }
  771. func (c *deviceConfig) Arches() []Arch {
  772. var arches []Arch
  773. for _, target := range c.config.Targets[Android] {
  774. arches = append(arches, target.Arch)
  775. }
  776. return arches
  777. }
  778. func (c *deviceConfig) BinderBitness() string {
  779. is32BitBinder := c.config.productVariables.Binder32bit
  780. if is32BitBinder != nil && *is32BitBinder {
  781. return "32"
  782. }
  783. return "64"
  784. }
  785. func (c *deviceConfig) VendorPath() string {
  786. if c.config.productVariables.VendorPath != nil {
  787. return *c.config.productVariables.VendorPath
  788. }
  789. return "vendor"
  790. }
  791. func (c *deviceConfig) VndkVersion() string {
  792. return String(c.config.productVariables.DeviceVndkVersion)
  793. }
  794. func (c *deviceConfig) PlatformVndkVersion() string {
  795. return String(c.config.productVariables.Platform_vndk_version)
  796. }
  797. func (c *deviceConfig) ProductVndkVersion() string {
  798. return String(c.config.productVariables.ProductVndkVersion)
  799. }
  800. func (c *deviceConfig) ExtraVndkVersions() []string {
  801. return c.config.productVariables.ExtraVndkVersions
  802. }
  803. func (c *deviceConfig) VndkUseCoreVariant() bool {
  804. return Bool(c.config.productVariables.VndkUseCoreVariant)
  805. }
  806. func (c *deviceConfig) SystemSdkVersions() []string {
  807. return c.config.productVariables.DeviceSystemSdkVersions
  808. }
  809. func (c *deviceConfig) PlatformSystemSdkVersions() []string {
  810. return c.config.productVariables.Platform_systemsdk_versions
  811. }
  812. func (c *deviceConfig) OdmPath() string {
  813. if c.config.productVariables.OdmPath != nil {
  814. return *c.config.productVariables.OdmPath
  815. }
  816. return "odm"
  817. }
  818. func (c *deviceConfig) ProductPath() string {
  819. if c.config.productVariables.ProductPath != nil {
  820. return *c.config.productVariables.ProductPath
  821. }
  822. return "product"
  823. }
  824. func (c *deviceConfig) SystemExtPath() string {
  825. if c.config.productVariables.SystemExtPath != nil {
  826. return *c.config.productVariables.SystemExtPath
  827. }
  828. return "system_ext"
  829. }
  830. func (c *deviceConfig) BtConfigIncludeDir() string {
  831. return String(c.config.productVariables.BtConfigIncludeDir)
  832. }
  833. func (c *deviceConfig) DeviceKernelHeaderDirs() []string {
  834. return c.config.productVariables.DeviceKernelHeaders
  835. }
  836. func (c *config) NativeLineCoverage() bool {
  837. return Bool(c.productVariables.NativeLineCoverage)
  838. }
  839. func (c *deviceConfig) NativeCoverageEnabled() bool {
  840. return Bool(c.config.productVariables.Native_coverage) || Bool(c.config.productVariables.NativeLineCoverage)
  841. }
  842. func (c *deviceConfig) ClangCoverageEnabled() bool {
  843. return Bool(c.config.productVariables.ClangCoverage)
  844. }
  845. func (c *deviceConfig) CoverageEnabledForPath(path string) bool {
  846. coverage := false
  847. if c.config.productVariables.CoveragePaths != nil {
  848. if InList("*", c.config.productVariables.CoveragePaths) || HasAnyPrefix(path, c.config.productVariables.CoveragePaths) {
  849. coverage = true
  850. }
  851. }
  852. if coverage && c.config.productVariables.CoverageExcludePaths != nil {
  853. if HasAnyPrefix(path, c.config.productVariables.CoverageExcludePaths) {
  854. coverage = false
  855. }
  856. }
  857. return coverage
  858. }
  859. func (c *deviceConfig) PgoAdditionalProfileDirs() []string {
  860. return c.config.productVariables.PgoAdditionalProfileDirs
  861. }
  862. func (c *deviceConfig) VendorSepolicyDirs() []string {
  863. return c.config.productVariables.BoardVendorSepolicyDirs
  864. }
  865. func (c *deviceConfig) OdmSepolicyDirs() []string {
  866. return c.config.productVariables.BoardOdmSepolicyDirs
  867. }
  868. func (c *deviceConfig) PlatPublicSepolicyDirs() []string {
  869. return c.config.productVariables.BoardPlatPublicSepolicyDirs
  870. }
  871. func (c *deviceConfig) PlatPrivateSepolicyDirs() []string {
  872. return c.config.productVariables.BoardPlatPrivateSepolicyDirs
  873. }
  874. func (c *deviceConfig) SepolicyM4Defs() []string {
  875. return c.config.productVariables.BoardSepolicyM4Defs
  876. }
  877. func (c *deviceConfig) OverrideManifestPackageNameFor(name string) (manifestName string, overridden bool) {
  878. return findOverrideValue(c.config.productVariables.ManifestPackageNameOverrides, name,
  879. "invalid override rule %q in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES should be <module_name>:<manifest_name>")
  880. }
  881. func (c *deviceConfig) OverrideCertificateFor(name string) (certificatePath string, overridden bool) {
  882. return findOverrideValue(c.config.productVariables.CertificateOverrides, name,
  883. "invalid override rule %q in PRODUCT_CERTIFICATE_OVERRIDES should be <module_name>:<certificate_module_name>")
  884. }
  885. func (c *deviceConfig) OverridePackageNameFor(name string) string {
  886. newName, overridden := findOverrideValue(
  887. c.config.productVariables.PackageNameOverrides,
  888. name,
  889. "invalid override rule %q in PRODUCT_PACKAGE_NAME_OVERRIDES should be <module_name>:<package_name>")
  890. if overridden {
  891. return newName
  892. }
  893. return name
  894. }
  895. func findOverrideValue(overrides []string, name string, errorMsg string) (newValue string, overridden bool) {
  896. if overrides == nil || len(overrides) == 0 {
  897. return "", false
  898. }
  899. for _, o := range overrides {
  900. split := strings.Split(o, ":")
  901. if len(split) != 2 {
  902. // This shouldn't happen as this is first checked in make, but just in case.
  903. panic(fmt.Errorf(errorMsg, o))
  904. }
  905. if matchPattern(split[0], name) {
  906. return substPattern(split[0], split[1], name), true
  907. }
  908. }
  909. return "", false
  910. }
  911. func (c *config) IntegerOverflowDisabledForPath(path string) bool {
  912. if c.productVariables.IntegerOverflowExcludePaths == nil {
  913. return false
  914. }
  915. return HasAnyPrefix(path, c.productVariables.IntegerOverflowExcludePaths)
  916. }
  917. func (c *config) CFIDisabledForPath(path string) bool {
  918. if c.productVariables.CFIExcludePaths == nil {
  919. return false
  920. }
  921. return HasAnyPrefix(path, c.productVariables.CFIExcludePaths)
  922. }
  923. func (c *config) CFIEnabledForPath(path string) bool {
  924. if c.productVariables.CFIIncludePaths == nil {
  925. return false
  926. }
  927. return HasAnyPrefix(path, c.productVariables.CFIIncludePaths)
  928. }
  929. func (c *config) VendorConfig(name string) VendorConfig {
  930. return soongconfig.Config(c.productVariables.VendorVars[name])
  931. }
  932. func (c *config) NdkAbis() bool {
  933. return Bool(c.productVariables.Ndk_abis)
  934. }
  935. func (c *config) AmlAbis() bool {
  936. return Bool(c.productVariables.Aml_abis)
  937. }
  938. func (c *config) ExcludeDraftNdkApis() bool {
  939. return Bool(c.productVariables.Exclude_draft_ndk_apis)
  940. }
  941. func (c *config) FlattenApex() bool {
  942. return Bool(c.productVariables.Flatten_apex)
  943. }
  944. func (c *config) EnforceSystemCertificate() bool {
  945. return Bool(c.productVariables.EnforceSystemCertificate)
  946. }
  947. func (c *config) EnforceSystemCertificateWhitelist() []string {
  948. return c.productVariables.EnforceSystemCertificateWhitelist
  949. }
  950. func (c *config) EnforceProductPartitionInterface() bool {
  951. return Bool(c.productVariables.EnforceProductPartitionInterface)
  952. }
  953. func (c *config) InstallExtraFlattenedApexes() bool {
  954. return Bool(c.productVariables.InstallExtraFlattenedApexes)
  955. }
  956. func (c *config) ProductHiddenAPIStubs() []string {
  957. return c.productVariables.ProductHiddenAPIStubs
  958. }
  959. func (c *config) ProductHiddenAPIStubsSystem() []string {
  960. return c.productVariables.ProductHiddenAPIStubsSystem
  961. }
  962. func (c *config) ProductHiddenAPIStubsTest() []string {
  963. return c.productVariables.ProductHiddenAPIStubsTest
  964. }
  965. func (c *deviceConfig) TargetFSConfigGen() []string {
  966. return c.config.productVariables.TargetFSConfigGen
  967. }
  968. func (c *config) ProductPublicSepolicyDirs() []string {
  969. return c.productVariables.ProductPublicSepolicyDirs
  970. }
  971. func (c *config) ProductPrivateSepolicyDirs() []string {
  972. return c.productVariables.ProductPrivateSepolicyDirs
  973. }
  974. func (c *config) ProductCompatibleProperty() bool {
  975. return Bool(c.productVariables.ProductCompatibleProperty)
  976. }
  977. func (c *config) MissingUsesLibraries() []string {
  978. return c.productVariables.MissingUsesLibraries
  979. }
  980. func (c *deviceConfig) BoardVndkRuntimeDisable() bool {
  981. return Bool(c.config.productVariables.BoardVndkRuntimeDisable)
  982. }
  983. func (c *deviceConfig) DeviceArch() string {
  984. return String(c.config.productVariables.DeviceArch)
  985. }
  986. func (c *deviceConfig) DeviceArchVariant() string {
  987. return String(c.config.productVariables.DeviceArchVariant)
  988. }
  989. func (c *deviceConfig) DeviceSecondaryArch() string {
  990. return String(c.config.productVariables.DeviceSecondaryArch)
  991. }
  992. func (c *deviceConfig) DeviceSecondaryArchVariant() string {
  993. return String(c.config.productVariables.DeviceSecondaryArchVariant)
  994. }
  995. func (c *deviceConfig) BoardUsesRecoveryAsBoot() bool {
  996. return Bool(c.config.productVariables.BoardUsesRecoveryAsBoot)
  997. }