config.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. // Copyright 2017 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 build
  15. import (
  16. "os"
  17. "path/filepath"
  18. "runtime"
  19. "strconv"
  20. "strings"
  21. "time"
  22. "android/soong/shared"
  23. )
  24. type Config struct{ *configImpl }
  25. type configImpl struct {
  26. // From the environment
  27. arguments []string
  28. goma bool
  29. environ *Environment
  30. distDir string
  31. buildDateTime string
  32. // From the arguments
  33. parallel int
  34. keepGoing int
  35. verbose bool
  36. checkbuild bool
  37. dist bool
  38. skipMake bool
  39. // From the product config
  40. katiArgs []string
  41. ninjaArgs []string
  42. katiSuffix string
  43. targetDevice string
  44. targetDeviceDir string
  45. // Autodetected
  46. totalRAM uint64
  47. pdkBuild bool
  48. brokenDupRules bool
  49. brokenUsesNetwork bool
  50. brokenNinjaEnvVars []string
  51. pathReplaced bool
  52. }
  53. const srcDirFileCheck = "build/soong/root.bp"
  54. var buildFiles = []string{"Android.mk", "Android.bp"}
  55. type BuildAction uint
  56. const (
  57. // Builds all of the modules and their dependencies of a specified directory, relative to the root
  58. // directory of the source tree.
  59. BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
  60. // Builds all of the modules and their dependencies of a list of specified directories. All specified
  61. // directories are relative to the root directory of the source tree.
  62. BUILD_MODULES_IN_DIRECTORIES
  63. // Build a list of specified modules. If none was specified, simply build the whole source tree.
  64. BUILD_MODULES
  65. )
  66. // checkTopDir validates that the current directory is at the root directory of the source tree.
  67. func checkTopDir(ctx Context) {
  68. if _, err := os.Stat(srcDirFileCheck); err != nil {
  69. if os.IsNotExist(err) {
  70. ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
  71. }
  72. ctx.Fatalln("Error verifying tree state:", err)
  73. }
  74. }
  75. func NewConfig(ctx Context, args ...string) Config {
  76. ret := &configImpl{
  77. environ: OsEnvironment(),
  78. }
  79. // Sane default matching ninja
  80. ret.parallel = runtime.NumCPU() + 2
  81. ret.keepGoing = 1
  82. ret.totalRAM = detectTotalRAM(ctx)
  83. ret.parseArgs(ctx, args)
  84. // Make sure OUT_DIR is set appropriately
  85. if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
  86. ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
  87. } else {
  88. outDir := "out"
  89. if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
  90. if wd, err := os.Getwd(); err != nil {
  91. ctx.Fatalln("Failed to get working directory:", err)
  92. } else {
  93. outDir = filepath.Join(baseDir, filepath.Base(wd))
  94. }
  95. }
  96. ret.environ.Set("OUT_DIR", outDir)
  97. }
  98. if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
  99. ret.distDir = filepath.Clean(distDir)
  100. } else {
  101. ret.distDir = filepath.Join(ret.OutDir(), "dist")
  102. }
  103. ret.environ.Unset(
  104. // We're already using it
  105. "USE_SOONG_UI",
  106. // We should never use GOROOT/GOPATH from the shell environment
  107. "GOROOT",
  108. "GOPATH",
  109. // These should only come from Soong, not the environment.
  110. "CLANG",
  111. "CLANG_CXX",
  112. "CCC_CC",
  113. "CCC_CXX",
  114. // Used by the goma compiler wrapper, but should only be set by
  115. // gomacc
  116. "GOMACC_PATH",
  117. // We handle this above
  118. "OUT_DIR_COMMON_BASE",
  119. // This is handled above too, and set for individual commands later
  120. "DIST_DIR",
  121. // Variables that have caused problems in the past
  122. "BASH_ENV",
  123. "CDPATH",
  124. "DISPLAY",
  125. "GREP_OPTIONS",
  126. "NDK_ROOT",
  127. "POSIXLY_CORRECT",
  128. // Drop make flags
  129. "MAKEFLAGS",
  130. "MAKELEVEL",
  131. "MFLAGS",
  132. // Set in envsetup.sh, reset in makefiles
  133. "ANDROID_JAVA_TOOLCHAIN",
  134. // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
  135. "ANDROID_BUILD_TOP",
  136. "ANDROID_HOST_OUT",
  137. "ANDROID_PRODUCT_OUT",
  138. "ANDROID_HOST_OUT_TESTCASES",
  139. "ANDROID_TARGET_OUT_TESTCASES",
  140. "ANDROID_TOOLCHAIN",
  141. "ANDROID_TOOLCHAIN_2ND_ARCH",
  142. "ANDROID_DEV_SCRIPTS",
  143. "ANDROID_EMULATOR_PREBUILTS",
  144. "ANDROID_PRE_BUILD_PATHS",
  145. // Only set in multiproduct_kati after config generation
  146. "EMPTY_NINJA_FILE",
  147. )
  148. // Tell python not to spam the source tree with .pyc files.
  149. ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
  150. ret.environ.Set("TMPDIR", absPath(ctx, ret.TempDir()))
  151. // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
  152. symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
  153. "llvm-binutils-stable/llvm-symbolizer")
  154. ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath))
  155. // Precondition: the current directory is the top of the source tree
  156. checkTopDir(ctx)
  157. if srcDir := absPath(ctx, "."); strings.ContainsRune(srcDir, ' ') {
  158. ctx.Println("You are building in a directory whose absolute path contains a space character:")
  159. ctx.Println()
  160. ctx.Printf("%q\n", srcDir)
  161. ctx.Println()
  162. ctx.Fatalln("Directory names containing spaces are not supported")
  163. }
  164. if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
  165. ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
  166. ctx.Println()
  167. ctx.Printf("%q\n", outDir)
  168. ctx.Println()
  169. ctx.Fatalln("Directory names containing spaces are not supported")
  170. }
  171. if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
  172. ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
  173. ctx.Println()
  174. ctx.Printf("%q\n", distDir)
  175. ctx.Println()
  176. ctx.Fatalln("Directory names containing spaces are not supported")
  177. }
  178. // Configure Java-related variables, including adding it to $PATH
  179. java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
  180. java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
  181. java11Home := filepath.Join("prebuilts/jdk/jdk11", ret.HostPrebuiltTag())
  182. javaHome := func() string {
  183. if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
  184. return override
  185. }
  186. if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
  187. ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN is no longer supported. An OpenJDK 11 toolchain is now the global default.")
  188. }
  189. return java11Home
  190. }()
  191. absJavaHome := absPath(ctx, javaHome)
  192. ret.configureLocale(ctx)
  193. newPath := []string{filepath.Join(absJavaHome, "bin")}
  194. if path, ok := ret.environ.Get("PATH"); ok && path != "" {
  195. newPath = append(newPath, path)
  196. }
  197. ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
  198. ret.environ.Set("JAVA_HOME", absJavaHome)
  199. ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
  200. ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
  201. ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
  202. ret.environ.Set("ANDROID_JAVA11_HOME", java11Home)
  203. ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
  204. outDir := ret.OutDir()
  205. buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
  206. if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
  207. ret.buildDateTime = buildDateTime
  208. } else {
  209. ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
  210. }
  211. if ctx.Metrics != nil {
  212. ctx.Metrics.SetBuildDateTime(ret.buildDateTime)
  213. }
  214. ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
  215. return Config{ret}
  216. }
  217. // NewBuildActionConfig returns a build configuration based on the build action. The arguments are
  218. // processed based on the build action and extracts any arguments that belongs to the build action.
  219. func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
  220. return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
  221. }
  222. // getConfigArgs processes the command arguments based on the build action and creates a set of new
  223. // arguments to be accepted by Config.
  224. func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
  225. // The next block of code verifies that the current directory is the root directory of the source
  226. // tree. It then finds the relative path of dir based on the root directory of the source tree
  227. // and verify that dir is inside of the source tree.
  228. checkTopDir(ctx)
  229. topDir, err := os.Getwd()
  230. if err != nil {
  231. ctx.Fatalf("Error retrieving top directory: %v", err)
  232. }
  233. dir, err = filepath.EvalSymlinks(dir)
  234. if err != nil {
  235. ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
  236. }
  237. dir, err = filepath.Abs(dir)
  238. if err != nil {
  239. ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
  240. }
  241. relDir, err := filepath.Rel(topDir, dir)
  242. if err != nil {
  243. ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
  244. }
  245. // If there are ".." in the path, it's not in the source tree.
  246. if strings.Contains(relDir, "..") {
  247. ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
  248. }
  249. configArgs := args[:]
  250. // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
  251. // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
  252. targetNamePrefix := "MODULES-IN-"
  253. if inList("GET-INSTALL-PATH", configArgs) {
  254. targetNamePrefix = "GET-INSTALL-PATH-IN-"
  255. configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
  256. }
  257. var targets []string
  258. switch action {
  259. case BUILD_MODULES:
  260. // No additional processing is required when building a list of specific modules or all modules.
  261. case BUILD_MODULES_IN_A_DIRECTORY:
  262. // If dir is the root source tree, all the modules are built of the source tree are built so
  263. // no need to find the build file.
  264. if topDir == dir {
  265. break
  266. }
  267. buildFile := findBuildFile(ctx, relDir)
  268. if buildFile == "" {
  269. ctx.Fatalf("Build file not found for %s directory", relDir)
  270. }
  271. targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
  272. case BUILD_MODULES_IN_DIRECTORIES:
  273. newConfigArgs, dirs := splitArgs(configArgs)
  274. configArgs = newConfigArgs
  275. targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
  276. }
  277. // Tidy only override all other specified targets.
  278. tidyOnly := os.Getenv("WITH_TIDY_ONLY")
  279. if tidyOnly == "true" || tidyOnly == "1" {
  280. configArgs = append(configArgs, "tidy_only")
  281. } else {
  282. configArgs = append(configArgs, targets...)
  283. }
  284. return configArgs
  285. }
  286. // convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
  287. func convertToTarget(dir string, targetNamePrefix string) string {
  288. return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
  289. }
  290. // hasBuildFile returns true if dir contains an Android build file.
  291. func hasBuildFile(ctx Context, dir string) bool {
  292. for _, buildFile := range buildFiles {
  293. _, err := os.Stat(filepath.Join(dir, buildFile))
  294. if err == nil {
  295. return true
  296. }
  297. if !os.IsNotExist(err) {
  298. ctx.Fatalf("Error retrieving the build file stats: %v", err)
  299. }
  300. }
  301. return false
  302. }
  303. // findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
  304. // in the current and any sub directory of dir. If a build file is not found, traverse the path
  305. // up by one directory and repeat again until either a build file is found or reached to the root
  306. // source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
  307. // string is returned.
  308. func findBuildFile(ctx Context, dir string) string {
  309. // If the string is empty or ".", assume it is top directory of the source tree.
  310. if dir == "" || dir == "." {
  311. return ""
  312. }
  313. found := false
  314. for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
  315. err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
  316. if err != nil {
  317. return err
  318. }
  319. if found {
  320. return filepath.SkipDir
  321. }
  322. if info.IsDir() {
  323. return nil
  324. }
  325. for _, buildFile := range buildFiles {
  326. if info.Name() == buildFile {
  327. found = true
  328. return filepath.SkipDir
  329. }
  330. }
  331. return nil
  332. })
  333. if err != nil {
  334. ctx.Fatalf("Error finding Android build file: %v", err)
  335. }
  336. if found {
  337. return filepath.Join(buildDir, "Android.mk")
  338. }
  339. }
  340. return ""
  341. }
  342. // splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
  343. func splitArgs(args []string) (newArgs []string, dirs []string) {
  344. specialArgs := map[string]bool{
  345. "showcommands": true,
  346. "snod": true,
  347. "dist": true,
  348. "checkbuild": true,
  349. }
  350. newArgs = []string{}
  351. dirs = []string{}
  352. for _, arg := range args {
  353. // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
  354. if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
  355. newArgs = append(newArgs, arg)
  356. continue
  357. }
  358. if _, ok := specialArgs[arg]; ok {
  359. newArgs = append(newArgs, arg)
  360. continue
  361. }
  362. dirs = append(dirs, arg)
  363. }
  364. return newArgs, dirs
  365. }
  366. // getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
  367. // directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
  368. // source root tree where the build action command was invoked. Each directory is validated if the
  369. // build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
  370. func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
  371. for _, dir := range dirs {
  372. // The directory may have specified specific modules to build. ":" is the separator to separate
  373. // the directory and the list of modules.
  374. s := strings.Split(dir, ":")
  375. l := len(s)
  376. if l > 2 { // more than one ":" was specified.
  377. ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
  378. }
  379. dir = filepath.Join(relDir, s[0])
  380. if _, err := os.Stat(dir); err != nil {
  381. ctx.Fatalf("couldn't find directory %s", dir)
  382. }
  383. // Verify that if there are any targets specified after ":". Each target is separated by ",".
  384. var newTargets []string
  385. if l == 2 && s[1] != "" {
  386. newTargets = strings.Split(s[1], ",")
  387. if inList("", newTargets) {
  388. ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
  389. }
  390. }
  391. // If there are specified targets to build in dir, an android build file must exist for the one
  392. // shot build. For the non-targets case, find the appropriate build file and build all the
  393. // modules in dir (or the closest one in the dir path).
  394. if len(newTargets) > 0 {
  395. if !hasBuildFile(ctx, dir) {
  396. ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
  397. }
  398. } else {
  399. buildFile := findBuildFile(ctx, dir)
  400. if buildFile == "" {
  401. ctx.Fatalf("Build file not found for %s directory", dir)
  402. }
  403. newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
  404. }
  405. targets = append(targets, newTargets...)
  406. }
  407. return targets
  408. }
  409. func (c *configImpl) parseArgs(ctx Context, args []string) {
  410. for i := 0; i < len(args); i++ {
  411. arg := strings.TrimSpace(args[i])
  412. if arg == "--make-mode" {
  413. } else if arg == "showcommands" {
  414. c.verbose = true
  415. } else if arg == "--skip-make" {
  416. c.skipMake = true
  417. } else if len(arg) > 0 && arg[0] == '-' {
  418. parseArgNum := func(def int) int {
  419. if len(arg) > 2 {
  420. p, err := strconv.ParseUint(arg[2:], 10, 31)
  421. if err != nil {
  422. ctx.Fatalf("Failed to parse %q: %v", arg, err)
  423. }
  424. return int(p)
  425. } else if i+1 < len(args) {
  426. p, err := strconv.ParseUint(args[i+1], 10, 31)
  427. if err == nil {
  428. i++
  429. return int(p)
  430. }
  431. }
  432. return def
  433. }
  434. if len(arg) > 1 && arg[1] == 'j' {
  435. c.parallel = parseArgNum(c.parallel)
  436. } else if len(arg) > 1 && arg[1] == 'k' {
  437. c.keepGoing = parseArgNum(0)
  438. } else {
  439. ctx.Fatalln("Unknown option:", arg)
  440. }
  441. } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
  442. c.environ.Set(k, v)
  443. } else if arg == "dist" {
  444. c.dist = true
  445. } else {
  446. if arg == "checkbuild" {
  447. c.checkbuild = true
  448. }
  449. c.arguments = append(c.arguments, arg)
  450. }
  451. }
  452. }
  453. func (c *configImpl) configureLocale(ctx Context) {
  454. cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
  455. output, err := cmd.Output()
  456. var locales []string
  457. if err == nil {
  458. locales = strings.Split(string(output), "\n")
  459. } else {
  460. // If we're unable to list the locales, let's assume en_US.UTF-8
  461. locales = []string{"en_US.UTF-8"}
  462. ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
  463. }
  464. // gettext uses LANGUAGE, which is passed directly through
  465. // For LANG and LC_*, only preserve the evaluated version of
  466. // LC_MESSAGES
  467. user_lang := ""
  468. if lc_all, ok := c.environ.Get("LC_ALL"); ok {
  469. user_lang = lc_all
  470. } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
  471. user_lang = lc_messages
  472. } else if lang, ok := c.environ.Get("LANG"); ok {
  473. user_lang = lang
  474. }
  475. c.environ.UnsetWithPrefix("LC_")
  476. if user_lang != "" {
  477. c.environ.Set("LC_MESSAGES", user_lang)
  478. }
  479. // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
  480. // for others)
  481. if inList("C.UTF-8", locales) {
  482. c.environ.Set("LANG", "C.UTF-8")
  483. } else if inList("C.utf8", locales) {
  484. // These normalize to the same thing
  485. c.environ.Set("LANG", "C.UTF-8")
  486. } else if inList("en_US.UTF-8", locales) {
  487. c.environ.Set("LANG", "en_US.UTF-8")
  488. } else if inList("en_US.utf8", locales) {
  489. // These normalize to the same thing
  490. c.environ.Set("LANG", "en_US.UTF-8")
  491. } else {
  492. ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
  493. }
  494. }
  495. // Lunch configures the environment for a specific product similarly to the
  496. // `lunch` bash function.
  497. func (c *configImpl) Lunch(ctx Context, product, variant string) {
  498. if variant != "eng" && variant != "userdebug" && variant != "user" {
  499. ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
  500. }
  501. c.environ.Set("TARGET_PRODUCT", product)
  502. c.environ.Set("TARGET_BUILD_VARIANT", variant)
  503. c.environ.Set("TARGET_BUILD_TYPE", "release")
  504. c.environ.Unset("TARGET_BUILD_APPS")
  505. }
  506. // Tapas configures the environment to build one or more unbundled apps,
  507. // similarly to the `tapas` bash function.
  508. func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
  509. if len(apps) == 0 {
  510. apps = []string{"all"}
  511. }
  512. if variant == "" {
  513. variant = "eng"
  514. }
  515. if variant != "eng" && variant != "userdebug" && variant != "user" {
  516. ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
  517. }
  518. var product string
  519. switch arch {
  520. case "arm", "":
  521. product = "aosp_arm"
  522. case "arm64":
  523. product = "aosm_arm64"
  524. case "x86":
  525. product = "aosp_x86"
  526. case "x86_64":
  527. product = "aosp_x86_64"
  528. default:
  529. ctx.Fatalf("Invalid architecture: %q", arch)
  530. }
  531. c.environ.Set("TARGET_PRODUCT", product)
  532. c.environ.Set("TARGET_BUILD_VARIANT", variant)
  533. c.environ.Set("TARGET_BUILD_TYPE", "release")
  534. c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
  535. }
  536. func (c *configImpl) Environment() *Environment {
  537. return c.environ
  538. }
  539. func (c *configImpl) Arguments() []string {
  540. return c.arguments
  541. }
  542. func (c *configImpl) OutDir() string {
  543. if outDir, ok := c.environ.Get("OUT_DIR"); ok {
  544. return outDir
  545. }
  546. return "out"
  547. }
  548. func (c *configImpl) DistDir() string {
  549. return c.distDir
  550. }
  551. func (c *configImpl) NinjaArgs() []string {
  552. if c.skipMake {
  553. return c.arguments
  554. }
  555. return c.ninjaArgs
  556. }
  557. func (c *configImpl) SoongOutDir() string {
  558. return filepath.Join(c.OutDir(), "soong")
  559. }
  560. func (c *configImpl) TempDir() string {
  561. return shared.TempDirForOutDir(c.SoongOutDir())
  562. }
  563. func (c *configImpl) FileListDir() string {
  564. return filepath.Join(c.OutDir(), ".module_paths")
  565. }
  566. func (c *configImpl) KatiSuffix() string {
  567. if c.katiSuffix != "" {
  568. return c.katiSuffix
  569. }
  570. panic("SetKatiSuffix has not been called")
  571. }
  572. // Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
  573. // user is interested in additional checks at the expense of build time.
  574. func (c *configImpl) Checkbuild() bool {
  575. return c.checkbuild
  576. }
  577. func (c *configImpl) Dist() bool {
  578. return c.dist
  579. }
  580. func (c *configImpl) IsVerbose() bool {
  581. return c.verbose
  582. }
  583. func (c *configImpl) SkipMake() bool {
  584. return c.skipMake
  585. }
  586. func (c *configImpl) TargetProduct() string {
  587. if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
  588. return v
  589. }
  590. panic("TARGET_PRODUCT is not defined")
  591. }
  592. func (c *configImpl) TargetDevice() string {
  593. return c.targetDevice
  594. }
  595. func (c *configImpl) SetTargetDevice(device string) {
  596. c.targetDevice = device
  597. }
  598. func (c *configImpl) TargetBuildVariant() string {
  599. if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
  600. return v
  601. }
  602. panic("TARGET_BUILD_VARIANT is not defined")
  603. }
  604. func (c *configImpl) KatiArgs() []string {
  605. return c.katiArgs
  606. }
  607. func (c *configImpl) Parallel() int {
  608. return c.parallel
  609. }
  610. func (c *configImpl) HighmemParallel() int {
  611. if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
  612. return i
  613. }
  614. const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
  615. parallel := c.Parallel()
  616. if c.UseRemoteBuild() {
  617. // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
  618. // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
  619. // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
  620. // Return 1/16th of the size of the local pool, rounding up.
  621. return (parallel + 15) / 16
  622. } else if c.totalRAM == 0 {
  623. // Couldn't detect the total RAM, don't restrict highmem processes.
  624. return parallel
  625. } else if c.totalRAM <= 32*1024*1024*1024 {
  626. // Less than 32GB of ram, restrict to 2 highmem processes
  627. return 2
  628. } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
  629. // If less than 8GB total RAM per process, reduce the number of highmem processes
  630. return p
  631. }
  632. // No restriction on highmem processes
  633. return parallel
  634. }
  635. func (c *configImpl) TotalRAM() uint64 {
  636. return c.totalRAM
  637. }
  638. func (c *configImpl) UseGoma() bool {
  639. if v, ok := c.environ.Get("USE_GOMA"); ok {
  640. v = strings.TrimSpace(v)
  641. if v != "" && v != "false" {
  642. return true
  643. }
  644. }
  645. return false
  646. }
  647. func (c *configImpl) StartGoma() bool {
  648. if !c.UseGoma() {
  649. return false
  650. }
  651. if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
  652. v = strings.TrimSpace(v)
  653. if v != "" && v != "false" {
  654. return false
  655. }
  656. }
  657. return true
  658. }
  659. func (c *configImpl) UseRBE() bool {
  660. if v, ok := c.environ.Get("USE_RBE"); ok {
  661. v = strings.TrimSpace(v)
  662. if v != "" && v != "false" {
  663. return true
  664. }
  665. }
  666. return false
  667. }
  668. func (c *configImpl) UseRBEJAVAC() bool {
  669. if !c.UseRBE() {
  670. return false
  671. }
  672. if v, ok := c.environ.Get("RBE_JAVAC"); ok {
  673. v = strings.TrimSpace(v)
  674. if v != "" && v != "false" {
  675. return true
  676. }
  677. }
  678. return false
  679. }
  680. func (c *configImpl) UseRBER8() bool {
  681. if !c.UseRBE() {
  682. return false
  683. }
  684. if v, ok := c.environ.Get("RBE_R8"); ok {
  685. v = strings.TrimSpace(v)
  686. if v != "" && v != "false" {
  687. return true
  688. }
  689. }
  690. return false
  691. }
  692. func (c *configImpl) UseRBED8() bool {
  693. if !c.UseRBE() {
  694. return false
  695. }
  696. if v, ok := c.environ.Get("RBE_D8"); ok {
  697. v = strings.TrimSpace(v)
  698. if v != "" && v != "false" {
  699. return true
  700. }
  701. }
  702. return false
  703. }
  704. func (c *configImpl) StartRBE() bool {
  705. if !c.UseRBE() {
  706. return false
  707. }
  708. if v, ok := c.environ.Get("NOSTART_RBE"); ok {
  709. v = strings.TrimSpace(v)
  710. if v != "" && v != "false" {
  711. return false
  712. }
  713. }
  714. return true
  715. }
  716. func (c *configImpl) UseRemoteBuild() bool {
  717. return c.UseGoma() || c.UseRBE()
  718. }
  719. // RemoteParallel controls how many remote jobs (i.e., commands which contain
  720. // gomacc) are run in parallel. Note the parallelism of all other jobs is
  721. // still limited by Parallel()
  722. func (c *configImpl) RemoteParallel() int {
  723. if !c.UseRemoteBuild() {
  724. return 0
  725. }
  726. if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
  727. return i
  728. }
  729. return 500
  730. }
  731. func (c *configImpl) SetKatiArgs(args []string) {
  732. c.katiArgs = args
  733. }
  734. func (c *configImpl) SetNinjaArgs(args []string) {
  735. c.ninjaArgs = args
  736. }
  737. func (c *configImpl) SetKatiSuffix(suffix string) {
  738. c.katiSuffix = suffix
  739. }
  740. func (c *configImpl) LastKatiSuffixFile() string {
  741. return filepath.Join(c.OutDir(), "last_kati_suffix")
  742. }
  743. func (c *configImpl) HasKatiSuffix() bool {
  744. return c.katiSuffix != ""
  745. }
  746. func (c *configImpl) KatiEnvFile() string {
  747. return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
  748. }
  749. func (c *configImpl) KatiBuildNinjaFile() string {
  750. return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
  751. }
  752. func (c *configImpl) KatiPackageNinjaFile() string {
  753. return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
  754. }
  755. func (c *configImpl) SoongNinjaFile() string {
  756. return filepath.Join(c.SoongOutDir(), "build.ninja")
  757. }
  758. func (c *configImpl) CombinedNinjaFile() string {
  759. if c.katiSuffix == "" {
  760. return filepath.Join(c.OutDir(), "combined.ninja")
  761. }
  762. return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
  763. }
  764. func (c *configImpl) SoongAndroidMk() string {
  765. return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
  766. }
  767. func (c *configImpl) SoongMakeVarsMk() string {
  768. return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
  769. }
  770. func (c *configImpl) ProductOut() string {
  771. return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
  772. }
  773. func (c *configImpl) DevicePreviousProductConfig() string {
  774. return filepath.Join(c.ProductOut(), "previous_build_config.mk")
  775. }
  776. func (c *configImpl) KatiPackageMkDir() string {
  777. return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
  778. }
  779. func (c *configImpl) hostOutRoot() string {
  780. return filepath.Join(c.OutDir(), "host")
  781. }
  782. func (c *configImpl) HostOut() string {
  783. return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
  784. }
  785. // This probably needs to be multi-valued, so not exporting it for now
  786. func (c *configImpl) hostCrossOut() string {
  787. if runtime.GOOS == "linux" {
  788. return filepath.Join(c.hostOutRoot(), "windows-x86")
  789. } else {
  790. return ""
  791. }
  792. }
  793. func (c *configImpl) HostPrebuiltTag() string {
  794. if runtime.GOOS == "linux" {
  795. return "linux-x86"
  796. } else if runtime.GOOS == "darwin" {
  797. return "darwin-x86"
  798. } else {
  799. panic("Unsupported OS")
  800. }
  801. }
  802. func (c *configImpl) PrebuiltBuildTool(name string) string {
  803. if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
  804. if sanitize := strings.Fields(v); inList("address", sanitize) {
  805. asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
  806. if _, err := os.Stat(asan); err == nil {
  807. return asan
  808. }
  809. }
  810. }
  811. return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
  812. }
  813. func (c *configImpl) SetBuildBrokenDupRules(val bool) {
  814. c.brokenDupRules = val
  815. }
  816. func (c *configImpl) BuildBrokenDupRules() bool {
  817. return c.brokenDupRules
  818. }
  819. func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
  820. c.brokenUsesNetwork = val
  821. }
  822. func (c *configImpl) BuildBrokenUsesNetwork() bool {
  823. return c.brokenUsesNetwork
  824. }
  825. func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
  826. c.brokenNinjaEnvVars = val
  827. }
  828. func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
  829. return c.brokenNinjaEnvVars
  830. }
  831. func (c *configImpl) SetTargetDeviceDir(dir string) {
  832. c.targetDeviceDir = dir
  833. }
  834. func (c *configImpl) TargetDeviceDir() string {
  835. return c.targetDeviceDir
  836. }
  837. func (c *configImpl) SetPdkBuild(pdk bool) {
  838. c.pdkBuild = pdk
  839. }
  840. func (c *configImpl) IsPdkBuild() bool {
  841. return c.pdkBuild
  842. }