• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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	"fmt"
19	"os"
20	"path/filepath"
21	"strings"
22
23	"android/soong/android"
24	"android/soong/bazel"
25	"android/soong/shared"
26)
27
28func deleteFilesExcept(ctx *CodegenContext, rootOutputPath android.OutputPath, except []BazelFile) {
29	// Delete files that should no longer be present.
30	bp2buildDirAbs := shared.JoinPath(ctx.topDir, rootOutputPath.String())
31
32	filesToDelete := make(map[string]struct{})
33	err := filepath.Walk(bp2buildDirAbs,
34		func(path string, info os.FileInfo, err error) error {
35			if err != nil {
36				return err
37			}
38			if !info.IsDir() {
39				relPath, err := filepath.Rel(bp2buildDirAbs, path)
40				if err != nil {
41					return err
42				}
43				filesToDelete[relPath] = struct{}{}
44			}
45			return nil
46		})
47	if err != nil {
48		fmt.Printf("ERROR reading %s: %s", bp2buildDirAbs, err)
49		os.Exit(1)
50	}
51
52	for _, bazelFile := range except {
53		filePath := filepath.Join(bazelFile.Dir, bazelFile.Basename)
54		delete(filesToDelete, filePath)
55	}
56	for f, _ := range filesToDelete {
57		absPath := shared.JoinPath(bp2buildDirAbs, f)
58		if err := os.RemoveAll(absPath); err != nil {
59			fmt.Printf("ERROR deleting %s: %s", absPath, err)
60			os.Exit(1)
61		}
62	}
63}
64
65// Codegen is the backend of bp2build. The code generator is responsible for
66// writing .bzl files that are equivalent to Android.bp files that are capable
67// of being built with Bazel.
68func Codegen(ctx *CodegenContext) *CodegenMetrics {
69	// This directory stores BUILD files that could be eventually checked-in.
70	bp2buildDir := android.PathForOutput(ctx, "bp2build")
71
72	res, errs := GenerateBazelTargets(ctx, true)
73	if len(errs) > 0 {
74		errMsgs := make([]string, len(errs))
75		for i, err := range errs {
76			errMsgs[i] = fmt.Sprintf("%q", err)
77		}
78		fmt.Printf("ERROR: Encountered %d error(s): \nERROR: %s", len(errs), strings.Join(errMsgs, "\n"))
79		os.Exit(1)
80	}
81	bp2buildFiles := CreateBazelFiles(ctx.Config(), nil, res.buildFileToTargets, ctx.mode)
82	writeFiles(ctx, bp2buildDir, bp2buildFiles)
83	// Delete files under the bp2build root which weren't just written. An
84	// alternative would have been to delete the whole directory and write these
85	// files. However, this would regenerate files which were otherwise unchanged
86	// since the last bp2build run, which would have negative incremental
87	// performance implications.
88	deleteFilesExcept(ctx, bp2buildDir, bp2buildFiles)
89
90	injectionFiles, err := CreateSoongInjectionDirFiles(ctx, res.metrics)
91	if err != nil {
92		fmt.Printf("%s\n", err.Error())
93		os.Exit(1)
94	}
95	writeFiles(ctx, android.PathForOutput(ctx, bazel.SoongInjectionDirName), injectionFiles)
96	return &res.metrics
97}
98
99// Wrapper function that will be responsible for all files in soong_injection directory
100// This includes
101// 1. config value(s) that are hardcoded in Soong
102// 2. product_config variables
103func CreateSoongInjectionDirFiles(ctx *CodegenContext, metrics CodegenMetrics) ([]BazelFile, error) {
104	var ret []BazelFile
105
106	productConfigFiles, err := CreateProductConfigFiles(ctx)
107	if err != nil {
108		return nil, err
109	}
110	ret = append(ret, productConfigFiles...)
111	injectionFiles, err := soongInjectionFiles(ctx.Config(), metrics)
112	if err != nil {
113		return nil, err
114	}
115	ret = append(ret, injectionFiles...)
116	return ret, nil
117}
118
119// Get the output directory and create it if it doesn't exist.
120func getOrCreateOutputDir(outputDir android.OutputPath, ctx android.PathContext, dir string) android.OutputPath {
121	dirPath := outputDir.Join(ctx, dir)
122	if err := android.CreateOutputDirIfNonexistent(dirPath, os.ModePerm); err != nil {
123		fmt.Printf("ERROR: path %s: %s", dirPath, err.Error())
124	}
125	return dirPath
126}
127
128// writeFiles materializes a list of BazelFile rooted at outputDir.
129func writeFiles(ctx android.PathContext, outputDir android.OutputPath, files []BazelFile) {
130	for _, f := range files {
131		p := getOrCreateOutputDir(outputDir, ctx, f.Dir).Join(ctx, f.Basename)
132		if err := writeFile(p, f.Contents); err != nil {
133			panic(fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err))
134		}
135	}
136}
137
138func writeFile(pathToFile android.OutputPath, content string) error {
139	// These files are made editable to allow users to modify and iterate on them
140	// in the source tree.
141	return android.WriteFileToOutputDir(pathToFile, []byte(content), 0644)
142}
143