blob: 45a3cb6fe47b407e15faff43469e7173c046fa0e [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"
Liz Kammer09f947d2021-05-12 14:51:49 -040019 "android/soong/bazel"
Jingwen Chendaa54bc2020-12-14 02:58:54 -050020 "fmt"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080021 "os"
Liz Kammer6eff3232021-08-26 08:37:59 -040022 "strings"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080023)
24
Jingwen Chen12b4c272021-03-10 02:05:59 -050025// Codegen is the backend of bp2build. The code generator is responsible for
26// writing .bzl files that are equivalent to Android.bp files that are capable
27// of being built with Bazel.
Liz Kammerba3ea162021-02-17 13:22:03 -050028func Codegen(ctx *CodegenContext) CodegenMetrics {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000029 // This directory stores BUILD files that could be eventually checked-in.
30 bp2buildDir := android.PathForOutput(ctx, "bp2build")
31 android.RemoveAllOutputDir(bp2buildDir)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080032
Liz Kammer6eff3232021-08-26 08:37:59 -040033 res, errs := GenerateBazelTargets(ctx, true)
34 if len(errs) > 0 {
35 errMsgs := make([]string, len(errs))
36 for i, err := range errs {
37 errMsgs[i] = fmt.Sprintf("%q", err)
38 }
39 fmt.Printf("ERROR: Encountered %d error(s): \nERROR: %s", len(errs), strings.Join(errMsgs, "\n"))
40 os.Exit(1)
41 }
42 bp2buildFiles := CreateBazelFiles(nil, res.buildFileToTargets, ctx.mode)
Jingwen Chenbf61afb2021-05-06 13:31:18 +000043 writeFiles(ctx, bp2buildDir, bp2buildFiles)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080044
Liz Kammer09f947d2021-05-12 14:51:49 -040045 soongInjectionDir := android.PathForOutput(ctx, bazel.SoongInjectionDirName)
Jingwen Chen61174502021-09-17 08:40:45 +000046 writeFiles(ctx, soongInjectionDir, CreateSoongInjectionFiles(res.metrics))
Jingwen Chen164e0862021-02-19 00:48:40 -050047
Liz Kammer6eff3232021-08-26 08:37:59 -040048 return res.metrics
Liz Kammer2dd9ca42020-11-25 16:06:39 -080049}
50
Jingwen Chen12b4c272021-03-10 02:05:59 -050051// Get the output directory and create it if it doesn't exist.
52func getOrCreateOutputDir(outputDir android.OutputPath, ctx android.PathContext, dir string) android.OutputPath {
53 dirPath := outputDir.Join(ctx, dir)
54 android.CreateOutputDirIfNonexistent(dirPath, os.ModePerm)
55 return dirPath
Liz Kammer2dd9ca42020-11-25 16:06:39 -080056}
57
Jingwen Chenbf61afb2021-05-06 13:31:18 +000058// writeFiles materializes a list of BazelFile rooted at outputDir.
59func writeFiles(ctx android.PathContext, outputDir android.OutputPath, files []BazelFile) {
60 for _, f := range files {
61 p := getOrCreateOutputDir(outputDir, ctx, f.Dir).Join(ctx, f.Basename)
62 if err := writeFile(ctx, p, f.Contents); err != nil {
63 panic(fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err))
64 }
65 }
66}
67
Jingwen Chen12b4c272021-03-10 02:05:59 -050068func writeFile(ctx android.PathContext, pathToFile android.OutputPath, content string) error {
69 // These files are made editable to allow users to modify and iterate on them
70 // in the source tree.
71 return android.WriteFileToOutputDir(pathToFile, []byte(content), 0644)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080072}