blob: 2059f5e53de809bc77b897280932d509e95bee92 [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
255 cppStd *string
Liz Kammer35687bc2021-09-10 10:07:07 -0400256
257 localIncludes bazel.StringListAttribute
258 absoluteIncludes bazel.StringListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000259}
260
Liz Kammere6583482021-10-19 13:56:10 -0400261func parseCommandLineFlags(soongFlags []string) []string {
262 var result []string
263 for _, flag := range soongFlags {
264 // Soong's cflags can contain spaces, like `-include header.h`. For
265 // Bazel's copts, split them up to be compatible with the
266 // no_copts_tokenization feature.
267 result = append(result, strings.Split(flag, " ")...)
268 }
269 return result
270}
Jingwen Chened9c17d2021-04-13 07:14:55 +0000271
Jingwen Chen55bc8202021-11-02 06:40:51 +0000272func (ca *compilerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, props *BaseCompilerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400273 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
274 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
275 if srcsList, ok := parseSrcs(ctx, props); ok {
276 ca.srcs.SetSelectValue(axis, config, srcsList)
Chris Parsons990c4f42021-05-25 12:10:58 -0400277 }
278
Liz Kammere6583482021-10-19 13:56:10 -0400279 localIncludeDirs := props.Local_include_dirs
280 if axis == bazel.NoConfigAxis {
281 ca.cppStd = bp2buildResolveCppStdValue(props.Cpp_std, props.Gnu_extensions)
Chris Parsons484e50a2021-05-13 15:13:04 -0400282
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 {
323 if props, exists := productVariableProps[propName]; exists {
324 for _, prop := range props {
325 flags, ok := prop.Property.([]string)
326 if !ok {
327 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
328 }
329 newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable)
Jingwen Chena47f28d2021-11-02 16:43:57 +0000330 attr.SetSelectValue(prop.ConfigurationAxis(), prop.FullConfig, 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
374func bp2buildResolveCppStdValue(cpp_std *string, gnu_extensions *bool) *string {
375 var cppStd *string
376 // If cpp_std is not specified, don't generate it in the
377 // BUILD file. For readability purposes, cpp_std and gnu_extensions are
378 // combined into a single -std=<version> copt, except in the
379 // default case where cpp_std is nil and gnu_extensions is true or unspecified,
380 // then the toolchain's default "gnu++17" will be used.
381 if cpp_std != nil {
382 // TODO(b/202491296): Handle C_std.
383 // These transformations are shared with compiler.go.
384 cppStdVal := parseCppStd(cpp_std)
385 _, cppStdVal = maybeReplaceGnuToC(gnu_extensions, "", cppStdVal)
386 cppStd = &cppStdVal
387 } else if gnu_extensions != nil && !*gnu_extensions {
388 cppStdVal := "c++17"
389 cppStd = &cppStdVal
390 }
391 return cppStd
392}
393
394// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000395func bp2BuildParseBaseProps(ctx android.BazelConversionPathContext, module *Module) baseAttributes {
Liz Kammere6583482021-10-19 13:56:10 -0400396 archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
397 archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{})
398
399 var implementationHdrs bazel.LabelListAttribute
400
401 axisToConfigs := map[bazel.ConfigurationAxis]map[string]bool{}
402 allAxesAndConfigs := func(cp android.ConfigurationAxisToArchVariantProperties) {
403 for axis, configMap := range cp {
404 if _, ok := axisToConfigs[axis]; !ok {
405 axisToConfigs[axis] = map[string]bool{}
406 }
407 for config, _ := range configMap {
408 axisToConfigs[axis][config] = true
Chris Parsonsa967f252021-09-23 16:34:35 -0400409 }
410 }
411 }
Liz Kammere6583482021-10-19 13:56:10 -0400412 allAxesAndConfigs(archVariantCompilerProps)
413 allAxesAndConfigs(archVariantLinkerProps)
Chris Parsonsa967f252021-09-23 16:34:35 -0400414
Liz Kammere6583482021-10-19 13:56:10 -0400415 compilerAttrs := compilerAttributes{}
416 linkerAttrs := linkerAttributes{}
417
418 for axis, configs := range axisToConfigs {
419 for config, _ := range configs {
420 var allHdrs []string
421 if baseCompilerProps, ok := archVariantCompilerProps[axis][config].(*BaseCompilerProperties); ok {
422 allHdrs = baseCompilerProps.Generated_headers
423
424 (&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, config, baseCompilerProps)
425 }
426
427 var exportHdrs []string
428
429 if baseLinkerProps, ok := archVariantLinkerProps[axis][config].(*BaseLinkerProperties); ok {
430 exportHdrs = baseLinkerProps.Export_generated_headers
431
432 (&linkerAttrs).bp2buildForAxisAndConfig(ctx, module.Binary(), axis, config, baseLinkerProps)
433 }
434 headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportHdrs, android.BazelLabelForModuleDeps)
435 implementationHdrs.SetSelectValue(axis, config, headers.implementation)
436 compilerAttrs.hdrs.SetSelectValue(axis, config, headers.export)
437 }
438 }
439
440 compilerAttrs.convertStlProps(ctx, module)
441 (&linkerAttrs).convertStripProps(ctx, module)
442
443 productVariableProps := android.ProductVariableProperties(ctx)
444
445 (&compilerAttrs).convertProductVariables(ctx, productVariableProps)
446 (&linkerAttrs).convertProductVariables(ctx, productVariableProps)
447
448 (&compilerAttrs).finalize(ctx, implementationHdrs)
449 (&linkerAttrs).finalize()
450
451 return baseAttributes{
452 compilerAttrs,
453 linkerAttrs,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000454 }
455}
456
457// Convenience struct to hold all attributes parsed from linker properties.
458type linkerAttributes struct {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400459 deps bazel.LabelListAttribute
460 implementationDeps bazel.LabelListAttribute
461 dynamicDeps bazel.LabelListAttribute
462 implementationDynamicDeps bazel.LabelListAttribute
463 wholeArchiveDeps bazel.LabelListAttribute
464 systemDynamicDeps bazel.LabelListAttribute
465
Jingwen Chen6ada5892021-09-17 11:38:09 +0000466 linkCrt bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000467 useLibcrt bazel.BoolAttribute
468 linkopts bazel.StringListAttribute
Liz Kammerd2871182021-10-04 13:54:37 -0400469 additionalLinkerInputs bazel.LabelListAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000470 stripKeepSymbols bazel.BoolAttribute
471 stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
472 stripKeepSymbolsList bazel.StringListAttribute
473 stripAll bazel.BoolAttribute
474 stripNone bazel.BoolAttribute
Liz Kammer0eae52e2021-10-06 10:32:26 -0400475 features bazel.StringListAttribute
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400476}
477
Jingwen Chen55bc8202021-11-02 06:40:51 +0000478func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, isBinary bool, axis bazel.ConfigurationAxis, config string, props *BaseLinkerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400479 // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module
480 var axisFeatures []string
Liz Kammer7a210ac2021-09-22 15:52:58 -0400481
Liz Kammere6583482021-10-19 13:56:10 -0400482 // Excludes to parallel Soong:
483 // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0
484 staticLibs := android.FirstUniqueStrings(props.Static_libs)
485 staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, staticLibs, props.Exclude_static_libs, props.Export_static_lib_headers, bazelLabelForStaticDepsExcludes)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400486
Liz Kammere6583482021-10-19 13:56:10 -0400487 headerLibs := android.FirstUniqueStrings(props.Header_libs)
488 hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps)
Jingwen Chen63930982021-03-24 10:04:33 -0400489
Liz Kammere6583482021-10-19 13:56:10 -0400490 (&hDeps.export).Append(staticDeps.export)
491 la.deps.SetSelectValue(axis, config, hDeps.export)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000492
Liz Kammere6583482021-10-19 13:56:10 -0400493 (&hDeps.implementation).Append(staticDeps.implementation)
494 la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation)
Liz Kammer0eae52e2021-10-06 10:32:26 -0400495
Liz Kammere6583482021-10-19 13:56:10 -0400496 wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs)
497 la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs))
498
499 systemSharedLibs := props.System_shared_libs
500 // systemSharedLibs distinguishes between nil/empty list behavior:
501 // nil -> use default values
502 // empty list -> no values specified
503 if len(systemSharedLibs) > 0 {
504 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
505 }
506 la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
507
508 sharedLibs := android.FirstUniqueStrings(props.Shared_libs)
509 sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, sharedLibs, props.Exclude_shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes)
510 la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export)
511 la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation)
512
513 if !BoolDefault(props.Pack_relocations, packRelocationsDefault) {
514 axisFeatures = append(axisFeatures, "disable_pack_relocations")
515 }
516
517 if Bool(props.Allow_undefined_symbols) {
518 axisFeatures = append(axisFeatures, "-no_undefined_symbols")
519 }
520
521 var linkerFlags []string
522 if len(props.Ldflags) > 0 {
523 linkerFlags = append(linkerFlags, props.Ldflags...)
524 // binaries remove static flag if -shared is in the linker flags
525 if isBinary && android.InList("-shared", linkerFlags) {
526 axisFeatures = append(axisFeatures, "-static_flag")
527 }
528 }
529 if props.Version_script != nil {
530 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script)
531 la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}})
532 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label))
533 }
534 la.linkopts.SetSelectValue(axis, config, linkerFlags)
535 la.useLibcrt.SetSelectValue(axis, config, props.libCrt())
536
537 // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it.
538 if props.crt() != nil {
539 if axis == bazel.NoConfigAxis {
540 la.linkCrt.SetSelectValue(axis, config, props.crt())
541 } else if axis == bazel.ArchConfigurationAxis {
542 ctx.ModuleErrorf("nocrt is not supported for arch variants")
543 }
544 }
545
546 if axisFeatures != nil {
547 la.features.SetSelectValue(axis, config, axisFeatures)
548 }
549}
550
Jingwen Chen55bc8202021-11-02 06:40:51 +0000551func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) {
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000552 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StripProperties{}) {
553 for config, props := range configToProps {
554 if stripProperties, ok := props.(*StripProperties); ok {
Liz Kammere6583482021-10-19 13:56:10 -0400555 la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols)
556 la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list)
557 la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame)
558 la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All)
559 la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000560 }
561 }
562 }
Liz Kammere6583482021-10-19 13:56:10 -0400563}
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000564
Jingwen Chen55bc8202021-11-02 06:40:51 +0000565func (la *linkerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Jingwen Chen6ada5892021-09-17 11:38:09 +0000566
Liz Kammer47535c52021-06-02 16:02:22 -0400567 type productVarDep struct {
568 // the name of the corresponding excludes field, if one exists
569 excludesField string
570 // reference to the bazel attribute that should be set for the given product variable config
571 attribute *bazel.LabelListAttribute
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400572
Jingwen Chen55bc8202021-11-02 06:40:51 +0000573 depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList
Liz Kammer47535c52021-06-02 16:02:22 -0400574 }
575
576 productVarToDepFields := map[string]productVarDep{
577 // product variables do not support exclude_shared_libs
Jingwen Chen55bc8202021-11-02 06:40:51 +0000578 "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes},
579 "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes},
580 "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes},
Liz Kammer47535c52021-06-02 16:02:22 -0400581 }
582
Liz Kammer47535c52021-06-02 16:02:22 -0400583 for name, dep := range productVarToDepFields {
584 props, exists := productVariableProps[name]
585 excludeProps, excludesExists := productVariableProps[dep.excludesField]
586 // if neither an include or excludes property exists, then skip it
587 if !exists && !excludesExists {
588 continue
589 }
590 // collect all the configurations that an include or exclude property exists for.
591 // we want to iterate all configurations rather than either the include or exclude because for a
592 // particular configuration we may have only and include or only an exclude to handle
593 configs := make(map[string]bool, len(props)+len(excludeProps))
594 for config := range props {
595 configs[config] = true
596 }
597 for config := range excludeProps {
598 configs[config] = true
599 }
600
601 for config := range configs {
602 prop, includesExists := props[config]
603 excludesProp, excludesExists := excludeProps[config]
604 var includes, excludes []string
605 var ok bool
606 // if there was no includes/excludes property, casting fails and that's expected
607 if includes, ok = prop.Property.([]string); includesExists && !ok {
608 ctx.ModuleErrorf("Could not convert product variable %s property", name)
609 }
610 if excludes, ok = excludesProp.Property.([]string); excludesExists && !ok {
611 ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
612 }
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400613
Jingwen Chena47f28d2021-11-02 16:43:57 +0000614 dep.attribute.SetSelectValue(prop.ConfigurationAxis(), config, dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes))
Liz Kammer47535c52021-06-02 16:02:22 -0400615 }
616 }
Liz Kammere6583482021-10-19 13:56:10 -0400617}
Liz Kammer47535c52021-06-02 16:02:22 -0400618
Liz Kammere6583482021-10-19 13:56:10 -0400619func (la *linkerAttributes) finalize() {
620 la.deps.ResolveExcludes()
621 la.implementationDeps.ResolveExcludes()
622 la.dynamicDeps.ResolveExcludes()
623 la.implementationDynamicDeps.ResolveExcludes()
624 la.wholeArchiveDeps.ResolveExcludes()
625 la.systemDynamicDeps.ForceSpecifyEmptyList = true
Jingwen Chen91220d72021-03-24 02:18:33 -0400626}
627
Jingwen Chened9c17d2021-04-13 07:14:55 +0000628// Relativize a list of root-relative paths with respect to the module's
629// directory.
630//
631// include_dirs Soong prop are root-relative (b/183742505), but
632// local_include_dirs, export_include_dirs and export_system_include_dirs are
633// module dir relative. This function makes a list of paths entirely module dir
634// relative.
635//
636// For the `include` attribute, Bazel wants the paths to be relative to the
637// module.
638func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000639 var relativePaths []string
640 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000641 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
642 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
643 if err != nil {
644 panic(err)
645 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000646 relativePaths = append(relativePaths, relativePath)
647 }
648 return relativePaths
649}
650
Liz Kammer5fad5012021-09-09 14:08:21 -0400651// BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel
652// attributes.
653type BazelIncludes struct {
654 Includes bazel.StringListAttribute
655 SystemIncludes bazel.StringListAttribute
656}
657
Jingwen Chen55bc8202021-11-02 06:40:51 +0000658func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module) BazelIncludes {
Jingwen Chen91220d72021-03-24 02:18:33 -0400659 libraryDecorator := module.linker.(*libraryDecorator)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400660 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
661}
Jingwen Chen91220d72021-03-24 02:18:33 -0400662
Liz Kammer5fad5012021-09-09 14:08:21 -0400663// Bp2buildParseExportedIncludesForPrebuiltLibrary returns a BazelIncludes with Bazel-ified values
664// to export includes from the underlying module's properties.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000665func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.BazelConversionPathContext, module *Module) BazelIncludes {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400666 prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker)
667 libraryDecorator := prebuiltLibraryLinker.libraryDecorator
668 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
669}
670
671// bp2BuildParseExportedIncludes creates a string list attribute contains the
672// exported included directories of a module.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000673func bp2BuildParseExportedIncludesHelper(ctx android.BazelConversionPathContext, module *Module, libraryDecorator *libraryDecorator) BazelIncludes {
Liz Kammer5fad5012021-09-09 14:08:21 -0400674 exported := BazelIncludes{}
Liz Kammer9abd62d2021-05-21 08:37:59 -0400675 for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) {
676 for config, props := range configToProps {
677 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
Liz Kammer5fad5012021-09-09 14:08:21 -0400678 if len(flagExporterProperties.Export_include_dirs) > 0 {
679 exported.Includes.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs)
680 }
681 if len(flagExporterProperties.Export_system_include_dirs) > 0 {
682 exported.SystemIncludes.SetSelectValue(axis, config, flagExporterProperties.Export_system_include_dirs)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400683 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400684 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400685 }
686 }
Liz Kammer5fad5012021-09-09 14:08:21 -0400687 exported.Includes.DeduplicateAxesFromBase()
688 exported.SystemIncludes.DeduplicateAxesFromBase()
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400689
Liz Kammer5fad5012021-09-09 14:08:21 -0400690 return exported
Jingwen Chen91220d72021-03-24 02:18:33 -0400691}
Chris Parsons953b3562021-09-20 15:14:39 -0400692
Jingwen Chen55bc8202021-11-02 06:40:51 +0000693func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400694 label := android.BazelModuleLabel(ctx, m)
695 if aModule, ok := m.(android.Module); ok {
696 if ctx.OtherModuleType(aModule) == "cc_library" && !android.GenerateCcLibraryStaticOnly(m.Name()) {
697 label += "_bp2build_cc_library_static"
698 }
699 }
700 return label
701}
702
Jingwen Chen55bc8202021-11-02 06:40:51 +0000703func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400704 // cc_library, at it's root name, propagates the shared library, which depends on the static
705 // library.
706 return android.BazelModuleLabel(ctx, m)
707}
708
Jingwen Chen55bc8202021-11-02 06:40:51 +0000709func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400710 label := bazelLabelForStaticModule(ctx, m)
711 if aModule, ok := m.(android.Module); ok {
712 if android.IsModulePrebuilt(aModule) {
713 label += "_alwayslink"
714 }
715 }
716 return label
717}
718
Jingwen Chen55bc8202021-11-02 06:40:51 +0000719func bazelLabelForWholeDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400720 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps)
721}
722
Jingwen Chen55bc8202021-11-02 06:40:51 +0000723func bazelLabelForWholeDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400724 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps)
725}
726
Jingwen Chen55bc8202021-11-02 06:40:51 +0000727func bazelLabelForStaticDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400728 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule)
729}
730
Jingwen Chen55bc8202021-11-02 06:40:51 +0000731func bazelLabelForStaticDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400732 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule)
733}
734
Jingwen Chen55bc8202021-11-02 06:40:51 +0000735func bazelLabelForSharedDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400736 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule)
737}
738
Jingwen Chen55bc8202021-11-02 06:40:51 +0000739func bazelLabelForHeaderDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400740 // This is not elegant, but bp2build's shared library targets only propagate
741 // their header information as part of the normal C++ provider.
742 return bazelLabelForSharedDeps(ctx, modules)
743}
744
Jingwen Chen55bc8202021-11-02 06:40:51 +0000745func bazelLabelForSharedDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400746 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
747}
Liz Kammer2b8004b2021-10-04 13:55:44 -0400748
749type binaryLinkerAttrs struct {
750 Linkshared *bool
751}
752
Jingwen Chen55bc8202021-11-02 06:40:51 +0000753func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400754 attrs := binaryLinkerAttrs{}
755 archVariantProps := m.GetArchVariantProperties(ctx, &BinaryLinkerProperties{})
756 for axis, configToProps := range archVariantProps {
757 for _, p := range configToProps {
758 props := p.(*BinaryLinkerProperties)
759 staticExecutable := props.Static_executable
760 if axis == bazel.NoConfigAxis {
761 if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared {
762 attrs.Linkshared = &linkBinaryShared
763 }
764 } else if staticExecutable != nil {
765 // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a
766 // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling
767 ctx.ModuleErrorf("bp2build cannot migrate a module with arch/target-specific static_executable values")
768 }
769 }
770 }
771
772 return attrs
773}