blob: 683495bd6a8a238557ef7ca6546d33b7845f7356 [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.
35 Bp2build_available bool
36}
37
38// Properties contains common module properties for Bazel migration purposes.
39type properties struct {
40 // In USE_BAZEL_ANALYSIS=1 mode, this represents the Bazel target replacing
41 // this Soong module.
42 Bazel_module bazelModuleProperties
43}
Liz Kammerea6666f2021-02-17 10:17:28 -050044
45// BazelModuleBase contains the property structs with metadata for modules which can be converted to
46// Bazel.
47type BazelModuleBase struct {
Liz Kammerba3ea162021-02-17 13:22:03 -050048 bazelProperties properties
Liz Kammerea6666f2021-02-17 10:17:28 -050049}
50
51// Bazelable is specifies the interface for modules that can be converted to Bazel.
52type Bazelable interface {
Liz Kammerba3ea162021-02-17 13:22:03 -050053 bazelProps() *properties
54 HasHandcraftedLabel() bool
Liz Kammerbdc60992021-02-24 16:55:11 -050055 HandcraftedLabel() string
56 GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
Liz Kammerea6666f2021-02-17 10:17:28 -050057 ConvertWithBp2build() bool
Liz Kammerba3ea162021-02-17 13:22:03 -050058 GetBazelBuildFileContents(c Config, path, name string) (string, error)
Liz Kammerbdc60992021-02-24 16:55:11 -050059 ConvertedToBazel() bool
Liz Kammerea6666f2021-02-17 10:17:28 -050060}
61
62// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
63type BazelModule interface {
64 Module
65 Bazelable
66}
67
68// InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
69// properties.
70func InitBazelModule(module BazelModule) {
71 module.AddProperties(module.bazelProps())
72}
73
74// bazelProps returns the Bazel properties for the given BazelModuleBase.
Liz Kammerba3ea162021-02-17 13:22:03 -050075func (b *BazelModuleBase) bazelProps() *properties {
Liz Kammerea6666f2021-02-17 10:17:28 -050076 return &b.bazelProperties
77}
78
Liz Kammerba3ea162021-02-17 13:22:03 -050079// HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
80func (b *BazelModuleBase) HasHandcraftedLabel() bool {
81 return b.bazelProperties.Bazel_module.Label != nil
82}
83
84// HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
85func (b *BazelModuleBase) HandcraftedLabel() string {
86 return proptools.String(b.bazelProperties.Bazel_module.Label)
87}
88
Liz Kammerea6666f2021-02-17 10:17:28 -050089// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
Liz Kammerbdc60992021-02-24 16:55:11 -050090func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
91 if b.HasHandcraftedLabel() {
92 return b.HandcraftedLabel()
93 }
94 if b.ConvertWithBp2build() {
95 return bp2buildModuleLabel(ctx, module)
96 }
97 return "" // no label for unconverted module
Liz Kammerea6666f2021-02-17 10:17:28 -050098}
99
100// ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build.
101func (b *BazelModuleBase) ConvertWithBp2build() bool {
102 return b.bazelProperties.Bazel_module.Bp2build_available
103}
Liz Kammerba3ea162021-02-17 13:22:03 -0500104
105// GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or
106// an error if there are errors reading the file.
107// TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
108// something more targeted based on the rule type and target.
109func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) {
Liz Kammerbdc60992021-02-24 16:55:11 -0500110 if !strings.Contains(b.HandcraftedLabel(), path) {
111 return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.HandcraftedLabel())
Liz Kammerba3ea162021-02-17 13:22:03 -0500112 }
113 name = filepath.Join(path, name)
114 f, err := c.fs.Open(name)
115 if err != nil {
116 return "", err
117 }
118 defer f.Close()
119
120 data, err := ioutil.ReadAll(f)
121 if err != nil {
122 return "", err
123 }
124 return string(data[:]), nil
125}
Liz Kammerbdc60992021-02-24 16:55:11 -0500126
127// ConvertedToBazel returns whether this module has been converted to Bazel, whether automatically
128// or manually
129func (b *BazelModuleBase) ConvertedToBazel() bool {
130 return b.ConvertWithBp2build() || b.HasHandcraftedLabel()
131}