// 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 cc import ( "fmt" "path/filepath" "strings" "android/soong/android" "android/soong/bazel" "github.com/google/blueprint/proptools" ) // bp2build functions and helpers for converting cc_* modules to Bazel. func init() { android.DepsBp2BuildMutators(RegisterDepsBp2Build) } func RegisterDepsBp2Build(ctx android.RegisterMutatorsContext) { ctx.BottomUp("cc_bp2build_deps", depsBp2BuildMutator) } // A naive deps mutator to add deps on all modules across all combinations of // target props for cc modules. This is needed to make module -> bazel label // resolution work in the bp2build mutator later. This is probably // the wrong way to do it, but it works. // // TODO(jingwen): can we create a custom os mutator in depsBp2BuildMutator to do this? func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) { module, ok := ctx.Module().(*Module) if !ok { // Not a cc module return } if !module.ConvertWithBp2build(ctx) { return } var allDeps []string for _, configToProps := range module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) { for _, props := range configToProps { if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { allDeps = append(allDeps, baseCompilerProps.Generated_headers...) allDeps = append(allDeps, baseCompilerProps.Generated_sources...) } } } for _, configToProps := range module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) { for _, props := range configToProps { if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok { allDeps = append(allDeps, baseLinkerProps.Header_libs...) allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...) allDeps = append(allDeps, baseLinkerProps.Static_libs...) allDeps = append(allDeps, baseLinkerProps.Exclude_static_libs...) allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...) allDeps = append(allDeps, baseLinkerProps.Shared_libs...) allDeps = append(allDeps, baseLinkerProps.Exclude_shared_libs...) } } } // Deps in the static: { .. } and shared: { .. } props of a cc_library. if lib, ok := module.compiler.(*libraryDecorator); ok { appendDeps := func(deps []string, p StaticOrSharedProperties) []string { deps = append(deps, p.Static_libs...) deps = append(deps, p.Whole_static_libs...) deps = append(deps, p.Shared_libs...) return deps } allDeps = appendDeps(allDeps, lib.SharedProperties.Shared) allDeps = appendDeps(allDeps, lib.StaticProperties.Static) // TODO(b/186024507, b/186489250): Temporarily exclude adding // system_shared_libs deps until libc and libm builds. // allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...) // allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...) // Deps in the target/arch nested static: { .. } and shared: { .. } props of a cc_library. // target: { : shared: { ... } } for _, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) { for _, props := range configToProps { if p, ok := props.(*SharedProperties); ok { allDeps = appendDeps(allDeps, p.Shared) } } } for _, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) { for _, props := range configToProps { if p, ok := props.(*StaticProperties); ok { allDeps = appendDeps(allDeps, p.Static) } } } } // product variables only support a limited set of fields, this is the full list of field names // related to cc module dependency management that are supported. productVariableDepFields := [4]string{ "Shared_libs", "Static_libs", "Exclude_static_libs", "Whole_static_libs", } productVariableProps := android.ProductVariableProperties(ctx) for _, name := range productVariableDepFields { props, exists := productVariableProps[name] if !exists { continue } for _, prop := range props { if p, ok := prop.Property.([]string); !ok { ctx.ModuleErrorf("Could not convert product variable %s property", name) } else { allDeps = append(allDeps, p...) } } } ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...) } // staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties -- // properties which apply to either the shared or static version of a cc_library module. type staticOrSharedAttributes struct { Srcs bazel.LabelListAttribute Srcs_c bazel.LabelListAttribute Srcs_as bazel.LabelListAttribute Copts bazel.StringListAttribute Static_deps bazel.LabelListAttribute Dynamic_deps bazel.LabelListAttribute Whole_archive_deps bazel.LabelListAttribute } func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) (cppSrcs, cSrcs, asSrcs bazel.LabelListAttribute) { // Branch srcs into three language-specific groups. // C++ is the "catch-all" group, and comprises generated sources because we don't // know the language of these sources until the genrule is executed. // TODO(b/190006308): Handle language detection of sources in a Bazel rule. isCSrcOrFilegroup := func(s string) bool { return strings.HasSuffix(s, ".c") || strings.HasSuffix(s, "_c_srcs") } isAsmSrcOrFilegroup := func(s string) bool { return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s") || strings.HasSuffix(s, "_as_srcs") } // Check that a module is a filegroup type named