blob: 67cb6cfc8a78854e4d3a1ff4349f51885f006215 [file] [log] [blame]
Jingwen Chen5ba7e472020-07-15 10:06:41 +00001// 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 main
16
17import (
Cole Faustc9508aa2023-02-07 11:38:27 -080018 "android/soong/starlark_import"
Cole Faust6d51e732022-11-29 12:45:43 -080019 "io/fs"
Jingwen Chen5ba7e472020-07-15 10:06:41 +000020 "io/ioutil"
21 "os"
22 "path/filepath"
Lukacs T. Berki7edd91c2021-09-07 08:47:21 +020023
24 "android/soong/android"
25 "android/soong/bp2build"
Jingwen Chen5ba7e472020-07-15 10:06:41 +000026)
27
Spandan Das5af0bd32022-09-28 20:43:08 +000028// A helper function to generate a Read-only Bazel workspace in outDir
Spandan Das98cb8562023-03-09 23:05:47 +000029func createBazelWorkspace(ctx *bp2build.CodegenContext, outDir string, generateFilegroups bool) error {
Spandan Das5af0bd32022-09-28 20:43:08 +000030 os.RemoveAll(outDir)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080031 ruleShims := bp2build.CreateRuleShims(android.ModuleTypeFactories())
Jingwen Chen164e0862021-02-19 00:48:40 -050032
Spandan Das98cb8562023-03-09 23:05:47 +000033 res, err := bp2build.GenerateBazelTargets(ctx, generateFilegroups)
Liz Kammer6eff3232021-08-26 08:37:59 -040034 if err != nil {
35 panic(err)
36 }
Jingwen Chen5ba7e472020-07-15 10:06:41 +000037
Sasha Smundak0fd93e02022-05-19 19:34:31 -070038 filesToWrite := bp2build.CreateBazelFiles(ctx.Config(), ruleShims, res.BuildDirToTargets(),
Spandan Das5af0bd32022-09-28 20:43:08 +000039 ctx.Mode())
Cole Faust6d51e732022-11-29 12:45:43 -080040 bazelRcFiles, err2 := CopyBazelRcFiles()
41 if err2 != nil {
42 return err2
43 }
44 filesToWrite = append(filesToWrite, bazelRcFiles...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080045 for _, f := range filesToWrite {
Spandan Das5af0bd32022-09-28 20:43:08 +000046 if err := writeReadOnlyFile(outDir, f); err != nil {
Jingwen Chend8004ef2020-08-27 09:40:43 +000047 return err
48 }
49 }
50
Cole Faustc9508aa2023-02-07 11:38:27 -080051 // Add starlark deps here, so that they apply to both queryview and apibp2build which
52 // both run this function.
53 starlarkDeps, err2 := starlark_import.GetNinjaDeps()
54 if err2 != nil {
55 return err2
56 }
57 ctx.AddNinjaFileDeps(starlarkDeps...)
58
Liz Kammer2dd9ca42020-11-25 16:06:39 -080059 return nil
Jingwen Chen5ba7e472020-07-15 10:06:41 +000060}
61
Cole Faust6d51e732022-11-29 12:45:43 -080062// CopyBazelRcFiles creates BazelFiles for all the bazelrc files under
63// build/bazel. They're needed because the rc files are still read when running
64// queryview, so they have to be in the queryview workspace.
65func CopyBazelRcFiles() ([]bp2build.BazelFile, error) {
66 result := make([]bp2build.BazelFile, 0)
67 err := filepath.WalkDir(filepath.Join(topDir, "build/bazel"), func(path string, info fs.DirEntry, err error) error {
68 if filepath.Ext(path) == ".bazelrc" {
69 contents, err := os.ReadFile(path)
70 if err != nil {
71 return err
72 }
73 path, err = filepath.Rel(topDir, path)
74 if err != nil {
75 return err
76 }
77 result = append(result, bp2build.BazelFile{
78 Dir: filepath.Dir(path),
79 Basename: filepath.Base(path),
80 Contents: string(contents),
81 })
82 }
83 return nil
84 })
85 return result, err
86}
87
Liz Kammer2dd9ca42020-11-25 16:06:39 -080088// The auto-conversion directory should be read-only, sufficient for bazel query. The files
Jingwen Chend8004ef2020-08-27 09:40:43 +000089// are not intended to be edited by end users.
Liz Kammer2dd9ca42020-11-25 16:06:39 -080090func writeReadOnlyFile(dir string, f bp2build.BazelFile) error {
91 dir = filepath.Join(dir, f.Dir)
92 if err := createDirectoryIfNonexistent(dir); err != nil {
93 return err
94 }
95 pathToFile := filepath.Join(dir, f.Basename)
96
Jingwen Chen5ba7e472020-07-15 10:06:41 +000097 // 0444 is read-only
Liz Kammer2dd9ca42020-11-25 16:06:39 -080098 err := ioutil.WriteFile(pathToFile, []byte(f.Contents), 0444)
99
100 return err
Jingwen Chen5ba7e472020-07-15 10:06:41 +0000101}
Jingwen Chen69d4cbe2020-08-07 14:16:34 +0000102
Spandan Das067210f2022-12-07 01:14:52 +0000103func writeReadWriteFile(dir string, f bp2build.BazelFile) error {
104 dir = filepath.Join(dir, f.Dir)
105 if err := createDirectoryIfNonexistent(dir); err != nil {
106 return err
107 }
108 pathToFile := filepath.Join(dir, f.Basename)
109
110 // 0644 is read-write
111 err := ioutil.WriteFile(pathToFile, []byte(f.Contents), 0644)
112
113 return err
114}
115
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800116func createDirectoryIfNonexistent(dir string) error {
117 if _, err := os.Stat(dir); os.IsNotExist(err) {
118 return os.MkdirAll(dir, os.ModePerm)
119 } else {
120 return err
Jingwen Chen69d4cbe2020-08-07 14:16:34 +0000121 }
122}