blob: 7cc7caf3e8dc32147cf61eb2866c378ac72a0187 [file] [log] [blame]
Rupert Shuttleworth680387b2020-10-25 12:31:27 +00001// 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
15package build
16
17import (
Chris Parsonsc09495b2020-11-04 20:45:50 -050018 "io/ioutil"
19 "os"
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000020 "path/filepath"
21 "strings"
Patrice Arruda18cb70d2020-11-13 11:37:06 -080022
23 "android/soong/shared"
Patrice Arrudab7cf9ba2020-11-13 13:04:17 -080024 "android/soong/ui/metrics"
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000025)
26
Jingwen Chen3a4b58d2020-11-16 04:19:35 -050027// Main entry point to construct the Bazel build command line, environment variables
28// and post-processing steps (e.g. converge output directories)
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000029func runBazel(ctx Context, config Config) {
Patrice Arrudab7cf9ba2020-11-13 13:04:17 -080030 ctx.BeginTrace(metrics.RunBazel, "bazel")
31 defer ctx.EndTrace()
32
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000033 // "droid" is the default ninja target.
Jingwen Chen8024c952020-11-08 23:57:56 -050034 // TODO(b/160568333): stop hardcoding 'droid' to support building any
35 // Ninja target.
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000036 outputGroups := "droid"
37 if len(config.ninjaArgs) > 0 {
38 // At this stage, the residue slice of args passed to ninja
39 // are the ninja targets to build, which can correspond directly
40 // to ninja_build's output_groups.
41 outputGroups = strings.Join(config.ninjaArgs, ",")
42 }
43
Jingwen Chena26ac3c2020-11-10 08:17:59 -050044 config.environ.Set("COMBINED_NINJA", config.CombinedNinjaFile())
45 config.environ.Set("KATI_NINJA", config.KatiBuildNinjaFile())
46 config.environ.Set("PACKAGE_NINJA", config.KatiPackageNinjaFile())
47 config.environ.Set("SOONG_NINJA", config.SoongNinjaFile())
48
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000049 bazelExecutable := filepath.Join("tools", "bazel")
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000050 cmd := Command(ctx, config, "bazel", bazelExecutable)
51
52 if extra_startup_args, ok := cmd.Environment.Get("BAZEL_STARTUP_ARGS"); ok {
53 cmd.Args = append(cmd.Args, strings.Fields(extra_startup_args)...)
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000054 }
55
Patrice Arruda18cb70d2020-11-13 11:37:06 -080056 actionName := "build"
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000057 cmd.Args = append(cmd.Args,
Patrice Arruda18cb70d2020-11-13 11:37:06 -080058 actionName,
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000059 "--output_groups="+outputGroups,
Patrice Arruda18cb70d2020-11-13 11:37:06 -080060 "--profile="+filepath.Join(shared.BazelMetricsFilename(config.OutDir(), actionName)),
61 "--slim_profile=true",
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000062 )
63
64 if extra_build_args, ok := cmd.Environment.Get("BAZEL_BUILD_ARGS"); ok {
65 cmd.Args = append(cmd.Args, strings.Fields(extra_build_args)...)
66 }
67
68 cmd.Args = append(cmd.Args,
69 "//:"+config.TargetProduct()+"-"+config.TargetBuildVariant(),
70 )
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000071
Jingwen Chen3a4b58d2020-11-16 04:19:35 -050072 if pathEnvValue, ok := config.environ.Get("PATH"); ok {
73 cmd.Environment.Set("PATH", pathEnvValue)
74 cmd.Args = append(cmd.Args, "--action_env=PATH="+pathEnvValue)
75 }
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000076 cmd.Environment.Set("DIST_DIR", config.DistDir())
77 cmd.Environment.Set("SHELL", "/bin/bash")
78
79 ctx.Println(cmd.Cmd)
80 cmd.Dir = filepath.Join(config.OutDir(), "..")
81 ctx.Status.Status("Starting Bazel..")
82 cmd.RunAndStreamOrFatal()
Chris Parsonsc09495b2020-11-04 20:45:50 -050083
84 // Obtain the Bazel output directory for ninja_build.
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000085 infoCmd := Command(ctx, config, "bazel", bazelExecutable)
86
87 if extra_startup_args, ok := infoCmd.Environment.Get("BAZEL_STARTUP_ARGS"); ok {
88 infoCmd.Args = append(infoCmd.Args, strings.Fields(extra_startup_args)...)
Chris Parsonsc09495b2020-11-04 20:45:50 -050089 }
90
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000091 infoCmd.Args = append(infoCmd.Args,
92 "info",
93 "output_path",
94 )
Chris Parsonsc09495b2020-11-04 20:45:50 -050095
96 infoCmd.Environment.Set("DIST_DIR", config.DistDir())
97 infoCmd.Environment.Set("SHELL", "/bin/bash")
98 infoCmd.Dir = filepath.Join(config.OutDir(), "..")
99 ctx.Status.Status("Getting Bazel Info..")
100 outputBasePath := string(infoCmd.OutputOrFatal())
101 // TODO: Don't hardcode out/ as the bazel output directory. This is
102 // currently hardcoded as ninja_build.output_root.
103 bazelNinjaBuildOutputRoot := filepath.Join(outputBasePath, "..", "out")
104
105 symlinkOutdir(ctx, config, bazelNinjaBuildOutputRoot, ".")
106}
107
108// For all files F recursively under rootPath/relativePath, creates symlinks
109// such that OutDir/F resolves to rootPath/F via symlinks.
110func symlinkOutdir(ctx Context, config Config, rootPath string, relativePath string) {
111 destDir := filepath.Join(rootPath, relativePath)
112 os.MkdirAll(destDir, 0755)
113 files, err := ioutil.ReadDir(destDir)
114 if err != nil {
115 ctx.Fatal(err)
116 }
117 for _, f := range files {
118 destPath := filepath.Join(destDir, f.Name())
119 srcPath := filepath.Join(config.OutDir(), relativePath, f.Name())
120 if statResult, err := os.Stat(srcPath); err == nil {
121 if statResult.Mode().IsDir() && f.IsDir() {
122 // Directory under OutDir already exists, so recurse on its contents.
123 symlinkOutdir(ctx, config, rootPath, filepath.Join(relativePath, f.Name()))
124 } else if !statResult.Mode().IsDir() && !f.IsDir() {
125 // File exists both in source and destination, and it's not a directory
126 // in either location. Do nothing.
127 // This can arise for files which are generated under OutDir outside of
128 // soong_build, such as .bootstrap files.
129 } else {
130 // File is a directory in one location but not the other. Raise an error.
131 ctx.Fatalf("Could not link %s to %s due to conflict", srcPath, destPath)
132 }
133 } else if os.IsNotExist(err) {
134 // Create symlink srcPath -> fullDestPath.
135 os.Symlink(destPath, srcPath)
136 } else {
137 ctx.Fatalf("Unable to stat %s: %s", srcPath, err)
138 }
139 }
Rupert Shuttleworth680387b2020-10-25 12:31:27 +0000140}