blob: 625e7ce149a35b035ba287cfd8fa114e75e796f5 [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"
Vinh Tran9f6796a2022-08-16 13:10:31 -040038 aidlSrcPartition = "aidl"
Liz Kammerae3994e2021-10-19 09:45:48 -040039)
40
Liz Kammer2222c6b2021-05-24 15:41:47 -040041// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
Jingwen Chenbcf53042021-05-26 04:42:42 +000042// properties which apply to either the shared or static version of a cc_library module.
Liz Kammer2222c6b2021-05-24 15:41:47 -040043type staticOrSharedAttributes struct {
Vinh Tran9f6796a2022-08-16 13:10:31 -040044 Srcs bazel.LabelListAttribute
45 Srcs_c bazel.LabelListAttribute
46 Srcs_as bazel.LabelListAttribute
47 Srcs_aidl bazel.LabelListAttribute
48 Hdrs bazel.LabelListAttribute
49 Copts bazel.StringListAttribute
Jingwen Chen14a8bda2021-06-02 11:10:02 +000050
Liz Kammer12615db2021-09-28 09:19:17 -040051 Deps bazel.LabelListAttribute
52 Implementation_deps bazel.LabelListAttribute
53 Dynamic_deps bazel.LabelListAttribute
54 Implementation_dynamic_deps bazel.LabelListAttribute
55 Whole_archive_deps bazel.LabelListAttribute
56 Implementation_whole_archive_deps bazel.LabelListAttribute
Cole Faust6b29f592022-08-09 09:50:56 -070057 Runtime_deps bazel.LabelListAttribute
Chris Parsons51f8c392021-08-03 21:01:05 -040058
59 System_dynamic_deps bazel.LabelListAttribute
Chris Parsons58852a02021-12-09 18:10:18 -050060
61 Enabled bazel.BoolAttribute
Yu Liufc603162022-03-01 15:44:08 -080062
Yu Liu8d82ac52022-05-17 15:13:28 -070063 Native_coverage bazel.BoolAttribute
64
Yu Liufc603162022-03-01 15:44:08 -080065 sdkAttributes
Jingwen Chen53681ef2021-04-29 08:15:13 +000066}
67
Sam Delmericoc7681022022-02-04 21:01:20 +000068// groupSrcsByExtension partitions `srcs` into groups based on file extension.
Jingwen Chen55bc8202021-11-02 06:40:51 +000069func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute {
Liz Kammer57e2e7a2021-09-20 12:55:02 -040070 // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl
71 // macro.
72 addSuffixForFilegroup := func(suffix string) bazel.LabelMapper {
Vinh Tran9f6796a2022-08-16 13:10:31 -040073 return func(otherModuleCtx bazel.OtherModuleContext, label bazel.Label) (string, bool) {
74
75 m, exists := otherModuleCtx.ModuleFromName(label.OriginalModuleName)
Liz Kammer12615db2021-09-28 09:19:17 -040076 labelStr := label.Label
Vinh Tran9f6796a2022-08-16 13:10:31 -040077 if !exists || !android.IsFilegroup(otherModuleCtx, m) {
78 return labelStr, false
79 }
80 // If the filegroup is already converted to aidl_library, skip creating
81 // _c_srcs, _as_srcs, _cpp_srcs filegroups
82 fg, _ := m.(android.Bp2buildAidlLibrary)
83 if fg.ShouldConvertToAidlLibrary(ctx) {
Liz Kammer12615db2021-09-28 09:19:17 -040084 return labelStr, false
Jingwen Chen14a8bda2021-06-02 11:10:02 +000085 }
Liz Kammer12615db2021-09-28 09:19:17 -040086 return labelStr + suffix, true
Chris Parsons5a34ffb2021-07-21 14:34:58 -040087 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +000088 }
89
Liz Kammer57e2e7a2021-09-20 12:55:02 -040090 // TODO(b/190006308): Handle language detection of sources in a Bazel rule.
Sam Delmericoc7681022022-02-04 21:01:20 +000091 labels := bazel.LabelPartitions{
92 protoSrcPartition: android.ProtoSrcLabelPartition,
Liz Kammeraabfb5d2021-12-08 15:25:06 -050093 cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")},
94 asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")},
Cole Faust7071a052022-07-29 15:58:33 -070095 asmSrcPartition: bazel.LabelPartition{Extensions: []string{".asm"}},
Vinh Tran9f6796a2022-08-16 13:10:31 -040096 aidlSrcPartition: android.AidlSrcLabelPartition,
Trevor Radcliffeef9c9002022-05-13 20:55:35 +000097 // TODO(http://b/231968910): If there is ever a filegroup target that
98 // contains .l or .ll files we will need to find a way to add a
99 // LabelMapper for these that identifies these filegroups and
100 // converts them appropriately
101 lSrcPartition: bazel.LabelPartition{Extensions: []string{".l"}},
102 llSrcPartition: bazel.LabelPartition{Extensions: []string{".ll"}},
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400103 // C++ is the "catch-all" group, and comprises generated sources because we don't
104 // know the language of these sources until the genrule is executed.
Liz Kammeraabfb5d2021-12-08 15:25:06 -0500105 cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true},
Sam Delmericoc7681022022-02-04 21:01:20 +0000106 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000107
Sam Delmericoc7681022022-02-04 21:01:20 +0000108 return bazel.PartitionLabelListAttribute(ctx, &srcs, labels)
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000109}
110
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000111// bp2BuildParseLibProps returns the attributes for a variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000112func bp2BuildParseLibProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000113 lib, ok := module.compiler.(*libraryDecorator)
114 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400115 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000116 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000117 return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic)
118}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000119
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000120// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000121func bp2BuildParseSharedProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000122 return bp2BuildParseLibProps(ctx, module, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000123}
124
125// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000126func bp2BuildParseStaticProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000127 return bp2BuildParseLibProps(ctx, module, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400128}
129
Liz Kammer7a210ac2021-09-22 15:52:58 -0400130type depsPartition struct {
131 export bazel.LabelList
132 implementation bazel.LabelList
133}
134
Jingwen Chen55bc8202021-11-02 06:40:51 +0000135type bazelLabelForDepsFn func(android.BazelConversionPathContext, []string) bazel.LabelList
Liz Kammer7a210ac2021-09-22 15:52:58 -0400136
Jingwen Chen55bc8202021-11-02 06:40:51 +0000137func maybePartitionExportedAndImplementationsDeps(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, exportedDeps []string, fn bazelLabelForDepsFn) depsPartition {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400138 if !exportsDeps {
139 return depsPartition{
140 implementation: fn(ctx, allDeps),
141 }
142 }
143
Liz Kammer7a210ac2021-09-22 15:52:58 -0400144 implementation, export := android.FilterList(allDeps, exportedDeps)
145
146 return depsPartition{
147 export: fn(ctx, export),
148 implementation: fn(ctx, implementation),
149 }
150}
151
Jingwen Chen55bc8202021-11-02 06:40:51 +0000152type bazelLabelForDepsExcludesFn func(android.BazelConversionPathContext, []string, []string) bazel.LabelList
Liz Kammer7a210ac2021-09-22 15:52:58 -0400153
Jingwen Chen55bc8202021-11-02 06:40:51 +0000154func maybePartitionExportedAndImplementationsDepsExcludes(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, excludes, exportedDeps []string, fn bazelLabelForDepsExcludesFn) depsPartition {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400155 if !exportsDeps {
156 return depsPartition{
157 implementation: fn(ctx, allDeps, excludes),
158 }
159 }
Liz Kammer7a210ac2021-09-22 15:52:58 -0400160 implementation, export := android.FilterList(allDeps, exportedDeps)
161
162 return depsPartition{
163 export: fn(ctx, export, excludes),
164 implementation: fn(ctx, implementation, excludes),
165 }
166}
167
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000168// Parses properties common to static and shared libraries. Also used for prebuilt libraries.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000169func bp2buildParseStaticOrSharedProps(ctx android.BazelConversionPathContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes {
Liz Kammer135bf552021-08-11 10:46:06 -0400170 attrs := staticOrSharedAttributes{}
Jingwen Chenbcf53042021-05-26 04:42:42 +0000171
Liz Kammer9abd62d2021-05-21 08:37:59 -0400172 setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
Alix1be00d42022-05-16 22:56:04 +0000173 attrs.Copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, true, filterOutStdFlag))
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000174 attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
Chris Parsons953b3562021-09-20 15:14:39 -0400175 attrs.System_dynamic_deps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, props.System_shared_libs))
Liz Kammer7a210ac2021-09-22 15:52:58 -0400176
Liz Kammer2b8004b2021-10-04 13:55:44 -0400177 staticDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Static_libs, props.Export_static_lib_headers, bazelLabelForStaticDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400178 attrs.Deps.SetSelectValue(axis, config, staticDeps.export)
179 attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation)
180
Liz Kammer2b8004b2021-10-04 13:55:44 -0400181 sharedDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400182 attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export)
183 attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation)
184
185 attrs.Whole_archive_deps.SetSelectValue(axis, config, bazelLabelForWholeDeps(ctx, props.Whole_static_libs))
Chris Parsons58852a02021-12-09 18:10:18 -0500186 attrs.Enabled.SetSelectValue(axis, config, props.Enabled)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000187 }
Liz Kammer135bf552021-08-11 10:46:06 -0400188 // system_dynamic_deps distinguishes between nil/empty list behavior:
189 // nil -> use default values
190 // empty list -> no values specified
191 attrs.System_dynamic_deps.ForceSpecifyEmptyList = true
Jingwen Chenbcf53042021-05-26 04:42:42 +0000192
193 if isStatic {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000194 bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
195 if staticOrSharedProps, ok := props.(*StaticProperties); ok {
196 setAttrs(axis, config, staticOrSharedProps.Static)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000197 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000198 })
Jingwen Chenbcf53042021-05-26 04:42:42 +0000199 } else {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000200 bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
201 if staticOrSharedProps, ok := props.(*SharedProperties); ok {
202 setAttrs(axis, config, staticOrSharedProps.Shared)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000203 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000204 })
Jingwen Chenbcf53042021-05-26 04:42:42 +0000205 }
206
Liz Kammerae3994e2021-10-19 09:45:48 -0400207 partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs)
208 attrs.Srcs = partitionedSrcs[cppSrcPartition]
209 attrs.Srcs_c = partitionedSrcs[cSrcPartition]
210 attrs.Srcs_as = partitionedSrcs[asSrcPartition]
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000211
Liz Kammer12615db2021-09-28 09:19:17 -0400212 if !partitionedSrcs[protoSrcPartition].IsEmpty() {
213 // TODO(b/208815215): determine whether this is used and add support if necessary
214 ctx.ModuleErrorf("Migrating static/shared only proto srcs is not currently supported")
215 }
216
Jingwen Chenbcf53042021-05-26 04:42:42 +0000217 return attrs
Jingwen Chen53681ef2021-04-29 08:15:13 +0000218}
219
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400220// Convenience struct to hold all attributes parsed from prebuilt properties.
221type prebuiltAttributes struct {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000222 Src bazel.LabelAttribute
223 Enabled bazel.BoolAttribute
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400224}
225
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000226// NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000227func Bp2BuildParsePrebuiltLibraryProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) prebuiltAttributes {
228 manySourceFileError := func(axis bazel.ConfigurationAxis, config string) {
229 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most one source file for %s %s\n", axis, config)
230 }
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400231 var srcLabelAttribute bazel.LabelAttribute
232
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000233 parseSrcs := func(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, srcs []string) {
234 if len(srcs) > 1 {
235 manySourceFileError(axis, config)
236 return
237 } else if len(srcs) == 0 {
238 return
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400239 }
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000240 if srcLabelAttribute.SelectValue(axis, config) != nil {
241 manySourceFileError(axis, config)
242 return
243 }
244
245 src := android.BazelLabelForModuleSrcSingle(ctx, srcs[0])
246 srcLabelAttribute.SetSelectValue(axis, config, src)
247 }
248
249 bp2BuildPropParseHelper(ctx, module, &prebuiltLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
250 if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok {
251 parseSrcs(ctx, axis, config, prebuiltLinkerProperties.Srcs)
252 }
253 })
254
255 var enabledLabelAttribute bazel.BoolAttribute
256 parseAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
257 if props.Enabled != nil {
258 enabledLabelAttribute.SetSelectValue(axis, config, props.Enabled)
259 }
260 parseSrcs(ctx, axis, config, props.Srcs)
261 }
262
263 if isStatic {
264 bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
265 if staticProperties, ok := props.(*StaticProperties); ok {
266 parseAttrs(axis, config, staticProperties.Static)
267 }
268 })
269 } else {
270 bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
271 if sharedProperties, ok := props.(*SharedProperties); ok {
272 parseAttrs(axis, config, sharedProperties.Shared)
273 }
274 })
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400275 }
276
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400277 return prebuiltAttributes{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000278 Src: srcLabelAttribute,
279 Enabled: enabledLabelAttribute,
280 }
281}
282
283func bp2BuildPropParseHelper(ctx android.ArchVariantContext, module *Module, propsType interface{}, parseFunc func(axis bazel.ConfigurationAxis, config string, props interface{})) {
284 for axis, configToProps := range module.GetArchVariantProperties(ctx, propsType) {
285 for config, props := range configToProps {
286 parseFunc(axis, config, props)
287 }
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400288 }
289}
290
Liz Kammere6583482021-10-19 13:56:10 -0400291type baseAttributes struct {
292 compilerAttributes
293 linkerAttributes
Liz Kammer12615db2021-09-28 09:19:17 -0400294
Cole Faust5fa4e962022-08-22 14:31:04 -0700295 // A combination of compilerAttributes.features and linkerAttributes.features
296 features bazel.StringListAttribute
Liz Kammer12615db2021-09-28 09:19:17 -0400297 protoDependency *bazel.LabelAttribute
Vinh Tran9f6796a2022-08-16 13:10:31 -0400298 aidlDependency *bazel.LabelAttribute
Liz Kammere6583482021-10-19 13:56:10 -0400299}
300
Jingwen Chen107c0de2021-04-09 10:43:12 +0000301// Convenience struct to hold all attributes parsed from compiler properties.
302type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400303 // Options for all languages
304 copts bazel.StringListAttribute
305 // Assembly options and sources
306 asFlags bazel.StringListAttribute
307 asSrcs bazel.LabelListAttribute
Cole Faust7071a052022-07-29 15:58:33 -0700308 asmSrcs bazel.LabelListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400309 // C options and sources
310 conlyFlags bazel.StringListAttribute
311 cSrcs bazel.LabelListAttribute
312 // C++ options and sources
313 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000314 srcs bazel.LabelListAttribute
Chris Parsons2c788392021-08-10 11:58:07 -0400315
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000316 // Lex sources and options
317 lSrcs bazel.LabelListAttribute
318 llSrcs bazel.LabelListAttribute
319 lexopts bazel.StringListAttribute
320
Liz Kammere6583482021-10-19 13:56:10 -0400321 hdrs bazel.LabelListAttribute
322
Chris Parsons2c788392021-08-10 11:58:07 -0400323 rtti bazel.BoolAttribute
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000324
325 // Not affected by arch variants
326 stl *string
Chris Parsons79bd2b72021-11-29 17:52:41 -0500327 cStd *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000328 cppStd *string
Liz Kammer35687bc2021-09-10 10:07:07 -0400329
330 localIncludes bazel.StringListAttribute
331 absoluteIncludes bazel.StringListAttribute
Liz Kammer12615db2021-09-28 09:19:17 -0400332
Liz Kammer1263d9b2021-12-10 14:28:20 -0500333 includes BazelIncludes
334
Liz Kammer12615db2021-09-28 09:19:17 -0400335 protoSrcs bazel.LabelListAttribute
Vinh Tran9f6796a2022-08-16 13:10:31 -0400336 aidlSrcs bazel.LabelListAttribute
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000337
338 stubsSymbolFile *string
339 stubsVersions bazel.StringListAttribute
Cole Faust5fa4e962022-08-22 14:31:04 -0700340
341 features bazel.StringListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000342}
343
Liz Kammercac7f692021-12-16 14:19:32 -0500344type filterOutFn func(string) bool
345
346func filterOutStdFlag(flag string) bool {
347 return strings.HasPrefix(flag, "-std=")
348}
349
Alix1be00d42022-05-16 22:56:04 +0000350func filterOutClangUnknownCflags(flag string) bool {
351 for _, f := range config.ClangUnknownCflags {
352 if f == flag {
353 return true
354 }
355 }
356 return false
357}
358
359func parseCommandLineFlags(soongFlags []string, noCoptsTokenization bool, filterOut ...filterOutFn) []string {
Liz Kammere6583482021-10-19 13:56:10 -0400360 var result []string
361 for _, flag := range soongFlags {
Alix1be00d42022-05-16 22:56:04 +0000362 skipFlag := false
363 for _, filter := range filterOut {
364 if filter != nil && filter(flag) {
365 skipFlag = true
366 }
367 }
368 if skipFlag {
Liz Kammercac7f692021-12-16 14:19:32 -0500369 continue
370 }
Liz Kammere6583482021-10-19 13:56:10 -0400371 // Soong's cflags can contain spaces, like `-include header.h`. For
372 // Bazel's copts, split them up to be compatible with the
373 // no_copts_tokenization feature.
Alix1be00d42022-05-16 22:56:04 +0000374 if noCoptsTokenization {
375 result = append(result, strings.Split(flag, " ")...)
376 } else {
377 // Soong's Version Script and Dynamic List Properties are added as flags
378 // to Bazel's linkopts using "($location label)" syntax.
379 // Splitting on spaces would separate this into two different flags
380 // "($ location" and "label)"
381 result = append(result, flag)
382 }
Liz Kammere6583482021-10-19 13:56:10 -0400383 }
384 return result
385}
Jingwen Chened9c17d2021-04-13 07:14:55 +0000386
Jingwen Chen55bc8202021-11-02 06:40:51 +0000387func (ca *compilerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, props *BaseCompilerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400388 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
389 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
390 if srcsList, ok := parseSrcs(ctx, props); ok {
391 ca.srcs.SetSelectValue(axis, config, srcsList)
Chris Parsons990c4f42021-05-25 12:10:58 -0400392 }
393
Liz Kammere6583482021-10-19 13:56:10 -0400394 localIncludeDirs := props.Local_include_dirs
395 if axis == bazel.NoConfigAxis {
Chris Parsons79bd2b72021-11-29 17:52:41 -0500396 ca.cStd, ca.cppStd = bp2buildResolveCppStdValue(props.C_std, props.Cpp_std, props.Gnu_extensions)
Liz Kammere6583482021-10-19 13:56:10 -0400397 if includeBuildDirectory(props.Include_build_directory) {
398 localIncludeDirs = append(localIncludeDirs, ".")
Liz Kammer222bdcf2021-10-11 14:15:51 -0400399 }
Jingwen Chene32e9e02021-04-23 09:17:24 +0000400 }
401
Liz Kammere6583482021-10-19 13:56:10 -0400402 ca.absoluteIncludes.SetSelectValue(axis, config, props.Include_dirs)
403 ca.localIncludes.SetSelectValue(axis, config, localIncludeDirs)
404
Cole Faust5fa4e962022-08-22 14:31:04 -0700405 instructionSet := proptools.StringDefault(props.Instruction_set, "")
406 if instructionSet == "arm" {
407 ca.features.SetSelectValue(axis, config, []string{"arm_isa_arm", "-arm_isa_thumb"})
408 } else if instructionSet != "" && instructionSet != "thumb" {
409 ctx.ModuleErrorf("Unknown value for instruction_set: %s", instructionSet)
410 }
411
Liz Kammercac7f692021-12-16 14:19:32 -0500412 // In Soong, cflags occur on the command line before -std=<val> flag, resulting in the value being
413 // overridden. In Bazel we always allow overriding, via flags; however, this can cause
414 // incompatibilities, so we remove "-std=" flags from Cflag properties while leaving it in other
415 // cases.
Alix1be00d42022-05-16 22:56:04 +0000416 ca.copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, true, filterOutStdFlag, filterOutClangUnknownCflags))
417 ca.asFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Asflags, true, nil))
418 ca.conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Conlyflags, true, filterOutClangUnknownCflags))
419 ca.cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Cppflags, true, filterOutClangUnknownCflags))
Liz Kammere6583482021-10-19 13:56:10 -0400420 ca.rtti.SetSelectValue(axis, config, props.Rtti)
421}
422
Jingwen Chen55bc8202021-11-02 06:40:51 +0000423func (ca *compilerAttributes) convertStlProps(ctx android.ArchVariantContext, module *Module) {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000424 bp2BuildPropParseHelper(ctx, module, &StlProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
425 if stlProps, ok := props.(*StlProperties); ok {
426 if stlProps.Stl == nil {
427 return
428 }
429 if ca.stl == nil {
Liz Kammer7128d382022-05-12 11:42:33 -0400430 stl := deduplicateStlInput(*stlProps.Stl)
431 ca.stl = &stl
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000432 } else if ca.stl != stlProps.Stl {
433 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 -0400434 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000435 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000436 })
Liz Kammere6583482021-10-19 13:56:10 -0400437}
Jingwen Chenc1c26502021-04-05 10:35:13 +0000438
Jingwen Chen55bc8202021-11-02 06:40:51 +0000439func (ca *compilerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Liz Kammerba7a9c52021-05-26 08:45:30 -0400440 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
Liz Kammere6583482021-10-19 13:56:10 -0400441 "Cflags": &ca.copts,
442 "Asflags": &ca.asFlags,
443 "CppFlags": &ca.cppFlags,
Liz Kammerba7a9c52021-05-26 08:45:30 -0400444 }
Liz Kammerba7a9c52021-05-26 08:45:30 -0400445 for propName, attr := range productVarPropNameToAttribute {
Jingwen Chen25825ca2021-11-15 12:28:43 +0000446 if productConfigProps, exists := productVariableProps[propName]; exists {
447 for productConfigProp, prop := range productConfigProps {
448 flags, ok := prop.([]string)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400449 if !ok {
450 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
451 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000452 newFlags, _ := bazel.TryVariableSubstitutions(flags, productConfigProp.Name)
453 attr.SetSelectValue(productConfigProp.ConfigurationAxis(), productConfigProp.SelectKey(), newFlags)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400454 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400455 }
456 }
Liz Kammere6583482021-10-19 13:56:10 -0400457}
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400458
Jingwen Chen55bc8202021-11-02 06:40:51 +0000459func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, implementationHdrs bazel.LabelListAttribute) {
Liz Kammere6583482021-10-19 13:56:10 -0400460 ca.srcs.ResolveExcludes()
461 partitionedSrcs := groupSrcsByExtension(ctx, ca.srcs)
462
Liz Kammer12615db2021-09-28 09:19:17 -0400463 ca.protoSrcs = partitionedSrcs[protoSrcPartition]
Vinh Tran9f6796a2022-08-16 13:10:31 -0400464 ca.aidlSrcs = partitionedSrcs[aidlSrcPartition]
Liz Kammer12615db2021-09-28 09:19:17 -0400465
Liz Kammere6583482021-10-19 13:56:10 -0400466 for p, lla := range partitionedSrcs {
467 // if there are no sources, there is no need for headers
468 if lla.IsEmpty() {
469 continue
470 }
471 lla.Append(implementationHdrs)
472 partitionedSrcs[p] = lla
473 }
474
475 ca.srcs = partitionedSrcs[cppSrcPartition]
476 ca.cSrcs = partitionedSrcs[cSrcPartition]
477 ca.asSrcs = partitionedSrcs[asSrcPartition]
Cole Faust7071a052022-07-29 15:58:33 -0700478 ca.asmSrcs = partitionedSrcs[asmSrcPartition]
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000479 ca.lSrcs = partitionedSrcs[lSrcPartition]
480 ca.llSrcs = partitionedSrcs[llSrcPartition]
Liz Kammere6583482021-10-19 13:56:10 -0400481
482 ca.absoluteIncludes.DeduplicateAxesFromBase()
483 ca.localIncludes.DeduplicateAxesFromBase()
484}
485
486// Parse srcs from an arch or OS's props value.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000487func parseSrcs(ctx android.BazelConversionPathContext, props *BaseCompilerProperties) (bazel.LabelList, bool) {
Liz Kammere6583482021-10-19 13:56:10 -0400488 anySrcs := false
489 // Add srcs-like dependencies such as generated files.
490 // First create a LabelList containing these dependencies, then merge the values with srcs.
491 generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources)
492 if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 {
493 anySrcs = true
494 }
495
496 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs)
Vinh Tran9f6796a2022-08-16 13:10:31 -0400497
Liz Kammere6583482021-10-19 13:56:10 -0400498 if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 {
499 anySrcs = true
500 }
Vinh Tran9f6796a2022-08-16 13:10:31 -0400501
Liz Kammere6583482021-10-19 13:56:10 -0400502 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs
503}
504
Vinh Tran9f6796a2022-08-16 13:10:31 -0400505// Given a name in srcs prop, check to see if the name references a filegroup
506// and the filegroup is converted to aidl_library
507func isConvertedToAidlLibrary(ctx android.BazelConversionPathContext, name string) bool {
508 if module, ok := ctx.ModuleFromName(name); ok {
509 if android.IsFilegroup(ctx, module) {
510 if fg, ok := module.(android.Bp2buildAidlLibrary); ok {
511 return fg.ShouldConvertToAidlLibrary(ctx)
512 }
513 }
514 }
515 return false
516}
517
Liz Kammera5a29de2022-05-25 23:19:37 -0400518func bp2buildStdVal(std *string, prefix string, useGnu bool) *string {
519 defaultVal := prefix + "_std_default"
Chris Parsons79bd2b72021-11-29 17:52:41 -0500520 // If c{,pp}std properties are not specified, don't generate them in the BUILD file.
521 // Defaults are handled by the toolchain definition.
522 // However, if gnu_extensions is false, then the default gnu-to-c version must be specified.
Liz Kammera5a29de2022-05-25 23:19:37 -0400523 stdVal := proptools.StringDefault(std, defaultVal)
524 if stdVal == "experimental" || stdVal == defaultVal {
525 if stdVal == "experimental" {
526 stdVal = prefix + "_std_experimental"
527 }
528 if !useGnu {
529 stdVal += "_no_gnu"
530 }
531 } else if !useGnu {
532 stdVal = gnuToCReplacer.Replace(stdVal)
Chris Parsons79bd2b72021-11-29 17:52:41 -0500533 }
534
Liz Kammera5a29de2022-05-25 23:19:37 -0400535 if stdVal == defaultVal {
536 return nil
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500537 }
Liz Kammera5a29de2022-05-25 23:19:37 -0400538 return &stdVal
539}
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500540
Liz Kammera5a29de2022-05-25 23:19:37 -0400541func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) {
542 useGnu := useGnuExtensions(gnu_extensions)
543
544 return bp2buildStdVal(c_std, "c", useGnu), bp2buildStdVal(cpp_std, "cpp", useGnu)
Liz Kammere6583482021-10-19 13:56:10 -0400545}
546
Liz Kammer1263d9b2021-12-10 14:28:20 -0500547// packageFromLabel extracts package from a fully-qualified or relative Label and whether the label
548// is fully-qualified.
549// e.g. fully-qualified "//a/b:foo" -> "a/b", true, relative: ":bar" -> ".", false
550func packageFromLabel(label string) (string, bool) {
551 split := strings.Split(label, ":")
552 if len(split) != 2 {
553 return "", false
554 }
555 if split[0] == "" {
556 return ".", false
557 }
558 // remove leading "//"
559 return split[0][2:], true
560}
561
562// includesFromLabelList extracts relative/absolute includes from a bazel.LabelList>
563func includesFromLabelList(labelList bazel.LabelList) (relative, absolute []string) {
564 for _, hdr := range labelList.Includes {
565 if pkg, hasPkg := packageFromLabel(hdr.Label); hasPkg {
566 absolute = append(absolute, pkg)
567 } else if pkg != "" {
568 relative = append(relative, pkg)
569 }
570 }
571 return relative, absolute
572}
573
Cole Faust7071a052022-07-29 15:58:33 -0700574type YasmAttributes struct {
575 Srcs bazel.LabelListAttribute
576 Flags bazel.StringListAttribute
577 Include_dirs bazel.StringListAttribute
578}
579
580func bp2BuildYasm(ctx android.Bp2buildMutatorContext, m *Module, ca compilerAttributes) *bazel.LabelAttribute {
581 if ca.asmSrcs.IsEmpty() {
582 return nil
583 }
584
585 // Yasm needs the include directories from both local_includes and
586 // export_include_dirs. We don't care about actually exporting them from the
587 // yasm rule though, because they will also be present on the cc_ rule that
588 // wraps this yasm rule.
589 includes := ca.localIncludes.Clone()
590 bp2BuildPropParseHelper(ctx, m, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
591 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
592 if len(flagExporterProperties.Export_include_dirs) > 0 {
593 x := bazel.StringListAttribute{}
594 x.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs)
595 includes.Append(x)
596 }
597 }
598 })
599
600 ctx.CreateBazelTargetModule(
601 bazel.BazelTargetModuleProperties{
602 Rule_class: "yasm",
603 Bzl_load_location: "//build/bazel/rules/cc:yasm.bzl",
604 },
605 android.CommonAttributes{Name: m.Name() + "_yasm"},
606 &YasmAttributes{
607 Srcs: ca.asmSrcs,
608 Flags: ca.asFlags,
609 Include_dirs: *includes,
610 })
611
612 // We only want to add a dependency on the _yasm target if there are asm
613 // sources in the current configuration. If there are unconfigured asm
614 // sources, always add the dependency. Otherwise, add the dependency only
615 // on the configuration axes and values that had asm sources.
616 if len(ca.asmSrcs.Value.Includes) > 0 {
617 return bazel.MakeLabelAttribute(":" + m.Name() + "_yasm")
618 }
619
620 ret := &bazel.LabelAttribute{}
621 for _, axis := range ca.asmSrcs.SortedConfigurationAxes() {
622 for config := range ca.asmSrcs.ConfigurableValues[axis] {
623 ret.SetSelectValue(axis, config, bazel.Label{Label: ":" + m.Name() + "_yasm"})
624 }
625 }
626 return ret
627}
628
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000629// bp2BuildParseBaseProps returns all compiler, linker, library attributes of a cc module..
Liz Kammer12615db2021-09-28 09:19:17 -0400630func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) baseAttributes {
Liz Kammere6583482021-10-19 13:56:10 -0400631 archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
632 archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{})
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000633 archVariantLibraryProperties := module.GetArchVariantProperties(ctx, &LibraryProperties{})
Liz Kammere6583482021-10-19 13:56:10 -0400634
635 var implementationHdrs bazel.LabelListAttribute
636
637 axisToConfigs := map[bazel.ConfigurationAxis]map[string]bool{}
638 allAxesAndConfigs := func(cp android.ConfigurationAxisToArchVariantProperties) {
639 for axis, configMap := range cp {
640 if _, ok := axisToConfigs[axis]; !ok {
641 axisToConfigs[axis] = map[string]bool{}
642 }
643 for config, _ := range configMap {
644 axisToConfigs[axis][config] = true
Chris Parsonsa967f252021-09-23 16:34:35 -0400645 }
646 }
647 }
Liz Kammere6583482021-10-19 13:56:10 -0400648 allAxesAndConfigs(archVariantCompilerProps)
649 allAxesAndConfigs(archVariantLinkerProps)
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000650 allAxesAndConfigs(archVariantLibraryProperties)
Chris Parsonsa967f252021-09-23 16:34:35 -0400651
Liz Kammere6583482021-10-19 13:56:10 -0400652 compilerAttrs := compilerAttributes{}
653 linkerAttrs := linkerAttributes{}
654
655 for axis, configs := range axisToConfigs {
656 for config, _ := range configs {
657 var allHdrs []string
658 if baseCompilerProps, ok := archVariantCompilerProps[axis][config].(*BaseCompilerProperties); ok {
659 allHdrs = baseCompilerProps.Generated_headers
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000660 if baseCompilerProps.Lex != nil {
661 compilerAttrs.lexopts.SetSelectValue(axis, config, baseCompilerProps.Lex.Flags)
662 }
Liz Kammere6583482021-10-19 13:56:10 -0400663 (&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, config, baseCompilerProps)
664 }
665
666 var exportHdrs []string
667
668 if baseLinkerProps, ok := archVariantLinkerProps[axis][config].(*BaseLinkerProperties); ok {
669 exportHdrs = baseLinkerProps.Export_generated_headers
670
671 (&linkerAttrs).bp2buildForAxisAndConfig(ctx, module.Binary(), axis, config, baseLinkerProps)
672 }
673 headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportHdrs, android.BazelLabelForModuleDeps)
674 implementationHdrs.SetSelectValue(axis, config, headers.implementation)
675 compilerAttrs.hdrs.SetSelectValue(axis, config, headers.export)
Liz Kammer1263d9b2021-12-10 14:28:20 -0500676
677 exportIncludes, exportAbsoluteIncludes := includesFromLabelList(headers.export)
678 compilerAttrs.includes.Includes.SetSelectValue(axis, config, exportIncludes)
679 compilerAttrs.includes.AbsoluteIncludes.SetSelectValue(axis, config, exportAbsoluteIncludes)
680
681 includes, absoluteIncludes := includesFromLabelList(headers.implementation)
682 currAbsoluteIncludes := compilerAttrs.absoluteIncludes.SelectValue(axis, config)
683 currAbsoluteIncludes = android.FirstUniqueStrings(append(currAbsoluteIncludes, absoluteIncludes...))
Vinh Tran9f6796a2022-08-16 13:10:31 -0400684
Liz Kammer1263d9b2021-12-10 14:28:20 -0500685 compilerAttrs.absoluteIncludes.SetSelectValue(axis, config, currAbsoluteIncludes)
Vinh Tran9f6796a2022-08-16 13:10:31 -0400686
Liz Kammer1263d9b2021-12-10 14:28:20 -0500687 currIncludes := compilerAttrs.localIncludes.SelectValue(axis, config)
688 currIncludes = android.FirstUniqueStrings(append(currIncludes, includes...))
Vinh Tran9f6796a2022-08-16 13:10:31 -0400689
Liz Kammer1263d9b2021-12-10 14:28:20 -0500690 compilerAttrs.localIncludes.SetSelectValue(axis, config, currIncludes)
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000691
692 if libraryProps, ok := archVariantLibraryProperties[axis][config].(*LibraryProperties); ok {
693 if axis == bazel.NoConfigAxis {
694 compilerAttrs.stubsSymbolFile = libraryProps.Stubs.Symbol_file
695 compilerAttrs.stubsVersions.SetSelectValue(axis, config, libraryProps.Stubs.Versions)
696 }
697 }
Liz Kammere6583482021-10-19 13:56:10 -0400698 }
699 }
Vinh Tran9f6796a2022-08-16 13:10:31 -0400700
Liz Kammere6583482021-10-19 13:56:10 -0400701 compilerAttrs.convertStlProps(ctx, module)
702 (&linkerAttrs).convertStripProps(ctx, module)
703
Yu Liu8d82ac52022-05-17 15:13:28 -0700704 if module.coverage != nil && module.coverage.Properties.Native_coverage != nil &&
705 !Bool(module.coverage.Properties.Native_coverage) {
706 // Native_coverage is arch neutral
707 (&linkerAttrs).features.Append(bazel.MakeStringListAttribute([]string{"-coverage"}))
708 }
709
Liz Kammere6583482021-10-19 13:56:10 -0400710 productVariableProps := android.ProductVariableProperties(ctx)
711
712 (&compilerAttrs).convertProductVariables(ctx, productVariableProps)
713 (&linkerAttrs).convertProductVariables(ctx, productVariableProps)
714
715 (&compilerAttrs).finalize(ctx, implementationHdrs)
Liz Kammer54309532021-12-14 12:21:22 -0500716 (&linkerAttrs).finalize(ctx)
Liz Kammere6583482021-10-19 13:56:10 -0400717
Cole Faust7071a052022-07-29 15:58:33 -0700718 (&compilerAttrs.srcs).Add(bp2BuildYasm(ctx, module, compilerAttrs))
719
Liz Kammer12615db2021-09-28 09:19:17 -0400720 protoDep := bp2buildProto(ctx, module, compilerAttrs.protoSrcs)
Vinh Tran9f6796a2022-08-16 13:10:31 -0400721 aidlDep := bp2buildCcAidlLibrary(ctx, module, compilerAttrs.aidlSrcs)
Liz Kammer12615db2021-09-28 09:19:17 -0400722
723 // bp2buildProto will only set wholeStaticLib or implementationWholeStaticLib, but we don't know
724 // which. This will add the newly generated proto library to the appropriate attribute and nothing
725 // to the other
726 (&linkerAttrs).wholeArchiveDeps.Add(protoDep.wholeStaticLib)
727 (&linkerAttrs).implementationWholeArchiveDeps.Add(protoDep.implementationWholeStaticLib)
Vinh Tran9f6796a2022-08-16 13:10:31 -0400728 // TODO(b/243023967) Add aidlDep to implementationWholeArchiveDeps if aidl.export_aidl_headers is true
729 (&linkerAttrs).wholeArchiveDeps.Add(aidlDep)
Liz Kammer12615db2021-09-28 09:19:17 -0400730
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000731 convertedLSrcs := bp2BuildLex(ctx, module.Name(), compilerAttrs)
732 (&compilerAttrs).srcs.Add(&convertedLSrcs.srcName)
733 (&compilerAttrs).cSrcs.Add(&convertedLSrcs.cSrcName)
734
Cole Faust5fa4e962022-08-22 14:31:04 -0700735 features := compilerAttrs.features.Clone().Append(linkerAttrs.features)
736 features.DeduplicateAxesFromBase()
737
Liz Kammere6583482021-10-19 13:56:10 -0400738 return baseAttributes{
739 compilerAttrs,
740 linkerAttrs,
Cole Faust5fa4e962022-08-22 14:31:04 -0700741 *features,
Liz Kammer12615db2021-09-28 09:19:17 -0400742 protoDep.protoDep,
Vinh Tran9f6796a2022-08-16 13:10:31 -0400743 aidlDep,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000744 }
745}
746
Vinh Tran9f6796a2022-08-16 13:10:31 -0400747func bp2buildAidlLibraries(
748 ctx android.Bp2buildMutatorContext,
749 m *Module,
750 aidlSrcs bazel.LabelListAttribute,
751) bazel.LabelList {
752 var aidlLibraries bazel.LabelList
753 var directAidlSrcs bazel.LabelList
754
755 // Make a list of labels that correspond to filegroups that are already converted to aidl_library
756 for _, aidlSrc := range aidlSrcs.Value.Includes {
757 src := aidlSrc.OriginalModuleName
758 if isConvertedToAidlLibrary(ctx, src) {
759 module, _ := ctx.ModuleFromName(src)
760 fg, _ := module.(android.Bp2buildAidlLibrary)
761 aidlLibraries.Add(&bazel.Label{
762 Label: fg.GetAidlLibraryLabel(ctx),
763 })
764 } else {
765 directAidlSrcs.Add(&aidlSrc)
766 }
767 }
768
769 if len(directAidlSrcs.Includes) > 0 {
770 aidlLibraryLabel := m.Name() + "_aidl_library"
771 ctx.CreateBazelTargetModule(
772 bazel.BazelTargetModuleProperties{
773 Rule_class: "aidl_library",
774 Bzl_load_location: "//build/bazel/rules/aidl:library.bzl",
775 },
776 android.CommonAttributes{Name: aidlLibraryLabel},
777 &aidlLibraryAttributes{
778 Srcs: bazel.MakeLabelListAttribute(directAidlSrcs),
779 },
780 )
781 aidlLibraries.Add(&bazel.Label{
782 Label: ":" + aidlLibraryLabel,
783 })
784 }
785 return aidlLibraries
786}
787
788func bp2buildCcAidlLibrary(
789 ctx android.Bp2buildMutatorContext,
790 m *Module,
791 aidlSrcs bazel.LabelListAttribute,
792) *bazel.LabelAttribute {
793 suffix := "_cc_aidl_library"
794 ccAidlLibrarylabel := m.Name() + suffix
795
796 aidlLibraries := bp2buildAidlLibraries(ctx, m, aidlSrcs)
797
798 if aidlLibraries.IsEmpty() {
799 return nil
800 }
801
802 ctx.CreateBazelTargetModule(
803 bazel.BazelTargetModuleProperties{
804 Rule_class: "cc_aidl_library",
805 Bzl_load_location: "//build/bazel/rules/cc:cc_aidl_library.bzl",
806 },
807 android.CommonAttributes{Name: ccAidlLibrarylabel},
808 &ccAidlLibraryAttributes{
809 Deps: bazel.MakeLabelListAttribute(aidlLibraries),
810 },
811 )
812
813 label := &bazel.LabelAttribute{
814 Value: &bazel.Label{
815 Label: ":" + ccAidlLibrarylabel,
816 },
817 }
818 return label
819}
820
Yu Liufc603162022-03-01 15:44:08 -0800821func bp2BuildParseSdkAttributes(module *Module) sdkAttributes {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000822 return sdkAttributes{
823 Sdk_version: module.Properties.Sdk_version,
Yu Liufc603162022-03-01 15:44:08 -0800824 Min_sdk_version: module.Properties.Min_sdk_version,
825 }
826}
827
828type sdkAttributes struct {
829 Sdk_version *string
830 Min_sdk_version *string
831}
832
Jingwen Chen107c0de2021-04-09 10:43:12 +0000833// Convenience struct to hold all attributes parsed from linker properties.
834type linkerAttributes struct {
Liz Kammer54309532021-12-14 12:21:22 -0500835 deps bazel.LabelListAttribute
836 implementationDeps bazel.LabelListAttribute
837 dynamicDeps bazel.LabelListAttribute
838 implementationDynamicDeps bazel.LabelListAttribute
Cole Faust6b29f592022-08-09 09:50:56 -0700839 runtimeDeps bazel.LabelListAttribute
Liz Kammer54309532021-12-14 12:21:22 -0500840 wholeArchiveDeps bazel.LabelListAttribute
841 implementationWholeArchiveDeps bazel.LabelListAttribute
842 systemDynamicDeps bazel.LabelListAttribute
843 usedSystemDynamicDepAsDynamicDep map[string]bool
Liz Kammer7a210ac2021-09-22 15:52:58 -0400844
Jingwen Chen6ada5892021-09-17 11:38:09 +0000845 linkCrt bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000846 useLibcrt bazel.BoolAttribute
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500847 useVersionLib bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000848 linkopts bazel.StringListAttribute
Liz Kammerd2871182021-10-04 13:54:37 -0400849 additionalLinkerInputs bazel.LabelListAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000850 stripKeepSymbols bazel.BoolAttribute
851 stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
852 stripKeepSymbolsList bazel.StringListAttribute
853 stripAll bazel.BoolAttribute
854 stripNone bazel.BoolAttribute
Liz Kammer0eae52e2021-10-06 10:32:26 -0400855 features bazel.StringListAttribute
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400856}
857
Liz Kammer54309532021-12-14 12:21:22 -0500858var (
859 soongSystemSharedLibs = []string{"libc", "libm", "libdl"}
860)
861
Jingwen Chen55bc8202021-11-02 06:40:51 +0000862func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, isBinary bool, axis bazel.ConfigurationAxis, config string, props *BaseLinkerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400863 // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module
864 var axisFeatures []string
Liz Kammer7a210ac2021-09-22 15:52:58 -0400865
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400866 wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs)
867 la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs))
Liz Kammere6583482021-10-19 13:56:10 -0400868 // Excludes to parallel Soong:
869 // 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 -0400870 staticLibs := android.FirstUniqueStrings(android.RemoveListFromList(props.Static_libs, wholeStaticLibs))
871
Liz Kammere6583482021-10-19 13:56:10 -0400872 staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, staticLibs, props.Exclude_static_libs, props.Export_static_lib_headers, bazelLabelForStaticDepsExcludes)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400873
Liz Kammere6583482021-10-19 13:56:10 -0400874 headerLibs := android.FirstUniqueStrings(props.Header_libs)
875 hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps)
Jingwen Chen63930982021-03-24 10:04:33 -0400876
Liz Kammere6583482021-10-19 13:56:10 -0400877 (&hDeps.export).Append(staticDeps.export)
878 la.deps.SetSelectValue(axis, config, hDeps.export)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000879
Liz Kammere6583482021-10-19 13:56:10 -0400880 (&hDeps.implementation).Append(staticDeps.implementation)
881 la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation)
Liz Kammer0eae52e2021-10-06 10:32:26 -0400882
Liz Kammere6583482021-10-19 13:56:10 -0400883 systemSharedLibs := props.System_shared_libs
884 // systemSharedLibs distinguishes between nil/empty list behavior:
885 // nil -> use default values
886 // empty list -> no values specified
887 if len(systemSharedLibs) > 0 {
888 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
889 }
890 la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
891
892 sharedLibs := android.FirstUniqueStrings(props.Shared_libs)
Liz Kammer54309532021-12-14 12:21:22 -0500893 excludeSharedLibs := props.Exclude_shared_libs
894 usedSystem := android.FilterListPred(sharedLibs, func(s string) bool {
895 return android.InList(s, soongSystemSharedLibs) && !android.InList(s, excludeSharedLibs)
896 })
897 for _, el := range usedSystem {
898 if la.usedSystemDynamicDepAsDynamicDep == nil {
899 la.usedSystemDynamicDepAsDynamicDep = map[string]bool{}
900 }
901 la.usedSystemDynamicDepAsDynamicDep[el] = true
902 }
903
Liz Kammere6583482021-10-19 13:56:10 -0400904 sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, sharedLibs, props.Exclude_shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes)
905 la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export)
906 la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation)
Wei Li81852ca2022-07-27 00:22:06 -0700907 if axis == bazel.NoConfigAxis || (axis == bazel.OsConfigurationAxis && config == bazel.OsAndroid) {
908 // If a dependency in la.implementationDynamicDeps has stubs, its stub variant should be
909 // used when the dependency is linked in a APEX. The dependencies in NoConfigAxis and
910 // OsConfigurationAxis/OsAndroid are grouped by having stubs or not, so Bazel select()
911 // statement can be used to choose source/stub variants of them.
912 depsWithStubs := []bazel.Label{}
913 for _, l := range sharedDeps.implementation.Includes {
914 dep, _ := ctx.ModuleFromName(l.OriginalModuleName)
915 if m, ok := dep.(*Module); ok && m.HasStubsVariants() {
916 depsWithStubs = append(depsWithStubs, l)
917 }
918 }
919 if len(depsWithStubs) > 0 {
920 implDynamicDeps := bazel.SubtractBazelLabelList(sharedDeps.implementation, bazel.MakeLabelList(depsWithStubs))
921 la.implementationDynamicDeps.SetSelectValue(axis, config, implDynamicDeps)
922
923 stubLibLabels := []bazel.Label{}
924 for _, l := range depsWithStubs {
925 l.Label = l.Label + "_stub_libs_current"
926 stubLibLabels = append(stubLibLabels, l)
927 }
928 inApexSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex)
929 nonApexSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex)
930 defaultSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey)
931 if axis == bazel.NoConfigAxis {
932 (&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels))
933 (&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
934 (&defaultSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
935 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, inApexSelectValue)
936 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, nonApexSelectValue)
937 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, defaultSelectValue)
938 } else if config == bazel.OsAndroid {
939 (&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels))
940 (&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
941 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, inApexSelectValue)
942 la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, nonApexSelectValue)
943 }
944 }
945 }
Liz Kammere6583482021-10-19 13:56:10 -0400946
947 if !BoolDefault(props.Pack_relocations, packRelocationsDefault) {
948 axisFeatures = append(axisFeatures, "disable_pack_relocations")
949 }
950
951 if Bool(props.Allow_undefined_symbols) {
952 axisFeatures = append(axisFeatures, "-no_undefined_symbols")
953 }
954
955 var linkerFlags []string
956 if len(props.Ldflags) > 0 {
Liz Kammerf38a8372022-02-04 15:39:00 -0500957 linkerFlags = append(linkerFlags, proptools.NinjaEscapeList(props.Ldflags)...)
Liz Kammere6583482021-10-19 13:56:10 -0400958 // binaries remove static flag if -shared is in the linker flags
959 if isBinary && android.InList("-shared", linkerFlags) {
960 axisFeatures = append(axisFeatures, "-static_flag")
961 }
962 }
963 if props.Version_script != nil {
964 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script)
965 la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}})
966 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label))
967 }
Alix773adaa2022-04-27 17:49:34 +0000968
969 if props.Dynamic_list != nil {
970 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Dynamic_list)
971 la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}})
972 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--dynamic-list,$(location %s)", label.Label))
973 }
974
Alix1be00d42022-05-16 22:56:04 +0000975 la.linkopts.SetSelectValue(axis, config, parseCommandLineFlags(linkerFlags, false, filterOutClangUnknownCflags))
Liz Kammere6583482021-10-19 13:56:10 -0400976 la.useLibcrt.SetSelectValue(axis, config, props.libCrt())
977
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500978 if axis == bazel.NoConfigAxis {
979 la.useVersionLib.SetSelectValue(axis, config, props.Use_version_lib)
980 }
981
Liz Kammere6583482021-10-19 13:56:10 -0400982 // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it.
983 if props.crt() != nil {
984 if axis == bazel.NoConfigAxis {
985 la.linkCrt.SetSelectValue(axis, config, props.crt())
986 } else if axis == bazel.ArchConfigurationAxis {
987 ctx.ModuleErrorf("nocrt is not supported for arch variants")
988 }
989 }
990
991 if axisFeatures != nil {
992 la.features.SetSelectValue(axis, config, axisFeatures)
993 }
Cole Faust6b29f592022-08-09 09:50:56 -0700994
995 runtimeDeps := android.BazelLabelForModuleDepsExcludes(ctx, props.Runtime_libs, props.Exclude_runtime_libs)
996 if !runtimeDeps.IsEmpty() {
997 la.runtimeDeps.SetSelectValue(axis, config, runtimeDeps)
998 }
Liz Kammere6583482021-10-19 13:56:10 -0400999}
1000
Jingwen Chen55bc8202021-11-02 06:40:51 +00001001func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) {
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001002 bp2BuildPropParseHelper(ctx, module, &StripProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1003 if stripProperties, ok := props.(*StripProperties); ok {
1004 la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols)
1005 la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list)
1006 la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame)
1007 la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All)
1008 la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None)
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001009 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001010 })
Liz Kammere6583482021-10-19 13:56:10 -04001011}
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001012
Jingwen Chen55bc8202021-11-02 06:40:51 +00001013func (la *linkerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Jingwen Chen6ada5892021-09-17 11:38:09 +00001014
Liz Kammer47535c52021-06-02 16:02:22 -04001015 type productVarDep struct {
1016 // the name of the corresponding excludes field, if one exists
1017 excludesField string
1018 // reference to the bazel attribute that should be set for the given product variable config
1019 attribute *bazel.LabelListAttribute
Liz Kammer2d7bbe32021-06-10 18:20:06 -04001020
Jingwen Chen55bc8202021-11-02 06:40:51 +00001021 depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList
Liz Kammer47535c52021-06-02 16:02:22 -04001022 }
1023
1024 productVarToDepFields := map[string]productVarDep{
1025 // product variables do not support exclude_shared_libs
Jingwen Chen55bc8202021-11-02 06:40:51 +00001026 "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes},
1027 "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes},
1028 "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes},
Liz Kammer47535c52021-06-02 16:02:22 -04001029 }
1030
Liz Kammer47535c52021-06-02 16:02:22 -04001031 for name, dep := range productVarToDepFields {
1032 props, exists := productVariableProps[name]
1033 excludeProps, excludesExists := productVariableProps[dep.excludesField]
1034 // if neither an include or excludes property exists, then skip it
1035 if !exists && !excludesExists {
1036 continue
1037 }
Jingwen Chen25825ca2021-11-15 12:28:43 +00001038 // Collect all the configurations that an include or exclude property exists for.
1039 // We want to iterate all configurations rather than either the include or exclude because, for a
1040 // particular configuration, we may have either only an include or an exclude to handle.
1041 productConfigProps := make(map[android.ProductConfigProperty]bool, len(props)+len(excludeProps))
1042 for p := range props {
1043 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -04001044 }
Jingwen Chen25825ca2021-11-15 12:28:43 +00001045 for p := range excludeProps {
1046 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -04001047 }
1048
Jingwen Chen25825ca2021-11-15 12:28:43 +00001049 for productConfigProp := range productConfigProps {
1050 prop, includesExists := props[productConfigProp]
1051 excludesProp, excludesExists := excludeProps[productConfigProp]
Liz Kammer47535c52021-06-02 16:02:22 -04001052 var includes, excludes []string
1053 var ok bool
1054 // if there was no includes/excludes property, casting fails and that's expected
Jingwen Chen25825ca2021-11-15 12:28:43 +00001055 if includes, ok = prop.([]string); includesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -04001056 ctx.ModuleErrorf("Could not convert product variable %s property", name)
1057 }
Jingwen Chen25825ca2021-11-15 12:28:43 +00001058 if excludes, ok = excludesProp.([]string); excludesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -04001059 ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
1060 }
Liz Kammer2d7bbe32021-06-10 18:20:06 -04001061
Jingwen Chen58ff6802021-11-17 12:14:41 +00001062 dep.attribute.EmitEmptyList = productConfigProp.AlwaysEmit()
Jingwen Chen25825ca2021-11-15 12:28:43 +00001063 dep.attribute.SetSelectValue(
1064 productConfigProp.ConfigurationAxis(),
1065 productConfigProp.SelectKey(),
1066 dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes),
1067 )
Liz Kammer47535c52021-06-02 16:02:22 -04001068 }
1069 }
Liz Kammere6583482021-10-19 13:56:10 -04001070}
Liz Kammer47535c52021-06-02 16:02:22 -04001071
Liz Kammer54309532021-12-14 12:21:22 -05001072func (la *linkerAttributes) finalize(ctx android.BazelConversionPathContext) {
1073 // if system dynamic deps have the default value, any use of a system dynamic library used will
1074 // result in duplicate library errors for bionic OSes. Here, we explicitly exclude those libraries
1075 // from bionic OSes.
1076 if la.systemDynamicDeps.IsNil() && len(la.usedSystemDynamicDepAsDynamicDep) > 0 {
1077 toRemove := bazelLabelForSharedDeps(ctx, android.SortedStringKeys(la.usedSystemDynamicDepAsDynamicDep))
1078 la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove)
1079 la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove)
1080 la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove)
1081 la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove)
1082 }
1083
Liz Kammere6583482021-10-19 13:56:10 -04001084 la.deps.ResolveExcludes()
1085 la.implementationDeps.ResolveExcludes()
1086 la.dynamicDeps.ResolveExcludes()
1087 la.implementationDynamicDeps.ResolveExcludes()
1088 la.wholeArchiveDeps.ResolveExcludes()
1089 la.systemDynamicDeps.ForceSpecifyEmptyList = true
Liz Kammer54309532021-12-14 12:21:22 -05001090
Jingwen Chen91220d72021-03-24 02:18:33 -04001091}
1092
Jingwen Chened9c17d2021-04-13 07:14:55 +00001093// Relativize a list of root-relative paths with respect to the module's
1094// directory.
1095//
1096// include_dirs Soong prop are root-relative (b/183742505), but
1097// local_include_dirs, export_include_dirs and export_system_include_dirs are
1098// module dir relative. This function makes a list of paths entirely module dir
1099// relative.
1100//
1101// For the `include` attribute, Bazel wants the paths to be relative to the
1102// module.
1103func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +00001104 var relativePaths []string
1105 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +00001106 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
1107 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
1108 if err != nil {
1109 panic(err)
1110 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +00001111 relativePaths = append(relativePaths, relativePath)
1112 }
1113 return relativePaths
1114}
1115
Liz Kammer5fad5012021-09-09 14:08:21 -04001116// BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel
1117// attributes.
1118type BazelIncludes struct {
Liz Kammer1263d9b2021-12-10 14:28:20 -05001119 AbsoluteIncludes bazel.StringListAttribute
1120 Includes bazel.StringListAttribute
1121 SystemIncludes bazel.StringListAttribute
Liz Kammer5fad5012021-09-09 14:08:21 -04001122}
1123
Liz Kammer54549442022-05-11 13:55:06 -04001124func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module, includes *BazelIncludes) BazelIncludes {
Liz Kammer1263d9b2021-12-10 14:28:20 -05001125 var exported BazelIncludes
1126 if includes != nil {
1127 exported = *includes
1128 } else {
1129 exported = BazelIncludes{}
1130 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001131 bp2BuildPropParseHelper(ctx, module, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1132 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
1133 if len(flagExporterProperties.Export_include_dirs) > 0 {
1134 exported.Includes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.Includes.SelectValue(axis, config), flagExporterProperties.Export_include_dirs...)))
1135 }
1136 if len(flagExporterProperties.Export_system_include_dirs) > 0 {
1137 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 -04001138 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -04001139 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001140 })
Liz Kammer1263d9b2021-12-10 14:28:20 -05001141 exported.AbsoluteIncludes.DeduplicateAxesFromBase()
Liz Kammer5fad5012021-09-09 14:08:21 -04001142 exported.Includes.DeduplicateAxesFromBase()
1143 exported.SystemIncludes.DeduplicateAxesFromBase()
Rupert Shuttleworth375451e2021-04-26 07:49:08 -04001144
Liz Kammer5fad5012021-09-09 14:08:21 -04001145 return exported
Jingwen Chen91220d72021-03-24 02:18:33 -04001146}
Chris Parsons953b3562021-09-20 15:14:39 -04001147
Jingwen Chen55bc8202021-11-02 06:40:51 +00001148func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001149 label := android.BazelModuleLabel(ctx, m)
Chris Parsonsad876012022-08-20 14:48:32 -04001150 if ccModule, ok := m.(*Module); ok && ccModule.typ() == fullLibrary && !android.GetBp2BuildAllowList().GenerateCcLibraryStaticOnly(m.Name()) {
Liz Kammer35ca77e2021-12-22 15:31:40 -05001151 label += "_bp2build_cc_library_static"
Chris Parsons953b3562021-09-20 15:14:39 -04001152 }
1153 return label
1154}
1155
Jingwen Chen55bc8202021-11-02 06:40:51 +00001156func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001157 // cc_library, at it's root name, propagates the shared library, which depends on the static
1158 // library.
1159 return android.BazelModuleLabel(ctx, m)
1160}
1161
Jingwen Chen55bc8202021-11-02 06:40:51 +00001162func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001163 label := bazelLabelForStaticModule(ctx, m)
1164 if aModule, ok := m.(android.Module); ok {
1165 if android.IsModulePrebuilt(aModule) {
1166 label += "_alwayslink"
1167 }
1168 }
1169 return label
1170}
1171
Jingwen Chen55bc8202021-11-02 06:40:51 +00001172func bazelLabelForWholeDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001173 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps)
1174}
1175
Jingwen Chen55bc8202021-11-02 06:40:51 +00001176func bazelLabelForWholeDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001177 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps)
1178}
1179
Jingwen Chen55bc8202021-11-02 06:40:51 +00001180func bazelLabelForStaticDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001181 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule)
1182}
1183
Jingwen Chen55bc8202021-11-02 06:40:51 +00001184func bazelLabelForStaticDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001185 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule)
1186}
1187
Jingwen Chen55bc8202021-11-02 06:40:51 +00001188func bazelLabelForSharedDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001189 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule)
1190}
1191
Jingwen Chen55bc8202021-11-02 06:40:51 +00001192func bazelLabelForHeaderDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001193 // This is not elegant, but bp2build's shared library targets only propagate
1194 // their header information as part of the normal C++ provider.
1195 return bazelLabelForSharedDeps(ctx, modules)
1196}
1197
Jingwen Chen55bc8202021-11-02 06:40:51 +00001198func bazelLabelForSharedDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001199 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
1200}
Liz Kammer2b8004b2021-10-04 13:55:44 -04001201
1202type binaryLinkerAttrs struct {
1203 Linkshared *bool
1204}
1205
Jingwen Chen55bc8202021-11-02 06:40:51 +00001206func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs {
Liz Kammer2b8004b2021-10-04 13:55:44 -04001207 attrs := binaryLinkerAttrs{}
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001208 bp2BuildPropParseHelper(ctx, m, &BinaryLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1209 linkerProps := props.(*BinaryLinkerProperties)
1210 staticExecutable := linkerProps.Static_executable
1211 if axis == bazel.NoConfigAxis {
1212 if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared {
1213 attrs.Linkshared = &linkBinaryShared
Liz Kammer2b8004b2021-10-04 13:55:44 -04001214 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001215 } else if staticExecutable != nil {
1216 // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a
1217 // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling
1218 ctx.ModuleErrorf("bp2build cannot migrate a module with arch/target-specific static_executable values")
Liz Kammer2b8004b2021-10-04 13:55:44 -04001219 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001220 })
Liz Kammer2b8004b2021-10-04 13:55:44 -04001221
1222 return attrs
1223}