blob: 9880444385b7c7e18f00ae25091f72846cda63e6 [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//
Cole Faust7071a052022-07-29 15:58:33 -07007// http://www.apache.org/licenses/LICENSE-2.0
Jingwen Chen91220d72021-03-24 02:18:33 -04008//
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"
Alix1be00d42022-05-16 22:56:04 +000023 "android/soong/cc/config"
Liz Kammer7a210ac2021-09-22 15:52:58 -040024
Chris Parsons953b3562021-09-20 15:14:39 -040025 "github.com/google/blueprint"
Liz Kammerba7a9c52021-05-26 08:45:30 -040026
27 "github.com/google/blueprint/proptools"
Jingwen Chen91220d72021-03-24 02:18:33 -040028)
29
Liz Kammerae3994e2021-10-19 09:45:48 -040030const (
Liz Kammer12615db2021-09-28 09:19:17 -040031 cSrcPartition = "c"
32 asSrcPartition = "as"
Cole Faust7071a052022-07-29 15:58:33 -070033 asmSrcPartition = "asm"
Trevor Radcliffeef9c9002022-05-13 20:55:35 +000034 lSrcPartition = "l"
35 llSrcPartition = "ll"
Liz Kammer12615db2021-09-28 09:19:17 -040036 cppSrcPartition = "cpp"
37 protoSrcPartition = "proto"
Liz Kammerae3994e2021-10-19 09:45:48 -040038)
39
Liz Kammer2222c6b2021-05-24 15:41:47 -040040// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
Jingwen Chenbcf53042021-05-26 04:42:42 +000041// properties which apply to either the shared or static version of a cc_library module.
Liz Kammer2222c6b2021-05-24 15:41:47 -040042type staticOrSharedAttributes struct {
Jingwen Chenc4dc9b42021-06-11 12:51:48 +000043 Srcs bazel.LabelListAttribute
44 Srcs_c bazel.LabelListAttribute
45 Srcs_as bazel.LabelListAttribute
Liz Kammere6583482021-10-19 13:56:10 -040046 Hdrs bazel.LabelListAttribute
Jingwen Chenc4dc9b42021-06-11 12:51:48 +000047 Copts bazel.StringListAttribute
Jingwen Chen14a8bda2021-06-02 11:10:02 +000048
Liz Kammer12615db2021-09-28 09:19:17 -040049 Deps bazel.LabelListAttribute
50 Implementation_deps bazel.LabelListAttribute
51 Dynamic_deps bazel.LabelListAttribute
52 Implementation_dynamic_deps bazel.LabelListAttribute
53 Whole_archive_deps bazel.LabelListAttribute
54 Implementation_whole_archive_deps bazel.LabelListAttribute
Cole Faust6b29f592022-08-09 09:50:56 -070055 Runtime_deps bazel.LabelListAttribute
Chris Parsons51f8c392021-08-03 21:01:05 -040056
57 System_dynamic_deps bazel.LabelListAttribute
Chris Parsons58852a02021-12-09 18:10:18 -050058
59 Enabled bazel.BoolAttribute
Yu Liufc603162022-03-01 15:44:08 -080060
Yu Liu8d82ac52022-05-17 15:13:28 -070061 Native_coverage bazel.BoolAttribute
62
Yu Liufc603162022-03-01 15:44:08 -080063 sdkAttributes
Jingwen Chen53681ef2021-04-29 08:15:13 +000064}
65
Sam Delmericoc7681022022-02-04 21:01:20 +000066// groupSrcsByExtension partitions `srcs` into groups based on file extension.
Jingwen Chen55bc8202021-11-02 06:40:51 +000067func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute {
Liz Kammer57e2e7a2021-09-20 12:55:02 -040068 // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl
69 // macro.
70 addSuffixForFilegroup := func(suffix string) bazel.LabelMapper {
Liz Kammer12615db2021-09-28 09:19:17 -040071 return func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) {
72 m, exists := ctx.ModuleFromName(label.OriginalModuleName)
73 labelStr := label.Label
Sam Delmericoc7681022022-02-04 21:01:20 +000074 if !exists || !android.IsFilegroup(ctx, m) {
Liz Kammer12615db2021-09-28 09:19:17 -040075 return labelStr, false
Jingwen Chen14a8bda2021-06-02 11:10:02 +000076 }
Liz Kammer12615db2021-09-28 09:19:17 -040077 return labelStr + suffix, true
Chris Parsons5a34ffb2021-07-21 14:34:58 -040078 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +000079 }
80
Liz Kammer57e2e7a2021-09-20 12:55:02 -040081 // TODO(b/190006308): Handle language detection of sources in a Bazel rule.
Sam Delmericoc7681022022-02-04 21:01:20 +000082 labels := bazel.LabelPartitions{
83 protoSrcPartition: android.ProtoSrcLabelPartition,
Liz Kammeraabfb5d2021-12-08 15:25:06 -050084 cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")},
85 asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")},
Cole Faust7071a052022-07-29 15:58:33 -070086 asmSrcPartition: bazel.LabelPartition{Extensions: []string{".asm"}},
Trevor Radcliffeef9c9002022-05-13 20:55:35 +000087 // TODO(http://b/231968910): If there is ever a filegroup target that
88 // contains .l or .ll files we will need to find a way to add a
89 // LabelMapper for these that identifies these filegroups and
90 // converts them appropriately
91 lSrcPartition: bazel.LabelPartition{Extensions: []string{".l"}},
92 llSrcPartition: bazel.LabelPartition{Extensions: []string{".ll"}},
Liz Kammer57e2e7a2021-09-20 12:55:02 -040093 // C++ is the "catch-all" group, and comprises generated sources because we don't
94 // know the language of these sources until the genrule is executed.
Liz Kammeraabfb5d2021-12-08 15:25:06 -050095 cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true},
Sam Delmericoc7681022022-02-04 21:01:20 +000096 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +000097
Sam Delmericoc7681022022-02-04 21:01:20 +000098 return bazel.PartitionLabelListAttribute(ctx, &srcs, labels)
Jingwen Chen14a8bda2021-06-02 11:10:02 +000099}
100
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000101// bp2BuildParseLibProps returns the attributes for a variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000102func bp2BuildParseLibProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000103 lib, ok := module.compiler.(*libraryDecorator)
104 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400105 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000106 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000107 return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic)
108}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000109
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000110// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000111func bp2BuildParseSharedProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000112 return bp2BuildParseLibProps(ctx, module, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000113}
114
115// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000116func bp2BuildParseStaticProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000117 return bp2BuildParseLibProps(ctx, module, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400118}
119
Liz Kammer7a210ac2021-09-22 15:52:58 -0400120type depsPartition struct {
121 export bazel.LabelList
122 implementation bazel.LabelList
123}
124
Jingwen Chen55bc8202021-11-02 06:40:51 +0000125type bazelLabelForDepsFn func(android.BazelConversionPathContext, []string) bazel.LabelList
Liz Kammer7a210ac2021-09-22 15:52:58 -0400126
Jingwen Chen55bc8202021-11-02 06:40:51 +0000127func maybePartitionExportedAndImplementationsDeps(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, exportedDeps []string, fn bazelLabelForDepsFn) depsPartition {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400128 if !exportsDeps {
129 return depsPartition{
130 implementation: fn(ctx, allDeps),
131 }
132 }
133
Liz Kammer7a210ac2021-09-22 15:52:58 -0400134 implementation, export := android.FilterList(allDeps, exportedDeps)
135
136 return depsPartition{
137 export: fn(ctx, export),
138 implementation: fn(ctx, implementation),
139 }
140}
141
Jingwen Chen55bc8202021-11-02 06:40:51 +0000142type bazelLabelForDepsExcludesFn func(android.BazelConversionPathContext, []string, []string) bazel.LabelList
Liz Kammer7a210ac2021-09-22 15:52:58 -0400143
Jingwen Chen55bc8202021-11-02 06:40:51 +0000144func maybePartitionExportedAndImplementationsDepsExcludes(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, excludes, exportedDeps []string, fn bazelLabelForDepsExcludesFn) depsPartition {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400145 if !exportsDeps {
146 return depsPartition{
147 implementation: fn(ctx, allDeps, excludes),
148 }
149 }
Liz Kammer7a210ac2021-09-22 15:52:58 -0400150 implementation, export := android.FilterList(allDeps, exportedDeps)
151
152 return depsPartition{
153 export: fn(ctx, export, excludes),
154 implementation: fn(ctx, implementation, excludes),
155 }
156}
157
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000158// Parses properties common to static and shared libraries. Also used for prebuilt libraries.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000159func bp2buildParseStaticOrSharedProps(ctx android.BazelConversionPathContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes {
Liz Kammer135bf552021-08-11 10:46:06 -0400160 attrs := staticOrSharedAttributes{}
Jingwen Chenbcf53042021-05-26 04:42:42 +0000161
Liz Kammer9abd62d2021-05-21 08:37:59 -0400162 setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
Alix1be00d42022-05-16 22:56:04 +0000163 attrs.Copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, true, filterOutStdFlag))
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000164 attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
Chris Parsons953b3562021-09-20 15:14:39 -0400165 attrs.System_dynamic_deps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, props.System_shared_libs))
Liz Kammer7a210ac2021-09-22 15:52:58 -0400166
Liz Kammer2b8004b2021-10-04 13:55:44 -0400167 staticDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Static_libs, props.Export_static_lib_headers, bazelLabelForStaticDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400168 attrs.Deps.SetSelectValue(axis, config, staticDeps.export)
169 attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation)
170
Liz Kammer2b8004b2021-10-04 13:55:44 -0400171 sharedDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400172 attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export)
173 attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation)
174
175 attrs.Whole_archive_deps.SetSelectValue(axis, config, bazelLabelForWholeDeps(ctx, props.Whole_static_libs))
Chris Parsons58852a02021-12-09 18:10:18 -0500176 attrs.Enabled.SetSelectValue(axis, config, props.Enabled)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000177 }
Liz Kammer135bf552021-08-11 10:46:06 -0400178 // system_dynamic_deps distinguishes between nil/empty list behavior:
179 // nil -> use default values
180 // empty list -> no values specified
181 attrs.System_dynamic_deps.ForceSpecifyEmptyList = true
Jingwen Chenbcf53042021-05-26 04:42:42 +0000182
183 if isStatic {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000184 bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
185 if staticOrSharedProps, ok := props.(*StaticProperties); ok {
186 setAttrs(axis, config, staticOrSharedProps.Static)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000187 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000188 })
Jingwen Chenbcf53042021-05-26 04:42:42 +0000189 } else {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000190 bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
191 if staticOrSharedProps, ok := props.(*SharedProperties); ok {
192 setAttrs(axis, config, staticOrSharedProps.Shared)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000193 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000194 })
Jingwen Chenbcf53042021-05-26 04:42:42 +0000195 }
196
Liz Kammerae3994e2021-10-19 09:45:48 -0400197 partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs)
198 attrs.Srcs = partitionedSrcs[cppSrcPartition]
199 attrs.Srcs_c = partitionedSrcs[cSrcPartition]
200 attrs.Srcs_as = partitionedSrcs[asSrcPartition]
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000201
Liz Kammer12615db2021-09-28 09:19:17 -0400202 if !partitionedSrcs[protoSrcPartition].IsEmpty() {
203 // TODO(b/208815215): determine whether this is used and add support if necessary
204 ctx.ModuleErrorf("Migrating static/shared only proto srcs is not currently supported")
205 }
206
Jingwen Chenbcf53042021-05-26 04:42:42 +0000207 return attrs
Jingwen Chen53681ef2021-04-29 08:15:13 +0000208}
209
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400210// Convenience struct to hold all attributes parsed from prebuilt properties.
211type prebuiltAttributes struct {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000212 Src bazel.LabelAttribute
213 Enabled bazel.BoolAttribute
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400214}
215
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000216// NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000217func Bp2BuildParsePrebuiltLibraryProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) prebuiltAttributes {
218 manySourceFileError := func(axis bazel.ConfigurationAxis, config string) {
219 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most one source file for %s %s\n", axis, config)
220 }
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400221 var srcLabelAttribute bazel.LabelAttribute
222
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000223 parseSrcs := func(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, srcs []string) {
224 if len(srcs) > 1 {
225 manySourceFileError(axis, config)
226 return
227 } else if len(srcs) == 0 {
228 return
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400229 }
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000230 if srcLabelAttribute.SelectValue(axis, config) != nil {
231 manySourceFileError(axis, config)
232 return
233 }
234
235 src := android.BazelLabelForModuleSrcSingle(ctx, srcs[0])
236 srcLabelAttribute.SetSelectValue(axis, config, src)
237 }
238
239 bp2BuildPropParseHelper(ctx, module, &prebuiltLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
240 if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok {
241 parseSrcs(ctx, axis, config, prebuiltLinkerProperties.Srcs)
242 }
243 })
244
245 var enabledLabelAttribute bazel.BoolAttribute
246 parseAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
247 if props.Enabled != nil {
248 enabledLabelAttribute.SetSelectValue(axis, config, props.Enabled)
249 }
250 parseSrcs(ctx, axis, config, props.Srcs)
251 }
252
253 if isStatic {
254 bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
255 if staticProperties, ok := props.(*StaticProperties); ok {
256 parseAttrs(axis, config, staticProperties.Static)
257 }
258 })
259 } else {
260 bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
261 if sharedProperties, ok := props.(*SharedProperties); ok {
262 parseAttrs(axis, config, sharedProperties.Shared)
263 }
264 })
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400265 }
266
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400267 return prebuiltAttributes{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000268 Src: srcLabelAttribute,
269 Enabled: enabledLabelAttribute,
270 }
271}
272
273func bp2BuildPropParseHelper(ctx android.ArchVariantContext, module *Module, propsType interface{}, parseFunc func(axis bazel.ConfigurationAxis, config string, props interface{})) {
274 for axis, configToProps := range module.GetArchVariantProperties(ctx, propsType) {
275 for config, props := range configToProps {
276 parseFunc(axis, config, props)
277 }
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400278 }
279}
280
Liz Kammere6583482021-10-19 13:56:10 -0400281type baseAttributes struct {
282 compilerAttributes
283 linkerAttributes
Liz Kammer12615db2021-09-28 09:19:17 -0400284
285 protoDependency *bazel.LabelAttribute
Liz Kammere6583482021-10-19 13:56:10 -0400286}
287
Jingwen Chen107c0de2021-04-09 10:43:12 +0000288// Convenience struct to hold all attributes parsed from compiler properties.
289type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400290 // Options for all languages
291 copts bazel.StringListAttribute
292 // Assembly options and sources
293 asFlags bazel.StringListAttribute
294 asSrcs bazel.LabelListAttribute
Cole Faust7071a052022-07-29 15:58:33 -0700295 asmSrcs bazel.LabelListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400296 // C options and sources
297 conlyFlags bazel.StringListAttribute
298 cSrcs bazel.LabelListAttribute
299 // C++ options and sources
300 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000301 srcs bazel.LabelListAttribute
Chris Parsons2c788392021-08-10 11:58:07 -0400302
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000303 // Lex sources and options
304 lSrcs bazel.LabelListAttribute
305 llSrcs bazel.LabelListAttribute
306 lexopts bazel.StringListAttribute
307
Liz Kammere6583482021-10-19 13:56:10 -0400308 hdrs bazel.LabelListAttribute
309
Chris Parsons2c788392021-08-10 11:58:07 -0400310 rtti bazel.BoolAttribute
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000311
312 // Not affected by arch variants
313 stl *string
Chris Parsons79bd2b72021-11-29 17:52:41 -0500314 cStd *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000315 cppStd *string
Liz Kammer35687bc2021-09-10 10:07:07 -0400316
317 localIncludes bazel.StringListAttribute
318 absoluteIncludes bazel.StringListAttribute
Liz Kammer12615db2021-09-28 09:19:17 -0400319
Liz Kammer1263d9b2021-12-10 14:28:20 -0500320 includes BazelIncludes
321
Liz Kammer12615db2021-09-28 09:19:17 -0400322 protoSrcs bazel.LabelListAttribute
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000323
324 stubsSymbolFile *string
325 stubsVersions bazel.StringListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000326}
327
Liz Kammercac7f692021-12-16 14:19:32 -0500328type filterOutFn func(string) bool
329
330func filterOutStdFlag(flag string) bool {
331 return strings.HasPrefix(flag, "-std=")
332}
333
Alix1be00d42022-05-16 22:56:04 +0000334func filterOutClangUnknownCflags(flag string) bool {
335 for _, f := range config.ClangUnknownCflags {
336 if f == flag {
337 return true
338 }
339 }
340 return false
341}
342
343func parseCommandLineFlags(soongFlags []string, noCoptsTokenization bool, filterOut ...filterOutFn) []string {
Liz Kammere6583482021-10-19 13:56:10 -0400344 var result []string
345 for _, flag := range soongFlags {
Alix1be00d42022-05-16 22:56:04 +0000346 skipFlag := false
347 for _, filter := range filterOut {
348 if filter != nil && filter(flag) {
349 skipFlag = true
350 }
351 }
352 if skipFlag {
Liz Kammercac7f692021-12-16 14:19:32 -0500353 continue
354 }
Liz Kammere6583482021-10-19 13:56:10 -0400355 // Soong's cflags can contain spaces, like `-include header.h`. For
356 // Bazel's copts, split them up to be compatible with the
357 // no_copts_tokenization feature.
Alix1be00d42022-05-16 22:56:04 +0000358 if noCoptsTokenization {
359 result = append(result, strings.Split(flag, " ")...)
360 } else {
361 // Soong's Version Script and Dynamic List Properties are added as flags
362 // to Bazel's linkopts using "($location label)" syntax.
363 // Splitting on spaces would separate this into two different flags
364 // "($ location" and "label)"
365 result = append(result, flag)
366 }
Liz Kammere6583482021-10-19 13:56:10 -0400367 }
368 return result
369}
Jingwen Chened9c17d2021-04-13 07:14:55 +0000370
Jingwen Chen55bc8202021-11-02 06:40:51 +0000371func (ca *compilerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, props *BaseCompilerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400372 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
373 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
374 if srcsList, ok := parseSrcs(ctx, props); ok {
375 ca.srcs.SetSelectValue(axis, config, srcsList)
Chris Parsons990c4f42021-05-25 12:10:58 -0400376 }
377
Liz Kammere6583482021-10-19 13:56:10 -0400378 localIncludeDirs := props.Local_include_dirs
379 if axis == bazel.NoConfigAxis {
Chris Parsons79bd2b72021-11-29 17:52:41 -0500380 ca.cStd, ca.cppStd = bp2buildResolveCppStdValue(props.C_std, props.Cpp_std, props.Gnu_extensions)
Liz Kammere6583482021-10-19 13:56:10 -0400381 if includeBuildDirectory(props.Include_build_directory) {
382 localIncludeDirs = append(localIncludeDirs, ".")
Liz Kammer222bdcf2021-10-11 14:15:51 -0400383 }
Jingwen Chene32e9e02021-04-23 09:17:24 +0000384 }
385
Liz Kammere6583482021-10-19 13:56:10 -0400386 ca.absoluteIncludes.SetSelectValue(axis, config, props.Include_dirs)
387 ca.localIncludes.SetSelectValue(axis, config, localIncludeDirs)
388
Liz Kammercac7f692021-12-16 14:19:32 -0500389 // In Soong, cflags occur on the command line before -std=<val> flag, resulting in the value being
390 // overridden. In Bazel we always allow overriding, via flags; however, this can cause
391 // incompatibilities, so we remove "-std=" flags from Cflag properties while leaving it in other
392 // cases.
Alix1be00d42022-05-16 22:56:04 +0000393 ca.copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, true, filterOutStdFlag, filterOutClangUnknownCflags))
394 ca.asFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Asflags, true, nil))
395 ca.conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Conlyflags, true, filterOutClangUnknownCflags))
396 ca.cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Cppflags, true, filterOutClangUnknownCflags))
Liz Kammere6583482021-10-19 13:56:10 -0400397 ca.rtti.SetSelectValue(axis, config, props.Rtti)
398}
399
Jingwen Chen55bc8202021-11-02 06:40:51 +0000400func (ca *compilerAttributes) convertStlProps(ctx android.ArchVariantContext, module *Module) {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000401 bp2BuildPropParseHelper(ctx, module, &StlProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
402 if stlProps, ok := props.(*StlProperties); ok {
403 if stlProps.Stl == nil {
404 return
405 }
406 if ca.stl == nil {
Liz Kammer7128d382022-05-12 11:42:33 -0400407 stl := deduplicateStlInput(*stlProps.Stl)
408 ca.stl = &stl
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000409 } else if ca.stl != stlProps.Stl {
410 ctx.ModuleErrorf("Unsupported conversion: module with different stl for different variants: %s and %s", *ca.stl, stlProps.Stl)
Liz Kammer9abd62d2021-05-21 08:37:59 -0400411 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000412 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000413 })
Liz Kammere6583482021-10-19 13:56:10 -0400414}
Jingwen Chenc1c26502021-04-05 10:35:13 +0000415
Jingwen Chen55bc8202021-11-02 06:40:51 +0000416func (ca *compilerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Liz Kammerba7a9c52021-05-26 08:45:30 -0400417 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
Liz Kammere6583482021-10-19 13:56:10 -0400418 "Cflags": &ca.copts,
419 "Asflags": &ca.asFlags,
420 "CppFlags": &ca.cppFlags,
Liz Kammerba7a9c52021-05-26 08:45:30 -0400421 }
Liz Kammerba7a9c52021-05-26 08:45:30 -0400422 for propName, attr := range productVarPropNameToAttribute {
Jingwen Chen25825ca2021-11-15 12:28:43 +0000423 if productConfigProps, exists := productVariableProps[propName]; exists {
424 for productConfigProp, prop := range productConfigProps {
425 flags, ok := prop.([]string)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400426 if !ok {
427 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
428 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000429 newFlags, _ := bazel.TryVariableSubstitutions(flags, productConfigProp.Name)
430 attr.SetSelectValue(productConfigProp.ConfigurationAxis(), productConfigProp.SelectKey(), newFlags)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400431 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400432 }
433 }
Liz Kammere6583482021-10-19 13:56:10 -0400434}
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400435
Jingwen Chen55bc8202021-11-02 06:40:51 +0000436func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, implementationHdrs bazel.LabelListAttribute) {
Liz Kammere6583482021-10-19 13:56:10 -0400437 ca.srcs.ResolveExcludes()
438 partitionedSrcs := groupSrcsByExtension(ctx, ca.srcs)
439
Liz Kammer12615db2021-09-28 09:19:17 -0400440 ca.protoSrcs = partitionedSrcs[protoSrcPartition]
441
Liz Kammere6583482021-10-19 13:56:10 -0400442 for p, lla := range partitionedSrcs {
443 // if there are no sources, there is no need for headers
444 if lla.IsEmpty() {
445 continue
446 }
447 lla.Append(implementationHdrs)
448 partitionedSrcs[p] = lla
449 }
450
451 ca.srcs = partitionedSrcs[cppSrcPartition]
452 ca.cSrcs = partitionedSrcs[cSrcPartition]
453 ca.asSrcs = partitionedSrcs[asSrcPartition]
Cole Faust7071a052022-07-29 15:58:33 -0700454 ca.asmSrcs = partitionedSrcs[asmSrcPartition]
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000455 ca.lSrcs = partitionedSrcs[lSrcPartition]
456 ca.llSrcs = partitionedSrcs[llSrcPartition]
Liz Kammere6583482021-10-19 13:56:10 -0400457
458 ca.absoluteIncludes.DeduplicateAxesFromBase()
459 ca.localIncludes.DeduplicateAxesFromBase()
460}
461
462// Parse srcs from an arch or OS's props value.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000463func parseSrcs(ctx android.BazelConversionPathContext, props *BaseCompilerProperties) (bazel.LabelList, bool) {
Liz Kammere6583482021-10-19 13:56:10 -0400464 anySrcs := false
465 // Add srcs-like dependencies such as generated files.
466 // First create a LabelList containing these dependencies, then merge the values with srcs.
467 generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources)
468 if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 {
469 anySrcs = true
470 }
471
472 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs)
473 if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 {
474 anySrcs = true
475 }
476 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs
477}
478
Liz Kammera5a29de2022-05-25 23:19:37 -0400479func bp2buildStdVal(std *string, prefix string, useGnu bool) *string {
480 defaultVal := prefix + "_std_default"
Chris Parsons79bd2b72021-11-29 17:52:41 -0500481 // If c{,pp}std properties are not specified, don't generate them in the BUILD file.
482 // Defaults are handled by the toolchain definition.
483 // However, if gnu_extensions is false, then the default gnu-to-c version must be specified.
Liz Kammera5a29de2022-05-25 23:19:37 -0400484 stdVal := proptools.StringDefault(std, defaultVal)
485 if stdVal == "experimental" || stdVal == defaultVal {
486 if stdVal == "experimental" {
487 stdVal = prefix + "_std_experimental"
488 }
489 if !useGnu {
490 stdVal += "_no_gnu"
491 }
492 } else if !useGnu {
493 stdVal = gnuToCReplacer.Replace(stdVal)
Chris Parsons79bd2b72021-11-29 17:52:41 -0500494 }
495
Liz Kammera5a29de2022-05-25 23:19:37 -0400496 if stdVal == defaultVal {
497 return nil
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500498 }
Liz Kammera5a29de2022-05-25 23:19:37 -0400499 return &stdVal
500}
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500501
Liz Kammera5a29de2022-05-25 23:19:37 -0400502func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) {
503 useGnu := useGnuExtensions(gnu_extensions)
504
505 return bp2buildStdVal(c_std, "c", useGnu), bp2buildStdVal(cpp_std, "cpp", useGnu)
Liz Kammere6583482021-10-19 13:56:10 -0400506}
507
Liz Kammer1263d9b2021-12-10 14:28:20 -0500508// packageFromLabel extracts package from a fully-qualified or relative Label and whether the label
509// is fully-qualified.
510// e.g. fully-qualified "//a/b:foo" -> "a/b", true, relative: ":bar" -> ".", false
511func packageFromLabel(label string) (string, bool) {
512 split := strings.Split(label, ":")
513 if len(split) != 2 {
514 return "", false
515 }
516 if split[0] == "" {
517 return ".", false
518 }
519 // remove leading "//"
520 return split[0][2:], true
521}
522
523// includesFromLabelList extracts relative/absolute includes from a bazel.LabelList>
524func includesFromLabelList(labelList bazel.LabelList) (relative, absolute []string) {
525 for _, hdr := range labelList.Includes {
526 if pkg, hasPkg := packageFromLabel(hdr.Label); hasPkg {
527 absolute = append(absolute, pkg)
528 } else if pkg != "" {
529 relative = append(relative, pkg)
530 }
531 }
532 return relative, absolute
533}
534
Cole Faust7071a052022-07-29 15:58:33 -0700535type YasmAttributes struct {
536 Srcs bazel.LabelListAttribute
537 Flags bazel.StringListAttribute
538 Include_dirs bazel.StringListAttribute
539}
540
541func bp2BuildYasm(ctx android.Bp2buildMutatorContext, m *Module, ca compilerAttributes) *bazel.LabelAttribute {
542 if ca.asmSrcs.IsEmpty() {
543 return nil
544 }
545
546 // Yasm needs the include directories from both local_includes and
547 // export_include_dirs. We don't care about actually exporting them from the
548 // yasm rule though, because they will also be present on the cc_ rule that
549 // wraps this yasm rule.
550 includes := ca.localIncludes.Clone()
551 bp2BuildPropParseHelper(ctx, m, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
552 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
553 if len(flagExporterProperties.Export_include_dirs) > 0 {
554 x := bazel.StringListAttribute{}
555 x.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs)
556 includes.Append(x)
557 }
558 }
559 })
560
561 ctx.CreateBazelTargetModule(
562 bazel.BazelTargetModuleProperties{
563 Rule_class: "yasm",
564 Bzl_load_location: "//build/bazel/rules/cc:yasm.bzl",
565 },
566 android.CommonAttributes{Name: m.Name() + "_yasm"},
567 &YasmAttributes{
568 Srcs: ca.asmSrcs,
569 Flags: ca.asFlags,
570 Include_dirs: *includes,
571 })
572
573 // We only want to add a dependency on the _yasm target if there are asm
574 // sources in the current configuration. If there are unconfigured asm
575 // sources, always add the dependency. Otherwise, add the dependency only
576 // on the configuration axes and values that had asm sources.
577 if len(ca.asmSrcs.Value.Includes) > 0 {
578 return bazel.MakeLabelAttribute(":" + m.Name() + "_yasm")
579 }
580
581 ret := &bazel.LabelAttribute{}
582 for _, axis := range ca.asmSrcs.SortedConfigurationAxes() {
583 for config := range ca.asmSrcs.ConfigurableValues[axis] {
584 ret.SetSelectValue(axis, config, bazel.Label{Label: ":" + m.Name() + "_yasm"})
585 }
586 }
587 return ret
588}
589
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000590// bp2BuildParseBaseProps returns all compiler, linker, library attributes of a cc module..
Liz Kammer12615db2021-09-28 09:19:17 -0400591func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) baseAttributes {
Liz Kammere6583482021-10-19 13:56:10 -0400592 archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
593 archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{})
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000594 archVariantLibraryProperties := module.GetArchVariantProperties(ctx, &LibraryProperties{})
Liz Kammere6583482021-10-19 13:56:10 -0400595
596 var implementationHdrs bazel.LabelListAttribute
597
598 axisToConfigs := map[bazel.ConfigurationAxis]map[string]bool{}
599 allAxesAndConfigs := func(cp android.ConfigurationAxisToArchVariantProperties) {
600 for axis, configMap := range cp {
601 if _, ok := axisToConfigs[axis]; !ok {
602 axisToConfigs[axis] = map[string]bool{}
603 }
604 for config, _ := range configMap {
605 axisToConfigs[axis][config] = true
Chris Parsonsa967f252021-09-23 16:34:35 -0400606 }
607 }
608 }
Liz Kammere6583482021-10-19 13:56:10 -0400609 allAxesAndConfigs(archVariantCompilerProps)
610 allAxesAndConfigs(archVariantLinkerProps)
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000611 allAxesAndConfigs(archVariantLibraryProperties)
Chris Parsonsa967f252021-09-23 16:34:35 -0400612
Liz Kammere6583482021-10-19 13:56:10 -0400613 compilerAttrs := compilerAttributes{}
614 linkerAttrs := linkerAttributes{}
615
616 for axis, configs := range axisToConfigs {
617 for config, _ := range configs {
618 var allHdrs []string
619 if baseCompilerProps, ok := archVariantCompilerProps[axis][config].(*BaseCompilerProperties); ok {
620 allHdrs = baseCompilerProps.Generated_headers
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000621 if baseCompilerProps.Lex != nil {
622 compilerAttrs.lexopts.SetSelectValue(axis, config, baseCompilerProps.Lex.Flags)
623 }
Liz Kammere6583482021-10-19 13:56:10 -0400624 (&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, config, baseCompilerProps)
625 }
626
627 var exportHdrs []string
628
629 if baseLinkerProps, ok := archVariantLinkerProps[axis][config].(*BaseLinkerProperties); ok {
630 exportHdrs = baseLinkerProps.Export_generated_headers
631
632 (&linkerAttrs).bp2buildForAxisAndConfig(ctx, module.Binary(), axis, config, baseLinkerProps)
633 }
634 headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportHdrs, android.BazelLabelForModuleDeps)
635 implementationHdrs.SetSelectValue(axis, config, headers.implementation)
636 compilerAttrs.hdrs.SetSelectValue(axis, config, headers.export)
Liz Kammer1263d9b2021-12-10 14:28:20 -0500637
638 exportIncludes, exportAbsoluteIncludes := includesFromLabelList(headers.export)
639 compilerAttrs.includes.Includes.SetSelectValue(axis, config, exportIncludes)
640 compilerAttrs.includes.AbsoluteIncludes.SetSelectValue(axis, config, exportAbsoluteIncludes)
641
642 includes, absoluteIncludes := includesFromLabelList(headers.implementation)
643 currAbsoluteIncludes := compilerAttrs.absoluteIncludes.SelectValue(axis, config)
644 currAbsoluteIncludes = android.FirstUniqueStrings(append(currAbsoluteIncludes, absoluteIncludes...))
645 compilerAttrs.absoluteIncludes.SetSelectValue(axis, config, currAbsoluteIncludes)
646 currIncludes := compilerAttrs.localIncludes.SelectValue(axis, config)
647 currIncludes = android.FirstUniqueStrings(append(currIncludes, includes...))
648 compilerAttrs.localIncludes.SetSelectValue(axis, config, currIncludes)
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000649
650 if libraryProps, ok := archVariantLibraryProperties[axis][config].(*LibraryProperties); ok {
651 if axis == bazel.NoConfigAxis {
652 compilerAttrs.stubsSymbolFile = libraryProps.Stubs.Symbol_file
653 compilerAttrs.stubsVersions.SetSelectValue(axis, config, libraryProps.Stubs.Versions)
654 }
655 }
Liz Kammere6583482021-10-19 13:56:10 -0400656 }
657 }
Liz Kammere6583482021-10-19 13:56:10 -0400658 compilerAttrs.convertStlProps(ctx, module)
659 (&linkerAttrs).convertStripProps(ctx, module)
660
Yu Liu8d82ac52022-05-17 15:13:28 -0700661 if module.coverage != nil && module.coverage.Properties.Native_coverage != nil &&
662 !Bool(module.coverage.Properties.Native_coverage) {
663 // Native_coverage is arch neutral
664 (&linkerAttrs).features.Append(bazel.MakeStringListAttribute([]string{"-coverage"}))
665 }
666
Liz Kammere6583482021-10-19 13:56:10 -0400667 productVariableProps := android.ProductVariableProperties(ctx)
668
669 (&compilerAttrs).convertProductVariables(ctx, productVariableProps)
670 (&linkerAttrs).convertProductVariables(ctx, productVariableProps)
671
672 (&compilerAttrs).finalize(ctx, implementationHdrs)
Liz Kammer54309532021-12-14 12:21:22 -0500673 (&linkerAttrs).finalize(ctx)
Liz Kammere6583482021-10-19 13:56:10 -0400674
Cole Faust7071a052022-07-29 15:58:33 -0700675 (&compilerAttrs.srcs).Add(bp2BuildYasm(ctx, module, compilerAttrs))
676
Liz Kammer12615db2021-09-28 09:19:17 -0400677 protoDep := bp2buildProto(ctx, module, compilerAttrs.protoSrcs)
678
679 // bp2buildProto will only set wholeStaticLib or implementationWholeStaticLib, but we don't know
680 // which. This will add the newly generated proto library to the appropriate attribute and nothing
681 // to the other
682 (&linkerAttrs).wholeArchiveDeps.Add(protoDep.wholeStaticLib)
683 (&linkerAttrs).implementationWholeArchiveDeps.Add(protoDep.implementationWholeStaticLib)
684
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000685 convertedLSrcs := bp2BuildLex(ctx, module.Name(), compilerAttrs)
686 (&compilerAttrs).srcs.Add(&convertedLSrcs.srcName)
687 (&compilerAttrs).cSrcs.Add(&convertedLSrcs.cSrcName)
688
Liz Kammere6583482021-10-19 13:56:10 -0400689 return baseAttributes{
690 compilerAttrs,
691 linkerAttrs,
Liz Kammer12615db2021-09-28 09:19:17 -0400692 protoDep.protoDep,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000693 }
694}
695
Yu Liufc603162022-03-01 15:44:08 -0800696func bp2BuildParseSdkAttributes(module *Module) sdkAttributes {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000697 return sdkAttributes{
698 Sdk_version: module.Properties.Sdk_version,
Yu Liufc603162022-03-01 15:44:08 -0800699 Min_sdk_version: module.Properties.Min_sdk_version,
700 }
701}
702
703type sdkAttributes struct {
704 Sdk_version *string
705 Min_sdk_version *string
706}
707
Jingwen Chen107c0de2021-04-09 10:43:12 +0000708// Convenience struct to hold all attributes parsed from linker properties.
709type linkerAttributes struct {
Liz Kammer54309532021-12-14 12:21:22 -0500710 deps bazel.LabelListAttribute
711 implementationDeps bazel.LabelListAttribute
712 dynamicDeps bazel.LabelListAttribute
713 implementationDynamicDeps bazel.LabelListAttribute
Cole Faust6b29f592022-08-09 09:50:56 -0700714 runtimeDeps bazel.LabelListAttribute
Liz Kammer54309532021-12-14 12:21:22 -0500715 wholeArchiveDeps bazel.LabelListAttribute
716 implementationWholeArchiveDeps bazel.LabelListAttribute
717 systemDynamicDeps bazel.LabelListAttribute
718 usedSystemDynamicDepAsDynamicDep map[string]bool
Liz Kammer7a210ac2021-09-22 15:52:58 -0400719
Jingwen Chen6ada5892021-09-17 11:38:09 +0000720 linkCrt bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000721 useLibcrt bazel.BoolAttribute
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500722 useVersionLib bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000723 linkopts bazel.StringListAttribute
Liz Kammerd2871182021-10-04 13:54:37 -0400724 additionalLinkerInputs bazel.LabelListAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000725 stripKeepSymbols bazel.BoolAttribute
726 stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
727 stripKeepSymbolsList bazel.StringListAttribute
728 stripAll bazel.BoolAttribute
729 stripNone bazel.BoolAttribute
Liz Kammer0eae52e2021-10-06 10:32:26 -0400730 features bazel.StringListAttribute
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400731}
732
Liz Kammer54309532021-12-14 12:21:22 -0500733var (
734 soongSystemSharedLibs = []string{"libc", "libm", "libdl"}
735)
736
Jingwen Chen55bc8202021-11-02 06:40:51 +0000737func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, isBinary bool, axis bazel.ConfigurationAxis, config string, props *BaseLinkerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400738 // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module
739 var axisFeatures []string
Liz Kammer7a210ac2021-09-22 15:52:58 -0400740
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400741 wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs)
742 la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs))
Liz Kammere6583482021-10-19 13:56:10 -0400743 // Excludes to parallel Soong:
744 // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400745 staticLibs := android.FirstUniqueStrings(android.RemoveListFromList(props.Static_libs, wholeStaticLibs))
746
Liz Kammere6583482021-10-19 13:56:10 -0400747 staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, staticLibs, props.Exclude_static_libs, props.Export_static_lib_headers, bazelLabelForStaticDepsExcludes)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400748
Liz Kammere6583482021-10-19 13:56:10 -0400749 headerLibs := android.FirstUniqueStrings(props.Header_libs)
750 hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps)
Jingwen Chen63930982021-03-24 10:04:33 -0400751
Liz Kammere6583482021-10-19 13:56:10 -0400752 (&hDeps.export).Append(staticDeps.export)
753 la.deps.SetSelectValue(axis, config, hDeps.export)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000754
Liz Kammere6583482021-10-19 13:56:10 -0400755 (&hDeps.implementation).Append(staticDeps.implementation)
756 la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation)
Liz Kammer0eae52e2021-10-06 10:32:26 -0400757
Liz Kammere6583482021-10-19 13:56:10 -0400758 systemSharedLibs := props.System_shared_libs
759 // systemSharedLibs distinguishes between nil/empty list behavior:
760 // nil -> use default values
761 // empty list -> no values specified
762 if len(systemSharedLibs) > 0 {
763 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
764 }
765 la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
766
767 sharedLibs := android.FirstUniqueStrings(props.Shared_libs)
Liz Kammer54309532021-12-14 12:21:22 -0500768 excludeSharedLibs := props.Exclude_shared_libs
769 usedSystem := android.FilterListPred(sharedLibs, func(s string) bool {
770 return android.InList(s, soongSystemSharedLibs) && !android.InList(s, excludeSharedLibs)
771 })
772 for _, el := range usedSystem {
773 if la.usedSystemDynamicDepAsDynamicDep == nil {
774 la.usedSystemDynamicDepAsDynamicDep = map[string]bool{}
775 }
776 la.usedSystemDynamicDepAsDynamicDep[el] = true
777 }
778
Liz Kammere6583482021-10-19 13:56:10 -0400779 sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, sharedLibs, props.Exclude_shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes)
780 la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export)
781 la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation)
Wei Li81852ca2022-07-27 00:22:06 -0700782 if axis == bazel.NoConfigAxis || (axis == bazel.OsConfigurationAxis && config == bazel.OsAndroid) {
783 // If a dependency in la.implementationDynamicDeps has stubs, its stub variant should be
784 // used when the dependency is linked in a APEX. The dependencies in NoConfigAxis and
785 // OsConfigurationAxis/OsAndroid are grouped by having stubs or not, so Bazel select()
786 // statement can be used to choose source/stub variants of them.
787 depsWithStubs := []bazel.Label{}
788 for _, l := range sharedDeps.implementation.Includes {
789 dep, _ := ctx.ModuleFromName(l.OriginalModuleName)
790 if m, ok := dep.(*Module); ok && m.HasStubsVariants() {
791 depsWithStubs = append(depsWithStubs, l)
792 }
793 }
794 if len(depsWithStubs) > 0 {
795 implDynamicDeps := bazel.SubtractBazelLabelList(sharedDeps.implementation, bazel.MakeLabelList(depsWithStubs))
796 la.implementationDynamicDeps.SetSelectValue(axis, config, implDynamicDeps)
797
798 stubLibLabels := []bazel.Label{}
799 for _, l := range depsWithStubs {
800 l.Label = l.Label + "_stub_libs_current"
801 stubLibLabels = append(stubLibLabels, l)
802 }
803 inApexSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex)
804 nonApexSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex)
805 defaultSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey)
806 if axis == bazel.NoConfigAxis {
807 (&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels))
808 (&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
809 (&defaultSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
810 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, inApexSelectValue)
811 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, nonApexSelectValue)
812 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, defaultSelectValue)
813 } else if config == bazel.OsAndroid {
814 (&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels))
815 (&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
816 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, inApexSelectValue)
817 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, nonApexSelectValue)
818 }
819 }
820 }
Liz Kammere6583482021-10-19 13:56:10 -0400821
822 if !BoolDefault(props.Pack_relocations, packRelocationsDefault) {
823 axisFeatures = append(axisFeatures, "disable_pack_relocations")
824 }
825
826 if Bool(props.Allow_undefined_symbols) {
827 axisFeatures = append(axisFeatures, "-no_undefined_symbols")
828 }
829
830 var linkerFlags []string
831 if len(props.Ldflags) > 0 {
Liz Kammerf38a8372022-02-04 15:39:00 -0500832 linkerFlags = append(linkerFlags, proptools.NinjaEscapeList(props.Ldflags)...)
Liz Kammere6583482021-10-19 13:56:10 -0400833 // binaries remove static flag if -shared is in the linker flags
834 if isBinary && android.InList("-shared", linkerFlags) {
835 axisFeatures = append(axisFeatures, "-static_flag")
836 }
837 }
838 if props.Version_script != nil {
839 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script)
840 la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}})
841 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label))
842 }
Alix773adaa2022-04-27 17:49:34 +0000843
844 if props.Dynamic_list != nil {
845 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Dynamic_list)
846 la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}})
847 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--dynamic-list,$(location %s)", label.Label))
848 }
849
Alix1be00d42022-05-16 22:56:04 +0000850 la.linkopts.SetSelectValue(axis, config, parseCommandLineFlags(linkerFlags, false, filterOutClangUnknownCflags))
Liz Kammere6583482021-10-19 13:56:10 -0400851 la.useLibcrt.SetSelectValue(axis, config, props.libCrt())
852
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500853 if axis == bazel.NoConfigAxis {
854 la.useVersionLib.SetSelectValue(axis, config, props.Use_version_lib)
855 }
856
Liz Kammere6583482021-10-19 13:56:10 -0400857 // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it.
858 if props.crt() != nil {
859 if axis == bazel.NoConfigAxis {
860 la.linkCrt.SetSelectValue(axis, config, props.crt())
861 } else if axis == bazel.ArchConfigurationAxis {
862 ctx.ModuleErrorf("nocrt is not supported for arch variants")
863 }
864 }
865
866 if axisFeatures != nil {
867 la.features.SetSelectValue(axis, config, axisFeatures)
868 }
Cole Faust6b29f592022-08-09 09:50:56 -0700869
870 runtimeDeps := android.BazelLabelForModuleDepsExcludes(ctx, props.Runtime_libs, props.Exclude_runtime_libs)
871 if !runtimeDeps.IsEmpty() {
872 la.runtimeDeps.SetSelectValue(axis, config, runtimeDeps)
873 }
Liz Kammere6583482021-10-19 13:56:10 -0400874}
875
Jingwen Chen55bc8202021-11-02 06:40:51 +0000876func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000877 bp2BuildPropParseHelper(ctx, module, &StripProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
878 if stripProperties, ok := props.(*StripProperties); ok {
879 la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols)
880 la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list)
881 la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame)
882 la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All)
883 la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000884 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000885 })
Liz Kammere6583482021-10-19 13:56:10 -0400886}
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000887
Jingwen Chen55bc8202021-11-02 06:40:51 +0000888func (la *linkerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Jingwen Chen6ada5892021-09-17 11:38:09 +0000889
Liz Kammer47535c52021-06-02 16:02:22 -0400890 type productVarDep struct {
891 // the name of the corresponding excludes field, if one exists
892 excludesField string
893 // reference to the bazel attribute that should be set for the given product variable config
894 attribute *bazel.LabelListAttribute
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400895
Jingwen Chen55bc8202021-11-02 06:40:51 +0000896 depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList
Liz Kammer47535c52021-06-02 16:02:22 -0400897 }
898
899 productVarToDepFields := map[string]productVarDep{
900 // product variables do not support exclude_shared_libs
Jingwen Chen55bc8202021-11-02 06:40:51 +0000901 "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes},
902 "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes},
903 "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes},
Liz Kammer47535c52021-06-02 16:02:22 -0400904 }
905
Liz Kammer47535c52021-06-02 16:02:22 -0400906 for name, dep := range productVarToDepFields {
907 props, exists := productVariableProps[name]
908 excludeProps, excludesExists := productVariableProps[dep.excludesField]
909 // if neither an include or excludes property exists, then skip it
910 if !exists && !excludesExists {
911 continue
912 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000913 // Collect all the configurations that an include or exclude property exists for.
914 // We want to iterate all configurations rather than either the include or exclude because, for a
915 // particular configuration, we may have either only an include or an exclude to handle.
916 productConfigProps := make(map[android.ProductConfigProperty]bool, len(props)+len(excludeProps))
917 for p := range props {
918 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -0400919 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000920 for p := range excludeProps {
921 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -0400922 }
923
Jingwen Chen25825ca2021-11-15 12:28:43 +0000924 for productConfigProp := range productConfigProps {
925 prop, includesExists := props[productConfigProp]
926 excludesProp, excludesExists := excludeProps[productConfigProp]
Liz Kammer47535c52021-06-02 16:02:22 -0400927 var includes, excludes []string
928 var ok bool
929 // if there was no includes/excludes property, casting fails and that's expected
Jingwen Chen25825ca2021-11-15 12:28:43 +0000930 if includes, ok = prop.([]string); includesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -0400931 ctx.ModuleErrorf("Could not convert product variable %s property", name)
932 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000933 if excludes, ok = excludesProp.([]string); excludesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -0400934 ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
935 }
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400936
Jingwen Chen58ff6802021-11-17 12:14:41 +0000937 dep.attribute.EmitEmptyList = productConfigProp.AlwaysEmit()
Jingwen Chen25825ca2021-11-15 12:28:43 +0000938 dep.attribute.SetSelectValue(
939 productConfigProp.ConfigurationAxis(),
940 productConfigProp.SelectKey(),
941 dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes),
942 )
Liz Kammer47535c52021-06-02 16:02:22 -0400943 }
944 }
Liz Kammere6583482021-10-19 13:56:10 -0400945}
Liz Kammer47535c52021-06-02 16:02:22 -0400946
Liz Kammer54309532021-12-14 12:21:22 -0500947func (la *linkerAttributes) finalize(ctx android.BazelConversionPathContext) {
948 // if system dynamic deps have the default value, any use of a system dynamic library used will
949 // result in duplicate library errors for bionic OSes. Here, we explicitly exclude those libraries
950 // from bionic OSes.
951 if la.systemDynamicDeps.IsNil() && len(la.usedSystemDynamicDepAsDynamicDep) > 0 {
952 toRemove := bazelLabelForSharedDeps(ctx, android.SortedStringKeys(la.usedSystemDynamicDepAsDynamicDep))
953 la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove)
954 la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove)
955 la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove)
956 la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove)
957 }
958
Liz Kammere6583482021-10-19 13:56:10 -0400959 la.deps.ResolveExcludes()
960 la.implementationDeps.ResolveExcludes()
961 la.dynamicDeps.ResolveExcludes()
962 la.implementationDynamicDeps.ResolveExcludes()
963 la.wholeArchiveDeps.ResolveExcludes()
964 la.systemDynamicDeps.ForceSpecifyEmptyList = true
Liz Kammer54309532021-12-14 12:21:22 -0500965
Jingwen Chen91220d72021-03-24 02:18:33 -0400966}
967
Jingwen Chened9c17d2021-04-13 07:14:55 +0000968// Relativize a list of root-relative paths with respect to the module's
969// directory.
970//
971// include_dirs Soong prop are root-relative (b/183742505), but
972// local_include_dirs, export_include_dirs and export_system_include_dirs are
973// module dir relative. This function makes a list of paths entirely module dir
974// relative.
975//
976// For the `include` attribute, Bazel wants the paths to be relative to the
977// module.
978func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000979 var relativePaths []string
980 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000981 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
982 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
983 if err != nil {
984 panic(err)
985 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000986 relativePaths = append(relativePaths, relativePath)
987 }
988 return relativePaths
989}
990
Liz Kammer5fad5012021-09-09 14:08:21 -0400991// BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel
992// attributes.
993type BazelIncludes struct {
Liz Kammer1263d9b2021-12-10 14:28:20 -0500994 AbsoluteIncludes bazel.StringListAttribute
995 Includes bazel.StringListAttribute
996 SystemIncludes bazel.StringListAttribute
Liz Kammer5fad5012021-09-09 14:08:21 -0400997}
998
Liz Kammer54549442022-05-11 13:55:06 -0400999func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module, includes *BazelIncludes) BazelIncludes {
Liz Kammer1263d9b2021-12-10 14:28:20 -05001000 var exported BazelIncludes
1001 if includes != nil {
1002 exported = *includes
1003 } else {
1004 exported = BazelIncludes{}
1005 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001006 bp2BuildPropParseHelper(ctx, module, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1007 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
1008 if len(flagExporterProperties.Export_include_dirs) > 0 {
1009 exported.Includes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.Includes.SelectValue(axis, config), flagExporterProperties.Export_include_dirs...)))
1010 }
1011 if len(flagExporterProperties.Export_system_include_dirs) > 0 {
1012 exported.SystemIncludes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.SystemIncludes.SelectValue(axis, config), flagExporterProperties.Export_system_include_dirs...)))
Rupert Shuttleworth375451e2021-04-26 07:49:08 -04001013 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -04001014 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001015 })
Liz Kammer1263d9b2021-12-10 14:28:20 -05001016 exported.AbsoluteIncludes.DeduplicateAxesFromBase()
Liz Kammer5fad5012021-09-09 14:08:21 -04001017 exported.Includes.DeduplicateAxesFromBase()
1018 exported.SystemIncludes.DeduplicateAxesFromBase()
Rupert Shuttleworth375451e2021-04-26 07:49:08 -04001019
Liz Kammer5fad5012021-09-09 14:08:21 -04001020 return exported
Jingwen Chen91220d72021-03-24 02:18:33 -04001021}
Chris Parsons953b3562021-09-20 15:14:39 -04001022
Jingwen Chen55bc8202021-11-02 06:40:51 +00001023func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001024 label := android.BazelModuleLabel(ctx, m)
Chris Parsonsad876012022-08-20 14:48:32 -04001025 if ccModule, ok := m.(*Module); ok && ccModule.typ() == fullLibrary && !android.GetBp2BuildAllowList().GenerateCcLibraryStaticOnly(m.Name()) {
Liz Kammer35ca77e2021-12-22 15:31:40 -05001026 label += "_bp2build_cc_library_static"
Chris Parsons953b3562021-09-20 15:14:39 -04001027 }
1028 return label
1029}
1030
Jingwen Chen55bc8202021-11-02 06:40:51 +00001031func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001032 // cc_library, at it's root name, propagates the shared library, which depends on the static
1033 // library.
1034 return android.BazelModuleLabel(ctx, m)
1035}
1036
Jingwen Chen55bc8202021-11-02 06:40:51 +00001037func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001038 label := bazelLabelForStaticModule(ctx, m)
1039 if aModule, ok := m.(android.Module); ok {
1040 if android.IsModulePrebuilt(aModule) {
1041 label += "_alwayslink"
1042 }
1043 }
1044 return label
1045}
1046
Jingwen Chen55bc8202021-11-02 06:40:51 +00001047func bazelLabelForWholeDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001048 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps)
1049}
1050
Jingwen Chen55bc8202021-11-02 06:40:51 +00001051func bazelLabelForWholeDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001052 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps)
1053}
1054
Jingwen Chen55bc8202021-11-02 06:40:51 +00001055func bazelLabelForStaticDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001056 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule)
1057}
1058
Jingwen Chen55bc8202021-11-02 06:40:51 +00001059func bazelLabelForStaticDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001060 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule)
1061}
1062
Jingwen Chen55bc8202021-11-02 06:40:51 +00001063func bazelLabelForSharedDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001064 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule)
1065}
1066
Jingwen Chen55bc8202021-11-02 06:40:51 +00001067func bazelLabelForHeaderDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001068 // This is not elegant, but bp2build's shared library targets only propagate
1069 // their header information as part of the normal C++ provider.
1070 return bazelLabelForSharedDeps(ctx, modules)
1071}
1072
Jingwen Chen55bc8202021-11-02 06:40:51 +00001073func bazelLabelForSharedDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001074 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
1075}
Liz Kammer2b8004b2021-10-04 13:55:44 -04001076
1077type binaryLinkerAttrs struct {
1078 Linkshared *bool
1079}
1080
Jingwen Chen55bc8202021-11-02 06:40:51 +00001081func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs {
Liz Kammer2b8004b2021-10-04 13:55:44 -04001082 attrs := binaryLinkerAttrs{}
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001083 bp2BuildPropParseHelper(ctx, m, &BinaryLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1084 linkerProps := props.(*BinaryLinkerProperties)
1085 staticExecutable := linkerProps.Static_executable
1086 if axis == bazel.NoConfigAxis {
1087 if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared {
1088 attrs.Linkshared = &linkBinaryShared
Liz Kammer2b8004b2021-10-04 13:55:44 -04001089 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001090 } else if staticExecutable != nil {
1091 // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a
1092 // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling
1093 ctx.ModuleErrorf("bp2build cannot migrate a module with arch/target-specific static_executable values")
Liz Kammer2b8004b2021-10-04 13:55:44 -04001094 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001095 })
Liz Kammer2b8004b2021-10-04 13:55:44 -04001096
1097 return attrs
1098}