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