blob: cf6994fd95d20031ea0fdc71f486d83a9d5cadbb [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"
21)
22
Jingwen Chen12b4c272021-03-10 02:05:59 -050023// Codegen is the backend of bp2build. The code generator is responsible for
24// writing .bzl files that are equivalent to Android.bp files that are capable
25// of being built with Bazel.
Liz Kammerba3ea162021-02-17 13:22:03 -050026func Codegen(ctx *CodegenContext) CodegenMetrics {
Jingwen Chendaa54bc2020-12-14 02:58:54 -050027 outputDir := android.PathForOutput(ctx, "bp2build")
28 android.RemoveAllOutputDir(outputDir)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080029
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040030 buildToTargets, metrics := GenerateBazelTargets(ctx, true)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080031
Jingwen Chen12b4c272021-03-10 02:05:59 -050032 filesToWrite := CreateBazelFiles(nil, buildToTargets, ctx.mode)
33
34 generatedBuildFiles := []string{}
Liz Kammer2dd9ca42020-11-25 16:06:39 -080035 for _, f := range filesToWrite {
Jingwen Chen12b4c272021-03-10 02:05:59 -050036 p := getOrCreateOutputDir(outputDir, ctx, f.Dir).Join(ctx, f.Basename)
37 if err := writeFile(ctx, p, f.Contents); err != nil {
Jingwen Chen2b54eb82021-05-03 06:44:44 +000038 panic(fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err))
Liz Kammer2dd9ca42020-11-25 16:06:39 -080039 }
Jingwen Chen12b4c272021-03-10 02:05:59 -050040 // if these generated files are modified, regenerate on next run.
41 generatedBuildFiles = append(generatedBuildFiles, p.String())
Liz Kammer2dd9ca42020-11-25 16:06:39 -080042 }
Jingwen Chen164e0862021-02-19 00:48:40 -050043
44 return metrics
Liz Kammer2dd9ca42020-11-25 16:06:39 -080045}
46
Jingwen Chen12b4c272021-03-10 02:05:59 -050047// Get the output directory and create it if it doesn't exist.
48func getOrCreateOutputDir(outputDir android.OutputPath, ctx android.PathContext, dir string) android.OutputPath {
49 dirPath := outputDir.Join(ctx, dir)
50 android.CreateOutputDirIfNonexistent(dirPath, os.ModePerm)
51 return dirPath
Liz Kammer2dd9ca42020-11-25 16:06:39 -080052}
53
Jingwen Chen12b4c272021-03-10 02:05:59 -050054func writeFile(ctx android.PathContext, pathToFile android.OutputPath, content string) error {
55 // These files are made editable to allow users to modify and iterate on them
56 // in the source tree.
57 return android.WriteFileToOutputDir(pathToFile, []byte(content), 0644)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080058}