blob: 7169d7e972c38eba5790d4abe31ed3e86133f601 [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 Chendaa54bc2020-12-14 02:58:54 -050023// The Bazel bp2build code generator is responsible for writing .bzl files that are equivalent to
Liz Kammer2dd9ca42020-11-25 16:06:39 -080024// Android.bp files that are capable of being built with Bazel.
Jingwen Chen164e0862021-02-19 00:48:40 -050025func Codegen(ctx CodegenContext) CodegenMetrics {
Jingwen Chendaa54bc2020-12-14 02:58:54 -050026 outputDir := android.PathForOutput(ctx, "bp2build")
27 android.RemoveAllOutputDir(outputDir)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080028
29 ruleShims := CreateRuleShims(android.ModuleTypeFactories())
30
Jingwen Chen164e0862021-02-19 00:48:40 -050031 buildToTargets, metrics := GenerateBazelTargets(ctx)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080032
Jingwen Chen33832f92021-01-24 22:55:54 -050033 filesToWrite := CreateBazelFiles(ruleShims, buildToTargets, ctx.mode)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080034 for _, f := range filesToWrite {
Jingwen Chendaa54bc2020-12-14 02:58:54 -050035 if err := writeFile(outputDir, ctx, f); err != nil {
36 fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080037 }
38 }
Jingwen Chen164e0862021-02-19 00:48:40 -050039
40 return metrics
Liz Kammer2dd9ca42020-11-25 16:06:39 -080041}
42
Jingwen Chendaa54bc2020-12-14 02:58:54 -050043func writeFile(outputDir android.OutputPath, ctx android.PathContext, f BazelFile) error {
44 return writeReadOnlyFile(ctx, getOutputPath(outputDir, ctx, f.Dir), f.Basename, f.Contents)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080045}
46
Jingwen Chendaa54bc2020-12-14 02:58:54 -050047func getOutputPath(outputDir android.OutputPath, ctx android.PathContext, dir string) android.OutputPath {
48 return outputDir.Join(ctx, dir)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080049}
50
51// The auto-conversion directory should be read-only, sufficient for bazel query. The files
52// are not intended to be edited by end users.
53func writeReadOnlyFile(ctx android.PathContext, dir android.OutputPath, baseName, content string) error {
54 android.CreateOutputDirIfNonexistent(dir, os.ModePerm)
55 pathToFile := dir.Join(ctx, baseName)
56
57 // 0444 is read-only
58 err := android.WriteFileToOutputDir(pathToFile, []byte(content), 0444)
59
60 return err
61}