blob: 5dc9612ad145a60d082e707c1c8a6248a9b3a777 [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 (
Jingwen Chendaa54bc2020-12-14 02:58:54 -050018 "fmt"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080019 "os"
Liz Kammer6eff3232021-08-26 08:37:59 -040020 "strings"
Chris Parsons3b1f83d2021-10-14 14:08:38 -040021
22 "android/soong/android"
23 "android/soong/bazel"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080024)
25
Jingwen Chen12b4c272021-03-10 02:05:59 -050026// Codegen is the backend of bp2build. The code generator is responsible for
27// writing .bzl files that are equivalent to Android.bp files that are capable
28// of being built with Bazel.
usta4f5d2c12022-10-28 23:32:01 -040029func Codegen(ctx *CodegenContext) *CodegenMetrics {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000030 // This directory stores BUILD files that could be eventually checked-in.
31 bp2buildDir := android.PathForOutput(ctx, "bp2build")
Usta Shresthadb46a9b2022-07-11 11:29:56 -040032 if err := android.RemoveAllOutputDir(bp2buildDir); err != nil {
33 fmt.Printf("ERROR: Encountered error while cleaning %s: %s", bp2buildDir, err.Error())
34 }
Liz Kammer2dd9ca42020-11-25 16:06:39 -080035
Liz Kammer6eff3232021-08-26 08:37:59 -040036 res, errs := GenerateBazelTargets(ctx, true)
37 if len(errs) > 0 {
38 errMsgs := make([]string, len(errs))
39 for i, err := range errs {
40 errMsgs[i] = fmt.Sprintf("%q", err)
41 }
42 fmt.Printf("ERROR: Encountered %d error(s): \nERROR: %s", len(errs), strings.Join(errMsgs, "\n"))
43 os.Exit(1)
44 }
Sasha Smundak0fd93e02022-05-19 19:34:31 -070045 bp2buildFiles := CreateBazelFiles(ctx.Config(), nil, res.buildFileToTargets, ctx.mode)
Jingwen Chenbf61afb2021-05-06 13:31:18 +000046 writeFiles(ctx, bp2buildDir, bp2buildFiles)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080047
Cole Faustb85d1a12022-11-08 18:14:01 -080048 productConfigFiles, err := CreateProductConfigFiles(ctx)
49 if err != nil {
50 fmt.Printf("ERROR: %s", err.Error())
51 os.Exit(1)
52 }
53
Liz Kammer09f947d2021-05-12 14:51:49 -040054 soongInjectionDir := android.PathForOutput(ctx, bazel.SoongInjectionDirName)
Cole Faustb85d1a12022-11-08 18:14:01 -080055 writeFiles(ctx, soongInjectionDir, productConfigFiles)
Chris Parsons3b1f83d2021-10-14 14:08:38 -040056 writeFiles(ctx, soongInjectionDir, CreateSoongInjectionFiles(ctx.Config(), res.metrics))
Jingwen Chen164e0862021-02-19 00:48:40 -050057
usta4f5d2c12022-10-28 23:32:01 -040058 return &res.metrics
Liz Kammer2dd9ca42020-11-25 16:06:39 -080059}
60
Jingwen Chen12b4c272021-03-10 02:05:59 -050061// Get the output directory and create it if it doesn't exist.
62func getOrCreateOutputDir(outputDir android.OutputPath, ctx android.PathContext, dir string) android.OutputPath {
63 dirPath := outputDir.Join(ctx, dir)
Usta Shresthadb46a9b2022-07-11 11:29:56 -040064 if err := android.CreateOutputDirIfNonexistent(dirPath, os.ModePerm); err != nil {
65 fmt.Printf("ERROR: path %s: %s", dirPath, err.Error())
66 }
Jingwen Chen12b4c272021-03-10 02:05:59 -050067 return dirPath
Liz Kammer2dd9ca42020-11-25 16:06:39 -080068}
69
Jingwen Chenbf61afb2021-05-06 13:31:18 +000070// writeFiles materializes a list of BazelFile rooted at outputDir.
71func writeFiles(ctx android.PathContext, outputDir android.OutputPath, files []BazelFile) {
72 for _, f := range files {
73 p := getOrCreateOutputDir(outputDir, ctx, f.Dir).Join(ctx, f.Basename)
Usta Shresthadb46a9b2022-07-11 11:29:56 -040074 if err := writeFile(p, f.Contents); err != nil {
Jingwen Chenbf61afb2021-05-06 13:31:18 +000075 panic(fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err))
76 }
77 }
78}
79
Usta Shresthadb46a9b2022-07-11 11:29:56 -040080func writeFile(pathToFile android.OutputPath, content string) error {
Jingwen Chen12b4c272021-03-10 02:05:59 -050081 // These files are made editable to allow users to modify and iterate on them
82 // in the source tree.
83 return android.WriteFileToOutputDir(pathToFile, []byte(content), 0644)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080084}