2020-11-26 01:06:39 +01:00
|
|
|
// Copyright 2020 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 bp2build
|
|
|
|
|
|
|
|
import (
|
2020-12-14 08:58:54 +01:00
|
|
|
"fmt"
|
2020-11-26 01:06:39 +01:00
|
|
|
"os"
|
2023-02-09 23:54:00 +01:00
|
|
|
"path/filepath"
|
2021-08-26 14:37:59 +02:00
|
|
|
"strings"
|
2021-10-14 20:08:38 +02:00
|
|
|
|
|
|
|
"android/soong/android"
|
|
|
|
"android/soong/bazel"
|
2023-02-09 23:54:00 +01:00
|
|
|
"android/soong/shared"
|
2023-07-01 17:40:36 +02:00
|
|
|
"android/soong/starlark_import"
|
2020-11-26 01:06:39 +01:00
|
|
|
)
|
|
|
|
|
2023-02-09 23:54:00 +01:00
|
|
|
func deleteFilesExcept(ctx *CodegenContext, rootOutputPath android.OutputPath, except []BazelFile) {
|
|
|
|
// Delete files that should no longer be present.
|
|
|
|
bp2buildDirAbs := shared.JoinPath(ctx.topDir, rootOutputPath.String())
|
|
|
|
|
|
|
|
filesToDelete := make(map[string]struct{})
|
|
|
|
err := filepath.Walk(bp2buildDirAbs,
|
|
|
|
func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
relPath, err := filepath.Rel(bp2buildDirAbs, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
filesToDelete[relPath] = struct{}{}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERROR reading %s: %s", bp2buildDirAbs, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, bazelFile := range except {
|
|
|
|
filePath := filepath.Join(bazelFile.Dir, bazelFile.Basename)
|
|
|
|
delete(filesToDelete, filePath)
|
|
|
|
}
|
|
|
|
for f, _ := range filesToDelete {
|
|
|
|
absPath := shared.JoinPath(bp2buildDirAbs, f)
|
|
|
|
if err := os.RemoveAll(absPath); err != nil {
|
|
|
|
fmt.Printf("ERROR deleting %s: %s", absPath, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 08:05:59 +01:00
|
|
|
// Codegen is the backend of bp2build. The code generator is responsible for
|
|
|
|
// writing .bzl files that are equivalent to Android.bp files that are capable
|
|
|
|
// of being built with Bazel.
|
2022-10-29 05:32:01 +02:00
|
|
|
func Codegen(ctx *CodegenContext) *CodegenMetrics {
|
2023-07-01 17:40:36 +02:00
|
|
|
ctx.Context().BeginEvent("Codegen")
|
|
|
|
defer ctx.Context().EndEvent("Codegen")
|
2021-05-06 15:31:18 +02:00
|
|
|
// This directory stores BUILD files that could be eventually checked-in.
|
|
|
|
bp2buildDir := android.PathForOutput(ctx, "bp2build")
|
2020-11-26 01:06:39 +01:00
|
|
|
|
2021-08-26 14:37:59 +02:00
|
|
|
res, errs := GenerateBazelTargets(ctx, true)
|
|
|
|
if len(errs) > 0 {
|
|
|
|
errMsgs := make([]string, len(errs))
|
|
|
|
for i, err := range errs {
|
|
|
|
errMsgs[i] = fmt.Sprintf("%q", err)
|
|
|
|
}
|
|
|
|
fmt.Printf("ERROR: Encountered %d error(s): \nERROR: %s", len(errs), strings.Join(errMsgs, "\n"))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-07-01 17:40:36 +02:00
|
|
|
var bp2buildFiles []BazelFile
|
2023-09-12 19:07:07 +02:00
|
|
|
productConfig, err := createProductConfigFiles(ctx, res.metrics)
|
2023-07-01 17:40:36 +02:00
|
|
|
ctx.Context().EventHandler.Do("CreateBazelFile", func() {
|
2023-09-12 19:07:07 +02:00
|
|
|
allTargets := make(map[string]BazelTargets)
|
|
|
|
for k, v := range res.buildFileToTargets {
|
|
|
|
allTargets[k] = append(allTargets[k], v...)
|
|
|
|
}
|
|
|
|
for k, v := range productConfig.bp2buildTargets {
|
|
|
|
allTargets[k] = append(allTargets[k], v...)
|
|
|
|
}
|
|
|
|
bp2buildFiles = CreateBazelFiles(nil, allTargets, ctx.mode)
|
2023-07-01 17:40:36 +02:00
|
|
|
})
|
2023-09-12 19:07:07 +02:00
|
|
|
bp2buildFiles = append(bp2buildFiles, productConfig.bp2buildFiles...)
|
|
|
|
injectionFiles, err := createSoongInjectionDirFiles(ctx, res.metrics)
|
2023-04-22 02:37:11 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%s\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-09-12 19:07:07 +02:00
|
|
|
injectionFiles = append(injectionFiles, productConfig.injectionFiles...)
|
|
|
|
|
2021-05-06 15:31:18 +02:00
|
|
|
writeFiles(ctx, bp2buildDir, bp2buildFiles)
|
2023-02-09 23:54:00 +01:00
|
|
|
// Delete files under the bp2build root which weren't just written. An
|
|
|
|
// alternative would have been to delete the whole directory and write these
|
|
|
|
// files. However, this would regenerate files which were otherwise unchanged
|
|
|
|
// since the last bp2build run, which would have negative incremental
|
|
|
|
// performance implications.
|
|
|
|
deleteFilesExcept(ctx, bp2buildDir, bp2buildFiles)
|
2020-11-26 01:06:39 +01:00
|
|
|
|
2023-02-09 02:43:09 +01:00
|
|
|
writeFiles(ctx, android.PathForOutput(ctx, bazel.SoongInjectionDirName), injectionFiles)
|
Load starlark files from soong
There are a number of instances where we are exporting information
from soong to bazel via soong_injection. This could be more bazel-centric
if the information was instead held in bzl files, and both bazel and
soong read it from there.
Add a starlark package that will run
//build/bazel/constants_exported_to_soong.bzl at initialization time,
and then results can be retreived with GetStarlarkValue.
Since changes to the starlark files mean that soong has to rerun,
add them as ninja deps.
Unfortunately, the starlark code has to be run at runtime rather than
pregenerating their results, because tests run from intellij wouldn't
go through any pregeneration steps. This means that starlark is run
multiple times during the build, once per test package and once per
primary builder invocation. (currently 3, could be reduced to 2 if we
made the symlink forest generation into its own standalone tool) The
starlark code we have so far in this cl is very fast, roughly half a
millisecond, so it's not a big deal for now, but something to keep an
eye on as we add more starlark constants.
Bug: 279095899
Test: go test
Change-Id: I1e7ca1df1d8d67333cbfc46e8396e229820e4476
2023-02-07 20:38:27 +01:00
|
|
|
starlarkDeps, err := starlark_import.GetNinjaDeps()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
ctx.AddNinjaFileDeps(starlarkDeps...)
|
2023-01-11 03:50:00 +01:00
|
|
|
return &res.metrics
|
|
|
|
}
|
|
|
|
|
2021-03-10 08:05:59 +01:00
|
|
|
// Get the output directory and create it if it doesn't exist.
|
|
|
|
func getOrCreateOutputDir(outputDir android.OutputPath, ctx android.PathContext, dir string) android.OutputPath {
|
|
|
|
dirPath := outputDir.Join(ctx, dir)
|
2022-07-11 17:29:56 +02:00
|
|
|
if err := android.CreateOutputDirIfNonexistent(dirPath, os.ModePerm); err != nil {
|
|
|
|
fmt.Printf("ERROR: path %s: %s", dirPath, err.Error())
|
|
|
|
}
|
2021-03-10 08:05:59 +01:00
|
|
|
return dirPath
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|
|
|
|
|
2021-05-06 15:31:18 +02:00
|
|
|
// writeFiles materializes a list of BazelFile rooted at outputDir.
|
|
|
|
func writeFiles(ctx android.PathContext, outputDir android.OutputPath, files []BazelFile) {
|
|
|
|
for _, f := range files {
|
|
|
|
p := getOrCreateOutputDir(outputDir, ctx, f.Dir).Join(ctx, f.Basename)
|
2022-07-11 17:29:56 +02:00
|
|
|
if err := writeFile(p, f.Contents); err != nil {
|
2021-05-06 15:31:18 +02:00
|
|
|
panic(fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:29:56 +02:00
|
|
|
func writeFile(pathToFile android.OutputPath, content string) error {
|
2021-03-10 08:05:59 +01:00
|
|
|
// These files are made editable to allow users to modify and iterate on them
|
|
|
|
// in the source tree.
|
|
|
|
return android.WriteFileToOutputDir(pathToFile, []byte(content), 0644)
|
2020-11-26 01:06:39 +01:00
|
|
|
}
|