2021-02-17 16:17:28 +01:00
|
|
|
// Copyright 2021 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package android
|
|
|
|
|
2021-02-17 19:22:03 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2021-02-24 22:55:11 +01:00
|
|
|
"github.com/google/blueprint"
|
2021-02-17 19:22:03 +01:00
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
)
|
|
|
|
|
|
|
|
type bazelModuleProperties struct {
|
|
|
|
// The label of the Bazel target replacing this Soong module. When run in conversion mode, this
|
|
|
|
// will import the handcrafted build target into the autogenerated file. Note: this may result in
|
|
|
|
// a conflict due to duplicate targets if bp2build_available is also set.
|
|
|
|
Label *string
|
|
|
|
|
|
|
|
// If true, bp2build will generate the converted Bazel target for this module. Note: this may
|
|
|
|
// cause a conflict due to the duplicate targets if label is also set.
|
|
|
|
Bp2build_available bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Properties contains common module properties for Bazel migration purposes.
|
|
|
|
type properties struct {
|
|
|
|
// In USE_BAZEL_ANALYSIS=1 mode, this represents the Bazel target replacing
|
|
|
|
// this Soong module.
|
|
|
|
Bazel_module bazelModuleProperties
|
|
|
|
}
|
2021-02-17 16:17:28 +01:00
|
|
|
|
|
|
|
// BazelModuleBase contains the property structs with metadata for modules which can be converted to
|
|
|
|
// Bazel.
|
|
|
|
type BazelModuleBase struct {
|
2021-02-17 19:22:03 +01:00
|
|
|
bazelProperties properties
|
2021-02-17 16:17:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bazelable is specifies the interface for modules that can be converted to Bazel.
|
|
|
|
type Bazelable interface {
|
2021-02-17 19:22:03 +01:00
|
|
|
bazelProps() *properties
|
|
|
|
HasHandcraftedLabel() bool
|
2021-02-24 22:55:11 +01:00
|
|
|
HandcraftedLabel() string
|
|
|
|
GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string
|
2021-02-17 16:17:28 +01:00
|
|
|
ConvertWithBp2build() bool
|
2021-02-17 19:22:03 +01:00
|
|
|
GetBazelBuildFileContents(c Config, path, name string) (string, error)
|
2021-02-24 22:55:11 +01:00
|
|
|
ConvertedToBazel() bool
|
2021-02-17 16:17:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// BazelModule is a lightweight wrapper interface around Module for Bazel-convertible modules.
|
|
|
|
type BazelModule interface {
|
|
|
|
Module
|
|
|
|
Bazelable
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitBazelModule is a wrapper function that decorates a BazelModule with Bazel-conversion
|
|
|
|
// properties.
|
|
|
|
func InitBazelModule(module BazelModule) {
|
|
|
|
module.AddProperties(module.bazelProps())
|
|
|
|
}
|
|
|
|
|
|
|
|
// bazelProps returns the Bazel properties for the given BazelModuleBase.
|
2021-02-17 19:22:03 +01:00
|
|
|
func (b *BazelModuleBase) bazelProps() *properties {
|
2021-02-17 16:17:28 +01:00
|
|
|
return &b.bazelProperties
|
|
|
|
}
|
|
|
|
|
2021-02-17 19:22:03 +01:00
|
|
|
// HasHandcraftedLabel returns whether this module has a handcrafted Bazel label.
|
|
|
|
func (b *BazelModuleBase) HasHandcraftedLabel() bool {
|
|
|
|
return b.bazelProperties.Bazel_module.Label != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandcraftedLabel returns the handcrafted label for this module, or empty string if there is none
|
|
|
|
func (b *BazelModuleBase) HandcraftedLabel() string {
|
|
|
|
return proptools.String(b.bazelProperties.Bazel_module.Label)
|
|
|
|
}
|
|
|
|
|
2021-02-17 16:17:28 +01:00
|
|
|
// GetBazelLabel returns the Bazel label for the given BazelModuleBase.
|
2021-02-24 22:55:11 +01:00
|
|
|
func (b *BazelModuleBase) GetBazelLabel(ctx BazelConversionPathContext, module blueprint.Module) string {
|
|
|
|
if b.HasHandcraftedLabel() {
|
|
|
|
return b.HandcraftedLabel()
|
|
|
|
}
|
|
|
|
if b.ConvertWithBp2build() {
|
|
|
|
return bp2buildModuleLabel(ctx, module)
|
|
|
|
}
|
|
|
|
return "" // no label for unconverted module
|
2021-02-17 16:17:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConvertWithBp2build returns whether the given BazelModuleBase should be converted with bp2build.
|
|
|
|
func (b *BazelModuleBase) ConvertWithBp2build() bool {
|
|
|
|
return b.bazelProperties.Bazel_module.Bp2build_available
|
|
|
|
}
|
2021-02-17 19:22:03 +01:00
|
|
|
|
|
|
|
// GetBazelBuildFileContents returns the file contents of a hand-crafted BUILD file if available or
|
|
|
|
// an error if there are errors reading the file.
|
|
|
|
// TODO(b/181575318): currently we append the whole BUILD file, let's change that to do
|
|
|
|
// something more targeted based on the rule type and target.
|
|
|
|
func (b *BazelModuleBase) GetBazelBuildFileContents(c Config, path, name string) (string, error) {
|
2021-02-24 22:55:11 +01:00
|
|
|
if !strings.Contains(b.HandcraftedLabel(), path) {
|
|
|
|
return "", fmt.Errorf("%q not found in bazel_module.label %q", path, b.HandcraftedLabel())
|
2021-02-17 19:22:03 +01:00
|
|
|
}
|
|
|
|
name = filepath.Join(path, name)
|
|
|
|
f, err := c.fs.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(data[:]), nil
|
|
|
|
}
|
2021-02-24 22:55:11 +01:00
|
|
|
|
|
|
|
// ConvertedToBazel returns whether this module has been converted to Bazel, whether automatically
|
|
|
|
// or manually
|
|
|
|
func (b *BazelModuleBase) ConvertedToBazel() bool {
|
|
|
|
return b.ConvertWithBp2build() || b.HasHandcraftedLabel()
|
|
|
|
}
|