blob: 3fe0802e26d55f8a35270f8cd6c4c9fcf7b89138 [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
Cole Faust5fa4e962022-08-22 14:31:04 -0700285 // A combination of compilerAttributes.features and linkerAttributes.features
286 features bazel.StringListAttribute
Liz Kammer12615db2021-09-28 09:19:17 -0400287 protoDependency *bazel.LabelAttribute
Liz Kammere6583482021-10-19 13:56:10 -0400288}
289
Jingwen Chen107c0de2021-04-09 10:43:12 +0000290// Convenience struct to hold all attributes parsed from compiler properties.
291type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400292 // Options for all languages
293 copts bazel.StringListAttribute
294 // Assembly options and sources
295 asFlags bazel.StringListAttribute
296 asSrcs bazel.LabelListAttribute
Cole Faust7071a052022-07-29 15:58:33 -0700297 asmSrcs bazel.LabelListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400298 // C options and sources
299 conlyFlags bazel.StringListAttribute
300 cSrcs bazel.LabelListAttribute
301 // C++ options and sources
302 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000303 srcs bazel.LabelListAttribute
Chris Parsons2c788392021-08-10 11:58:07 -0400304
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000305 // Lex sources and options
306 lSrcs bazel.LabelListAttribute
307 llSrcs bazel.LabelListAttribute
308 lexopts bazel.StringListAttribute
309
Liz Kammere6583482021-10-19 13:56:10 -0400310 hdrs bazel.LabelListAttribute
311
Chris Parsons2c788392021-08-10 11:58:07 -0400312 rtti bazel.BoolAttribute
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000313
314 // Not affected by arch variants
315 stl *string
Chris Parsons79bd2b72021-11-29 17:52:41 -0500316 cStd *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000317 cppStd *string
Liz Kammer35687bc2021-09-10 10:07:07 -0400318
319 localIncludes bazel.StringListAttribute
320 absoluteIncludes bazel.StringListAttribute
Liz Kammer12615db2021-09-28 09:19:17 -0400321
Liz Kammer1263d9b2021-12-10 14:28:20 -0500322 includes BazelIncludes
323
Liz Kammer12615db2021-09-28 09:19:17 -0400324 protoSrcs bazel.LabelListAttribute
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000325
326 stubsSymbolFile *string
327 stubsVersions bazel.StringListAttribute
Cole Faust5fa4e962022-08-22 14:31:04 -0700328
329 features bazel.StringListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000330}
331
Liz Kammercac7f692021-12-16 14:19:32 -0500332type filterOutFn func(string) bool
333
334func filterOutStdFlag(flag string) bool {
335 return strings.HasPrefix(flag, "-std=")
336}
337
Alix1be00d42022-05-16 22:56:04 +0000338func filterOutClangUnknownCflags(flag string) bool {
339 for _, f := range config.ClangUnknownCflags {
340 if f == flag {
341 return true
342 }
343 }
344 return false
345}
346
347func parseCommandLineFlags(soongFlags []string, noCoptsTokenization bool, filterOut ...filterOutFn) []string {
Liz Kammere6583482021-10-19 13:56:10 -0400348 var result []string
349 for _, flag := range soongFlags {
Alix1be00d42022-05-16 22:56:04 +0000350 skipFlag := false
351 for _, filter := range filterOut {
352 if filter != nil && filter(flag) {
353 skipFlag = true
354 }
355 }
356 if skipFlag {
Liz Kammercac7f692021-12-16 14:19:32 -0500357 continue
358 }
Liz Kammere6583482021-10-19 13:56:10 -0400359 // Soong's cflags can contain spaces, like `-include header.h`. For
360 // Bazel's copts, split them up to be compatible with the
361 // no_copts_tokenization feature.
Alix1be00d42022-05-16 22:56:04 +0000362 if noCoptsTokenization {
363 result = append(result, strings.Split(flag, " ")...)
364 } else {
365 // Soong's Version Script and Dynamic List Properties are added as flags
366 // to Bazel's linkopts using "($location label)" syntax.
367 // Splitting on spaces would separate this into two different flags
368 // "($ location" and "label)"
369 result = append(result, flag)
370 }
Liz Kammere6583482021-10-19 13:56:10 -0400371 }
372 return result
373}
Jingwen Chened9c17d2021-04-13 07:14:55 +0000374
Jingwen Chen55bc8202021-11-02 06:40:51 +0000375func (ca *compilerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, props *BaseCompilerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400376 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
377 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
378 if srcsList, ok := parseSrcs(ctx, props); ok {
379 ca.srcs.SetSelectValue(axis, config, srcsList)
Chris Parsons990c4f42021-05-25 12:10:58 -0400380 }
381
Liz Kammere6583482021-10-19 13:56:10 -0400382 localIncludeDirs := props.Local_include_dirs
383 if axis == bazel.NoConfigAxis {
Chris Parsons79bd2b72021-11-29 17:52:41 -0500384 ca.cStd, ca.cppStd = bp2buildResolveCppStdValue(props.C_std, props.Cpp_std, props.Gnu_extensions)
Liz Kammere6583482021-10-19 13:56:10 -0400385 if includeBuildDirectory(props.Include_build_directory) {
386 localIncludeDirs = append(localIncludeDirs, ".")
Liz Kammer222bdcf2021-10-11 14:15:51 -0400387 }
Jingwen Chene32e9e02021-04-23 09:17:24 +0000388 }
389
Liz Kammere6583482021-10-19 13:56:10 -0400390 ca.absoluteIncludes.SetSelectValue(axis, config, props.Include_dirs)
391 ca.localIncludes.SetSelectValue(axis, config, localIncludeDirs)
392
Cole Faust5fa4e962022-08-22 14:31:04 -0700393 instructionSet := proptools.StringDefault(props.Instruction_set, "")
394 if instructionSet == "arm" {
395 ca.features.SetSelectValue(axis, config, []string{"arm_isa_arm", "-arm_isa_thumb"})
396 } else if instructionSet != "" && instructionSet != "thumb" {
397 ctx.ModuleErrorf("Unknown value for instruction_set: %s", instructionSet)
398 }
399
Liz Kammercac7f692021-12-16 14:19:32 -0500400 // In Soong, cflags occur on the command line before -std=<val> flag, resulting in the value being
401 // overridden. In Bazel we always allow overriding, via flags; however, this can cause
402 // incompatibilities, so we remove "-std=" flags from Cflag properties while leaving it in other
403 // cases.
Alix1be00d42022-05-16 22:56:04 +0000404 ca.copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, true, filterOutStdFlag, filterOutClangUnknownCflags))
405 ca.asFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Asflags, true, nil))
406 ca.conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Conlyflags, true, filterOutClangUnknownCflags))
407 ca.cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Cppflags, true, filterOutClangUnknownCflags))
Liz Kammere6583482021-10-19 13:56:10 -0400408 ca.rtti.SetSelectValue(axis, config, props.Rtti)
409}
410
Jingwen Chen55bc8202021-11-02 06:40:51 +0000411func (ca *compilerAttributes) convertStlProps(ctx android.ArchVariantContext, module *Module) {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000412 bp2BuildPropParseHelper(ctx, module, &StlProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
413 if stlProps, ok := props.(*StlProperties); ok {
414 if stlProps.Stl == nil {
415 return
416 }
417 if ca.stl == nil {
Liz Kammer7128d382022-05-12 11:42:33 -0400418 stl := deduplicateStlInput(*stlProps.Stl)
419 ca.stl = &stl
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000420 } else if ca.stl != stlProps.Stl {
421 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 -0400422 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000423 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000424 })
Liz Kammere6583482021-10-19 13:56:10 -0400425}
Jingwen Chenc1c26502021-04-05 10:35:13 +0000426
Jingwen Chen55bc8202021-11-02 06:40:51 +0000427func (ca *compilerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Liz Kammerba7a9c52021-05-26 08:45:30 -0400428 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
Liz Kammere6583482021-10-19 13:56:10 -0400429 "Cflags": &ca.copts,
430 "Asflags": &ca.asFlags,
431 "CppFlags": &ca.cppFlags,
Liz Kammerba7a9c52021-05-26 08:45:30 -0400432 }
Liz Kammerba7a9c52021-05-26 08:45:30 -0400433 for propName, attr := range productVarPropNameToAttribute {
Jingwen Chen25825ca2021-11-15 12:28:43 +0000434 if productConfigProps, exists := productVariableProps[propName]; exists {
435 for productConfigProp, prop := range productConfigProps {
436 flags, ok := prop.([]string)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400437 if !ok {
438 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
439 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000440 newFlags, _ := bazel.TryVariableSubstitutions(flags, productConfigProp.Name)
441 attr.SetSelectValue(productConfigProp.ConfigurationAxis(), productConfigProp.SelectKey(), newFlags)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400442 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400443 }
444 }
Liz Kammere6583482021-10-19 13:56:10 -0400445}
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400446
Jingwen Chen55bc8202021-11-02 06:40:51 +0000447func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, implementationHdrs bazel.LabelListAttribute) {
Liz Kammere6583482021-10-19 13:56:10 -0400448 ca.srcs.ResolveExcludes()
449 partitionedSrcs := groupSrcsByExtension(ctx, ca.srcs)
450
Liz Kammer12615db2021-09-28 09:19:17 -0400451 ca.protoSrcs = partitionedSrcs[protoSrcPartition]
452
Liz Kammere6583482021-10-19 13:56:10 -0400453 for p, lla := range partitionedSrcs {
454 // if there are no sources, there is no need for headers
455 if lla.IsEmpty() {
456 continue
457 }
458 lla.Append(implementationHdrs)
459 partitionedSrcs[p] = lla
460 }
461
462 ca.srcs = partitionedSrcs[cppSrcPartition]
463 ca.cSrcs = partitionedSrcs[cSrcPartition]
464 ca.asSrcs = partitionedSrcs[asSrcPartition]
Cole Faust7071a052022-07-29 15:58:33 -0700465 ca.asmSrcs = partitionedSrcs[asmSrcPartition]
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000466 ca.lSrcs = partitionedSrcs[lSrcPartition]
467 ca.llSrcs = partitionedSrcs[llSrcPartition]
Liz Kammere6583482021-10-19 13:56:10 -0400468
469 ca.absoluteIncludes.DeduplicateAxesFromBase()
470 ca.localIncludes.DeduplicateAxesFromBase()
471}
472
473// Parse srcs from an arch or OS's props value.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000474func parseSrcs(ctx android.BazelConversionPathContext, props *BaseCompilerProperties) (bazel.LabelList, bool) {
Liz Kammere6583482021-10-19 13:56:10 -0400475 anySrcs := false
476 // Add srcs-like dependencies such as generated files.
477 // First create a LabelList containing these dependencies, then merge the values with srcs.
478 generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources)
479 if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 {
480 anySrcs = true
481 }
482
483 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs)
484 if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 {
485 anySrcs = true
486 }
487 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs
488}
489
Liz Kammera5a29de2022-05-25 23:19:37 -0400490func bp2buildStdVal(std *string, prefix string, useGnu bool) *string {
491 defaultVal := prefix + "_std_default"
Chris Parsons79bd2b72021-11-29 17:52:41 -0500492 // If c{,pp}std properties are not specified, don't generate them in the BUILD file.
493 // Defaults are handled by the toolchain definition.
494 // However, if gnu_extensions is false, then the default gnu-to-c version must be specified.
Liz Kammera5a29de2022-05-25 23:19:37 -0400495 stdVal := proptools.StringDefault(std, defaultVal)
496 if stdVal == "experimental" || stdVal == defaultVal {
497 if stdVal == "experimental" {
498 stdVal = prefix + "_std_experimental"
499 }
500 if !useGnu {
501 stdVal += "_no_gnu"
502 }
503 } else if !useGnu {
504 stdVal = gnuToCReplacer.Replace(stdVal)
Chris Parsons79bd2b72021-11-29 17:52:41 -0500505 }
506
Liz Kammera5a29de2022-05-25 23:19:37 -0400507 if stdVal == defaultVal {
508 return nil
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500509 }
Liz Kammera5a29de2022-05-25 23:19:37 -0400510 return &stdVal
511}
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500512
Liz Kammera5a29de2022-05-25 23:19:37 -0400513func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) {
514 useGnu := useGnuExtensions(gnu_extensions)
515
516 return bp2buildStdVal(c_std, "c", useGnu), bp2buildStdVal(cpp_std, "cpp", useGnu)
Liz Kammere6583482021-10-19 13:56:10 -0400517}
518
Liz Kammer1263d9b2021-12-10 14:28:20 -0500519// packageFromLabel extracts package from a fully-qualified or relative Label and whether the label
520// is fully-qualified.
521// e.g. fully-qualified "//a/b:foo" -> "a/b", true, relative: ":bar" -> ".", false
522func packageFromLabel(label string) (string, bool) {
523 split := strings.Split(label, ":")
524 if len(split) != 2 {
525 return "", false
526 }
527 if split[0] == "" {
528 return ".", false
529 }
530 // remove leading "//"
531 return split[0][2:], true
532}
533
534// includesFromLabelList extracts relative/absolute includes from a bazel.LabelList>
535func includesFromLabelList(labelList bazel.LabelList) (relative, absolute []string) {
536 for _, hdr := range labelList.Includes {
537 if pkg, hasPkg := packageFromLabel(hdr.Label); hasPkg {
538 absolute = append(absolute, pkg)
539 } else if pkg != "" {
540 relative = append(relative, pkg)
541 }
542 }
543 return relative, absolute
544}
545
Cole Faust7071a052022-07-29 15:58:33 -0700546type YasmAttributes struct {
547 Srcs bazel.LabelListAttribute
548 Flags bazel.StringListAttribute
549 Include_dirs bazel.StringListAttribute
550}
551
552func bp2BuildYasm(ctx android.Bp2buildMutatorContext, m *Module, ca compilerAttributes) *bazel.LabelAttribute {
553 if ca.asmSrcs.IsEmpty() {
554 return nil
555 }
556
557 // Yasm needs the include directories from both local_includes and
558 // export_include_dirs. We don't care about actually exporting them from the
559 // yasm rule though, because they will also be present on the cc_ rule that
560 // wraps this yasm rule.
561 includes := ca.localIncludes.Clone()
562 bp2BuildPropParseHelper(ctx, m, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
563 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
564 if len(flagExporterProperties.Export_include_dirs) > 0 {
565 x := bazel.StringListAttribute{}
566 x.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs)
567 includes.Append(x)
568 }
569 }
570 })
571
572 ctx.CreateBazelTargetModule(
573 bazel.BazelTargetModuleProperties{
574 Rule_class: "yasm",
575 Bzl_load_location: "//build/bazel/rules/cc:yasm.bzl",
576 },
577 android.CommonAttributes{Name: m.Name() + "_yasm"},
578 &YasmAttributes{
579 Srcs: ca.asmSrcs,
580 Flags: ca.asFlags,
581 Include_dirs: *includes,
582 })
583
584 // We only want to add a dependency on the _yasm target if there are asm
585 // sources in the current configuration. If there are unconfigured asm
586 // sources, always add the dependency. Otherwise, add the dependency only
587 // on the configuration axes and values that had asm sources.
588 if len(ca.asmSrcs.Value.Includes) > 0 {
589 return bazel.MakeLabelAttribute(":" + m.Name() + "_yasm")
590 }
591
592 ret := &bazel.LabelAttribute{}
593 for _, axis := range ca.asmSrcs.SortedConfigurationAxes() {
594 for config := range ca.asmSrcs.ConfigurableValues[axis] {
595 ret.SetSelectValue(axis, config, bazel.Label{Label: ":" + m.Name() + "_yasm"})
596 }
597 }
598 return ret
599}
600
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000601// bp2BuildParseBaseProps returns all compiler, linker, library attributes of a cc module..
Liz Kammer12615db2021-09-28 09:19:17 -0400602func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) baseAttributes {
Liz Kammere6583482021-10-19 13:56:10 -0400603 archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
604 archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{})
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000605 archVariantLibraryProperties := module.GetArchVariantProperties(ctx, &LibraryProperties{})
Liz Kammere6583482021-10-19 13:56:10 -0400606
607 var implementationHdrs bazel.LabelListAttribute
608
609 axisToConfigs := map[bazel.ConfigurationAxis]map[string]bool{}
610 allAxesAndConfigs := func(cp android.ConfigurationAxisToArchVariantProperties) {
611 for axis, configMap := range cp {
612 if _, ok := axisToConfigs[axis]; !ok {
613 axisToConfigs[axis] = map[string]bool{}
614 }
615 for config, _ := range configMap {
616 axisToConfigs[axis][config] = true
Chris Parsonsa967f252021-09-23 16:34:35 -0400617 }
618 }
619 }
Liz Kammere6583482021-10-19 13:56:10 -0400620 allAxesAndConfigs(archVariantCompilerProps)
621 allAxesAndConfigs(archVariantLinkerProps)
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000622 allAxesAndConfigs(archVariantLibraryProperties)
Chris Parsonsa967f252021-09-23 16:34:35 -0400623
Liz Kammere6583482021-10-19 13:56:10 -0400624 compilerAttrs := compilerAttributes{}
625 linkerAttrs := linkerAttributes{}
626
627 for axis, configs := range axisToConfigs {
628 for config, _ := range configs {
629 var allHdrs []string
630 if baseCompilerProps, ok := archVariantCompilerProps[axis][config].(*BaseCompilerProperties); ok {
631 allHdrs = baseCompilerProps.Generated_headers
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000632 if baseCompilerProps.Lex != nil {
633 compilerAttrs.lexopts.SetSelectValue(axis, config, baseCompilerProps.Lex.Flags)
634 }
Liz Kammere6583482021-10-19 13:56:10 -0400635 (&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, config, baseCompilerProps)
636 }
637
638 var exportHdrs []string
639
640 if baseLinkerProps, ok := archVariantLinkerProps[axis][config].(*BaseLinkerProperties); ok {
641 exportHdrs = baseLinkerProps.Export_generated_headers
642
643 (&linkerAttrs).bp2buildForAxisAndConfig(ctx, module.Binary(), axis, config, baseLinkerProps)
644 }
645 headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportHdrs, android.BazelLabelForModuleDeps)
646 implementationHdrs.SetSelectValue(axis, config, headers.implementation)
647 compilerAttrs.hdrs.SetSelectValue(axis, config, headers.export)
Liz Kammer1263d9b2021-12-10 14:28:20 -0500648
649 exportIncludes, exportAbsoluteIncludes := includesFromLabelList(headers.export)
650 compilerAttrs.includes.Includes.SetSelectValue(axis, config, exportIncludes)
651 compilerAttrs.includes.AbsoluteIncludes.SetSelectValue(axis, config, exportAbsoluteIncludes)
652
653 includes, absoluteIncludes := includesFromLabelList(headers.implementation)
654 currAbsoluteIncludes := compilerAttrs.absoluteIncludes.SelectValue(axis, config)
655 currAbsoluteIncludes = android.FirstUniqueStrings(append(currAbsoluteIncludes, absoluteIncludes...))
656 compilerAttrs.absoluteIncludes.SetSelectValue(axis, config, currAbsoluteIncludes)
657 currIncludes := compilerAttrs.localIncludes.SelectValue(axis, config)
658 currIncludes = android.FirstUniqueStrings(append(currIncludes, includes...))
659 compilerAttrs.localIncludes.SetSelectValue(axis, config, currIncludes)
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000660
661 if libraryProps, ok := archVariantLibraryProperties[axis][config].(*LibraryProperties); ok {
662 if axis == bazel.NoConfigAxis {
663 compilerAttrs.stubsSymbolFile = libraryProps.Stubs.Symbol_file
664 compilerAttrs.stubsVersions.SetSelectValue(axis, config, libraryProps.Stubs.Versions)
665 }
666 }
Liz Kammere6583482021-10-19 13:56:10 -0400667 }
668 }
Liz Kammere6583482021-10-19 13:56:10 -0400669 compilerAttrs.convertStlProps(ctx, module)
670 (&linkerAttrs).convertStripProps(ctx, module)
671
Yu Liu8d82ac52022-05-17 15:13:28 -0700672 if module.coverage != nil && module.coverage.Properties.Native_coverage != nil &&
673 !Bool(module.coverage.Properties.Native_coverage) {
674 // Native_coverage is arch neutral
675 (&linkerAttrs).features.Append(bazel.MakeStringListAttribute([]string{"-coverage"}))
676 }
677
Liz Kammere6583482021-10-19 13:56:10 -0400678 productVariableProps := android.ProductVariableProperties(ctx)
679
680 (&compilerAttrs).convertProductVariables(ctx, productVariableProps)
681 (&linkerAttrs).convertProductVariables(ctx, productVariableProps)
682
683 (&compilerAttrs).finalize(ctx, implementationHdrs)
Liz Kammer54309532021-12-14 12:21:22 -0500684 (&linkerAttrs).finalize(ctx)
Liz Kammere6583482021-10-19 13:56:10 -0400685
Cole Faust7071a052022-07-29 15:58:33 -0700686 (&compilerAttrs.srcs).Add(bp2BuildYasm(ctx, module, compilerAttrs))
687
Liz Kammer12615db2021-09-28 09:19:17 -0400688 protoDep := bp2buildProto(ctx, module, compilerAttrs.protoSrcs)
689
690 // bp2buildProto will only set wholeStaticLib or implementationWholeStaticLib, but we don't know
691 // which. This will add the newly generated proto library to the appropriate attribute and nothing
692 // to the other
693 (&linkerAttrs).wholeArchiveDeps.Add(protoDep.wholeStaticLib)
694 (&linkerAttrs).implementationWholeArchiveDeps.Add(protoDep.implementationWholeStaticLib)
695
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000696 convertedLSrcs := bp2BuildLex(ctx, module.Name(), compilerAttrs)
697 (&compilerAttrs).srcs.Add(&convertedLSrcs.srcName)
698 (&compilerAttrs).cSrcs.Add(&convertedLSrcs.cSrcName)
699
Cole Faust5fa4e962022-08-22 14:31:04 -0700700 features := compilerAttrs.features.Clone().Append(linkerAttrs.features)
701 features.DeduplicateAxesFromBase()
702
Liz Kammere6583482021-10-19 13:56:10 -0400703 return baseAttributes{
704 compilerAttrs,
705 linkerAttrs,
Cole Faust5fa4e962022-08-22 14:31:04 -0700706 *features,
Liz Kammer12615db2021-09-28 09:19:17 -0400707 protoDep.protoDep,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000708 }
709}
710
Yu Liufc603162022-03-01 15:44:08 -0800711func bp2BuildParseSdkAttributes(module *Module) sdkAttributes {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000712 return sdkAttributes{
713 Sdk_version: module.Properties.Sdk_version,
Yu Liufc603162022-03-01 15:44:08 -0800714 Min_sdk_version: module.Properties.Min_sdk_version,
715 }
716}
717
718type sdkAttributes struct {
719 Sdk_version *string
720 Min_sdk_version *string
721}
722
Jingwen Chen107c0de2021-04-09 10:43:12 +0000723// Convenience struct to hold all attributes parsed from linker properties.
724type linkerAttributes struct {
Liz Kammer54309532021-12-14 12:21:22 -0500725 deps bazel.LabelListAttribute
726 implementationDeps bazel.LabelListAttribute
727 dynamicDeps bazel.LabelListAttribute
728 implementationDynamicDeps bazel.LabelListAttribute
Cole Faust6b29f592022-08-09 09:50:56 -0700729 runtimeDeps bazel.LabelListAttribute
Liz Kammer54309532021-12-14 12:21:22 -0500730 wholeArchiveDeps bazel.LabelListAttribute
731 implementationWholeArchiveDeps bazel.LabelListAttribute
732 systemDynamicDeps bazel.LabelListAttribute
733 usedSystemDynamicDepAsDynamicDep map[string]bool
Liz Kammer7a210ac2021-09-22 15:52:58 -0400734
Jingwen Chen6ada5892021-09-17 11:38:09 +0000735 linkCrt bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000736 useLibcrt bazel.BoolAttribute
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500737 useVersionLib bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000738 linkopts bazel.StringListAttribute
Liz Kammerd2871182021-10-04 13:54:37 -0400739 additionalLinkerInputs bazel.LabelListAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000740 stripKeepSymbols bazel.BoolAttribute
741 stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
742 stripKeepSymbolsList bazel.StringListAttribute
743 stripAll bazel.BoolAttribute
744 stripNone bazel.BoolAttribute
Liz Kammer0eae52e2021-10-06 10:32:26 -0400745 features bazel.StringListAttribute
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400746}
747
Liz Kammer54309532021-12-14 12:21:22 -0500748var (
749 soongSystemSharedLibs = []string{"libc", "libm", "libdl"}
750)
751
Jingwen Chen55bc8202021-11-02 06:40:51 +0000752func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, isBinary bool, axis bazel.ConfigurationAxis, config string, props *BaseLinkerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400753 // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module
754 var axisFeatures []string
Liz Kammer7a210ac2021-09-22 15:52:58 -0400755
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400756 wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs)
757 la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs))
Liz Kammere6583482021-10-19 13:56:10 -0400758 // Excludes to parallel Soong:
759 // 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 -0400760 staticLibs := android.FirstUniqueStrings(android.RemoveListFromList(props.Static_libs, wholeStaticLibs))
761
Liz Kammere6583482021-10-19 13:56:10 -0400762 staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, staticLibs, props.Exclude_static_libs, props.Export_static_lib_headers, bazelLabelForStaticDepsExcludes)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400763
Liz Kammere6583482021-10-19 13:56:10 -0400764 headerLibs := android.FirstUniqueStrings(props.Header_libs)
765 hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps)
Jingwen Chen63930982021-03-24 10:04:33 -0400766
Liz Kammere6583482021-10-19 13:56:10 -0400767 (&hDeps.export).Append(staticDeps.export)
768 la.deps.SetSelectValue(axis, config, hDeps.export)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000769
Liz Kammere6583482021-10-19 13:56:10 -0400770 (&hDeps.implementation).Append(staticDeps.implementation)
771 la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation)
Liz Kammer0eae52e2021-10-06 10:32:26 -0400772
Liz Kammere6583482021-10-19 13:56:10 -0400773 systemSharedLibs := props.System_shared_libs
774 // systemSharedLibs distinguishes between nil/empty list behavior:
775 // nil -> use default values
776 // empty list -> no values specified
777 if len(systemSharedLibs) > 0 {
778 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
779 }
780 la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
781
782 sharedLibs := android.FirstUniqueStrings(props.Shared_libs)
Liz Kammer54309532021-12-14 12:21:22 -0500783 excludeSharedLibs := props.Exclude_shared_libs
784 usedSystem := android.FilterListPred(sharedLibs, func(s string) bool {
785 return android.InList(s, soongSystemSharedLibs) && !android.InList(s, excludeSharedLibs)
786 })
787 for _, el := range usedSystem {
788 if la.usedSystemDynamicDepAsDynamicDep == nil {
789 la.usedSystemDynamicDepAsDynamicDep = map[string]bool{}
790 }
791 la.usedSystemDynamicDepAsDynamicDep[el] = true
792 }
793
Liz Kammere6583482021-10-19 13:56:10 -0400794 sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, sharedLibs, props.Exclude_shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes)
795 la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export)
796 la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation)
Wei Li81852ca2022-07-27 00:22:06 -0700797 if axis == bazel.NoConfigAxis || (axis == bazel.OsConfigurationAxis && config == bazel.OsAndroid) {
798 // If a dependency in la.implementationDynamicDeps has stubs, its stub variant should be
799 // used when the dependency is linked in a APEX. The dependencies in NoConfigAxis and
800 // OsConfigurationAxis/OsAndroid are grouped by having stubs or not, so Bazel select()
801 // statement can be used to choose source/stub variants of them.
802 depsWithStubs := []bazel.Label{}
803 for _, l := range sharedDeps.implementation.Includes {
804 dep, _ := ctx.ModuleFromName(l.OriginalModuleName)
805 if m, ok := dep.(*Module); ok && m.HasStubsVariants() {
806 depsWithStubs = append(depsWithStubs, l)
807 }
808 }
809 if len(depsWithStubs) > 0 {
810 implDynamicDeps := bazel.SubtractBazelLabelList(sharedDeps.implementation, bazel.MakeLabelList(depsWithStubs))
811 la.implementationDynamicDeps.SetSelectValue(axis, config, implDynamicDeps)
812
813 stubLibLabels := []bazel.Label{}
814 for _, l := range depsWithStubs {
815 l.Label = l.Label + "_stub_libs_current"
816 stubLibLabels = append(stubLibLabels, l)
817 }
818 inApexSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex)
819 nonApexSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex)
820 defaultSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey)
821 if axis == bazel.NoConfigAxis {
822 (&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels))
823 (&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
824 (&defaultSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
825 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, inApexSelectValue)
826 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, nonApexSelectValue)
827 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, defaultSelectValue)
828 } else if config == bazel.OsAndroid {
829 (&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels))
830 (&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
831 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, inApexSelectValue)
832 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, nonApexSelectValue)
833 }
834 }
835 }
Liz Kammere6583482021-10-19 13:56:10 -0400836
837 if !BoolDefault(props.Pack_relocations, packRelocationsDefault) {
838 axisFeatures = append(axisFeatures, "disable_pack_relocations")
839 }
840
841 if Bool(props.Allow_undefined_symbols) {
842 axisFeatures = append(axisFeatures, "-no_undefined_symbols")
843 }
844
845 var linkerFlags []string
846 if len(props.Ldflags) > 0 {
Liz Kammerf38a8372022-02-04 15:39:00 -0500847 linkerFlags = append(linkerFlags, proptools.NinjaEscapeList(props.Ldflags)...)
Liz Kammere6583482021-10-19 13:56:10 -0400848 // binaries remove static flag if -shared is in the linker flags
849 if isBinary && android.InList("-shared", linkerFlags) {
850 axisFeatures = append(axisFeatures, "-static_flag")
851 }
852 }
853 if props.Version_script != nil {
854 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script)
855 la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}})
856 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label))
857 }
Alix773adaa2022-04-27 17:49:34 +0000858
859 if props.Dynamic_list != nil {
860 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Dynamic_list)
861 la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}})
862 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--dynamic-list,$(location %s)", label.Label))
863 }
864
Alix1be00d42022-05-16 22:56:04 +0000865 la.linkopts.SetSelectValue(axis, config, parseCommandLineFlags(linkerFlags, false, filterOutClangUnknownCflags))
Liz Kammere6583482021-10-19 13:56:10 -0400866 la.useLibcrt.SetSelectValue(axis, config, props.libCrt())
867
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500868 if axis == bazel.NoConfigAxis {
869 la.useVersionLib.SetSelectValue(axis, config, props.Use_version_lib)
870 }
871
Liz Kammere6583482021-10-19 13:56:10 -0400872 // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it.
873 if props.crt() != nil {
874 if axis == bazel.NoConfigAxis {
875 la.linkCrt.SetSelectValue(axis, config, props.crt())
876 } else if axis == bazel.ArchConfigurationAxis {
877 ctx.ModuleErrorf("nocrt is not supported for arch variants")
878 }
879 }
880
881 if axisFeatures != nil {
882 la.features.SetSelectValue(axis, config, axisFeatures)
883 }
Cole Faust6b29f592022-08-09 09:50:56 -0700884
885 runtimeDeps := android.BazelLabelForModuleDepsExcludes(ctx, props.Runtime_libs, props.Exclude_runtime_libs)
886 if !runtimeDeps.IsEmpty() {
887 la.runtimeDeps.SetSelectValue(axis, config, runtimeDeps)
888 }
Liz Kammere6583482021-10-19 13:56:10 -0400889}
890
Jingwen Chen55bc8202021-11-02 06:40:51 +0000891func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000892 bp2BuildPropParseHelper(ctx, module, &StripProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
893 if stripProperties, ok := props.(*StripProperties); ok {
894 la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols)
895 la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list)
896 la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame)
897 la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All)
898 la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000899 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000900 })
Liz Kammere6583482021-10-19 13:56:10 -0400901}
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000902
Jingwen Chen55bc8202021-11-02 06:40:51 +0000903func (la *linkerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Jingwen Chen6ada5892021-09-17 11:38:09 +0000904
Liz Kammer47535c52021-06-02 16:02:22 -0400905 type productVarDep struct {
906 // the name of the corresponding excludes field, if one exists
907 excludesField string
908 // reference to the bazel attribute that should be set for the given product variable config
909 attribute *bazel.LabelListAttribute
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400910
Jingwen Chen55bc8202021-11-02 06:40:51 +0000911 depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList
Liz Kammer47535c52021-06-02 16:02:22 -0400912 }
913
914 productVarToDepFields := map[string]productVarDep{
915 // product variables do not support exclude_shared_libs
Jingwen Chen55bc8202021-11-02 06:40:51 +0000916 "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes},
917 "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes},
918 "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes},
Liz Kammer47535c52021-06-02 16:02:22 -0400919 }
920
Liz Kammer47535c52021-06-02 16:02:22 -0400921 for name, dep := range productVarToDepFields {
922 props, exists := productVariableProps[name]
923 excludeProps, excludesExists := productVariableProps[dep.excludesField]
924 // if neither an include or excludes property exists, then skip it
925 if !exists && !excludesExists {
926 continue
927 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000928 // Collect all the configurations that an include or exclude property exists for.
929 // We want to iterate all configurations rather than either the include or exclude because, for a
930 // particular configuration, we may have either only an include or an exclude to handle.
931 productConfigProps := make(map[android.ProductConfigProperty]bool, len(props)+len(excludeProps))
932 for p := range props {
933 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -0400934 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000935 for p := range excludeProps {
936 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -0400937 }
938
Jingwen Chen25825ca2021-11-15 12:28:43 +0000939 for productConfigProp := range productConfigProps {
940 prop, includesExists := props[productConfigProp]
941 excludesProp, excludesExists := excludeProps[productConfigProp]
Liz Kammer47535c52021-06-02 16:02:22 -0400942 var includes, excludes []string
943 var ok bool
944 // if there was no includes/excludes property, casting fails and that's expected
Jingwen Chen25825ca2021-11-15 12:28:43 +0000945 if includes, ok = prop.([]string); includesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -0400946 ctx.ModuleErrorf("Could not convert product variable %s property", name)
947 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000948 if excludes, ok = excludesProp.([]string); excludesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -0400949 ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
950 }
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400951
Jingwen Chen58ff6802021-11-17 12:14:41 +0000952 dep.attribute.EmitEmptyList = productConfigProp.AlwaysEmit()
Jingwen Chen25825ca2021-11-15 12:28:43 +0000953 dep.attribute.SetSelectValue(
954 productConfigProp.ConfigurationAxis(),
955 productConfigProp.SelectKey(),
956 dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes),
957 )
Liz Kammer47535c52021-06-02 16:02:22 -0400958 }
959 }
Liz Kammere6583482021-10-19 13:56:10 -0400960}
Liz Kammer47535c52021-06-02 16:02:22 -0400961
Liz Kammer54309532021-12-14 12:21:22 -0500962func (la *linkerAttributes) finalize(ctx android.BazelConversionPathContext) {
963 // if system dynamic deps have the default value, any use of a system dynamic library used will
964 // result in duplicate library errors for bionic OSes. Here, we explicitly exclude those libraries
965 // from bionic OSes.
966 if la.systemDynamicDeps.IsNil() && len(la.usedSystemDynamicDepAsDynamicDep) > 0 {
967 toRemove := bazelLabelForSharedDeps(ctx, android.SortedStringKeys(la.usedSystemDynamicDepAsDynamicDep))
968 la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove)
969 la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove)
970 la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove)
971 la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove)
972 }
973
Liz Kammere6583482021-10-19 13:56:10 -0400974 la.deps.ResolveExcludes()
975 la.implementationDeps.ResolveExcludes()
976 la.dynamicDeps.ResolveExcludes()
977 la.implementationDynamicDeps.ResolveExcludes()
978 la.wholeArchiveDeps.ResolveExcludes()
979 la.systemDynamicDeps.ForceSpecifyEmptyList = true
Liz Kammer54309532021-12-14 12:21:22 -0500980
Jingwen Chen91220d72021-03-24 02:18:33 -0400981}
982
Jingwen Chened9c17d2021-04-13 07:14:55 +0000983// Relativize a list of root-relative paths with respect to the module's
984// directory.
985//
986// include_dirs Soong prop are root-relative (b/183742505), but
987// local_include_dirs, export_include_dirs and export_system_include_dirs are
988// module dir relative. This function makes a list of paths entirely module dir
989// relative.
990//
991// For the `include` attribute, Bazel wants the paths to be relative to the
992// module.
993func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000994 var relativePaths []string
995 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000996 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
997 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
998 if err != nil {
999 panic(err)
1000 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +00001001 relativePaths = append(relativePaths, relativePath)
1002 }
1003 return relativePaths
1004}
1005
Liz Kammer5fad5012021-09-09 14:08:21 -04001006// BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel
1007// attributes.
1008type BazelIncludes struct {
Liz Kammer1263d9b2021-12-10 14:28:20 -05001009 AbsoluteIncludes bazel.StringListAttribute
1010 Includes bazel.StringListAttribute
1011 SystemIncludes bazel.StringListAttribute
Liz Kammer5fad5012021-09-09 14:08:21 -04001012}
1013
Liz Kammer54549442022-05-11 13:55:06 -04001014func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module, includes *BazelIncludes) BazelIncludes {
Liz Kammer1263d9b2021-12-10 14:28:20 -05001015 var exported BazelIncludes
1016 if includes != nil {
1017 exported = *includes
1018 } else {
1019 exported = BazelIncludes{}
1020 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001021 bp2BuildPropParseHelper(ctx, module, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1022 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
1023 if len(flagExporterProperties.Export_include_dirs) > 0 {
1024 exported.Includes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.Includes.SelectValue(axis, config), flagExporterProperties.Export_include_dirs...)))
1025 }
1026 if len(flagExporterProperties.Export_system_include_dirs) > 0 {
1027 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 -04001028 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -04001029 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001030 })
Liz Kammer1263d9b2021-12-10 14:28:20 -05001031 exported.AbsoluteIncludes.DeduplicateAxesFromBase()
Liz Kammer5fad5012021-09-09 14:08:21 -04001032 exported.Includes.DeduplicateAxesFromBase()
1033 exported.SystemIncludes.DeduplicateAxesFromBase()
Rupert Shuttleworth375451e2021-04-26 07:49:08 -04001034
Liz Kammer5fad5012021-09-09 14:08:21 -04001035 return exported
Jingwen Chen91220d72021-03-24 02:18:33 -04001036}
Chris Parsons953b3562021-09-20 15:14:39 -04001037
Jingwen Chen55bc8202021-11-02 06:40:51 +00001038func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001039 label := android.BazelModuleLabel(ctx, m)
Chris Parsonsad876012022-08-20 14:48:32 -04001040 if ccModule, ok := m.(*Module); ok && ccModule.typ() == fullLibrary && !android.GetBp2BuildAllowList().GenerateCcLibraryStaticOnly(m.Name()) {
Liz Kammer35ca77e2021-12-22 15:31:40 -05001041 label += "_bp2build_cc_library_static"
Chris Parsons953b3562021-09-20 15:14:39 -04001042 }
1043 return label
1044}
1045
Jingwen Chen55bc8202021-11-02 06:40:51 +00001046func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001047 // cc_library, at it's root name, propagates the shared library, which depends on the static
1048 // library.
1049 return android.BazelModuleLabel(ctx, m)
1050}
1051
Jingwen Chen55bc8202021-11-02 06:40:51 +00001052func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001053 label := bazelLabelForStaticModule(ctx, m)
1054 if aModule, ok := m.(android.Module); ok {
1055 if android.IsModulePrebuilt(aModule) {
1056 label += "_alwayslink"
1057 }
1058 }
1059 return label
1060}
1061
Jingwen Chen55bc8202021-11-02 06:40:51 +00001062func bazelLabelForWholeDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001063 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps)
1064}
1065
Jingwen Chen55bc8202021-11-02 06:40:51 +00001066func bazelLabelForWholeDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001067 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps)
1068}
1069
Jingwen Chen55bc8202021-11-02 06:40:51 +00001070func bazelLabelForStaticDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001071 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule)
1072}
1073
Jingwen Chen55bc8202021-11-02 06:40:51 +00001074func bazelLabelForStaticDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001075 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule)
1076}
1077
Jingwen Chen55bc8202021-11-02 06:40:51 +00001078func bazelLabelForSharedDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001079 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule)
1080}
1081
Jingwen Chen55bc8202021-11-02 06:40:51 +00001082func bazelLabelForHeaderDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001083 // This is not elegant, but bp2build's shared library targets only propagate
1084 // their header information as part of the normal C++ provider.
1085 return bazelLabelForSharedDeps(ctx, modules)
1086}
1087
Jingwen Chen55bc8202021-11-02 06:40:51 +00001088func bazelLabelForSharedDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001089 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
1090}
Liz Kammer2b8004b2021-10-04 13:55:44 -04001091
1092type binaryLinkerAttrs struct {
1093 Linkshared *bool
1094}
1095
Jingwen Chen55bc8202021-11-02 06:40:51 +00001096func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs {
Liz Kammer2b8004b2021-10-04 13:55:44 -04001097 attrs := binaryLinkerAttrs{}
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001098 bp2BuildPropParseHelper(ctx, m, &BinaryLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1099 linkerProps := props.(*BinaryLinkerProperties)
1100 staticExecutable := linkerProps.Static_executable
1101 if axis == bazel.NoConfigAxis {
1102 if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared {
1103 attrs.Linkshared = &linkBinaryShared
Liz Kammer2b8004b2021-10-04 13:55:44 -04001104 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001105 } else if staticExecutable != nil {
1106 // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a
1107 // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling
1108 ctx.ModuleErrorf("bp2build cannot migrate a module with arch/target-specific static_executable values")
Liz Kammer2b8004b2021-10-04 13:55:44 -04001109 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001110 })
Liz Kammer2b8004b2021-10-04 13:55:44 -04001111
1112 return attrs
1113}