From a87506f5e8a88c0dc925d90970021d47e7e2a359 Mon Sep 17 00:00:00 2001 From: Jeongik Cha Date: Thu, 1 Jun 2023 23:16:41 +0900 Subject: [PATCH] Use HINT_FROM_SOONG if ninja_log doesn't exist In non-incremental build, there is no ninja_log. For this case, use HINT_FROM_SOONG as an alternative solution. Bug: 273947040 Test: 1.m after removing out/.ninja_log 2.check if non-incremental CI build uses HINT_FROM_SOOONG 3.check if incremental CI build uses NINJA_LOG 4.check if there is no regression in CUJ Change-Id: I00cd216df096cb2288eeab233729acefb0d1b73c --- cmd/soong_build/main.go | 17 +++++++++++++++-- ui/build/config.go | 21 +++++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/cmd/soong_build/main.go b/cmd/soong_build/main.go index d589925c9..2e6b6d44a 100644 --- a/cmd/soong_build/main.go +++ b/cmd/soong_build/main.go @@ -16,6 +16,7 @@ package main import ( "bytes" + "errors" "flag" "fmt" "os" @@ -135,12 +136,24 @@ func runMixedModeBuild(ctx *android.Context, extraNinjaDeps []string) string { writeDepFile(cmdlineArgs.OutFile, ctx.EventHandler, ninjaDeps) - if ctx.Config().IsEnvTrue("SOONG_GENERATES_NINJA_HINT") { + if needToWriteNinjaHint(ctx) { writeNinjaHint(ctx) } return cmdlineArgs.OutFile } +func needToWriteNinjaHint(ctx *android.Context) bool { + switch ctx.Config().GetenvWithDefault("SOONG_GENERATES_NINJA_HINT", "") { + case "always": + return true + case "depend": + if _, err := os.Stat(filepath.Join(ctx.Config().OutDir(), ".ninja_log")); errors.Is(err, os.ErrNotExist) { + return true + } + } + return false +} + // Run the code-generation phase to convert BazelTargetModules to BUILD files. func runQueryView(queryviewDir, queryviewMarker string, ctx *android.Context) { ctx.EventHandler.Begin("queryview") @@ -460,7 +473,7 @@ func runSoongOnlyBuild(ctx *android.Context, extraNinjaDeps []string) string { // The actual output (build.ninja) was written in the RunBlueprint() call // above writeDepFile(cmdlineArgs.OutFile, ctx.EventHandler, ninjaDeps) - if ctx.Config().IsEnvTrue("SOONG_GENERATES_NINJA_HINT") { + if needToWriteNinjaHint(ctx) { writeNinjaHint(ctx) } return cmdlineArgs.OutFile diff --git a/ui/build/config.go b/ui/build/config.go index 711774a8a..697cc6611 100644 --- a/ui/build/config.go +++ b/ui/build/config.go @@ -16,6 +16,7 @@ package build import ( "encoding/json" + "errors" "fmt" "io/ioutil" "math/rand" @@ -136,6 +137,9 @@ const ( EXTERNAL_FILE // ninja uses a prioritized module list from Soong HINT_FROM_SOONG + // If ninja log exists, use NINJA_LOG, if not, use HINT_FROM_SOONG instead. + // We can assume it is an incremental build if ninja log exists. + DEFAULT ) const srcDirFileCheck = "build/soong/root.bp" @@ -233,7 +237,7 @@ func NewConfig(ctx Context, args ...string) Config { ret := &configImpl{ environ: OsEnvironment(), sandboxConfig: &SandboxConfig{}, - ninjaWeightListSource: NINJA_LOG, + ninjaWeightListSource: DEFAULT, } // Default matching ninja @@ -244,8 +248,21 @@ func NewConfig(ctx Context, args ...string) Config { ret.parseArgs(ctx, args) if ret.ninjaWeightListSource == HINT_FROM_SOONG { - ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "true") + ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "always") + } else if ret.ninjaWeightListSource == DEFAULT { + defaultNinjaWeightListSource := NINJA_LOG + if _, err := os.Stat(filepath.Join(ret.OutDir(), ninjaLogFileName)); errors.Is(err, os.ErrNotExist) { + ctx.Verboseln("$OUT/.ninja_log doesn't exist, use HINT_FROM_SOONG instead") + defaultNinjaWeightListSource = HINT_FROM_SOONG + } else { + ctx.Verboseln("$OUT/.ninja_log exist, use NINJA_LOG") + } + ret.ninjaWeightListSource = defaultNinjaWeightListSource + // soong_build generates ninja hint depending on ninja log existence. + // Set it "depend" to avoid soong re-run due to env variable change. + ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "depend") } + // Make sure OUT_DIR is set appropriately if outDir, ok := ret.environ.Get("OUT_DIR"); ok { ret.environ.Set("OUT_DIR", filepath.Clean(outDir))