blob: 35ae009bf286d1c27c7fdb08a64b84c20346b54f [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"
Jingwen Chen5ba7e472020-07-15 10:06:41 +000025)
26
Spandan Das5af0bd32022-09-28 20:43:08 +000027// A helper function to generate a Read-only Bazel workspace in outDir
28func createBazelWorkspace(ctx *bp2build.CodegenContext, outDir string) error {
29 os.RemoveAll(outDir)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080030 ruleShims := bp2build.CreateRuleShims(android.ModuleTypeFactories())
Jingwen Chen164e0862021-02-19 00:48:40 -050031
Jingwen Chencf9e2c82023-03-08 13:32:35 +000032 res, err := bp2build.GenerateBazelTargets(ctx, false)
Liz Kammer6eff3232021-08-26 08:37:59 -040033 if err != nil {
34 panic(err)
35 }
Jingwen Chen5ba7e472020-07-15 10:06:41 +000036
Sasha Smundak0fd93e02022-05-19 19:34:31 -070037 filesToWrite := bp2build.CreateBazelFiles(ctx.Config(), ruleShims, res.BuildDirToTargets(),
Spandan Das5af0bd32022-09-28 20:43:08 +000038 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
Liz Kammer2dd9ca42020-11-25 16:06:39 -080050 return nil
Jingwen Chen5ba7e472020-07-15 10:06:41 +000051}
52
Cole Faust6d51e732022-11-29 12:45:43 -080053// CopyBazelRcFiles creates BazelFiles for all the bazelrc files under
54// build/bazel. They're needed because the rc files are still read when running
55// queryview, so they have to be in the queryview workspace.
56func CopyBazelRcFiles() ([]bp2build.BazelFile, error) {
57 result := make([]bp2build.BazelFile, 0)
58 err := filepath.WalkDir(filepath.Join(topDir, "build/bazel"), func(path string, info fs.DirEntry, err error) error {
59 if filepath.Ext(path) == ".bazelrc" {
60 contents, err := os.ReadFile(path)
61 if err != nil {
62 return err
63 }
64 path, err = filepath.Rel(topDir, path)
65 if err != nil {
66 return err
67 }
68 result = append(result, bp2build.BazelFile{
69 Dir: filepath.Dir(path),
70 Basename: filepath.Base(path),
71 Contents: string(contents),
72 })
73 }
74 return nil
75 })
76 return result, err
77}
78
Liz Kammer2dd9ca42020-11-25 16:06:39 -080079// The auto-conversion directory should be read-only, sufficient for bazel query. The files
Jingwen Chend8004ef2020-08-27 09:40:43 +000080// are not intended to be edited by end users.
Liz Kammer2dd9ca42020-11-25 16:06:39 -080081func writeReadOnlyFile(dir string, f bp2build.BazelFile) error {
82 dir = filepath.Join(dir, f.Dir)
83 if err := createDirectoryIfNonexistent(dir); err != nil {
84 return err
85 }
86 pathToFile := filepath.Join(dir, f.Basename)
87
Jingwen Chen5ba7e472020-07-15 10:06:41 +000088 // 0444 is read-only
Liz Kammer2dd9ca42020-11-25 16:06:39 -080089 err := ioutil.WriteFile(pathToFile, []byte(f.Contents), 0444)
90
91 return err
Jingwen Chen5ba7e472020-07-15 10:06:41 +000092}
Jingwen Chen69d4cbe2020-08-07 14:16:34 +000093
Spandan Das067210f2022-12-07 01:14:52 +000094func writeReadWriteFile(dir string, f bp2build.BazelFile) error {
95 dir = filepath.Join(dir, f.Dir)
96 if err := createDirectoryIfNonexistent(dir); err != nil {
97 return err
98 }
99 pathToFile := filepath.Join(dir, f.Basename)
100
101 // 0644 is read-write
102 err := ioutil.WriteFile(pathToFile, []byte(f.Contents), 0644)
103
104 return err
105}
106
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800107func createDirectoryIfNonexistent(dir string) error {
108 if _, err := os.Stat(dir); os.IsNotExist(err) {
109 return os.MkdirAll(dir, os.ModePerm)
110 } else {
111 return err
Jingwen Chen69d4cbe2020-08-07 14:16:34 +0000112 }
113}