blob: 5c2316a31587ca8f300b82d65fdd76805d935e53 [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 Faust6d51e732022-11-29 12:45:43 -080018 "io/fs"
Jingwen Chen5ba7e472020-07-15 10:06:41 +000019 "io/ioutil"
20 "os"
21 "path/filepath"
Lukacs T. Berki7edd91c2021-09-07 08:47:21 +020022
23 "android/soong/android"
24 "android/soong/bp2build"
usta40caf952023-08-04 16:52:14 -040025 "android/soong/starlark_import"
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
usta40caf952023-08-04 16:52:14 -040038 filesToWrite := bp2build.CreateBazelFiles(ruleShims, res.BuildDirToTargets(), ctx.Mode())
Cole Faust6d51e732022-11-29 12:45:43 -080039 bazelRcFiles, err2 := CopyBazelRcFiles()
40 if err2 != nil {
41 return err2
42 }
43 filesToWrite = append(filesToWrite, bazelRcFiles...)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080044 for _, f := range filesToWrite {
Spandan Das5af0bd32022-09-28 20:43:08 +000045 if err := writeReadOnlyFile(outDir, f); err != nil {
Jingwen Chend8004ef2020-08-27 09:40:43 +000046 return err
47 }
48 }
49
Cole Faustc9508aa2023-02-07 11:38:27 -080050 // Add starlark deps here, so that they apply to both queryview and apibp2build which
51 // both run this function.
52 starlarkDeps, err2 := starlark_import.GetNinjaDeps()
53 if err2 != nil {
54 return err2
55 }
56 ctx.AddNinjaFileDeps(starlarkDeps...)
57
Liz Kammer2dd9ca42020-11-25 16:06:39 -080058 return nil
Jingwen Chen5ba7e472020-07-15 10:06:41 +000059}
60
Cole Faust6d51e732022-11-29 12:45:43 -080061// CopyBazelRcFiles creates BazelFiles for all the bazelrc files under
62// build/bazel. They're needed because the rc files are still read when running
63// queryview, so they have to be in the queryview workspace.
64func CopyBazelRcFiles() ([]bp2build.BazelFile, error) {
65 result := make([]bp2build.BazelFile, 0)
66 err := filepath.WalkDir(filepath.Join(topDir, "build/bazel"), func(path string, info fs.DirEntry, err error) error {
67 if filepath.Ext(path) == ".bazelrc" {
68 contents, err := os.ReadFile(path)
69 if err != nil {
70 return err
71 }
72 path, err = filepath.Rel(topDir, path)
73 if err != nil {
74 return err
75 }
76 result = append(result, bp2build.BazelFile{
77 Dir: filepath.Dir(path),
78 Basename: filepath.Base(path),
79 Contents: string(contents),
80 })
81 }
82 return nil
83 })
84 return result, err
85}
86
Liz Kammer2dd9ca42020-11-25 16:06:39 -080087// The auto-conversion directory should be read-only, sufficient for bazel query. The files
Jingwen Chend8004ef2020-08-27 09:40:43 +000088// are not intended to be edited by end users.
Liz Kammer2dd9ca42020-11-25 16:06:39 -080089func writeReadOnlyFile(dir string, f bp2build.BazelFile) error {
90 dir = filepath.Join(dir, f.Dir)
91 if err := createDirectoryIfNonexistent(dir); err != nil {
92 return err
93 }
94 pathToFile := filepath.Join(dir, f.Basename)
95
Jingwen Chen5ba7e472020-07-15 10:06:41 +000096 // 0444 is read-only
Liz Kammer2dd9ca42020-11-25 16:06:39 -080097 err := ioutil.WriteFile(pathToFile, []byte(f.Contents), 0444)
98
99 return err
Jingwen Chen5ba7e472020-07-15 10:06:41 +0000100}
Jingwen Chen69d4cbe2020-08-07 14:16:34 +0000101
Spandan Das067210f2022-12-07 01:14:52 +0000102func writeReadWriteFile(dir string, f bp2build.BazelFile) error {
103 dir = filepath.Join(dir, f.Dir)
104 if err := createDirectoryIfNonexistent(dir); err != nil {
105 return err
106 }
107 pathToFile := filepath.Join(dir, f.Basename)
108
109 // 0644 is read-write
110 err := ioutil.WriteFile(pathToFile, []byte(f.Contents), 0644)
111
112 return err
113}
114
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800115func createDirectoryIfNonexistent(dir string) error {
116 if _, err := os.Stat(dir); os.IsNotExist(err) {
117 return os.MkdirAll(dir, os.ModePerm)
118 } else {
119 return err
Jingwen Chen69d4cbe2020-08-07 14:16:34 +0000120 }
121}