Make @soong_injection usable in standalone builds.

Test: Presubmits.
Change-Id: I942e627f9f7b7428834258ef3ccefb7f1f5c7606
This commit is contained in:
Lukacs T. Berki 2021-05-11 16:54:29 +02:00
parent 564fce4578
commit 3069dd9d71
2 changed files with 16 additions and 12 deletions

View File

@ -347,7 +347,10 @@ func (r *builtinBazelRunner) issueBazelCommand(paths *bazelPaths, runName bazel.
bazelCmd := exec.Command(paths.bazelPath, cmdFlags...) bazelCmd := exec.Command(paths.bazelPath, cmdFlags...)
bazelCmd.Dir = absolutePath(paths.syntheticWorkspaceDir()) bazelCmd.Dir = absolutePath(paths.syntheticWorkspaceDir())
bazelCmd.Env = append(os.Environ(), "HOME="+paths.homeDir, pwdPrefix(), bazelCmd.Env = append(os.Environ(),
"HOME="+paths.homeDir,
pwdPrefix(),
"BUILD_DIR="+absolutePath(paths.buildDir),
// Disables local host detection of gcc; toolchain information is defined // Disables local host detection of gcc; toolchain information is defined
// explicitly in BUILD files. // explicitly in BUILD files.
"BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1") "BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1")
@ -583,8 +586,9 @@ func (context *bazelContext) InvokeBazel() error {
var err error var err error
soongInjectionPath := absolutePath(context.paths.injectedFilesDir()) soongInjectionPath := absolutePath(context.paths.injectedFilesDir())
if _, err := os.Stat(soongInjectionPath); os.IsNotExist(err) { mixedBuildsPath := filepath.Join(soongInjectionPath, "mixed_builds")
err = os.Mkdir(soongInjectionPath, 0777) if _, err := os.Stat(mixedBuildsPath); os.IsNotExist(err) {
err = os.MkdirAll(mixedBuildsPath, 0777)
} }
if err != nil { if err != nil {
return err return err
@ -596,14 +600,14 @@ func (context *bazelContext) InvokeBazel() error {
} }
err = ioutil.WriteFile( err = ioutil.WriteFile(
filepath.Join(soongInjectionPath, "main.bzl"), filepath.Join(mixedBuildsPath, "main.bzl"),
context.mainBzlFileContents(), 0666) context.mainBzlFileContents(), 0666)
if err != nil { if err != nil {
return err return err
} }
err = ioutil.WriteFile( err = ioutil.WriteFile(
filepath.Join(soongInjectionPath, "BUILD.bazel"), filepath.Join(mixedBuildsPath, "BUILD.bazel"),
context.mainBuildFileContents(), 0666) context.mainBuildFileContents(), 0666)
if err != nil { if err != nil {
return err return err
@ -615,7 +619,7 @@ func (context *bazelContext) InvokeBazel() error {
if err != nil { if err != nil {
return err return err
} }
buildrootLabel := "@soong_injection//:buildroot" buildrootLabel := "@soong_injection//mixed_builds:buildroot"
cqueryOutput, cqueryErr, err = context.issueBazelCommand( cqueryOutput, cqueryErr, err = context.issueBazelCommand(
context.paths, context.paths,
bazel.CqueryBuildRootRunName, bazel.CqueryBuildRootRunName,
@ -676,7 +680,7 @@ func (context *bazelContext) InvokeBazel() error {
_, _, err = context.issueBazelCommand( _, _, err = context.issueBazelCommand(
context.paths, context.paths,
bazel.BazelBuildPhonyRootRunName, bazel.BazelBuildPhonyRootRunName,
bazelCommand{"build", "@soong_injection//:phonyroot"}) bazelCommand{"build", "@soong_injection//mixed_builds:phonyroot"})
if err != nil { if err != nil {
return err return err

View File

@ -11,7 +11,7 @@ func TestRequestResultsAfterInvokeBazel(t *testing.T) {
label := "//foo:bar" label := "//foo:bar"
arch := Arm64 arch := Arm64
bazelContext, _ := testBazelContext(t, map[bazelCommand]string{ bazelContext, _ := testBazelContext(t, map[bazelCommand]string{
bazelCommand{command: "cquery", expression: "kind(rule, deps(@soong_injection//:buildroot))"}: `//foo:bar|arm64>>out/foo/bar.txt`, bazelCommand{command: "cquery", expression: "kind(rule, deps(@soong_injection//mixed_builds:buildroot))"}: `//foo:bar|arm64>>out/foo/bar.txt`,
}) })
g, ok := bazelContext.GetOutputFiles(label, arch) g, ok := bazelContext.GetOutputFiles(label, arch)
if ok { if ok {
@ -35,13 +35,13 @@ func TestInvokeBazelWritesBazelFiles(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Did not expect error invoking Bazel, but got %s", err) t.Fatalf("Did not expect error invoking Bazel, but got %s", err)
} }
if _, err := os.Stat(filepath.Join(baseDir, "soong_injection", "main.bzl")); os.IsNotExist(err) { if _, err := os.Stat(filepath.Join(baseDir, "soong_injection", "mixed_builds", "main.bzl")); os.IsNotExist(err) {
t.Errorf("Expected main.bzl to exist, but it does not") t.Errorf("Expected main.bzl to exist, but it does not")
} else if err != nil { } else if err != nil {
t.Errorf("Unexpected error stating main.bzl %s", err) t.Errorf("Unexpected error stating main.bzl %s", err)
} }
if _, err := os.Stat(filepath.Join(baseDir, "soong_injection", "BUILD.bazel")); os.IsNotExist(err) { if _, err := os.Stat(filepath.Join(baseDir, "soong_injection", "mixed_builds", "BUILD.bazel")); os.IsNotExist(err) {
t.Errorf("Expected BUILD.bazel to exist, but it does not") t.Errorf("Expected BUILD.bazel to exist, but it does not")
} else if err != nil { } else if err != nil {
t.Errorf("Unexpected error stating BUILD.bazel %s", err) t.Errorf("Unexpected error stating BUILD.bazel %s", err)
@ -56,7 +56,7 @@ func TestInvokeBazelWritesBazelFiles(t *testing.T) {
func TestInvokeBazelPopulatesBuildStatements(t *testing.T) { func TestInvokeBazelPopulatesBuildStatements(t *testing.T) {
bazelContext, _ := testBazelContext(t, map[bazelCommand]string{ bazelContext, _ := testBazelContext(t, map[bazelCommand]string{
bazelCommand{command: "aquery", expression: "deps(@soong_injection//:buildroot)"}: ` bazelCommand{command: "aquery", expression: "deps(@soong_injection//mixed_builds:buildroot)"}: `
{ {
"artifacts": [{ "artifacts": [{
"id": 1, "id": 1,
@ -105,7 +105,7 @@ func testBazelContext(t *testing.T, bazelCommandResults map[bazelCommand]string)
outputBase: "outputbase", outputBase: "outputbase",
workspaceDir: "workspace_dir", workspaceDir: "workspace_dir",
} }
aqueryCommand := bazelCommand{command: "aquery", expression: "deps(@soong_injection//:buildroot)"} aqueryCommand := bazelCommand{command: "aquery", expression: "deps(@soong_injection//mixed_builds:buildroot)"}
if _, exists := bazelCommandResults[aqueryCommand]; !exists { if _, exists := bazelCommandResults[aqueryCommand]; !exists {
bazelCommandResults[aqueryCommand] = "{}\n" bazelCommandResults[aqueryCommand] = "{}\n"
} }