Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 1 | // Copyright 2020 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 ( |
Chris Parsons | c09495b | 2020-11-04 20:45:50 -0500 | [diff] [blame] | 18 | "io/ioutil" |
| 19 | "os" |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 20 | "path/filepath" |
| 21 | "strings" |
Patrice Arruda | 18cb70d | 2020-11-13 11:37:06 -0800 | [diff] [blame^] | 22 | |
| 23 | "android/soong/shared" |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func runBazel(ctx Context, config Config) { |
| 27 | // "droid" is the default ninja target. |
Jingwen Chen | 8024c95 | 2020-11-08 23:57:56 -0500 | [diff] [blame] | 28 | // TODO(b/160568333): stop hardcoding 'droid' to support building any |
| 29 | // Ninja target. |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 30 | outputGroups := "droid" |
| 31 | if len(config.ninjaArgs) > 0 { |
| 32 | // At this stage, the residue slice of args passed to ninja |
| 33 | // are the ninja targets to build, which can correspond directly |
| 34 | // to ninja_build's output_groups. |
| 35 | outputGroups = strings.Join(config.ninjaArgs, ",") |
| 36 | } |
| 37 | |
Jingwen Chen | a26ac3c | 2020-11-10 08:17:59 -0500 | [diff] [blame] | 38 | config.environ.Set("COMBINED_NINJA", config.CombinedNinjaFile()) |
| 39 | config.environ.Set("KATI_NINJA", config.KatiBuildNinjaFile()) |
| 40 | config.environ.Set("PACKAGE_NINJA", config.KatiPackageNinjaFile()) |
| 41 | config.environ.Set("SOONG_NINJA", config.SoongNinjaFile()) |
| 42 | |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 43 | bazelExecutable := filepath.Join("tools", "bazel") |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 44 | cmd := Command(ctx, config, "bazel", bazelExecutable) |
| 45 | |
| 46 | if extra_startup_args, ok := cmd.Environment.Get("BAZEL_STARTUP_ARGS"); ok { |
| 47 | cmd.Args = append(cmd.Args, strings.Fields(extra_startup_args)...) |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Patrice Arruda | 18cb70d | 2020-11-13 11:37:06 -0800 | [diff] [blame^] | 50 | actionName := "build" |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 51 | cmd.Args = append(cmd.Args, |
Patrice Arruda | 18cb70d | 2020-11-13 11:37:06 -0800 | [diff] [blame^] | 52 | actionName, |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 53 | "--output_groups="+outputGroups, |
Patrice Arruda | 18cb70d | 2020-11-13 11:37:06 -0800 | [diff] [blame^] | 54 | "--profile="+filepath.Join(shared.BazelMetricsFilename(config.OutDir(), actionName)), |
| 55 | "--slim_profile=true", |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 56 | ) |
| 57 | |
| 58 | if extra_build_args, ok := cmd.Environment.Get("BAZEL_BUILD_ARGS"); ok { |
| 59 | cmd.Args = append(cmd.Args, strings.Fields(extra_build_args)...) |
| 60 | } |
| 61 | |
| 62 | cmd.Args = append(cmd.Args, |
| 63 | "//:"+config.TargetProduct()+"-"+config.TargetBuildVariant(), |
| 64 | ) |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 65 | |
| 66 | cmd.Environment.Set("DIST_DIR", config.DistDir()) |
| 67 | cmd.Environment.Set("SHELL", "/bin/bash") |
| 68 | |
| 69 | ctx.Println(cmd.Cmd) |
| 70 | cmd.Dir = filepath.Join(config.OutDir(), "..") |
| 71 | ctx.Status.Status("Starting Bazel..") |
| 72 | cmd.RunAndStreamOrFatal() |
Chris Parsons | c09495b | 2020-11-04 20:45:50 -0500 | [diff] [blame] | 73 | |
| 74 | // Obtain the Bazel output directory for ninja_build. |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 75 | infoCmd := Command(ctx, config, "bazel", bazelExecutable) |
| 76 | |
| 77 | if extra_startup_args, ok := infoCmd.Environment.Get("BAZEL_STARTUP_ARGS"); ok { |
| 78 | infoCmd.Args = append(infoCmd.Args, strings.Fields(extra_startup_args)...) |
Chris Parsons | c09495b | 2020-11-04 20:45:50 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Rupert Shuttleworth | f8ae317 | 2020-11-10 02:04:14 +0000 | [diff] [blame] | 81 | infoCmd.Args = append(infoCmd.Args, |
| 82 | "info", |
| 83 | "output_path", |
| 84 | ) |
Chris Parsons | c09495b | 2020-11-04 20:45:50 -0500 | [diff] [blame] | 85 | |
| 86 | infoCmd.Environment.Set("DIST_DIR", config.DistDir()) |
| 87 | infoCmd.Environment.Set("SHELL", "/bin/bash") |
| 88 | infoCmd.Dir = filepath.Join(config.OutDir(), "..") |
| 89 | ctx.Status.Status("Getting Bazel Info..") |
| 90 | outputBasePath := string(infoCmd.OutputOrFatal()) |
| 91 | // TODO: Don't hardcode out/ as the bazel output directory. This is |
| 92 | // currently hardcoded as ninja_build.output_root. |
| 93 | bazelNinjaBuildOutputRoot := filepath.Join(outputBasePath, "..", "out") |
| 94 | |
| 95 | symlinkOutdir(ctx, config, bazelNinjaBuildOutputRoot, ".") |
| 96 | } |
| 97 | |
| 98 | // For all files F recursively under rootPath/relativePath, creates symlinks |
| 99 | // such that OutDir/F resolves to rootPath/F via symlinks. |
| 100 | func symlinkOutdir(ctx Context, config Config, rootPath string, relativePath string) { |
| 101 | destDir := filepath.Join(rootPath, relativePath) |
| 102 | os.MkdirAll(destDir, 0755) |
| 103 | files, err := ioutil.ReadDir(destDir) |
| 104 | if err != nil { |
| 105 | ctx.Fatal(err) |
| 106 | } |
| 107 | for _, f := range files { |
| 108 | destPath := filepath.Join(destDir, f.Name()) |
| 109 | srcPath := filepath.Join(config.OutDir(), relativePath, f.Name()) |
| 110 | if statResult, err := os.Stat(srcPath); err == nil { |
| 111 | if statResult.Mode().IsDir() && f.IsDir() { |
| 112 | // Directory under OutDir already exists, so recurse on its contents. |
| 113 | symlinkOutdir(ctx, config, rootPath, filepath.Join(relativePath, f.Name())) |
| 114 | } else if !statResult.Mode().IsDir() && !f.IsDir() { |
| 115 | // File exists both in source and destination, and it's not a directory |
| 116 | // in either location. Do nothing. |
| 117 | // This can arise for files which are generated under OutDir outside of |
| 118 | // soong_build, such as .bootstrap files. |
| 119 | } else { |
| 120 | // File is a directory in one location but not the other. Raise an error. |
| 121 | ctx.Fatalf("Could not link %s to %s due to conflict", srcPath, destPath) |
| 122 | } |
| 123 | } else if os.IsNotExist(err) { |
| 124 | // Create symlink srcPath -> fullDestPath. |
| 125 | os.Symlink(destPath, srcPath) |
| 126 | } else { |
| 127 | ctx.Fatalf("Unable to stat %s: %s", srcPath, err) |
| 128 | } |
| 129 | } |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 130 | } |