blob: a7cabcc859a4af2285fd667ceb8a442d25ea03b6 [file] [log] [blame]
Jingwen Chen91220d72021-03-24 02:18:33 -04001// 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.
14package cc
15
16import (
Liz Kammerd2871182021-10-04 13:54:37 -040017 "fmt"
Jingwen Chened9c17d2021-04-13 07:14:55 +000018 "path/filepath"
Jingwen Chen3950cd62021-05-12 04:33:00 +000019 "strings"
Chris Parsons484e50a2021-05-13 15:13:04 -040020
21 "android/soong/android"
22 "android/soong/bazel"
Liz Kammer7a210ac2021-09-22 15:52:58 -040023
Chris Parsons953b3562021-09-20 15:14:39 -040024 "github.com/google/blueprint"
Liz Kammerba7a9c52021-05-26 08:45:30 -040025
26 "github.com/google/blueprint/proptools"
Jingwen Chen91220d72021-03-24 02:18:33 -040027)
28
Liz Kammerae3994e2021-10-19 09:45:48 -040029const (
30 cSrcPartition = "c"
31 asSrcPartition = "as"
32 cppSrcPartition = "cpp"
33)
34
Liz Kammer2222c6b2021-05-24 15:41:47 -040035// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
Jingwen Chenbcf53042021-05-26 04:42:42 +000036// properties which apply to either the shared or static version of a cc_library module.
Liz Kammer2222c6b2021-05-24 15:41:47 -040037type staticOrSharedAttributes struct {
Jingwen Chenc4dc9b42021-06-11 12:51:48 +000038 Srcs bazel.LabelListAttribute
39 Srcs_c bazel.LabelListAttribute
40 Srcs_as bazel.LabelListAttribute
41 Copts bazel.StringListAttribute
Jingwen Chen14a8bda2021-06-02 11:10:02 +000042
Liz Kammer7a210ac2021-09-22 15:52:58 -040043 Deps bazel.LabelListAttribute
44 Implementation_deps bazel.LabelListAttribute
45 Dynamic_deps bazel.LabelListAttribute
46 Implementation_dynamic_deps bazel.LabelListAttribute
47 Whole_archive_deps bazel.LabelListAttribute
Chris Parsons51f8c392021-08-03 21:01:05 -040048
49 System_dynamic_deps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +000050}
51
Liz Kammerae3994e2021-10-19 09:45:48 -040052func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute {
Jingwen Chen14a8bda2021-06-02 11:10:02 +000053 // Check that a module is a filegroup type named <label>.
54 isFilegroupNamed := func(m android.Module, fullLabel string) bool {
55 if ctx.OtherModuleType(m) != "filegroup" {
56 return false
57 }
58 labelParts := strings.Split(fullLabel, ":")
59 if len(labelParts) > 2 {
60 // There should not be more than one colon in a label.
Liz Kammer57e2e7a2021-09-20 12:55:02 -040061 ctx.ModuleErrorf("%s is not a valid Bazel label for a filegroup", fullLabel)
Jingwen Chen14a8bda2021-06-02 11:10:02 +000062 }
Liz Kammer57e2e7a2021-09-20 12:55:02 -040063 return m.Name() == labelParts[len(labelParts)-1]
Jingwen Chen14a8bda2021-06-02 11:10:02 +000064 }
65
Liz Kammer57e2e7a2021-09-20 12:55:02 -040066 // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl
67 // macro.
68 addSuffixForFilegroup := func(suffix string) bazel.LabelMapper {
69 return func(ctx bazel.OtherModuleContext, label string) (string, bool) {
70 m, exists := ctx.ModuleFromName(label)
71 if !exists {
72 return label, false
Jingwen Chen14a8bda2021-06-02 11:10:02 +000073 }
Chris Parsons5a34ffb2021-07-21 14:34:58 -040074 aModule, _ := m.(android.Module)
Liz Kammer57e2e7a2021-09-20 12:55:02 -040075 if !isFilegroupNamed(aModule, label) {
76 return label, false
Jingwen Chen14a8bda2021-06-02 11:10:02 +000077 }
Liz Kammer57e2e7a2021-09-20 12:55:02 -040078 return label + suffix, true
Chris Parsons5a34ffb2021-07-21 14:34:58 -040079 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +000080 }
81
Liz Kammer57e2e7a2021-09-20 12:55:02 -040082 // TODO(b/190006308): Handle language detection of sources in a Bazel rule.
83 partitioned := bazel.PartitionLabelListAttribute(ctx, &srcs, bazel.LabelPartitions{
Liz Kammerae3994e2021-10-19 09:45:48 -040084 cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")},
85 asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")},
Liz Kammer57e2e7a2021-09-20 12:55:02 -040086 // C++ is the "catch-all" group, and comprises generated sources because we don't
87 // know the language of these sources until the genrule is executed.
Liz Kammerae3994e2021-10-19 09:45:48 -040088 cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true},
Liz Kammer57e2e7a2021-09-20 12:55:02 -040089 })
Jingwen Chen14a8bda2021-06-02 11:10:02 +000090
Liz Kammerae3994e2021-10-19 09:45:48 -040091 return partitioned
Jingwen Chen14a8bda2021-06-02 11:10:02 +000092}
93
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000094// bp2BuildParseLibProps returns the attributes for a variant of a cc_library.
95func bp2BuildParseLibProps(ctx android.TopDownMutatorContext, module *Module, isStatic bool) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +000096 lib, ok := module.compiler.(*libraryDecorator)
97 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -040098 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +000099 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000100 return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic)
101}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000102
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000103// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
104func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
105 return bp2BuildParseLibProps(ctx, module, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000106}
107
108// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400109func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000110 return bp2BuildParseLibProps(ctx, module, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400111}
112
Liz Kammer7a210ac2021-09-22 15:52:58 -0400113type depsPartition struct {
114 export bazel.LabelList
115 implementation bazel.LabelList
116}
117
118type bazelLabelForDepsFn func(android.TopDownMutatorContext, []string) bazel.LabelList
119
120func partitionExportedAndImplementationsDeps(ctx android.TopDownMutatorContext, allDeps, exportedDeps []string, fn bazelLabelForDepsFn) depsPartition {
121 implementation, export := android.FilterList(allDeps, exportedDeps)
122
123 return depsPartition{
124 export: fn(ctx, export),
125 implementation: fn(ctx, implementation),
126 }
127}
128
129type bazelLabelForDepsExcludesFn func(android.TopDownMutatorContext, []string, []string) bazel.LabelList
130
131func partitionExportedAndImplementationsDepsExcludes(ctx android.TopDownMutatorContext, allDeps, excludes, exportedDeps []string, fn bazelLabelForDepsExcludesFn) depsPartition {
132 implementation, export := android.FilterList(allDeps, exportedDeps)
133
134 return depsPartition{
135 export: fn(ctx, export, excludes),
136 implementation: fn(ctx, implementation, excludes),
137 }
138}
139
Jingwen Chenbcf53042021-05-26 04:42:42 +0000140func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes {
Liz Kammer135bf552021-08-11 10:46:06 -0400141 attrs := staticOrSharedAttributes{}
Jingwen Chenbcf53042021-05-26 04:42:42 +0000142
Liz Kammer9abd62d2021-05-21 08:37:59 -0400143 setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000144 attrs.Copts.SetSelectValue(axis, config, props.Cflags)
145 attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
Chris Parsons953b3562021-09-20 15:14:39 -0400146 attrs.System_dynamic_deps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, props.System_shared_libs))
Liz Kammer7a210ac2021-09-22 15:52:58 -0400147
148 staticDeps := partitionExportedAndImplementationsDeps(ctx, props.Static_libs, props.Export_static_lib_headers, bazelLabelForStaticDeps)
149 attrs.Deps.SetSelectValue(axis, config, staticDeps.export)
150 attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation)
151
152 sharedDeps := partitionExportedAndImplementationsDeps(ctx, props.Shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDeps)
153 attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export)
154 attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation)
155
156 attrs.Whole_archive_deps.SetSelectValue(axis, config, bazelLabelForWholeDeps(ctx, props.Whole_static_libs))
Jingwen Chenbcf53042021-05-26 04:42:42 +0000157 }
Liz Kammer135bf552021-08-11 10:46:06 -0400158 // system_dynamic_deps distinguishes between nil/empty list behavior:
159 // nil -> use default values
160 // empty list -> no values specified
161 attrs.System_dynamic_deps.ForceSpecifyEmptyList = true
Jingwen Chenbcf53042021-05-26 04:42:42 +0000162
163 if isStatic {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400164 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) {
165 for config, props := range configToProps {
166 if staticOrSharedProps, ok := props.(*StaticProperties); ok {
167 setAttrs(axis, config, staticOrSharedProps.Static)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000168 }
169 }
170 }
171 } else {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400172 for axis, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) {
173 for config, props := range configToProps {
174 if staticOrSharedProps, ok := props.(*SharedProperties); ok {
175 setAttrs(axis, config, staticOrSharedProps.Shared)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000176 }
177 }
178 }
179 }
180
Liz Kammerae3994e2021-10-19 09:45:48 -0400181 partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs)
182 attrs.Srcs = partitionedSrcs[cppSrcPartition]
183 attrs.Srcs_c = partitionedSrcs[cSrcPartition]
184 attrs.Srcs_as = partitionedSrcs[asSrcPartition]
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000185
Jingwen Chenbcf53042021-05-26 04:42:42 +0000186 return attrs
Jingwen Chen53681ef2021-04-29 08:15:13 +0000187}
188
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400189// Convenience struct to hold all attributes parsed from prebuilt properties.
190type prebuiltAttributes struct {
191 Src bazel.LabelAttribute
192}
193
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000194// NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400195func Bp2BuildParsePrebuiltLibraryProps(ctx android.TopDownMutatorContext, module *Module) prebuiltAttributes {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400196 var srcLabelAttribute bazel.LabelAttribute
197
Liz Kammer9abd62d2021-05-21 08:37:59 -0400198 for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltLinkerProperties{}) {
199 for config, props := range configToProps {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400200 if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok {
201 if len(prebuiltLinkerProperties.Srcs) > 1 {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400202 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for %s %s\n", axis, config)
203 continue
204 } else if len(prebuiltLinkerProperties.Srcs) == 0 {
205 continue
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400206 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400207 src := android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0])
208 srcLabelAttribute.SetSelectValue(axis, config, src)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400209 }
210 }
211 }
212
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400213 return prebuiltAttributes{
214 Src: srcLabelAttribute,
215 }
216}
217
Jingwen Chen107c0de2021-04-09 10:43:12 +0000218// Convenience struct to hold all attributes parsed from compiler properties.
219type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400220 // Options for all languages
221 copts bazel.StringListAttribute
222 // Assembly options and sources
223 asFlags bazel.StringListAttribute
224 asSrcs bazel.LabelListAttribute
225 // C options and sources
226 conlyFlags bazel.StringListAttribute
227 cSrcs bazel.LabelListAttribute
228 // C++ options and sources
229 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000230 srcs bazel.LabelListAttribute
Chris Parsons2c788392021-08-10 11:58:07 -0400231
232 rtti bazel.BoolAttribute
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000233
234 // Not affected by arch variants
235 stl *string
236 cppStd *string
Liz Kammer35687bc2021-09-10 10:07:07 -0400237
238 localIncludes bazel.StringListAttribute
239 absoluteIncludes bazel.StringListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000240}
241
Jingwen Chen63930982021-03-24 10:04:33 -0400242// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000243func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
Jingwen Chen882bcc12021-04-27 05:54:20 +0000244 var srcs bazel.LabelListAttribute
Liz Kammerae3994e2021-10-19 09:45:48 -0400245 var implementationHdrs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000246 var copts bazel.StringListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400247 var asFlags bazel.StringListAttribute
248 var conlyFlags bazel.StringListAttribute
249 var cppFlags bazel.StringListAttribute
Chris Parsons2c788392021-08-10 11:58:07 -0400250 var rtti bazel.BoolAttribute
Liz Kammer35687bc2021-09-10 10:07:07 -0400251 var localIncludes bazel.StringListAttribute
252 var absoluteIncludes bazel.StringListAttribute
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000253 var stl *string = nil
254 var cppStd *string = nil
Jingwen Chened9c17d2021-04-13 07:14:55 +0000255
Chris Parsons990c4f42021-05-25 12:10:58 -0400256 parseCommandLineFlags := func(soongFlags []string) []string {
257 var result []string
258 for _, flag := range soongFlags {
Colin Cross52aa4e12021-05-25 15:20:39 +0000259 // Soong's cflags can contain spaces, like `-include header.h`. For
260 // Bazel's copts, split them up to be compatible with the
261 // no_copts_tokenization feature.
Chris Parsons990c4f42021-05-25 12:10:58 -0400262 result = append(result, strings.Split(flag, " ")...)
Colin Cross52aa4e12021-05-25 15:20:39 +0000263 }
Chris Parsons990c4f42021-05-25 12:10:58 -0400264 return result
265 }
266
Liz Kammer74deed42021-06-02 13:02:03 -0400267 // Parse srcs from an arch or OS's props value.
Liz Kammer222bdcf2021-10-11 14:15:51 -0400268 parseSrcs := func(props *BaseCompilerProperties) (bazel.LabelList, bool) {
269 anySrcs := false
Chris Parsons484e50a2021-05-13 15:13:04 -0400270 // Add srcs-like dependencies such as generated files.
271 // First create a LabelList containing these dependencies, then merge the values with srcs.
Liz Kammerae3994e2021-10-19 09:45:48 -0400272 generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources)
273 if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 {
Liz Kammer222bdcf2021-10-11 14:15:51 -0400274 anySrcs = true
275 }
Chris Parsons484e50a2021-05-13 15:13:04 -0400276
Liz Kammer222bdcf2021-10-11 14:15:51 -0400277 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs)
278 if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 {
279 anySrcs = true
280 }
Liz Kammerae3994e2021-10-19 09:45:48 -0400281 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs
Jingwen Chene32e9e02021-04-23 09:17:24 +0000282 }
283
Liz Kammer9abd62d2021-05-21 08:37:59 -0400284 archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
Liz Kammer9abd62d2021-05-21 08:37:59 -0400285 for axis, configToProps := range archVariantCompilerProps {
286 for config, props := range configToProps {
287 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
288 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
289 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
Liz Kammer222bdcf2021-10-11 14:15:51 -0400290 if srcsList, ok := parseSrcs(baseCompilerProps); ok {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400291 srcs.SetSelectValue(axis, config, srcsList)
Liz Kammer9abd62d2021-05-21 08:37:59 -0400292 }
Liz Kammerae3994e2021-10-19 09:45:48 -0400293 if len(baseCompilerProps.Generated_headers) > 0 {
294 implementationHdrs.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, baseCompilerProps.Generated_headers))
295 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400296
Jingwen Chen97b85312021-10-08 10:41:31 +0000297 if axis == bazel.NoConfigAxis {
298 // If cpp_std is not specified, don't generate it in the
299 // BUILD file. For readability purposes, cpp_std and gnu_extensions are
300 // combined into a single -std=<version> copt, except in the
301 // default case where cpp_std is nil and gnu_extensions is true or unspecified,
302 // then the toolchain's default "gnu++17" will be used.
303 if baseCompilerProps.Cpp_std != nil {
304 // TODO(b/202491296): Handle C_std.
305 // These transformations are shared with compiler.go.
306 cppStdVal := parseCppStd(baseCompilerProps.Cpp_std)
307 _, cppStdVal = maybeReplaceGnuToC(baseCompilerProps.Gnu_extensions, "", cppStdVal)
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000308 cppStd = &cppStdVal
Jingwen Chen97b85312021-10-08 10:41:31 +0000309 } else if baseCompilerProps.Gnu_extensions != nil && !*baseCompilerProps.Gnu_extensions {
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000310 cppStdVal := "c++17"
311 cppStd = &cppStdVal
Jingwen Chen97b85312021-10-08 10:41:31 +0000312 }
313 }
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000314
315 var archVariantCopts []string
Jingwen Chen97b85312021-10-08 10:41:31 +0000316 archVariantCopts = append(archVariantCopts, parseCommandLineFlags(baseCompilerProps.Cflags)...)
Chris Parsons69fa9f92021-07-13 11:47:44 -0400317 archVariantAsflags := parseCommandLineFlags(baseCompilerProps.Asflags)
Liz Kammer35687bc2021-09-10 10:07:07 -0400318
319 localIncludeDirs := baseCompilerProps.Local_include_dirs
320 if axis == bazel.NoConfigAxis && includeBuildDirectory(baseCompilerProps.Include_build_directory) {
321 localIncludeDirs = append(localIncludeDirs, ".")
Chris Parsons69fa9f92021-07-13 11:47:44 -0400322 }
323
Liz Kammer35687bc2021-09-10 10:07:07 -0400324 absoluteIncludes.SetSelectValue(axis, config, baseCompilerProps.Include_dirs)
325 localIncludes.SetSelectValue(axis, config, localIncludeDirs)
Liz Kammer135bf552021-08-11 10:46:06 -0400326
Chris Parsons69fa9f92021-07-13 11:47:44 -0400327 copts.SetSelectValue(axis, config, archVariantCopts)
328 asFlags.SetSelectValue(axis, config, archVariantAsflags)
Liz Kammer9abd62d2021-05-21 08:37:59 -0400329 conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Conlyflags))
330 cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Cppflags))
Chris Parsons2c788392021-08-10 11:58:07 -0400331 rtti.SetSelectValue(axis, config, baseCompilerProps.Rtti)
Liz Kammer9abd62d2021-05-21 08:37:59 -0400332 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000333 }
334 }
335
Liz Kammer74deed42021-06-02 13:02:03 -0400336 srcs.ResolveExcludes()
Liz Kammerae3994e2021-10-19 09:45:48 -0400337 partitionedSrcs := groupSrcsByExtension(ctx, srcs)
338
339 for p, lla := range partitionedSrcs {
340 // if there are no sources, there is no need for headers
341 if lla.IsEmpty() {
342 continue
343 }
344 lla.Append(implementationHdrs)
345 partitionedSrcs[p] = lla
346 }
347
348 srcs = partitionedSrcs[cppSrcPartition]
349 cSrcs := partitionedSrcs[cSrcPartition]
350 asSrcs := partitionedSrcs[asSrcPartition]
351
Liz Kammer35687bc2021-09-10 10:07:07 -0400352 absoluteIncludes.DeduplicateAxesFromBase()
353 localIncludes.DeduplicateAxesFromBase()
Jingwen Chenc1c26502021-04-05 10:35:13 +0000354
Liz Kammerba7a9c52021-05-26 08:45:30 -0400355 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
356 "Cflags": &copts,
357 "Asflags": &asFlags,
358 "CppFlags": &cppFlags,
359 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400360 productVariableProps := android.ProductVariableProperties(ctx)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400361 for propName, attr := range productVarPropNameToAttribute {
362 if props, exists := productVariableProps[propName]; exists {
363 for _, prop := range props {
364 flags, ok := prop.Property.([]string)
365 if !ok {
366 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
367 }
368 newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable)
Liz Kammer47535c52021-06-02 16:02:22 -0400369 attr.SetSelectValue(bazel.ProductVariableConfigurationAxis(prop.FullConfig), prop.FullConfig, newFlags)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400370 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400371 }
372 }
373
Chris Parsonsa967f252021-09-23 16:34:35 -0400374 stlPropsByArch := module.GetArchVariantProperties(ctx, &StlProperties{})
375 for _, configToProps := range stlPropsByArch {
376 for _, props := range configToProps {
377 if stlProps, ok := props.(*StlProperties); ok {
378 if stlProps.Stl != nil {
379 if stl == nil {
380 stl = stlProps.Stl
381 } else {
382 if stl != stlProps.Stl {
383 ctx.ModuleErrorf("Unsupported conversion: module with different stl for different variants: %s and %s", *stl, stlProps.Stl)
384 }
385 }
386 }
387 }
388 }
389 }
390
Jingwen Chen107c0de2021-04-09 10:43:12 +0000391 return compilerAttributes{
Liz Kammer35687bc2021-09-10 10:07:07 -0400392 copts: copts,
393 srcs: srcs,
394 asFlags: asFlags,
395 asSrcs: asSrcs,
396 cSrcs: cSrcs,
397 conlyFlags: conlyFlags,
398 cppFlags: cppFlags,
399 rtti: rtti,
Chris Parsonsa967f252021-09-23 16:34:35 -0400400 stl: stl,
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000401 cppStd: cppStd,
Liz Kammer35687bc2021-09-10 10:07:07 -0400402 localIncludes: localIncludes,
403 absoluteIncludes: absoluteIncludes,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000404 }
405}
406
407// Convenience struct to hold all attributes parsed from linker properties.
408type linkerAttributes struct {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400409 deps bazel.LabelListAttribute
410 implementationDeps bazel.LabelListAttribute
411 dynamicDeps bazel.LabelListAttribute
412 implementationDynamicDeps bazel.LabelListAttribute
413 wholeArchiveDeps bazel.LabelListAttribute
414 systemDynamicDeps bazel.LabelListAttribute
415
Jingwen Chen6ada5892021-09-17 11:38:09 +0000416 linkCrt bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000417 useLibcrt bazel.BoolAttribute
418 linkopts bazel.StringListAttribute
Liz Kammerd2871182021-10-04 13:54:37 -0400419 additionalLinkerInputs bazel.LabelListAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000420 stripKeepSymbols bazel.BoolAttribute
421 stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
422 stripKeepSymbolsList bazel.StringListAttribute
423 stripAll bazel.BoolAttribute
424 stripNone bazel.BoolAttribute
Liz Kammer0eae52e2021-10-06 10:32:26 -0400425 features bazel.StringListAttribute
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400426}
427
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200428// bp2BuildParseLinkerProps parses the linker properties of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400429// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000430func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400431
Liz Kammer47535c52021-06-02 16:02:22 -0400432 var headerDeps bazel.LabelListAttribute
Liz Kammer7a210ac2021-09-22 15:52:58 -0400433 var implementationHeaderDeps bazel.LabelListAttribute
434 var deps bazel.LabelListAttribute
435 var implementationDeps bazel.LabelListAttribute
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400436 var dynamicDeps bazel.LabelListAttribute
Liz Kammer7a210ac2021-09-22 15:52:58 -0400437 var implementationDynamicDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400438 var wholeArchiveDeps bazel.LabelListAttribute
Liz Kammer135bf552021-08-11 10:46:06 -0400439 systemSharedDeps := bazel.LabelListAttribute{ForceSpecifyEmptyList: true}
Liz Kammer7a210ac2021-09-22 15:52:58 -0400440
Jingwen Chen63930982021-03-24 10:04:33 -0400441 var linkopts bazel.StringListAttribute
Jingwen Chen6ada5892021-09-17 11:38:09 +0000442 var linkCrt bazel.BoolAttribute
Liz Kammerd2871182021-10-04 13:54:37 -0400443 var additionalLinkerInputs bazel.LabelListAttribute
Liz Kammerd366c902021-06-03 13:43:01 -0400444 var useLibcrt bazel.BoolAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400445
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000446 var stripKeepSymbols bazel.BoolAttribute
447 var stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
448 var stripKeepSymbolsList bazel.StringListAttribute
449 var stripAll bazel.BoolAttribute
450 var stripNone bazel.BoolAttribute
451
Liz Kammer0eae52e2021-10-06 10:32:26 -0400452 var features bazel.StringListAttribute
453
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000454 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StripProperties{}) {
455 for config, props := range configToProps {
456 if stripProperties, ok := props.(*StripProperties); ok {
457 stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols)
458 stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list)
459 stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame)
460 stripAll.SetSelectValue(axis, config, stripProperties.Strip.All)
461 stripNone.SetSelectValue(axis, config, stripProperties.Strip.None)
462 }
463 }
464 }
465
Jingwen Chen6ada5892021-09-17 11:38:09 +0000466 // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module
467 var disallowedArchVariantCrt bool
468
Liz Kammer9abd62d2021-05-21 08:37:59 -0400469 for axis, configToProps := range module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) {
470 for config, props := range configToProps {
471 if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
Liz Kammer0eae52e2021-10-06 10:32:26 -0400472 var axisFeatures []string
Liz Kammer7a210ac2021-09-22 15:52:58 -0400473
Liz Kammer135bf552021-08-11 10:46:06 -0400474 // Excludes to parallel Soong:
475 // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0
Liz Kammer47535c52021-06-02 16:02:22 -0400476 staticLibs := android.FirstUniqueStrings(baseLinkerProps.Static_libs)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400477 staticDeps := partitionExportedAndImplementationsDepsExcludes(ctx, staticLibs, baseLinkerProps.Exclude_static_libs, baseLinkerProps.Export_static_lib_headers, bazelLabelForStaticDepsExcludes)
478 deps.SetSelectValue(axis, config, staticDeps.export)
479 implementationDeps.SetSelectValue(axis, config, staticDeps.implementation)
480
481 wholeStaticLibs := android.FirstUniqueStrings(baseLinkerProps.Whole_static_libs)
482 wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, baseLinkerProps.Exclude_static_libs))
Chris Parsons51f8c392021-08-03 21:01:05 -0400483
Liz Kammer135bf552021-08-11 10:46:06 -0400484 systemSharedLibs := baseLinkerProps.System_shared_libs
485 // systemSharedLibs distinguishes between nil/empty list behavior:
486 // nil -> use default values
487 // empty list -> no values specified
488 if len(systemSharedLibs) > 0 {
489 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
Chris Parsons51f8c392021-08-03 21:01:05 -0400490 }
Chris Parsons953b3562021-09-20 15:14:39 -0400491 systemSharedDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
Chris Parsons51f8c392021-08-03 21:01:05 -0400492
493 sharedLibs := android.FirstUniqueStrings(baseLinkerProps.Shared_libs)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400494 sharedDeps := partitionExportedAndImplementationsDepsExcludes(ctx, sharedLibs, baseLinkerProps.Exclude_shared_libs, baseLinkerProps.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes)
495 dynamicDeps.SetSelectValue(axis, config, sharedDeps.export)
496 implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400497
Liz Kammer47535c52021-06-02 16:02:22 -0400498 headerLibs := android.FirstUniqueStrings(baseLinkerProps.Header_libs)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400499 hDeps := partitionExportedAndImplementationsDeps(ctx, headerLibs, baseLinkerProps.Export_header_lib_headers, bazelLabelForHeaderDeps)
500
501 headerDeps.SetSelectValue(axis, config, hDeps.export)
502 implementationHeaderDeps.SetSelectValue(axis, config, hDeps.implementation)
Liz Kammer47535c52021-06-02 16:02:22 -0400503
Liz Kammer0eae52e2021-10-06 10:32:26 -0400504 if !BoolDefault(baseLinkerProps.Pack_relocations, packRelocationsDefault) {
505 axisFeatures = append(axisFeatures, "disable_pack_relocations")
506 }
507
508 if Bool(baseLinkerProps.Allow_undefined_symbols) {
509 axisFeatures = append(axisFeatures, "-no_undefined_symbols")
510 }
511
Liz Kammerd2871182021-10-04 13:54:37 -0400512 var linkerFlags []string
513 if len(baseLinkerProps.Ldflags) > 0 {
514 linkerFlags = append(linkerFlags, baseLinkerProps.Ldflags...)
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400515 }
Liz Kammerd2871182021-10-04 13:54:37 -0400516 if baseLinkerProps.Version_script != nil {
517 label := android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)
518 additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}})
519 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label))
520 }
521 linkopts.SetSelectValue(axis, config, linkerFlags)
Liz Kammerd366c902021-06-03 13:43:01 -0400522 useLibcrt.SetSelectValue(axis, config, baseLinkerProps.libCrt())
Jingwen Chen6ada5892021-09-17 11:38:09 +0000523
524 // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it.
525 if baseLinkerProps.crt() != nil {
526 if axis == bazel.NoConfigAxis {
527 linkCrt.SetSelectValue(axis, config, baseLinkerProps.crt())
528 } else if axis == bazel.ArchConfigurationAxis {
529 disallowedArchVariantCrt = true
530 }
531 }
Liz Kammer0eae52e2021-10-06 10:32:26 -0400532
533 if axisFeatures != nil {
534 features.SetSelectValue(axis, config, axisFeatures)
535 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400536 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400537 }
538 }
539
Jingwen Chen6ada5892021-09-17 11:38:09 +0000540 if disallowedArchVariantCrt {
541 ctx.ModuleErrorf("nocrt is not supported for arch variants")
542 }
543
Liz Kammer47535c52021-06-02 16:02:22 -0400544 type productVarDep struct {
545 // the name of the corresponding excludes field, if one exists
546 excludesField string
547 // reference to the bazel attribute that should be set for the given product variable config
548 attribute *bazel.LabelListAttribute
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400549
Chris Parsons953b3562021-09-20 15:14:39 -0400550 depResolutionFunc func(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList
Liz Kammer47535c52021-06-02 16:02:22 -0400551 }
552
553 productVarToDepFields := map[string]productVarDep{
554 // product variables do not support exclude_shared_libs
Liz Kammer7a210ac2021-09-22 15:52:58 -0400555 "Shared_libs": productVarDep{attribute: &implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes},
556 "Static_libs": productVarDep{"Exclude_static_libs", &implementationDeps, bazelLabelForStaticDepsExcludes},
Chris Parsons953b3562021-09-20 15:14:39 -0400557 "Whole_static_libs": productVarDep{"Exclude_static_libs", &wholeArchiveDeps, bazelLabelForWholeDepsExcludes},
Liz Kammer47535c52021-06-02 16:02:22 -0400558 }
559
560 productVariableProps := android.ProductVariableProperties(ctx)
561 for name, dep := range productVarToDepFields {
562 props, exists := productVariableProps[name]
563 excludeProps, excludesExists := productVariableProps[dep.excludesField]
564 // if neither an include or excludes property exists, then skip it
565 if !exists && !excludesExists {
566 continue
567 }
568 // collect all the configurations that an include or exclude property exists for.
569 // we want to iterate all configurations rather than either the include or exclude because for a
570 // particular configuration we may have only and include or only an exclude to handle
571 configs := make(map[string]bool, len(props)+len(excludeProps))
572 for config := range props {
573 configs[config] = true
574 }
575 for config := range excludeProps {
576 configs[config] = true
577 }
578
579 for config := range configs {
580 prop, includesExists := props[config]
581 excludesProp, excludesExists := excludeProps[config]
582 var includes, excludes []string
583 var ok bool
584 // if there was no includes/excludes property, casting fails and that's expected
585 if includes, ok = prop.Property.([]string); includesExists && !ok {
586 ctx.ModuleErrorf("Could not convert product variable %s property", name)
587 }
588 if excludes, ok = excludesProp.Property.([]string); excludesExists && !ok {
589 ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
590 }
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400591
592 dep.attribute.SetSelectValue(bazel.ProductVariableConfigurationAxis(config), config, dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes))
Liz Kammer47535c52021-06-02 16:02:22 -0400593 }
594 }
595
Liz Kammer7a210ac2021-09-22 15:52:58 -0400596 headerDeps.Append(deps)
597 implementationHeaderDeps.Append(implementationDeps)
598
599 headerDeps.ResolveExcludes()
600 implementationHeaderDeps.ResolveExcludes()
Liz Kammer47535c52021-06-02 16:02:22 -0400601 dynamicDeps.ResolveExcludes()
Liz Kammer7a210ac2021-09-22 15:52:58 -0400602 implementationDynamicDeps.ResolveExcludes()
Liz Kammer47535c52021-06-02 16:02:22 -0400603 wholeArchiveDeps.ResolveExcludes()
604
Jingwen Chen107c0de2021-04-09 10:43:12 +0000605 return linkerAttributes{
Liz Kammer7a210ac2021-09-22 15:52:58 -0400606 deps: headerDeps,
607 implementationDeps: implementationHeaderDeps,
608 dynamicDeps: dynamicDeps,
609 implementationDynamicDeps: implementationDynamicDeps,
610 wholeArchiveDeps: wholeArchiveDeps,
611 systemDynamicDeps: systemSharedDeps,
612
Liz Kammerd2871182021-10-04 13:54:37 -0400613 linkCrt: linkCrt,
614 linkopts: linkopts,
615 useLibcrt: useLibcrt,
616 additionalLinkerInputs: additionalLinkerInputs,
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000617
618 // Strip properties
619 stripKeepSymbols: stripKeepSymbols,
620 stripKeepSymbolsAndDebugFrame: stripKeepSymbolsAndDebugFrame,
621 stripKeepSymbolsList: stripKeepSymbolsList,
622 stripAll: stripAll,
623 stripNone: stripNone,
Liz Kammer0eae52e2021-10-06 10:32:26 -0400624
625 features: features,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000626 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400627}
628
Jingwen Chened9c17d2021-04-13 07:14:55 +0000629// Relativize a list of root-relative paths with respect to the module's
630// directory.
631//
632// include_dirs Soong prop are root-relative (b/183742505), but
633// local_include_dirs, export_include_dirs and export_system_include_dirs are
634// module dir relative. This function makes a list of paths entirely module dir
635// relative.
636//
637// For the `include` attribute, Bazel wants the paths to be relative to the
638// module.
639func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000640 var relativePaths []string
641 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000642 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
643 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
644 if err != nil {
645 panic(err)
646 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000647 relativePaths = append(relativePaths, relativePath)
648 }
649 return relativePaths
650}
651
Liz Kammer5fad5012021-09-09 14:08:21 -0400652// BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel
653// attributes.
654type BazelIncludes struct {
655 Includes bazel.StringListAttribute
656 SystemIncludes bazel.StringListAttribute
657}
658
659func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) BazelIncludes {
Jingwen Chen91220d72021-03-24 02:18:33 -0400660 libraryDecorator := module.linker.(*libraryDecorator)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400661 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
662}
Jingwen Chen91220d72021-03-24 02:18:33 -0400663
Liz Kammer5fad5012021-09-09 14:08:21 -0400664// Bp2buildParseExportedIncludesForPrebuiltLibrary returns a BazelIncludes with Bazel-ified values
665// to export includes from the underlying module's properties.
666func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.TopDownMutatorContext, module *Module) BazelIncludes {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400667 prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker)
668 libraryDecorator := prebuiltLibraryLinker.libraryDecorator
669 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
670}
671
672// bp2BuildParseExportedIncludes creates a string list attribute contains the
673// exported included directories of a module.
Liz Kammer5fad5012021-09-09 14:08:21 -0400674func bp2BuildParseExportedIncludesHelper(ctx android.TopDownMutatorContext, module *Module, libraryDecorator *libraryDecorator) BazelIncludes {
675 exported := BazelIncludes{}
Liz Kammer9abd62d2021-05-21 08:37:59 -0400676 for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) {
677 for config, props := range configToProps {
678 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
Liz Kammer5fad5012021-09-09 14:08:21 -0400679 if len(flagExporterProperties.Export_include_dirs) > 0 {
680 exported.Includes.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs)
681 }
682 if len(flagExporterProperties.Export_system_include_dirs) > 0 {
683 exported.SystemIncludes.SetSelectValue(axis, config, flagExporterProperties.Export_system_include_dirs)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400684 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400685 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400686 }
687 }
Liz Kammer5fad5012021-09-09 14:08:21 -0400688 exported.Includes.DeduplicateAxesFromBase()
689 exported.SystemIncludes.DeduplicateAxesFromBase()
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400690
Liz Kammer5fad5012021-09-09 14:08:21 -0400691 return exported
Jingwen Chen91220d72021-03-24 02:18:33 -0400692}
Chris Parsons953b3562021-09-20 15:14:39 -0400693
694func bazelLabelForStaticModule(ctx android.TopDownMutatorContext, m blueprint.Module) string {
695 label := android.BazelModuleLabel(ctx, m)
696 if aModule, ok := m.(android.Module); ok {
697 if ctx.OtherModuleType(aModule) == "cc_library" && !android.GenerateCcLibraryStaticOnly(m.Name()) {
698 label += "_bp2build_cc_library_static"
699 }
700 }
701 return label
702}
703
704func bazelLabelForSharedModule(ctx android.TopDownMutatorContext, m blueprint.Module) string {
705 // cc_library, at it's root name, propagates the shared library, which depends on the static
706 // library.
707 return android.BazelModuleLabel(ctx, m)
708}
709
710func bazelLabelForStaticWholeModuleDeps(ctx android.TopDownMutatorContext, m blueprint.Module) string {
711 label := bazelLabelForStaticModule(ctx, m)
712 if aModule, ok := m.(android.Module); ok {
713 if android.IsModulePrebuilt(aModule) {
714 label += "_alwayslink"
715 }
716 }
717 return label
718}
719
720func bazelLabelForWholeDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList {
721 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps)
722}
723
724func bazelLabelForWholeDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList {
725 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps)
726}
727
728func bazelLabelForStaticDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList {
729 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule)
730}
731
732func bazelLabelForStaticDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList {
733 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule)
734}
735
736func bazelLabelForSharedDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList {
737 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule)
738}
739
740func bazelLabelForHeaderDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList {
741 // This is not elegant, but bp2build's shared library targets only propagate
742 // their header information as part of the normal C++ provider.
743 return bazelLabelForSharedDeps(ctx, modules)
744}
745
746func bazelLabelForSharedDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList {
747 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
748}