2015-01-31 02:27:36 +01:00
|
|
|
// Copyright 2015 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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2021-03-23 11:46:47 +01:00
|
|
|
"io/ioutil"
|
2015-01-31 02:27:36 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-01-28 14:22:12 +01:00
|
|
|
"strings"
|
2021-03-24 10:50:06 +01:00
|
|
|
"time"
|
2015-01-31 02:27:36 +01:00
|
|
|
|
2021-09-22 02:50:30 +02:00
|
|
|
"android/soong/android"
|
2021-04-14 10:31:00 +02:00
|
|
|
"android/soong/bp2build"
|
2021-02-26 14:27:36 +01:00
|
|
|
"android/soong/shared"
|
2022-03-23 00:23:40 +01:00
|
|
|
"android/soong/ui/metrics/bp2build_metrics_proto"
|
2021-08-25 14:14:13 +02:00
|
|
|
|
2015-03-23 20:57:34 +01:00
|
|
|
"github.com/google/blueprint/bootstrap"
|
2021-04-14 13:49:50 +02:00
|
|
|
"github.com/google/blueprint/deptools"
|
2022-03-23 00:23:40 +01:00
|
|
|
"github.com/google/blueprint/metrics"
|
2021-09-22 02:50:30 +02:00
|
|
|
androidProtobuf "google.golang.org/protobuf/android"
|
2015-01-31 02:27:36 +01:00
|
|
|
)
|
|
|
|
|
2017-12-12 00:52:26 +01:00
|
|
|
var (
|
2021-04-14 10:31:00 +02:00
|
|
|
topDir string
|
2021-09-07 09:10:33 +02:00
|
|
|
outDir string
|
2021-09-01 16:25:51 +02:00
|
|
|
soongOutDir string
|
2021-04-14 10:31:00 +02:00
|
|
|
availableEnvFile string
|
|
|
|
usedEnvFile string
|
|
|
|
|
2021-09-07 09:10:33 +02:00
|
|
|
runGoTests bool
|
|
|
|
|
2021-08-18 10:55:32 +02:00
|
|
|
globFile string
|
|
|
|
globListDir string
|
2021-04-14 10:31:00 +02:00
|
|
|
delveListen string
|
|
|
|
delvePath string
|
|
|
|
|
2021-08-25 14:14:13 +02:00
|
|
|
moduleGraphFile string
|
2022-01-25 06:50:25 +01:00
|
|
|
moduleActionsFile string
|
2020-11-05 13:42:11 +01:00
|
|
|
docFile string
|
|
|
|
bazelQueryViewDir string
|
2021-04-14 10:31:00 +02:00
|
|
|
bp2buildMarker string
|
2021-08-16 15:24:48 +02:00
|
|
|
|
|
|
|
cmdlineArgs bootstrap.Args
|
2017-12-12 00:52:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-04-14 10:31:00 +02:00
|
|
|
// Flags that make sense in every mode
|
2021-02-26 14:27:36 +01:00
|
|
|
flag.StringVar(&topDir, "top", "", "Top directory of the Android source tree")
|
2021-09-01 16:25:51 +02:00
|
|
|
flag.StringVar(&soongOutDir, "soong_out", "", "Soong output directory (usually $TOP/out/soong)")
|
2021-04-14 10:31:00 +02:00
|
|
|
flag.StringVar(&availableEnvFile, "available_env", "", "File containing available environment variables")
|
|
|
|
flag.StringVar(&usedEnvFile, "used_env", "", "File containing used environment variables")
|
2021-08-31 10:42:08 +02:00
|
|
|
flag.StringVar(&globFile, "globFile", "build-globs.ninja", "the Ninja file of globs to output")
|
|
|
|
flag.StringVar(&globListDir, "globListDir", "", "the directory containing the glob list files")
|
2021-09-07 09:10:33 +02:00
|
|
|
flag.StringVar(&outDir, "out", "", "the ninja builddir directory")
|
2021-08-31 10:42:08 +02:00
|
|
|
flag.StringVar(&cmdlineArgs.ModuleListFile, "l", "", "file that lists filepaths to parse")
|
2021-04-14 10:31:00 +02:00
|
|
|
|
|
|
|
// Debug flags
|
2021-03-02 10:09:41 +01:00
|
|
|
flag.StringVar(&delveListen, "delve_listen", "", "Delve port to listen on for debugging")
|
|
|
|
flag.StringVar(&delvePath, "delve_path", "", "Path to Delve. Only used if --delve_listen is set")
|
2021-08-31 10:42:08 +02:00
|
|
|
flag.StringVar(&cmdlineArgs.Cpuprofile, "cpuprofile", "", "write cpu profile to file")
|
|
|
|
flag.StringVar(&cmdlineArgs.TraceFile, "trace", "", "write trace to file")
|
|
|
|
flag.StringVar(&cmdlineArgs.Memprofile, "memprofile", "", "write memory profile to file")
|
|
|
|
flag.BoolVar(&cmdlineArgs.NoGC, "nogc", false, "turn off GC for debugging")
|
2021-04-14 10:31:00 +02:00
|
|
|
|
|
|
|
// Flags representing various modes soong_build can run in
|
2021-08-25 14:14:13 +02:00
|
|
|
flag.StringVar(&moduleGraphFile, "module_graph_file", "", "JSON module graph file to output")
|
2022-01-25 06:50:25 +01:00
|
|
|
flag.StringVar(&moduleActionsFile, "module_actions_file", "", "JSON file to output inputs/outputs of actions of modules")
|
2017-12-12 00:52:26 +01:00
|
|
|
flag.StringVar(&docFile, "soong_docs", "", "build documentation file to output")
|
2021-03-08 16:34:09 +01:00
|
|
|
flag.StringVar(&bazelQueryViewDir, "bazel_queryview_dir", "", "path to the bazel queryview directory relative to --top")
|
2021-04-14 10:31:00 +02:00
|
|
|
flag.StringVar(&bp2buildMarker, "bp2build_marker", "", "If set, run bp2build, touch the specified marker file then exit")
|
2021-08-16 15:24:48 +02:00
|
|
|
flag.StringVar(&cmdlineArgs.OutFile, "o", "build.ninja", "the Ninja file to output")
|
2021-08-31 10:42:08 +02:00
|
|
|
flag.BoolVar(&cmdlineArgs.EmptyNinjaFile, "empty-ninja-file", false, "write out a 0-byte ninja file")
|
|
|
|
|
|
|
|
// Flags that probably shouldn't be flags of soong_build but we haven't found
|
|
|
|
// the time to remove them yet
|
2021-09-07 09:10:33 +02:00
|
|
|
flag.BoolVar(&runGoTests, "t", false, "build and run go tests during bootstrap")
|
2021-09-22 02:50:30 +02:00
|
|
|
|
|
|
|
// Disable deterministic randomization in the protobuf package, so incremental
|
|
|
|
// builds with unrelated Soong changes don't trigger large rebuilds (since we
|
|
|
|
// write out text protos in command lines, and command line changes trigger
|
|
|
|
// rebuilds).
|
|
|
|
androidProtobuf.DisableRand()
|
2017-12-12 00:52:26 +01:00
|
|
|
}
|
|
|
|
|
2017-11-30 01:47:17 +01:00
|
|
|
func newNameResolver(config android.Config) *android.NameResolver {
|
|
|
|
namespacePathsToExport := make(map[string]bool)
|
|
|
|
|
2018-03-12 23:30:26 +01:00
|
|
|
for _, namespaceName := range config.ExportedNamespaces() {
|
2017-11-30 01:47:17 +01:00
|
|
|
namespacePathsToExport[namespaceName] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
namespacePathsToExport["."] = true // always export the root namespace
|
|
|
|
|
|
|
|
exportFilter := func(namespace *android.Namespace) bool {
|
|
|
|
return namespacePathsToExport[namespace.Path]
|
|
|
|
}
|
|
|
|
|
|
|
|
return android.NewNameResolver(exportFilter)
|
|
|
|
}
|
|
|
|
|
2021-09-07 17:54:38 +02:00
|
|
|
func newContext(configuration android.Config) *android.Context {
|
2020-10-30 01:09:13 +01:00
|
|
|
ctx := android.NewContext(configuration)
|
2020-12-14 08:58:54 +01:00
|
|
|
ctx.Register()
|
2020-09-29 08:23:17 +02:00
|
|
|
ctx.SetNameInterface(newNameResolver(configuration))
|
|
|
|
ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies())
|
|
|
|
return ctx
|
|
|
|
}
|
2015-01-31 02:27:36 +01:00
|
|
|
|
2021-09-07 09:10:33 +02:00
|
|
|
func newConfig(availableEnv map[string]string) android.Config {
|
2022-08-20 20:48:32 +02:00
|
|
|
var buildMode android.SoongBuildMode
|
|
|
|
|
|
|
|
if bp2buildMarker != "" {
|
|
|
|
buildMode = android.Bp2build
|
|
|
|
} else if bazelQueryViewDir != "" {
|
|
|
|
buildMode = android.GenerateQueryView
|
|
|
|
} else if moduleGraphFile != "" {
|
|
|
|
buildMode = android.GenerateModuleGraph
|
|
|
|
} else if docFile != "" {
|
|
|
|
buildMode = android.GenerateDocFile
|
|
|
|
} else {
|
|
|
|
buildMode = android.AnalysisNoBazel
|
|
|
|
}
|
|
|
|
|
|
|
|
configuration, err := android.NewConfig(cmdlineArgs.ModuleListFile, buildMode, runGoTests, outDir, soongOutDir, availableEnv)
|
2015-01-31 02:27:36 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-09-29 08:23:17 +02:00
|
|
|
return configuration
|
|
|
|
}
|
2015-01-31 02:27:36 +01:00
|
|
|
|
2022-05-10 19:50:12 +02:00
|
|
|
// Bazel-enabled mode. Attaches a mutator to queue Bazel requests, adds a
|
|
|
|
// BeforePrepareBuildActionsHook to invoke Bazel, and then uses Bazel metadata
|
|
|
|
// for modules that should be handled by Bazel.
|
|
|
|
func runMixedModeBuild(configuration android.Config, ctx *android.Context, extraNinjaDeps []string) {
|
|
|
|
ctx.EventHandler.Begin("mixed_build")
|
|
|
|
defer ctx.EventHandler.End("mixed_build")
|
|
|
|
|
|
|
|
bazelHook := func() error {
|
|
|
|
ctx.EventHandler.Begin("bazel")
|
|
|
|
defer ctx.EventHandler.End("bazel")
|
2022-05-18 00:13:28 +02:00
|
|
|
return configuration.BazelContext.InvokeBazel(configuration)
|
2021-04-01 17:55:58 +02:00
|
|
|
}
|
2022-05-10 19:50:12 +02:00
|
|
|
ctx.SetBeforePrepareBuildActionsHook(bazelHook)
|
2022-03-23 00:23:40 +01:00
|
|
|
|
2022-05-10 19:50:12 +02:00
|
|
|
ninjaDeps := bootstrap.RunBlueprint(cmdlineArgs, bootstrap.DoEverything, ctx.Context, configuration)
|
2022-05-24 21:38:38 +02:00
|
|
|
ninjaDeps = append(ninjaDeps, extraNinjaDeps...)
|
2021-08-18 10:55:32 +02:00
|
|
|
|
2022-05-10 19:50:12 +02:00
|
|
|
globListFiles := writeBuildGlobsNinjaFile(ctx, configuration.SoongOutDir(), configuration)
|
2021-08-18 10:55:32 +02:00
|
|
|
ninjaDeps = append(ninjaDeps, globListFiles...)
|
|
|
|
|
2022-05-10 19:50:12 +02:00
|
|
|
writeDepFile(cmdlineArgs.OutFile, *ctx.EventHandler, ninjaDeps)
|
2021-04-01 17:55:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run the code-generation phase to convert BazelTargetModules to BUILD files.
|
2021-09-06 17:08:02 +02:00
|
|
|
func runQueryView(queryviewDir, queryviewMarker string, configuration android.Config, ctx *android.Context) {
|
2022-03-23 00:23:40 +01:00
|
|
|
ctx.EventHandler.Begin("queryview")
|
|
|
|
defer ctx.EventHandler.End("queryview")
|
2021-04-01 17:55:58 +02:00
|
|
|
codegenContext := bp2build.NewCodegenContext(configuration, *ctx, bp2build.QueryView)
|
2021-09-06 17:08:02 +02:00
|
|
|
absoluteQueryViewDir := shared.JoinPath(topDir, queryviewDir)
|
2021-04-01 17:55:58 +02:00
|
|
|
if err := createBazelQueryView(codegenContext, absoluteQueryViewDir); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-09-06 17:08:02 +02:00
|
|
|
|
|
|
|
touch(shared.JoinPath(topDir, queryviewMarker))
|
2021-04-01 17:55:58 +02:00
|
|
|
}
|
|
|
|
|
2022-04-21 08:11:43 +02:00
|
|
|
func writeMetrics(configuration android.Config, eventHandler metrics.EventHandler, metricsDir string) {
|
2022-03-23 00:23:40 +01:00
|
|
|
if len(metricsDir) < 1 {
|
|
|
|
fmt.Fprintf(os.Stderr, "\nMissing required env var for generating soong metrics: LOG_DIR\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
metricsFile := filepath.Join(metricsDir, "soong_build_metrics.pb")
|
|
|
|
err := android.WriteMetrics(configuration, eventHandler, metricsFile)
|
2021-04-01 17:55:58 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error writing soong_build metrics %s: %s", metricsFile, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 06:50:25 +01:00
|
|
|
func writeJsonModuleGraphAndActions(ctx *android.Context, graphPath string, actionsPath string) {
|
|
|
|
graphFile, graphErr := os.Create(shared.JoinPath(topDir, graphPath))
|
|
|
|
actionsFile, actionsErr := os.Create(shared.JoinPath(topDir, actionsPath))
|
|
|
|
if graphErr != nil || actionsErr != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Graph err: %s, actions err: %s", graphErr, actionsErr)
|
2021-04-01 18:28:45 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2022-01-25 06:50:25 +01:00
|
|
|
defer graphFile.Close()
|
|
|
|
defer actionsFile.Close()
|
|
|
|
ctx.Context.PrintJSONGraphAndActions(graphFile, actionsFile)
|
2021-04-01 18:28:45 +02:00
|
|
|
}
|
|
|
|
|
2022-03-23 00:23:40 +01:00
|
|
|
func writeBuildGlobsNinjaFile(ctx *android.Context, buildDir string, config interface{}) []string {
|
|
|
|
ctx.EventHandler.Begin("globs_ninja_file")
|
|
|
|
defer ctx.EventHandler.End("globs_ninja_file")
|
|
|
|
|
2021-08-18 10:55:32 +02:00
|
|
|
globDir := bootstrap.GlobDirectory(buildDir, globListDir)
|
|
|
|
bootstrap.WriteBuildGlobsNinjaFile(&bootstrap.GlobSingleton{
|
2022-03-23 00:23:40 +01:00
|
|
|
GlobLister: ctx.Globs,
|
2021-08-18 10:55:32 +02:00
|
|
|
GlobFile: globFile,
|
|
|
|
GlobDir: globDir,
|
2022-03-23 00:23:40 +01:00
|
|
|
SrcDir: ctx.SrcDir(),
|
2021-08-18 10:55:32 +02:00
|
|
|
}, config)
|
|
|
|
return bootstrap.GlobFileListFiles(globDir)
|
|
|
|
}
|
|
|
|
|
2022-03-23 00:23:40 +01:00
|
|
|
func writeDepFile(outputFile string, eventHandler metrics.EventHandler, ninjaDeps []string) {
|
|
|
|
eventHandler.Begin("ninja_deps")
|
|
|
|
defer eventHandler.End("ninja_deps")
|
2021-08-25 14:14:13 +02:00
|
|
|
depFile := shared.JoinPath(topDir, outputFile+".d")
|
|
|
|
err := deptools.WriteDepFile(depFile, outputFile, ninjaDeps)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error writing depfile '%s': %s\n", depFile, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 04:40:29 +02:00
|
|
|
// doChosenActivity runs Soong for a specific activity, like bp2build, queryview
|
|
|
|
// or the actual Soong build for the build.ninja file. Returns the top level
|
|
|
|
// output file of the specific activity.
|
2022-06-08 02:16:08 +02:00
|
|
|
func doChosenActivity(ctx *android.Context, configuration android.Config, extraNinjaDeps []string, logDir string) string {
|
2022-08-20 20:48:32 +02:00
|
|
|
if configuration.BuildMode == android.Bp2build {
|
2021-04-01 17:55:58 +02:00
|
|
|
// Run the alternate pipeline of bp2build mutators and singleton to convert
|
|
|
|
// Blueprint to BUILD files before everything else.
|
|
|
|
runBp2Build(configuration, extraNinjaDeps)
|
2021-05-25 04:40:29 +02:00
|
|
|
return bp2buildMarker
|
2022-08-20 20:48:32 +02:00
|
|
|
} else if configuration.IsMixedBuildsEnabled() {
|
2021-04-01 17:55:58 +02:00
|
|
|
runMixedModeBuild(configuration, ctx, extraNinjaDeps)
|
|
|
|
} else {
|
2022-03-23 00:23:40 +01:00
|
|
|
var stopBefore bootstrap.StopBefore
|
2022-08-20 20:48:32 +02:00
|
|
|
if configuration.BuildMode == android.GenerateModuleGraph {
|
2022-03-23 00:23:40 +01:00
|
|
|
stopBefore = bootstrap.StopBeforeWriteNinja
|
2022-08-20 20:48:32 +02:00
|
|
|
} else if configuration.BuildMode == android.GenerateQueryView || configuration.BuildMode == android.GenerateDocFile {
|
2022-03-23 00:23:40 +01:00
|
|
|
stopBefore = bootstrap.StopBeforePrepareBuildActions
|
|
|
|
} else {
|
|
|
|
stopBefore = bootstrap.DoEverything
|
|
|
|
}
|
|
|
|
|
2022-08-20 20:48:32 +02:00
|
|
|
ninjaDeps := bootstrap.RunBlueprint(cmdlineArgs, stopBefore, ctx.Context, configuration)
|
2021-04-15 15:06:40 +02:00
|
|
|
ninjaDeps = append(ninjaDeps, extraNinjaDeps...)
|
2021-08-18 10:55:32 +02:00
|
|
|
|
2022-03-23 00:23:40 +01:00
|
|
|
globListFiles := writeBuildGlobsNinjaFile(ctx, configuration.SoongOutDir(), configuration)
|
2021-08-18 10:55:32 +02:00
|
|
|
ninjaDeps = append(ninjaDeps, globListFiles...)
|
|
|
|
|
2021-08-25 14:14:13 +02:00
|
|
|
// Convert the Soong module graph into Bazel BUILD files.
|
2022-08-20 20:48:32 +02:00
|
|
|
if configuration.BuildMode == android.GenerateQueryView {
|
2021-09-06 17:08:02 +02:00
|
|
|
queryviewMarkerFile := bazelQueryViewDir + ".marker"
|
|
|
|
runQueryView(bazelQueryViewDir, queryviewMarkerFile, configuration, ctx)
|
2022-03-23 00:23:40 +01:00
|
|
|
writeDepFile(queryviewMarkerFile, *ctx.EventHandler, ninjaDeps)
|
2021-09-06 17:08:02 +02:00
|
|
|
return queryviewMarkerFile
|
2022-08-20 20:48:32 +02:00
|
|
|
} else if configuration.BuildMode == android.GenerateModuleGraph {
|
2022-01-25 06:50:25 +01:00
|
|
|
writeJsonModuleGraphAndActions(ctx, moduleGraphFile, moduleActionsFile)
|
2022-03-23 00:23:40 +01:00
|
|
|
writeDepFile(moduleGraphFile, *ctx.EventHandler, ninjaDeps)
|
2021-08-25 14:14:13 +02:00
|
|
|
return moduleGraphFile
|
2022-08-20 20:48:32 +02:00
|
|
|
} else if configuration.BuildMode == android.GenerateDocFile {
|
2021-09-06 18:31:46 +02:00
|
|
|
// TODO: we could make writeDocs() return the list of documentation files
|
|
|
|
// written and add them to the .d file. Then soong_docs would be re-run
|
|
|
|
// whenever one is deleted.
|
|
|
|
if err := writeDocs(ctx, shared.JoinPath(topDir, docFile)); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error building Soong documentation: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-03-23 00:23:40 +01:00
|
|
|
writeDepFile(docFile, *ctx.EventHandler, ninjaDeps)
|
2021-09-06 18:31:46 +02:00
|
|
|
return docFile
|
2021-08-25 14:14:13 +02:00
|
|
|
} else {
|
|
|
|
// The actual output (build.ninja) was written in the RunBlueprint() call
|
|
|
|
// above
|
2022-03-23 00:23:40 +01:00
|
|
|
writeDepFile(cmdlineArgs.OutFile, *ctx.EventHandler, ninjaDeps)
|
2021-04-14 13:49:50 +02:00
|
|
|
}
|
2021-04-01 17:55:58 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 15:24:48 +02:00
|
|
|
return cmdlineArgs.OutFile
|
2021-04-14 10:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// soong_ui dumps the available environment variables to
|
|
|
|
// soong.environment.available . Then soong_build itself is run with an empty
|
|
|
|
// environment so that the only way environment variables can be accessed is
|
|
|
|
// using Config, which tracks access to them.
|
|
|
|
|
|
|
|
// At the end of the build, a file called soong.environment.used is written
|
|
|
|
// containing the current value of all used environment variables. The next
|
|
|
|
// time soong_ui is run, it checks whether any environment variables that was
|
|
|
|
// used had changed and if so, it deletes soong.environment.used to cause a
|
|
|
|
// rebuild.
|
|
|
|
//
|
|
|
|
// The dependency of build.ninja on soong.environment.used is declared in
|
|
|
|
// build.ninja.d
|
|
|
|
func parseAvailableEnv() map[string]string {
|
|
|
|
if availableEnvFile == "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "--available_env not set\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := shared.EnvFromFile(shared.JoinPath(topDir, availableEnvFile))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error reading available environment file '%s': %s\n", availableEnvFile, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2021-04-01 17:55:58 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 08:23:17 +02:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2015-12-18 01:39:19 +01:00
|
|
|
|
2021-03-02 10:09:41 +01:00
|
|
|
shared.ReexecWithDelveMaybe(delveListen, delvePath)
|
2021-02-26 14:27:36 +01:00
|
|
|
android.InitSandbox(topDir)
|
|
|
|
|
2021-04-14 10:31:00 +02:00
|
|
|
availableEnv := parseAvailableEnv()
|
2021-04-12 14:04:24 +02:00
|
|
|
|
2021-09-07 09:10:33 +02:00
|
|
|
configuration := newConfig(availableEnv)
|
2021-03-23 11:46:47 +01:00
|
|
|
extraNinjaDeps := []string{
|
|
|
|
configuration.ProductVariablesFileName,
|
2021-04-14 13:49:50 +02:00
|
|
|
usedEnvFile,
|
2021-03-23 11:46:47 +01:00
|
|
|
}
|
2019-06-19 22:33:24 +02:00
|
|
|
|
2021-03-16 08:55:23 +01:00
|
|
|
if configuration.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" {
|
|
|
|
configuration.SetAllowMissingDependencies()
|
|
|
|
}
|
|
|
|
|
2021-03-02 10:09:41 +01:00
|
|
|
if shared.IsDebugging() {
|
2019-06-19 22:33:24 +02:00
|
|
|
// Add a non-existent file to the dependencies so that soong_build will rerun when the debugger is
|
|
|
|
// enabled even if it completed successfully.
|
2021-08-26 15:07:24 +02:00
|
|
|
extraNinjaDeps = append(extraNinjaDeps, filepath.Join(configuration.SoongOutDir(), "always_rerun_for_delve"))
|
2019-06-19 22:33:24 +02:00
|
|
|
}
|
2020-12-14 08:58:54 +01:00
|
|
|
|
2022-04-21 08:11:43 +02:00
|
|
|
// Bypass configuration.Getenv, as LOG_DIR does not need to be dependency tracked. By definition, it will
|
|
|
|
// change between every CI build, so tracking it would require re-running Soong for every build.
|
|
|
|
logDir := availableEnv["LOG_DIR"]
|
|
|
|
|
2022-06-08 02:16:08 +02:00
|
|
|
ctx := newContext(configuration)
|
|
|
|
ctx.EventHandler.Begin("soong_build")
|
|
|
|
|
|
|
|
finalOutputFile := doChosenActivity(ctx, configuration, extraNinjaDeps, logDir)
|
|
|
|
|
|
|
|
ctx.EventHandler.End("soong_build")
|
|
|
|
writeMetrics(configuration, *ctx.EventHandler, logDir)
|
2022-03-23 00:23:40 +01:00
|
|
|
|
2021-04-14 10:31:00 +02:00
|
|
|
writeUsedEnvironmentFile(configuration, finalOutputFile)
|
2021-03-23 11:46:47 +01:00
|
|
|
}
|
|
|
|
|
2021-04-14 10:31:00 +02:00
|
|
|
func writeUsedEnvironmentFile(configuration android.Config, finalOutputFile string) {
|
|
|
|
if usedEnvFile == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
path := shared.JoinPath(topDir, usedEnvFile)
|
2021-03-23 11:46:47 +01:00
|
|
|
data, err := shared.EnvFileContents(configuration.EnvDeps())
|
|
|
|
if err != nil {
|
2021-04-14 10:31:00 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "error writing used environment file '%s': %s\n", usedEnvFile, err)
|
2021-03-23 11:46:47 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(path, data, 0666)
|
|
|
|
if err != nil {
|
2021-04-14 10:31:00 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "error writing used environment file '%s': %s\n", usedEnvFile, err)
|
2021-03-24 10:50:06 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-04-14 10:31:00 +02:00
|
|
|
// Touch the output file so that it's not older than the file we just
|
2021-03-24 10:50:06 +01:00
|
|
|
// wrote. We can't write the environment file earlier because one an access
|
|
|
|
// new environment variables while writing it.
|
2021-04-14 10:31:00 +02:00
|
|
|
touch(shared.JoinPath(topDir, finalOutputFile))
|
2015-01-31 02:27:36 +01:00
|
|
|
}
|
2020-07-15 12:06:41 +02:00
|
|
|
|
2021-04-14 10:31:00 +02:00
|
|
|
func touch(path string) {
|
|
|
|
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error touching '%s': %s\n", path, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error touching '%s': %s\n", path, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTime := time.Now().Local()
|
|
|
|
err = os.Chtimes(path, currentTime, currentTime)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error touching '%s': %s\n", path, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 03:20:13 +02:00
|
|
|
// Find BUILD files in the srcDir which...
|
|
|
|
//
|
|
|
|
// - are not on the allow list (android/bazel.go#ShouldKeepExistingBuildFileForDir())
|
|
|
|
//
|
|
|
|
// - won't be overwritten by corresponding bp2build generated files
|
|
|
|
//
|
|
|
|
// And return their paths so they can be left out of the Bazel workspace dir (i.e. ignored)
|
2021-10-18 22:59:00 +02:00
|
|
|
func getPathsToIgnoredBuildFiles(topDir string, generatedRoot string, srcDirBazelFiles []string, verbose bool) []string {
|
2021-05-13 03:20:13 +02:00
|
|
|
paths := make([]string, 0)
|
|
|
|
|
2021-05-21 01:34:50 +02:00
|
|
|
for _, srcDirBazelFileRelativePath := range srcDirBazelFiles {
|
|
|
|
srcDirBazelFileFullPath := shared.JoinPath(topDir, srcDirBazelFileRelativePath)
|
|
|
|
fileInfo, err := os.Stat(srcDirBazelFileFullPath)
|
2021-05-13 03:20:13 +02:00
|
|
|
if err != nil {
|
2021-05-21 01:34:50 +02:00
|
|
|
// Warn about error, but continue trying to check files
|
|
|
|
fmt.Fprintf(os.Stderr, "WARNING: Error accessing path '%s', err: %s\n", srcDirBazelFileFullPath, err)
|
|
|
|
continue
|
2021-05-13 03:20:13 +02:00
|
|
|
}
|
2021-05-21 01:34:50 +02:00
|
|
|
if fileInfo.IsDir() {
|
2021-05-13 03:20:13 +02:00
|
|
|
// Don't ignore entire directories
|
2021-05-21 01:34:50 +02:00
|
|
|
continue
|
2021-05-13 03:20:13 +02:00
|
|
|
}
|
2021-05-21 01:34:50 +02:00
|
|
|
if !(fileInfo.Name() == "BUILD" || fileInfo.Name() == "BUILD.bazel") {
|
2021-05-13 03:20:13 +02:00
|
|
|
// Don't ignore this file - it is not a build file
|
2021-05-21 01:34:50 +02:00
|
|
|
continue
|
2021-05-13 03:20:13 +02:00
|
|
|
}
|
2021-05-21 01:34:50 +02:00
|
|
|
srcDirBazelFileDir := filepath.Dir(srcDirBazelFileRelativePath)
|
|
|
|
if android.ShouldKeepExistingBuildFileForDir(srcDirBazelFileDir) {
|
2021-05-13 03:20:13 +02:00
|
|
|
// Don't ignore this existing build file
|
2021-05-21 01:34:50 +02:00
|
|
|
continue
|
2021-05-13 03:20:13 +02:00
|
|
|
}
|
2021-05-21 01:34:50 +02:00
|
|
|
correspondingBp2BuildFile := shared.JoinPath(topDir, generatedRoot, srcDirBazelFileRelativePath)
|
|
|
|
if _, err := os.Stat(correspondingBp2BuildFile); err == nil {
|
2021-05-13 03:20:13 +02:00
|
|
|
// If bp2build generated an alternate BUILD file, don't exclude this workspace path
|
|
|
|
// BUILD file clash resolution happens later in the symlink forest creation
|
2021-05-21 01:34:50 +02:00
|
|
|
continue
|
2021-05-13 03:20:13 +02:00
|
|
|
}
|
2021-10-18 22:59:00 +02:00
|
|
|
if verbose {
|
|
|
|
fmt.Fprintf(os.Stderr, "Ignoring existing BUILD file: %s\n", srcDirBazelFileRelativePath)
|
|
|
|
}
|
2021-05-21 01:34:50 +02:00
|
|
|
paths = append(paths, srcDirBazelFileRelativePath)
|
|
|
|
}
|
2021-05-13 03:20:13 +02:00
|
|
|
|
2021-05-21 01:34:50 +02:00
|
|
|
return paths
|
2021-05-13 03:20:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns temporary symlink forest excludes necessary for bazel build //external/... (and bazel build //frameworks/...) to work
|
|
|
|
func getTemporaryExcludes() []string {
|
|
|
|
excludes := make([]string, 0)
|
|
|
|
|
|
|
|
// FIXME: 'autotest_lib' is a symlink back to external/autotest, and this causes an infinite symlink expansion error for Bazel
|
|
|
|
excludes = append(excludes, "external/autotest/venv/autotest_lib")
|
2022-06-07 12:07:22 +02:00
|
|
|
excludes = append(excludes, "external/autotest/autotest_lib")
|
|
|
|
excludes = append(excludes, "external/autotest/client/autotest_lib/client")
|
2021-05-13 03:20:13 +02:00
|
|
|
|
|
|
|
// FIXME: The external/google-fruit/extras/bazel_root/third_party/fruit dir is poison
|
|
|
|
// It contains several symlinks back to real source dirs, and those source dirs contain BUILD files we want to ignore
|
|
|
|
excludes = append(excludes, "external/google-fruit/extras/bazel_root/third_party/fruit")
|
|
|
|
|
|
|
|
// FIXME: 'frameworks/compile/slang' has a filegroup error due to an escaping issue
|
|
|
|
excludes = append(excludes, "frameworks/compile/slang")
|
|
|
|
|
|
|
|
return excludes
|
|
|
|
}
|
|
|
|
|
2021-05-21 01:34:50 +02:00
|
|
|
// Read the bazel.list file that the Soong Finder already dumped earlier (hopefully)
|
|
|
|
// It contains the locations of BUILD files, BUILD.bazel files, etc. in the source dir
|
|
|
|
func getExistingBazelRelatedFiles(topDir string) ([]string, error) {
|
2021-08-16 15:24:48 +02:00
|
|
|
bazelFinderFile := filepath.Join(filepath.Dir(cmdlineArgs.ModuleListFile), "bazel.list")
|
2021-05-21 01:34:50 +02:00
|
|
|
if !filepath.IsAbs(bazelFinderFile) {
|
|
|
|
// Assume this was a relative path under topDir
|
|
|
|
bazelFinderFile = filepath.Join(topDir, bazelFinderFile)
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadFile(bazelFinderFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
files := strings.Split(strings.TrimSpace(string(data)), "\n")
|
|
|
|
return files, nil
|
|
|
|
}
|
|
|
|
|
2020-12-14 08:58:54 +01:00
|
|
|
// Run Soong in the bp2build mode. This creates a standalone context that registers
|
|
|
|
// an alternate pipeline of mutators and singletons specifically for generating
|
|
|
|
// Bazel BUILD files instead of Ninja files.
|
2021-04-01 17:55:58 +02:00
|
|
|
func runBp2Build(configuration android.Config, extraNinjaDeps []string) {
|
2022-03-23 00:23:40 +01:00
|
|
|
eventHandler := metrics.EventHandler{}
|
2022-05-26 18:22:17 +02:00
|
|
|
var metrics bp2build.CodegenMetrics
|
|
|
|
eventHandler.Do("bp2build", func() {
|
|
|
|
|
|
|
|
// Register an alternate set of singletons and mutators for bazel
|
|
|
|
// conversion for Bazel conversion.
|
|
|
|
bp2buildCtx := android.NewContext(configuration)
|
|
|
|
|
|
|
|
// Propagate "allow misssing dependencies" bit. This is normally set in
|
|
|
|
// newContext(), but we create bp2buildCtx without calling that method.
|
|
|
|
bp2buildCtx.SetAllowMissingDependencies(configuration.AllowMissingDependencies())
|
|
|
|
bp2buildCtx.SetNameInterface(newNameResolver(configuration))
|
|
|
|
bp2buildCtx.RegisterForBazelConversion()
|
|
|
|
|
|
|
|
// The bp2build process is a purely functional process that only depends on
|
|
|
|
// Android.bp files. It must not depend on the values of per-build product
|
|
|
|
// configurations or variables, since those will generate different BUILD
|
|
|
|
// files based on how the user has configured their tree.
|
|
|
|
bp2buildCtx.SetModuleListFile(cmdlineArgs.ModuleListFile)
|
|
|
|
modulePaths, err := bp2buildCtx.ListModulePaths(".")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-02-05 07:28:44 +01:00
|
|
|
|
2022-05-26 18:22:17 +02:00
|
|
|
extraNinjaDeps = append(extraNinjaDeps, modulePaths...)
|
2021-03-23 11:46:47 +01:00
|
|
|
|
2022-05-26 18:22:17 +02:00
|
|
|
// Run the loading and analysis pipeline to prepare the graph of regular
|
|
|
|
// Modules parsed from Android.bp files, and the BazelTargetModules mapped
|
|
|
|
// from the regular Modules.
|
|
|
|
blueprintArgs := cmdlineArgs
|
|
|
|
ninjaDeps := bootstrap.RunBlueprint(blueprintArgs, bootstrap.StopBeforePrepareBuildActions, bp2buildCtx.Context, configuration)
|
|
|
|
ninjaDeps = append(ninjaDeps, extraNinjaDeps...)
|
2021-04-14 13:49:50 +02:00
|
|
|
|
2022-05-26 18:22:17 +02:00
|
|
|
globListFiles := writeBuildGlobsNinjaFile(bp2buildCtx, configuration.SoongOutDir(), configuration)
|
|
|
|
ninjaDeps = append(ninjaDeps, globListFiles...)
|
2021-04-14 13:49:50 +02:00
|
|
|
|
2022-05-26 18:22:17 +02:00
|
|
|
// Run the code-generation phase to convert BazelTargetModules to BUILD files
|
|
|
|
// and print conversion metrics to the user.
|
|
|
|
codegenContext := bp2build.NewCodegenContext(configuration, *bp2buildCtx, bp2build.Bp2Build)
|
|
|
|
metrics = bp2build.Codegen(codegenContext)
|
2021-02-19 06:48:40 +01:00
|
|
|
|
2022-05-26 18:22:17 +02:00
|
|
|
generatedRoot := shared.JoinPath(configuration.SoongOutDir(), "bp2build")
|
|
|
|
workspaceRoot := shared.JoinPath(configuration.SoongOutDir(), "workspace")
|
2021-04-16 13:47:36 +02:00
|
|
|
|
2022-05-26 18:22:17 +02:00
|
|
|
excludes := []string{
|
|
|
|
"bazel-bin",
|
|
|
|
"bazel-genfiles",
|
|
|
|
"bazel-out",
|
|
|
|
"bazel-testlogs",
|
|
|
|
"bazel-" + filepath.Base(topDir),
|
|
|
|
}
|
2021-04-16 13:47:36 +02:00
|
|
|
|
2022-05-26 18:22:17 +02:00
|
|
|
if outDir[0] != '/' {
|
|
|
|
excludes = append(excludes, outDir)
|
|
|
|
}
|
2021-05-21 01:34:50 +02:00
|
|
|
|
2022-05-26 18:22:17 +02:00
|
|
|
existingBazelRelatedFiles, err := getExistingBazelRelatedFiles(topDir)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Error determining existing Bazel-related files: %s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-05-13 03:20:13 +02:00
|
|
|
|
2022-05-26 18:22:17 +02:00
|
|
|
pathsToIgnoredBuildFiles := getPathsToIgnoredBuildFiles(topDir, generatedRoot, existingBazelRelatedFiles, configuration.IsEnvTrue("BP2BUILD_VERBOSE"))
|
|
|
|
excludes = append(excludes, pathsToIgnoredBuildFiles...)
|
2021-05-13 03:20:13 +02:00
|
|
|
|
2022-05-26 18:22:17 +02:00
|
|
|
excludes = append(excludes, getTemporaryExcludes()...)
|
2021-04-16 13:47:36 +02:00
|
|
|
|
2022-05-26 18:22:17 +02:00
|
|
|
symlinkForestDeps := bp2build.PlantSymlinkForest(
|
2022-05-20 04:34:31 +02:00
|
|
|
configuration, topDir, workspaceRoot, generatedRoot, ".", excludes)
|
2021-04-16 13:47:36 +02:00
|
|
|
|
2022-05-26 18:22:17 +02:00
|
|
|
ninjaDeps = append(ninjaDeps, codegenContext.AdditionalNinjaDeps()...)
|
|
|
|
ninjaDeps = append(ninjaDeps, symlinkForestDeps...)
|
2021-04-16 13:47:36 +02:00
|
|
|
|
2022-05-26 18:22:17 +02:00
|
|
|
writeDepFile(bp2buildMarker, eventHandler, ninjaDeps)
|
2022-03-23 00:23:40 +01:00
|
|
|
|
2022-05-26 18:22:17 +02:00
|
|
|
// Create an empty bp2build marker file.
|
|
|
|
touch(shared.JoinPath(topDir, bp2buildMarker))
|
|
|
|
})
|
2022-03-23 00:23:40 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
// 1:1 mapping for each module.
|
2022-05-20 04:34:31 +02:00
|
|
|
if configuration.IsEnvTrue("BP2BUILD_VERBOSE") {
|
|
|
|
metrics.Print()
|
|
|
|
}
|
2022-03-23 00:23:40 +01:00
|
|
|
writeBp2BuildMetrics(&metrics, configuration, eventHandler)
|
2020-12-14 08:58:54 +01:00
|
|
|
}
|
2021-11-10 15:55:20 +01:00
|
|
|
|
|
|
|
// Write Bp2Build metrics into $LOG_DIR
|
2022-03-23 00:23:40 +01:00
|
|
|
func writeBp2BuildMetrics(codegenMetrics *bp2build.CodegenMetrics,
|
|
|
|
configuration android.Config, eventHandler metrics.EventHandler) {
|
|
|
|
for _, event := range eventHandler.CompletedEvents() {
|
|
|
|
codegenMetrics.Events = append(codegenMetrics.Events,
|
|
|
|
&bp2build_metrics_proto.Event{
|
|
|
|
Name: event.Id,
|
|
|
|
StartTime: uint64(event.Start.UnixNano()),
|
|
|
|
RealTime: event.RuntimeNanoseconds(),
|
|
|
|
})
|
|
|
|
}
|
2021-11-10 15:55:20 +01:00
|
|
|
metricsDir := configuration.Getenv("LOG_DIR")
|
|
|
|
if len(metricsDir) < 1 {
|
|
|
|
fmt.Fprintf(os.Stderr, "\nMissing required env var for generating bp2build metrics: LOG_DIR\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-03-23 00:23:40 +01:00
|
|
|
codegenMetrics.Write(metricsDir)
|
2021-11-10 15:55:20 +01:00
|
|
|
}
|