Create a build/bazel/product_config/generated/products/<product_name>/BUILD file that contains the platform definitions needed for a particular product. Currently we just create it for the current lunch target, but the idea is that eventually when all product config is in starlark, all the products will have their platform definitions in the tree at once. Bug: 249685973 Test: Presubmits Change-Id: I08c82ff28dcf62f09d3b1d2e3186a6b961e12f6e
85 lines
3.0 KiB
Go
85 lines
3.0 KiB
Go
// 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 (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"android/soong/android"
|
|
"android/soong/bazel"
|
|
)
|
|
|
|
// 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.
|
|
func Codegen(ctx *CodegenContext) *CodegenMetrics {
|
|
// This directory stores BUILD files that could be eventually checked-in.
|
|
bp2buildDir := android.PathForOutput(ctx, "bp2build")
|
|
if err := android.RemoveAllOutputDir(bp2buildDir); err != nil {
|
|
fmt.Printf("ERROR: Encountered error while cleaning %s: %s", bp2buildDir, err.Error())
|
|
}
|
|
|
|
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)
|
|
}
|
|
bp2buildFiles := CreateBazelFiles(ctx.Config(), nil, res.buildFileToTargets, ctx.mode)
|
|
writeFiles(ctx, bp2buildDir, bp2buildFiles)
|
|
|
|
productConfigFiles, err := CreateProductConfigFiles(ctx)
|
|
if err != nil {
|
|
fmt.Printf("ERROR: %s", err.Error())
|
|
os.Exit(1)
|
|
}
|
|
|
|
soongInjectionDir := android.PathForOutput(ctx, bazel.SoongInjectionDirName)
|
|
writeFiles(ctx, soongInjectionDir, productConfigFiles)
|
|
writeFiles(ctx, soongInjectionDir, CreateSoongInjectionFiles(ctx.Config(), res.metrics))
|
|
|
|
return &res.metrics
|
|
}
|
|
|
|
// 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)
|
|
if err := android.CreateOutputDirIfNonexistent(dirPath, os.ModePerm); err != nil {
|
|
fmt.Printf("ERROR: path %s: %s", dirPath, err.Error())
|
|
}
|
|
return dirPath
|
|
}
|
|
|
|
// 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)
|
|
if err := writeFile(p, f.Contents); err != nil {
|
|
panic(fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err))
|
|
}
|
|
}
|
|
}
|
|
|
|
func writeFile(pathToFile android.OutputPath, content string) error {
|
|
// 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)
|
|
}
|