blob: 978553d44d3b9451899fb49b0d27a5204cedebc1 [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.
26 outputGroups := "droid"
27 if len(config.ninjaArgs) > 0 {
28 // At this stage, the residue slice of args passed to ninja
29 // are the ninja targets to build, which can correspond directly
30 // to ninja_build's output_groups.
31 outputGroups = strings.Join(config.ninjaArgs, ",")
32 }
33
34 bazelExecutable := filepath.Join("tools", "bazel")
35 args := []string{
36 "build",
Rupert Shuttleworth680387b2020-10-25 12:31:27 +000037 "--output_groups=" + outputGroups,
38 "//:" + config.TargetProduct() + "-" + config.TargetBuildVariant(),
39 }
40
41 cmd := Command(ctx, config, "bazel", bazelExecutable, args...)
42
43 cmd.Environment.Set("DIST_DIR", config.DistDir())
44 cmd.Environment.Set("SHELL", "/bin/bash")
45
46 ctx.Println(cmd.Cmd)
47 cmd.Dir = filepath.Join(config.OutDir(), "..")
48 ctx.Status.Status("Starting Bazel..")
49 cmd.RunAndStreamOrFatal()
Chris Parsonsc09495b2020-11-04 20:45:50 -050050
51 // Obtain the Bazel output directory for ninja_build.
52 infoArgs := []string{
53 "info",
54 "output_path",
55 }
56
57 infoCmd := Command(ctx, config, "bazel", bazelExecutable, infoArgs...)
58
59 infoCmd.Environment.Set("DIST_DIR", config.DistDir())
60 infoCmd.Environment.Set("SHELL", "/bin/bash")
61 infoCmd.Dir = filepath.Join(config.OutDir(), "..")
62 ctx.Status.Status("Getting Bazel Info..")
63 outputBasePath := string(infoCmd.OutputOrFatal())
64 // TODO: Don't hardcode out/ as the bazel output directory. This is
65 // currently hardcoded as ninja_build.output_root.
66 bazelNinjaBuildOutputRoot := filepath.Join(outputBasePath, "..", "out")
67
68 symlinkOutdir(ctx, config, bazelNinjaBuildOutputRoot, ".")
69}
70
71// For all files F recursively under rootPath/relativePath, creates symlinks
72// such that OutDir/F resolves to rootPath/F via symlinks.
73func symlinkOutdir(ctx Context, config Config, rootPath string, relativePath string) {
74 destDir := filepath.Join(rootPath, relativePath)
75 os.MkdirAll(destDir, 0755)
76 files, err := ioutil.ReadDir(destDir)
77 if err != nil {
78 ctx.Fatal(err)
79 }
80 for _, f := range files {
81 destPath := filepath.Join(destDir, f.Name())
82 srcPath := filepath.Join(config.OutDir(), relativePath, f.Name())
83 if statResult, err := os.Stat(srcPath); err == nil {
84 if statResult.Mode().IsDir() && f.IsDir() {
85 // Directory under OutDir already exists, so recurse on its contents.
86 symlinkOutdir(ctx, config, rootPath, filepath.Join(relativePath, f.Name()))
87 } else if !statResult.Mode().IsDir() && !f.IsDir() {
88 // File exists both in source and destination, and it's not a directory
89 // in either location. Do nothing.
90 // This can arise for files which are generated under OutDir outside of
91 // soong_build, such as .bootstrap files.
92 } else {
93 // File is a directory in one location but not the other. Raise an error.
94 ctx.Fatalf("Could not link %s to %s due to conflict", srcPath, destPath)
95 }
96 } else if os.IsNotExist(err) {
97 // Create symlink srcPath -> fullDestPath.
98 os.Symlink(destPath, srcPath)
99 } else {
100 ctx.Fatalf("Unable to stat %s: %s", srcPath, err)
101 }
102 }
Rupert Shuttleworth680387b2020-10-25 12:31:27 +0000103}