blob: 5be3d3ad869f813de336def8494b770bcd971178 [file] [log] [blame]
Liz Kammer09ddb3a2022-10-19 01:29:18 -04001// Copyright 2022 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 android
16
17import (
18 "encoding/json"
19 "fmt"
20 "io/ioutil"
21 "os"
22 "strings"
23
24 "github.com/google/blueprint"
25)
26
27func init() {
28 RegisterPluginSingletonBuildComponents(InitRegistrationContext)
29}
30
31func RegisterPluginSingletonBuildComponents(ctx RegistrationContext) {
32 ctx.RegisterSingletonType("plugins", pluginSingletonFactory)
33}
34
35// pluginSingleton is a singleton to handle allowlisting of the final Android-<product_name>.mk file
36// output.
37func pluginSingletonFactory() Singleton {
38 return &pluginSingleton{}
39}
40
41type pluginSingleton struct{}
42
43var allowedPluginsByName = map[string]bool{
44 "aidl-soong-rules": true,
45 "arm_compute_library_nn_driver": true,
46 "cuttlefish-soong-rules": true,
47 "gki-soong-rules": true,
48 "hidl-soong-rules": true,
49 "kernel-config-soong-rules": true,
50 "soong-angle-codegen": true,
51 "soong-api": true,
52 "soong-art": true,
53 "soong-ca-certificates": true,
54 "soong-clang": true,
55 "soong-clang-prebuilts": true,
56 "soong-csuite": true,
57 "soong-fluoride": true,
58 "soong-fs_config": true,
59 "soong-icu": true,
60 "soong-java-config-error_prone": true,
61 "soong-libchrome": true,
62 "soong-llvm": true,
63 "soong-robolectric": true,
64 "soong-rust-prebuilts": true,
65 "soong-selinux": true,
66 "soong-wayland-protocol-codegen": true,
67 "treble_report_app": true,
68 "treble_report_local": true,
69 "treble_report_module": true,
70 "vintf-compatibility-matrix-soong-rules": true,
71 "xsdc-soong-rules": true,
72}
73
74const (
75 internalPluginsPath = "vendor/google/build/soong/internal_plugins.json"
76)
77
78type pluginProvider interface {
79 IsPluginFor(string) bool
80}
81
82func maybeAddInternalPluginsToAllowlist(ctx SingletonContext) {
83 if path := ExistentPathForSource(ctx, internalPluginsPath); path.Valid() {
84 ctx.AddNinjaFileDeps(path.String())
85 absPath := absolutePath(path.String())
86 var moreAllowed map[string]bool
87 data, err := ioutil.ReadFile(absPath)
88 if err != nil {
89 ctx.Errorf("Failed to open internal plugins path %q %q", internalPluginsPath, err)
90 }
91 if err := json.Unmarshal(data, &moreAllowed); err != nil {
92 fmt.Fprintf(os.Stderr, "Internal plugins file %q did not parse correctly: %q", data, err)
93 }
94 for k, v := range moreAllowed {
95 allowedPluginsByName[k] = v
96 }
97 }
98}
99
100func (p *pluginSingleton) GenerateBuildActions(ctx SingletonContext) {
101 for _, p := range ctx.DeviceConfig().BuildBrokenPluginValidation() {
102 allowedPluginsByName[p] = true
103 }
104 maybeAddInternalPluginsToAllowlist(ctx)
105
106 disallowedPlugins := map[string]bool{}
107 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
108 if ctx.ModuleType(module) != "bootstrap_go_package" {
109 return
110 }
111
112 p, ok := module.(pluginProvider)
113 if !ok || !p.IsPluginFor("soong_build") {
114 return
115 }
116
117 name := ctx.ModuleName(module)
118 if _, ok := allowedPluginsByName[name]; ok {
119 return
120 }
121
122 dir := ctx.ModuleDir(module)
123
124 // allow use of plugins within Soong to not allowlist everything
125 if strings.HasPrefix(dir, "build/soong") {
126 return
127 }
128
129 // allow third party users outside of external to create new plugins, i.e. non-google paths
130 // under vendor or hardware
131 if !strings.HasPrefix(dir, "external/") && IsThirdPartyPath(dir) {
132 return
133 }
134 disallowedPlugins[name] = true
135 })
136 if len(disallowedPlugins) > 0 {
137 ctx.Errorf("New plugins are not supported; however %q were found. Please reach out to the build team or use BUILD_BROKEN_PLUGIN_VALIDATION (see Changes.md for more info).", SortedStringKeys(disallowedPlugins))
138 }
139}