blob: cd1d6fb5d571d38633cad4a220bba2101517d122 [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 (
Jingwen Chen5ba7e472020-07-15 10:06:41 +000018 "io/ioutil"
19 "os"
20 "path/filepath"
Lukacs T. Berki7edd91c2021-09-07 08:47:21 +020021
22 "android/soong/android"
23 "android/soong/bp2build"
Jingwen Chen5ba7e472020-07-15 10:06:41 +000024)
25
Spandan Das5af0bd32022-09-28 20:43:08 +000026// A helper function to generate a Read-only Bazel workspace in outDir
27func createBazelWorkspace(ctx *bp2build.CodegenContext, outDir string) error {
28 os.RemoveAll(outDir)
Liz Kammer2dd9ca42020-11-25 16:06:39 -080029 ruleShims := bp2build.CreateRuleShims(android.ModuleTypeFactories())
Jingwen Chen164e0862021-02-19 00:48:40 -050030
Liz Kammer6eff3232021-08-26 08:37:59 -040031 res, err := bp2build.GenerateBazelTargets(ctx, true)
32 if err != nil {
33 panic(err)
34 }
Jingwen Chen5ba7e472020-07-15 10:06:41 +000035
Sasha Smundak0fd93e02022-05-19 19:34:31 -070036 filesToWrite := bp2build.CreateBazelFiles(ctx.Config(), ruleShims, res.BuildDirToTargets(),
Spandan Das5af0bd32022-09-28 20:43:08 +000037 ctx.Mode())
Liz Kammer2dd9ca42020-11-25 16:06:39 -080038 for _, f := range filesToWrite {
Spandan Das5af0bd32022-09-28 20:43:08 +000039 if err := writeReadOnlyFile(outDir, f); err != nil {
Jingwen Chend8004ef2020-08-27 09:40:43 +000040 return err
41 }
42 }
43
Liz Kammer2dd9ca42020-11-25 16:06:39 -080044 return nil
Jingwen Chen5ba7e472020-07-15 10:06:41 +000045}
46
Liz Kammer2dd9ca42020-11-25 16:06:39 -080047// The auto-conversion directory should be read-only, sufficient for bazel query. The files
Jingwen Chend8004ef2020-08-27 09:40:43 +000048// are not intended to be edited by end users.
Liz Kammer2dd9ca42020-11-25 16:06:39 -080049func writeReadOnlyFile(dir string, f bp2build.BazelFile) error {
50 dir = filepath.Join(dir, f.Dir)
51 if err := createDirectoryIfNonexistent(dir); err != nil {
52 return err
53 }
54 pathToFile := filepath.Join(dir, f.Basename)
55
Jingwen Chen5ba7e472020-07-15 10:06:41 +000056 // 0444 is read-only
Liz Kammer2dd9ca42020-11-25 16:06:39 -080057 err := ioutil.WriteFile(pathToFile, []byte(f.Contents), 0444)
58
59 return err
Jingwen Chen5ba7e472020-07-15 10:06:41 +000060}
Jingwen Chen69d4cbe2020-08-07 14:16:34 +000061
Liz Kammer2dd9ca42020-11-25 16:06:39 -080062func createDirectoryIfNonexistent(dir string) error {
63 if _, err := os.Stat(dir); os.IsNotExist(err) {
64 return os.MkdirAll(dir, os.ModePerm)
65 } else {
66 return err
Jingwen Chen69d4cbe2020-08-07 14:16:34 +000067 }
68}