blob: 9b06426968c9a81a2eb6566591588d9755f92e5d [file] [log] [blame]
Liz Kammerea6666f2021-02-17 10:17:28 -05001// Copyright 2021 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
Liz Kammerba3ea162021-02-17 13:22:03 -050017import (
18 "fmt"
19 "io/ioutil"
20 "path/filepath"
21 "strings"
22
Liz Kammerbdc60992021-02-24 16:55:11 -050023 "github.com/google/blueprint"
Liz Kammerba3ea162021-02-17 13:22:03 -050024 "github.com/google/blueprint/proptools"
25)
26
27type bazelModuleProperties struct {
28 // The label of the Bazel target replacing this Soong module. When run in conversion mode, this
29 // will import the handcrafted build target into the autogenerated file. Note: this may result in
30 // a conflict due to duplicate targets if bp2build_available is also set.
31 Label *string
32
33 // If true, bp2build will generate the converted Bazel target for this module. Note: this may
34 // cause a conflict due to the duplicate targets if label is also set.
Jingwen Chen12b4c272021-03-10 02:05:59 -050035 //
36 // This is a bool pointer to support tristates: true, false, not set.
37 //
38 // To opt-in a module, set bazel_module: { bp2build_available: true }
39 // To opt-out a module, set bazel_module: { bp2build_available: false }
40 // To defer the default setting for the directory, do not set the value.
41 Bp2build_available *bool
Liz Kammerba3ea162021-02-17 13:22:03 -050042}
43
44// Properties contains common module properties for Bazel migration purposes.
45type properties struct {
46 // In USE_BAZEL_ANALYSIS=1 mode, this represents the Bazel target replacing
47 // this Soong module.
48 Bazel_module bazelModuleProperties
49}
Liz Kammerea6666f2021-02-17 10:17:28 -050050
51// BazelModuleBase contains the property structs with metadata for modules which can be converted to
52// Bazel.
53type BazelModuleBase struct {
Liz Kammerba3ea162021-02-17 13:22:03 -050054 bazelProperties properties
Liz Kammerea6666f2021-02-17 10:17:28 -050055}
56
57// Bazelable is specifies the interface for modules that can be converted to Bazel.
58type Bazelable interface {
Liz Kammerba3ea162021-02-17 13:22:03 -050059 bazelProps() *properties
60 HasHandcraftedLabel() bool
Liz Kammerbdc60992021-02-24 16:55:11 -050061 HandcraftedLabel() string
62 GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
Jingwen Chen12b4c272021-03-10 02:05:59 -050063 ConvertWithBp2build(ctx BazelConversionPathContext) bool
Liz Kammerba3ea162021-02-17 13:22:03 -050064 GetBazelBuildFileContents(c Config, path, name string) (string, error)
Jingwen Chen12b4c272021-03-10 02:05:59 -050065 ConvertedToBazel(ctx BazelConversionPathContext) bool
Liz Kammerea6666f2021-02-17 10:17:28 -050066}
67
68// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
69type BazelModule interface {
70 Module
71 Bazelable
72}
73
74// InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
75// properties.
76func InitBazelModule(module BazelModule) {
77 module.AddProperties(module.bazelProps())
78}
79
80// bazelProps returns the Bazel properties for the given BazelModuleBase.
Liz Kammerba3ea162021-02-17 13:22:03 -050081func (b *BazelModuleBase) bazelProps() *properties {
Liz Kammerea6666f2021-02-17 10:17:28 -050082 return &b.bazelProperties
83}
84
Liz Kammerba3ea162021-02-17 13:22:03 -050085// HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
86func (b *BazelModuleBase) HasHandcraftedLabel() bool {
87 return b.bazelProperties.Bazel_module.Label != nil
88}
89
90// HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
91func (b *BazelModuleBase) HandcraftedLabel() string {
92 return proptools.String(b.bazelProperties.Bazel_module.Label)
93}
94
Liz Kammerea6666f2021-02-17 10:17:28 -050095// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
Liz Kammerbdc60992021-02-24 16:55:11 -050096func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
97 if b.HasHandcraftedLabel() {
98 return b.HandcraftedLabel()
99 }
Jingwen Chen12b4c272021-03-10 02:05:59 -0500100 if b.ConvertWithBp2build(ctx) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500101 return bp2buildModuleLabel(ctx, module)
102 }
103 return "" // no label for unconverted module
Liz Kammerea6666f2021-02-17 10:17:28 -0500104}
105
Jingwen Chen12b4c272021-03-10 02:05:59 -0500106// Configuration to decide if modules in a directory should default to true/false for bp2build_available
107type Bp2BuildConfig map[string]BazelConversionConfigEntry
108type BazelConversionConfigEntry int
109
110const (
111 // iota + 1 ensures that the int value is not 0 when used in the Bp2buildAllowlist map,
112 // which can also mean that the key doesn't exist in a lookup.
113
114 // all modules in this package and subpackages default to bp2build_available: true.
115 // allows modules to opt-out.
116 Bp2BuildDefaultTrueRecursively BazelConversionConfigEntry = iota + 1
117
118 // all modules in this package (not recursively) default to bp2build_available: false.
119 // allows modules to opt-in.
120 Bp2BuildDefaultFalse
121)
122
123var (
124 // Configure modules in these directories to enable bp2build_available: true or false by default.
125 bp2buildDefaultConfig = Bp2BuildConfig{
126 "bionic": Bp2BuildDefaultTrueRecursively,
127 "system/core/libcutils": Bp2BuildDefaultTrueRecursively,
128 "system/logging/liblog": Bp2BuildDefaultTrueRecursively,
129 }
130)
131
Liz Kammerea6666f2021-02-17 10:17:28 -0500132// ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build.
Jingwen Chen12b4c272021-03-10 02:05:59 -0500133func (b *BazelModuleBase) ConvertWithBp2build(ctx BazelConversionPathContext) bool {
134 // Ensure that the module type of this module has a bp2build converter. This
135 // prevents mixed builds from using auto-converted modules just by matching
136 // the package dir; it also has to have a bp2build mutator as well.
137 if ctx.Config().bp2buildModuleTypeConfig[ctx.ModuleType()] == false {
138 return false
139 }
140
141 packagePath := ctx.ModuleDir()
142 config := ctx.Config().bp2buildPackageConfig
143
144 // This is a tristate value: true, false, or unset.
145 propValue := b.bazelProperties.Bazel_module.Bp2build_available
146 if bp2buildDefaultTrueRecursively(packagePath, config) {
147 // Allow modules to explicitly opt-out.
148 return proptools.BoolDefault(propValue, true)
149 }
150
151 // Allow modules to explicitly opt-in.
152 return proptools.BoolDefault(propValue, false)
153}
154
155// bp2buildDefaultTrueRecursively checks that the package contains a prefix from the
156// set of package prefixes where all modules must be converted. That is, if the
157// package is x/y/z, and the list contains either x, x/y, or x/y/z, this function will
158// return true.
159//
160// However, if the package is x/y, and it matches a Bp2BuildDefaultFalse "x/y" entry
161// exactly, this module will return false early.
162//
163// This function will also return false if the package doesn't match anything in
164// the config.
165func bp2buildDefaultTrueRecursively(packagePath string, config Bp2BuildConfig) bool {
166 ret := false
167
168 if config[packagePath] == Bp2BuildDefaultFalse {
169 return false
170 }
171
172 packagePrefix := ""
173 // e.g. for x/y/z, iterate over x, x/y, then x/y/z, taking the final value from the allowlist.
174 for _, part := range strings.Split(packagePath, "/") {
175 packagePrefix += part
176 if config[packagePrefix] == Bp2BuildDefaultTrueRecursively {
177 // package contains this prefix and this prefix should convert all modules
178 return true
179 }
180 // Continue to the next part of the package dir.
181 packagePrefix += "/"
182 }
183
184 return ret
Liz Kammerea6666f2021-02-17 10:17:28 -0500185}
Liz Kammerba3ea162021-02-17 13:22:03 -0500186
187// GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or
188// an error if there are errors reading the file.
189// TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
190// something more targeted based on the rule type and target.
191func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500192 if !strings.Contains(b.HandcraftedLabel(), path) {
193 return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.HandcraftedLabel())
Liz Kammerba3ea162021-02-17 13:22:03 -0500194 }
195 name = filepath.Join(path, name)
196 f, err := c.fs.Open(name)
197 if err != nil {
198 return "", err
199 }
200 defer f.Close()
201
202 data, err := ioutil.ReadAll(f)
203 if err != nil {
204 return "", err
205 }
206 return string(data[:]), nil
207}
Liz Kammerbdc60992021-02-24 16:55:11 -0500208
209// ConvertedToBazel returns whether this module has been converted to Bazel, whether automatically
210// or manually
Jingwen Chen12b4c272021-03-10 02:05:59 -0500211func (b *BazelModuleBase) ConvertedToBazel(ctx BazelConversionPathContext) bool {
212 return b.ConvertWithBp2build(ctx) || b.HasHandcraftedLabel()
Liz Kammerbdc60992021-02-24 16:55:11 -0500213}