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 (
2022-10-24 17:33:09 +02:00
"bytes"
2023-06-01 16:16:41 +02:00
"errors"
2015-01-31 02:27:36 +01:00
"flag"
"fmt"
"os"
"path/filepath"
2023-06-06 18:17:50 +02:00
"regexp"
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"
2023-03-18 16:12:39 +01:00
"android/soong/android/allowlists"
2022-09-28 22:43:08 +02:00
"android/soong/bazel"
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
2023-04-11 07:28:43 +02:00
"github.com/google/blueprint"
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
availableEnvFile string
usedEnvFile string
2021-08-18 10:55:32 +02:00
globFile string
globListDir string
2021-04-14 10:31:00 +02:00
delveListen string
delvePath string
2022-12-13 06:23:34 +01:00
cmdlineArgs android . CmdArgs
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" )
2022-12-13 06:23:34 +01:00
flag . StringVar ( & cmdlineArgs . 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" )
2022-12-13 06:23:34 +01:00
flag . StringVar ( & cmdlineArgs . 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
2022-12-13 06:23:34 +01:00
flag . StringVar ( & cmdlineArgs . ModuleGraphFile , "module_graph_file" , "" , "JSON module graph file to output" )
flag . StringVar ( & cmdlineArgs . ModuleActionsFile , "module_actions_file" , "" , "JSON file to output inputs/outputs of actions of modules" )
flag . StringVar ( & cmdlineArgs . DocFile , "soong_docs" , "" , "build documentation file to output" )
flag . StringVar ( & cmdlineArgs . BazelQueryViewDir , "bazel_queryview_dir" , "" , "path to the bazel queryview directory relative to --top" )
flag . StringVar ( & cmdlineArgs . BazelApiBp2buildDir , "bazel_api_bp2build_dir" , "" , "path to the bazel api_bp2build directory relative to --top" )
flag . StringVar ( & cmdlineArgs . Bp2buildMarker , "bp2build_marker" , "" , "If set, run bp2build, touch the specified marker file then exit" )
flag . StringVar ( & cmdlineArgs . SymlinkForestMarker , "symlink_forest_marker" , "" , "If set, create the bp2build symlink forest, 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" )
2023-04-19 06:13:45 +02:00
flag . StringVar ( & cmdlineArgs . SoongVariables , "soong_variables" , "soong.variables" , "the file contains all build variables" )
2022-11-29 01:47:59 +01:00
flag . StringVar ( & cmdlineArgs . BazelForceEnabledModules , "bazel-force-enabled-modules" , "" , "additional modules to build with Bazel. Comma-delimited" )
2021-08-31 10:42:08 +02:00
flag . BoolVar ( & cmdlineArgs . EmptyNinjaFile , "empty-ninja-file" , false , "write out a 0-byte ninja file" )
2023-03-09 19:19:35 +01:00
flag . BoolVar ( & cmdlineArgs . MultitreeBuild , "multitree-build" , false , "this is a multitree build" )
2022-08-19 04:04:11 +02:00
flag . BoolVar ( & cmdlineArgs . BazelMode , "bazel-mode" , false , "use bazel for analysis of certain modules" )
2022-11-01 12:28:29 +01:00
flag . BoolVar ( & cmdlineArgs . BazelModeStaging , "bazel-mode-staging" , false , "use bazel for analysis of certain near-ready modules" )
2022-08-19 04:04:11 +02:00
flag . BoolVar ( & cmdlineArgs . BazelModeDev , "bazel-mode-dev" , false , "use bazel for analysis of a large number of modules (less stable)" )
2023-02-23 23:28:06 +01:00
flag . BoolVar ( & cmdlineArgs . UseBazelProxy , "use-bazel-proxy" , false , "communicate with bazel using unix socket proxy instead of spawning subprocesses" )
2023-01-17 21:40:22 +01:00
flag . BoolVar ( & cmdlineArgs . BuildFromTextStub , "build-from-text-stub" , false , "build Java stubs from API text files instead of source files" )
2023-04-19 18:47:36 +02:00
flag . BoolVar ( & cmdlineArgs . EnsureAllowlistIntegrity , "ensure-allowlist-integrity" , false , "verify that allowlisted modules are mixed-built" )
2022-12-13 06:23:34 +01:00
// Flags that probably shouldn't be flags of soong_build, but we haven't found
2021-08-31 10:42:08 +02:00
// the time to remove them yet
2022-12-13 06:23:34 +01:00
flag . BoolVar ( & cmdlineArgs . 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 {
2022-11-08 13:21:15 +01:00
return android . NewNameResolver ( config )
2017-11-30 01:47:17 +01:00
}
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-09-29 08:23:17 +02:00
ctx . SetNameInterface ( newNameResolver ( configuration ) )
ctx . SetAllowMissingDependencies ( configuration . AllowMissingDependencies ( ) )
2022-11-08 19:42:16 +01:00
ctx . AddIncludeTags ( configuration . IncludeTags ( ) ... )
2023-02-21 17:50:29 +01:00
ctx . AddSourceRootDirs ( configuration . SourceRootDirs ( ) ... )
2020-09-29 08:23:17 +02:00
return ctx
}
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.
2022-12-13 23:18:58 +01:00
func runMixedModeBuild ( ctx * android . Context , extraNinjaDeps [ ] string ) string {
2022-05-10 19:50:12 +02:00
ctx . EventHandler . Begin ( "mixed_build" )
defer ctx . EventHandler . End ( "mixed_build" )
bazelHook := func ( ) error {
2022-12-13 23:18:58 +01:00
return ctx . Config ( ) . BazelContext . InvokeBazel ( ctx . Config ( ) , ctx )
2021-04-01 17:55:58 +02:00
}
2022-05-10 19:50:12 +02:00
ctx . SetBeforePrepareBuildActionsHook ( bazelHook )
2022-12-13 23:18:58 +01:00
ninjaDeps := bootstrap . RunBlueprint ( cmdlineArgs . Args , bootstrap . DoEverything , ctx . Context , ctx . Config ( ) )
2022-05-24 21:38:38 +02:00
ninjaDeps = append ( ninjaDeps , extraNinjaDeps ... )
2021-08-18 10:55:32 +02:00
2022-12-13 23:18:58 +01:00
bazelPaths , err := readFileLines ( ctx . Config ( ) . Getenv ( "BAZEL_DEPS_FILE" ) )
2022-10-10 22:07:48 +02:00
if err != nil {
panic ( "Bazel deps file not found: " + err . Error ( ) )
}
ninjaDeps = append ( ninjaDeps , bazelPaths ... )
2022-12-13 23:18:58 +01:00
ninjaDeps = append ( ninjaDeps , writeBuildGlobsNinjaFile ( ctx ) ... )
2021-08-18 10:55:32 +02:00
2022-11-05 11:17:12 +01:00
writeDepFile ( cmdlineArgs . OutFile , ctx . EventHandler , ninjaDeps )
2023-05-08 04:32:52 +02:00
2023-06-01 16:16:41 +02:00
if needToWriteNinjaHint ( ctx ) {
2023-05-08 04:32:52 +02:00
writeNinjaHint ( ctx )
}
2022-11-05 16:28:04 +01:00
return cmdlineArgs . OutFile
2021-04-01 17:55:58 +02:00
}
2023-06-01 16:16:41 +02:00
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
}
2021-04-01 17:55:58 +02:00
// Run the code-generation phase to convert BazelTargetModules to BUILD files.
2022-12-13 23:18:58 +01:00
func runQueryView ( queryviewDir , queryviewMarker string , ctx * android . Context ) {
2022-03-23 00:23:40 +01:00
ctx . EventHandler . Begin ( "queryview" )
defer ctx . EventHandler . End ( "queryview" )
2022-11-09 03:14:01 +01:00
codegenContext := bp2build . NewCodegenContext ( ctx . Config ( ) , ctx , bp2build . QueryView , topDir )
2023-03-10 00:05:47 +01:00
err := createBazelWorkspace ( codegenContext , shared . JoinPath ( topDir , queryviewDir ) , false )
2022-12-13 23:18:58 +01:00
maybeQuit ( err , "" )
2021-09-06 17:08:02 +02:00
touch ( shared . JoinPath ( topDir , queryviewMarker ) )
2021-04-01 17:55:58 +02:00
}
2022-09-28 22:43:08 +02:00
// Run the code-generation phase to convert API contributions to BUILD files.
// Return marker file for the new synthetic workspace
2022-12-13 23:18:58 +01:00
func runApiBp2build ( ctx * android . Context , extraNinjaDeps [ ] string ) string {
2022-09-28 22:43:08 +02:00
ctx . EventHandler . Begin ( "api_bp2build" )
defer ctx . EventHandler . End ( "api_bp2build" )
2023-01-11 04:05:24 +01:00
// api_bp2build does not run the typical pipeline of soong mutators.
// Hoevever, it still runs the defaults mutator which can create dependencies.
// These dependencies might not always exist (e.g. in tests)
ctx . SetAllowMissingDependencies ( ctx . Config ( ) . AllowMissingDependencies ( ) )
2022-09-28 22:43:08 +02:00
ctx . RegisterForApiBazelConversion ( )
// Register the Android.bp files in the tree
// Add them to the workspace's .d file
ctx . SetModuleListFile ( cmdlineArgs . ModuleListFile )
if paths , err := ctx . ListModulePaths ( "." ) ; err == nil {
extraNinjaDeps = append ( extraNinjaDeps , paths ... )
} else {
panic ( err )
}
// Run the loading and analysis phase
2022-12-13 06:23:34 +01:00
ninjaDeps := bootstrap . RunBlueprint ( cmdlineArgs . Args ,
2022-09-28 22:43:08 +02:00
bootstrap . StopBeforePrepareBuildActions ,
ctx . Context ,
2022-12-13 23:18:58 +01:00
ctx . Config ( ) )
2022-09-28 22:43:08 +02:00
ninjaDeps = append ( ninjaDeps , extraNinjaDeps ... )
// Add the globbed dependencies
2022-12-13 23:18:58 +01:00
ninjaDeps = append ( ninjaDeps , writeBuildGlobsNinjaFile ( ctx ) ... )
2022-09-28 22:43:08 +02:00
// Run codegen to generate BUILD files
2022-11-09 03:14:01 +01:00
codegenContext := bp2build . NewCodegenContext ( ctx . Config ( ) , ctx , bp2build . ApiBp2build , topDir )
2022-12-13 06:23:34 +01:00
absoluteApiBp2buildDir := shared . JoinPath ( topDir , cmdlineArgs . BazelApiBp2buildDir )
2023-03-10 00:05:47 +01:00
// Always generate bp2build_all_srcs filegroups in api_bp2build.
// This is necessary to force each Android.bp file to create an equivalent BUILD file
// and prevent package boundray issues.
// e.g.
// Source
// f/b/Android.bp
// java_library{
// name: "foo",
// api: "api/current.txt",
// }
//
// f/b/api/Android.bp <- will cause package boundary issues
//
// Gen
// f/b/BUILD
// java_contribution{
// name: "foo.contribution",
// api: "//f/b/api:current.txt",
// }
//
// If we don't generate f/b/api/BUILD, foo.contribution will be unbuildable.
err := createBazelWorkspace ( codegenContext , absoluteApiBp2buildDir , true )
2022-12-13 23:18:58 +01:00
maybeQuit ( err , "" )
2022-09-28 22:43:08 +02:00
ninjaDeps = append ( ninjaDeps , codegenContext . AdditionalNinjaDeps ( ) ... )
// Create soong_injection repository
2023-02-09 02:43:09 +01:00
soongInjectionFiles , err := bp2build . CreateSoongInjectionDirFiles ( codegenContext , bp2build . CreateCodegenMetrics ( ) )
maybeQuit ( err , "" )
2022-12-13 23:18:58 +01:00
absoluteSoongInjectionDir := shared . JoinPath ( topDir , ctx . Config ( ) . SoongOutDir ( ) , bazel . SoongInjectionDirName )
2022-09-28 22:43:08 +02:00
for _ , file := range soongInjectionFiles {
2022-12-07 02:14:52 +01:00
// The API targets in api_bp2build workspace do not have any dependency on api_bp2build.
// But we need to create these files to prevent errors during Bazel analysis.
// These need to be created in Read-Write mode.
// This is because the subsequent step (bp2build in api domain analysis) creates them in Read-Write mode
// to allow users to edit/experiment in the synthetic workspace.
writeReadWriteFile ( absoluteSoongInjectionDir , file )
2022-09-28 22:43:08 +02:00
}
2022-12-13 23:18:58 +01:00
workspace := shared . JoinPath ( ctx . Config ( ) . SoongOutDir ( ) , "api_bp2build" )
2022-09-28 22:43:08 +02:00
// Create the symlink forest
2022-11-08 20:12:36 +01:00
symlinkDeps , _ , _ := bp2build . PlantSymlinkForest (
2022-12-13 23:18:58 +01:00
ctx . Config ( ) . IsEnvTrue ( "BP2BUILD_VERBOSE" ) ,
2022-09-28 22:43:08 +02:00
topDir ,
workspace ,
2022-12-13 06:23:34 +01:00
cmdlineArgs . BazelApiBp2buildDir ,
2022-12-13 23:18:58 +01:00
apiBuildFileExcludes ( ctx ) )
2022-09-28 22:43:08 +02:00
ninjaDeps = append ( ninjaDeps , symlinkDeps ... )
workspaceMarkerFile := workspace + ".marker"
2022-11-05 11:17:12 +01:00
writeDepFile ( workspaceMarkerFile , ctx . EventHandler , ninjaDeps )
2022-09-28 22:43:08 +02:00
touch ( shared . JoinPath ( topDir , workspaceMarkerFile ) )
return workspaceMarkerFile
}
// With some exceptions, api_bp2build does not have any dependencies on the checked-in BUILD files
// Exclude them from the generated workspace to prevent unrelated errors during the loading phase
2022-12-13 23:18:58 +01:00
func apiBuildFileExcludes ( ctx * android . Context ) [ ] string {
ret := bazelArtifacts ( )
2022-09-28 22:43:08 +02:00
srcs , err := getExistingBazelRelatedFiles ( topDir )
2022-12-13 23:18:58 +01:00
maybeQuit ( err , "Error determining existing Bazel-related files" )
2022-09-28 22:43:08 +02:00
for _ , src := range srcs {
2022-12-13 23:18:58 +01:00
// Exclude all src BUILD files
2022-09-28 22:43:08 +02:00
if src != "WORKSPACE" &&
src != "BUILD" &&
src != "BUILD.bazel" &&
! strings . HasPrefix ( src , "build/bazel" ) &&
2022-11-30 03:17:06 +01:00
! strings . HasPrefix ( src , "external/bazel-skylib" ) &&
2022-09-28 22:43:08 +02:00
! strings . HasPrefix ( src , "prebuilts/clang" ) {
ret = append ( ret , src )
}
}
2022-12-13 23:18:58 +01:00
// Android.bp files for api surfaces are mounted to out/, but out/ should not be a
// dep for api_bp2build. Otherwise, api_bp2build will be run every single time
ret = append ( ret , ctx . Config ( ) . OutDir ( ) )
2022-09-28 22:43:08 +02:00
return ret
}
2023-03-18 16:12:39 +01:00
func writeNinjaHint ( ctx * android . Context ) error {
2023-05-04 11:16:11 +02:00
ctx . BeginEvent ( "ninja_hint" )
defer ctx . EndEvent ( "ninja_hint" )
2023-04-11 07:28:43 +02:00
// The current predictor focuses on reducing false negatives.
// If there are too many false positives (e.g., most modules are marked as positive),
// real long-running jobs cannot run early.
// Therefore, the model should be adjusted in this case.
// The model should also be adjusted if there are critical false negatives.
predicate := func ( j * blueprint . JsonModule ) ( prioritized bool , weight int ) {
prioritized = false
weight = 0
for prefix , w := range allowlists . HugeModuleTypePrefixMap {
if strings . HasPrefix ( j . Type , prefix ) {
prioritized = true
weight = w
return
}
}
dep_count := len ( j . Deps )
src_count := 0
for _ , a := range j . Module [ "Actions" ] . ( [ ] blueprint . JSONAction ) {
src_count += len ( a . Inputs )
}
input_size := dep_count + src_count
// Current threshold is an arbitrary value which only consider recall rather than accuracy.
if input_size > allowlists . INPUT_SIZE_THRESHOLD {
prioritized = true
weight += ( ( input_size ) / allowlists . INPUT_SIZE_THRESHOLD ) * allowlists . DEFAULT_PRIORITIZED_WEIGHT
// To prevent some modules from having too large a priority value.
if weight > allowlists . HIGH_PRIORITIZED_WEIGHT {
weight = allowlists . HIGH_PRIORITIZED_WEIGHT
}
}
return
2023-03-18 16:12:39 +01:00
}
2023-04-11 07:28:43 +02:00
outputsMap := ctx . Context . GetWeightedOutputsFromPredicate ( predicate )
2023-03-18 16:12:39 +01:00
var outputBuilder strings . Builder
2023-04-11 07:28:43 +02:00
for output , weight := range outputsMap {
outputBuilder . WriteString ( fmt . Sprintf ( "%s,%d\n" , output , weight ) )
2023-03-18 16:12:39 +01:00
}
weightListFile := filepath . Join ( topDir , ctx . Config ( ) . OutDir ( ) , ".ninja_weight_list" )
err := os . WriteFile ( weightListFile , [ ] byte ( outputBuilder . String ( ) ) , 0644 )
if err != nil {
return fmt . Errorf ( "could not write ninja weight list file %s" , err )
}
return nil
}
2022-11-05 11:17:12 +01: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 )
2022-12-13 23:18:58 +01:00
maybeQuit ( err , "error writing soong_build metrics %s" , metricsFile )
2021-04-01 17:55:58 +02:00
}
2023-04-19 18:47:36 +02:00
// Errors out if any modules expected to be mixed_built were not, unless
2023-05-02 18:28:55 +02:00
// the modules did not exist.
2023-04-19 18:47:36 +02:00
func checkForAllowlistIntegrityError ( configuration android . Config , isStagingMode bool ) error {
2023-05-02 18:28:55 +02:00
modules := findMisconfiguredModules ( configuration , isStagingMode )
2023-04-19 18:47:36 +02:00
if len ( modules ) == 0 {
return nil
}
return fmt . Errorf ( "Error: expected the following modules to be mixed_built: %s" , modules )
}
2023-05-02 18:28:55 +02:00
// Returns true if the given module has all of the following true:
// 1. Is allowlisted to be built with Bazel.
// 2. Has a variant which is *not* built with Bazel.
// 3. Has no variant which is built with Bazel.
//
// This indicates the allowlisting of this variant had no effect.
// TODO(b/280457637): Return true for nonexistent modules.
func isAllowlistMisconfiguredForModule ( module string , mixedBuildsEnabled map [ string ] struct { } , mixedBuildsDisabled map [ string ] struct { } ) bool {
_ , enabled := mixedBuildsEnabled [ module ]
if enabled {
return false
}
_ , disabled := mixedBuildsDisabled [ module ]
return disabled
}
2023-04-19 18:47:36 +02:00
// Returns the list of modules that should have been mixed_built (per the
// allowlists and cmdline flags) but were not.
2023-05-02 18:28:55 +02:00
// Note: nonexistent modules are excluded from the list. See b/280457637
func findMisconfiguredModules ( configuration android . Config , isStagingMode bool ) [ ] string {
2023-04-19 18:47:36 +02:00
retval := [ ] string { }
forceEnabledModules := configuration . BazelModulesForceEnabledByFlag ( )
mixedBuildsEnabled := configuration . GetMixedBuildsEnabledModules ( )
2023-05-02 18:28:55 +02:00
mixedBuildsDisabled := configuration . GetMixedBuildsDisabledModules ( )
2023-04-19 18:47:36 +02:00
for _ , module := range allowlists . ProdMixedBuildsEnabledList {
2023-05-02 18:28:55 +02:00
if isAllowlistMisconfiguredForModule ( module , mixedBuildsEnabled , mixedBuildsDisabled ) {
2023-04-19 18:47:36 +02:00
retval = append ( retval , module )
}
}
if isStagingMode {
for _ , module := range allowlists . StagingMixedBuildsEnabledList {
2023-05-02 18:28:55 +02:00
if isAllowlistMisconfiguredForModule ( module , mixedBuildsEnabled , mixedBuildsDisabled ) {
2023-04-19 18:47:36 +02:00
retval = append ( retval , module )
}
}
}
for module , _ := range forceEnabledModules {
2023-05-02 18:28:55 +02:00
if isAllowlistMisconfiguredForModule ( module , mixedBuildsEnabled , mixedBuildsDisabled ) {
2023-04-19 18:47:36 +02:00
retval = append ( retval , module )
}
}
return retval
}
2022-12-13 06:23:34 +01:00
func writeJsonModuleGraphAndActions ( ctx * android . Context , cmdArgs android . CmdArgs ) {
graphFile , graphErr := os . Create ( shared . JoinPath ( topDir , cmdArgs . ModuleGraphFile ) )
2022-12-13 23:18:58 +01:00
maybeQuit ( graphErr , "graph err" )
2022-01-25 06:50:25 +01:00
defer graphFile . Close ( )
2022-12-13 23:18:58 +01:00
actionsFile , actionsErr := os . Create ( shared . JoinPath ( topDir , cmdArgs . ModuleActionsFile ) )
maybeQuit ( actionsErr , "actions err" )
2022-01-25 06:50:25 +01:00
defer actionsFile . Close ( )
ctx . Context . PrintJSONGraphAndActions ( graphFile , actionsFile )
2021-04-01 18:28:45 +02:00
}
2022-12-13 23:18:58 +01:00
func writeBuildGlobsNinjaFile ( ctx * android . Context ) [ ] string {
2022-03-23 00:23:40 +01:00
ctx . EventHandler . Begin ( "globs_ninja_file" )
defer ctx . EventHandler . End ( "globs_ninja_file" )
2022-12-13 23:18:58 +01:00
globDir := bootstrap . GlobDirectory ( ctx . Config ( ) . SoongOutDir ( ) , globListDir )
2021-08-18 10:55:32 +02:00
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 ( ) ,
2022-12-13 23:18:58 +01:00
} , ctx . Config ( ) )
2021-08-18 10:55:32 +02:00
return bootstrap . GlobFileListFiles ( globDir )
}
2022-11-05 11:17:12 +01:00
func writeDepFile ( outputFile string , eventHandler * metrics . EventHandler , ninjaDeps [ ] string ) {
2022-03-23 00:23:40 +01:00
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 )
2022-12-13 23:18:58 +01:00
maybeQuit ( err , "error writing depfile '%s'" , depFile )
2022-11-05 16:28:04 +01:00
}
2022-03-23 00:23:40 +01:00
2022-11-05 16:28:04 +01:00
// runSoongOnlyBuild runs the standard Soong build in a number of different modes.
2022-12-13 23:18:58 +01:00
func runSoongOnlyBuild ( ctx * android . Context , extraNinjaDeps [ ] string ) string {
2022-11-05 15:59:52 +01:00
ctx . EventHandler . Begin ( "soong_build" )
defer ctx . EventHandler . End ( "soong_build" )
2022-11-05 16:28:04 +01:00
var stopBefore bootstrap . StopBefore
2022-12-13 23:18:58 +01:00
switch ctx . Config ( ) . BuildMode {
case android . GenerateModuleGraph :
2022-11-05 16:28:04 +01:00
stopBefore = bootstrap . StopBeforeWriteNinja
2022-12-21 17:46:28 +01:00
case android . GenerateQueryView , android . GenerateDocFile :
2022-11-05 16:28:04 +01:00
stopBefore = bootstrap . StopBeforePrepareBuildActions
2022-12-13 23:18:58 +01:00
default :
2022-11-05 16:28:04 +01:00
stopBefore = bootstrap . DoEverything
2021-04-01 17:55:58 +02:00
}
2022-12-13 23:18:58 +01:00
ninjaDeps := bootstrap . RunBlueprint ( cmdlineArgs . Args , stopBefore , ctx . Context , ctx . Config ( ) )
2022-11-05 16:28:04 +01:00
ninjaDeps = append ( ninjaDeps , extraNinjaDeps ... )
2022-12-13 23:18:58 +01:00
globListFiles := writeBuildGlobsNinjaFile ( ctx )
2022-11-05 16:28:04 +01:00
ninjaDeps = append ( ninjaDeps , globListFiles ... )
// Convert the Soong module graph into Bazel BUILD files.
2022-12-13 23:18:58 +01:00
switch ctx . Config ( ) . BuildMode {
case android . GenerateQueryView :
2022-12-13 06:23:34 +01:00
queryviewMarkerFile := cmdlineArgs . BazelQueryViewDir + ".marker"
2022-12-13 23:18:58 +01:00
runQueryView ( cmdlineArgs . BazelQueryViewDir , queryviewMarkerFile , ctx )
2022-11-05 16:28:04 +01:00
writeDepFile ( queryviewMarkerFile , ctx . EventHandler , ninjaDeps )
return queryviewMarkerFile
2022-12-13 23:18:58 +01:00
case android . GenerateModuleGraph :
2022-12-13 06:23:34 +01:00
writeJsonModuleGraphAndActions ( ctx , cmdlineArgs )
writeDepFile ( cmdlineArgs . ModuleGraphFile , ctx . EventHandler , ninjaDeps )
return cmdlineArgs . ModuleGraphFile
2022-12-13 23:18:58 +01:00
case android . GenerateDocFile :
2022-11-05 16:28:04 +01: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.
2022-12-13 23:18:58 +01:00
err := writeDocs ( ctx , shared . JoinPath ( topDir , cmdlineArgs . DocFile ) )
maybeQuit ( err , "error building Soong documentation" )
2022-12-13 06:23:34 +01:00
writeDepFile ( cmdlineArgs . DocFile , ctx . EventHandler , ninjaDeps )
return cmdlineArgs . DocFile
2022-12-13 23:18:58 +01:00
default :
2022-11-05 16:28:04 +01:00
// The actual output (build.ninja) was written in the RunBlueprint() call
// above
writeDepFile ( cmdlineArgs . OutFile , ctx . EventHandler , ninjaDeps )
2023-06-01 16:16:41 +02:00
if needToWriteNinjaHint ( ctx ) {
2023-05-08 04:32:52 +02:00
writeNinjaHint ( ctx )
}
2022-11-05 17:14:30 +01:00
return cmdlineArgs . OutFile
2022-11-05 16:28:04 +01:00
}
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 ) )
2022-12-13 23:18:58 +01:00
maybeQuit ( err , "error reading available environment file '%s'" , availableEnvFile )
2021-04-14 10:31:00 +02:00
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 ( )
2022-12-13 06:23:34 +01:00
configuration , err := android . NewConfig ( cmdlineArgs , availableEnv )
2022-12-13 23:18:58 +01:00
maybeQuit ( err , "" )
2021-03-16 08:55:23 +01:00
if configuration . Getenv ( "ALLOW_MISSING_DEPENDENCIES" ) == "true" {
configuration . SetAllowMissingDependencies ( )
}
2022-12-13 06:23:34 +01:00
extraNinjaDeps := [ ] string { configuration . ProductVariablesFileName , usedEnvFile }
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.
2022-12-13 23:18:58 +01:00
metricsDir := availableEnv [ "LOG_DIR" ]
2022-04-21 08:11:43 +02:00
2022-06-08 02:16:08 +02:00
ctx := newContext ( configuration )
2022-12-13 23:18:58 +01:00
var finalOutputFile string
2022-06-08 02:16:08 +02:00
2023-04-19 06:13:45 +02:00
writeSymlink := false
2022-12-13 23:18:58 +01:00
// Run Soong for a specific activity, like bp2build, queryview
// or the actual Soong build for the build.ninja file.
switch configuration . BuildMode {
case android . SymlinkForest :
finalOutputFile = runSymlinkForestCreation ( ctx , extraNinjaDeps , metricsDir )
case android . Bp2build :
// Run the alternate pipeline of bp2build mutators and singleton to convert
// Blueprint to BUILD files before everything else.
finalOutputFile = runBp2Build ( ctx , extraNinjaDeps , metricsDir )
case android . ApiBp2build :
finalOutputFile = runApiBp2build ( ctx , extraNinjaDeps )
writeMetrics ( configuration , ctx . EventHandler , metricsDir )
default :
ctx . Register ( )
2023-04-19 18:47:36 +02:00
isMixedBuildsEnabled := configuration . IsMixedBuildsEnabled ( )
if isMixedBuildsEnabled {
2022-12-13 23:18:58 +01:00
finalOutputFile = runMixedModeBuild ( ctx , extraNinjaDeps )
2023-04-19 18:47:36 +02:00
if cmdlineArgs . EnsureAllowlistIntegrity {
if err := checkForAllowlistIntegrityError ( configuration , cmdlineArgs . BazelModeStaging ) ; err != nil {
maybeQuit ( err , "" )
}
}
2023-04-19 06:13:45 +02:00
writeSymlink = true
2022-12-13 23:18:58 +01:00
} else {
finalOutputFile = runSoongOnlyBuild ( ctx , extraNinjaDeps )
2023-04-19 06:13:45 +02:00
if configuration . BuildMode == android . AnalysisNoBazel {
writeSymlink = true
}
2022-12-13 23:18:58 +01:00
}
writeMetrics ( configuration , ctx . EventHandler , metricsDir )
}
2023-05-31 20:41:41 +02:00
// Register this environment variablesas being an implicit dependencies of
// soong_build. Changes to this environment variable will result in
// retriggering soong_build.
configuration . Getenv ( "USE_BAZEL_VERSION" )
2023-05-10 23:10:08 +02:00
writeUsedEnvironmentFile ( configuration )
// Touch the output file so that it's the newest file created by soong_build.
// This is necessary because, if soong_build generated any files which
// are ninja inputs to the main output file, then ninja would superfluously
// rebuild this output file on the next build invocation.
touch ( shared . JoinPath ( topDir , finalOutputFile ) )
2023-04-19 06:13:45 +02:00
// TODO(b/277029044): Remove this function once build.<product>.ninja lands
if writeSymlink {
writeBuildNinjaSymlink ( configuration , finalOutputFile )
}
}
// TODO(b/277029044): Remove this function once build.<product>.ninja lands
func writeBuildNinjaSymlink ( config android . Config , source string ) {
targetPath := shared . JoinPath ( topDir , config . SoongOutDir ( ) , "build.ninja" )
sourcePath := shared . JoinPath ( topDir , source )
if targetPath == sourcePath {
return
}
os . Remove ( targetPath )
os . Symlink ( sourcePath , targetPath )
2021-03-23 11:46:47 +01:00
}
2023-05-10 23:10:08 +02:00
func writeUsedEnvironmentFile ( configuration android . Config ) {
2021-04-14 10:31:00 +02:00
if usedEnvFile == "" {
return
}
path := shared . JoinPath ( topDir , usedEnvFile )
2021-03-23 11:46:47 +01:00
data , err := shared . EnvFileContents ( configuration . EnvDeps ( ) )
2022-12-13 23:18:58 +01:00
maybeQuit ( err , "error writing used environment file '%s'\n" , usedEnvFile )
2021-03-23 11:46:47 +01:00
2022-10-24 17:33:09 +02:00
if preexistingData , err := os . ReadFile ( path ) ; err != nil {
if ! os . IsNotExist ( err ) {
2022-12-13 23:18:58 +01:00
maybeQuit ( err , "error reading used environment file '%s'" , usedEnvFile )
2022-10-24 17:33:09 +02:00
}
} else if bytes . Equal ( preexistingData , data ) {
// used environment file is unchanged
return
}
2022-12-13 23:18:58 +01:00
err = os . WriteFile ( path , data , 0666 )
maybeQuit ( err , "error writing used environment file '%s'" , usedEnvFile )
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 )
2022-12-13 23:18:58 +01:00
maybeQuit ( err , "Error touching '%s'" , path )
2021-04-14 10:31:00 +02:00
err = f . Close ( )
2022-12-13 23:18:58 +01:00
maybeQuit ( err , "Error touching '%s'" , path )
2021-04-14 10:31:00 +02:00
currentTime := time . Now ( ) . Local ( )
err = os . Chtimes ( path , currentTime , currentTime )
2022-12-13 23:18:58 +01:00
maybeQuit ( err , "error touching '%s'" , path )
2021-05-13 03:20:13 +02:00
}
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 )
}
2022-12-13 23:18:58 +01:00
return readFileLines ( bazelFinderFile )
2021-05-21 01:34:50 +02:00
}
2022-09-28 22:43:08 +02:00
func bazelArtifacts ( ) [ ] string {
return [ ] string {
"bazel-bin" ,
"bazel-genfiles" ,
"bazel-out" ,
"bazel-testlogs" ,
reduce forest generation to be incremental
Previously, symlink forest generation involved removing the entire
symlink forest and recreating it from scratch. With this change,
a) symlinks which need not change are untouched,
b) symlinks pointing to the wrong location are fixed, and
c) symlinks which should no longer exist are removed.
On AOSP on my local machine, this reduces the symlink forest generation
step from 2.5s to 1.1s clean, and 0.6s when a single file is added to
a source directory.
Bug: 257528847
Test: m bp2build, touch `fakefile` under the forest, remove a file
from the source tree, rerun m bp2build. Manually verify the new forest
does not retain the link to the deleted source file, and that fakefile
no longer exists in the forest.
Change-Id: I481371ae487e9419af6a3a4370c552578b07d650
2022-11-09 00:31:14 +01:00
"bazel-workspace" ,
2022-09-28 22:43:08 +02:00
"bazel-" + filepath . Base ( topDir ) ,
}
}
2022-10-26 09:26:50 +02:00
// This could in theory easily be separated into a binary that generically
// merges two directories into a symlink tree. The main obstacle is that this
// function currently depends on both Bazel-specific knowledge (the existence
// of bazel-* symlinks) and configuration (the set of BUILD.bazel files that
// should and should not be kept)
//
// Ideally, bp2build would write a file that contains instructions to the
// symlink tree creation binary. Then the latter would not need to depend on
// the very heavy-weight machinery of soong_build .
2022-12-13 23:18:58 +01:00
func runSymlinkForestCreation ( ctx * android . Context , extraNinjaDeps [ ] string , metricsDir string ) string {
2022-11-08 20:12:36 +01:00
var ninjaDeps [ ] string
var mkdirCount , symlinkCount uint64
2022-12-01 06:55:35 +01:00
ctx . EventHandler . Do ( "symlink_forest" , func ( ) {
ninjaDeps = append ( ninjaDeps , extraNinjaDeps ... )
2022-12-13 23:18:58 +01:00
verbose := ctx . Config ( ) . IsEnvTrue ( "BP2BUILD_VERBOSE" )
2022-12-01 06:55:35 +01:00
// PlantSymlinkForest() returns all the directories that were readdir()'ed.
// Such a directory SHOULD be added to `ninjaDeps` so that a child directory
// or file created/deleted under it would trigger an update of the symlink forest.
2022-12-13 23:18:58 +01:00
generatedRoot := shared . JoinPath ( ctx . Config ( ) . SoongOutDir ( ) , "bp2build" )
workspaceRoot := shared . JoinPath ( ctx . Config ( ) . SoongOutDir ( ) , "workspace" )
2022-11-08 20:12:36 +01:00
var symlinkForestDeps [ ] string
2022-12-01 06:55:35 +01:00
ctx . EventHandler . Do ( "plant" , func ( ) {
2022-11-08 20:12:36 +01:00
symlinkForestDeps , mkdirCount , symlinkCount = bp2build . PlantSymlinkForest (
2022-12-13 23:18:58 +01:00
verbose , topDir , workspaceRoot , generatedRoot , excludedFromSymlinkForest ( ctx , verbose ) )
2022-12-01 06:55:35 +01:00
} )
2022-11-08 20:12:36 +01:00
ninjaDeps = append ( ninjaDeps , symlinkForestDeps ... )
2022-10-26 09:26:50 +02:00
} )
2022-11-08 20:12:36 +01:00
writeDepFile ( cmdlineArgs . SymlinkForestMarker , ctx . EventHandler , ninjaDeps )
touch ( shared . JoinPath ( topDir , cmdlineArgs . SymlinkForestMarker ) )
2022-10-29 05:32:01 +02:00
codegenMetrics := bp2build . ReadCodegenMetrics ( metricsDir )
if codegenMetrics == nil {
m := bp2build . CreateCodegenMetrics ( )
codegenMetrics = & m
} else {
//TODO (usta) we cannot determine if we loaded a stale file, i.e. from an unrelated prior
//invocation of codegen. We should simply use a separate .pb file
}
2022-11-08 20:12:36 +01:00
codegenMetrics . SetSymlinkCount ( symlinkCount )
codegenMetrics . SetMkDirCount ( mkdirCount )
2022-11-04 14:35:50 +01:00
writeBp2BuildMetrics ( codegenMetrics , ctx . EventHandler , metricsDir )
2022-12-13 06:23:34 +01:00
return cmdlineArgs . SymlinkForestMarker
2022-10-26 09:26:50 +02:00
}
2022-12-13 23:18:58 +01:00
func excludedFromSymlinkForest ( ctx * android . Context , verbose bool ) [ ] string {
excluded := bazelArtifacts ( )
if cmdlineArgs . OutDir [ 0 ] != '/' {
excluded = append ( excluded , cmdlineArgs . OutDir )
}
// Find BUILD files in the srcDir which are not in the allowlist
// (android.Bp2BuildConversionAllowlist#ShouldKeepExistingBuildFileForDir)
// and return their paths so they can be left out of the Bazel workspace dir (i.e. ignored)
existingBazelFiles , err := getExistingBazelRelatedFiles ( topDir )
maybeQuit ( err , "Error determining existing Bazel-related files" )
for _ , path := range existingBazelFiles {
fullPath := shared . JoinPath ( topDir , path )
fileInfo , err2 := os . Stat ( fullPath )
if err2 != nil {
// Warn about error, but continue trying to check files
fmt . Fprintf ( os . Stderr , "WARNING: Error accessing path '%s', err: %s\n" , fullPath , err2 )
continue
}
// Exclude only files named 'BUILD' or 'BUILD.bazel' and unless forcibly kept
if fileInfo . IsDir ( ) ||
( fileInfo . Name ( ) != "BUILD" && fileInfo . Name ( ) != "BUILD.bazel" ) ||
ctx . Config ( ) . Bp2buildPackageConfig . ShouldKeepExistingBuildFileForDir ( filepath . Dir ( path ) ) {
// Don't ignore this existing build file
continue
}
if verbose {
fmt . Fprintf ( os . Stderr , "Ignoring existing BUILD file: %s\n" , path )
}
excluded = append ( excluded , path )
}
// Temporarily exclude stuff to make `bazel build //external/...` (and `bazel build //frameworks/...`) work
excluded = append ( excluded ,
// FIXME: 'autotest_lib' is a symlink back to external/autotest, and this causes an infinite
// symlink expansion error for Bazel
"external/autotest/venv/autotest_lib" ,
"external/autotest/autotest_lib" ,
"external/autotest/client/autotest_lib/client" ,
// 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
"external/google-fruit/extras/bazel_root/third_party/fruit" ,
// FIXME: 'frameworks/compile/slang' has a filegroup error due to an escaping issue
"frameworks/compile/slang" ,
// FIXME(b/260809113): 'prebuilts/clang/host/linux-x86/clang-dev' is a tool-generated symlink
// directory that contains a BUILD file. The bazel files finder code doesn't traverse into symlink dirs,
// and hence is not aware of this BUILD file and exclude it accordingly during symlink forest generation
// when checking against keepExistingBuildFiles allowlist.
//
// This is necessary because globs in //prebuilts/clang/host/linux-x86/BUILD
// currently assume no subpackages (keepExistingBuildFile is not recursive for that directory).
//
// This is a bandaid until we the symlink forest logic can intelligently exclude BUILD files found in
// source symlink dirs according to the keepExistingBuildFile allowlist.
"prebuilts/clang/host/linux-x86/clang-dev" ,
)
return excluded
}
2023-06-06 18:17:50 +02:00
// buildTargetsByPackage parses Bazel BUILD.bazel and BUILD files under
// the workspace, and returns a map containing names of Bazel targets defined in
// these BUILD files.
// For example, maps "//foo/bar" to ["baz", "qux"] if `//foo/bar:{baz,qux}` exist.
func buildTargetsByPackage ( ctx * android . Context ) map [ string ] [ ] string {
existingBazelFiles , err := getExistingBazelRelatedFiles ( topDir )
maybeQuit ( err , "Error determining existing Bazel-related files" )
result := map [ string ] [ ] string { }
// Search for instances of `name = "$NAME"` (with arbitrary spacing).
targetNameRegex := regexp . MustCompile ( ` (?m)^\s*name\s*=\s*\"([^\"]+)\" ` )
for _ , path := range existingBazelFiles {
if ! ctx . Config ( ) . Bp2buildPackageConfig . ShouldKeepExistingBuildFileForDir ( filepath . Dir ( path ) ) {
continue
}
fullPath := shared . JoinPath ( topDir , path )
sourceDir := filepath . Dir ( path )
fileInfo , err := os . Stat ( fullPath )
maybeQuit ( err , "Error accessing Bazel file '%s'" , fullPath )
if ! fileInfo . IsDir ( ) &&
( fileInfo . Name ( ) == "BUILD" || fileInfo . Name ( ) == "BUILD.bazel" ) {
// Process this BUILD file.
buildFileContent , err := os . ReadFile ( fullPath )
maybeQuit ( err , "Error reading Bazel file '%s'" , fullPath )
matches := targetNameRegex . FindAllStringSubmatch ( string ( buildFileContent ) , - 1 )
for _ , match := range matches {
result [ sourceDir ] = append ( result [ sourceDir ] , match [ 1 ] )
}
}
}
return result
}
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.
2022-12-13 23:18:58 +01:00
func runBp2Build ( ctx * android . Context , extraNinjaDeps [ ] string , metricsDir string ) string {
2022-10-29 05:32:01 +02:00
var codegenMetrics * bp2build . CodegenMetrics
2022-11-04 14:35:50 +01:00
ctx . EventHandler . Do ( "bp2build" , func ( ) {
2022-05-26 18:22:17 +02:00
2023-06-06 18:17:50 +02:00
ctx . EventHandler . Do ( "read_build" , func ( ) {
ctx . Config ( ) . SetBazelBuildFileTargets ( buildTargetsByPackage ( ctx ) )
} )
2022-05-26 18:22:17 +02:00
// Propagate "allow misssing dependencies" bit. This is normally set in
2022-11-04 14:35:50 +01:00
// newContext(), but we create ctx without calling that method.
2022-12-13 23:18:58 +01:00
ctx . SetAllowMissingDependencies ( ctx . Config ( ) . AllowMissingDependencies ( ) )
ctx . SetNameInterface ( newNameResolver ( ctx . Config ( ) ) )
2022-11-04 14:35:50 +01:00
ctx . RegisterForBazelConversion ( )
ctx . SetModuleListFile ( cmdlineArgs . ModuleListFile )
2023-06-13 03:10:20 +02:00
// Skip cloning modules during bp2build's blueprint run. Some mutators set
// bp2build-related module values which should be preserved during codegen.
ctx . SkipCloneModulesAfterMutators = true
2022-05-26 18:22:17 +02:00
2022-10-04 18:13:25 +02:00
var ninjaDeps [ ] string
ninjaDeps = append ( ninjaDeps , extraNinjaDeps ... )
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.
2022-11-04 14:35:50 +01:00
ctx . EventHandler . Do ( "bootstrap" , func ( ) {
2022-10-04 18:13:25 +02:00
blueprintArgs := cmdlineArgs
2022-12-13 23:18:58 +01:00
bootstrapDeps := bootstrap . RunBlueprint ( blueprintArgs . Args ,
bootstrap . StopBeforePrepareBuildActions , ctx . Context , ctx . Config ( ) )
2022-10-04 18:13:25 +02:00
ninjaDeps = append ( ninjaDeps , bootstrapDeps ... )
} )
2021-04-14 13:49:50 +02:00
2022-12-13 23:18:58 +01:00
globListFiles := writeBuildGlobsNinjaFile ( ctx )
2022-05-26 18:22:17 +02:00
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
2022-10-04 18:13:25 +02:00
// and print conversion codegenMetrics to the user.
2022-11-09 03:14:01 +01:00
codegenContext := bp2build . NewCodegenContext ( ctx . Config ( ) , ctx , bp2build . Bp2Build , topDir )
2022-11-04 14:35:50 +01:00
ctx . EventHandler . Do ( "codegen" , func ( ) {
2022-10-04 18:13:25 +02:00
codegenMetrics = bp2build . Codegen ( codegenContext )
} )
2021-02-19 06:48:40 +01:00
2022-05-26 18:22:17 +02:00
ninjaDeps = append ( ninjaDeps , codegenContext . AdditionalNinjaDeps ( ) ... )
2021-04-16 13:47:36 +02:00
2022-12-13 06:23:34 +01:00
writeDepFile ( cmdlineArgs . Bp2buildMarker , ctx . EventHandler , ninjaDeps )
touch ( shared . JoinPath ( topDir , cmdlineArgs . Bp2buildMarker ) )
2022-05-26 18:22:17 +02:00
} )
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-12-13 23:18:58 +01:00
if ctx . Config ( ) . IsEnvTrue ( "BP2BUILD_VERBOSE" ) {
2022-10-04 18:13:25 +02:00
codegenMetrics . Print ( )
2022-05-20 04:34:31 +02:00
}
2022-11-04 14:35:50 +01:00
writeBp2BuildMetrics ( codegenMetrics , ctx . EventHandler , metricsDir )
2022-12-13 06:23:34 +01:00
return cmdlineArgs . Bp2buildMarker
2020-12-14 08:58:54 +01:00
}
2021-11-10 15:55:20 +01:00
// Write Bp2Build metrics into $LOG_DIR
2022-11-05 15:21:16 +01:00
func writeBp2BuildMetrics ( codegenMetrics * bp2build . CodegenMetrics , eventHandler * metrics . EventHandler , metricsDir string ) {
2022-03-23 00:23:40 +01:00
for _ , event := range eventHandler . CompletedEvents ( ) {
2022-10-29 05:32:01 +02:00
codegenMetrics . AddEvent ( & bp2build_metrics_proto . Event {
Name : event . Id ,
StartTime : uint64 ( event . Start . UnixNano ( ) ) ,
RealTime : event . RuntimeNanoseconds ( ) ,
} )
2022-03-23 00:23:40 +01:00
}
2021-11-10 15:55:20 +01:00
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
}
2022-10-10 22:07:48 +02:00
2022-12-13 23:18:58 +01:00
func readFileLines ( path string ) ( [ ] string , error ) {
data , err := os . ReadFile ( path )
if err == nil {
return strings . Split ( strings . TrimSpace ( string ( data ) ) , "\n" ) , nil
}
return nil , err
2022-10-10 22:07:48 +02:00
2022-12-13 23:18:58 +01:00
}
func maybeQuit ( err error , format string , args ... interface { } ) {
if err == nil {
return
}
if format != "" {
fmt . Fprintln ( os . Stderr , fmt . Sprintf ( format , args ... ) + ": " + err . Error ( ) )
} else {
fmt . Fprintln ( os . Stderr , err )
2022-10-10 22:07:48 +02:00
}
2022-12-13 23:18:58 +01:00
os . Exit ( 1 )
2022-10-10 22:07:48 +02:00
}