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 main 16 17import ( 18 "io/fs" 19 "io/ioutil" 20 "os" 21 "path/filepath" 22 23 "android/soong/android" 24 "android/soong/bp2build" 25) 26 27// A helper function to generate a Read-only Bazel workspace in outDir 28func createBazelWorkspace(ctx *bp2build.CodegenContext, outDir string, generateFilegroups bool) error { 29 os.RemoveAll(outDir) 30 ruleShims := bp2build.CreateRuleShims(android.ModuleTypeFactories()) 31 32 res, err := bp2build.GenerateBazelTargets(ctx, generateFilegroups) 33 if err != nil { 34 panic(err) 35 } 36 37 filesToWrite := bp2build.CreateBazelFiles(ctx.Config(), ruleShims, res.BuildDirToTargets(), 38 ctx.Mode()) 39 bazelRcFiles, err2 := CopyBazelRcFiles() 40 if err2 != nil { 41 return err2 42 } 43 filesToWrite = append(filesToWrite, bazelRcFiles...) 44 for _, f := range filesToWrite { 45 if err := writeReadOnlyFile(outDir, f); err != nil { 46 return err 47 } 48 } 49 50 return nil 51} 52 53// 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 79// The auto-conversion directory should be read-only, sufficient for bazel query. The files 80// are not intended to be edited by end users. 81func 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 88 // 0444 is read-only 89 err := ioutil.WriteFile(pathToFile, []byte(f.Contents), 0444) 90 91 return err 92} 93 94func 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 107func 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 112 } 113} 114