2020-11-26 08:06:39 +08: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 (
|
|
|
|
"android/soong/android"
|
2020-12-14 15:58:54 +08:00
|
|
|
"fmt"
|
2020-11-26 08:06:39 +08:00
|
|
|
"os"
|
2021-03-10 15:05:59 +08:00
|
|
|
"strings"
|
2020-11-26 08:06:39 +08:00
|
|
|
)
|
|
|
|
|
2021-03-10 15:05:59 +08: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.
|
2021-02-18 02:22:03 +08:00
|
|
|
func Codegen(ctx *CodegenContext) CodegenMetrics {
|
2020-12-14 15:58:54 +08:00
|
|
|
outputDir := android.PathForOutput(ctx, "bp2build")
|
|
|
|
android.RemoveAllOutputDir(outputDir)
|
2020-11-26 08:06:39 +08:00
|
|
|
|
2021-02-19 13:48:40 +08:00
|
|
|
buildToTargets, metrics := GenerateBazelTargets(ctx)
|
2020-11-26 08:06:39 +08:00
|
|
|
|
2021-03-10 15:05:59 +08:00
|
|
|
filesToWrite := CreateBazelFiles(nil, buildToTargets, ctx.mode)
|
|
|
|
|
|
|
|
generatedBuildFiles := []string{}
|
2020-11-26 08:06:39 +08:00
|
|
|
for _, f := range filesToWrite {
|
2021-03-10 15:05:59 +08:00
|
|
|
p := getOrCreateOutputDir(outputDir, ctx, f.Dir).Join(ctx, f.Basename)
|
|
|
|
if err := writeFile(ctx, p, f.Contents); err != nil {
|
2020-12-14 15:58:54 +08:00
|
|
|
fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err)
|
2020-11-26 08:06:39 +08:00
|
|
|
}
|
2021-03-10 15:05:59 +08:00
|
|
|
// if these generated files are modified, regenerate on next run.
|
|
|
|
generatedBuildFiles = append(generatedBuildFiles, p.String())
|
2020-11-26 08:06:39 +08:00
|
|
|
}
|
2021-02-19 13:48:40 +08:00
|
|
|
|
2021-03-10 15:05:59 +08:00
|
|
|
// The MANIFEST file contains the full list of files generated by bp2build, excluding itself.
|
|
|
|
// Its purpose is for downstream tools to understand the set of files converted by bp2build.
|
|
|
|
manifestFile := outputDir.Join(ctx, "MANIFEST")
|
|
|
|
writeFile(ctx, manifestFile, strings.Join(generatedBuildFiles, "\n"))
|
|
|
|
generatedBuildFiles = append(generatedBuildFiles, manifestFile.String())
|
2020-11-26 08:06:39 +08:00
|
|
|
|
2021-03-10 15:05:59 +08:00
|
|
|
return metrics
|
2020-11-26 08:06:39 +08:00
|
|
|
}
|
|
|
|
|
2021-03-10 15:05:59 +08: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)
|
|
|
|
android.CreateOutputDirIfNonexistent(dirPath, os.ModePerm)
|
|
|
|
return dirPath
|
2020-11-26 08:06:39 +08:00
|
|
|
}
|
|
|
|
|
2021-03-10 15:05:59 +08:00
|
|
|
func writeFile(ctx android.PathContext, 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)
|
2020-11-26 08:06:39 +08:00
|
|
|
}
|