blob: 0a77d67a9b0bf314dc9984f3240be366a77551a9 [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 (
18 "android/soong/android"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080019 "android/soong/bp2build"
Jingwen Chen5ba7e472020-07-15 10:06:41 +000020 "io/ioutil"
21 "os"
22 "path/filepath"
Jingwen Chen5ba7e472020-07-15 10:06:41 +000023)
24
Jingwen Chen164e0862021-02-19 00:48:40 -050025func createBazelQueryView(ctx bp2build.CodegenContext, bazelQueryViewDir string) error {
Liz Kammer2dd9ca42020-11-25 16:06:39 -080026 ruleShims := bp2build.CreateRuleShims(android.ModuleTypeFactories())
Jingwen Chen164e0862021-02-19 00:48:40 -050027
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)
Jingwen Chen5ba7e472020-07-15 10:06:41 +000031
Jingwen Chen33832f92021-01-24 22:55:54 -050032 filesToWrite := bp2build.CreateBazelFiles(ruleShims, buildToTargets, bp2build.QueryView)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080033 for _, f := range filesToWrite {
34 if err := writeReadOnlyFile(bazelQueryViewDir, f); err != nil {
Jingwen Chend8004ef2020-08-27 09:40:43 +000035 return err
36 }
37 }
38
Liz Kammer2dd9ca42020-11-25 16:06:39 -080039 return nil
Jingwen Chen5ba7e472020-07-15 10:06:41 +000040}
41
Liz Kammer2dd9ca42020-11-25 16:06:39 -080042// The auto-conversion directory should be read-only, sufficient for bazel query. The files
Jingwen Chend8004ef2020-08-27 09:40:43 +000043// are not intended to be edited by end users.
Liz Kammer2dd9ca42020-11-25 16:06:39 -080044func 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
Jingwen Chen5ba7e472020-07-15 10:06:41 +000051 // 0444 is read-only
Liz Kammer2dd9ca42020-11-25 16:06:39 -080052 err := ioutil.WriteFile(pathToFile, []byte(f.Contents), 0444)
53
54 return err
Jingwen Chen5ba7e472020-07-15 10:06:41 +000055}
Jingwen Chen69d4cbe2020-08-07 14:16:34 +000056
Liz Kammer2dd9ca42020-11-25 16:06:39 -080057func 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
Jingwen Chen69d4cbe2020-08-07 14:16:34 +000062 }
63}