blob: ca9ad43526e4e4d1bda37f4f1ea3f0a935713530 [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"
22)
23
24func runBazel(ctx Context, config Config) {
25 // "droid" is the default ninja target.
Jingwen Chen8024c952020-11-08 23:57:56 -050026 // TODO(b/160568333): stop hardcoding 'droid' to support building any
27 // Ninja target.
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000028 outputGroups := "droid"
29 if len(config.ninjaArgs) > 0 {
30 // At this stage, the residue slice of args passed to ninja
31 // are the ninja targets to build, which can correspond directly
32 // to ninja_build's output_groups.
33 outputGroups = strings.Join(config.ninjaArgs, ",")
34 }
35
36 bazelExecutable := filepath.Join("tools", "bazel")
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000037 cmd := Command(ctx, config, "bazel", bazelExecutable)
38
39 if extra_startup_args, ok := cmd.Environment.Get("BAZEL_STARTUP_ARGS"); ok {
40 cmd.Args = append(cmd.Args, strings.Fields(extra_startup_args)...)
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000041 }
42
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000043 cmd.Args = append(cmd.Args,
44 "build",
45 "--output_groups="+outputGroups,
46 )
47
48 if extra_build_args, ok := cmd.Environment.Get("BAZEL_BUILD_ARGS"); ok {
49 cmd.Args = append(cmd.Args, strings.Fields(extra_build_args)...)
50 }
51
52 cmd.Args = append(cmd.Args,
53 "//:"+config.TargetProduct()+"-"+config.TargetBuildVariant(),
54 )
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000055
56 cmd.Environment.Set("DIST_DIR", config.DistDir())
57 cmd.Environment.Set("SHELL", "/bin/bash")
58
59 ctx.Println(cmd.Cmd)
60 cmd.Dir = filepath.Join(config.OutDir(), "..")
61 ctx.Status.Status("Starting Bazel..")
62 cmd.RunAndStreamOrFatal()
Chris Parsonsc09495b2020-11-04 20:45:50 -050063
64 // Obtain the Bazel output directory for ninja_build.
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000065 infoCmd := Command(ctx, config, "bazel", bazelExecutable)
66
67 if extra_startup_args, ok := infoCmd.Environment.Get("BAZEL_STARTUP_ARGS"); ok {
68 infoCmd.Args = append(infoCmd.Args, strings.Fields(extra_startup_args)...)
Chris Parsonsc09495b2020-11-04 20:45:50 -050069 }
70
Rupert Shuttleworthf8ae3172020-11-10 02:04:14 +000071 infoCmd.Args = append(infoCmd.Args,
72 "info",
73 "output_path",
74 )
Chris Parsonsc09495b2020-11-04 20:45:50 -050075
76 infoCmd.Environment.Set("DIST_DIR", config.DistDir())
77 infoCmd.Environment.Set("SHELL", "/bin/bash")
78 infoCmd.Dir = filepath.Join(config.OutDir(), "..")
79 ctx.Status.Status("Getting Bazel Info..")
80 outputBasePath := string(infoCmd.OutputOrFatal())
81 // TODO: Don't hardcode out/ as the bazel output directory. This is
82 // currently hardcoded as ninja_build.output_root.
83 bazelNinjaBuildOutputRoot := filepath.Join(outputBasePath, "..", "out")
84
85 symlinkOutdir(ctx, config, bazelNinjaBuildOutputRoot, ".")
86}
87
88// For all files F recursively under rootPath/relativePath, creates symlinks
89// such that OutDir/F resolves to rootPath/F via symlinks.
90func symlinkOutdir(ctx Context, config Config, rootPath string, relativePath string) {
91 destDir := filepath.Join(rootPath, relativePath)
92 os.MkdirAll(destDir, 0755)
93 files, err := ioutil.ReadDir(destDir)
94 if err != nil {
95 ctx.Fatal(err)
96 }
97 for _, f := range files {
98 destPath := filepath.Join(destDir, f.Name())
99 srcPath := filepath.Join(config.OutDir(), relativePath, f.Name())
100 if statResult, err := os.Stat(srcPath); err == nil {
101 if statResult.Mode().IsDir() && f.IsDir() {
102 // Directory under OutDir already exists, so recurse on its contents.
103 symlinkOutdir(ctx, config, rootPath, filepath.Join(relativePath, f.Name()))
104 } else if !statResult.Mode().IsDir() && !f.IsDir() {
105 // File exists both in source and destination, and it's not a directory
106 // in either location. Do nothing.
107 // This can arise for files which are generated under OutDir outside of
108 // soong_build, such as .bootstrap files.
109 } else {
110 // File is a directory in one location but not the other. Raise an error.
111 ctx.Fatalf("Could not link %s to %s due to conflict", srcPath, destPath)
112 }
113 } else if os.IsNotExist(err) {
114 // Create symlink srcPath -> fullDestPath.
115 os.Symlink(destPath, srcPath)
116 } else {
117 ctx.Fatalf("Unable to stat %s: %s", srcPath, err)
118 }
119 }
Rupert Shuttleworth680387b2020-10-25 12:31:27 +0000120}