Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 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 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 19 | "android/soong/bp2build" |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 20 | "io/ioutil" |
| 21 | "os" |
| 22 | "path/filepath" |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 23 | |
| 24 | "github.com/google/blueprint" |
| 25 | ) |
| 26 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 27 | type queryviewContext struct { |
| 28 | bpCtx *blueprint.Context |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 31 | func (ctx *queryviewContext) ModuleName(module blueprint.Module) string { |
| 32 | return ctx.bpCtx.ModuleName(module) |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 35 | func (ctx *queryviewContext) ModuleDir(module blueprint.Module) string { |
| 36 | return ctx.bpCtx.ModuleDir(module) |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 39 | func (ctx *queryviewContext) ModuleSubDir(module blueprint.Module) string { |
| 40 | return ctx.bpCtx.ModuleSubDir(module) |
Jingwen Chen | 69d4cbe | 2020-08-07 14:16:34 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 43 | func (ctx *queryviewContext) ModuleType(module blueprint.Module) string { |
| 44 | return ctx.bpCtx.ModuleType(module) |
Jingwen Chen | 69d4cbe | 2020-08-07 14:16:34 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 47 | func (ctx *queryviewContext) VisitAllModulesBlueprint(visit func(blueprint.Module)) { |
| 48 | ctx.bpCtx.VisitAllModules(visit) |
Jingwen Chen | 69d4cbe | 2020-08-07 14:16:34 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 51 | func (ctx *queryviewContext) VisitDirectDeps(module android.Module, visit func(android.Module)) { |
| 52 | ctx.bpCtx.VisitDirectDeps(module, func(m blueprint.Module) { |
| 53 | if aModule, ok := m.(android.Module); ok { |
| 54 | visit(aModule) |
Jingwen Chen | 69d4cbe | 2020-08-07 14:16:34 +0000 | [diff] [blame] | 55 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 56 | }) |
Jingwen Chen | d8004ef | 2020-08-27 09:40:43 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 59 | func createBazelQueryView(ctx *android.Context, bazelQueryViewDir string) error { |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 60 | qvCtx := queryviewContext{ |
| 61 | bpCtx: ctx.Context, |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 62 | } |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 63 | ruleShims := bp2build.CreateRuleShims(android.ModuleTypeFactories()) |
| 64 | buildToTargets := bp2build.GenerateSoongModuleTargets(&qvCtx) |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 65 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 66 | filesToWrite := bp2build.CreateBazelFiles(ruleShims, buildToTargets) |
| 67 | for _, f := range filesToWrite { |
| 68 | if err := writeReadOnlyFile(bazelQueryViewDir, f); err != nil { |
Jingwen Chen | d8004ef | 2020-08-27 09:40:43 +0000 | [diff] [blame] | 69 | return err |
| 70 | } |
| 71 | } |
| 72 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 73 | return nil |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 76 | // The auto-conversion directory should be read-only, sufficient for bazel query. The files |
Jingwen Chen | d8004ef | 2020-08-27 09:40:43 +0000 | [diff] [blame] | 77 | // are not intended to be edited by end users. |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 78 | func writeReadOnlyFile(dir string, f bp2build.BazelFile) error { |
| 79 | dir = filepath.Join(dir, f.Dir) |
| 80 | if err := createDirectoryIfNonexistent(dir); err != nil { |
| 81 | return err |
| 82 | } |
| 83 | pathToFile := filepath.Join(dir, f.Basename) |
| 84 | |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 85 | // 0444 is read-only |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 86 | err := ioutil.WriteFile(pathToFile, []byte(f.Contents), 0444) |
| 87 | |
| 88 | return err |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 89 | } |
Jingwen Chen | 69d4cbe | 2020-08-07 14:16:34 +0000 | [diff] [blame] | 90 | |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame^] | 91 | func createDirectoryIfNonexistent(dir string) error { |
| 92 | if _, err := os.Stat(dir); os.IsNotExist(err) { |
| 93 | return os.MkdirAll(dir, os.ModePerm) |
| 94 | } else { |
| 95 | return err |
Jingwen Chen | 69d4cbe | 2020-08-07 14:16:34 +0000 | [diff] [blame] | 96 | } |
| 97 | } |