blob: 4b03bc16f8d270223990882026aaab1dbb68d721 [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
27func runBazel(ctx Context, config Config) {
Patrice Arrudab7cf9ba2020-11-13 13:04:17 -080028 ctx.BeginTrace(metrics.RunBazel, "bazel")
29 defer ctx.EndTrace()
30
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000031 // "droid" is the default ninja target.
Jingwen Chen8024c952020-11-08 23:57:56 -050032 // TODO(b/160568333): stop hardcoding 'droid' to support building any
33 // Ninja target.
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000034 outputGroups := "droid"
35 if len(config.ninjaArgs) > 0 {
36 // At this stage, the residue slice of args passed to ninja
37 // are the ninja targets to build, which can correspond directly
38 // to ninja_build's output_groups.
39 outputGroups = strings.Join(config.ninjaArgs, ",")
40 }
41
Jingwen Chena26ac3c2020-11-10 08:17:59 -050042 config.environ.Set("COMBINED_NINJA", config.CombinedNinjaFile())
43 config.environ.Set("KATI_NINJA", config.KatiBuildNinjaFile())
44 config.environ.Set("PACKAGE_NINJA", config.KatiPackageNinjaFile())
45 config.environ.Set("SOONG_NINJA", config.SoongNinjaFile())
46
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000047 bazelExecutable := filepath.Join("tools", "bazel")
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000048 cmd := Command(ctx, config, "bazel", bazelExecutable)
49
50 if extra_startup_args, ok := cmd.Environment.Get("BAZEL_STARTUP_ARGS"); ok {
51 cmd.Args = append(cmd.Args, strings.Fields(extra_startup_args)...)
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000052 }
53
Patrice Arruda18cb70d2020-11-13 11:37:06 -080054 actionName := "build"
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000055 cmd.Args = append(cmd.Args,
Patrice Arruda18cb70d2020-11-13 11:37:06 -080056 actionName,
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000057 "--output_groups="+outputGroups,
Patrice Arruda18cb70d2020-11-13 11:37:06 -080058 "--profile="+filepath.Join(shared.BazelMetricsFilename(config.OutDir(), actionName)),
59 "--slim_profile=true",
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000060 )
61
62 if extra_build_args, ok := cmd.Environment.Get("BAZEL_BUILD_ARGS"); ok {
63 cmd.Args = append(cmd.Args, strings.Fields(extra_build_args)...)
64 }
65
66 cmd.Args = append(cmd.Args,
67 "//:"+config.TargetProduct()+"-"+config.TargetBuildVariant(),
68 )
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000069
70 cmd.Environment.Set("DIST_DIR", config.DistDir())
71 cmd.Environment.Set("SHELL", "/bin/bash")
72
73 ctx.Println(cmd.Cmd)
74 cmd.Dir = filepath.Join(config.OutDir(), "..")
75 ctx.Status.Status("Starting Bazel..")
76 cmd.RunAndStreamOrFatal()
Chris Parsonsc09495b2020-11-04 20:45:50 -050077
78 // Obtain the Bazel output directory for ninja_build.
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000079 infoCmd := Command(ctx, config, "bazel", bazelExecutable)
80
81 if extra_startup_args, ok := infoCmd.Environment.Get("BAZEL_STARTUP_ARGS"); ok {
82 infoCmd.Args = append(infoCmd.Args, strings.Fields(extra_startup_args)...)
Chris Parsonsc09495b2020-11-04 20:45:50 -050083 }
84
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000085 infoCmd.Args = append(infoCmd.Args,
86 "info",
87 "output_path",
88 )
Chris Parsonsc09495b2020-11-04 20:45:50 -050089
90 infoCmd.Environment.Set("DIST_DIR", config.DistDir())
91 infoCmd.Environment.Set("SHELL", "/bin/bash")
92 infoCmd.Dir = filepath.Join(config.OutDir(), "..")
93 ctx.Status.Status("Getting Bazel Info..")
94 outputBasePath := string(infoCmd.OutputOrFatal())
95 // TODO: Don't hardcode out/ as the bazel output directory. This is
96 // currently hardcoded as ninja_build.output_root.
97 bazelNinjaBuildOutputRoot := filepath.Join(outputBasePath, "..", "out")
98
99 symlinkOutdir(ctx, config, bazelNinjaBuildOutputRoot, ".")
100}
101
102// For all files F recursively under rootPath/relativePath, creates symlinks
103// such that OutDir/F resolves to rootPath/F via symlinks.
104func symlinkOutdir(ctx Context, config Config, rootPath string, relativePath string) {
105 destDir := filepath.Join(rootPath, relativePath)
106 os.MkdirAll(destDir, 0755)
107 files, err := ioutil.ReadDir(destDir)
108 if err != nil {
109 ctx.Fatal(err)
110 }
111 for _, f := range files {
112 destPath := filepath.Join(destDir, f.Name())
113 srcPath := filepath.Join(config.OutDir(), relativePath, f.Name())
114 if statResult, err := os.Stat(srcPath); err == nil {
115 if statResult.Mode().IsDir() && f.IsDir() {
116 // Directory under OutDir already exists, so recurse on its contents.
117 symlinkOutdir(ctx, config, rootPath, filepath.Join(relativePath, f.Name()))
118 } else if !statResult.Mode().IsDir() && !f.IsDir() {
119 // File exists both in source and destination, and it's not a directory
120 // in either location. Do nothing.
121 // This can arise for files which are generated under OutDir outside of
122 // soong_build, such as .bootstrap files.
123 } else {
124 // File is a directory in one location but not the other. Raise an error.
125 ctx.Fatalf("Could not link %s to %s due to conflict", srcPath, destPath)
126 }
127 } else if os.IsNotExist(err) {
128 // Create symlink srcPath -> fullDestPath.
129 os.Symlink(destPath, srcPath)
130 } else {
131 ctx.Fatalf("Unable to stat %s: %s", srcPath, err)
132 }
133 }
Rupert Shuttleworth680387b2020-10-25 12:31:27 +0000134}