blob: 888c3bacb96c0450e3dca02941dfdd4723e90398 [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
Liz Kammere6583482021-10-19 13:56:10 -040041 Hdrs bazel.LabelListAttribute
Jingwen Chenc4dc9b42021-06-11 12:51:48 +000042 Copts bazel.StringListAttribute
Jingwen Chen14a8bda2021-06-02 11:10:02 +000043
Liz Kammer7a210ac2021-09-22 15:52:58 -040044 Deps bazel.LabelListAttribute
45 Implementation_deps bazel.LabelListAttribute
46 Dynamic_deps bazel.LabelListAttribute
47 Implementation_dynamic_deps bazel.LabelListAttribute
48 Whole_archive_deps bazel.LabelListAttribute
Chris Parsons51f8c392021-08-03 21:01:05 -040049
50 System_dynamic_deps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +000051}
52
Jingwen Chen55bc8202021-11-02 06:40:51 +000053func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute {
Jingwen Chen14a8bda2021-06-02 11:10:02 +000054 // Check that a module is a filegroup type named <label>.
55 isFilegroupNamed := func(m android.Module, fullLabel string) bool {
56 if ctx.OtherModuleType(m) != "filegroup" {
57 return false
58 }
59 labelParts := strings.Split(fullLabel, ":")
60 if len(labelParts) > 2 {
61 // There should not be more than one colon in a label.
Liz Kammer57e2e7a2021-09-20 12:55:02 -040062 ctx.ModuleErrorf("%s is not a valid Bazel label for a filegroup", fullLabel)
Jingwen Chen14a8bda2021-06-02 11:10:02 +000063 }
Liz Kammer57e2e7a2021-09-20 12:55:02 -040064 return m.Name() == labelParts[len(labelParts)-1]
Jingwen Chen14a8bda2021-06-02 11:10:02 +000065 }
66
Liz Kammer57e2e7a2021-09-20 12:55:02 -040067 // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl
68 // macro.
69 addSuffixForFilegroup := func(suffix string) bazel.LabelMapper {
70 return func(ctx bazel.OtherModuleContext, label string) (string, bool) {
71 m, exists := ctx.ModuleFromName(label)
72 if !exists {
73 return label, false
Jingwen Chen14a8bda2021-06-02 11:10:02 +000074 }
Chris Parsons5a34ffb2021-07-21 14:34:58 -040075 aModule, _ := m.(android.Module)
Liz Kammer57e2e7a2021-09-20 12:55:02 -040076 if !isFilegroupNamed(aModule, label) {
77 return label, false
Jingwen Chen14a8bda2021-06-02 11:10:02 +000078 }
Liz Kammer57e2e7a2021-09-20 12:55:02 -040079 return label + suffix, true
Chris Parsons5a34ffb2021-07-21 14:34:58 -040080 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +000081 }
82
Liz Kammer57e2e7a2021-09-20 12:55:02 -040083 // TODO(b/190006308): Handle language detection of sources in a Bazel rule.
84 partitioned := bazel.PartitionLabelListAttribute(ctx, &srcs, bazel.LabelPartitions{
Liz Kammerae3994e2021-10-19 09:45:48 -040085 cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")},
86 asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")},
Liz Kammer57e2e7a2021-09-20 12:55:02 -040087 // C++ is the "catch-all" group, and comprises generated sources because we don't
88 // know the language of these sources until the genrule is executed.
Liz Kammerae3994e2021-10-19 09:45:48 -040089 cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true},
Liz Kammer57e2e7a2021-09-20 12:55:02 -040090 })
Jingwen Chen14a8bda2021-06-02 11:10:02 +000091
Liz Kammerae3994e2021-10-19 09:45:48 -040092 return partitioned
Jingwen Chen14a8bda2021-06-02 11:10:02 +000093}
94
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000095// bp2BuildParseLibProps returns the attributes for a variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +000096func bp2BuildParseLibProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +000097 lib, ok := module.compiler.(*libraryDecorator)
98 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -040099 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000100 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000101 return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic)
102}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000103
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000104// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000105func bp2BuildParseSharedProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000106 return bp2BuildParseLibProps(ctx, module, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000107}
108
109// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000110func bp2BuildParseStaticProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000111 return bp2BuildParseLibProps(ctx, module, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400112}
113
Liz Kammer7a210ac2021-09-22 15:52:58 -0400114type depsPartition struct {
115 export bazel.LabelList
116 implementation bazel.LabelList
117}
118
Jingwen Chen55bc8202021-11-02 06:40:51 +0000119type bazelLabelForDepsFn func(android.BazelConversionPathContext, []string) bazel.LabelList
Liz Kammer7a210ac2021-09-22 15:52:58 -0400120
Jingwen Chen55bc8202021-11-02 06:40:51 +0000121func maybePartitionExportedAndImplementationsDeps(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, exportedDeps []string, fn bazelLabelForDepsFn) depsPartition {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400122 if !exportsDeps {
123 return depsPartition{
124 implementation: fn(ctx, allDeps),
125 }
126 }
127
Liz Kammer7a210ac2021-09-22 15:52:58 -0400128 implementation, export := android.FilterList(allDeps, exportedDeps)
129
130 return depsPartition{
131 export: fn(ctx, export),
132 implementation: fn(ctx, implementation),
133 }
134}
135
Jingwen Chen55bc8202021-11-02 06:40:51 +0000136type bazelLabelForDepsExcludesFn func(android.BazelConversionPathContext, []string, []string) bazel.LabelList
Liz Kammer7a210ac2021-09-22 15:52:58 -0400137
Jingwen Chen55bc8202021-11-02 06:40:51 +0000138func maybePartitionExportedAndImplementationsDepsExcludes(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, excludes, exportedDeps []string, fn bazelLabelForDepsExcludesFn) depsPartition {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400139 if !exportsDeps {
140 return depsPartition{
141 implementation: fn(ctx, allDeps, excludes),
142 }
143 }
Liz Kammer7a210ac2021-09-22 15:52:58 -0400144 implementation, export := android.FilterList(allDeps, exportedDeps)
145
146 return depsPartition{
147 export: fn(ctx, export, excludes),
148 implementation: fn(ctx, implementation, excludes),
149 }
150}
151
Jingwen Chen55bc8202021-11-02 06:40:51 +0000152func bp2buildParseStaticOrSharedProps(ctx android.BazelConversionPathContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes {
Liz Kammer135bf552021-08-11 10:46:06 -0400153 attrs := staticOrSharedAttributes{}
Jingwen Chenbcf53042021-05-26 04:42:42 +0000154
Liz Kammer9abd62d2021-05-21 08:37:59 -0400155 setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000156 attrs.Copts.SetSelectValue(axis, config, props.Cflags)
157 attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
Chris Parsons953b3562021-09-20 15:14:39 -0400158 attrs.System_dynamic_deps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, props.System_shared_libs))
Liz Kammer7a210ac2021-09-22 15:52:58 -0400159
Liz Kammer2b8004b2021-10-04 13:55:44 -0400160 staticDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Static_libs, props.Export_static_lib_headers, bazelLabelForStaticDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400161 attrs.Deps.SetSelectValue(axis, config, staticDeps.export)
162 attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation)
163
Liz Kammer2b8004b2021-10-04 13:55:44 -0400164 sharedDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400165 attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export)
166 attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation)
167
168 attrs.Whole_archive_deps.SetSelectValue(axis, config, bazelLabelForWholeDeps(ctx, props.Whole_static_libs))
Jingwen Chenbcf53042021-05-26 04:42:42 +0000169 }
Liz Kammer135bf552021-08-11 10:46:06 -0400170 // system_dynamic_deps distinguishes between nil/empty list behavior:
171 // nil -> use default values
172 // empty list -> no values specified
173 attrs.System_dynamic_deps.ForceSpecifyEmptyList = true
Jingwen Chenbcf53042021-05-26 04:42:42 +0000174
175 if isStatic {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400176 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) {
177 for config, props := range configToProps {
178 if staticOrSharedProps, ok := props.(*StaticProperties); ok {
179 setAttrs(axis, config, staticOrSharedProps.Static)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000180 }
181 }
182 }
183 } else {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400184 for axis, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) {
185 for config, props := range configToProps {
186 if staticOrSharedProps, ok := props.(*SharedProperties); ok {
187 setAttrs(axis, config, staticOrSharedProps.Shared)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000188 }
189 }
190 }
191 }
192
Liz Kammerae3994e2021-10-19 09:45:48 -0400193 partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs)
194 attrs.Srcs = partitionedSrcs[cppSrcPartition]
195 attrs.Srcs_c = partitionedSrcs[cSrcPartition]
196 attrs.Srcs_as = partitionedSrcs[asSrcPartition]
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000197
Jingwen Chenbcf53042021-05-26 04:42:42 +0000198 return attrs
Jingwen Chen53681ef2021-04-29 08:15:13 +0000199}
200
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400201// Convenience struct to hold all attributes parsed from prebuilt properties.
202type prebuiltAttributes struct {
203 Src bazel.LabelAttribute
204}
205
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000206// NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package
Jingwen Chen55bc8202021-11-02 06:40:51 +0000207func Bp2BuildParsePrebuiltLibraryProps(ctx android.BazelConversionPathContext, module *Module) prebuiltAttributes {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400208 var srcLabelAttribute bazel.LabelAttribute
209
Liz Kammer9abd62d2021-05-21 08:37:59 -0400210 for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltLinkerProperties{}) {
211 for config, props := range configToProps {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400212 if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok {
213 if len(prebuiltLinkerProperties.Srcs) > 1 {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400214 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for %s %s\n", axis, config)
215 continue
216 } else if len(prebuiltLinkerProperties.Srcs) == 0 {
217 continue
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400218 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400219 src := android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0])
220 srcLabelAttribute.SetSelectValue(axis, config, src)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400221 }
222 }
223 }
224
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400225 return prebuiltAttributes{
226 Src: srcLabelAttribute,
227 }
228}
229
Liz Kammere6583482021-10-19 13:56:10 -0400230type baseAttributes struct {
231 compilerAttributes
232 linkerAttributes
233}
234
Jingwen Chen107c0de2021-04-09 10:43:12 +0000235// Convenience struct to hold all attributes parsed from compiler properties.
236type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400237 // Options for all languages
238 copts bazel.StringListAttribute
239 // Assembly options and sources
240 asFlags bazel.StringListAttribute
241 asSrcs bazel.LabelListAttribute
242 // C options and sources
243 conlyFlags bazel.StringListAttribute
244 cSrcs bazel.LabelListAttribute
245 // C++ options and sources
246 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000247 srcs bazel.LabelListAttribute
Chris Parsons2c788392021-08-10 11:58:07 -0400248
Liz Kammere6583482021-10-19 13:56:10 -0400249 hdrs bazel.LabelListAttribute
250
Chris Parsons2c788392021-08-10 11:58:07 -0400251 rtti bazel.BoolAttribute
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000252
253 // Not affected by arch variants
254 stl *string
Chris Parsons79bd2b72021-11-29 17:52:41 -0500255 cStd *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000256 cppStd *string
Liz Kammer35687bc2021-09-10 10:07:07 -0400257
258 localIncludes bazel.StringListAttribute
259 absoluteIncludes bazel.StringListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000260}
261
Liz Kammere6583482021-10-19 13:56:10 -0400262func parseCommandLineFlags(soongFlags []string) []string {
263 var result []string
264 for _, flag := range soongFlags {
265 // Soong's cflags can contain spaces, like `-include header.h`. For
266 // Bazel's copts, split them up to be compatible with the
267 // no_copts_tokenization feature.
268 result = append(result, strings.Split(flag, " ")...)
269 }
270 return result
271}
Jingwen Chened9c17d2021-04-13 07:14:55 +0000272
Jingwen Chen55bc8202021-11-02 06:40:51 +0000273func (ca *compilerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, props *BaseCompilerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400274 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
275 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
276 if srcsList, ok := parseSrcs(ctx, props); ok {
277 ca.srcs.SetSelectValue(axis, config, srcsList)
Chris Parsons990c4f42021-05-25 12:10:58 -0400278 }
279
Liz Kammere6583482021-10-19 13:56:10 -0400280 localIncludeDirs := props.Local_include_dirs
281 if axis == bazel.NoConfigAxis {
Chris Parsons79bd2b72021-11-29 17:52:41 -0500282 ca.cStd, ca.cppStd = bp2buildResolveCppStdValue(props.C_std, props.Cpp_std, props.Gnu_extensions)
Liz Kammere6583482021-10-19 13:56:10 -0400283 if includeBuildDirectory(props.Include_build_directory) {
284 localIncludeDirs = append(localIncludeDirs, ".")
Liz Kammer222bdcf2021-10-11 14:15:51 -0400285 }
Jingwen Chene32e9e02021-04-23 09:17:24 +0000286 }
287
Liz Kammere6583482021-10-19 13:56:10 -0400288 ca.absoluteIncludes.SetSelectValue(axis, config, props.Include_dirs)
289 ca.localIncludes.SetSelectValue(axis, config, localIncludeDirs)
290
291 ca.copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags))
292 ca.asFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Asflags))
293 ca.conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Conlyflags))
294 ca.cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Cppflags))
295 ca.rtti.SetSelectValue(axis, config, props.Rtti)
296}
297
Jingwen Chen55bc8202021-11-02 06:40:51 +0000298func (ca *compilerAttributes) convertStlProps(ctx android.ArchVariantContext, module *Module) {
Liz Kammere6583482021-10-19 13:56:10 -0400299 stlPropsByArch := module.GetArchVariantProperties(ctx, &StlProperties{})
300 for _, configToProps := range stlPropsByArch {
301 for _, props := range configToProps {
302 if stlProps, ok := props.(*StlProperties); ok {
303 if stlProps.Stl == nil {
304 continue
Liz Kammer9abd62d2021-05-21 08:37:59 -0400305 }
Liz Kammere6583482021-10-19 13:56:10 -0400306 if ca.stl == nil {
307 ca.stl = stlProps.Stl
308 } else if ca.stl != stlProps.Stl {
309 ctx.ModuleErrorf("Unsupported conversion: module with different stl for different variants: %s and %s", *ca.stl, stlProps.Stl)
Liz Kammerae3994e2021-10-19 09:45:48 -0400310 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400311 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000312 }
313 }
Liz Kammere6583482021-10-19 13:56:10 -0400314}
Jingwen Chenc1c26502021-04-05 10:35:13 +0000315
Jingwen Chen55bc8202021-11-02 06:40:51 +0000316func (ca *compilerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Liz Kammerba7a9c52021-05-26 08:45:30 -0400317 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
Liz Kammere6583482021-10-19 13:56:10 -0400318 "Cflags": &ca.copts,
319 "Asflags": &ca.asFlags,
320 "CppFlags": &ca.cppFlags,
Liz Kammerba7a9c52021-05-26 08:45:30 -0400321 }
Liz Kammerba7a9c52021-05-26 08:45:30 -0400322 for propName, attr := range productVarPropNameToAttribute {
Jingwen Chen25825ca2021-11-15 12:28:43 +0000323 if productConfigProps, exists := productVariableProps[propName]; exists {
324 for productConfigProp, prop := range productConfigProps {
325 flags, ok := prop.([]string)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400326 if !ok {
327 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
328 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000329 newFlags, _ := bazel.TryVariableSubstitutions(flags, productConfigProp.Name)
330 attr.SetSelectValue(productConfigProp.ConfigurationAxis(), productConfigProp.SelectKey(), newFlags)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400331 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400332 }
333 }
Liz Kammere6583482021-10-19 13:56:10 -0400334}
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400335
Jingwen Chen55bc8202021-11-02 06:40:51 +0000336func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, implementationHdrs bazel.LabelListAttribute) {
Liz Kammere6583482021-10-19 13:56:10 -0400337 ca.srcs.ResolveExcludes()
338 partitionedSrcs := groupSrcsByExtension(ctx, ca.srcs)
339
340 for p, lla := range partitionedSrcs {
341 // if there are no sources, there is no need for headers
342 if lla.IsEmpty() {
343 continue
344 }
345 lla.Append(implementationHdrs)
346 partitionedSrcs[p] = lla
347 }
348
349 ca.srcs = partitionedSrcs[cppSrcPartition]
350 ca.cSrcs = partitionedSrcs[cSrcPartition]
351 ca.asSrcs = partitionedSrcs[asSrcPartition]
352
353 ca.absoluteIncludes.DeduplicateAxesFromBase()
354 ca.localIncludes.DeduplicateAxesFromBase()
355}
356
357// Parse srcs from an arch or OS's props value.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000358func parseSrcs(ctx android.BazelConversionPathContext, props *BaseCompilerProperties) (bazel.LabelList, bool) {
Liz Kammere6583482021-10-19 13:56:10 -0400359 anySrcs := false
360 // Add srcs-like dependencies such as generated files.
361 // First create a LabelList containing these dependencies, then merge the values with srcs.
362 generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources)
363 if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 {
364 anySrcs = true
365 }
366
367 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs)
368 if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 {
369 anySrcs = true
370 }
371 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs
372}
373
Chris Parsons79bd2b72021-11-29 17:52:41 -0500374func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) {
375 var cStdVal, cppStdVal string
376 // If c{,pp}std properties are not specified, don't generate them in the BUILD file.
377 // Defaults are handled by the toolchain definition.
378 // However, if gnu_extensions is false, then the default gnu-to-c version must be specified.
Liz Kammere6583482021-10-19 13:56:10 -0400379 if cpp_std != nil {
Chris Parsons79bd2b72021-11-29 17:52:41 -0500380 cppStdVal = parseCppStd(cpp_std)
Liz Kammere6583482021-10-19 13:56:10 -0400381 } else if gnu_extensions != nil && !*gnu_extensions {
Chris Parsons79bd2b72021-11-29 17:52:41 -0500382 cppStdVal = "c++17"
Liz Kammere6583482021-10-19 13:56:10 -0400383 }
Chris Parsons79bd2b72021-11-29 17:52:41 -0500384 if c_std != nil {
385 cStdVal = parseCStd(c_std)
386 } else if gnu_extensions != nil && !*gnu_extensions {
387 cStdVal = "c99"
388 }
389
390 cStdVal, cppStdVal = maybeReplaceGnuToC(gnu_extensions, cStdVal, cppStdVal)
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500391 var c_std_prop, cpp_std_prop *string
392 if cStdVal != "" {
393 c_std_prop = &cStdVal
394 }
395 if cppStdVal != "" {
396 cpp_std_prop = &cppStdVal
397 }
398
399 return c_std_prop, cpp_std_prop
Liz Kammere6583482021-10-19 13:56:10 -0400400}
401
402// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000403func bp2BuildParseBaseProps(ctx android.BazelConversionPathContext, module *Module) baseAttributes {
Liz Kammere6583482021-10-19 13:56:10 -0400404 archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
405 archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{})
406
407 var implementationHdrs bazel.LabelListAttribute
408
409 axisToConfigs := map[bazel.ConfigurationAxis]map[string]bool{}
410 allAxesAndConfigs := func(cp android.ConfigurationAxisToArchVariantProperties) {
411 for axis, configMap := range cp {
412 if _, ok := axisToConfigs[axis]; !ok {
413 axisToConfigs[axis] = map[string]bool{}
414 }
415 for config, _ := range configMap {
416 axisToConfigs[axis][config] = true
Chris Parsonsa967f252021-09-23 16:34:35 -0400417 }
418 }
419 }
Liz Kammere6583482021-10-19 13:56:10 -0400420 allAxesAndConfigs(archVariantCompilerProps)
421 allAxesAndConfigs(archVariantLinkerProps)
Chris Parsonsa967f252021-09-23 16:34:35 -0400422
Liz Kammere6583482021-10-19 13:56:10 -0400423 compilerAttrs := compilerAttributes{}
424 linkerAttrs := linkerAttributes{}
425
426 for axis, configs := range axisToConfigs {
427 for config, _ := range configs {
428 var allHdrs []string
429 if baseCompilerProps, ok := archVariantCompilerProps[axis][config].(*BaseCompilerProperties); ok {
430 allHdrs = baseCompilerProps.Generated_headers
431
432 (&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, config, baseCompilerProps)
433 }
434
435 var exportHdrs []string
436
437 if baseLinkerProps, ok := archVariantLinkerProps[axis][config].(*BaseLinkerProperties); ok {
438 exportHdrs = baseLinkerProps.Export_generated_headers
439
440 (&linkerAttrs).bp2buildForAxisAndConfig(ctx, module.Binary(), axis, config, baseLinkerProps)
441 }
442 headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportHdrs, android.BazelLabelForModuleDeps)
443 implementationHdrs.SetSelectValue(axis, config, headers.implementation)
444 compilerAttrs.hdrs.SetSelectValue(axis, config, headers.export)
445 }
446 }
447
448 compilerAttrs.convertStlProps(ctx, module)
449 (&linkerAttrs).convertStripProps(ctx, module)
450
451 productVariableProps := android.ProductVariableProperties(ctx)
452
453 (&compilerAttrs).convertProductVariables(ctx, productVariableProps)
454 (&linkerAttrs).convertProductVariables(ctx, productVariableProps)
455
456 (&compilerAttrs).finalize(ctx, implementationHdrs)
457 (&linkerAttrs).finalize()
458
459 return baseAttributes{
460 compilerAttrs,
461 linkerAttrs,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000462 }
463}
464
465// Convenience struct to hold all attributes parsed from linker properties.
466type linkerAttributes struct {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400467 deps bazel.LabelListAttribute
468 implementationDeps bazel.LabelListAttribute
469 dynamicDeps bazel.LabelListAttribute
470 implementationDynamicDeps bazel.LabelListAttribute
471 wholeArchiveDeps bazel.LabelListAttribute
472 systemDynamicDeps bazel.LabelListAttribute
473
Jingwen Chen6ada5892021-09-17 11:38:09 +0000474 linkCrt bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000475 useLibcrt bazel.BoolAttribute
476 linkopts bazel.StringListAttribute
Liz Kammerd2871182021-10-04 13:54:37 -0400477 additionalLinkerInputs bazel.LabelListAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000478 stripKeepSymbols bazel.BoolAttribute
479 stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
480 stripKeepSymbolsList bazel.StringListAttribute
481 stripAll bazel.BoolAttribute
482 stripNone bazel.BoolAttribute
Liz Kammer0eae52e2021-10-06 10:32:26 -0400483 features bazel.StringListAttribute
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400484}
485
Jingwen Chen55bc8202021-11-02 06:40:51 +0000486func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, isBinary bool, axis bazel.ConfigurationAxis, config string, props *BaseLinkerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400487 // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module
488 var axisFeatures []string
Liz Kammer7a210ac2021-09-22 15:52:58 -0400489
Liz Kammere6583482021-10-19 13:56:10 -0400490 // Excludes to parallel Soong:
491 // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0
492 staticLibs := android.FirstUniqueStrings(props.Static_libs)
493 staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, staticLibs, props.Exclude_static_libs, props.Export_static_lib_headers, bazelLabelForStaticDepsExcludes)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400494
Liz Kammere6583482021-10-19 13:56:10 -0400495 headerLibs := android.FirstUniqueStrings(props.Header_libs)
496 hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps)
Jingwen Chen63930982021-03-24 10:04:33 -0400497
Liz Kammere6583482021-10-19 13:56:10 -0400498 (&hDeps.export).Append(staticDeps.export)
499 la.deps.SetSelectValue(axis, config, hDeps.export)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000500
Liz Kammere6583482021-10-19 13:56:10 -0400501 (&hDeps.implementation).Append(staticDeps.implementation)
502 la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation)
Liz Kammer0eae52e2021-10-06 10:32:26 -0400503
Liz Kammere6583482021-10-19 13:56:10 -0400504 wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs)
505 la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs))
506
507 systemSharedLibs := props.System_shared_libs
508 // systemSharedLibs distinguishes between nil/empty list behavior:
509 // nil -> use default values
510 // empty list -> no values specified
511 if len(systemSharedLibs) > 0 {
512 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
513 }
514 la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
515
516 sharedLibs := android.FirstUniqueStrings(props.Shared_libs)
517 sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, sharedLibs, props.Exclude_shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes)
518 la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export)
519 la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation)
520
521 if !BoolDefault(props.Pack_relocations, packRelocationsDefault) {
522 axisFeatures = append(axisFeatures, "disable_pack_relocations")
523 }
524
525 if Bool(props.Allow_undefined_symbols) {
526 axisFeatures = append(axisFeatures, "-no_undefined_symbols")
527 }
528
529 var linkerFlags []string
530 if len(props.Ldflags) > 0 {
531 linkerFlags = append(linkerFlags, props.Ldflags...)
532 // binaries remove static flag if -shared is in the linker flags
533 if isBinary && android.InList("-shared", linkerFlags) {
534 axisFeatures = append(axisFeatures, "-static_flag")
535 }
536 }
537 if props.Version_script != nil {
538 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script)
539 la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}})
540 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label))
541 }
542 la.linkopts.SetSelectValue(axis, config, linkerFlags)
543 la.useLibcrt.SetSelectValue(axis, config, props.libCrt())
544
545 // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it.
546 if props.crt() != nil {
547 if axis == bazel.NoConfigAxis {
548 la.linkCrt.SetSelectValue(axis, config, props.crt())
549 } else if axis == bazel.ArchConfigurationAxis {
550 ctx.ModuleErrorf("nocrt is not supported for arch variants")
551 }
552 }
553
554 if axisFeatures != nil {
555 la.features.SetSelectValue(axis, config, axisFeatures)
556 }
557}
558
Jingwen Chen55bc8202021-11-02 06:40:51 +0000559func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) {
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000560 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StripProperties{}) {
561 for config, props := range configToProps {
562 if stripProperties, ok := props.(*StripProperties); ok {
Liz Kammere6583482021-10-19 13:56:10 -0400563 la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols)
564 la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list)
565 la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame)
566 la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All)
567 la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000568 }
569 }
570 }
Liz Kammere6583482021-10-19 13:56:10 -0400571}
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000572
Jingwen Chen55bc8202021-11-02 06:40:51 +0000573func (la *linkerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Jingwen Chen6ada5892021-09-17 11:38:09 +0000574
Liz Kammer47535c52021-06-02 16:02:22 -0400575 type productVarDep struct {
576 // the name of the corresponding excludes field, if one exists
577 excludesField string
578 // reference to the bazel attribute that should be set for the given product variable config
579 attribute *bazel.LabelListAttribute
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400580
Jingwen Chen55bc8202021-11-02 06:40:51 +0000581 depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList
Liz Kammer47535c52021-06-02 16:02:22 -0400582 }
583
584 productVarToDepFields := map[string]productVarDep{
585 // product variables do not support exclude_shared_libs
Jingwen Chen55bc8202021-11-02 06:40:51 +0000586 "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes},
587 "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes},
588 "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes},
Liz Kammer47535c52021-06-02 16:02:22 -0400589 }
590
Liz Kammer47535c52021-06-02 16:02:22 -0400591 for name, dep := range productVarToDepFields {
592 props, exists := productVariableProps[name]
593 excludeProps, excludesExists := productVariableProps[dep.excludesField]
594 // if neither an include or excludes property exists, then skip it
595 if !exists && !excludesExists {
596 continue
597 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000598 // Collect all the configurations that an include or exclude property exists for.
599 // We want to iterate all configurations rather than either the include or exclude because, for a
600 // particular configuration, we may have either only an include or an exclude to handle.
601 productConfigProps := make(map[android.ProductConfigProperty]bool, len(props)+len(excludeProps))
602 for p := range props {
603 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -0400604 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000605 for p := range excludeProps {
606 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -0400607 }
608
Jingwen Chen25825ca2021-11-15 12:28:43 +0000609 for productConfigProp := range productConfigProps {
610 prop, includesExists := props[productConfigProp]
611 excludesProp, excludesExists := excludeProps[productConfigProp]
Liz Kammer47535c52021-06-02 16:02:22 -0400612 var includes, excludes []string
613 var ok bool
614 // if there was no includes/excludes property, casting fails and that's expected
Jingwen Chen25825ca2021-11-15 12:28:43 +0000615 if includes, ok = prop.([]string); includesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -0400616 ctx.ModuleErrorf("Could not convert product variable %s property", name)
617 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000618 if excludes, ok = excludesProp.([]string); excludesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -0400619 ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
620 }
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400621
Jingwen Chen58ff6802021-11-17 12:14:41 +0000622 dep.attribute.EmitEmptyList = productConfigProp.AlwaysEmit()
Jingwen Chen25825ca2021-11-15 12:28:43 +0000623 dep.attribute.SetSelectValue(
624 productConfigProp.ConfigurationAxis(),
625 productConfigProp.SelectKey(),
626 dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes),
627 )
Liz Kammer47535c52021-06-02 16:02:22 -0400628 }
629 }
Liz Kammere6583482021-10-19 13:56:10 -0400630}
Liz Kammer47535c52021-06-02 16:02:22 -0400631
Liz Kammere6583482021-10-19 13:56:10 -0400632func (la *linkerAttributes) finalize() {
633 la.deps.ResolveExcludes()
634 la.implementationDeps.ResolveExcludes()
635 la.dynamicDeps.ResolveExcludes()
636 la.implementationDynamicDeps.ResolveExcludes()
637 la.wholeArchiveDeps.ResolveExcludes()
638 la.systemDynamicDeps.ForceSpecifyEmptyList = true
Jingwen Chen91220d72021-03-24 02:18:33 -0400639}
640
Jingwen Chened9c17d2021-04-13 07:14:55 +0000641// Relativize a list of root-relative paths with respect to the module's
642// directory.
643//
644// include_dirs Soong prop are root-relative (b/183742505), but
645// local_include_dirs, export_include_dirs and export_system_include_dirs are
646// module dir relative. This function makes a list of paths entirely module dir
647// relative.
648//
649// For the `include` attribute, Bazel wants the paths to be relative to the
650// module.
651func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000652 var relativePaths []string
653 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000654 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
655 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
656 if err != nil {
657 panic(err)
658 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000659 relativePaths = append(relativePaths, relativePath)
660 }
661 return relativePaths
662}
663
Liz Kammer5fad5012021-09-09 14:08:21 -0400664// BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel
665// attributes.
666type BazelIncludes struct {
667 Includes bazel.StringListAttribute
668 SystemIncludes bazel.StringListAttribute
669}
670
Jingwen Chen55bc8202021-11-02 06:40:51 +0000671func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module) BazelIncludes {
Jingwen Chen91220d72021-03-24 02:18:33 -0400672 libraryDecorator := module.linker.(*libraryDecorator)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400673 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
674}
Jingwen Chen91220d72021-03-24 02:18:33 -0400675
Liz Kammer5fad5012021-09-09 14:08:21 -0400676// Bp2buildParseExportedIncludesForPrebuiltLibrary returns a BazelIncludes with Bazel-ified values
677// to export includes from the underlying module's properties.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000678func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.BazelConversionPathContext, module *Module) BazelIncludes {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400679 prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker)
680 libraryDecorator := prebuiltLibraryLinker.libraryDecorator
681 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
682}
683
684// bp2BuildParseExportedIncludes creates a string list attribute contains the
685// exported included directories of a module.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000686func bp2BuildParseExportedIncludesHelper(ctx android.BazelConversionPathContext, module *Module, libraryDecorator *libraryDecorator) BazelIncludes {
Liz Kammer5fad5012021-09-09 14:08:21 -0400687 exported := BazelIncludes{}
Liz Kammer9abd62d2021-05-21 08:37:59 -0400688 for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) {
689 for config, props := range configToProps {
690 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
Liz Kammer5fad5012021-09-09 14:08:21 -0400691 if len(flagExporterProperties.Export_include_dirs) > 0 {
692 exported.Includes.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs)
693 }
694 if len(flagExporterProperties.Export_system_include_dirs) > 0 {
695 exported.SystemIncludes.SetSelectValue(axis, config, flagExporterProperties.Export_system_include_dirs)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400696 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400697 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400698 }
699 }
Liz Kammer5fad5012021-09-09 14:08:21 -0400700 exported.Includes.DeduplicateAxesFromBase()
701 exported.SystemIncludes.DeduplicateAxesFromBase()
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400702
Liz Kammer5fad5012021-09-09 14:08:21 -0400703 return exported
Jingwen Chen91220d72021-03-24 02:18:33 -0400704}
Chris Parsons953b3562021-09-20 15:14:39 -0400705
Jingwen Chen55bc8202021-11-02 06:40:51 +0000706func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400707 label := android.BazelModuleLabel(ctx, m)
708 if aModule, ok := m.(android.Module); ok {
709 if ctx.OtherModuleType(aModule) == "cc_library" && !android.GenerateCcLibraryStaticOnly(m.Name()) {
710 label += "_bp2build_cc_library_static"
711 }
712 }
713 return label
714}
715
Jingwen Chen55bc8202021-11-02 06:40:51 +0000716func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400717 // cc_library, at it's root name, propagates the shared library, which depends on the static
718 // library.
719 return android.BazelModuleLabel(ctx, m)
720}
721
Jingwen Chen55bc8202021-11-02 06:40:51 +0000722func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400723 label := bazelLabelForStaticModule(ctx, m)
724 if aModule, ok := m.(android.Module); ok {
725 if android.IsModulePrebuilt(aModule) {
726 label += "_alwayslink"
727 }
728 }
729 return label
730}
731
Jingwen Chen55bc8202021-11-02 06:40:51 +0000732func bazelLabelForWholeDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400733 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps)
734}
735
Jingwen Chen55bc8202021-11-02 06:40:51 +0000736func bazelLabelForWholeDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400737 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps)
738}
739
Jingwen Chen55bc8202021-11-02 06:40:51 +0000740func bazelLabelForStaticDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400741 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule)
742}
743
Jingwen Chen55bc8202021-11-02 06:40:51 +0000744func bazelLabelForStaticDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400745 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule)
746}
747
Jingwen Chen55bc8202021-11-02 06:40:51 +0000748func bazelLabelForSharedDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400749 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule)
750}
751
Jingwen Chen55bc8202021-11-02 06:40:51 +0000752func bazelLabelForHeaderDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400753 // This is not elegant, but bp2build's shared library targets only propagate
754 // their header information as part of the normal C++ provider.
755 return bazelLabelForSharedDeps(ctx, modules)
756}
757
Jingwen Chen55bc8202021-11-02 06:40:51 +0000758func bazelLabelForSharedDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400759 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
760}
Liz Kammer2b8004b2021-10-04 13:55:44 -0400761
762type binaryLinkerAttrs struct {
763 Linkshared *bool
764}
765
Jingwen Chen55bc8202021-11-02 06:40:51 +0000766func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400767 attrs := binaryLinkerAttrs{}
768 archVariantProps := m.GetArchVariantProperties(ctx, &BinaryLinkerProperties{})
769 for axis, configToProps := range archVariantProps {
770 for _, p := range configToProps {
771 props := p.(*BinaryLinkerProperties)
772 staticExecutable := props.Static_executable
773 if axis == bazel.NoConfigAxis {
774 if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared {
775 attrs.Linkshared = &linkBinaryShared
776 }
777 } else if staticExecutable != nil {
778 // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a
779 // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling
780 ctx.ModuleErrorf("bp2build cannot migrate a module with arch/target-specific static_executable values")
781 }
782 }
783 }
784
785 return attrs
786}