Add a Go replacement for our top-level Make wrapper
Right now this mostly just copies what Make is doing in
build/core/ninja.mk and build/core/soong.mk. The only major feature it
adds is a rotating log file with some verbose logging.
There is one major functional difference -- you cannot override random
Make variables during the Make phase anymore. The environment variable
is set, and if Make uses ?= or the equivalent, it can still use those
variables. We already made this change for Kati, which also loads all of
the same code and actually does the build, so it has been half-removed
for a while.
The only "UI" this implements is what I'll call "Make Emulation" mode --
it's expected that current command lines will continue working, and
we'll explore alternate user interfaces later.
We're still using Make as a wrapper, but all it does is call into this
single Go program, it won't even load the product configuration. Once
this is default, we can start moving individual users over to using this
directly (still in Make emulation mode), skipping the Make wrapper.
Ideas for the future:
* Generating trace files showing time spent in Make/Kati/Soong/Ninja
(also importing ninja traces into the same stream). I had this working
in a previous version of this patch, but removed it to keep the size
down and focus on the current features.
* More intelligent SIGALRM handling, once we fully remove the Make
wrapper (which hides the SIGALRM)
* Reading the experimental binary output stream from Ninja, so that we
can always save the verbose log even if we're not printing it out to
the console
Test: USE_SOONG_UI=true m -j blueprint_tools
Change-Id: I884327b9a8ae24499eb6c56f6e1ad26df1cfa4e4
2016-08-22 00:17:17 +02:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
2023-10-27 23:56:12 +02:00
|
|
|
"android/soong/ui/tracer"
|
2021-10-29 22:11:32 +02:00
|
|
|
"fmt"
|
2023-10-04 06:14:28 +02:00
|
|
|
"io/fs"
|
2017-08-05 01:04:04 +02:00
|
|
|
"os"
|
Add a Go replacement for our top-level Make wrapper
Right now this mostly just copies what Make is doing in
build/core/ninja.mk and build/core/soong.mk. The only major feature it
adds is a rotating log file with some verbose logging.
There is one major functional difference -- you cannot override random
Make variables during the Make phase anymore. The environment variable
is set, and if Make uses ?= or the equivalent, it can still use those
variables. We already made this change for Kati, which also loads all of
the same code and actually does the build, so it has been half-removed
for a while.
The only "UI" this implements is what I'll call "Make Emulation" mode --
it's expected that current command lines will continue working, and
we'll explore alternate user interfaces later.
We're still using Make as a wrapper, but all it does is call into this
single Go program, it won't even load the product configuration. Once
this is default, we can start moving individual users over to using this
directly (still in Make emulation mode), skipping the Make wrapper.
Ideas for the future:
* Generating trace files showing time spent in Make/Kati/Soong/Ninja
(also importing ninja traces into the same stream). I had this working
in a previous version of this patch, but removed it to keep the size
down and focus on the current features.
* More intelligent SIGALRM handling, once we fully remove the Make
wrapper (which hides the SIGALRM)
* Reading the experimental binary output stream from Ninja, so that we
can always save the verbose log even if we're not printing it out to
the console
Test: USE_SOONG_UI=true m -j blueprint_tools
Change-Id: I884327b9a8ae24499eb6c56f6e1ad26df1cfa4e4
2016-08-22 00:17:17 +02:00
|
|
|
"path/filepath"
|
2017-08-05 01:04:04 +02:00
|
|
|
"strconv"
|
2022-08-10 23:39:37 +02:00
|
|
|
"strings"
|
2023-10-04 06:14:28 +02:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2023-10-27 19:54:27 +02:00
|
|
|
"time"
|
2016-08-22 00:17:17 +02:00
|
|
|
|
2023-02-23 23:28:06 +01:00
|
|
|
"android/soong/bazel"
|
2021-05-24 23:24:12 +02:00
|
|
|
"android/soong/ui/metrics"
|
2023-10-27 19:54:27 +02:00
|
|
|
"android/soong/ui/metrics/metrics_proto"
|
2021-05-24 23:24:12 +02:00
|
|
|
"android/soong/ui/status"
|
|
|
|
|
2021-02-25 14:44:14 +01:00
|
|
|
"android/soong/shared"
|
2021-02-26 14:27:36 +01:00
|
|
|
|
2021-03-16 08:55:23 +01:00
|
|
|
"github.com/google/blueprint"
|
|
|
|
"github.com/google/blueprint/bootstrap"
|
2017-08-05 01:04:04 +02:00
|
|
|
"github.com/google/blueprint/microfactory"
|
2024-01-03 02:02:52 +01:00
|
|
|
"github.com/google/blueprint/pathtools"
|
2023-10-27 19:54:27 +02:00
|
|
|
|
|
|
|
"google.golang.org/protobuf/proto"
|
2017-08-05 01:04:04 +02:00
|
|
|
)
|
Add a Go replacement for our top-level Make wrapper
Right now this mostly just copies what Make is doing in
build/core/ninja.mk and build/core/soong.mk. The only major feature it
adds is a rotating log file with some verbose logging.
There is one major functional difference -- you cannot override random
Make variables during the Make phase anymore. The environment variable
is set, and if Make uses ?= or the equivalent, it can still use those
variables. We already made this change for Kati, which also loads all of
the same code and actually does the build, so it has been half-removed
for a while.
The only "UI" this implements is what I'll call "Make Emulation" mode --
it's expected that current command lines will continue working, and
we'll explore alternate user interfaces later.
We're still using Make as a wrapper, but all it does is call into this
single Go program, it won't even load the product configuration. Once
this is default, we can start moving individual users over to using this
directly (still in Make emulation mode), skipping the Make wrapper.
Ideas for the future:
* Generating trace files showing time spent in Make/Kati/Soong/Ninja
(also importing ninja traces into the same stream). I had this working
in a previous version of this patch, but removed it to keep the size
down and focus on the current features.
* More intelligent SIGALRM handling, once we fully remove the Make
wrapper (which hides the SIGALRM)
* Reading the experimental binary output stream from Ninja, so that we
can always save the verbose log even if we're not printing it out to
the console
Test: USE_SOONG_UI=true m -j blueprint_tools
Change-Id: I884327b9a8ae24499eb6c56f6e1ad26df1cfa4e4
2016-08-22 00:17:17 +02:00
|
|
|
|
2021-04-14 10:31:00 +02:00
|
|
|
const (
|
|
|
|
availableEnvFile = "soong.environment.available"
|
|
|
|
usedEnvFile = "soong.environment.used"
|
2021-09-07 09:10:33 +02:00
|
|
|
|
2023-12-07 19:31:24 +01:00
|
|
|
soongBuildTag = "build"
|
|
|
|
jsonModuleGraphTag = "modulegraph"
|
|
|
|
queryviewTag = "queryview"
|
|
|
|
soongDocsTag = "soong_docs"
|
2021-10-29 22:11:32 +02:00
|
|
|
|
|
|
|
// bootstrapEpoch is used to determine if an incremental build is incompatible with the current
|
|
|
|
// version of bootstrap and needs cleaning before continuing the build. Increment this for
|
|
|
|
// incompatible changes, for example when moving the location of the bpglob binary that is
|
|
|
|
// executed during bootstrap before the primary builder has had a chance to update the path.
|
2021-11-04 11:47:42 +01:00
|
|
|
bootstrapEpoch = 1
|
2021-04-14 10:31:00 +02:00
|
|
|
)
|
|
|
|
|
2023-10-04 06:14:28 +02:00
|
|
|
var (
|
|
|
|
// Used during parallel update of symlinks in out directory to reflect new
|
|
|
|
// TOP dir.
|
|
|
|
symlinkWg sync.WaitGroup
|
|
|
|
numFound, numUpdated uint32
|
|
|
|
)
|
|
|
|
|
2022-11-29 02:02:40 +01:00
|
|
|
func writeEnvironmentFile(_ Context, envFile string, envDeps map[string]string) error {
|
2021-02-26 14:27:36 +01:00
|
|
|
data, err := shared.EnvFileContents(envDeps)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-29 02:02:40 +01:00
|
|
|
return os.WriteFile(envFile, data, 0644)
|
2021-02-26 14:27:36 +01:00
|
|
|
}
|
|
|
|
|
2020-11-25 11:19:29 +01:00
|
|
|
// This uses Android.bp files and various tools to generate <builddir>/build.ninja.
|
|
|
|
//
|
2021-03-16 08:55:23 +01:00
|
|
|
// However, the execution of <builddir>/build.ninja happens later in
|
|
|
|
// build/soong/ui/build/build.go#Build()
|
2020-11-25 11:19:29 +01:00
|
|
|
//
|
2021-03-16 08:55:23 +01:00
|
|
|
// We want to rely on as few prebuilts as possible, so we need to bootstrap
|
|
|
|
// Soong. The process is as follows:
|
2020-11-25 11:19:29 +01:00
|
|
|
//
|
2021-03-16 08:55:23 +01:00
|
|
|
// 1. We use "Microfactory", a simple tool to compile Go code, to build
|
|
|
|
// first itself, then soong_ui from soong_ui.bash. This binary contains
|
|
|
|
// parts of soong_build that are needed to build itself.
|
|
|
|
// 2. This simplified version of soong_build then reads the Blueprint files
|
|
|
|
// that describe itself and emits .bootstrap/build.ninja that describes
|
|
|
|
// how to build its full version and use that to produce the final Ninja
|
|
|
|
// file Soong emits.
|
|
|
|
// 3. soong_ui executes .bootstrap/build.ninja
|
2020-11-25 11:19:29 +01:00
|
|
|
//
|
2021-03-16 08:55:23 +01:00
|
|
|
// (After this, Kati is executed to parse the Makefiles, but that's not part of
|
|
|
|
// bootstrapping Soong)
|
|
|
|
|
|
|
|
// A tiny struct used to tell Blueprint that it's in bootstrap mode. It would
|
|
|
|
// probably be nicer to use a flag in bootstrap.Args instead.
|
|
|
|
type BlueprintConfig struct {
|
2021-09-02 09:58:09 +02:00
|
|
|
toolDir string
|
|
|
|
soongOutDir string
|
|
|
|
outDir string
|
|
|
|
runGoTests bool
|
|
|
|
debugCompilation bool
|
|
|
|
subninjas []string
|
|
|
|
primaryBuilderInvocations []bootstrap.PrimaryBuilderInvocation
|
2021-03-16 08:55:23 +01:00
|
|
|
}
|
|
|
|
|
2021-09-01 08:57:48 +02:00
|
|
|
func (c BlueprintConfig) HostToolDir() string {
|
|
|
|
return c.toolDir
|
|
|
|
}
|
|
|
|
|
2021-08-26 15:07:24 +02:00
|
|
|
func (c BlueprintConfig) SoongOutDir() string {
|
|
|
|
return c.soongOutDir
|
2021-03-16 08:55:23 +01:00
|
|
|
}
|
|
|
|
|
2021-08-26 15:07:24 +02:00
|
|
|
func (c BlueprintConfig) OutDir() string {
|
|
|
|
return c.outDir
|
2021-03-16 08:55:23 +01:00
|
|
|
}
|
|
|
|
|
2021-09-02 09:58:09 +02:00
|
|
|
func (c BlueprintConfig) RunGoTests() bool {
|
|
|
|
return c.runGoTests
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:03:14 +01:00
|
|
|
func (c BlueprintConfig) DebugCompilation() bool {
|
|
|
|
return c.debugCompilation
|
|
|
|
}
|
|
|
|
|
2021-09-02 09:58:09 +02:00
|
|
|
func (c BlueprintConfig) Subninjas() []string {
|
|
|
|
return c.subninjas
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c BlueprintConfig) PrimaryBuilderInvocations() []bootstrap.PrimaryBuilderInvocation {
|
|
|
|
return c.primaryBuilderInvocations
|
|
|
|
}
|
|
|
|
|
2021-09-08 15:31:14 +02:00
|
|
|
func environmentArgs(config Config, tag string) []string {
|
2021-04-14 10:31:00 +02:00
|
|
|
return []string{
|
|
|
|
"--available_env", shared.JoinPath(config.SoongOutDir(), availableEnvFile),
|
2021-09-08 15:31:14 +02:00
|
|
|
"--used_env", config.UsedEnvFile(tag),
|
2021-04-14 10:31:00 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-11 18:48:06 +02:00
|
|
|
|
2021-10-29 22:11:32 +02:00
|
|
|
func writeEmptyFile(ctx Context, path string) {
|
2021-06-11 18:48:06 +02:00
|
|
|
err := os.MkdirAll(filepath.Dir(path), 0777)
|
|
|
|
if err != nil {
|
2021-10-29 22:11:32 +02:00
|
|
|
ctx.Fatalf("Failed to create parent directories of empty file '%s': %s", path, err)
|
2021-06-11 18:48:06 +02:00
|
|
|
}
|
|
|
|
|
2021-10-29 22:11:32 +02:00
|
|
|
if exists, err := fileExists(path); err != nil {
|
|
|
|
ctx.Fatalf("Failed to check if file '%s' exists: %s", path, err)
|
|
|
|
} else if !exists {
|
2022-11-29 02:02:40 +01:00
|
|
|
err = os.WriteFile(path, nil, 0666)
|
2021-06-11 18:48:06 +02:00
|
|
|
if err != nil {
|
2021-10-29 22:11:32 +02:00
|
|
|
ctx.Fatalf("Failed to create empty file '%s': %s", path, err)
|
2021-06-11 18:48:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 22:11:32 +02:00
|
|
|
func fileExists(path string) (bool, error) {
|
|
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
} else if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-11-29 02:02:40 +01:00
|
|
|
type PrimaryBuilderFactory struct {
|
|
|
|
name string
|
|
|
|
description string
|
|
|
|
config Config
|
|
|
|
output string
|
|
|
|
specificArgs []string
|
|
|
|
debugPort string
|
|
|
|
}
|
|
|
|
|
2023-08-03 18:46:32 +02:00
|
|
|
func getGlobPathName(config Config) string {
|
|
|
|
globPathName, ok := config.TargetProductOrErr()
|
|
|
|
if ok != nil {
|
|
|
|
globPathName = soongBuildTag
|
|
|
|
}
|
|
|
|
return globPathName
|
|
|
|
}
|
|
|
|
|
2024-01-03 02:02:52 +01:00
|
|
|
func getGlobPathNameFromPrimaryBuilderFactory(config Config, pb PrimaryBuilderFactory) string {
|
|
|
|
if pb.name == soongBuildTag {
|
|
|
|
// Glob path for soong build would be separated per product target
|
|
|
|
return getGlobPathName(config)
|
|
|
|
}
|
|
|
|
return pb.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pb PrimaryBuilderFactory) primaryBuilderInvocation(config Config) bootstrap.PrimaryBuilderInvocation {
|
2021-09-07 09:10:33 +02:00
|
|
|
commonArgs := make([]string, 0, 0)
|
2021-08-12 14:03:55 +02:00
|
|
|
|
2022-11-29 02:02:40 +01:00
|
|
|
if !pb.config.skipSoongTests {
|
2021-09-07 09:10:33 +02:00
|
|
|
commonArgs = append(commonArgs, "-t")
|
2021-04-14 10:31:00 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 21:02:07 +02:00
|
|
|
if pb.config.buildFromSourceStub {
|
|
|
|
commonArgs = append(commonArgs, "--build-from-source-stub")
|
2023-01-17 21:40:22 +01:00
|
|
|
}
|
2023-03-09 19:19:35 +01:00
|
|
|
|
2024-02-02 23:52:05 +01:00
|
|
|
if pb.config.moduleDebugFile != "" {
|
|
|
|
commonArgs = append(commonArgs, "--soong_module_debug")
|
|
|
|
commonArgs = append(commonArgs, pb.config.moduleDebugFile)
|
|
|
|
}
|
|
|
|
|
2022-11-29 02:02:40 +01:00
|
|
|
commonArgs = append(commonArgs, "-l", filepath.Join(pb.config.FileListDir(), "Android.bp.list"))
|
2022-01-05 10:29:56 +01:00
|
|
|
invocationEnv := make(map[string]string)
|
2022-11-29 02:02:40 +01:00
|
|
|
if pb.debugPort != "" {
|
2022-08-20 01:26:00 +02:00
|
|
|
//debug mode
|
2022-11-29 02:02:40 +01:00
|
|
|
commonArgs = append(commonArgs, "--delve_listen", pb.debugPort,
|
|
|
|
"--delve_path", shared.ResolveDelveBinary())
|
2022-01-05 10:29:56 +01:00
|
|
|
// GODEBUG=asyncpreemptoff=1 disables the preemption of goroutines. This
|
|
|
|
// is useful because the preemption happens by sending SIGURG to the OS
|
|
|
|
// thread hosting the goroutine in question and each signal results in
|
|
|
|
// work that needs to be done by Delve; it uses ptrace to debug the Go
|
|
|
|
// process and the tracer process must deal with every signal (it is not
|
|
|
|
// possible to selectively ignore SIGURG). This makes debugging slower,
|
|
|
|
// sometimes by an order of magnitude depending on luck.
|
|
|
|
// The original reason for adding async preemption to Go is here:
|
|
|
|
// https://github.com/golang/proposal/blob/master/design/24543-non-cooperative-preemption.md
|
|
|
|
invocationEnv["GODEBUG"] = "asyncpreemptoff=1"
|
2021-04-14 10:31:00 +02:00
|
|
|
}
|
|
|
|
|
2022-08-20 01:26:00 +02:00
|
|
|
var allArgs []string
|
2022-11-29 02:02:40 +01:00
|
|
|
allArgs = append(allArgs, pb.specificArgs...)
|
2024-01-03 02:02:52 +01:00
|
|
|
globPathName := getGlobPathNameFromPrimaryBuilderFactory(config, pb)
|
2021-09-07 09:10:33 +02:00
|
|
|
allArgs = append(allArgs,
|
2023-08-03 18:46:32 +02:00
|
|
|
"--globListDir", globPathName,
|
|
|
|
"--globFile", pb.config.NamedGlobFile(globPathName))
|
2021-04-14 10:31:00 +02:00
|
|
|
|
2021-09-07 09:10:33 +02:00
|
|
|
allArgs = append(allArgs, commonArgs...)
|
2022-11-29 02:02:40 +01:00
|
|
|
allArgs = append(allArgs, environmentArgs(pb.config, pb.name)...)
|
2022-11-19 00:32:49 +01:00
|
|
|
if profileCpu := os.Getenv("SOONG_PROFILE_CPU"); profileCpu != "" {
|
2022-11-29 02:02:40 +01:00
|
|
|
allArgs = append(allArgs, "--cpuprofile", profileCpu+"."+pb.name)
|
2022-11-19 00:32:49 +01:00
|
|
|
}
|
|
|
|
if profileMem := os.Getenv("SOONG_PROFILE_MEM"); profileMem != "" {
|
2022-11-29 02:02:40 +01:00
|
|
|
allArgs = append(allArgs, "--memprofile", profileMem+"."+pb.name)
|
2022-11-19 00:32:49 +01:00
|
|
|
}
|
2021-09-07 09:10:33 +02:00
|
|
|
allArgs = append(allArgs, "Android.bp")
|
2021-08-12 14:03:55 +02:00
|
|
|
|
2024-01-03 02:02:52 +01:00
|
|
|
globfiles := bootstrap.GlobFileListFiles(bootstrap.GlobDirectory(config.SoongOutDir(), globPathName))
|
|
|
|
|
2021-09-07 09:10:33 +02:00
|
|
|
return bootstrap.PrimaryBuilderInvocation{
|
2021-12-06 14:27:43 +01:00
|
|
|
Inputs: []string{"Android.bp"},
|
2024-01-03 02:02:52 +01:00
|
|
|
Implicits: globfiles,
|
2022-11-29 02:02:40 +01:00
|
|
|
Outputs: []string{pb.output},
|
2021-12-06 14:27:43 +01:00
|
|
|
Args: allArgs,
|
2022-11-29 02:02:40 +01:00
|
|
|
Description: pb.description,
|
2022-01-04 14:40:13 +01:00
|
|
|
// NB: Changing the value of this environment variable will not result in a
|
|
|
|
// rebuild. The bootstrap Ninja file will change, but apparently Ninja does
|
|
|
|
// not consider changing the pool specified in a statement a change that's
|
|
|
|
// worth rebuilding for.
|
|
|
|
Console: os.Getenv("SOONG_UNBUFFERED_OUTPUT") == "1",
|
2022-01-05 10:29:56 +01:00
|
|
|
Env: invocationEnv,
|
2021-08-12 14:03:55 +02:00
|
|
|
}
|
2021-09-07 09:10:33 +02:00
|
|
|
}
|
2021-08-25 14:14:13 +02:00
|
|
|
|
2021-10-29 22:11:32 +02:00
|
|
|
// bootstrapEpochCleanup deletes files used by bootstrap during incremental builds across
|
|
|
|
// incompatible changes. Incompatible changes are marked by incrementing the bootstrapEpoch
|
|
|
|
// constant. A tree is considered out of date for the current epoch of the
|
|
|
|
// .soong.bootstrap.epoch.<epoch> file doesn't exist.
|
|
|
|
func bootstrapEpochCleanup(ctx Context, config Config) {
|
|
|
|
epochFile := fmt.Sprintf(".soong.bootstrap.epoch.%d", bootstrapEpoch)
|
|
|
|
epochPath := filepath.Join(config.SoongOutDir(), epochFile)
|
|
|
|
if exists, err := fileExists(epochPath); err != nil {
|
|
|
|
ctx.Fatalf("failed to check if bootstrap epoch file %q exists: %q", epochPath, err)
|
|
|
|
} else if !exists {
|
|
|
|
// The tree is out of date for the current epoch, delete files used by bootstrap
|
|
|
|
// and force the primary builder to rerun.
|
2023-04-19 06:13:45 +02:00
|
|
|
os.Remove(config.SoongNinjaFile())
|
2021-10-29 22:11:32 +02:00
|
|
|
for _, globFile := range bootstrapGlobFileList(config) {
|
|
|
|
os.Remove(globFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the tree as up to date with the current epoch by writing the epoch marker file.
|
|
|
|
writeEmptyFile(ctx, epochPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func bootstrapGlobFileList(config Config) []string {
|
|
|
|
return []string{
|
2023-08-03 18:46:32 +02:00
|
|
|
config.NamedGlobFile(getGlobPathName(config)),
|
2021-10-29 22:11:32 +02:00
|
|
|
config.NamedGlobFile(jsonModuleGraphTag),
|
|
|
|
config.NamedGlobFile(queryviewTag),
|
|
|
|
config.NamedGlobFile(soongDocsTag),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 09:10:33 +02:00
|
|
|
func bootstrapBlueprint(ctx Context, config Config) {
|
|
|
|
ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap")
|
|
|
|
defer ctx.EndTrace()
|
2021-09-06 17:08:02 +02:00
|
|
|
|
2021-10-29 22:11:32 +02:00
|
|
|
// Clean up some files for incremental builds across incompatible changes.
|
|
|
|
bootstrapEpochCleanup(ctx, config)
|
|
|
|
|
2023-04-19 06:13:45 +02:00
|
|
|
baseArgs := []string{"--soong_variables", config.SoongVarsFile()}
|
|
|
|
|
|
|
|
mainSoongBuildExtraArgs := append(baseArgs, "-o", config.SoongNinjaFile())
|
2021-09-07 09:10:33 +02:00
|
|
|
if config.EmptyNinjaFile() {
|
|
|
|
mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--empty-ninja-file")
|
2021-09-06 18:31:46 +02:00
|
|
|
}
|
2023-06-08 21:02:07 +02:00
|
|
|
if config.buildFromSourceStub {
|
|
|
|
mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--build-from-source-stub")
|
2023-01-17 21:40:22 +01:00
|
|
|
}
|
2023-04-19 18:47:36 +02:00
|
|
|
if config.ensureAllowlistIntegrity {
|
|
|
|
mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--ensure-allowlist-integrity")
|
|
|
|
}
|
2022-11-29 01:47:59 +01:00
|
|
|
|
2022-11-29 02:02:40 +01:00
|
|
|
queryviewDir := filepath.Join(config.SoongOutDir(), "queryview")
|
2021-09-06 18:31:46 +02:00
|
|
|
|
2022-11-29 02:02:40 +01:00
|
|
|
pbfs := []PrimaryBuilderFactory{
|
|
|
|
{
|
|
|
|
name: soongBuildTag,
|
|
|
|
description: fmt.Sprintf("analyzing Android.bp files and generating ninja file at %s", config.SoongNinjaFile()),
|
|
|
|
config: config,
|
|
|
|
output: config.SoongNinjaFile(),
|
|
|
|
specificArgs: mainSoongBuildExtraArgs,
|
2021-12-06 14:27:43 +01:00
|
|
|
},
|
2022-11-29 02:02:40 +01:00
|
|
|
{
|
|
|
|
name: jsonModuleGraphTag,
|
|
|
|
description: fmt.Sprintf("generating the Soong module graph at %s", config.ModuleGraphFile()),
|
|
|
|
config: config,
|
|
|
|
output: config.ModuleGraphFile(),
|
2023-04-19 06:13:45 +02:00
|
|
|
specificArgs: append(baseArgs,
|
2022-11-29 02:02:40 +01:00
|
|
|
"--module_graph_file", config.ModuleGraphFile(),
|
|
|
|
"--module_actions_file", config.ModuleActionsFile(),
|
2023-04-19 06:13:45 +02:00
|
|
|
),
|
2021-12-06 14:27:43 +01:00
|
|
|
},
|
2022-11-29 02:02:40 +01:00
|
|
|
{
|
2023-04-19 06:13:45 +02:00
|
|
|
name: queryviewTag,
|
|
|
|
description: fmt.Sprintf("generating the Soong module graph as a Bazel workspace at %s", queryviewDir),
|
|
|
|
config: config,
|
|
|
|
output: config.QueryviewMarkerFile(),
|
|
|
|
specificArgs: append(baseArgs,
|
|
|
|
"--bazel_queryview_dir", queryviewDir,
|
|
|
|
),
|
2022-11-29 02:02:40 +01:00
|
|
|
},
|
|
|
|
{
|
2023-04-19 06:13:45 +02:00
|
|
|
name: soongDocsTag,
|
|
|
|
description: fmt.Sprintf("generating Soong docs at %s", config.SoongDocsHtml()),
|
|
|
|
config: config,
|
|
|
|
output: config.SoongDocsHtml(),
|
|
|
|
specificArgs: append(baseArgs,
|
|
|
|
"--soong_docs", config.SoongDocsHtml(),
|
|
|
|
),
|
2021-12-06 14:27:43 +01:00
|
|
|
},
|
2022-11-29 02:02:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out which invocations will be run under the debugger:
|
|
|
|
// * SOONG_DELVE if set specifies listening port
|
|
|
|
// * SOONG_DELVE_STEPS if set specifies specific invocations to be debugged, otherwise all are
|
|
|
|
debuggedInvocations := make(map[string]bool)
|
|
|
|
delvePort := os.Getenv("SOONG_DELVE")
|
|
|
|
if delvePort != "" {
|
|
|
|
if steps := os.Getenv("SOONG_DELVE_STEPS"); steps != "" {
|
|
|
|
var validSteps []string
|
|
|
|
for _, pbf := range pbfs {
|
|
|
|
debuggedInvocations[pbf.name] = false
|
|
|
|
validSteps = append(validSteps, pbf.name)
|
|
|
|
|
|
|
|
}
|
|
|
|
for _, step := range strings.Split(steps, ",") {
|
|
|
|
if _, ok := debuggedInvocations[step]; ok {
|
|
|
|
debuggedInvocations[step] = true
|
|
|
|
} else {
|
|
|
|
ctx.Fatalf("SOONG_DELVE_STEPS contains unknown soong_build step %s\n"+
|
|
|
|
"Valid steps are %v", step, validSteps)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// SOONG_DELVE_STEPS is not set, run all steps in the debugger
|
|
|
|
for _, pbf := range pbfs {
|
|
|
|
debuggedInvocations[pbf.name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var invocations []bootstrap.PrimaryBuilderInvocation
|
|
|
|
for _, pbf := range pbfs {
|
|
|
|
if debuggedInvocations[pbf.name] {
|
|
|
|
pbf.debugPort = delvePort
|
|
|
|
}
|
2024-01-03 02:02:52 +01:00
|
|
|
pbi := pbf.primaryBuilderInvocation(config)
|
2022-11-29 02:02:40 +01:00
|
|
|
invocations = append(invocations, pbi)
|
|
|
|
}
|
2021-09-07 09:10:33 +02:00
|
|
|
|
2022-11-29 02:02:40 +01:00
|
|
|
blueprintArgs := bootstrap.Args{
|
|
|
|
ModuleListFile: filepath.Join(config.FileListDir(), "Android.bp.list"),
|
|
|
|
OutFile: shared.JoinPath(config.SoongOutDir(), "bootstrap.ninja"),
|
|
|
|
EmptyNinjaFile: false,
|
|
|
|
}
|
2021-04-14 10:31:00 +02:00
|
|
|
|
2021-03-16 08:55:23 +01:00
|
|
|
blueprintCtx := blueprint.NewContext()
|
2022-11-08 19:42:16 +01:00
|
|
|
blueprintCtx.AddIncludeTags(config.GetIncludeTags()...)
|
2023-02-21 17:50:29 +01:00
|
|
|
blueprintCtx.AddSourceRootDirs(config.GetSourceRootDirs()...)
|
2021-03-16 08:55:23 +01:00
|
|
|
blueprintCtx.SetIgnoreUnknownModuleTypes(true)
|
|
|
|
blueprintConfig := BlueprintConfig{
|
2021-09-08 15:31:14 +02:00
|
|
|
soongOutDir: config.SoongOutDir(),
|
|
|
|
toolDir: config.HostToolDir(),
|
|
|
|
outDir: config.OutDir(),
|
|
|
|
runGoTests: !config.skipSoongTests,
|
2021-09-07 09:10:33 +02:00
|
|
|
// If we want to debug soong_build, we need to compile it for debugging
|
2022-11-29 02:02:40 +01:00
|
|
|
debugCompilation: delvePort != "",
|
|
|
|
subninjas: bootstrapGlobFileList(config),
|
|
|
|
primaryBuilderInvocations: invocations,
|
2021-03-16 08:55:23 +01:00
|
|
|
}
|
|
|
|
|
2024-01-03 02:02:52 +01:00
|
|
|
// The glob ninja files are generated during the main build phase. However, the
|
|
|
|
// primary buildifer invocation depends on all of its glob files, even before
|
|
|
|
// it's been run. Generate a "empty" glob ninja file on the first run,
|
|
|
|
// so that the files can be there to satisfy the dependency.
|
|
|
|
for _, pb := range pbfs {
|
|
|
|
globPathName := getGlobPathNameFromPrimaryBuilderFactory(config, pb)
|
|
|
|
globNinjaFile := config.NamedGlobFile(globPathName)
|
|
|
|
if _, err := os.Stat(globNinjaFile); os.IsNotExist(err) {
|
|
|
|
err := bootstrap.WriteBuildGlobsNinjaFile(&bootstrap.GlobSingleton{
|
|
|
|
GlobLister: func() pathtools.MultipleGlobResults { return nil },
|
|
|
|
GlobFile: globNinjaFile,
|
|
|
|
GlobDir: bootstrap.GlobDirectory(config.SoongOutDir(), globPathName),
|
|
|
|
SrcDir: ".",
|
|
|
|
}, blueprintConfig)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Fatal(err)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
ctx.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 18:53:46 +02:00
|
|
|
// since `bootstrap.ninja` is regenerated unconditionally, we ignore the deps, i.e. little
|
|
|
|
// reason to write a `bootstrap.ninja.d` file
|
2023-06-20 11:30:06 +02:00
|
|
|
_, err := bootstrap.RunBlueprint(blueprintArgs, bootstrap.DoEverything, blueprintCtx, blueprintConfig)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Fatal(err)
|
|
|
|
}
|
2021-03-16 08:55:23 +01:00
|
|
|
}
|
|
|
|
|
2023-05-31 01:45:36 +02:00
|
|
|
func checkEnvironmentFile(ctx Context, currentEnv *Environment, envFile string) {
|
2021-04-14 10:31:00 +02:00
|
|
|
getenv := func(k string) string {
|
|
|
|
v, _ := currentEnv.Get(k)
|
|
|
|
return v
|
|
|
|
}
|
2021-09-08 15:31:14 +02:00
|
|
|
|
2023-05-31 01:45:36 +02:00
|
|
|
// Log the changed environment variables to ChangedEnvironmentVariable field
|
|
|
|
if stale, changedEnvironmentVariableList, _ := shared.StaleEnvFile(envFile, getenv); stale {
|
|
|
|
for _, changedEnvironmentVariable := range changedEnvironmentVariableList {
|
|
|
|
ctx.Metrics.AddChangedEnvironmentVariable(changedEnvironmentVariable)
|
|
|
|
}
|
2021-04-14 10:31:00 +02:00
|
|
|
os.Remove(envFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 06:14:28 +02:00
|
|
|
func updateSymlinks(ctx Context, dir, prevCWD, cwd string) error {
|
|
|
|
defer symlinkWg.Done()
|
|
|
|
|
|
|
|
visit := func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if d.IsDir() && path != dir {
|
|
|
|
symlinkWg.Add(1)
|
|
|
|
go updateSymlinks(ctx, path, prevCWD, cwd)
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
f, err := d.Info()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// If the file is not a symlink, we don't have to update it.
|
|
|
|
if f.Mode()&os.ModeSymlink != os.ModeSymlink {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.AddUint32(&numFound, 1)
|
|
|
|
target, err := os.Readlink(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(target, prevCWD) &&
|
|
|
|
(len(target) == len(prevCWD) || target[len(prevCWD)] == '/') {
|
|
|
|
target = filepath.Join(cwd, target[len(prevCWD):])
|
|
|
|
if err := os.Remove(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.Symlink(target, path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
atomic.AddUint32(&numUpdated, 1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := filepath.WalkDir(dir, visit); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fixOutDirSymlinks(ctx Context, config Config, outDir string) error {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record the .top as the very last thing in the function.
|
|
|
|
tf := filepath.Join(outDir, ".top")
|
|
|
|
defer func() {
|
|
|
|
if err := os.WriteFile(tf, []byte(cwd), 0644); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, fmt.Sprintf("Unable to log CWD: %v", err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Find the previous working directory if it was recorded.
|
|
|
|
var prevCWD string
|
|
|
|
pcwd, err := os.ReadFile(tf)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// No previous working directory recorded, nothing to do.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
prevCWD = strings.Trim(string(pcwd), "\n")
|
|
|
|
|
|
|
|
if prevCWD == cwd {
|
|
|
|
// We are in the same source dir, nothing to update.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
symlinkWg.Add(1)
|
|
|
|
if err := updateSymlinks(ctx, outDir, prevCWD, cwd); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
symlinkWg.Wait()
|
|
|
|
ctx.Println(fmt.Sprintf("Updated %d/%d symlinks in dir %v", numUpdated, numFound, outDir))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrateOutputSymlinks(ctx Context, config Config) error {
|
|
|
|
// Figure out the real out directory ("out" could be a symlink).
|
|
|
|
outDir := config.OutDir()
|
|
|
|
s, err := os.Lstat(outDir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// No out dir exists, no symlinks to migrate.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if s.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
target, err := filepath.EvalSymlinks(outDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
outDir = target
|
|
|
|
}
|
|
|
|
return fixOutDirSymlinks(ctx, config, outDir)
|
|
|
|
}
|
|
|
|
|
Add a Go replacement for our top-level Make wrapper
Right now this mostly just copies what Make is doing in
build/core/ninja.mk and build/core/soong.mk. The only major feature it
adds is a rotating log file with some verbose logging.
There is one major functional difference -- you cannot override random
Make variables during the Make phase anymore. The environment variable
is set, and if Make uses ?= or the equivalent, it can still use those
variables. We already made this change for Kati, which also loads all of
the same code and actually does the build, so it has been half-removed
for a while.
The only "UI" this implements is what I'll call "Make Emulation" mode --
it's expected that current command lines will continue working, and
we'll explore alternate user interfaces later.
We're still using Make as a wrapper, but all it does is call into this
single Go program, it won't even load the product configuration. Once
this is default, we can start moving individual users over to using this
directly (still in Make emulation mode), skipping the Make wrapper.
Ideas for the future:
* Generating trace files showing time spent in Make/Kati/Soong/Ninja
(also importing ninja traces into the same stream). I had this working
in a previous version of this patch, but removed it to keep the size
down and focus on the current features.
* More intelligent SIGALRM handling, once we fully remove the Make
wrapper (which hides the SIGALRM)
* Reading the experimental binary output stream from Ninja, so that we
can always save the verbose log even if we're not printing it out to
the console
Test: USE_SOONG_UI=true m -j blueprint_tools
Change-Id: I884327b9a8ae24499eb6c56f6e1ad26df1cfa4e4
2016-08-22 00:17:17 +02:00
|
|
|
func runSoong(ctx Context, config Config) {
|
2018-12-13 01:01:49 +01:00
|
|
|
ctx.BeginTrace(metrics.RunSoong, "soong")
|
2016-08-22 00:17:17 +02:00
|
|
|
defer ctx.EndTrace()
|
|
|
|
|
2023-10-04 06:14:28 +02:00
|
|
|
if err := migrateOutputSymlinks(ctx, config); err != nil {
|
|
|
|
ctx.Fatalf("failed to migrate output directory to current TOP dir: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-26 14:27:36 +01:00
|
|
|
// We have two environment files: .available is the one with every variable,
|
|
|
|
// .used with the ones that were actually used. The latter is used to
|
|
|
|
// determine whether Soong needs to be re-run since why re-run it if only
|
|
|
|
// unused variables were changed?
|
2021-04-14 10:31:00 +02:00
|
|
|
envFile := filepath.Join(config.SoongOutDir(), availableEnvFile)
|
2021-02-26 14:27:36 +01:00
|
|
|
|
2021-03-16 08:55:23 +01:00
|
|
|
// This is done unconditionally, but does not take a measurable amount of time
|
2021-08-12 14:03:55 +02:00
|
|
|
bootstrapBlueprint(ctx, config)
|
2017-08-05 01:04:04 +02:00
|
|
|
|
2021-02-26 14:27:36 +01:00
|
|
|
soongBuildEnv := config.Environment().Copy()
|
|
|
|
soongBuildEnv.Set("TOP", os.Getenv("TOP"))
|
2021-11-10 15:55:20 +01:00
|
|
|
soongBuildEnv.Set("LOG_DIR", config.LogsDir())
|
2021-02-26 14:27:36 +01:00
|
|
|
|
2021-03-16 08:55:23 +01:00
|
|
|
// For Soong bootstrapping tests
|
|
|
|
if os.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" {
|
|
|
|
soongBuildEnv.Set("ALLOW_MISSING_DEPENDENCIES", "true")
|
|
|
|
}
|
|
|
|
|
2021-03-05 13:26:14 +01:00
|
|
|
err := writeEnvironmentFile(ctx, envFile, soongBuildEnv.AsMap())
|
|
|
|
if err != nil {
|
|
|
|
ctx.Fatalf("failed to write environment file %s: %s", envFile, err)
|
|
|
|
}
|
2021-02-26 14:27:36 +01:00
|
|
|
|
2017-08-05 01:04:04 +02:00
|
|
|
func() {
|
2018-12-13 01:01:49 +01:00
|
|
|
ctx.BeginTrace(metrics.RunSoong, "environment check")
|
2017-08-05 01:04:04 +02:00
|
|
|
defer ctx.EndTrace()
|
|
|
|
|
2023-05-31 01:45:36 +02:00
|
|
|
checkEnvironmentFile(ctx, soongBuildEnv, config.UsedEnvFile(soongBuildTag))
|
2021-09-07 09:10:33 +02:00
|
|
|
|
2023-12-07 19:31:24 +01:00
|
|
|
// Remove bazel files in the event that bazel is disabled for the build.
|
|
|
|
// These files may have been left over from a previous bazel-enabled build.
|
|
|
|
cleanBazelFiles(config)
|
2021-09-07 09:10:33 +02:00
|
|
|
|
|
|
|
if config.JsonModuleGraph() {
|
2023-05-31 01:45:36 +02:00
|
|
|
checkEnvironmentFile(ctx, soongBuildEnv, config.UsedEnvFile(jsonModuleGraphTag))
|
2021-09-07 09:10:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Queryview() {
|
2023-05-31 01:45:36 +02:00
|
|
|
checkEnvironmentFile(ctx, soongBuildEnv, config.UsedEnvFile(queryviewTag))
|
2021-09-07 09:10:33 +02:00
|
|
|
}
|
2021-04-14 10:31:00 +02:00
|
|
|
|
2021-09-07 09:10:33 +02:00
|
|
|
if config.SoongDocs() {
|
2023-05-31 01:45:36 +02:00
|
|
|
checkEnvironmentFile(ctx, soongBuildEnv, config.UsedEnvFile(soongDocsTag))
|
2017-08-05 01:04:04 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-10-29 22:11:32 +02:00
|
|
|
runMicrofactory(ctx, config, "bpglob", "github.com/google/blueprint/bootstrap/bpglob",
|
2021-04-09 21:03:51 +02:00
|
|
|
map[string]string{"github.com/google/blueprint": "build/blueprint"})
|
2018-07-06 06:46:51 +02:00
|
|
|
|
2023-05-22 22:33:27 +02:00
|
|
|
ninja := func(targets ...string) {
|
|
|
|
ctx.BeginTrace(metrics.RunSoong, "bootstrap")
|
2017-08-05 01:04:04 +02:00
|
|
|
defer ctx.EndTrace()
|
|
|
|
|
Add a unified status reporting UI
This adds a new status package that merges the running of "actions"
(ninja calls them edges) of multiple tools into one view of the current
state, and gives that to a number of different outputs.
For inputs:
Kati's output parser has been rewritten (and moved) to map onto the
StartAction/FinishAction API. A byproduct of this is that the build
servers should be able to extract errors from Kati better, since they
look like the errors that Ninja used to write.
Ninja is no longer directly connected to the terminal, but its output is
read via the protobuf frontend API, so it's just another tool whose
output becomes merged together.
multiproduct_kati loses its custom status routines, and uses the common
one instead.
For outputs:
The primary output is the ui/terminal.Status type, which along with
ui/terminal.Writer now controls everything about the terminal output.
Today, this doesn't really change any behaviors, but having all terminal
output going through here allows a more complicated (multi-line / full
window) status display in the future.
The tracer acts as an output of the status package, tracing all the
action start / finish events. This replaces reading the .ninja_log file,
so it now properly handles multiple output files from a single action.
A new rotated log file (out/error.log, or out/dist/logs/error.log) just
contains a description of all of the errors that happened during the
current build.
Another new compressed and rotated log file (out/verbose.log.gz, or
out/dist/logs/verbose.log.gz) contains the full verbose (showcommands)
log of every execution run by the build. Since this is now written on
every build, the showcommands argument is now ignored -- if you want to
get the commands run, look at the log file after the build.
Test: m
Test: <built-in tests>
Test: NINJA_ARGS="-t list" m
Test: check the build.trace.gz
Test: check the new log files
Change-Id: If1d8994890d43ef68f65aa10ddd8e6e06dc7013a
2018-05-18 01:37:09 +02:00
|
|
|
fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
|
2019-03-22 00:02:58 +01:00
|
|
|
nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo)
|
|
|
|
defer nr.Close()
|
Add a unified status reporting UI
This adds a new status package that merges the running of "actions"
(ninja calls them edges) of multiple tools into one view of the current
state, and gives that to a number of different outputs.
For inputs:
Kati's output parser has been rewritten (and moved) to map onto the
StartAction/FinishAction API. A byproduct of this is that the build
servers should be able to extract errors from Kati better, since they
look like the errors that Ninja used to write.
Ninja is no longer directly connected to the terminal, but its output is
read via the protobuf frontend API, so it's just another tool whose
output becomes merged together.
multiproduct_kati loses its custom status routines, and uses the common
one instead.
For outputs:
The primary output is the ui/terminal.Status type, which along with
ui/terminal.Writer now controls everything about the terminal output.
Today, this doesn't really change any behaviors, but having all terminal
output going through here allows a more complicated (multi-line / full
window) status display in the future.
The tracer acts as an output of the status package, tracing all the
action start / finish events. This replaces reading the .ninja_log file,
so it now properly handles multiple output files from a single action.
A new rotated log file (out/error.log, or out/dist/logs/error.log) just
contains a description of all of the errors that happened during the
current build.
Another new compressed and rotated log file (out/verbose.log.gz, or
out/dist/logs/verbose.log.gz) contains the full verbose (showcommands)
log of every execution run by the build. Since this is now written on
every build, the showcommands argument is now ignored -- if you want to
get the commands run, look at the log file after the build.
Test: m
Test: <built-in tests>
Test: NINJA_ARGS="-t list" m
Test: check the build.trace.gz
Test: check the new log files
Change-Id: If1d8994890d43ef68f65aa10ddd8e6e06dc7013a
2018-05-18 01:37:09 +02:00
|
|
|
|
2021-08-12 14:03:55 +02:00
|
|
|
ninjaArgs := []string{
|
2017-08-05 01:04:04 +02:00
|
|
|
"-d", "keepdepfile",
|
2020-05-18 23:02:02 +02:00
|
|
|
"-d", "stats",
|
2020-04-19 05:25:59 +02:00
|
|
|
"-o", "usesphonyoutputs=yes",
|
|
|
|
"-o", "preremoveoutputs=yes",
|
2017-08-05 01:04:04 +02:00
|
|
|
"-w", "dupbuild=err",
|
2020-04-19 05:25:59 +02:00
|
|
|
"-w", "outputdir=err",
|
|
|
|
"-w", "missingoutfile=err",
|
2017-08-05 01:04:04 +02:00
|
|
|
"-j", strconv.Itoa(config.Parallel()),
|
2018-07-18 02:54:31 +02:00
|
|
|
"--frontend_file", fifo,
|
2023-05-22 22:33:27 +02:00
|
|
|
"-f", filepath.Join(config.SoongOutDir(), "bootstrap.ninja"),
|
2021-08-12 14:03:55 +02:00
|
|
|
}
|
|
|
|
|
2022-08-10 23:39:37 +02:00
|
|
|
if extra, ok := config.Environment().Get("SOONG_UI_NINJA_ARGS"); ok {
|
|
|
|
ctx.Printf(`CAUTION: arguments in $SOONG_UI_NINJA_ARGS=%q, e.g. "-n", can make soong_build FAIL or INCORRECT`, extra)
|
|
|
|
ninjaArgs = append(ninjaArgs, strings.Fields(extra)...)
|
|
|
|
}
|
|
|
|
|
2021-08-12 14:03:55 +02:00
|
|
|
ninjaArgs = append(ninjaArgs, targets...)
|
2023-05-22 22:33:27 +02:00
|
|
|
cmd := Command(ctx, config, "soong bootstrap",
|
2021-08-12 14:03:55 +02:00
|
|
|
config.PrebuiltBuildTool("ninja"), ninjaArgs...)
|
2020-11-02 08:56:20 +01:00
|
|
|
|
2021-03-09 10:43:57 +01:00
|
|
|
var ninjaEnv Environment
|
2021-03-10 10:48:39 +01:00
|
|
|
|
|
|
|
// This is currently how the command line to invoke soong_build finds the
|
|
|
|
// root of the source tree and the output root
|
2021-03-09 10:43:57 +01:00
|
|
|
ninjaEnv.Set("TOP", os.Getenv("TOP"))
|
2021-03-02 10:09:41 +01:00
|
|
|
|
2021-03-09 10:43:57 +01:00
|
|
|
cmd.Environment = &ninjaEnv
|
2017-08-05 01:04:04 +02:00
|
|
|
cmd.Sandbox = soongSandbox
|
2019-06-19 22:17:59 +02:00
|
|
|
cmd.RunAndStreamOrFatal()
|
Add a Go replacement for our top-level Make wrapper
Right now this mostly just copies what Make is doing in
build/core/ninja.mk and build/core/soong.mk. The only major feature it
adds is a rotating log file with some verbose logging.
There is one major functional difference -- you cannot override random
Make variables during the Make phase anymore. The environment variable
is set, and if Make uses ?= or the equivalent, it can still use those
variables. We already made this change for Kati, which also loads all of
the same code and actually does the build, so it has been half-removed
for a while.
The only "UI" this implements is what I'll call "Make Emulation" mode --
it's expected that current command lines will continue working, and
we'll explore alternate user interfaces later.
We're still using Make as a wrapper, but all it does is call into this
single Go program, it won't even load the product configuration. Once
this is default, we can start moving individual users over to using this
directly (still in Make emulation mode), skipping the Make wrapper.
Ideas for the future:
* Generating trace files showing time spent in Make/Kati/Soong/Ninja
(also importing ninja traces into the same stream). I had this working
in a previous version of this patch, but removed it to keep the size
down and focus on the current features.
* More intelligent SIGALRM handling, once we fully remove the Make
wrapper (which hides the SIGALRM)
* Reading the experimental binary output stream from Ninja, so that we
can always save the verbose log even if we're not printing it out to
the console
Test: USE_SOONG_UI=true m -j blueprint_tools
Change-Id: I884327b9a8ae24499eb6c56f6e1ad26df1cfa4e4
2016-08-22 00:17:17 +02:00
|
|
|
}
|
2021-08-12 14:03:55 +02:00
|
|
|
|
2021-09-02 17:23:06 +02:00
|
|
|
targets := make([]string, 0, 0)
|
2021-08-12 14:03:55 +02:00
|
|
|
|
2021-09-02 17:23:06 +02:00
|
|
|
if config.JsonModuleGraph() {
|
|
|
|
targets = append(targets, config.ModuleGraphFile())
|
|
|
|
}
|
|
|
|
|
2021-09-06 17:08:02 +02:00
|
|
|
if config.Queryview() {
|
|
|
|
targets = append(targets, config.QueryviewMarkerFile())
|
|
|
|
}
|
|
|
|
|
2021-09-06 18:31:46 +02:00
|
|
|
if config.SoongDocs() {
|
|
|
|
targets = append(targets, config.SoongDocsHtml())
|
|
|
|
}
|
|
|
|
|
2021-09-02 17:23:06 +02:00
|
|
|
if config.SoongBuildInvocationNeeded() {
|
2021-08-12 14:03:55 +02:00
|
|
|
// This build generates <builddir>/build.ninja, which is used later by build/soong/ui/build/build.go#Build().
|
2021-10-29 01:05:13 +02:00
|
|
|
targets = append(targets, config.SoongNinjaFile())
|
2021-08-12 14:03:55 +02:00
|
|
|
}
|
|
|
|
|
2023-10-27 19:54:27 +02:00
|
|
|
beforeSoongTimestamp := time.Now()
|
|
|
|
|
2023-05-22 22:33:27 +02:00
|
|
|
ninja(targets...)
|
2020-02-10 20:23:49 +01:00
|
|
|
|
2023-10-27 19:54:27 +02:00
|
|
|
loadSoongBuildMetrics(ctx, config, beforeSoongTimestamp)
|
|
|
|
|
2020-06-25 20:27:52 +02:00
|
|
|
distGzipFile(ctx, config, config.SoongNinjaFile(), "soong")
|
2022-08-16 02:57:30 +02:00
|
|
|
distFile(ctx, config, config.SoongVarsFile(), "soong")
|
2020-06-25 20:27:52 +02:00
|
|
|
|
2020-11-27 13:35:20 +01:00
|
|
|
if !config.SkipKati() {
|
2020-06-25 20:27:52 +02:00
|
|
|
distGzipFile(ctx, config, config.SoongAndroidMk(), "soong")
|
|
|
|
distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong")
|
|
|
|
}
|
|
|
|
|
2022-03-03 00:10:25 +01:00
|
|
|
if config.JsonModuleGraph() {
|
|
|
|
distGzipFile(ctx, config, config.ModuleGraphFile(), "soong")
|
|
|
|
}
|
2020-02-10 20:23:49 +01:00
|
|
|
}
|
|
|
|
|
2023-10-27 19:54:27 +02:00
|
|
|
// loadSoongBuildMetrics reads out/soong_build_metrics.pb if it was generated by soong_build and copies the
|
|
|
|
// events stored in it into the soong_ui trace to provide introspection into how long the different phases of
|
|
|
|
// soong_build are taking.
|
|
|
|
func loadSoongBuildMetrics(ctx Context, config Config, oldTimestamp time.Time) {
|
|
|
|
soongBuildMetricsFile := config.SoongBuildMetrics()
|
|
|
|
if metricsStat, err := os.Stat(soongBuildMetricsFile); err != nil {
|
|
|
|
ctx.Verbosef("Failed to stat %s: %s", soongBuildMetricsFile, err)
|
|
|
|
return
|
|
|
|
} else if !metricsStat.ModTime().After(oldTimestamp) {
|
|
|
|
ctx.Verbosef("%s timestamp not later after running soong, expected %s > %s",
|
|
|
|
soongBuildMetricsFile, metricsStat.ModTime(), oldTimestamp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-10-31 18:02:45 +01:00
|
|
|
metricsData, err := os.ReadFile(soongBuildMetricsFile)
|
2023-10-27 19:54:27 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.Verbosef("Failed to read %s: %s", soongBuildMetricsFile, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
soongBuildMetrics := metrics_proto.SoongBuildMetrics{}
|
|
|
|
err = proto.Unmarshal(metricsData, &soongBuildMetrics)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Verbosef("Failed to unmarshal %s: %s", soongBuildMetricsFile, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, event := range soongBuildMetrics.Events {
|
|
|
|
desc := event.GetDescription()
|
|
|
|
if dot := strings.LastIndexByte(desc, '.'); dot >= 0 {
|
|
|
|
desc = desc[dot+1:]
|
|
|
|
}
|
|
|
|
ctx.Tracer.Complete(desc, ctx.Thread,
|
|
|
|
event.GetStartTime(), event.GetStartTime()+event.GetRealTime())
|
|
|
|
}
|
2023-10-27 23:56:12 +02:00
|
|
|
for _, event := range soongBuildMetrics.PerfCounters {
|
|
|
|
timestamp := event.GetTime()
|
|
|
|
for _, group := range event.Groups {
|
|
|
|
counters := make([]tracer.Counter, 0, len(group.Counters))
|
|
|
|
for _, counter := range group.Counters {
|
|
|
|
counters = append(counters, tracer.Counter{
|
|
|
|
Name: counter.GetName(),
|
|
|
|
Value: counter.GetValue(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ctx.Tracer.CountersAtTime(group.GetName(), ctx.Thread, timestamp, counters)
|
|
|
|
}
|
|
|
|
}
|
2023-10-27 19:54:27 +02:00
|
|
|
}
|
|
|
|
|
2023-10-30 17:21:06 +01:00
|
|
|
func cleanBazelFiles(config Config) {
|
|
|
|
files := []string{
|
|
|
|
shared.JoinPath(config.SoongOutDir(), "bp2build"),
|
|
|
|
shared.JoinPath(config.SoongOutDir(), "workspace"),
|
|
|
|
shared.JoinPath(config.SoongOutDir(), bazel.SoongInjectionDirName),
|
|
|
|
shared.JoinPath(config.OutDir(), "bazel")}
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
os.RemoveAll(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 14:42:04 +01:00
|
|
|
func runMicrofactory(ctx Context, config Config, name string, pkg string, mapping map[string]string) {
|
2021-04-09 21:03:51 +02:00
|
|
|
ctx.BeginTrace(metrics.RunSoong, name)
|
|
|
|
defer ctx.EndTrace()
|
|
|
|
cfg := microfactory.Config{TrimPath: absPath(ctx, ".")}
|
|
|
|
for pkgPrefix, pathPrefix := range mapping {
|
|
|
|
cfg.Map(pkgPrefix, pathPrefix)
|
|
|
|
}
|
|
|
|
|
2021-11-02 14:42:04 +01:00
|
|
|
exePath := filepath.Join(config.SoongOutDir(), name)
|
2021-04-09 21:03:51 +02:00
|
|
|
dir := filepath.Dir(exePath)
|
|
|
|
if err := os.MkdirAll(dir, 0777); err != nil {
|
|
|
|
ctx.Fatalf("cannot create %s: %s", dir, err)
|
|
|
|
}
|
|
|
|
if _, err := microfactory.Build(&cfg, exePath, pkg); err != nil {
|
|
|
|
ctx.Fatalf("failed to build %s: %s", name, err)
|
|
|
|
}
|
|
|
|
}
|