blob: 007d6d8b09fc2ad0ddefc7dd366b489df75110fa [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package bp2build
16
17import (
18 "android/soong/android"
Jingwen Chendaa54bc2020-12-14 02:58:54 -050019 "fmt"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080020 "os"
Jingwen Chen12b4c272021-03-10 02:05:59 -050021 "strings"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080022)
23
Jingwen Chen12b4c272021-03-10 02:05:59 -050024// Codegen is the backend of bp2build. The code generator is responsible for
25// writing .bzl files that are equivalent to Android.bp files that are capable
26// of being built with Bazel.
Liz Kammerba3ea162021-02-17 13:22:03 -050027func Codegen(ctx *CodegenContext) CodegenMetrics {
Jingwen Chendaa54bc2020-12-14 02:58:54 -050028 outputDir := android.PathForOutput(ctx, "bp2build")
29 android.RemoveAllOutputDir(outputDir)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080030
Jingwen Chen164e0862021-02-19 00:48:40 -050031 buildToTargets, metrics := GenerateBazelTargets(ctx)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080032
Jingwen Chen12b4c272021-03-10 02:05:59 -050033 filesToWrite := CreateBazelFiles(nil, buildToTargets, ctx.mode)
34
35 generatedBuildFiles := []string{}
Liz Kammer2dd9ca42020-11-25 16:06:39 -080036 for _, f := range filesToWrite {
Jingwen Chen12b4c272021-03-10 02:05:59 -050037 p := getOrCreateOutputDir(outputDir, ctx, f.Dir).Join(ctx, f.Basename)
38 if err := writeFile(ctx, p, f.Contents); err != nil {
Jingwen Chendaa54bc2020-12-14 02:58:54 -050039 fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080040 }
Jingwen Chen12b4c272021-03-10 02:05:59 -050041 // if these generated files are modified, regenerate on next run.
42 generatedBuildFiles = append(generatedBuildFiles, p.String())
Liz Kammer2dd9ca42020-11-25 16:06:39 -080043 }
Jingwen Chen164e0862021-02-19 00:48:40 -050044
Jingwen Chen12b4c272021-03-10 02:05:59 -050045 // The MANIFEST file contains the full list of files generated by bp2build, excluding itself.
46 // Its purpose is for downstream tools to understand the set of files converted by bp2build.
47 manifestFile := outputDir.Join(ctx, "MANIFEST")
48 writeFile(ctx, manifestFile, strings.Join(generatedBuildFiles, "\n"))
49 generatedBuildFiles = append(generatedBuildFiles, manifestFile.String())
50
Jingwen Chen164e0862021-02-19 00:48:40 -050051 return metrics
Liz Kammer2dd9ca42020-11-25 16:06:39 -080052}
53
Jingwen Chen12b4c272021-03-10 02:05:59 -050054// Get the output directory and create it if it doesn't exist.
55func getOrCreateOutputDir(outputDir android.OutputPath, ctx android.PathContext, dir string) android.OutputPath {
56 dirPath := outputDir.Join(ctx, dir)
57 android.CreateOutputDirIfNonexistent(dirPath, os.ModePerm)
58 return dirPath
Liz Kammer2dd9ca42020-11-25 16:06:39 -080059}
60
Jingwen Chen12b4c272021-03-10 02:05:59 -050061func writeFile(ctx android.PathContext, pathToFile android.OutputPath, content string) error {
62 // These files are made editable to allow users to modify and iterate on them
63 // in the source tree.
64 return android.WriteFileToOutputDir(pathToFile, []byte(content), 0644)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080065}