blob: 03d9de85c83c7b1c03a406d6b900b49fabd42763 [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 (
Trevor Radcliffecee4e052022-09-06 19:31:25 +000031 cSrcPartition = "c"
32 asSrcPartition = "as"
33 asmSrcPartition = "asm"
34 lSrcPartition = "l"
35 llSrcPartition = "ll"
36 cppSrcPartition = "cpp"
37 protoSrcPartition = "proto"
38 aidlSrcPartition = "aidl"
39 syspropSrcPartition = "sysprop"
Liz Kammer91487d42022-09-13 11:27:11 -040040
41 stubsSuffix = "_stub_libs_current"
Liz Kammerae3994e2021-10-19 09:45:48 -040042)
43
Liz Kammer2222c6b2021-05-24 15:41:47 -040044// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
Jingwen Chenbcf53042021-05-26 04:42:42 +000045// properties which apply to either the shared or static version of a cc_library module.
Liz Kammer2222c6b2021-05-24 15:41:47 -040046type staticOrSharedAttributes struct {
Vinh Tran9f6796a2022-08-16 13:10:31 -040047 Srcs bazel.LabelListAttribute
48 Srcs_c bazel.LabelListAttribute
49 Srcs_as bazel.LabelListAttribute
50 Srcs_aidl bazel.LabelListAttribute
51 Hdrs bazel.LabelListAttribute
52 Copts bazel.StringListAttribute
Jingwen Chen14a8bda2021-06-02 11:10:02 +000053
Liz Kammer12615db2021-09-28 09:19:17 -040054 Deps bazel.LabelListAttribute
55 Implementation_deps bazel.LabelListAttribute
56 Dynamic_deps bazel.LabelListAttribute
57 Implementation_dynamic_deps bazel.LabelListAttribute
58 Whole_archive_deps bazel.LabelListAttribute
59 Implementation_whole_archive_deps bazel.LabelListAttribute
Cole Faust6b29f592022-08-09 09:50:56 -070060 Runtime_deps bazel.LabelListAttribute
Chris Parsons51f8c392021-08-03 21:01:05 -040061
62 System_dynamic_deps bazel.LabelListAttribute
Chris Parsons58852a02021-12-09 18:10:18 -050063
64 Enabled bazel.BoolAttribute
Yu Liufc603162022-03-01 15:44:08 -080065
Yu Liuf01a0f02022-12-07 15:45:30 -080066 Native_coverage *bool
Yu Liu8d82ac52022-05-17 15:13:28 -070067
Yu Liufc603162022-03-01 15:44:08 -080068 sdkAttributes
Sam Delmericofb3bb322022-10-21 10:42:24 -040069
70 tidyAttributes
71}
72
73type tidyAttributes struct {
Sam Delmerico63f0c932023-03-14 14:05:28 -040074 Tidy *string
Sam Delmericofb3bb322022-10-21 10:42:24 -040075 Tidy_flags []string
76 Tidy_checks []string
77 Tidy_checks_as_errors []string
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -040078 Tidy_disabled_srcs bazel.LabelListAttribute
Sam Delmerico4c902d62022-11-02 14:17:15 -040079 Tidy_timeout_srcs bazel.LabelListAttribute
Sam Delmericofb3bb322022-10-21 10:42:24 -040080}
81
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -040082func (m *Module) convertTidyAttributes(ctx android.BaseMutatorContext, moduleAttrs *tidyAttributes) {
Sam Delmericofb3bb322022-10-21 10:42:24 -040083 for _, f := range m.features {
84 if tidy, ok := f.(*tidyFeature); ok {
Sam Delmerico63f0c932023-03-14 14:05:28 -040085 var tidyAttr *string
86 if tidy.Properties.Tidy != nil {
87 if *tidy.Properties.Tidy {
88 tidyAttr = proptools.StringPtr("local")
89 } else {
90 tidyAttr = proptools.StringPtr("never")
91 }
92 }
93 moduleAttrs.Tidy = tidyAttr
Sam Delmericofb3bb322022-10-21 10:42:24 -040094 moduleAttrs.Tidy_flags = tidy.Properties.Tidy_flags
95 moduleAttrs.Tidy_checks = tidy.Properties.Tidy_checks
96 moduleAttrs.Tidy_checks_as_errors = tidy.Properties.Tidy_checks_as_errors
97 }
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -040098
99 }
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -0400100 archVariantProps := m.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
101 for axis, configToProps := range archVariantProps {
Sasha Smundak39a301c2022-12-29 17:11:49 -0800102 for cfg, _props := range configToProps {
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -0400103 if archProps, ok := _props.(*BaseCompilerProperties); ok {
104 archDisabledSrcs := android.BazelLabelForModuleSrc(ctx, archProps.Tidy_disabled_srcs)
Sasha Smundak39a301c2022-12-29 17:11:49 -0800105 moduleAttrs.Tidy_disabled_srcs.SetSelectValue(axis, cfg, archDisabledSrcs)
Sam Delmerico4c902d62022-11-02 14:17:15 -0400106 archTimeoutSrcs := android.BazelLabelForModuleSrc(ctx, archProps.Tidy_timeout_srcs)
Sasha Smundak39a301c2022-12-29 17:11:49 -0800107 moduleAttrs.Tidy_timeout_srcs.SetSelectValue(axis, cfg, archTimeoutSrcs)
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -0400108 }
109 }
Sam Delmericofb3bb322022-10-21 10:42:24 -0400110 }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000111}
112
Sam Delmericoc7681022022-02-04 21:01:20 +0000113// groupSrcsByExtension partitions `srcs` into groups based on file extension.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000114func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute {
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400115 // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl
116 // macro.
117 addSuffixForFilegroup := func(suffix string) bazel.LabelMapper {
Vinh Tran9f6796a2022-08-16 13:10:31 -0400118 return func(otherModuleCtx bazel.OtherModuleContext, label bazel.Label) (string, bool) {
119
120 m, exists := otherModuleCtx.ModuleFromName(label.OriginalModuleName)
Liz Kammer12615db2021-09-28 09:19:17 -0400121 labelStr := label.Label
Vinh Tran9f6796a2022-08-16 13:10:31 -0400122 if !exists || !android.IsFilegroup(otherModuleCtx, m) {
123 return labelStr, false
124 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700125 // If the filegroup is already converted to aidl_library or proto_library,
126 // skip creating _c_srcs, _as_srcs, _cpp_srcs filegroups
127 fg, _ := m.(android.FileGroupAsLibrary)
128 if fg.ShouldConvertToAidlLibrary(ctx) || fg.ShouldConvertToProtoLibrary(ctx) {
Liz Kammer12615db2021-09-28 09:19:17 -0400129 return labelStr, false
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000130 }
Liz Kammer12615db2021-09-28 09:19:17 -0400131 return labelStr + suffix, true
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400132 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000133 }
134
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400135 // TODO(b/190006308): Handle language detection of sources in a Bazel rule.
Sam Delmericoc7681022022-02-04 21:01:20 +0000136 labels := bazel.LabelPartitions{
137 protoSrcPartition: android.ProtoSrcLabelPartition,
Liz Kammeraabfb5d2021-12-08 15:25:06 -0500138 cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")},
139 asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")},
Cole Faust7071a052022-07-29 15:58:33 -0700140 asmSrcPartition: bazel.LabelPartition{Extensions: []string{".asm"}},
Vinh Tran9f6796a2022-08-16 13:10:31 -0400141 aidlSrcPartition: android.AidlSrcLabelPartition,
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000142 // TODO(http://b/231968910): If there is ever a filegroup target that
143 // contains .l or .ll files we will need to find a way to add a
144 // LabelMapper for these that identifies these filegroups and
145 // converts them appropriately
146 lSrcPartition: bazel.LabelPartition{Extensions: []string{".l"}},
147 llSrcPartition: bazel.LabelPartition{Extensions: []string{".ll"}},
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400148 // C++ is the "catch-all" group, and comprises generated sources because we don't
149 // know the language of these sources until the genrule is executed.
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000150 cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true},
151 syspropSrcPartition: bazel.LabelPartition{Extensions: []string{".sysprop"}},
Sam Delmericoc7681022022-02-04 21:01:20 +0000152 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000153
Sam Delmericoc7681022022-02-04 21:01:20 +0000154 return bazel.PartitionLabelListAttribute(ctx, &srcs, labels)
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000155}
156
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000157// bp2BuildParseLibProps returns the attributes for a variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000158func bp2BuildParseLibProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000159 lib, ok := module.compiler.(*libraryDecorator)
160 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400161 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000162 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000163 return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic)
164}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000165
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000166// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000167func bp2BuildParseSharedProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000168 return bp2BuildParseLibProps(ctx, module, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000169}
170
171// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000172func bp2BuildParseStaticProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000173 return bp2BuildParseLibProps(ctx, module, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400174}
175
Liz Kammer7a210ac2021-09-22 15:52:58 -0400176type depsPartition struct {
177 export bazel.LabelList
178 implementation bazel.LabelList
179}
180
Jingwen Chen55bc8202021-11-02 06:40:51 +0000181type bazelLabelForDepsFn func(android.BazelConversionPathContext, []string) bazel.LabelList
Liz Kammer7a210ac2021-09-22 15:52:58 -0400182
Jingwen Chen55bc8202021-11-02 06:40:51 +0000183func maybePartitionExportedAndImplementationsDeps(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, exportedDeps []string, fn bazelLabelForDepsFn) depsPartition {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400184 if !exportsDeps {
185 return depsPartition{
186 implementation: fn(ctx, allDeps),
187 }
188 }
189
Liz Kammer7a210ac2021-09-22 15:52:58 -0400190 implementation, export := android.FilterList(allDeps, exportedDeps)
191
192 return depsPartition{
193 export: fn(ctx, export),
194 implementation: fn(ctx, implementation),
195 }
196}
197
Jingwen Chen55bc8202021-11-02 06:40:51 +0000198type bazelLabelForDepsExcludesFn func(android.BazelConversionPathContext, []string, []string) bazel.LabelList
Liz Kammer7a210ac2021-09-22 15:52:58 -0400199
Jingwen Chen55bc8202021-11-02 06:40:51 +0000200func maybePartitionExportedAndImplementationsDepsExcludes(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, excludes, exportedDeps []string, fn bazelLabelForDepsExcludesFn) depsPartition {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400201 if !exportsDeps {
202 return depsPartition{
203 implementation: fn(ctx, allDeps, excludes),
204 }
205 }
Liz Kammer7a210ac2021-09-22 15:52:58 -0400206 implementation, export := android.FilterList(allDeps, exportedDeps)
207
208 return depsPartition{
209 export: fn(ctx, export, excludes),
210 implementation: fn(ctx, implementation, excludes),
211 }
212}
213
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000214func bp2BuildPropParseHelper(ctx android.ArchVariantContext, module *Module, propsType interface{}, parseFunc func(axis bazel.ConfigurationAxis, config string, props interface{})) {
215 for axis, configToProps := range module.GetArchVariantProperties(ctx, propsType) {
Sasha Smundak39a301c2022-12-29 17:11:49 -0800216 for cfg, props := range configToProps {
217 parseFunc(axis, cfg, props)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000218 }
219 }
220}
221
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000222// Parses properties common to static and shared libraries. Also used for prebuilt libraries.
Sasha Smundak39a301c2022-12-29 17:11:49 -0800223func bp2buildParseStaticOrSharedProps(ctx android.BazelConversionPathContext, module *Module, _ *libraryDecorator, isStatic bool) staticOrSharedAttributes {
Liz Kammer135bf552021-08-11 10:46:06 -0400224 attrs := staticOrSharedAttributes{}
Jingwen Chenbcf53042021-05-26 04:42:42 +0000225
Liz Kammer9abd62d2021-05-21 08:37:59 -0400226 setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000227 attrs.Copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, filterOutStdFlag))
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000228 attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
Chris Parsons953b3562021-09-20 15:14:39 -0400229 attrs.System_dynamic_deps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, props.System_shared_libs))
Liz Kammer7a210ac2021-09-22 15:52:58 -0400230
Liz Kammer2b8004b2021-10-04 13:55:44 -0400231 staticDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Static_libs, props.Export_static_lib_headers, bazelLabelForStaticDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400232 attrs.Deps.SetSelectValue(axis, config, staticDeps.export)
233 attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation)
234
Liz Kammer2b8004b2021-10-04 13:55:44 -0400235 sharedDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400236 attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export)
237 attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation)
238
239 attrs.Whole_archive_deps.SetSelectValue(axis, config, bazelLabelForWholeDeps(ctx, props.Whole_static_libs))
Chris Parsons58852a02021-12-09 18:10:18 -0500240 attrs.Enabled.SetSelectValue(axis, config, props.Enabled)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000241 }
Liz Kammer135bf552021-08-11 10:46:06 -0400242 // system_dynamic_deps distinguishes between nil/empty list behavior:
243 // nil -> use default values
244 // empty list -> no values specified
245 attrs.System_dynamic_deps.ForceSpecifyEmptyList = true
Jingwen Chenbcf53042021-05-26 04:42:42 +0000246
247 if isStatic {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000248 bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
249 if staticOrSharedProps, ok := props.(*StaticProperties); ok {
250 setAttrs(axis, config, staticOrSharedProps.Static)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000251 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000252 })
Jingwen Chenbcf53042021-05-26 04:42:42 +0000253 } else {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000254 bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
255 if staticOrSharedProps, ok := props.(*SharedProperties); ok {
256 setAttrs(axis, config, staticOrSharedProps.Shared)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000257 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000258 })
Jingwen Chenbcf53042021-05-26 04:42:42 +0000259 }
260
Liz Kammerae3994e2021-10-19 09:45:48 -0400261 partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs)
262 attrs.Srcs = partitionedSrcs[cppSrcPartition]
263 attrs.Srcs_c = partitionedSrcs[cSrcPartition]
264 attrs.Srcs_as = partitionedSrcs[asSrcPartition]
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000265
Liz Kammer12615db2021-09-28 09:19:17 -0400266 if !partitionedSrcs[protoSrcPartition].IsEmpty() {
267 // TODO(b/208815215): determine whether this is used and add support if necessary
268 ctx.ModuleErrorf("Migrating static/shared only proto srcs is not currently supported")
269 }
270
Jingwen Chenbcf53042021-05-26 04:42:42 +0000271 return attrs
Jingwen Chen53681ef2021-04-29 08:15:13 +0000272}
273
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400274// Convenience struct to hold all attributes parsed from prebuilt properties.
275type prebuiltAttributes struct {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000276 Src bazel.LabelAttribute
277 Enabled bazel.BoolAttribute
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400278}
279
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000280func parseSrc(ctx android.BazelConversionPathContext, srcLabelAttribute *bazel.LabelAttribute, axis bazel.ConfigurationAxis, config string, srcs []string) {
281 srcFileError := func() {
282 ctx.ModuleErrorf("parseSrc: Expected at most one source file for %s %s\n", axis, config)
283 }
284 if len(srcs) > 1 {
285 srcFileError()
286 return
287 } else if len(srcs) == 0 {
288 return
289 }
290 if srcLabelAttribute.SelectValue(axis, config) != nil {
291 srcFileError()
292 return
293 }
294 srcLabelAttribute.SetSelectValue(axis, config, android.BazelLabelForModuleSrcSingle(ctx, srcs[0]))
295}
296
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000297// NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000298func Bp2BuildParsePrebuiltLibraryProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) prebuiltAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000299
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400300 var srcLabelAttribute bazel.LabelAttribute
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000301 bp2BuildPropParseHelper(ctx, module, &prebuiltLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
302 if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000303 parseSrc(ctx, &srcLabelAttribute, axis, config, prebuiltLinkerProperties.Srcs)
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000304 }
305 })
306
307 var enabledLabelAttribute bazel.BoolAttribute
308 parseAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
309 if props.Enabled != nil {
310 enabledLabelAttribute.SetSelectValue(axis, config, props.Enabled)
311 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000312 parseSrc(ctx, &srcLabelAttribute, axis, config, props.Srcs)
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000313 }
314
315 if isStatic {
316 bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
317 if staticProperties, ok := props.(*StaticProperties); ok {
318 parseAttrs(axis, config, staticProperties.Static)
319 }
320 })
321 } else {
322 bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
323 if sharedProperties, ok := props.(*SharedProperties); ok {
324 parseAttrs(axis, config, sharedProperties.Shared)
325 }
326 })
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400327 }
328
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400329 return prebuiltAttributes{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000330 Src: srcLabelAttribute,
331 Enabled: enabledLabelAttribute,
332 }
333}
334
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000335func bp2BuildParsePrebuiltBinaryProps(ctx android.BazelConversionPathContext, module *Module) prebuiltAttributes {
336 var srcLabelAttribute bazel.LabelAttribute
337 bp2BuildPropParseHelper(ctx, module, &prebuiltLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
338 if props, ok := props.(*prebuiltLinkerProperties); ok {
339 parseSrc(ctx, &srcLabelAttribute, axis, config, props.Srcs)
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000340 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000341 })
342
343 return prebuiltAttributes{
344 Src: srcLabelAttribute,
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400345 }
346}
347
Colin Crossc5075e92022-12-05 16:46:39 -0800348func bp2BuildParsePrebuiltObjectProps(ctx android.BazelConversionPathContext, module *Module) prebuiltAttributes {
349 var srcLabelAttribute bazel.LabelAttribute
350 bp2BuildPropParseHelper(ctx, module, &prebuiltObjectProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
351 if props, ok := props.(*prebuiltObjectProperties); ok {
352 parseSrc(ctx, &srcLabelAttribute, axis, config, props.Srcs)
353 }
354 })
355
356 return prebuiltAttributes{
357 Src: srcLabelAttribute,
358 }
359}
360
Liz Kammere6583482021-10-19 13:56:10 -0400361type baseAttributes struct {
362 compilerAttributes
363 linkerAttributes
Liz Kammer12615db2021-09-28 09:19:17 -0400364
Trevor Radcliffedb7e0262022-10-28 16:48:18 +0000365 // A combination of compilerAttributes.features and linkerAttributes.features, as well as sanitizer features
Cole Faust5fa4e962022-08-22 14:31:04 -0700366 features bazel.StringListAttribute
Liz Kammer12615db2021-09-28 09:19:17 -0400367 protoDependency *bazel.LabelAttribute
Vinh Tran9f6796a2022-08-16 13:10:31 -0400368 aidlDependency *bazel.LabelAttribute
Yu Liuf01a0f02022-12-07 15:45:30 -0800369 Native_coverage *bool
Liz Kammere6583482021-10-19 13:56:10 -0400370}
371
Jingwen Chen107c0de2021-04-09 10:43:12 +0000372// Convenience struct to hold all attributes parsed from compiler properties.
373type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400374 // Options for all languages
375 copts bazel.StringListAttribute
376 // Assembly options and sources
377 asFlags bazel.StringListAttribute
378 asSrcs bazel.LabelListAttribute
Cole Faust7071a052022-07-29 15:58:33 -0700379 asmSrcs bazel.LabelListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400380 // C options and sources
381 conlyFlags bazel.StringListAttribute
382 cSrcs bazel.LabelListAttribute
383 // C++ options and sources
384 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000385 srcs bazel.LabelListAttribute
Chris Parsons2c788392021-08-10 11:58:07 -0400386
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000387 // Lex sources and options
388 lSrcs bazel.LabelListAttribute
389 llSrcs bazel.LabelListAttribute
390 lexopts bazel.StringListAttribute
391
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000392 // Sysprop sources
393 syspropSrcs bazel.LabelListAttribute
394
Liz Kammere6583482021-10-19 13:56:10 -0400395 hdrs bazel.LabelListAttribute
396
Chris Parsons2c788392021-08-10 11:58:07 -0400397 rtti bazel.BoolAttribute
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000398
399 // Not affected by arch variants
400 stl *string
Chris Parsons79bd2b72021-11-29 17:52:41 -0500401 cStd *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000402 cppStd *string
Liz Kammer35687bc2021-09-10 10:07:07 -0400403
404 localIncludes bazel.StringListAttribute
405 absoluteIncludes bazel.StringListAttribute
Liz Kammer12615db2021-09-28 09:19:17 -0400406
Liz Kammer1263d9b2021-12-10 14:28:20 -0500407 includes BazelIncludes
408
Liz Kammer12615db2021-09-28 09:19:17 -0400409 protoSrcs bazel.LabelListAttribute
Vinh Tran9f6796a2022-08-16 13:10:31 -0400410 aidlSrcs bazel.LabelListAttribute
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000411
412 stubsSymbolFile *string
413 stubsVersions bazel.StringListAttribute
Cole Faust5fa4e962022-08-22 14:31:04 -0700414
415 features bazel.StringListAttribute
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500416
417 suffix bazel.StringAttribute
Vinh Tran99270ea2022-11-28 11:15:23 -0500418
419 fdoProfile bazel.LabelAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000420}
421
Liz Kammercac7f692021-12-16 14:19:32 -0500422type filterOutFn func(string) bool
423
424func filterOutStdFlag(flag string) bool {
425 return strings.HasPrefix(flag, "-std=")
426}
427
Alix1be00d42022-05-16 22:56:04 +0000428func filterOutClangUnknownCflags(flag string) bool {
429 for _, f := range config.ClangUnknownCflags {
430 if f == flag {
431 return true
432 }
433 }
434 return false
435}
436
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000437func parseCommandLineFlags(soongFlags []string, filterOut ...filterOutFn) []string {
Liz Kammere6583482021-10-19 13:56:10 -0400438 var result []string
439 for _, flag := range soongFlags {
Alix1be00d42022-05-16 22:56:04 +0000440 skipFlag := false
441 for _, filter := range filterOut {
442 if filter != nil && filter(flag) {
443 skipFlag = true
444 }
445 }
446 if skipFlag {
Liz Kammercac7f692021-12-16 14:19:32 -0500447 continue
448 }
Liz Kammere6583482021-10-19 13:56:10 -0400449 // Soong's cflags can contain spaces, like `-include header.h`. For
450 // Bazel's copts, split them up to be compatible with the
451 // no_copts_tokenization feature.
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000452 result = append(result, strings.Split(flag, " ")...)
Liz Kammere6583482021-10-19 13:56:10 -0400453 }
454 return result
455}
Jingwen Chened9c17d2021-04-13 07:14:55 +0000456
Jingwen Chen55bc8202021-11-02 06:40:51 +0000457func (ca *compilerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, props *BaseCompilerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400458 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
459 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
460 if srcsList, ok := parseSrcs(ctx, props); ok {
461 ca.srcs.SetSelectValue(axis, config, srcsList)
Chris Parsons990c4f42021-05-25 12:10:58 -0400462 }
463
Liz Kammere6583482021-10-19 13:56:10 -0400464 localIncludeDirs := props.Local_include_dirs
465 if axis == bazel.NoConfigAxis {
Chris Parsons79bd2b72021-11-29 17:52:41 -0500466 ca.cStd, ca.cppStd = bp2buildResolveCppStdValue(props.C_std, props.Cpp_std, props.Gnu_extensions)
Liz Kammere6583482021-10-19 13:56:10 -0400467 if includeBuildDirectory(props.Include_build_directory) {
468 localIncludeDirs = append(localIncludeDirs, ".")
Liz Kammer222bdcf2021-10-11 14:15:51 -0400469 }
Jingwen Chene32e9e02021-04-23 09:17:24 +0000470 }
471
Liz Kammere6583482021-10-19 13:56:10 -0400472 ca.absoluteIncludes.SetSelectValue(axis, config, props.Include_dirs)
473 ca.localIncludes.SetSelectValue(axis, config, localIncludeDirs)
474
Cole Faust5fa4e962022-08-22 14:31:04 -0700475 instructionSet := proptools.StringDefault(props.Instruction_set, "")
476 if instructionSet == "arm" {
477 ca.features.SetSelectValue(axis, config, []string{"arm_isa_arm", "-arm_isa_thumb"})
478 } else if instructionSet != "" && instructionSet != "thumb" {
479 ctx.ModuleErrorf("Unknown value for instruction_set: %s", instructionSet)
480 }
481
Liz Kammercac7f692021-12-16 14:19:32 -0500482 // In Soong, cflags occur on the command line before -std=<val> flag, resulting in the value being
483 // overridden. In Bazel we always allow overriding, via flags; however, this can cause
484 // incompatibilities, so we remove "-std=" flags from Cflag properties while leaving it in other
485 // cases.
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000486 ca.copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, filterOutStdFlag, filterOutClangUnknownCflags))
487 ca.asFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Asflags, nil))
488 ca.conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Conlyflags, filterOutClangUnknownCflags))
489 ca.cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Cppflags, filterOutClangUnknownCflags))
Liz Kammere6583482021-10-19 13:56:10 -0400490 ca.rtti.SetSelectValue(axis, config, props.Rtti)
491}
492
Jingwen Chen55bc8202021-11-02 06:40:51 +0000493func (ca *compilerAttributes) convertStlProps(ctx android.ArchVariantContext, module *Module) {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000494 bp2BuildPropParseHelper(ctx, module, &StlProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
495 if stlProps, ok := props.(*StlProperties); ok {
496 if stlProps.Stl == nil {
497 return
498 }
499 if ca.stl == nil {
Liz Kammer7128d382022-05-12 11:42:33 -0400500 stl := deduplicateStlInput(*stlProps.Stl)
501 ca.stl = &stl
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000502 } else if ca.stl != stlProps.Stl {
503 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 -0400504 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000505 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000506 })
Liz Kammere6583482021-10-19 13:56:10 -0400507}
Jingwen Chenc1c26502021-04-05 10:35:13 +0000508
Jingwen Chen55bc8202021-11-02 06:40:51 +0000509func (ca *compilerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Liz Kammerba7a9c52021-05-26 08:45:30 -0400510 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
Liz Kammere6583482021-10-19 13:56:10 -0400511 "Cflags": &ca.copts,
512 "Asflags": &ca.asFlags,
513 "CppFlags": &ca.cppFlags,
Liz Kammerba7a9c52021-05-26 08:45:30 -0400514 }
Liz Kammerba7a9c52021-05-26 08:45:30 -0400515 for propName, attr := range productVarPropNameToAttribute {
Jingwen Chen25825ca2021-11-15 12:28:43 +0000516 if productConfigProps, exists := productVariableProps[propName]; exists {
517 for productConfigProp, prop := range productConfigProps {
518 flags, ok := prop.([]string)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400519 if !ok {
520 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
521 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000522 newFlags, _ := bazel.TryVariableSubstitutions(flags, productConfigProp.Name)
523 attr.SetSelectValue(productConfigProp.ConfigurationAxis(), productConfigProp.SelectKey(), newFlags)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400524 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400525 }
526 }
Liz Kammere6583482021-10-19 13:56:10 -0400527}
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400528
Jingwen Chen55bc8202021-11-02 06:40:51 +0000529func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, implementationHdrs bazel.LabelListAttribute) {
Liz Kammere6583482021-10-19 13:56:10 -0400530 ca.srcs.ResolveExcludes()
531 partitionedSrcs := groupSrcsByExtension(ctx, ca.srcs)
532
Liz Kammer12615db2021-09-28 09:19:17 -0400533 ca.protoSrcs = partitionedSrcs[protoSrcPartition]
Vinh Tran9f6796a2022-08-16 13:10:31 -0400534 ca.aidlSrcs = partitionedSrcs[aidlSrcPartition]
Liz Kammer12615db2021-09-28 09:19:17 -0400535
Liz Kammere6583482021-10-19 13:56:10 -0400536 for p, lla := range partitionedSrcs {
537 // if there are no sources, there is no need for headers
538 if lla.IsEmpty() {
539 continue
540 }
541 lla.Append(implementationHdrs)
542 partitionedSrcs[p] = lla
543 }
544
545 ca.srcs = partitionedSrcs[cppSrcPartition]
546 ca.cSrcs = partitionedSrcs[cSrcPartition]
547 ca.asSrcs = partitionedSrcs[asSrcPartition]
Cole Faust7071a052022-07-29 15:58:33 -0700548 ca.asmSrcs = partitionedSrcs[asmSrcPartition]
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000549 ca.lSrcs = partitionedSrcs[lSrcPartition]
550 ca.llSrcs = partitionedSrcs[llSrcPartition]
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000551 ca.syspropSrcs = partitionedSrcs[syspropSrcPartition]
Liz Kammere6583482021-10-19 13:56:10 -0400552
553 ca.absoluteIncludes.DeduplicateAxesFromBase()
554 ca.localIncludes.DeduplicateAxesFromBase()
555}
556
557// Parse srcs from an arch or OS's props value.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000558func parseSrcs(ctx android.BazelConversionPathContext, props *BaseCompilerProperties) (bazel.LabelList, bool) {
Liz Kammere6583482021-10-19 13:56:10 -0400559 anySrcs := false
560 // Add srcs-like dependencies such as generated files.
561 // First create a LabelList containing these dependencies, then merge the values with srcs.
562 generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources)
563 if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 {
564 anySrcs = true
565 }
566
567 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs)
Vinh Tran9f6796a2022-08-16 13:10:31 -0400568
Liz Kammere6583482021-10-19 13:56:10 -0400569 if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 {
570 anySrcs = true
571 }
Vinh Tran9f6796a2022-08-16 13:10:31 -0400572
Liz Kammere6583482021-10-19 13:56:10 -0400573 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs
574}
575
Liz Kammera5a29de2022-05-25 23:19:37 -0400576func bp2buildStdVal(std *string, prefix string, useGnu bool) *string {
577 defaultVal := prefix + "_std_default"
Chris Parsons79bd2b72021-11-29 17:52:41 -0500578 // If c{,pp}std properties are not specified, don't generate them in the BUILD file.
579 // Defaults are handled by the toolchain definition.
580 // However, if gnu_extensions is false, then the default gnu-to-c version must be specified.
Liz Kammera5a29de2022-05-25 23:19:37 -0400581 stdVal := proptools.StringDefault(std, defaultVal)
582 if stdVal == "experimental" || stdVal == defaultVal {
583 if stdVal == "experimental" {
584 stdVal = prefix + "_std_experimental"
585 }
586 if !useGnu {
587 stdVal += "_no_gnu"
588 }
589 } else if !useGnu {
590 stdVal = gnuToCReplacer.Replace(stdVal)
Chris Parsons79bd2b72021-11-29 17:52:41 -0500591 }
592
Liz Kammera5a29de2022-05-25 23:19:37 -0400593 if stdVal == defaultVal {
594 return nil
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500595 }
Liz Kammera5a29de2022-05-25 23:19:37 -0400596 return &stdVal
597}
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500598
Liz Kammera5a29de2022-05-25 23:19:37 -0400599func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) {
600 useGnu := useGnuExtensions(gnu_extensions)
601
602 return bp2buildStdVal(c_std, "c", useGnu), bp2buildStdVal(cpp_std, "cpp", useGnu)
Liz Kammere6583482021-10-19 13:56:10 -0400603}
604
Liz Kammer1263d9b2021-12-10 14:28:20 -0500605// packageFromLabel extracts package from a fully-qualified or relative Label and whether the label
606// is fully-qualified.
607// e.g. fully-qualified "//a/b:foo" -> "a/b", true, relative: ":bar" -> ".", false
608func packageFromLabel(label string) (string, bool) {
609 split := strings.Split(label, ":")
610 if len(split) != 2 {
611 return "", false
612 }
613 if split[0] == "" {
614 return ".", false
615 }
616 // remove leading "//"
617 return split[0][2:], true
618}
619
620// includesFromLabelList extracts relative/absolute includes from a bazel.LabelList>
621func includesFromLabelList(labelList bazel.LabelList) (relative, absolute []string) {
622 for _, hdr := range labelList.Includes {
623 if pkg, hasPkg := packageFromLabel(hdr.Label); hasPkg {
624 absolute = append(absolute, pkg)
625 } else if pkg != "" {
626 relative = append(relative, pkg)
627 }
628 }
629 return relative, absolute
630}
631
Cole Faust7071a052022-07-29 15:58:33 -0700632type YasmAttributes struct {
633 Srcs bazel.LabelListAttribute
634 Flags bazel.StringListAttribute
635 Include_dirs bazel.StringListAttribute
636}
637
638func bp2BuildYasm(ctx android.Bp2buildMutatorContext, m *Module, ca compilerAttributes) *bazel.LabelAttribute {
639 if ca.asmSrcs.IsEmpty() {
640 return nil
641 }
642
643 // Yasm needs the include directories from both local_includes and
644 // export_include_dirs. We don't care about actually exporting them from the
645 // yasm rule though, because they will also be present on the cc_ rule that
646 // wraps this yasm rule.
647 includes := ca.localIncludes.Clone()
648 bp2BuildPropParseHelper(ctx, m, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
649 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
650 if len(flagExporterProperties.Export_include_dirs) > 0 {
651 x := bazel.StringListAttribute{}
652 x.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs)
653 includes.Append(x)
654 }
655 }
656 })
657
658 ctx.CreateBazelTargetModule(
659 bazel.BazelTargetModuleProperties{
660 Rule_class: "yasm",
661 Bzl_load_location: "//build/bazel/rules/cc:yasm.bzl",
662 },
663 android.CommonAttributes{Name: m.Name() + "_yasm"},
664 &YasmAttributes{
665 Srcs: ca.asmSrcs,
666 Flags: ca.asFlags,
667 Include_dirs: *includes,
668 })
669
670 // We only want to add a dependency on the _yasm target if there are asm
671 // sources in the current configuration. If there are unconfigured asm
672 // sources, always add the dependency. Otherwise, add the dependency only
673 // on the configuration axes and values that had asm sources.
674 if len(ca.asmSrcs.Value.Includes) > 0 {
675 return bazel.MakeLabelAttribute(":" + m.Name() + "_yasm")
676 }
677
678 ret := &bazel.LabelAttribute{}
679 for _, axis := range ca.asmSrcs.SortedConfigurationAxes() {
Sasha Smundak39a301c2022-12-29 17:11:49 -0800680 for cfg := range ca.asmSrcs.ConfigurableValues[axis] {
681 ret.SetSelectValue(axis, cfg, bazel.Label{Label: ":" + m.Name() + "_yasm"})
Cole Faust7071a052022-07-29 15:58:33 -0700682 }
683 }
684 return ret
685}
686
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000687// bp2BuildParseBaseProps returns all compiler, linker, library attributes of a cc module..
Liz Kammer12615db2021-09-28 09:19:17 -0400688func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) baseAttributes {
Liz Kammere6583482021-10-19 13:56:10 -0400689 archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
690 archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{})
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000691 archVariantLibraryProperties := module.GetArchVariantProperties(ctx, &LibraryProperties{})
Liz Kammere6583482021-10-19 13:56:10 -0400692
693 var implementationHdrs bazel.LabelListAttribute
694
695 axisToConfigs := map[bazel.ConfigurationAxis]map[string]bool{}
696 allAxesAndConfigs := func(cp android.ConfigurationAxisToArchVariantProperties) {
697 for axis, configMap := range cp {
698 if _, ok := axisToConfigs[axis]; !ok {
699 axisToConfigs[axis] = map[string]bool{}
700 }
Sasha Smundak39a301c2022-12-29 17:11:49 -0800701 for cfg := range configMap {
702 axisToConfigs[axis][cfg] = true
Chris Parsonsa967f252021-09-23 16:34:35 -0400703 }
704 }
705 }
Liz Kammere6583482021-10-19 13:56:10 -0400706 allAxesAndConfigs(archVariantCompilerProps)
707 allAxesAndConfigs(archVariantLinkerProps)
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000708 allAxesAndConfigs(archVariantLibraryProperties)
Chris Parsonsa967f252021-09-23 16:34:35 -0400709
Liz Kammere6583482021-10-19 13:56:10 -0400710 compilerAttrs := compilerAttributes{}
711 linkerAttrs := linkerAttributes{}
712
Chris Parsons7b3289b2023-01-26 17:30:44 -0500713 // Iterate through these axes in a deterministic order. This is required
714 // because processing certain dependencies may result in concatenating
715 // elements along other axes. (For example, processing NoConfig may result
716 // in elements being added to InApex). This is thus the only way to ensure
717 // that the order of entries in each list is in a predictable order.
718 for _, axis := range bazel.SortedConfigurationAxes(axisToConfigs) {
719 configs := axisToConfigs[axis]
Sasha Smundak39a301c2022-12-29 17:11:49 -0800720 for cfg := range configs {
Liz Kammere6583482021-10-19 13:56:10 -0400721 var allHdrs []string
Sasha Smundak39a301c2022-12-29 17:11:49 -0800722 if baseCompilerProps, ok := archVariantCompilerProps[axis][cfg].(*BaseCompilerProperties); ok {
Liz Kammere6583482021-10-19 13:56:10 -0400723 allHdrs = baseCompilerProps.Generated_headers
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000724 if baseCompilerProps.Lex != nil {
Sasha Smundak39a301c2022-12-29 17:11:49 -0800725 compilerAttrs.lexopts.SetSelectValue(axis, cfg, baseCompilerProps.Lex.Flags)
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000726 }
Sasha Smundak39a301c2022-12-29 17:11:49 -0800727 (&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, cfg, baseCompilerProps)
Liz Kammere6583482021-10-19 13:56:10 -0400728 }
729
730 var exportHdrs []string
731
Sasha Smundak39a301c2022-12-29 17:11:49 -0800732 if baseLinkerProps, ok := archVariantLinkerProps[axis][cfg].(*BaseLinkerProperties); ok {
Liz Kammere6583482021-10-19 13:56:10 -0400733 exportHdrs = baseLinkerProps.Export_generated_headers
734
Sasha Smundak39a301c2022-12-29 17:11:49 -0800735 (&linkerAttrs).bp2buildForAxisAndConfig(ctx, module.Binary(), axis, cfg, baseLinkerProps)
Liz Kammere6583482021-10-19 13:56:10 -0400736 }
737 headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportHdrs, android.BazelLabelForModuleDeps)
Sasha Smundak39a301c2022-12-29 17:11:49 -0800738 implementationHdrs.SetSelectValue(axis, cfg, headers.implementation)
739 compilerAttrs.hdrs.SetSelectValue(axis, cfg, headers.export)
Liz Kammer1263d9b2021-12-10 14:28:20 -0500740
741 exportIncludes, exportAbsoluteIncludes := includesFromLabelList(headers.export)
Sasha Smundak39a301c2022-12-29 17:11:49 -0800742 compilerAttrs.includes.Includes.SetSelectValue(axis, cfg, exportIncludes)
743 compilerAttrs.includes.AbsoluteIncludes.SetSelectValue(axis, cfg, exportAbsoluteIncludes)
Liz Kammer1263d9b2021-12-10 14:28:20 -0500744
745 includes, absoluteIncludes := includesFromLabelList(headers.implementation)
Sasha Smundak39a301c2022-12-29 17:11:49 -0800746 currAbsoluteIncludes := compilerAttrs.absoluteIncludes.SelectValue(axis, cfg)
Liz Kammer1263d9b2021-12-10 14:28:20 -0500747 currAbsoluteIncludes = android.FirstUniqueStrings(append(currAbsoluteIncludes, absoluteIncludes...))
Vinh Tran9f6796a2022-08-16 13:10:31 -0400748
Sasha Smundak39a301c2022-12-29 17:11:49 -0800749 compilerAttrs.absoluteIncludes.SetSelectValue(axis, cfg, currAbsoluteIncludes)
Vinh Tran9f6796a2022-08-16 13:10:31 -0400750
Sasha Smundak39a301c2022-12-29 17:11:49 -0800751 currIncludes := compilerAttrs.localIncludes.SelectValue(axis, cfg)
Liz Kammer1263d9b2021-12-10 14:28:20 -0500752 currIncludes = android.FirstUniqueStrings(append(currIncludes, includes...))
Vinh Tran9f6796a2022-08-16 13:10:31 -0400753
Sasha Smundak39a301c2022-12-29 17:11:49 -0800754 compilerAttrs.localIncludes.SetSelectValue(axis, cfg, currIncludes)
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000755
Sasha Smundak39a301c2022-12-29 17:11:49 -0800756 if libraryProps, ok := archVariantLibraryProperties[axis][cfg].(*LibraryProperties); ok {
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000757 if axis == bazel.NoConfigAxis {
758 compilerAttrs.stubsSymbolFile = libraryProps.Stubs.Symbol_file
Sasha Smundak39a301c2022-12-29 17:11:49 -0800759 compilerAttrs.stubsVersions.SetSelectValue(axis, cfg, libraryProps.Stubs.Versions)
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000760 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500761 if suffix := libraryProps.Suffix; suffix != nil {
Sasha Smundak39a301c2022-12-29 17:11:49 -0800762 compilerAttrs.suffix.SetSelectValue(axis, cfg, suffix)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500763 }
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000764 }
Liz Kammere6583482021-10-19 13:56:10 -0400765 }
766 }
Vinh Tran9f6796a2022-08-16 13:10:31 -0400767
Liz Kammere6583482021-10-19 13:56:10 -0400768 compilerAttrs.convertStlProps(ctx, module)
769 (&linkerAttrs).convertStripProps(ctx, module)
770
Yu Liuf01a0f02022-12-07 15:45:30 -0800771 var nativeCoverage *bool
Yu Liu8d82ac52022-05-17 15:13:28 -0700772 if module.coverage != nil && module.coverage.Properties.Native_coverage != nil &&
773 !Bool(module.coverage.Properties.Native_coverage) {
Yu Liuf01a0f02022-12-07 15:45:30 -0800774 nativeCoverage = BoolPtr(false)
Yu Liu8d82ac52022-05-17 15:13:28 -0700775 }
776
Cole Faust912bc882023-03-08 12:29:50 -0800777 productVariableProps := android.ProductVariableProperties(ctx, ctx.Module())
Liz Kammere6583482021-10-19 13:56:10 -0400778
779 (&compilerAttrs).convertProductVariables(ctx, productVariableProps)
780 (&linkerAttrs).convertProductVariables(ctx, productVariableProps)
781
782 (&compilerAttrs).finalize(ctx, implementationHdrs)
Liz Kammer54309532021-12-14 12:21:22 -0500783 (&linkerAttrs).finalize(ctx)
Liz Kammere6583482021-10-19 13:56:10 -0400784
Cole Faust7071a052022-07-29 15:58:33 -0700785 (&compilerAttrs.srcs).Add(bp2BuildYasm(ctx, module, compilerAttrs))
786
Liz Kammer12615db2021-09-28 09:19:17 -0400787 protoDep := bp2buildProto(ctx, module, compilerAttrs.protoSrcs)
788
789 // bp2buildProto will only set wholeStaticLib or implementationWholeStaticLib, but we don't know
790 // which. This will add the newly generated proto library to the appropriate attribute and nothing
791 // to the other
792 (&linkerAttrs).wholeArchiveDeps.Add(protoDep.wholeStaticLib)
793 (&linkerAttrs).implementationWholeArchiveDeps.Add(protoDep.implementationWholeStaticLib)
Vinh Tranfde57eb2022-08-29 17:46:58 -0400794
Vinh Tran395a1e92022-09-16 18:27:29 -0400795 aidlDep := bp2buildCcAidlLibrary(ctx, module, compilerAttrs.aidlSrcs, linkerAttrs)
Vinh Tranfde57eb2022-08-29 17:46:58 -0400796 if aidlDep != nil {
797 if lib, ok := module.linker.(*libraryDecorator); ok {
798 if proptools.Bool(lib.Properties.Aidl.Export_aidl_headers) {
799 (&linkerAttrs).wholeArchiveDeps.Add(aidlDep)
800 } else {
801 (&linkerAttrs).implementationWholeArchiveDeps.Add(aidlDep)
802 }
803 }
804 }
Liz Kammer12615db2021-09-28 09:19:17 -0400805
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000806 convertedLSrcs := bp2BuildLex(ctx, module.Name(), compilerAttrs)
807 (&compilerAttrs).srcs.Add(&convertedLSrcs.srcName)
808 (&compilerAttrs).cSrcs.Add(&convertedLSrcs.cSrcName)
809
Vinh Tran99270ea2022-11-28 11:15:23 -0500810 if module.afdo != nil && module.afdo.Properties.Afdo {
811 fdoProfileDep := bp2buildFdoProfile(ctx, module)
812 if fdoProfileDep != nil {
813 (&compilerAttrs).fdoProfile.SetValue(*fdoProfileDep)
814 }
815 }
816
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000817 if !compilerAttrs.syspropSrcs.IsEmpty() {
818 (&linkerAttrs).wholeArchiveDeps.Add(bp2buildCcSysprop(ctx, module.Name(), module.Properties.Min_sdk_version, compilerAttrs.syspropSrcs))
819 }
820
Zi Wang9f609db2023-01-04 11:06:54 -0800821 linkerAttrs.wholeArchiveDeps.Prepend = true
822 linkerAttrs.deps.Prepend = true
823 compilerAttrs.localIncludes.Prepend = true
824 compilerAttrs.absoluteIncludes.Prepend = true
825 compilerAttrs.hdrs.Prepend = true
826
Trevor Radcliffedb7e0262022-10-28 16:48:18 +0000827 features := compilerAttrs.features.Clone().Append(linkerAttrs.features).Append(bp2buildSanitizerFeatures(ctx, module))
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +0000828 features = features.Append(bp2buildLtoFeatures(ctx, module))
Cole Faust5fa4e962022-08-22 14:31:04 -0700829 features.DeduplicateAxesFromBase()
830
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +0000831 addMuslSystemDynamicDeps(ctx, linkerAttrs)
832
Liz Kammere6583482021-10-19 13:56:10 -0400833 return baseAttributes{
834 compilerAttrs,
835 linkerAttrs,
Cole Faust5fa4e962022-08-22 14:31:04 -0700836 *features,
Liz Kammer12615db2021-09-28 09:19:17 -0400837 protoDep.protoDep,
Vinh Tran9f6796a2022-08-16 13:10:31 -0400838 aidlDep,
Yu Liuf01a0f02022-12-07 15:45:30 -0800839 nativeCoverage,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000840 }
841}
842
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +0000843// As a workaround for b/261657184, we are manually adding the default value
844// of system_dynamic_deps for the linux_musl os.
845// TODO: Solve this properly
846func addMuslSystemDynamicDeps(ctx android.Bp2buildMutatorContext, attrs linkerAttributes) {
847 systemDynamicDeps := attrs.systemDynamicDeps.SelectValue(bazel.OsConfigurationAxis, "linux_musl")
848 if attrs.systemDynamicDeps.HasAxisSpecificValues(bazel.OsConfigurationAxis) && systemDynamicDeps.IsNil() {
849 attrs.systemDynamicDeps.SetSelectValue(bazel.OsConfigurationAxis, "linux_musl", android.BazelLabelForModuleDeps(ctx, config.MuslDefaultSharedLibraries))
850 }
851}
852
Vinh Tran99270ea2022-11-28 11:15:23 -0500853type fdoProfileAttributes struct {
854 Absolute_path_profile string
855}
856
857func bp2buildFdoProfile(
858 ctx android.Bp2buildMutatorContext,
859 m *Module,
860) *bazel.Label {
861 for _, project := range globalAfdoProfileProjects {
Vinh Tranbc9c8b42022-12-09 12:03:52 -0500862 // Ensure handcrafted BUILD file exists in the project
863 BUILDPath := android.ExistentPathForSource(ctx, project, "BUILD")
864 if BUILDPath.Valid() {
865 // We handcraft a BUILD file with fdo_profile targets that use the existing profiles in the project
866 // This implementation is assuming that every afdo profile in globalAfdoProfileProjects already has
867 // an associated fdo_profile target declared in the same package.
868 // TODO(b/260714900): Handle arch-specific afdo profiles (e.g. `<module-name>-arm<64>.afdo`)
869 path := android.ExistentPathForSource(ctx, project, m.Name()+".afdo")
870 if path.Valid() {
871 // FIXME: Some profiles only exist internally and are not released to AOSP.
872 // When generated BUILD files are checked in, we'll run into merge conflict.
873 // The cc_library_shared target in AOSP won't have reference to an fdo_profile target because
874 // the profile doesn't exist. Internally, the same cc_library_shared target will
875 // have reference to the fdo_profile.
876 // For more context, see b/258682955#comment2
877 fdoProfileLabel := "//" + strings.TrimSuffix(project, "/") + ":" + m.Name()
878 return &bazel.Label{
879 Label: fdoProfileLabel,
880 }
Vinh Tran99270ea2022-11-28 11:15:23 -0500881 }
882 }
883 }
884
885 return nil
886}
887
Vinh Tran9f6796a2022-08-16 13:10:31 -0400888func bp2buildCcAidlLibrary(
889 ctx android.Bp2buildMutatorContext,
890 m *Module,
Vinh Trana3b8b782022-09-14 11:40:24 -0400891 aidlLabelList bazel.LabelListAttribute,
Vinh Tran395a1e92022-09-16 18:27:29 -0400892 linkerAttrs linkerAttributes,
Vinh Tran9f6796a2022-08-16 13:10:31 -0400893) *bazel.LabelAttribute {
Vinh Trana3b8b782022-09-14 11:40:24 -0400894 if !aidlLabelList.IsEmpty() {
895 aidlLibs, aidlSrcs := aidlLabelList.Partition(func(src bazel.Label) bool {
896 if fg, ok := android.ToFileGroupAsLibrary(ctx, src.OriginalModuleName); ok &&
897 fg.ShouldConvertToAidlLibrary(ctx) {
898 return true
899 }
900 return false
901 })
Vinh Tran9f6796a2022-08-16 13:10:31 -0400902
Vinh Trana3b8b782022-09-14 11:40:24 -0400903 if !aidlSrcs.IsEmpty() {
904 aidlLibName := m.Name() + "_aidl_library"
905 ctx.CreateBazelTargetModule(
906 bazel.BazelTargetModuleProperties{
907 Rule_class: "aidl_library",
908 Bzl_load_location: "//build/bazel/rules/aidl:library.bzl",
909 },
910 android.CommonAttributes{Name: aidlLibName},
911 &aidlLibraryAttributes{
912 Srcs: aidlSrcs,
913 },
914 )
915 aidlLibs.Add(&bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + aidlLibName}})
916 }
Vinh Tran9f6796a2022-08-16 13:10:31 -0400917
Vinh Trana3b8b782022-09-14 11:40:24 -0400918 if !aidlLibs.IsEmpty() {
919 ccAidlLibrarylabel := m.Name() + "_cc_aidl_library"
Sam Delmerico512437b2023-03-17 11:34:15 -0400920 // Since parent cc_library already has these dependencies, we can add them as implementation
921 // deps so that they don't re-export
922 implementationDeps := linkerAttrs.deps.Clone()
923 implementationDeps.Append(linkerAttrs.implementationDeps)
924 implementationDynamicDeps := linkerAttrs.dynamicDeps.Clone()
925 implementationDynamicDeps.Append(linkerAttrs.implementationDynamicDeps)
Vinh Tran395a1e92022-09-16 18:27:29 -0400926
Vinh Trana3b8b782022-09-14 11:40:24 -0400927 ctx.CreateBazelTargetModule(
928 bazel.BazelTargetModuleProperties{
929 Rule_class: "cc_aidl_library",
930 Bzl_load_location: "//build/bazel/rules/cc:cc_aidl_library.bzl",
931 },
932 android.CommonAttributes{Name: ccAidlLibrarylabel},
933 &ccAidlLibraryAttributes{
Vinh Tran395a1e92022-09-16 18:27:29 -0400934 Deps: aidlLibs,
Sam Delmerico512437b2023-03-17 11:34:15 -0400935 Implementation_deps: *implementationDeps,
936 Implementation_dynamic_deps: *implementationDynamicDeps,
Vinh Trana3b8b782022-09-14 11:40:24 -0400937 },
938 )
939 label := &bazel.LabelAttribute{
940 Value: &bazel.Label{
941 Label: ":" + ccAidlLibrarylabel,
942 },
943 }
944 return label
945 }
Vinh Tran9f6796a2022-08-16 13:10:31 -0400946 }
947
Vinh Trana3b8b782022-09-14 11:40:24 -0400948 return nil
Vinh Tran9f6796a2022-08-16 13:10:31 -0400949}
950
Yu Liufc603162022-03-01 15:44:08 -0800951func bp2BuildParseSdkAttributes(module *Module) sdkAttributes {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000952 return sdkAttributes{
953 Sdk_version: module.Properties.Sdk_version,
Yu Liufc603162022-03-01 15:44:08 -0800954 Min_sdk_version: module.Properties.Min_sdk_version,
955 }
956}
957
958type sdkAttributes struct {
959 Sdk_version *string
960 Min_sdk_version *string
961}
962
Jingwen Chen107c0de2021-04-09 10:43:12 +0000963// Convenience struct to hold all attributes parsed from linker properties.
964type linkerAttributes struct {
Liz Kammer54309532021-12-14 12:21:22 -0500965 deps bazel.LabelListAttribute
966 implementationDeps bazel.LabelListAttribute
967 dynamicDeps bazel.LabelListAttribute
968 implementationDynamicDeps bazel.LabelListAttribute
Cole Faust6b29f592022-08-09 09:50:56 -0700969 runtimeDeps bazel.LabelListAttribute
Liz Kammer54309532021-12-14 12:21:22 -0500970 wholeArchiveDeps bazel.LabelListAttribute
971 implementationWholeArchiveDeps bazel.LabelListAttribute
972 systemDynamicDeps bazel.LabelListAttribute
973 usedSystemDynamicDepAsDynamicDep map[string]bool
Liz Kammer7a210ac2021-09-22 15:52:58 -0400974
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500975 useVersionLib bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000976 linkopts bazel.StringListAttribute
Liz Kammerd2871182021-10-04 13:54:37 -0400977 additionalLinkerInputs bazel.LabelListAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000978 stripKeepSymbols bazel.BoolAttribute
979 stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
980 stripKeepSymbolsList bazel.StringListAttribute
981 stripAll bazel.BoolAttribute
982 stripNone bazel.BoolAttribute
Liz Kammer0eae52e2021-10-06 10:32:26 -0400983 features bazel.StringListAttribute
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400984}
985
Liz Kammer54309532021-12-14 12:21:22 -0500986var (
987 soongSystemSharedLibs = []string{"libc", "libm", "libdl"}
Liz Kammerbaced712022-09-16 09:01:29 -0400988 versionLib = "libbuildversion"
Liz Kammer54309532021-12-14 12:21:22 -0500989)
990
Vinh Tran85fb07c2022-09-16 16:17:48 -0400991// resolveTargetApex re-adds the shared and static libs in target.apex.exclude_shared|static_libs props to non-apex variant
992// since all libs are already excluded by default
Liz Kammer748d7072023-01-25 12:07:43 -0500993func (la *linkerAttributes) resolveTargetApexProp(ctx android.BazelConversionPathContext, props *BaseLinkerProperties) {
994 excludeSharedLibs := bazelLabelForSharedDeps(ctx, props.Target.Apex.Exclude_shared_libs)
995 sharedExcludes := bazel.LabelList{Excludes: excludeSharedLibs.Includes}
996 sharedExcludesLabelList := bazel.LabelListAttribute{}
997 sharedExcludesLabelList.SetSelectValue(bazel.InApexAxis, bazel.InApex, sharedExcludes)
Vinh Tran85fb07c2022-09-16 16:17:48 -0400998
Liz Kammer748d7072023-01-25 12:07:43 -0500999 la.dynamicDeps.Append(sharedExcludesLabelList)
1000 la.implementationDynamicDeps.Append(sharedExcludesLabelList)
1001
1002 excludeStaticLibs := bazelLabelForStaticDeps(ctx, props.Target.Apex.Exclude_static_libs)
1003 staticExcludes := bazel.LabelList{Excludes: excludeStaticLibs.Includes}
1004 staticExcludesLabelList := bazel.LabelListAttribute{}
1005 staticExcludesLabelList.SetSelectValue(bazel.InApexAxis, bazel.InApex, staticExcludes)
1006
1007 la.deps.Append(staticExcludesLabelList)
1008 la.implementationDeps.Append(staticExcludesLabelList)
Vinh Tran85fb07c2022-09-16 16:17:48 -04001009}
1010
Jingwen Chen55bc8202021-11-02 06:40:51 +00001011func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, isBinary bool, axis bazel.ConfigurationAxis, config string, props *BaseLinkerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -04001012 // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module
1013 var axisFeatures []string
Liz Kammer7a210ac2021-09-22 15:52:58 -04001014
Liz Kammercc2c1ef2022-03-21 09:03:29 -04001015 wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs)
Liz Kammerbaced712022-09-16 09:01:29 -04001016 staticLibs := android.FirstUniqueStrings(android.RemoveListFromList(props.Static_libs, wholeStaticLibs))
1017 if axis == bazel.NoConfigAxis {
1018 la.useVersionLib.SetSelectValue(axis, config, props.Use_version_lib)
1019 if proptools.Bool(props.Use_version_lib) {
1020 versionLibAlreadyInDeps := android.InList(versionLib, wholeStaticLibs)
1021 // remove from static libs so there is no duplicate dependency
1022 _, staticLibs = android.RemoveFromList(versionLib, staticLibs)
1023 // only add the dep if it is not in progress
1024 if !versionLibAlreadyInDeps {
1025 if isBinary {
1026 wholeStaticLibs = append(wholeStaticLibs, versionLib)
1027 } else {
1028 la.implementationWholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, []string{versionLib}, props.Exclude_static_libs))
1029 }
1030 }
1031 }
1032 }
1033
Liz Kammere6583482021-10-19 13:56:10 -04001034 // Excludes to parallel Soong:
1035 // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0
Liz Kammerbaced712022-09-16 09:01:29 -04001036 la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs))
Liz Kammercc2c1ef2022-03-21 09:03:29 -04001037
Vinh Tran85fb07c2022-09-16 16:17:48 -04001038 staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(
1039 ctx,
1040 !isBinary,
1041 staticLibs,
Liz Kammer748d7072023-01-25 12:07:43 -05001042 props.Exclude_static_libs,
Vinh Tran85fb07c2022-09-16 16:17:48 -04001043 props.Export_static_lib_headers,
1044 bazelLabelForStaticDepsExcludes,
1045 )
Liz Kammer7a210ac2021-09-22 15:52:58 -04001046
Liz Kammere6583482021-10-19 13:56:10 -04001047 headerLibs := android.FirstUniqueStrings(props.Header_libs)
1048 hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps)
Jingwen Chen63930982021-03-24 10:04:33 -04001049
Liz Kammere6583482021-10-19 13:56:10 -04001050 (&hDeps.export).Append(staticDeps.export)
1051 la.deps.SetSelectValue(axis, config, hDeps.export)
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001052
Liz Kammere6583482021-10-19 13:56:10 -04001053 (&hDeps.implementation).Append(staticDeps.implementation)
1054 la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation)
Liz Kammer0eae52e2021-10-06 10:32:26 -04001055
Liz Kammere6583482021-10-19 13:56:10 -04001056 systemSharedLibs := props.System_shared_libs
1057 // systemSharedLibs distinguishes between nil/empty list behavior:
1058 // nil -> use default values
1059 // empty list -> no values specified
1060 if len(systemSharedLibs) > 0 {
1061 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
1062 }
1063 la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
1064
1065 sharedLibs := android.FirstUniqueStrings(props.Shared_libs)
Liz Kammer54309532021-12-14 12:21:22 -05001066 excludeSharedLibs := props.Exclude_shared_libs
1067 usedSystem := android.FilterListPred(sharedLibs, func(s string) bool {
1068 return android.InList(s, soongSystemSharedLibs) && !android.InList(s, excludeSharedLibs)
1069 })
1070 for _, el := range usedSystem {
1071 if la.usedSystemDynamicDepAsDynamicDep == nil {
1072 la.usedSystemDynamicDepAsDynamicDep = map[string]bool{}
1073 }
1074 la.usedSystemDynamicDepAsDynamicDep[el] = true
1075 }
1076
Vinh Tran85fb07c2022-09-16 16:17:48 -04001077 sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(
1078 ctx,
1079 !isBinary,
1080 sharedLibs,
Liz Kammer748d7072023-01-25 12:07:43 -05001081 props.Exclude_shared_libs,
Vinh Tran85fb07c2022-09-16 16:17:48 -04001082 props.Export_shared_lib_headers,
1083 bazelLabelForSharedDepsExcludes,
1084 )
Liz Kammere6583482021-10-19 13:56:10 -04001085 la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export)
1086 la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation)
Liz Kammer748d7072023-01-25 12:07:43 -05001087 la.resolveTargetApexProp(ctx, props)
Vinh Tran85fb07c2022-09-16 16:17:48 -04001088
Wei Li81852ca2022-07-27 00:22:06 -07001089 if axis == bazel.NoConfigAxis || (axis == bazel.OsConfigurationAxis && config == bazel.OsAndroid) {
Yu Liu10174ff2023-02-21 12:05:26 -08001090 // If a dependency in la.implementationDynamicDeps or la.dynamicDeps has stubs, its
1091 // stub variant should be used when the dependency is linked in a APEX. The
1092 // dependencies in NoConfigAxis and OsConfigurationAxis/OsAndroid are grouped by
1093 // having stubs or not, so Bazel select() statement can be used to choose
1094 // source/stub variants of them.
1095 setStubsForDynamicDeps(ctx, axis, config, sharedDeps.export, &la.dynamicDeps, 0)
1096 setStubsForDynamicDeps(ctx, axis, config, sharedDeps.implementation, &la.implementationDynamicDeps, 1)
Wei Li81852ca2022-07-27 00:22:06 -07001097 }
Liz Kammere6583482021-10-19 13:56:10 -04001098
1099 if !BoolDefault(props.Pack_relocations, packRelocationsDefault) {
1100 axisFeatures = append(axisFeatures, "disable_pack_relocations")
1101 }
1102
1103 if Bool(props.Allow_undefined_symbols) {
1104 axisFeatures = append(axisFeatures, "-no_undefined_symbols")
1105 }
1106
1107 var linkerFlags []string
1108 if len(props.Ldflags) > 0 {
Liz Kammerf38a8372022-02-04 15:39:00 -05001109 linkerFlags = append(linkerFlags, proptools.NinjaEscapeList(props.Ldflags)...)
Liz Kammere6583482021-10-19 13:56:10 -04001110 // binaries remove static flag if -shared is in the linker flags
1111 if isBinary && android.InList("-shared", linkerFlags) {
1112 axisFeatures = append(axisFeatures, "-static_flag")
1113 }
1114 }
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +00001115
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001116 if !props.libCrt() {
1117 axisFeatures = append(axisFeatures, "-use_libcrt")
1118 }
1119 if !props.crt() {
1120 axisFeatures = append(axisFeatures, "-link_crt")
1121 }
1122
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +00001123 // This must happen before the addition of flags for Version Script and
1124 // Dynamic List, as these flags must be split on spaces and those must not
1125 linkerFlags = parseCommandLineFlags(linkerFlags, filterOutClangUnknownCflags)
1126
Trevor Radcliffe37ec2f72022-09-27 01:46:01 +00001127 additionalLinkerInputs := bazel.LabelList{}
Liz Kammere6583482021-10-19 13:56:10 -04001128 if props.Version_script != nil {
1129 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script)
Trevor Radcliffe37ec2f72022-09-27 01:46:01 +00001130 additionalLinkerInputs.Add(&label)
Liz Kammere6583482021-10-19 13:56:10 -04001131 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label))
1132 }
Alix773adaa2022-04-27 17:49:34 +00001133
1134 if props.Dynamic_list != nil {
1135 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Dynamic_list)
Trevor Radcliffe37ec2f72022-09-27 01:46:01 +00001136 additionalLinkerInputs.Add(&label)
Alix773adaa2022-04-27 17:49:34 +00001137 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--dynamic-list,$(location %s)", label.Label))
1138 }
1139
Trevor Radcliffe37ec2f72022-09-27 01:46:01 +00001140 la.additionalLinkerInputs.SetSelectValue(axis, config, additionalLinkerInputs)
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +00001141 la.linkopts.SetSelectValue(axis, config, linkerFlags)
Liz Kammere6583482021-10-19 13:56:10 -04001142
1143 if axisFeatures != nil {
1144 la.features.SetSelectValue(axis, config, axisFeatures)
1145 }
Cole Faust6b29f592022-08-09 09:50:56 -07001146
1147 runtimeDeps := android.BazelLabelForModuleDepsExcludes(ctx, props.Runtime_libs, props.Exclude_runtime_libs)
1148 if !runtimeDeps.IsEmpty() {
1149 la.runtimeDeps.SetSelectValue(axis, config, runtimeDeps)
1150 }
Liz Kammere6583482021-10-19 13:56:10 -04001151}
1152
Spandan Das2518c022023-03-17 03:02:32 +00001153var (
1154 apiSurfaceModuleLibCurrentPackage = "@api_surfaces//" + android.ModuleLibApi.String() + "/current:"
1155)
1156
Yu Liu10174ff2023-02-21 12:05:26 -08001157func setStubsForDynamicDeps(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis,
1158 config string, dynamicLibs bazel.LabelList, dynamicDeps *bazel.LabelListAttribute, ind int) {
1159 depsWithStubs := []bazel.Label{}
1160 for _, l := range dynamicLibs.Includes {
1161 dep, _ := ctx.ModuleFromName(l.OriginalModuleName)
1162 if m, ok := dep.(*Module); ok && m.HasStubsVariants() {
1163 depsWithStubs = append(depsWithStubs, l)
1164 }
1165 }
1166 if len(depsWithStubs) > 0 {
1167 implDynamicDeps := bazel.SubtractBazelLabelList(dynamicLibs, bazel.MakeLabelList(depsWithStubs))
1168 dynamicDeps.SetSelectValue(axis, config, implDynamicDeps)
1169
1170 stubLibLabels := []bazel.Label{}
1171 for _, l := range depsWithStubs {
Spandan Das2518c022023-03-17 03:02:32 +00001172 stubLabelInApiSurfaces := bazel.Label{
1173 Label: apiSurfaceModuleLibCurrentPackage + l.OriginalModuleName,
1174 }
1175 stubLibLabels = append(stubLibLabels, stubLabelInApiSurfaces)
Yu Liu10174ff2023-02-21 12:05:26 -08001176 }
1177 inApexSelectValue := dynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex)
1178 nonApexSelectValue := dynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex)
1179 defaultSelectValue := dynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey)
1180 if axis == bazel.NoConfigAxis {
1181 (&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels))
1182 (&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
1183 (&defaultSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
1184 dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, bazel.FirstUniqueBazelLabelList(inApexSelectValue))
1185 dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, bazel.FirstUniqueBazelLabelList(nonApexSelectValue))
1186 dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.FirstUniqueBazelLabelList(defaultSelectValue))
1187 } else if config == bazel.OsAndroid {
1188 (&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels))
1189 (&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
1190 dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, bazel.FirstUniqueBazelLabelList(inApexSelectValue))
1191 dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, bazel.FirstUniqueBazelLabelList(nonApexSelectValue))
1192 }
1193 }
1194}
1195
Jingwen Chen55bc8202021-11-02 06:40:51 +00001196func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) {
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001197 bp2BuildPropParseHelper(ctx, module, &StripProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1198 if stripProperties, ok := props.(*StripProperties); ok {
1199 la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols)
1200 la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list)
1201 la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame)
1202 la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All)
1203 la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None)
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001204 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001205 })
Liz Kammere6583482021-10-19 13:56:10 -04001206}
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001207
Jingwen Chen55bc8202021-11-02 06:40:51 +00001208func (la *linkerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Jingwen Chen6ada5892021-09-17 11:38:09 +00001209
Liz Kammer47535c52021-06-02 16:02:22 -04001210 type productVarDep struct {
1211 // the name of the corresponding excludes field, if one exists
1212 excludesField string
1213 // reference to the bazel attribute that should be set for the given product variable config
1214 attribute *bazel.LabelListAttribute
Liz Kammer2d7bbe32021-06-10 18:20:06 -04001215
Jingwen Chen55bc8202021-11-02 06:40:51 +00001216 depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList
Liz Kammer47535c52021-06-02 16:02:22 -04001217 }
1218
Zi Wang0a8a1292022-08-30 06:27:01 +00001219 // an intermediate attribute that holds Header_libs info, and will be appended to
1220 // implementationDeps at the end, to solve the confliction that both header_libs
1221 // and static_libs use implementationDeps.
1222 var headerDeps bazel.LabelListAttribute
1223
Liz Kammer47535c52021-06-02 16:02:22 -04001224 productVarToDepFields := map[string]productVarDep{
1225 // product variables do not support exclude_shared_libs
Jingwen Chen55bc8202021-11-02 06:40:51 +00001226 "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes},
1227 "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes},
1228 "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes},
Zi Wang0a8a1292022-08-30 06:27:01 +00001229 "Header_libs": {attribute: &headerDeps, depResolutionFunc: bazelLabelForHeaderDepsExcludes},
Liz Kammer47535c52021-06-02 16:02:22 -04001230 }
1231
Liz Kammer47535c52021-06-02 16:02:22 -04001232 for name, dep := range productVarToDepFields {
1233 props, exists := productVariableProps[name]
1234 excludeProps, excludesExists := productVariableProps[dep.excludesField]
Sasha Smundak39a301c2022-12-29 17:11:49 -08001235 // if neither an include nor excludes property exists, then skip it
Liz Kammer47535c52021-06-02 16:02:22 -04001236 if !exists && !excludesExists {
1237 continue
1238 }
Jingwen Chen25825ca2021-11-15 12:28:43 +00001239 // Collect all the configurations that an include or exclude property exists for.
1240 // We want to iterate all configurations rather than either the include or exclude because, for a
1241 // particular configuration, we may have either only an include or an exclude to handle.
1242 productConfigProps := make(map[android.ProductConfigProperty]bool, len(props)+len(excludeProps))
1243 for p := range props {
1244 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -04001245 }
Jingwen Chen25825ca2021-11-15 12:28:43 +00001246 for p := range excludeProps {
1247 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -04001248 }
1249
Jingwen Chen25825ca2021-11-15 12:28:43 +00001250 for productConfigProp := range productConfigProps {
1251 prop, includesExists := props[productConfigProp]
1252 excludesProp, excludesExists := excludeProps[productConfigProp]
Liz Kammer47535c52021-06-02 16:02:22 -04001253 var includes, excludes []string
1254 var ok bool
1255 // if there was no includes/excludes property, casting fails and that's expected
Jingwen Chen25825ca2021-11-15 12:28:43 +00001256 if includes, ok = prop.([]string); includesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -04001257 ctx.ModuleErrorf("Could not convert product variable %s property", name)
1258 }
Jingwen Chen25825ca2021-11-15 12:28:43 +00001259 if excludes, ok = excludesProp.([]string); excludesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -04001260 ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
1261 }
Liz Kammer2d7bbe32021-06-10 18:20:06 -04001262
Jingwen Chen58ff6802021-11-17 12:14:41 +00001263 dep.attribute.EmitEmptyList = productConfigProp.AlwaysEmit()
Jingwen Chen25825ca2021-11-15 12:28:43 +00001264 dep.attribute.SetSelectValue(
1265 productConfigProp.ConfigurationAxis(),
1266 productConfigProp.SelectKey(),
1267 dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes),
1268 )
Liz Kammer47535c52021-06-02 16:02:22 -04001269 }
1270 }
Zi Wang0a8a1292022-08-30 06:27:01 +00001271 la.implementationDeps.Append(headerDeps)
Liz Kammere6583482021-10-19 13:56:10 -04001272}
Liz Kammer47535c52021-06-02 16:02:22 -04001273
Liz Kammer54309532021-12-14 12:21:22 -05001274func (la *linkerAttributes) finalize(ctx android.BazelConversionPathContext) {
1275 // if system dynamic deps have the default value, any use of a system dynamic library used will
1276 // result in duplicate library errors for bionic OSes. Here, we explicitly exclude those libraries
Liz Kammer43345e22022-08-04 13:57:35 -04001277 // from bionic OSes and the no config case as these libraries only build for bionic OSes.
Liz Kammer54309532021-12-14 12:21:22 -05001278 if la.systemDynamicDeps.IsNil() && len(la.usedSystemDynamicDepAsDynamicDep) > 0 {
Cole Faust18994c72023-02-28 16:02:16 -08001279 toRemove := bazelLabelForSharedDeps(ctx, android.SortedKeys(la.usedSystemDynamicDepAsDynamicDep))
Liz Kammer43345e22022-08-04 13:57:35 -04001280 la.dynamicDeps.Exclude(bazel.NoConfigAxis, "", toRemove)
Liz Kammer54309532021-12-14 12:21:22 -05001281 la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove)
1282 la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove)
Liz Kammer91487d42022-09-13 11:27:11 -04001283 la.implementationDynamicDeps.Exclude(bazel.NoConfigAxis, "", toRemove)
Liz Kammer54309532021-12-14 12:21:22 -05001284 la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove)
1285 la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove)
Liz Kammer91487d42022-09-13 11:27:11 -04001286
1287 la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, toRemove)
1288 la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, toRemove)
1289 stubsToRemove := make([]bazel.Label, 0, len(la.usedSystemDynamicDepAsDynamicDep))
1290 for _, lib := range toRemove.Includes {
Spandan Das2518c022023-03-17 03:02:32 +00001291 stubLabelInApiSurfaces := bazel.Label{
1292 Label: apiSurfaceModuleLibCurrentPackage + lib.OriginalModuleName,
1293 }
1294 stubsToRemove = append(stubsToRemove, stubLabelInApiSurfaces)
Liz Kammer91487d42022-09-13 11:27:11 -04001295 }
1296 la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, bazel.MakeLabelList(stubsToRemove))
Liz Kammer54309532021-12-14 12:21:22 -05001297 }
1298
Liz Kammere6583482021-10-19 13:56:10 -04001299 la.deps.ResolveExcludes()
1300 la.implementationDeps.ResolveExcludes()
1301 la.dynamicDeps.ResolveExcludes()
1302 la.implementationDynamicDeps.ResolveExcludes()
1303 la.wholeArchiveDeps.ResolveExcludes()
1304 la.systemDynamicDeps.ForceSpecifyEmptyList = true
Liz Kammer54309532021-12-14 12:21:22 -05001305
Jingwen Chen91220d72021-03-24 02:18:33 -04001306}
1307
Jingwen Chened9c17d2021-04-13 07:14:55 +00001308// Relativize a list of root-relative paths with respect to the module's
1309// directory.
1310//
1311// include_dirs Soong prop are root-relative (b/183742505), but
1312// local_include_dirs, export_include_dirs and export_system_include_dirs are
1313// module dir relative. This function makes a list of paths entirely module dir
1314// relative.
1315//
1316// For the `include` attribute, Bazel wants the paths to be relative to the
1317// module.
1318func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +00001319 var relativePaths []string
1320 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +00001321 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
1322 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
1323 if err != nil {
1324 panic(err)
1325 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +00001326 relativePaths = append(relativePaths, relativePath)
1327 }
1328 return relativePaths
1329}
1330
Liz Kammer5fad5012021-09-09 14:08:21 -04001331// BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel
1332// attributes.
1333type BazelIncludes struct {
Liz Kammer1263d9b2021-12-10 14:28:20 -05001334 AbsoluteIncludes bazel.StringListAttribute
1335 Includes bazel.StringListAttribute
1336 SystemIncludes bazel.StringListAttribute
Liz Kammer5fad5012021-09-09 14:08:21 -04001337}
1338
Liz Kammer54549442022-05-11 13:55:06 -04001339func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module, includes *BazelIncludes) BazelIncludes {
Liz Kammer1263d9b2021-12-10 14:28:20 -05001340 var exported BazelIncludes
1341 if includes != nil {
1342 exported = *includes
1343 } else {
1344 exported = BazelIncludes{}
1345 }
Zi Wang1cb11802022-12-09 16:08:54 -08001346
1347 // cc library Export_include_dirs and Export_system_include_dirs are marked
1348 // "variant_prepend" in struct tag, set their prepend property to true to make
1349 // sure bp2build generates correct result.
1350 exported.Includes.Prepend = true
1351 exported.SystemIncludes.Prepend = true
1352
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001353 bp2BuildPropParseHelper(ctx, module, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1354 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
1355 if len(flagExporterProperties.Export_include_dirs) > 0 {
1356 exported.Includes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.Includes.SelectValue(axis, config), flagExporterProperties.Export_include_dirs...)))
1357 }
1358 if len(flagExporterProperties.Export_system_include_dirs) > 0 {
1359 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 -04001360 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -04001361 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001362 })
Liz Kammer1263d9b2021-12-10 14:28:20 -05001363 exported.AbsoluteIncludes.DeduplicateAxesFromBase()
Liz Kammer5fad5012021-09-09 14:08:21 -04001364 exported.Includes.DeduplicateAxesFromBase()
1365 exported.SystemIncludes.DeduplicateAxesFromBase()
Rupert Shuttleworth375451e2021-04-26 07:49:08 -04001366
Liz Kammer5fad5012021-09-09 14:08:21 -04001367 return exported
Jingwen Chen91220d72021-03-24 02:18:33 -04001368}
Chris Parsons953b3562021-09-20 15:14:39 -04001369
Trevor Radcliffecee4e052022-09-06 19:31:25 +00001370func BazelLabelNameForStaticModule(baseLabel string) string {
1371 return baseLabel + "_bp2build_cc_library_static"
1372}
1373
Jingwen Chen55bc8202021-11-02 06:40:51 +00001374func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001375 label := android.BazelModuleLabel(ctx, m)
Sasha Smundak39a301c2022-12-29 17:11:49 -08001376 if ccModule, ok := m.(*Module); ok && ccModule.typ() == fullLibrary {
Trevor Radcliffecee4e052022-09-06 19:31:25 +00001377 return BazelLabelNameForStaticModule(label)
Chris Parsons953b3562021-09-20 15:14:39 -04001378 }
1379 return label
1380}
1381
Jingwen Chen55bc8202021-11-02 06:40:51 +00001382func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001383 // cc_library, at it's root name, propagates the shared library, which depends on the static
1384 // library.
1385 return android.BazelModuleLabel(ctx, m)
1386}
1387
Jingwen Chen55bc8202021-11-02 06:40:51 +00001388func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001389 label := bazelLabelForStaticModule(ctx, m)
1390 if aModule, ok := m.(android.Module); ok {
1391 if android.IsModulePrebuilt(aModule) {
1392 label += "_alwayslink"
1393 }
1394 }
1395 return label
1396}
1397
Jingwen Chen55bc8202021-11-02 06:40:51 +00001398func bazelLabelForWholeDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001399 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps)
1400}
1401
Jingwen Chen55bc8202021-11-02 06:40:51 +00001402func bazelLabelForWholeDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001403 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps)
1404}
1405
Jingwen Chen55bc8202021-11-02 06:40:51 +00001406func bazelLabelForStaticDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001407 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule)
1408}
1409
Jingwen Chen55bc8202021-11-02 06:40:51 +00001410func bazelLabelForStaticDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001411 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule)
1412}
1413
Jingwen Chen55bc8202021-11-02 06:40:51 +00001414func bazelLabelForSharedDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001415 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule)
1416}
1417
Jingwen Chen55bc8202021-11-02 06:40:51 +00001418func bazelLabelForHeaderDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001419 // This is not elegant, but bp2build's shared library targets only propagate
1420 // their header information as part of the normal C++ provider.
1421 return bazelLabelForSharedDeps(ctx, modules)
1422}
1423
Zi Wang0a8a1292022-08-30 06:27:01 +00001424func bazelLabelForHeaderDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
1425 // This is only used when product_variable header_libs is processed, to follow
1426 // the pattern of depResolutionFunc
1427 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
1428}
1429
Jingwen Chen55bc8202021-11-02 06:40:51 +00001430func bazelLabelForSharedDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001431 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
1432}
Liz Kammer2b8004b2021-10-04 13:55:44 -04001433
1434type binaryLinkerAttrs struct {
1435 Linkshared *bool
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05001436 Suffix bazel.StringAttribute
Liz Kammer2b8004b2021-10-04 13:55:44 -04001437}
1438
Jingwen Chen55bc8202021-11-02 06:40:51 +00001439func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs {
Liz Kammer2b8004b2021-10-04 13:55:44 -04001440 attrs := binaryLinkerAttrs{}
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001441 bp2BuildPropParseHelper(ctx, m, &BinaryLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1442 linkerProps := props.(*BinaryLinkerProperties)
1443 staticExecutable := linkerProps.Static_executable
1444 if axis == bazel.NoConfigAxis {
1445 if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared {
1446 attrs.Linkshared = &linkBinaryShared
Liz Kammer2b8004b2021-10-04 13:55:44 -04001447 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001448 } else if staticExecutable != nil {
1449 // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a
1450 // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling
1451 ctx.ModuleErrorf("bp2build cannot migrate a module with arch/target-specific static_executable values")
Liz Kammer2b8004b2021-10-04 13:55:44 -04001452 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05001453 if suffix := linkerProps.Suffix; suffix != nil {
1454 attrs.Suffix.SetSelectValue(axis, config, suffix)
1455 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001456 })
Liz Kammer2b8004b2021-10-04 13:55:44 -04001457
1458 return attrs
1459}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00001460
1461func bp2buildSanitizerFeatures(ctx android.BazelConversionPathContext, m *Module) bazel.StringListAttribute {
1462 sanitizerFeatures := bazel.StringListAttribute{}
1463 bp2BuildPropParseHelper(ctx, m, &SanitizeProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1464 var features []string
1465 if sanitizerProps, ok := props.(*SanitizeProperties); ok {
1466 if sanitizerProps.Sanitize.Integer_overflow != nil && *sanitizerProps.Sanitize.Integer_overflow {
1467 features = append(features, "ubsan_integer_overflow")
1468 }
1469 for _, sanitizer := range sanitizerProps.Sanitize.Misc_undefined {
1470 features = append(features, "ubsan_"+sanitizer)
1471 }
1472 sanitizerFeatures.SetSelectValue(axis, config, features)
1473 }
1474 })
1475 return sanitizerFeatures
1476}
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00001477
1478func bp2buildLtoFeatures(ctx android.BazelConversionPathContext, m *Module) bazel.StringListAttribute {
1479 lto_feature_name := "android_thin_lto"
1480 ltoBoolFeatures := bazel.BoolAttribute{}
1481 bp2BuildPropParseHelper(ctx, m, &LTOProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1482 if ltoProps, ok := props.(*LTOProperties); ok {
1483 thinProp := ltoProps.Lto.Thin != nil && *ltoProps.Lto.Thin
1484 thinPropSetToFalse := ltoProps.Lto.Thin != nil && !*ltoProps.Lto.Thin
1485 neverProp := ltoProps.Lto.Never != nil && *ltoProps.Lto.Never
1486 if thinProp {
1487 ltoBoolFeatures.SetSelectValue(axis, config, BoolPtr(true))
1488 return
1489 }
1490 if neverProp || thinPropSetToFalse {
1491 if thinProp {
1492 ctx.ModuleErrorf("lto.thin and lto.never are mutually exclusive but were specified together")
1493 } else {
1494 ltoBoolFeatures.SetSelectValue(axis, config, BoolPtr(false))
1495 }
1496 return
1497 }
1498 }
1499 ltoBoolFeatures.SetSelectValue(axis, config, nil)
1500 })
1501
1502 props := m.GetArchVariantProperties(ctx, &LTOProperties{})
1503 ltoStringFeatures, err := ltoBoolFeatures.ToStringListAttribute(func(boolPtr *bool, axis bazel.ConfigurationAxis, config string) []string {
1504 if boolPtr == nil {
1505 return []string{}
1506 }
1507 if !*boolPtr {
1508 return []string{"-" + lto_feature_name}
1509 }
1510 features := []string{lto_feature_name}
1511 if ltoProps, ok := props[axis][config].(*LTOProperties); ok {
1512 if ltoProps.Whole_program_vtables != nil && *ltoProps.Whole_program_vtables {
1513 features = append(features, "android_thin_lto_whole_program_vtables")
1514 }
1515 }
1516 return features
1517 })
1518 if err != nil {
1519 ctx.ModuleErrorf("Error processing LTO attributes: %s", err)
1520 }
1521 return ltoStringFeatures
1522}