blob: 61a55ee6964bc61fdaf737f1288543307af84dbf [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
Chris Parsons51f8c392021-08-03 21:01:05 -040055
56 System_dynamic_deps bazel.LabelListAttribute
Chris Parsons58852a02021-12-09 18:10:18 -050057
58 Enabled bazel.BoolAttribute
Yu Liufc603162022-03-01 15:44:08 -080059
Yu Liu8d82ac52022-05-17 15:13:28 -070060 Native_coverage bazel.BoolAttribute
61
Yu Liufc603162022-03-01 15:44:08 -080062 sdkAttributes
Jingwen Chen53681ef2021-04-29 08:15:13 +000063}
64
Sam Delmericoc7681022022-02-04 21:01:20 +000065// groupSrcsByExtension partitions `srcs` into groups based on file extension.
Jingwen Chen55bc8202021-11-02 06:40:51 +000066func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute {
Liz Kammer57e2e7a2021-09-20 12:55:02 -040067 // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl
68 // macro.
69 addSuffixForFilegroup := func(suffix string) bazel.LabelMapper {
Liz Kammer12615db2021-09-28 09:19:17 -040070 return func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) {
71 m, exists := ctx.ModuleFromName(label.OriginalModuleName)
72 labelStr := label.Label
Sam Delmericoc7681022022-02-04 21:01:20 +000073 if !exists || !android.IsFilegroup(ctx, m) {
Liz Kammer12615db2021-09-28 09:19:17 -040074 return labelStr, false
Jingwen Chen14a8bda2021-06-02 11:10:02 +000075 }
Liz Kammer12615db2021-09-28 09:19:17 -040076 return labelStr + suffix, true
Chris Parsons5a34ffb2021-07-21 14:34:58 -040077 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +000078 }
79
Liz Kammer57e2e7a2021-09-20 12:55:02 -040080 // TODO(b/190006308): Handle language detection of sources in a Bazel rule.
Sam Delmericoc7681022022-02-04 21:01:20 +000081 labels := bazel.LabelPartitions{
82 protoSrcPartition: android.ProtoSrcLabelPartition,
Liz Kammeraabfb5d2021-12-08 15:25:06 -050083 cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")},
84 asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")},
Cole Faust7071a052022-07-29 15:58:33 -070085 asmSrcPartition: bazel.LabelPartition{Extensions: []string{".asm"}},
Trevor Radcliffeef9c9002022-05-13 20:55:35 +000086 // TODO(http://b/231968910): If there is ever a filegroup target that
87 // contains .l or .ll files we will need to find a way to add a
88 // LabelMapper for these that identifies these filegroups and
89 // converts them appropriately
90 lSrcPartition: bazel.LabelPartition{Extensions: []string{".l"}},
91 llSrcPartition: bazel.LabelPartition{Extensions: []string{".ll"}},
Liz Kammer57e2e7a2021-09-20 12:55:02 -040092 // C++ is the "catch-all" group, and comprises generated sources because we don't
93 // know the language of these sources until the genrule is executed.
Liz Kammeraabfb5d2021-12-08 15:25:06 -050094 cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true},
Sam Delmericoc7681022022-02-04 21:01:20 +000095 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +000096
Sam Delmericoc7681022022-02-04 21:01:20 +000097 return bazel.PartitionLabelListAttribute(ctx, &srcs, labels)
Jingwen Chen14a8bda2021-06-02 11:10:02 +000098}
99
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000100// bp2BuildParseLibProps returns the attributes for a variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000101func bp2BuildParseLibProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000102 lib, ok := module.compiler.(*libraryDecorator)
103 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400104 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000105 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000106 return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic)
107}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000108
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000109// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000110func bp2BuildParseSharedProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000111 return bp2BuildParseLibProps(ctx, module, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000112}
113
114// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000115func bp2BuildParseStaticProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000116 return bp2BuildParseLibProps(ctx, module, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400117}
118
Liz Kammer7a210ac2021-09-22 15:52:58 -0400119type depsPartition struct {
120 export bazel.LabelList
121 implementation bazel.LabelList
122}
123
Jingwen Chen55bc8202021-11-02 06:40:51 +0000124type bazelLabelForDepsFn func(android.BazelConversionPathContext, []string) bazel.LabelList
Liz Kammer7a210ac2021-09-22 15:52:58 -0400125
Jingwen Chen55bc8202021-11-02 06:40:51 +0000126func maybePartitionExportedAndImplementationsDeps(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, exportedDeps []string, fn bazelLabelForDepsFn) depsPartition {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400127 if !exportsDeps {
128 return depsPartition{
129 implementation: fn(ctx, allDeps),
130 }
131 }
132
Liz Kammer7a210ac2021-09-22 15:52:58 -0400133 implementation, export := android.FilterList(allDeps, exportedDeps)
134
135 return depsPartition{
136 export: fn(ctx, export),
137 implementation: fn(ctx, implementation),
138 }
139}
140
Jingwen Chen55bc8202021-11-02 06:40:51 +0000141type bazelLabelForDepsExcludesFn func(android.BazelConversionPathContext, []string, []string) bazel.LabelList
Liz Kammer7a210ac2021-09-22 15:52:58 -0400142
Jingwen Chen55bc8202021-11-02 06:40:51 +0000143func maybePartitionExportedAndImplementationsDepsExcludes(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, excludes, exportedDeps []string, fn bazelLabelForDepsExcludesFn) depsPartition {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400144 if !exportsDeps {
145 return depsPartition{
146 implementation: fn(ctx, allDeps, excludes),
147 }
148 }
Liz Kammer7a210ac2021-09-22 15:52:58 -0400149 implementation, export := android.FilterList(allDeps, exportedDeps)
150
151 return depsPartition{
152 export: fn(ctx, export, excludes),
153 implementation: fn(ctx, implementation, excludes),
154 }
155}
156
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000157// Parses properties common to static and shared libraries. Also used for prebuilt libraries.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000158func bp2buildParseStaticOrSharedProps(ctx android.BazelConversionPathContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes {
Liz Kammer135bf552021-08-11 10:46:06 -0400159 attrs := staticOrSharedAttributes{}
Jingwen Chenbcf53042021-05-26 04:42:42 +0000160
Liz Kammer9abd62d2021-05-21 08:37:59 -0400161 setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
Alix1be00d42022-05-16 22:56:04 +0000162 attrs.Copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, true, filterOutStdFlag))
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000163 attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
Chris Parsons953b3562021-09-20 15:14:39 -0400164 attrs.System_dynamic_deps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, props.System_shared_libs))
Liz Kammer7a210ac2021-09-22 15:52:58 -0400165
Liz Kammer2b8004b2021-10-04 13:55:44 -0400166 staticDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Static_libs, props.Export_static_lib_headers, bazelLabelForStaticDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400167 attrs.Deps.SetSelectValue(axis, config, staticDeps.export)
168 attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation)
169
Liz Kammer2b8004b2021-10-04 13:55:44 -0400170 sharedDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400171 attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export)
172 attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation)
173
174 attrs.Whole_archive_deps.SetSelectValue(axis, config, bazelLabelForWholeDeps(ctx, props.Whole_static_libs))
Chris Parsons58852a02021-12-09 18:10:18 -0500175 attrs.Enabled.SetSelectValue(axis, config, props.Enabled)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000176 }
Liz Kammer135bf552021-08-11 10:46:06 -0400177 // system_dynamic_deps distinguishes between nil/empty list behavior:
178 // nil -> use default values
179 // empty list -> no values specified
180 attrs.System_dynamic_deps.ForceSpecifyEmptyList = true
Jingwen Chenbcf53042021-05-26 04:42:42 +0000181
182 if isStatic {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000183 bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
184 if staticOrSharedProps, ok := props.(*StaticProperties); ok {
185 setAttrs(axis, config, staticOrSharedProps.Static)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000186 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000187 })
Jingwen Chenbcf53042021-05-26 04:42:42 +0000188 } else {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000189 bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
190 if staticOrSharedProps, ok := props.(*SharedProperties); ok {
191 setAttrs(axis, config, staticOrSharedProps.Shared)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000192 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000193 })
Jingwen Chenbcf53042021-05-26 04:42:42 +0000194 }
195
Liz Kammerae3994e2021-10-19 09:45:48 -0400196 partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs)
197 attrs.Srcs = partitionedSrcs[cppSrcPartition]
198 attrs.Srcs_c = partitionedSrcs[cSrcPartition]
199 attrs.Srcs_as = partitionedSrcs[asSrcPartition]
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000200
Liz Kammer12615db2021-09-28 09:19:17 -0400201 if !partitionedSrcs[protoSrcPartition].IsEmpty() {
202 // TODO(b/208815215): determine whether this is used and add support if necessary
203 ctx.ModuleErrorf("Migrating static/shared only proto srcs is not currently supported")
204 }
205
Jingwen Chenbcf53042021-05-26 04:42:42 +0000206 return attrs
Jingwen Chen53681ef2021-04-29 08:15:13 +0000207}
208
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400209// Convenience struct to hold all attributes parsed from prebuilt properties.
210type prebuiltAttributes struct {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000211 Src bazel.LabelAttribute
212 Enabled bazel.BoolAttribute
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400213}
214
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000215// NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000216func Bp2BuildParsePrebuiltLibraryProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) prebuiltAttributes {
217 manySourceFileError := func(axis bazel.ConfigurationAxis, config string) {
218 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most one source file for %s %s\n", axis, config)
219 }
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400220 var srcLabelAttribute bazel.LabelAttribute
221
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000222 parseSrcs := func(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, srcs []string) {
223 if len(srcs) > 1 {
224 manySourceFileError(axis, config)
225 return
226 } else if len(srcs) == 0 {
227 return
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400228 }
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000229 if srcLabelAttribute.SelectValue(axis, config) != nil {
230 manySourceFileError(axis, config)
231 return
232 }
233
234 src := android.BazelLabelForModuleSrcSingle(ctx, srcs[0])
235 srcLabelAttribute.SetSelectValue(axis, config, src)
236 }
237
238 bp2BuildPropParseHelper(ctx, module, &prebuiltLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
239 if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok {
240 parseSrcs(ctx, axis, config, prebuiltLinkerProperties.Srcs)
241 }
242 })
243
244 var enabledLabelAttribute bazel.BoolAttribute
245 parseAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
246 if props.Enabled != nil {
247 enabledLabelAttribute.SetSelectValue(axis, config, props.Enabled)
248 }
249 parseSrcs(ctx, axis, config, props.Srcs)
250 }
251
252 if isStatic {
253 bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
254 if staticProperties, ok := props.(*StaticProperties); ok {
255 parseAttrs(axis, config, staticProperties.Static)
256 }
257 })
258 } else {
259 bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
260 if sharedProperties, ok := props.(*SharedProperties); ok {
261 parseAttrs(axis, config, sharedProperties.Shared)
262 }
263 })
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400264 }
265
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400266 return prebuiltAttributes{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000267 Src: srcLabelAttribute,
268 Enabled: enabledLabelAttribute,
269 }
270}
271
272func bp2BuildPropParseHelper(ctx android.ArchVariantContext, module *Module, propsType interface{}, parseFunc func(axis bazel.ConfigurationAxis, config string, props interface{})) {
273 for axis, configToProps := range module.GetArchVariantProperties(ctx, propsType) {
274 for config, props := range configToProps {
275 parseFunc(axis, config, props)
276 }
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400277 }
278}
279
Liz Kammere6583482021-10-19 13:56:10 -0400280type baseAttributes struct {
281 compilerAttributes
282 linkerAttributes
Liz Kammer12615db2021-09-28 09:19:17 -0400283
284 protoDependency *bazel.LabelAttribute
Liz Kammere6583482021-10-19 13:56:10 -0400285}
286
Jingwen Chen107c0de2021-04-09 10:43:12 +0000287// Convenience struct to hold all attributes parsed from compiler properties.
288type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400289 // Options for all languages
290 copts bazel.StringListAttribute
291 // Assembly options and sources
292 asFlags bazel.StringListAttribute
293 asSrcs bazel.LabelListAttribute
Cole Faust7071a052022-07-29 15:58:33 -0700294 asmSrcs bazel.LabelListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400295 // C options and sources
296 conlyFlags bazel.StringListAttribute
297 cSrcs bazel.LabelListAttribute
298 // C++ options and sources
299 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000300 srcs bazel.LabelListAttribute
Chris Parsons2c788392021-08-10 11:58:07 -0400301
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000302 // Lex sources and options
303 lSrcs bazel.LabelListAttribute
304 llSrcs bazel.LabelListAttribute
305 lexopts bazel.StringListAttribute
306
Liz Kammere6583482021-10-19 13:56:10 -0400307 hdrs bazel.LabelListAttribute
308
Chris Parsons2c788392021-08-10 11:58:07 -0400309 rtti bazel.BoolAttribute
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000310
311 // Not affected by arch variants
312 stl *string
Chris Parsons79bd2b72021-11-29 17:52:41 -0500313 cStd *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000314 cppStd *string
Liz Kammer35687bc2021-09-10 10:07:07 -0400315
316 localIncludes bazel.StringListAttribute
317 absoluteIncludes bazel.StringListAttribute
Liz Kammer12615db2021-09-28 09:19:17 -0400318
Liz Kammer1263d9b2021-12-10 14:28:20 -0500319 includes BazelIncludes
320
Liz Kammer12615db2021-09-28 09:19:17 -0400321 protoSrcs bazel.LabelListAttribute
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000322
323 stubsSymbolFile *string
324 stubsVersions bazel.StringListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000325}
326
Liz Kammercac7f692021-12-16 14:19:32 -0500327type filterOutFn func(string) bool
328
329func filterOutStdFlag(flag string) bool {
330 return strings.HasPrefix(flag, "-std=")
331}
332
Alix1be00d42022-05-16 22:56:04 +0000333func filterOutClangUnknownCflags(flag string) bool {
334 for _, f := range config.ClangUnknownCflags {
335 if f == flag {
336 return true
337 }
338 }
339 return false
340}
341
342func parseCommandLineFlags(soongFlags []string, noCoptsTokenization bool, filterOut ...filterOutFn) []string {
Liz Kammere6583482021-10-19 13:56:10 -0400343 var result []string
344 for _, flag := range soongFlags {
Alix1be00d42022-05-16 22:56:04 +0000345 skipFlag := false
346 for _, filter := range filterOut {
347 if filter != nil && filter(flag) {
348 skipFlag = true
349 }
350 }
351 if skipFlag {
Liz Kammercac7f692021-12-16 14:19:32 -0500352 continue
353 }
Liz Kammere6583482021-10-19 13:56:10 -0400354 // Soong's cflags can contain spaces, like `-include header.h`. For
355 // Bazel's copts, split them up to be compatible with the
356 // no_copts_tokenization feature.
Alix1be00d42022-05-16 22:56:04 +0000357 if noCoptsTokenization {
358 result = append(result, strings.Split(flag, " ")...)
359 } else {
360 // Soong's Version Script and Dynamic List Properties are added as flags
361 // to Bazel's linkopts using "($location label)" syntax.
362 // Splitting on spaces would separate this into two different flags
363 // "($ location" and "label)"
364 result = append(result, flag)
365 }
Liz Kammere6583482021-10-19 13:56:10 -0400366 }
367 return result
368}
Jingwen Chened9c17d2021-04-13 07:14:55 +0000369
Jingwen Chen55bc8202021-11-02 06:40:51 +0000370func (ca *compilerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, props *BaseCompilerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400371 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
372 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
373 if srcsList, ok := parseSrcs(ctx, props); ok {
374 ca.srcs.SetSelectValue(axis, config, srcsList)
Chris Parsons990c4f42021-05-25 12:10:58 -0400375 }
376
Liz Kammere6583482021-10-19 13:56:10 -0400377 localIncludeDirs := props.Local_include_dirs
378 if axis == bazel.NoConfigAxis {
Chris Parsons79bd2b72021-11-29 17:52:41 -0500379 ca.cStd, ca.cppStd = bp2buildResolveCppStdValue(props.C_std, props.Cpp_std, props.Gnu_extensions)
Liz Kammere6583482021-10-19 13:56:10 -0400380 if includeBuildDirectory(props.Include_build_directory) {
381 localIncludeDirs = append(localIncludeDirs, ".")
Liz Kammer222bdcf2021-10-11 14:15:51 -0400382 }
Jingwen Chene32e9e02021-04-23 09:17:24 +0000383 }
384
Liz Kammere6583482021-10-19 13:56:10 -0400385 ca.absoluteIncludes.SetSelectValue(axis, config, props.Include_dirs)
386 ca.localIncludes.SetSelectValue(axis, config, localIncludeDirs)
387
Liz Kammercac7f692021-12-16 14:19:32 -0500388 // In Soong, cflags occur on the command line before -std=<val> flag, resulting in the value being
389 // overridden. In Bazel we always allow overriding, via flags; however, this can cause
390 // incompatibilities, so we remove "-std=" flags from Cflag properties while leaving it in other
391 // cases.
Alix1be00d42022-05-16 22:56:04 +0000392 ca.copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, true, filterOutStdFlag, filterOutClangUnknownCflags))
393 ca.asFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Asflags, true, nil))
394 ca.conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Conlyflags, true, filterOutClangUnknownCflags))
395 ca.cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Cppflags, true, filterOutClangUnknownCflags))
Liz Kammere6583482021-10-19 13:56:10 -0400396 ca.rtti.SetSelectValue(axis, config, props.Rtti)
397}
398
Jingwen Chen55bc8202021-11-02 06:40:51 +0000399func (ca *compilerAttributes) convertStlProps(ctx android.ArchVariantContext, module *Module) {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000400 bp2BuildPropParseHelper(ctx, module, &StlProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
401 if stlProps, ok := props.(*StlProperties); ok {
402 if stlProps.Stl == nil {
403 return
404 }
405 if ca.stl == nil {
Liz Kammer7128d382022-05-12 11:42:33 -0400406 stl := deduplicateStlInput(*stlProps.Stl)
407 ca.stl = &stl
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000408 } else if ca.stl != stlProps.Stl {
409 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 -0400410 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000411 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000412 })
Liz Kammere6583482021-10-19 13:56:10 -0400413}
Jingwen Chenc1c26502021-04-05 10:35:13 +0000414
Jingwen Chen55bc8202021-11-02 06:40:51 +0000415func (ca *compilerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Liz Kammerba7a9c52021-05-26 08:45:30 -0400416 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
Liz Kammere6583482021-10-19 13:56:10 -0400417 "Cflags": &ca.copts,
418 "Asflags": &ca.asFlags,
419 "CppFlags": &ca.cppFlags,
Liz Kammerba7a9c52021-05-26 08:45:30 -0400420 }
Liz Kammerba7a9c52021-05-26 08:45:30 -0400421 for propName, attr := range productVarPropNameToAttribute {
Jingwen Chen25825ca2021-11-15 12:28:43 +0000422 if productConfigProps, exists := productVariableProps[propName]; exists {
423 for productConfigProp, prop := range productConfigProps {
424 flags, ok := prop.([]string)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400425 if !ok {
426 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
427 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000428 newFlags, _ := bazel.TryVariableSubstitutions(flags, productConfigProp.Name)
429 attr.SetSelectValue(productConfigProp.ConfigurationAxis(), productConfigProp.SelectKey(), newFlags)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400430 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400431 }
432 }
Liz Kammere6583482021-10-19 13:56:10 -0400433}
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400434
Jingwen Chen55bc8202021-11-02 06:40:51 +0000435func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, implementationHdrs bazel.LabelListAttribute) {
Liz Kammere6583482021-10-19 13:56:10 -0400436 ca.srcs.ResolveExcludes()
437 partitionedSrcs := groupSrcsByExtension(ctx, ca.srcs)
438
Liz Kammer12615db2021-09-28 09:19:17 -0400439 ca.protoSrcs = partitionedSrcs[protoSrcPartition]
440
Liz Kammere6583482021-10-19 13:56:10 -0400441 for p, lla := range partitionedSrcs {
442 // if there are no sources, there is no need for headers
443 if lla.IsEmpty() {
444 continue
445 }
446 lla.Append(implementationHdrs)
447 partitionedSrcs[p] = lla
448 }
449
450 ca.srcs = partitionedSrcs[cppSrcPartition]
451 ca.cSrcs = partitionedSrcs[cSrcPartition]
452 ca.asSrcs = partitionedSrcs[asSrcPartition]
Cole Faust7071a052022-07-29 15:58:33 -0700453 ca.asmSrcs = partitionedSrcs[asmSrcPartition]
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000454 ca.lSrcs = partitionedSrcs[lSrcPartition]
455 ca.llSrcs = partitionedSrcs[llSrcPartition]
Liz Kammere6583482021-10-19 13:56:10 -0400456
457 ca.absoluteIncludes.DeduplicateAxesFromBase()
458 ca.localIncludes.DeduplicateAxesFromBase()
459}
460
461// Parse srcs from an arch or OS's props value.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000462func parseSrcs(ctx android.BazelConversionPathContext, props *BaseCompilerProperties) (bazel.LabelList, bool) {
Liz Kammere6583482021-10-19 13:56:10 -0400463 anySrcs := false
464 // Add srcs-like dependencies such as generated files.
465 // First create a LabelList containing these dependencies, then merge the values with srcs.
466 generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources)
467 if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 {
468 anySrcs = true
469 }
470
471 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs)
472 if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 {
473 anySrcs = true
474 }
475 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs
476}
477
Liz Kammera5a29de2022-05-25 23:19:37 -0400478func bp2buildStdVal(std *string, prefix string, useGnu bool) *string {
479 defaultVal := prefix + "_std_default"
Chris Parsons79bd2b72021-11-29 17:52:41 -0500480 // If c{,pp}std properties are not specified, don't generate them in the BUILD file.
481 // Defaults are handled by the toolchain definition.
482 // However, if gnu_extensions is false, then the default gnu-to-c version must be specified.
Liz Kammera5a29de2022-05-25 23:19:37 -0400483 stdVal := proptools.StringDefault(std, defaultVal)
484 if stdVal == "experimental" || stdVal == defaultVal {
485 if stdVal == "experimental" {
486 stdVal = prefix + "_std_experimental"
487 }
488 if !useGnu {
489 stdVal += "_no_gnu"
490 }
491 } else if !useGnu {
492 stdVal = gnuToCReplacer.Replace(stdVal)
Chris Parsons79bd2b72021-11-29 17:52:41 -0500493 }
494
Liz Kammera5a29de2022-05-25 23:19:37 -0400495 if stdVal == defaultVal {
496 return nil
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500497 }
Liz Kammera5a29de2022-05-25 23:19:37 -0400498 return &stdVal
499}
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500500
Liz Kammera5a29de2022-05-25 23:19:37 -0400501func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) {
502 useGnu := useGnuExtensions(gnu_extensions)
503
504 return bp2buildStdVal(c_std, "c", useGnu), bp2buildStdVal(cpp_std, "cpp", useGnu)
Liz Kammere6583482021-10-19 13:56:10 -0400505}
506
Liz Kammer1263d9b2021-12-10 14:28:20 -0500507// packageFromLabel extracts package from a fully-qualified or relative Label and whether the label
508// is fully-qualified.
509// e.g. fully-qualified "//a/b:foo" -> "a/b", true, relative: ":bar" -> ".", false
510func packageFromLabel(label string) (string, bool) {
511 split := strings.Split(label, ":")
512 if len(split) != 2 {
513 return "", false
514 }
515 if split[0] == "" {
516 return ".", false
517 }
518 // remove leading "//"
519 return split[0][2:], true
520}
521
522// includesFromLabelList extracts relative/absolute includes from a bazel.LabelList>
523func includesFromLabelList(labelList bazel.LabelList) (relative, absolute []string) {
524 for _, hdr := range labelList.Includes {
525 if pkg, hasPkg := packageFromLabel(hdr.Label); hasPkg {
526 absolute = append(absolute, pkg)
527 } else if pkg != "" {
528 relative = append(relative, pkg)
529 }
530 }
531 return relative, absolute
532}
533
Cole Faust7071a052022-07-29 15:58:33 -0700534type YasmAttributes struct {
535 Srcs bazel.LabelListAttribute
536 Flags bazel.StringListAttribute
537 Include_dirs bazel.StringListAttribute
538}
539
540func bp2BuildYasm(ctx android.Bp2buildMutatorContext, m *Module, ca compilerAttributes) *bazel.LabelAttribute {
541 if ca.asmSrcs.IsEmpty() {
542 return nil
543 }
544
545 // Yasm needs the include directories from both local_includes and
546 // export_include_dirs. We don't care about actually exporting them from the
547 // yasm rule though, because they will also be present on the cc_ rule that
548 // wraps this yasm rule.
549 includes := ca.localIncludes.Clone()
550 bp2BuildPropParseHelper(ctx, m, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
551 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
552 if len(flagExporterProperties.Export_include_dirs) > 0 {
553 x := bazel.StringListAttribute{}
554 x.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs)
555 includes.Append(x)
556 }
557 }
558 })
559
560 ctx.CreateBazelTargetModule(
561 bazel.BazelTargetModuleProperties{
562 Rule_class: "yasm",
563 Bzl_load_location: "//build/bazel/rules/cc:yasm.bzl",
564 },
565 android.CommonAttributes{Name: m.Name() + "_yasm"},
566 &YasmAttributes{
567 Srcs: ca.asmSrcs,
568 Flags: ca.asFlags,
569 Include_dirs: *includes,
570 })
571
572 // We only want to add a dependency on the _yasm target if there are asm
573 // sources in the current configuration. If there are unconfigured asm
574 // sources, always add the dependency. Otherwise, add the dependency only
575 // on the configuration axes and values that had asm sources.
576 if len(ca.asmSrcs.Value.Includes) > 0 {
577 return bazel.MakeLabelAttribute(":" + m.Name() + "_yasm")
578 }
579
580 ret := &bazel.LabelAttribute{}
581 for _, axis := range ca.asmSrcs.SortedConfigurationAxes() {
582 for config := range ca.asmSrcs.ConfigurableValues[axis] {
583 ret.SetSelectValue(axis, config, bazel.Label{Label: ":" + m.Name() + "_yasm"})
584 }
585 }
586 return ret
587}
588
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000589// bp2BuildParseBaseProps returns all compiler, linker, library attributes of a cc module..
Liz Kammer12615db2021-09-28 09:19:17 -0400590func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) baseAttributes {
Liz Kammere6583482021-10-19 13:56:10 -0400591 archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
592 archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{})
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000593 archVariantLibraryProperties := module.GetArchVariantProperties(ctx, &LibraryProperties{})
Liz Kammere6583482021-10-19 13:56:10 -0400594
595 var implementationHdrs bazel.LabelListAttribute
596
597 axisToConfigs := map[bazel.ConfigurationAxis]map[string]bool{}
598 allAxesAndConfigs := func(cp android.ConfigurationAxisToArchVariantProperties) {
599 for axis, configMap := range cp {
600 if _, ok := axisToConfigs[axis]; !ok {
601 axisToConfigs[axis] = map[string]bool{}
602 }
603 for config, _ := range configMap {
604 axisToConfigs[axis][config] = true
Chris Parsonsa967f252021-09-23 16:34:35 -0400605 }
606 }
607 }
Liz Kammere6583482021-10-19 13:56:10 -0400608 allAxesAndConfigs(archVariantCompilerProps)
609 allAxesAndConfigs(archVariantLinkerProps)
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000610 allAxesAndConfigs(archVariantLibraryProperties)
Chris Parsonsa967f252021-09-23 16:34:35 -0400611
Liz Kammere6583482021-10-19 13:56:10 -0400612 compilerAttrs := compilerAttributes{}
613 linkerAttrs := linkerAttributes{}
614
615 for axis, configs := range axisToConfigs {
616 for config, _ := range configs {
617 var allHdrs []string
618 if baseCompilerProps, ok := archVariantCompilerProps[axis][config].(*BaseCompilerProperties); ok {
619 allHdrs = baseCompilerProps.Generated_headers
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000620 if baseCompilerProps.Lex != nil {
621 compilerAttrs.lexopts.SetSelectValue(axis, config, baseCompilerProps.Lex.Flags)
622 }
Liz Kammere6583482021-10-19 13:56:10 -0400623 (&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, config, baseCompilerProps)
624 }
625
626 var exportHdrs []string
627
628 if baseLinkerProps, ok := archVariantLinkerProps[axis][config].(*BaseLinkerProperties); ok {
629 exportHdrs = baseLinkerProps.Export_generated_headers
630
631 (&linkerAttrs).bp2buildForAxisAndConfig(ctx, module.Binary(), axis, config, baseLinkerProps)
632 }
633 headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportHdrs, android.BazelLabelForModuleDeps)
634 implementationHdrs.SetSelectValue(axis, config, headers.implementation)
635 compilerAttrs.hdrs.SetSelectValue(axis, config, headers.export)
Liz Kammer1263d9b2021-12-10 14:28:20 -0500636
637 exportIncludes, exportAbsoluteIncludes := includesFromLabelList(headers.export)
638 compilerAttrs.includes.Includes.SetSelectValue(axis, config, exportIncludes)
639 compilerAttrs.includes.AbsoluteIncludes.SetSelectValue(axis, config, exportAbsoluteIncludes)
640
641 includes, absoluteIncludes := includesFromLabelList(headers.implementation)
642 currAbsoluteIncludes := compilerAttrs.absoluteIncludes.SelectValue(axis, config)
643 currAbsoluteIncludes = android.FirstUniqueStrings(append(currAbsoluteIncludes, absoluteIncludes...))
644 compilerAttrs.absoluteIncludes.SetSelectValue(axis, config, currAbsoluteIncludes)
645 currIncludes := compilerAttrs.localIncludes.SelectValue(axis, config)
646 currIncludes = android.FirstUniqueStrings(append(currIncludes, includes...))
647 compilerAttrs.localIncludes.SetSelectValue(axis, config, currIncludes)
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000648
649 if libraryProps, ok := archVariantLibraryProperties[axis][config].(*LibraryProperties); ok {
650 if axis == bazel.NoConfigAxis {
651 compilerAttrs.stubsSymbolFile = libraryProps.Stubs.Symbol_file
652 compilerAttrs.stubsVersions.SetSelectValue(axis, config, libraryProps.Stubs.Versions)
653 }
654 }
Liz Kammere6583482021-10-19 13:56:10 -0400655 }
656 }
Liz Kammere6583482021-10-19 13:56:10 -0400657 compilerAttrs.convertStlProps(ctx, module)
658 (&linkerAttrs).convertStripProps(ctx, module)
659
Yu Liu8d82ac52022-05-17 15:13:28 -0700660 if module.coverage != nil && module.coverage.Properties.Native_coverage != nil &&
661 !Bool(module.coverage.Properties.Native_coverage) {
662 // Native_coverage is arch neutral
663 (&linkerAttrs).features.Append(bazel.MakeStringListAttribute([]string{"-coverage"}))
664 }
665
Liz Kammere6583482021-10-19 13:56:10 -0400666 productVariableProps := android.ProductVariableProperties(ctx)
667
668 (&compilerAttrs).convertProductVariables(ctx, productVariableProps)
669 (&linkerAttrs).convertProductVariables(ctx, productVariableProps)
670
671 (&compilerAttrs).finalize(ctx, implementationHdrs)
Liz Kammer54309532021-12-14 12:21:22 -0500672 (&linkerAttrs).finalize(ctx)
Liz Kammere6583482021-10-19 13:56:10 -0400673
Cole Faust7071a052022-07-29 15:58:33 -0700674 (&compilerAttrs.srcs).Add(bp2BuildYasm(ctx, module, compilerAttrs))
675
Liz Kammer12615db2021-09-28 09:19:17 -0400676 protoDep := bp2buildProto(ctx, module, compilerAttrs.protoSrcs)
677
678 // bp2buildProto will only set wholeStaticLib or implementationWholeStaticLib, but we don't know
679 // which. This will add the newly generated proto library to the appropriate attribute and nothing
680 // to the other
681 (&linkerAttrs).wholeArchiveDeps.Add(protoDep.wholeStaticLib)
682 (&linkerAttrs).implementationWholeArchiveDeps.Add(protoDep.implementationWholeStaticLib)
683
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000684 convertedLSrcs := bp2BuildLex(ctx, module.Name(), compilerAttrs)
685 (&compilerAttrs).srcs.Add(&convertedLSrcs.srcName)
686 (&compilerAttrs).cSrcs.Add(&convertedLSrcs.cSrcName)
687
Liz Kammere6583482021-10-19 13:56:10 -0400688 return baseAttributes{
689 compilerAttrs,
690 linkerAttrs,
Liz Kammer12615db2021-09-28 09:19:17 -0400691 protoDep.protoDep,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000692 }
693}
694
Yu Liufc603162022-03-01 15:44:08 -0800695func bp2BuildParseSdkAttributes(module *Module) sdkAttributes {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000696 return sdkAttributes{
697 Sdk_version: module.Properties.Sdk_version,
Yu Liufc603162022-03-01 15:44:08 -0800698 Min_sdk_version: module.Properties.Min_sdk_version,
699 }
700}
701
702type sdkAttributes struct {
703 Sdk_version *string
704 Min_sdk_version *string
705}
706
Jingwen Chen107c0de2021-04-09 10:43:12 +0000707// Convenience struct to hold all attributes parsed from linker properties.
708type linkerAttributes struct {
Liz Kammer54309532021-12-14 12:21:22 -0500709 deps bazel.LabelListAttribute
710 implementationDeps bazel.LabelListAttribute
711 dynamicDeps bazel.LabelListAttribute
712 implementationDynamicDeps bazel.LabelListAttribute
713 wholeArchiveDeps bazel.LabelListAttribute
714 implementationWholeArchiveDeps bazel.LabelListAttribute
715 systemDynamicDeps bazel.LabelListAttribute
716 usedSystemDynamicDepAsDynamicDep map[string]bool
Liz Kammer7a210ac2021-09-22 15:52:58 -0400717
Jingwen Chen6ada5892021-09-17 11:38:09 +0000718 linkCrt bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000719 useLibcrt bazel.BoolAttribute
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500720 useVersionLib bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000721 linkopts bazel.StringListAttribute
Liz Kammerd2871182021-10-04 13:54:37 -0400722 additionalLinkerInputs bazel.LabelListAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000723 stripKeepSymbols bazel.BoolAttribute
724 stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
725 stripKeepSymbolsList bazel.StringListAttribute
726 stripAll bazel.BoolAttribute
727 stripNone bazel.BoolAttribute
Liz Kammer0eae52e2021-10-06 10:32:26 -0400728 features bazel.StringListAttribute
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400729}
730
Liz Kammer54309532021-12-14 12:21:22 -0500731var (
732 soongSystemSharedLibs = []string{"libc", "libm", "libdl"}
733)
734
Jingwen Chen55bc8202021-11-02 06:40:51 +0000735func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, isBinary bool, axis bazel.ConfigurationAxis, config string, props *BaseLinkerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400736 // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module
737 var axisFeatures []string
Liz Kammer7a210ac2021-09-22 15:52:58 -0400738
Liz Kammercc2c1ef2022-03-21 09:03:29 -0400739 wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs)
740 la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs))
Liz Kammere6583482021-10-19 13:56:10 -0400741 // Excludes to parallel Soong:
742 // 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 -0400743 staticLibs := android.FirstUniqueStrings(android.RemoveListFromList(props.Static_libs, wholeStaticLibs))
744
Liz Kammere6583482021-10-19 13:56:10 -0400745 staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, staticLibs, props.Exclude_static_libs, props.Export_static_lib_headers, bazelLabelForStaticDepsExcludes)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400746
Liz Kammere6583482021-10-19 13:56:10 -0400747 headerLibs := android.FirstUniqueStrings(props.Header_libs)
748 hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps)
Jingwen Chen63930982021-03-24 10:04:33 -0400749
Liz Kammere6583482021-10-19 13:56:10 -0400750 (&hDeps.export).Append(staticDeps.export)
751 la.deps.SetSelectValue(axis, config, hDeps.export)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000752
Liz Kammere6583482021-10-19 13:56:10 -0400753 (&hDeps.implementation).Append(staticDeps.implementation)
754 la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation)
Liz Kammer0eae52e2021-10-06 10:32:26 -0400755
Liz Kammere6583482021-10-19 13:56:10 -0400756 systemSharedLibs := props.System_shared_libs
757 // systemSharedLibs distinguishes between nil/empty list behavior:
758 // nil -> use default values
759 // empty list -> no values specified
760 if len(systemSharedLibs) > 0 {
761 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
762 }
763 la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
764
765 sharedLibs := android.FirstUniqueStrings(props.Shared_libs)
Liz Kammer54309532021-12-14 12:21:22 -0500766 excludeSharedLibs := props.Exclude_shared_libs
767 usedSystem := android.FilterListPred(sharedLibs, func(s string) bool {
768 return android.InList(s, soongSystemSharedLibs) && !android.InList(s, excludeSharedLibs)
769 })
770 for _, el := range usedSystem {
771 if la.usedSystemDynamicDepAsDynamicDep == nil {
772 la.usedSystemDynamicDepAsDynamicDep = map[string]bool{}
773 }
774 la.usedSystemDynamicDepAsDynamicDep[el] = true
775 }
776
Liz Kammere6583482021-10-19 13:56:10 -0400777 sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, sharedLibs, props.Exclude_shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes)
778 la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export)
779 la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation)
780
781 if !BoolDefault(props.Pack_relocations, packRelocationsDefault) {
782 axisFeatures = append(axisFeatures, "disable_pack_relocations")
783 }
784
785 if Bool(props.Allow_undefined_symbols) {
786 axisFeatures = append(axisFeatures, "-no_undefined_symbols")
787 }
788
789 var linkerFlags []string
790 if len(props.Ldflags) > 0 {
Liz Kammerf38a8372022-02-04 15:39:00 -0500791 linkerFlags = append(linkerFlags, proptools.NinjaEscapeList(props.Ldflags)...)
Liz Kammere6583482021-10-19 13:56:10 -0400792 // binaries remove static flag if -shared is in the linker flags
793 if isBinary && android.InList("-shared", linkerFlags) {
794 axisFeatures = append(axisFeatures, "-static_flag")
795 }
796 }
797 if props.Version_script != nil {
798 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script)
799 la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}})
800 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label))
801 }
Alix773adaa2022-04-27 17:49:34 +0000802
803 if props.Dynamic_list != nil {
804 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Dynamic_list)
805 la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}})
806 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--dynamic-list,$(location %s)", label.Label))
807 }
808
Alix1be00d42022-05-16 22:56:04 +0000809 la.linkopts.SetSelectValue(axis, config, parseCommandLineFlags(linkerFlags, false, filterOutClangUnknownCflags))
Liz Kammere6583482021-10-19 13:56:10 -0400810 la.useLibcrt.SetSelectValue(axis, config, props.libCrt())
811
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500812 if axis == bazel.NoConfigAxis {
813 la.useVersionLib.SetSelectValue(axis, config, props.Use_version_lib)
814 }
815
Liz Kammere6583482021-10-19 13:56:10 -0400816 // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it.
817 if props.crt() != nil {
818 if axis == bazel.NoConfigAxis {
819 la.linkCrt.SetSelectValue(axis, config, props.crt())
820 } else if axis == bazel.ArchConfigurationAxis {
821 ctx.ModuleErrorf("nocrt is not supported for arch variants")
822 }
823 }
824
825 if axisFeatures != nil {
826 la.features.SetSelectValue(axis, config, axisFeatures)
827 }
828}
829
Jingwen Chen55bc8202021-11-02 06:40:51 +0000830func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000831 bp2BuildPropParseHelper(ctx, module, &StripProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
832 if stripProperties, ok := props.(*StripProperties); ok {
833 la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols)
834 la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list)
835 la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame)
836 la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All)
837 la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000838 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000839 })
Liz Kammere6583482021-10-19 13:56:10 -0400840}
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000841
Jingwen Chen55bc8202021-11-02 06:40:51 +0000842func (la *linkerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Jingwen Chen6ada5892021-09-17 11:38:09 +0000843
Liz Kammer47535c52021-06-02 16:02:22 -0400844 type productVarDep struct {
845 // the name of the corresponding excludes field, if one exists
846 excludesField string
847 // reference to the bazel attribute that should be set for the given product variable config
848 attribute *bazel.LabelListAttribute
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400849
Jingwen Chen55bc8202021-11-02 06:40:51 +0000850 depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList
Liz Kammer47535c52021-06-02 16:02:22 -0400851 }
852
853 productVarToDepFields := map[string]productVarDep{
854 // product variables do not support exclude_shared_libs
Jingwen Chen55bc8202021-11-02 06:40:51 +0000855 "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes},
856 "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes},
857 "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes},
Liz Kammer47535c52021-06-02 16:02:22 -0400858 }
859
Liz Kammer47535c52021-06-02 16:02:22 -0400860 for name, dep := range productVarToDepFields {
861 props, exists := productVariableProps[name]
862 excludeProps, excludesExists := productVariableProps[dep.excludesField]
863 // if neither an include or excludes property exists, then skip it
864 if !exists && !excludesExists {
865 continue
866 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000867 // Collect all the configurations that an include or exclude property exists for.
868 // We want to iterate all configurations rather than either the include or exclude because, for a
869 // particular configuration, we may have either only an include or an exclude to handle.
870 productConfigProps := make(map[android.ProductConfigProperty]bool, len(props)+len(excludeProps))
871 for p := range props {
872 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -0400873 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000874 for p := range excludeProps {
875 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -0400876 }
877
Jingwen Chen25825ca2021-11-15 12:28:43 +0000878 for productConfigProp := range productConfigProps {
879 prop, includesExists := props[productConfigProp]
880 excludesProp, excludesExists := excludeProps[productConfigProp]
Liz Kammer47535c52021-06-02 16:02:22 -0400881 var includes, excludes []string
882 var ok bool
883 // if there was no includes/excludes property, casting fails and that's expected
Jingwen Chen25825ca2021-11-15 12:28:43 +0000884 if includes, ok = prop.([]string); includesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -0400885 ctx.ModuleErrorf("Could not convert product variable %s property", name)
886 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000887 if excludes, ok = excludesProp.([]string); excludesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -0400888 ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
889 }
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400890
Jingwen Chen58ff6802021-11-17 12:14:41 +0000891 dep.attribute.EmitEmptyList = productConfigProp.AlwaysEmit()
Jingwen Chen25825ca2021-11-15 12:28:43 +0000892 dep.attribute.SetSelectValue(
893 productConfigProp.ConfigurationAxis(),
894 productConfigProp.SelectKey(),
895 dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes),
896 )
Liz Kammer47535c52021-06-02 16:02:22 -0400897 }
898 }
Liz Kammere6583482021-10-19 13:56:10 -0400899}
Liz Kammer47535c52021-06-02 16:02:22 -0400900
Liz Kammer54309532021-12-14 12:21:22 -0500901func (la *linkerAttributes) finalize(ctx android.BazelConversionPathContext) {
902 // if system dynamic deps have the default value, any use of a system dynamic library used will
903 // result in duplicate library errors for bionic OSes. Here, we explicitly exclude those libraries
904 // from bionic OSes.
905 if la.systemDynamicDeps.IsNil() && len(la.usedSystemDynamicDepAsDynamicDep) > 0 {
906 toRemove := bazelLabelForSharedDeps(ctx, android.SortedStringKeys(la.usedSystemDynamicDepAsDynamicDep))
907 la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove)
908 la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove)
909 la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove)
910 la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove)
911 }
912
Liz Kammere6583482021-10-19 13:56:10 -0400913 la.deps.ResolveExcludes()
914 la.implementationDeps.ResolveExcludes()
915 la.dynamicDeps.ResolveExcludes()
916 la.implementationDynamicDeps.ResolveExcludes()
917 la.wholeArchiveDeps.ResolveExcludes()
918 la.systemDynamicDeps.ForceSpecifyEmptyList = true
Liz Kammer54309532021-12-14 12:21:22 -0500919
Jingwen Chen91220d72021-03-24 02:18:33 -0400920}
921
Jingwen Chened9c17d2021-04-13 07:14:55 +0000922// Relativize a list of root-relative paths with respect to the module's
923// directory.
924//
925// include_dirs Soong prop are root-relative (b/183742505), but
926// local_include_dirs, export_include_dirs and export_system_include_dirs are
927// module dir relative. This function makes a list of paths entirely module dir
928// relative.
929//
930// For the `include` attribute, Bazel wants the paths to be relative to the
931// module.
932func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000933 var relativePaths []string
934 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000935 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
936 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
937 if err != nil {
938 panic(err)
939 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000940 relativePaths = append(relativePaths, relativePath)
941 }
942 return relativePaths
943}
944
Liz Kammer5fad5012021-09-09 14:08:21 -0400945// BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel
946// attributes.
947type BazelIncludes struct {
Liz Kammer1263d9b2021-12-10 14:28:20 -0500948 AbsoluteIncludes bazel.StringListAttribute
949 Includes bazel.StringListAttribute
950 SystemIncludes bazel.StringListAttribute
Liz Kammer5fad5012021-09-09 14:08:21 -0400951}
952
Liz Kammer54549442022-05-11 13:55:06 -0400953func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module, includes *BazelIncludes) BazelIncludes {
Liz Kammer1263d9b2021-12-10 14:28:20 -0500954 var exported BazelIncludes
955 if includes != nil {
956 exported = *includes
957 } else {
958 exported = BazelIncludes{}
959 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000960 bp2BuildPropParseHelper(ctx, module, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
961 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
962 if len(flagExporterProperties.Export_include_dirs) > 0 {
963 exported.Includes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.Includes.SelectValue(axis, config), flagExporterProperties.Export_include_dirs...)))
964 }
965 if len(flagExporterProperties.Export_system_include_dirs) > 0 {
966 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 -0400967 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400968 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000969 })
Liz Kammer1263d9b2021-12-10 14:28:20 -0500970 exported.AbsoluteIncludes.DeduplicateAxesFromBase()
Liz Kammer5fad5012021-09-09 14:08:21 -0400971 exported.Includes.DeduplicateAxesFromBase()
972 exported.SystemIncludes.DeduplicateAxesFromBase()
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400973
Liz Kammer5fad5012021-09-09 14:08:21 -0400974 return exported
Jingwen Chen91220d72021-03-24 02:18:33 -0400975}
Chris Parsons953b3562021-09-20 15:14:39 -0400976
Jingwen Chen55bc8202021-11-02 06:40:51 +0000977func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400978 label := android.BazelModuleLabel(ctx, m)
Liz Kammer35ca77e2021-12-22 15:31:40 -0500979 if ccModule, ok := m.(*Module); ok && ccModule.typ() == fullLibrary && !android.GenerateCcLibraryStaticOnly(m.Name()) {
980 label += "_bp2build_cc_library_static"
Chris Parsons953b3562021-09-20 15:14:39 -0400981 }
982 return label
983}
984
Jingwen Chen55bc8202021-11-02 06:40:51 +0000985func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400986 // cc_library, at it's root name, propagates the shared library, which depends on the static
987 // library.
988 return android.BazelModuleLabel(ctx, m)
989}
990
Jingwen Chen55bc8202021-11-02 06:40:51 +0000991func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400992 label := bazelLabelForStaticModule(ctx, m)
993 if aModule, ok := m.(android.Module); ok {
994 if android.IsModulePrebuilt(aModule) {
995 label += "_alwayslink"
996 }
997 }
998 return label
999}
1000
Jingwen Chen55bc8202021-11-02 06:40:51 +00001001func bazelLabelForWholeDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001002 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps)
1003}
1004
Jingwen Chen55bc8202021-11-02 06:40:51 +00001005func bazelLabelForWholeDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001006 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps)
1007}
1008
Jingwen Chen55bc8202021-11-02 06:40:51 +00001009func bazelLabelForStaticDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001010 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule)
1011}
1012
Jingwen Chen55bc8202021-11-02 06:40:51 +00001013func bazelLabelForStaticDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001014 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule)
1015}
1016
Jingwen Chen55bc8202021-11-02 06:40:51 +00001017func bazelLabelForSharedDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001018 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule)
1019}
1020
Jingwen Chen55bc8202021-11-02 06:40:51 +00001021func bazelLabelForHeaderDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001022 // This is not elegant, but bp2build's shared library targets only propagate
1023 // their header information as part of the normal C++ provider.
1024 return bazelLabelForSharedDeps(ctx, modules)
1025}
1026
Jingwen Chen55bc8202021-11-02 06:40:51 +00001027func bazelLabelForSharedDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001028 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
1029}
Liz Kammer2b8004b2021-10-04 13:55:44 -04001030
1031type binaryLinkerAttrs struct {
1032 Linkshared *bool
1033}
1034
Jingwen Chen55bc8202021-11-02 06:40:51 +00001035func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs {
Liz Kammer2b8004b2021-10-04 13:55:44 -04001036 attrs := binaryLinkerAttrs{}
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001037 bp2BuildPropParseHelper(ctx, m, &BinaryLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1038 linkerProps := props.(*BinaryLinkerProperties)
1039 staticExecutable := linkerProps.Static_executable
1040 if axis == bazel.NoConfigAxis {
1041 if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared {
1042 attrs.Linkshared = &linkBinaryShared
Liz Kammer2b8004b2021-10-04 13:55:44 -04001043 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001044 } else if staticExecutable != nil {
1045 // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a
1046 // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling
1047 ctx.ModuleErrorf("bp2build cannot migrate a module with arch/target-specific static_executable values")
Liz Kammer2b8004b2021-10-04 13:55:44 -04001048 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001049 })
Liz Kammer2b8004b2021-10-04 13:55:44 -04001050
1051 return attrs
1052}