blob: 4672453ee41cb3afb890ccb5ea5cec3d0920253e [file] [log] [blame]
Liz Kammer33cc80e2023-05-18 18:20:28 +00001// 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) {
LaMont Jones9b44c2a2023-05-22 19:06:56 +000032 ctx.RegisterParallelSingletonType("plugins", pluginSingletonFactory)
Liz Kammer33cc80e2023-05-18 18:20:28 +000033}
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-ca-certificates-apex": true,
55 "soong-clang": true,
56 "soong-clang-prebuilts": true,
57 "soong-csuite": true,
58 "soong-fluoride": true,
59 "soong-fs_config": true,
60 "soong-icu": true,
61 "soong-java-config-error_prone": true,
62 "soong-libchrome": true,
63 "soong-llvm": true,
64 "soong-robolectric": true,
65 "soong-rust-prebuilts": true,
66 "soong-selinux": true,
67 "soong-wayland-protocol-codegen": true,
68 "treble_report_app": true,
69 "treble_report_local": true,
70 "treble_report_module": true,
71 "vintf-compatibility-matrix-soong-rules": true,
72 "xsdc-soong-rules": true,
73}
74
75const (
76 internalPluginsPath = "vendor/google/build/soong/internal_plugins.json"
77)
78
79type pluginProvider interface {
80 IsPluginFor(string) bool
81}
82
83func maybeAddInternalPluginsToAllowlist(ctx SingletonContext) {
84 if path := ExistentPathForSource(ctx, internalPluginsPath); path.Valid() {
85 ctx.AddNinjaFileDeps(path.String())
86 absPath := absolutePath(path.String())
87 var moreAllowed map[string]bool
88 data, err := ioutil.ReadFile(absPath)
89 if err != nil {
90 ctx.Errorf("Failed to open internal plugins path %q %q", internalPluginsPath, err)
91 }
92 if err := json.Unmarshal(data, &moreAllowed); err != nil {
93 fmt.Fprintf(os.Stderr, "Internal plugins file %q did not parse correctly: %q", data, err)
94 }
95 for k, v := range moreAllowed {
96 allowedPluginsByName[k] = v
97 }
98 }
99}
100
101func (p *pluginSingleton) GenerateBuildActions(ctx SingletonContext) {
102 for _, p := range ctx.DeviceConfig().BuildBrokenPluginValidation() {
103 allowedPluginsByName[p] = true
104 }
105 maybeAddInternalPluginsToAllowlist(ctx)
106
107 disallowedPlugins := map[string]bool{}
108 ctx.VisitAllModulesBlueprint(func(module blueprint.Module) {
109 if ctx.ModuleType(module) != "bootstrap_go_package" {
110 return
111 }
112
113 p, ok := module.(pluginProvider)
114 if !ok || !p.IsPluginFor("soong_build") {
115 return
116 }
117
118 name := ctx.ModuleName(module)
119 if _, ok := allowedPluginsByName[name]; ok {
120 return
121 }
122
123 dir := ctx.ModuleDir(module)
124
125 // allow use of plugins within Soong to not allowlist everything
126 if strings.HasPrefix(dir, "build/soong") {
127 return
128 }
129
130 // allow third party users outside of external to create new plugins, i.e. non-google paths
131 // under vendor or hardware
132 if !strings.HasPrefix(dir, "external/") && IsThirdPartyPath(dir) {
133 return
134 }
135 disallowedPlugins[name] = true
136 })
137 if len(disallowedPlugins) > 0 {
138 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))
139 }
140}