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