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