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