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 "android/soong/android" 19 "android/soong/bp2build" 20 "io/ioutil" 21 "os" 22 "path/filepath" 23) 24 25func createBazelQueryView(ctx *bp2build.CodegenContext, bazelQueryViewDir string) error { 26 ruleShims := bp2build.CreateRuleShims(android.ModuleTypeFactories()) 27 28 // Ignore metrics reporting for queryview, since queryview is already a full-repo 29 // conversion and can use data from bazel query directly. 30 buildToTargets, _ := bp2build.GenerateBazelTargets(ctx, true) 31 32 filesToWrite := bp2build.CreateBazelFiles(ruleShims, buildToTargets, bp2build.QueryView) 33 for _, f := range filesToWrite { 34 if err := writeReadOnlyFile(bazelQueryViewDir, f); err != nil { 35 return err 36 } 37 } 38 39 return nil 40} 41 42// The auto-conversion directory should be read-only, sufficient for bazel query. The files 43// are not intended to be edited by end users. 44func writeReadOnlyFile(dir string, f bp2build.BazelFile) error { 45 dir = filepath.Join(dir, f.Dir) 46 if err := createDirectoryIfNonexistent(dir); err != nil { 47 return err 48 } 49 pathToFile := filepath.Join(dir, f.Basename) 50 51 // 0444 is read-only 52 err := ioutil.WriteFile(pathToFile, []byte(f.Contents), 0444) 53 54 return err 55} 56 57func createDirectoryIfNonexistent(dir string) error { 58 if _, err := os.Stat(dir); os.IsNotExist(err) { 59 return os.MkdirAll(dir, os.ModePerm) 60 } else { 61 return err 62 } 63} 64