Make running soong_build in alternate modes nicer.
This includes the JSON graph generator and bp2build.
Before:
GENERATE_BAZEL_FILES=1 m nothing
GENERATE_JSON_MODULE_GRAPH=1 m nothing
Now:
m json-module-graph
m bp2build
They can now also be combined with other targets or each other.
The longer-term goal is to run "m queryview" and "m soong_docs" using
the same infrastructure. There are two alternate approaches:
1. Call soong_build from within the main Ninja invocation. This requires
two sequential soong_build invocations and is thus slower.
2. Do everything requested in the same soong_build invocation. This
would be faster, but one AFAIU can't tell Ninja that multiple possible
actions can build the same output so that doesn't work.
(1) is somewhat more desirable because soong_docs seems to be built
from build/make/core/main.mk ; I assume that that can be worked around
although I haven't checked where the output of "m soong_docs" goes.
Test: Presubmits.
Change-Id: If5ba36490d9f3f60733e6d6be9286eb2b67c3ff5
diff --git a/ui/build/build.go b/ui/build/build.go
index d869bf0..2e44aaa 100644
--- a/ui/build/build.go
+++ b/ui/build/build.go
@@ -248,6 +248,16 @@
what = what &^ RunNinja
}
+ if !config.SoongBuildInvocationNeeded() {
+ // This means that the output of soong_build is not needed and thus it would
+ // run unnecessarily. In addition, if this code wasn't there invocations
+ // with only special-cased target names like "m bp2build" would result in
+ // passing Ninja the empty target list and it would then build the default
+ // targets which is not what the user asked for.
+ what = what &^ RunNinja
+ what = what &^ RunKati
+ }
+
if config.StartGoma() {
startGoma(ctx, config)
}
@@ -278,16 +288,6 @@
if what&RunSoong != 0 {
runSoong(ctx, config)
-
- if config.bazelBuildMode() == generateBuildFiles {
- // Return early, if we're using Soong as solely the generator of BUILD files.
- return
- }
-
- if config.bazelBuildMode() == generateJsonModuleGraph {
- // Return early, if we're using Soong as solely the generator of the JSON module graph
- return
- }
}
if what&RunKati != 0 {
diff --git a/ui/build/config.go b/ui/build/config.go
index 6de7a05..0b93b18 100644
--- a/ui/build/config.go
+++ b/ui/build/config.go
@@ -33,7 +33,8 @@
type Config struct{ *configImpl }
type configImpl struct {
- // From the environment
+ // Some targets that are implemented in soong_build
+ // (bp2build, json-module-graph) are not here and have their own bits below.
arguments []string
goma bool
environ *Environment
@@ -41,17 +42,19 @@
buildDateTime string
// From the arguments
- parallel int
- keepGoing int
- verbose bool
- checkbuild bool
- dist bool
- skipConfig bool
- skipKati bool
- skipKatiNinja bool
- skipSoong bool
- skipNinja bool
- skipSoongTests bool
+ parallel int
+ keepGoing int
+ verbose bool
+ checkbuild bool
+ dist bool
+ jsonModuleGraph bool
+ bp2build bool
+ skipConfig bool
+ skipKati bool
+ skipKatiNinja bool
+ skipSoong bool
+ skipNinja bool
+ skipSoongTests bool
// From the product config
katiArgs []string
@@ -109,9 +112,6 @@
// Only generate build files (in a subdirectory of the out directory) and exit.
generateBuildFiles
- // Only generate the Soong json module graph for use with jq, and exit.
- generateJsonModuleGraph
-
// Generate synthetic build files and incorporate these files into a build which
// partially uses Bazel. Build metadata may come from Android.bp or BUILD files.
mixedBuild
@@ -639,6 +639,10 @@
c.environ.Set(k, v)
} else if arg == "dist" {
c.dist = true
+ } else if arg == "json-module-graph" {
+ c.jsonModuleGraph = true
+ } else if arg == "bp2build" {
+ c.bp2build = true
} else {
if arg == "checkbuild" {
c.checkbuild = true
@@ -705,6 +709,26 @@
return c.arguments
}
+func (c *configImpl) SoongBuildInvocationNeeded() bool {
+ if c.Dist() {
+ return true
+ }
+
+ if len(c.Arguments()) > 0 {
+ // Explicit targets requested that are not special targets like b2pbuild
+ // or the JSON module graph
+ return true
+ }
+
+ if !c.JsonModuleGraph() && !c.Bp2Build() {
+ // Command line was empty, the default Ninja target is built
+ return true
+ }
+
+ // build.ninja doesn't need to be generated
+ return false
+}
+
func (c *configImpl) OutDir() string {
if outDir, ok := c.environ.Get("OUT_DIR"); ok {
return outDir
@@ -790,6 +814,14 @@
return c.dist
}
+func (c *configImpl) JsonModuleGraph() bool {
+ return c.jsonModuleGraph
+}
+
+func (c *configImpl) Bp2Build() bool {
+ return c.bp2build
+}
+
func (c *configImpl) IsVerbose() bool {
return c.verbose
}
@@ -935,10 +967,6 @@
func (c *configImpl) bazelBuildMode() bazelBuildMode {
if c.Environment().IsEnvTrue("USE_BAZEL_ANALYSIS") {
return mixedBuild
- } else if c.Environment().IsEnvTrue("GENERATE_BAZEL_FILES") {
- return generateBuildFiles
- } else if c.Environment().IsEnvTrue("GENERATE_JSON_MODULE_GRAPH") {
- return generateJsonModuleGraph
} else {
return noBazel
}
diff --git a/ui/build/soong.go b/ui/build/soong.go
index d4f6f2f..fef30da 100644
--- a/ui/build/soong.go
+++ b/ui/build/soong.go
@@ -275,7 +275,7 @@
}
buildMode := config.bazelBuildMode()
- integratedBp2Build := (buildMode == mixedBuild) || (buildMode == generateBuildFiles)
+ integratedBp2Build := buildMode == mixedBuild
// This is done unconditionally, but does not take a measurable amount of time
bootstrapBlueprint(ctx, config)
@@ -351,18 +351,22 @@
cmd.RunAndStreamOrFatal()
}
- var target string
+ targets := make([]string, 0, 0)
- if config.bazelBuildMode() == generateBuildFiles {
- target = config.Bp2BuildMarkerFile()
- } else if config.bazelBuildMode() == generateJsonModuleGraph {
- target = config.ModuleGraphFile()
- } else {
- // This build generates <builddir>/build.ninja, which is used later by build/soong/ui/build/build.go#Build().
- target = config.MainNinjaFile()
+ if config.JsonModuleGraph() {
+ targets = append(targets, config.ModuleGraphFile())
}
- ninja("bootstrap", ".bootstrap/build.ninja", target)
+ if config.Bp2Build() {
+ targets = append(targets, config.Bp2BuildMarkerFile())
+ }
+
+ if config.SoongBuildInvocationNeeded() {
+ // This build generates <builddir>/build.ninja, which is used later by build/soong/ui/build/build.go#Build().
+ targets = append(targets, config.MainNinjaFile())
+ }
+
+ ninja("bootstrap", ".bootstrap/build.ninja", targets...)
var soongBuildMetrics *soong_metrics_proto.SoongBuildMetrics
if shouldCollectBuildSoongMetrics(config) {
@@ -404,7 +408,7 @@
func shouldCollectBuildSoongMetrics(config Config) bool {
// Do not collect metrics protobuf if the soong_build binary ran as the
// bp2build converter or the JSON graph dump.
- return config.bazelBuildMode() != generateBuildFiles && config.bazelBuildMode() != generateJsonModuleGraph
+ return config.SoongBuildInvocationNeeded()
}
func loadSoongBuildMetrics(ctx Context, config Config) *soong_metrics_proto.SoongBuildMetrics {