Output informational messages only when BP2BUILD_VERBOSE is set.

Test: manual
Change-Id: Ieeb00a002e07b04449d70614ce205c47c1dd7bce
This commit is contained in:
Sasha Smundak
2022-05-19 19:34:31 -07:00
parent 6a2b7c40b3
commit 0fd93e0756
8 changed files with 28 additions and 19 deletions

View File

@@ -40,7 +40,7 @@ func Codegen(ctx *CodegenContext) CodegenMetrics {
fmt.Printf("ERROR: Encountered %d error(s): \nERROR: %s", len(errs), strings.Join(errMsgs, "\n")) fmt.Printf("ERROR: Encountered %d error(s): \nERROR: %s", len(errs), strings.Join(errMsgs, "\n"))
os.Exit(1) os.Exit(1)
} }
bp2buildFiles := CreateBazelFiles(nil, res.buildFileToTargets, ctx.mode) bp2buildFiles := CreateBazelFiles(ctx.Config(), nil, res.buildFileToTargets, ctx.mode)
writeFiles(ctx, bp2buildDir, bp2buildFiles) writeFiles(ctx, bp2buildDir, bp2buildFiles)
soongInjectionDir := android.PathForOutput(ctx, bazel.SoongInjectionDirName) soongInjectionDir := android.PathForOutput(ctx, bazel.SoongInjectionDirName)

View File

@@ -189,7 +189,7 @@ func TestGenerateSoongModuleBzl(t *testing.T) {
content: "irrelevant", content: "irrelevant",
}, },
} }
files := CreateBazelFiles(ruleShims, make(map[string]BazelTargets), QueryView) files := CreateBazelFiles(android.NullConfig("out", "out/soong"), ruleShims, make(map[string]BazelTargets), QueryView)
var actualSoongModuleBzl BazelFile var actualSoongModuleBzl BazelFile
for _, f := range files { for _, f := range files {

View File

@@ -50,6 +50,7 @@ func convertedModules(convertedModules []string) string {
} }
func CreateBazelFiles( func CreateBazelFiles(
cfg android.Config,
ruleShims map[string]RuleShim, ruleShims map[string]RuleShim,
buildToTargets map[string]BazelTargets, buildToTargets map[string]BazelTargets,
mode CodegenMode) []BazelFile { mode CodegenMode) []BazelFile {
@@ -74,16 +75,19 @@ func CreateBazelFiles(
files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims))) files = append(files, newFile(bazelRulesSubDir, "soong_module.bzl", generateSoongModuleBzl(ruleShims)))
} }
files = append(files, createBuildFiles(buildToTargets, mode)...) files = append(files, createBuildFiles(cfg, buildToTargets, mode)...)
return files return files
} }
func createBuildFiles(buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile { func createBuildFiles(cfg android.Config, buildToTargets map[string]BazelTargets, mode CodegenMode) []BazelFile {
files := make([]BazelFile, 0, len(buildToTargets)) files := make([]BazelFile, 0, len(buildToTargets))
warnNotWriting := cfg.IsEnvTrue("BP2BUILD_VERBOSE")
for _, dir := range android.SortedStringKeys(buildToTargets) { for _, dir := range android.SortedStringKeys(buildToTargets) {
if mode == Bp2Build && android.ShouldKeepExistingBuildFileForDir(dir) { if mode == Bp2Build && android.ShouldKeepExistingBuildFileForDir(dir) {
fmt.Printf("[bp2build] Not writing generated BUILD file for dir: '%s'\n", dir) if warnNotWriting {
fmt.Printf("[bp2build] Not writing generated BUILD file for dir: '%s'\n", dir)
}
continue continue
} }
targets := buildToTargets[dir] targets := buildToTargets[dir]

View File

@@ -27,7 +27,8 @@ type bazelFilepath struct {
} }
func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) { func TestCreateBazelFiles_QueryView_AddsTopLevelFiles(t *testing.T) {
files := CreateBazelFiles(map[string]RuleShim{}, map[string]BazelTargets{}, QueryView) files := CreateBazelFiles(android.NullConfig("out", "out/soong"),
map[string]RuleShim{}, map[string]BazelTargets{}, QueryView)
expectedFilePaths := []bazelFilepath{ expectedFilePaths := []bazelFilepath{
{ {
dir: "", dir: "",

View File

@@ -116,8 +116,6 @@ func (metrics *CodegenMetrics) Write(dir string) {
} }
if _, err := os.Stat(metricsFile); err != nil { if _, err := os.Stat(metricsFile); err != nil {
fail(err, "MISSING BP2BUILD METRICS OUTPUT: Failed to `stat` %s", metricsFile) fail(err, "MISSING BP2BUILD METRICS OUTPUT: Failed to `stat` %s", metricsFile)
} else {
fmt.Printf("\nWrote bp2build metrics to: %s\n", metricsFile)
} }
} }

View File

@@ -1,6 +1,7 @@
package bp2build package bp2build
import ( import (
"android/soong/android"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@@ -114,7 +115,7 @@ func isDir(path string, fi os.FileInfo) bool {
// contain every file in buildFilesDir and srcDir excluding the files in // contain every file in buildFilesDir and srcDir excluding the files in
// exclude. Collects every directory encountered during the traversal of srcDir // exclude. Collects every directory encountered during the traversal of srcDir
// into acc. // into acc.
func plantSymlinkForestRecursive(topdir string, forestDir string, buildFilesDir string, srcDir string, exclude *node, acc *[]string, okay *bool) { func plantSymlinkForestRecursive(cfg android.Config, topdir string, forestDir string, buildFilesDir string, srcDir string, exclude *node, acc *[]string, okay *bool) {
if exclude != nil && exclude.excluded { if exclude != nil && exclude.excluded {
// This directory is not needed, bail out // This directory is not needed, bail out
return return
@@ -179,7 +180,7 @@ func plantSymlinkForestRecursive(topdir string, forestDir string, buildFilesDir
if bDir && excludeChild != nil { if bDir && excludeChild != nil {
// Not in the source tree, but we have to exclude something from under // Not in the source tree, but we have to exclude something from under
// this subtree, so descend // this subtree, so descend
plantSymlinkForestRecursive(topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay) plantSymlinkForestRecursive(cfg, topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay)
} else { } else {
// Not in the source tree, symlink BUILD file // Not in the source tree, symlink BUILD file
symlinkIntoForest(topdir, forestChild, buildFilesChild) symlinkIntoForest(topdir, forestChild, buildFilesChild)
@@ -188,19 +189,21 @@ func plantSymlinkForestRecursive(topdir string, forestDir string, buildFilesDir
if sDir && excludeChild != nil { if sDir && excludeChild != nil {
// Not in the build file tree, but we have to exclude something from // Not in the build file tree, but we have to exclude something from
// under this subtree, so descend // under this subtree, so descend
plantSymlinkForestRecursive(topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay) plantSymlinkForestRecursive(cfg, topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay)
} else { } else {
// Not in the build file tree, symlink source tree, carry on // Not in the build file tree, symlink source tree, carry on
symlinkIntoForest(topdir, forestChild, srcChild) symlinkIntoForest(topdir, forestChild, srcChild)
} }
} else if sDir && bDir { } else if sDir && bDir {
// Both are directories. Descend. // Both are directories. Descend.
plantSymlinkForestRecursive(topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay) plantSymlinkForestRecursive(cfg, topdir, forestChild, buildFilesChild, srcChild, excludeChild, acc, okay)
} else if !sDir && !bDir { } else if !sDir && !bDir {
// Neither is a directory. Prioritize BUILD files generated by bp2build // Neither is a directory. Prioritize BUILD files generated by bp2build
// over any BUILD file imported into external/. // over any BUILD file imported into external/.
fmt.Fprintf(os.Stderr, "Both '%s' and '%s' exist, symlinking the former to '%s'\n", if cfg.IsEnvTrue("BP2BUILD_VERBOSE") {
buildFilesChild, srcChild, forestChild) fmt.Fprintf(os.Stderr, "Both '%s' and '%s' exist, symlinking the former to '%s'\n",
buildFilesChild, srcChild, forestChild)
}
symlinkIntoForest(topdir, forestChild, buildFilesChild) symlinkIntoForest(topdir, forestChild, buildFilesChild)
} else { } else {
// Both exist and one is a file. This is an error. // Both exist and one is a file. This is an error.
@@ -216,12 +219,12 @@ func plantSymlinkForestRecursive(topdir string, forestDir string, buildFilesDir
// "srcDir" while excluding paths listed in "exclude". Returns the set of paths // "srcDir" while excluding paths listed in "exclude". Returns the set of paths
// under srcDir on which readdir() had to be called to produce the symlink // under srcDir on which readdir() had to be called to produce the symlink
// forest. // forest.
func PlantSymlinkForest(topdir string, forest string, buildFiles string, srcDir string, exclude []string) []string { func PlantSymlinkForest(cfg android.Config, topdir string, forest string, buildFiles string, srcDir string, exclude []string) []string {
deps := make([]string, 0) deps := make([]string, 0)
os.RemoveAll(shared.JoinPath(topdir, forest)) os.RemoveAll(shared.JoinPath(topdir, forest))
excludeTree := treeFromExcludePathList(exclude) excludeTree := treeFromExcludePathList(exclude)
okay := true okay := true
plantSymlinkForestRecursive(topdir, forest, buildFiles, srcDir, excludeTree, &deps, &okay) plantSymlinkForestRecursive(cfg, topdir, forest, buildFiles, srcDir, excludeTree, &deps, &okay)
if !okay { if !okay {
os.Exit(1) os.Exit(1)
} }

View File

@@ -552,7 +552,7 @@ func runBp2Build(configuration android.Config, extraNinjaDeps []string) {
excludes = append(excludes, getTemporaryExcludes()...) excludes = append(excludes, getTemporaryExcludes()...)
symlinkForestDeps := bp2build.PlantSymlinkForest( symlinkForestDeps := bp2build.PlantSymlinkForest(
topDir, workspaceRoot, generatedRoot, ".", excludes) configuration, topDir, workspaceRoot, generatedRoot, ".", excludes)
ninjaDeps = append(ninjaDeps, codegenContext.AdditionalNinjaDeps()...) ninjaDeps = append(ninjaDeps, codegenContext.AdditionalNinjaDeps()...)
ninjaDeps = append(ninjaDeps, symlinkForestDeps...) ninjaDeps = append(ninjaDeps, symlinkForestDeps...)
@@ -566,7 +566,9 @@ func runBp2Build(configuration android.Config, extraNinjaDeps []string) {
// Only report metrics when in bp2build mode. The metrics aren't relevant // Only report metrics when in bp2build mode. The metrics aren't relevant
// for queryview, since that's a total repo-wide conversion and there's a // for queryview, since that's a total repo-wide conversion and there's a
// 1:1 mapping for each module. // 1:1 mapping for each module.
metrics.Print() if configuration.IsEnvTrue("BP2BUILD_VERBOSE") {
metrics.Print()
}
writeBp2BuildMetrics(&metrics, configuration, eventHandler) writeBp2BuildMetrics(&metrics, configuration, eventHandler)
} }

View File

@@ -32,7 +32,8 @@ func createBazelQueryView(ctx *bp2build.CodegenContext, bazelQueryViewDir string
panic(err) panic(err)
} }
filesToWrite := bp2build.CreateBazelFiles(ruleShims, res.BuildDirToTargets(), bp2build.QueryView) filesToWrite := bp2build.CreateBazelFiles(ctx.Config(), ruleShims, res.BuildDirToTargets(),
bp2build.QueryView)
for _, f := range filesToWrite { for _, f := range filesToWrite {
if err := writeReadOnlyFile(bazelQueryViewDir, f); err != nil { if err := writeReadOnlyFile(bazelQueryViewDir, f); err != nil {
return err return err