blob: 8644bf6804201c8b9945b0602c1b1dc601d6ac5c [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 {
74 Tidy *bool
75 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 {
85 moduleAttrs.Tidy = tidy.Properties.Tidy
86 moduleAttrs.Tidy_flags = tidy.Properties.Tidy_flags
87 moduleAttrs.Tidy_checks = tidy.Properties.Tidy_checks
88 moduleAttrs.Tidy_checks_as_errors = tidy.Properties.Tidy_checks_as_errors
89 }
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -040090
91 }
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -040092 archVariantProps := m.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
93 for axis, configToProps := range archVariantProps {
Sasha Smundak39a301c2022-12-29 17:11:49 -080094 for cfg, _props := range configToProps {
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -040095 if archProps, ok := _props.(*BaseCompilerProperties); ok {
96 archDisabledSrcs := android.BazelLabelForModuleSrc(ctx, archProps.Tidy_disabled_srcs)
Sasha Smundak39a301c2022-12-29 17:11:49 -080097 moduleAttrs.Tidy_disabled_srcs.SetSelectValue(axis, cfg, archDisabledSrcs)
Sam Delmerico4c902d62022-11-02 14:17:15 -040098 archTimeoutSrcs := android.BazelLabelForModuleSrc(ctx, archProps.Tidy_timeout_srcs)
Sasha Smundak39a301c2022-12-29 17:11:49 -080099 moduleAttrs.Tidy_timeout_srcs.SetSelectValue(axis, cfg, archTimeoutSrcs)
Sam Delmericoc9b8fbd2022-10-25 15:47:17 -0400100 }
101 }
Sam Delmericofb3bb322022-10-21 10:42:24 -0400102 }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000103}
104
Sam Delmericoc7681022022-02-04 21:01:20 +0000105// groupSrcsByExtension partitions `srcs` into groups based on file extension.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000106func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute {
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400107 // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl
108 // macro.
109 addSuffixForFilegroup := func(suffix string) bazel.LabelMapper {
Vinh Tran9f6796a2022-08-16 13:10:31 -0400110 return func(otherModuleCtx bazel.OtherModuleContext, label bazel.Label) (string, bool) {
111
112 m, exists := otherModuleCtx.ModuleFromName(label.OriginalModuleName)
Liz Kammer12615db2021-09-28 09:19:17 -0400113 labelStr := label.Label
Vinh Tran9f6796a2022-08-16 13:10:31 -0400114 if !exists || !android.IsFilegroup(otherModuleCtx, m) {
115 return labelStr, false
116 }
Yu Liu2aa806b2022-09-01 11:54:47 -0700117 // If the filegroup is already converted to aidl_library or proto_library,
118 // skip creating _c_srcs, _as_srcs, _cpp_srcs filegroups
119 fg, _ := m.(android.FileGroupAsLibrary)
120 if fg.ShouldConvertToAidlLibrary(ctx) || fg.ShouldConvertToProtoLibrary(ctx) {
Liz Kammer12615db2021-09-28 09:19:17 -0400121 return labelStr, false
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000122 }
Liz Kammer12615db2021-09-28 09:19:17 -0400123 return labelStr + suffix, true
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400124 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000125 }
126
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400127 // TODO(b/190006308): Handle language detection of sources in a Bazel rule.
Sam Delmericoc7681022022-02-04 21:01:20 +0000128 labels := bazel.LabelPartitions{
129 protoSrcPartition: android.ProtoSrcLabelPartition,
Liz Kammeraabfb5d2021-12-08 15:25:06 -0500130 cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")},
131 asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")},
Cole Faust7071a052022-07-29 15:58:33 -0700132 asmSrcPartition: bazel.LabelPartition{Extensions: []string{".asm"}},
Vinh Tran9f6796a2022-08-16 13:10:31 -0400133 aidlSrcPartition: android.AidlSrcLabelPartition,
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000134 // TODO(http://b/231968910): If there is ever a filegroup target that
135 // contains .l or .ll files we will need to find a way to add a
136 // LabelMapper for these that identifies these filegroups and
137 // converts them appropriately
138 lSrcPartition: bazel.LabelPartition{Extensions: []string{".l"}},
139 llSrcPartition: bazel.LabelPartition{Extensions: []string{".ll"}},
Liz Kammer57e2e7a2021-09-20 12:55:02 -0400140 // C++ is the "catch-all" group, and comprises generated sources because we don't
141 // know the language of these sources until the genrule is executed.
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000142 cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true},
143 syspropSrcPartition: bazel.LabelPartition{Extensions: []string{".sysprop"}},
Sam Delmericoc7681022022-02-04 21:01:20 +0000144 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000145
Sam Delmericoc7681022022-02-04 21:01:20 +0000146 return bazel.PartitionLabelListAttribute(ctx, &srcs, labels)
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000147}
148
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000149// bp2BuildParseLibProps returns the attributes for a variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000150func bp2BuildParseLibProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000151 lib, ok := module.compiler.(*libraryDecorator)
152 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400153 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000154 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000155 return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic)
156}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000157
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000158// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000159func bp2BuildParseSharedProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000160 return bp2BuildParseLibProps(ctx, module, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000161}
162
163// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000164func bp2BuildParseStaticProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000165 return bp2BuildParseLibProps(ctx, module, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400166}
167
Liz Kammer7a210ac2021-09-22 15:52:58 -0400168type depsPartition struct {
169 export bazel.LabelList
170 implementation bazel.LabelList
171}
172
Jingwen Chen55bc8202021-11-02 06:40:51 +0000173type bazelLabelForDepsFn func(android.BazelConversionPathContext, []string) bazel.LabelList
Liz Kammer7a210ac2021-09-22 15:52:58 -0400174
Jingwen Chen55bc8202021-11-02 06:40:51 +0000175func maybePartitionExportedAndImplementationsDeps(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, exportedDeps []string, fn bazelLabelForDepsFn) depsPartition {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400176 if !exportsDeps {
177 return depsPartition{
178 implementation: fn(ctx, allDeps),
179 }
180 }
181
Liz Kammer7a210ac2021-09-22 15:52:58 -0400182 implementation, export := android.FilterList(allDeps, exportedDeps)
183
184 return depsPartition{
185 export: fn(ctx, export),
186 implementation: fn(ctx, implementation),
187 }
188}
189
Jingwen Chen55bc8202021-11-02 06:40:51 +0000190type bazelLabelForDepsExcludesFn func(android.BazelConversionPathContext, []string, []string) bazel.LabelList
Liz Kammer7a210ac2021-09-22 15:52:58 -0400191
Jingwen Chen55bc8202021-11-02 06:40:51 +0000192func maybePartitionExportedAndImplementationsDepsExcludes(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, excludes, exportedDeps []string, fn bazelLabelForDepsExcludesFn) depsPartition {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400193 if !exportsDeps {
194 return depsPartition{
195 implementation: fn(ctx, allDeps, excludes),
196 }
197 }
Liz Kammer7a210ac2021-09-22 15:52:58 -0400198 implementation, export := android.FilterList(allDeps, exportedDeps)
199
200 return depsPartition{
201 export: fn(ctx, export, excludes),
202 implementation: fn(ctx, implementation, excludes),
203 }
204}
205
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000206func bp2BuildPropParseHelper(ctx android.ArchVariantContext, module *Module, propsType interface{}, parseFunc func(axis bazel.ConfigurationAxis, config string, props interface{})) {
207 for axis, configToProps := range module.GetArchVariantProperties(ctx, propsType) {
Sasha Smundak39a301c2022-12-29 17:11:49 -0800208 for cfg, props := range configToProps {
209 parseFunc(axis, cfg, props)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000210 }
211 }
212}
213
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000214// Parses properties common to static and shared libraries. Also used for prebuilt libraries.
Sasha Smundak39a301c2022-12-29 17:11:49 -0800215func bp2buildParseStaticOrSharedProps(ctx android.BazelConversionPathContext, module *Module, _ *libraryDecorator, isStatic bool) staticOrSharedAttributes {
Liz Kammer135bf552021-08-11 10:46:06 -0400216 attrs := staticOrSharedAttributes{}
Jingwen Chenbcf53042021-05-26 04:42:42 +0000217
Liz Kammer9abd62d2021-05-21 08:37:59 -0400218 setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000219 attrs.Copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, filterOutStdFlag))
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000220 attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
Chris Parsons953b3562021-09-20 15:14:39 -0400221 attrs.System_dynamic_deps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, props.System_shared_libs))
Liz Kammer7a210ac2021-09-22 15:52:58 -0400222
Liz Kammer2b8004b2021-10-04 13:55:44 -0400223 staticDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Static_libs, props.Export_static_lib_headers, bazelLabelForStaticDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400224 attrs.Deps.SetSelectValue(axis, config, staticDeps.export)
225 attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation)
226
Liz Kammer2b8004b2021-10-04 13:55:44 -0400227 sharedDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400228 attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export)
229 attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation)
230
231 attrs.Whole_archive_deps.SetSelectValue(axis, config, bazelLabelForWholeDeps(ctx, props.Whole_static_libs))
Chris Parsons58852a02021-12-09 18:10:18 -0500232 attrs.Enabled.SetSelectValue(axis, config, props.Enabled)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000233 }
Liz Kammer135bf552021-08-11 10:46:06 -0400234 // system_dynamic_deps distinguishes between nil/empty list behavior:
235 // nil -> use default values
236 // empty list -> no values specified
237 attrs.System_dynamic_deps.ForceSpecifyEmptyList = true
Jingwen Chenbcf53042021-05-26 04:42:42 +0000238
239 if isStatic {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000240 bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
241 if staticOrSharedProps, ok := props.(*StaticProperties); ok {
242 setAttrs(axis, config, staticOrSharedProps.Static)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000243 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000244 })
Jingwen Chenbcf53042021-05-26 04:42:42 +0000245 } else {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000246 bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
247 if staticOrSharedProps, ok := props.(*SharedProperties); ok {
248 setAttrs(axis, config, staticOrSharedProps.Shared)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000249 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000250 })
Jingwen Chenbcf53042021-05-26 04:42:42 +0000251 }
252
Liz Kammerae3994e2021-10-19 09:45:48 -0400253 partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs)
254 attrs.Srcs = partitionedSrcs[cppSrcPartition]
255 attrs.Srcs_c = partitionedSrcs[cSrcPartition]
256 attrs.Srcs_as = partitionedSrcs[asSrcPartition]
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000257
Liz Kammer12615db2021-09-28 09:19:17 -0400258 if !partitionedSrcs[protoSrcPartition].IsEmpty() {
259 // TODO(b/208815215): determine whether this is used and add support if necessary
260 ctx.ModuleErrorf("Migrating static/shared only proto srcs is not currently supported")
261 }
262
Jingwen Chenbcf53042021-05-26 04:42:42 +0000263 return attrs
Jingwen Chen53681ef2021-04-29 08:15:13 +0000264}
265
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400266// Convenience struct to hold all attributes parsed from prebuilt properties.
267type prebuiltAttributes struct {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000268 Src bazel.LabelAttribute
269 Enabled bazel.BoolAttribute
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400270}
271
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000272func parseSrc(ctx android.BazelConversionPathContext, srcLabelAttribute *bazel.LabelAttribute, axis bazel.ConfigurationAxis, config string, srcs []string) {
273 srcFileError := func() {
274 ctx.ModuleErrorf("parseSrc: Expected at most one source file for %s %s\n", axis, config)
275 }
276 if len(srcs) > 1 {
277 srcFileError()
278 return
279 } else if len(srcs) == 0 {
280 return
281 }
282 if srcLabelAttribute.SelectValue(axis, config) != nil {
283 srcFileError()
284 return
285 }
286 srcLabelAttribute.SetSelectValue(axis, config, android.BazelLabelForModuleSrcSingle(ctx, srcs[0]))
287}
288
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000289// NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000290func 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 +0000291
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400292 var srcLabelAttribute bazel.LabelAttribute
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000293 bp2BuildPropParseHelper(ctx, module, &prebuiltLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
294 if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000295 parseSrc(ctx, &srcLabelAttribute, axis, config, prebuiltLinkerProperties.Srcs)
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000296 }
297 })
298
299 var enabledLabelAttribute bazel.BoolAttribute
300 parseAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
301 if props.Enabled != nil {
302 enabledLabelAttribute.SetSelectValue(axis, config, props.Enabled)
303 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000304 parseSrc(ctx, &srcLabelAttribute, axis, config, props.Srcs)
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000305 }
306
307 if isStatic {
308 bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
309 if staticProperties, ok := props.(*StaticProperties); ok {
310 parseAttrs(axis, config, staticProperties.Static)
311 }
312 })
313 } else {
314 bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
315 if sharedProperties, ok := props.(*SharedProperties); ok {
316 parseAttrs(axis, config, sharedProperties.Shared)
317 }
318 })
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400319 }
320
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400321 return prebuiltAttributes{
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000322 Src: srcLabelAttribute,
323 Enabled: enabledLabelAttribute,
324 }
325}
326
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000327func bp2BuildParsePrebuiltBinaryProps(ctx android.BazelConversionPathContext, module *Module) prebuiltAttributes {
328 var srcLabelAttribute bazel.LabelAttribute
329 bp2BuildPropParseHelper(ctx, module, &prebuiltLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
330 if props, ok := props.(*prebuiltLinkerProperties); ok {
331 parseSrc(ctx, &srcLabelAttribute, axis, config, props.Srcs)
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000332 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxb12ff592022-09-01 15:04:04 +0000333 })
334
335 return prebuiltAttributes{
336 Src: srcLabelAttribute,
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400337 }
338}
339
Colin Crossc5075e92022-12-05 16:46:39 -0800340func bp2BuildParsePrebuiltObjectProps(ctx android.BazelConversionPathContext, module *Module) prebuiltAttributes {
341 var srcLabelAttribute bazel.LabelAttribute
342 bp2BuildPropParseHelper(ctx, module, &prebuiltObjectProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
343 if props, ok := props.(*prebuiltObjectProperties); ok {
344 parseSrc(ctx, &srcLabelAttribute, axis, config, props.Srcs)
345 }
346 })
347
348 return prebuiltAttributes{
349 Src: srcLabelAttribute,
350 }
351}
352
Liz Kammere6583482021-10-19 13:56:10 -0400353type baseAttributes struct {
354 compilerAttributes
355 linkerAttributes
Liz Kammer12615db2021-09-28 09:19:17 -0400356
Trevor Radcliffedb7e0262022-10-28 16:48:18 +0000357 // A combination of compilerAttributes.features and linkerAttributes.features, as well as sanitizer features
Cole Faust5fa4e962022-08-22 14:31:04 -0700358 features bazel.StringListAttribute
Liz Kammer12615db2021-09-28 09:19:17 -0400359 protoDependency *bazel.LabelAttribute
Vinh Tran9f6796a2022-08-16 13:10:31 -0400360 aidlDependency *bazel.LabelAttribute
Yu Liuf01a0f02022-12-07 15:45:30 -0800361 Native_coverage *bool
Liz Kammere6583482021-10-19 13:56:10 -0400362}
363
Jingwen Chen107c0de2021-04-09 10:43:12 +0000364// Convenience struct to hold all attributes parsed from compiler properties.
365type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400366 // Options for all languages
367 copts bazel.StringListAttribute
368 // Assembly options and sources
369 asFlags bazel.StringListAttribute
370 asSrcs bazel.LabelListAttribute
Cole Faust7071a052022-07-29 15:58:33 -0700371 asmSrcs bazel.LabelListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400372 // C options and sources
373 conlyFlags bazel.StringListAttribute
374 cSrcs bazel.LabelListAttribute
375 // C++ options and sources
376 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000377 srcs bazel.LabelListAttribute
Chris Parsons2c788392021-08-10 11:58:07 -0400378
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000379 // Lex sources and options
380 lSrcs bazel.LabelListAttribute
381 llSrcs bazel.LabelListAttribute
382 lexopts bazel.StringListAttribute
383
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000384 // Sysprop sources
385 syspropSrcs bazel.LabelListAttribute
386
Liz Kammere6583482021-10-19 13:56:10 -0400387 hdrs bazel.LabelListAttribute
388
Chris Parsons2c788392021-08-10 11:58:07 -0400389 rtti bazel.BoolAttribute
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000390
391 // Not affected by arch variants
392 stl *string
Chris Parsons79bd2b72021-11-29 17:52:41 -0500393 cStd *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000394 cppStd *string
Liz Kammer35687bc2021-09-10 10:07:07 -0400395
396 localIncludes bazel.StringListAttribute
397 absoluteIncludes bazel.StringListAttribute
Liz Kammer12615db2021-09-28 09:19:17 -0400398
Liz Kammer1263d9b2021-12-10 14:28:20 -0500399 includes BazelIncludes
400
Liz Kammer12615db2021-09-28 09:19:17 -0400401 protoSrcs bazel.LabelListAttribute
Vinh Tran9f6796a2022-08-16 13:10:31 -0400402 aidlSrcs bazel.LabelListAttribute
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000403
404 stubsSymbolFile *string
405 stubsVersions bazel.StringListAttribute
Cole Faust5fa4e962022-08-22 14:31:04 -0700406
407 features bazel.StringListAttribute
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500408
409 suffix bazel.StringAttribute
Vinh Tran99270ea2022-11-28 11:15:23 -0500410
411 fdoProfile bazel.LabelAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000412}
413
Liz Kammercac7f692021-12-16 14:19:32 -0500414type filterOutFn func(string) bool
415
416func filterOutStdFlag(flag string) bool {
417 return strings.HasPrefix(flag, "-std=")
418}
419
Alix1be00d42022-05-16 22:56:04 +0000420func filterOutClangUnknownCflags(flag string) bool {
421 for _, f := range config.ClangUnknownCflags {
422 if f == flag {
423 return true
424 }
425 }
426 return false
427}
428
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000429func parseCommandLineFlags(soongFlags []string, filterOut ...filterOutFn) []string {
Liz Kammere6583482021-10-19 13:56:10 -0400430 var result []string
431 for _, flag := range soongFlags {
Alix1be00d42022-05-16 22:56:04 +0000432 skipFlag := false
433 for _, filter := range filterOut {
434 if filter != nil && filter(flag) {
435 skipFlag = true
436 }
437 }
438 if skipFlag {
Liz Kammercac7f692021-12-16 14:19:32 -0500439 continue
440 }
Liz Kammere6583482021-10-19 13:56:10 -0400441 // Soong's cflags can contain spaces, like `-include header.h`. For
442 // Bazel's copts, split them up to be compatible with the
443 // no_copts_tokenization feature.
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000444 result = append(result, strings.Split(flag, " ")...)
Liz Kammere6583482021-10-19 13:56:10 -0400445 }
446 return result
447}
Jingwen Chened9c17d2021-04-13 07:14:55 +0000448
Jingwen Chen55bc8202021-11-02 06:40:51 +0000449func (ca *compilerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, props *BaseCompilerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400450 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
451 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
452 if srcsList, ok := parseSrcs(ctx, props); ok {
453 ca.srcs.SetSelectValue(axis, config, srcsList)
Chris Parsons990c4f42021-05-25 12:10:58 -0400454 }
455
Liz Kammere6583482021-10-19 13:56:10 -0400456 localIncludeDirs := props.Local_include_dirs
457 if axis == bazel.NoConfigAxis {
Chris Parsons79bd2b72021-11-29 17:52:41 -0500458 ca.cStd, ca.cppStd = bp2buildResolveCppStdValue(props.C_std, props.Cpp_std, props.Gnu_extensions)
Liz Kammere6583482021-10-19 13:56:10 -0400459 if includeBuildDirectory(props.Include_build_directory) {
460 localIncludeDirs = append(localIncludeDirs, ".")
Liz Kammer222bdcf2021-10-11 14:15:51 -0400461 }
Jingwen Chene32e9e02021-04-23 09:17:24 +0000462 }
463
Liz Kammere6583482021-10-19 13:56:10 -0400464 ca.absoluteIncludes.SetSelectValue(axis, config, props.Include_dirs)
465 ca.localIncludes.SetSelectValue(axis, config, localIncludeDirs)
466
Cole Faust5fa4e962022-08-22 14:31:04 -0700467 instructionSet := proptools.StringDefault(props.Instruction_set, "")
468 if instructionSet == "arm" {
469 ca.features.SetSelectValue(axis, config, []string{"arm_isa_arm", "-arm_isa_thumb"})
470 } else if instructionSet != "" && instructionSet != "thumb" {
471 ctx.ModuleErrorf("Unknown value for instruction_set: %s", instructionSet)
472 }
473
Liz Kammercac7f692021-12-16 14:19:32 -0500474 // In Soong, cflags occur on the command line before -std=<val> flag, resulting in the value being
475 // overridden. In Bazel we always allow overriding, via flags; however, this can cause
476 // incompatibilities, so we remove "-std=" flags from Cflag properties while leaving it in other
477 // cases.
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +0000478 ca.copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, filterOutStdFlag, filterOutClangUnknownCflags))
479 ca.asFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Asflags, nil))
480 ca.conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Conlyflags, filterOutClangUnknownCflags))
481 ca.cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Cppflags, filterOutClangUnknownCflags))
Liz Kammere6583482021-10-19 13:56:10 -0400482 ca.rtti.SetSelectValue(axis, config, props.Rtti)
483}
484
Jingwen Chen55bc8202021-11-02 06:40:51 +0000485func (ca *compilerAttributes) convertStlProps(ctx android.ArchVariantContext, module *Module) {
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000486 bp2BuildPropParseHelper(ctx, module, &StlProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
487 if stlProps, ok := props.(*StlProperties); ok {
488 if stlProps.Stl == nil {
489 return
490 }
491 if ca.stl == nil {
Liz Kammer7128d382022-05-12 11:42:33 -0400492 stl := deduplicateStlInput(*stlProps.Stl)
493 ca.stl = &stl
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000494 } else if ca.stl != stlProps.Stl {
495 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 -0400496 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000497 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +0000498 })
Liz Kammere6583482021-10-19 13:56:10 -0400499}
Jingwen Chenc1c26502021-04-05 10:35:13 +0000500
Jingwen Chen55bc8202021-11-02 06:40:51 +0000501func (ca *compilerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Liz Kammerba7a9c52021-05-26 08:45:30 -0400502 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
Liz Kammere6583482021-10-19 13:56:10 -0400503 "Cflags": &ca.copts,
504 "Asflags": &ca.asFlags,
505 "CppFlags": &ca.cppFlags,
Liz Kammerba7a9c52021-05-26 08:45:30 -0400506 }
Liz Kammerba7a9c52021-05-26 08:45:30 -0400507 for propName, attr := range productVarPropNameToAttribute {
Jingwen Chen25825ca2021-11-15 12:28:43 +0000508 if productConfigProps, exists := productVariableProps[propName]; exists {
509 for productConfigProp, prop := range productConfigProps {
510 flags, ok := prop.([]string)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400511 if !ok {
512 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
513 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000514 newFlags, _ := bazel.TryVariableSubstitutions(flags, productConfigProp.Name)
515 attr.SetSelectValue(productConfigProp.ConfigurationAxis(), productConfigProp.SelectKey(), newFlags)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400516 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400517 }
518 }
Liz Kammere6583482021-10-19 13:56:10 -0400519}
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400520
Jingwen Chen55bc8202021-11-02 06:40:51 +0000521func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, implementationHdrs bazel.LabelListAttribute) {
Liz Kammere6583482021-10-19 13:56:10 -0400522 ca.srcs.ResolveExcludes()
523 partitionedSrcs := groupSrcsByExtension(ctx, ca.srcs)
524
Liz Kammer12615db2021-09-28 09:19:17 -0400525 ca.protoSrcs = partitionedSrcs[protoSrcPartition]
Vinh Tran9f6796a2022-08-16 13:10:31 -0400526 ca.aidlSrcs = partitionedSrcs[aidlSrcPartition]
Liz Kammer12615db2021-09-28 09:19:17 -0400527
Liz Kammere6583482021-10-19 13:56:10 -0400528 for p, lla := range partitionedSrcs {
529 // if there are no sources, there is no need for headers
530 if lla.IsEmpty() {
531 continue
532 }
533 lla.Append(implementationHdrs)
534 partitionedSrcs[p] = lla
535 }
536
537 ca.srcs = partitionedSrcs[cppSrcPartition]
538 ca.cSrcs = partitionedSrcs[cSrcPartition]
539 ca.asSrcs = partitionedSrcs[asSrcPartition]
Cole Faust7071a052022-07-29 15:58:33 -0700540 ca.asmSrcs = partitionedSrcs[asmSrcPartition]
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000541 ca.lSrcs = partitionedSrcs[lSrcPartition]
542 ca.llSrcs = partitionedSrcs[llSrcPartition]
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000543 ca.syspropSrcs = partitionedSrcs[syspropSrcPartition]
Liz Kammere6583482021-10-19 13:56:10 -0400544
545 ca.absoluteIncludes.DeduplicateAxesFromBase()
546 ca.localIncludes.DeduplicateAxesFromBase()
547}
548
549// Parse srcs from an arch or OS's props value.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000550func parseSrcs(ctx android.BazelConversionPathContext, props *BaseCompilerProperties) (bazel.LabelList, bool) {
Liz Kammere6583482021-10-19 13:56:10 -0400551 anySrcs := false
552 // Add srcs-like dependencies such as generated files.
553 // First create a LabelList containing these dependencies, then merge the values with srcs.
554 generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources)
555 if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 {
556 anySrcs = true
557 }
558
559 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs)
Vinh Tran9f6796a2022-08-16 13:10:31 -0400560
Liz Kammere6583482021-10-19 13:56:10 -0400561 if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 {
562 anySrcs = true
563 }
Vinh Tran9f6796a2022-08-16 13:10:31 -0400564
Liz Kammere6583482021-10-19 13:56:10 -0400565 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs
566}
567
Liz Kammera5a29de2022-05-25 23:19:37 -0400568func bp2buildStdVal(std *string, prefix string, useGnu bool) *string {
569 defaultVal := prefix + "_std_default"
Chris Parsons79bd2b72021-11-29 17:52:41 -0500570 // If c{,pp}std properties are not specified, don't generate them in the BUILD file.
571 // Defaults are handled by the toolchain definition.
572 // However, if gnu_extensions is false, then the default gnu-to-c version must be specified.
Liz Kammera5a29de2022-05-25 23:19:37 -0400573 stdVal := proptools.StringDefault(std, defaultVal)
574 if stdVal == "experimental" || stdVal == defaultVal {
575 if stdVal == "experimental" {
576 stdVal = prefix + "_std_experimental"
577 }
578 if !useGnu {
579 stdVal += "_no_gnu"
580 }
581 } else if !useGnu {
582 stdVal = gnuToCReplacer.Replace(stdVal)
Chris Parsons79bd2b72021-11-29 17:52:41 -0500583 }
584
Liz Kammera5a29de2022-05-25 23:19:37 -0400585 if stdVal == defaultVal {
586 return nil
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500587 }
Liz Kammera5a29de2022-05-25 23:19:37 -0400588 return &stdVal
589}
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500590
Liz Kammera5a29de2022-05-25 23:19:37 -0400591func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) {
592 useGnu := useGnuExtensions(gnu_extensions)
593
594 return bp2buildStdVal(c_std, "c", useGnu), bp2buildStdVal(cpp_std, "cpp", useGnu)
Liz Kammere6583482021-10-19 13:56:10 -0400595}
596
Liz Kammer1263d9b2021-12-10 14:28:20 -0500597// packageFromLabel extracts package from a fully-qualified or relative Label and whether the label
598// is fully-qualified.
599// e.g. fully-qualified "//a/b:foo" -> "a/b", true, relative: ":bar" -> ".", false
600func packageFromLabel(label string) (string, bool) {
601 split := strings.Split(label, ":")
602 if len(split) != 2 {
603 return "", false
604 }
605 if split[0] == "" {
606 return ".", false
607 }
608 // remove leading "//"
609 return split[0][2:], true
610}
611
612// includesFromLabelList extracts relative/absolute includes from a bazel.LabelList>
613func includesFromLabelList(labelList bazel.LabelList) (relative, absolute []string) {
614 for _, hdr := range labelList.Includes {
615 if pkg, hasPkg := packageFromLabel(hdr.Label); hasPkg {
616 absolute = append(absolute, pkg)
617 } else if pkg != "" {
618 relative = append(relative, pkg)
619 }
620 }
621 return relative, absolute
622}
623
Cole Faust7071a052022-07-29 15:58:33 -0700624type YasmAttributes struct {
625 Srcs bazel.LabelListAttribute
626 Flags bazel.StringListAttribute
627 Include_dirs bazel.StringListAttribute
628}
629
630func bp2BuildYasm(ctx android.Bp2buildMutatorContext, m *Module, ca compilerAttributes) *bazel.LabelAttribute {
631 if ca.asmSrcs.IsEmpty() {
632 return nil
633 }
634
635 // Yasm needs the include directories from both local_includes and
636 // export_include_dirs. We don't care about actually exporting them from the
637 // yasm rule though, because they will also be present on the cc_ rule that
638 // wraps this yasm rule.
639 includes := ca.localIncludes.Clone()
640 bp2BuildPropParseHelper(ctx, m, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
641 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
642 if len(flagExporterProperties.Export_include_dirs) > 0 {
643 x := bazel.StringListAttribute{}
644 x.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs)
645 includes.Append(x)
646 }
647 }
648 })
649
650 ctx.CreateBazelTargetModule(
651 bazel.BazelTargetModuleProperties{
652 Rule_class: "yasm",
653 Bzl_load_location: "//build/bazel/rules/cc:yasm.bzl",
654 },
655 android.CommonAttributes{Name: m.Name() + "_yasm"},
656 &YasmAttributes{
657 Srcs: ca.asmSrcs,
658 Flags: ca.asFlags,
659 Include_dirs: *includes,
660 })
661
662 // We only want to add a dependency on the _yasm target if there are asm
663 // sources in the current configuration. If there are unconfigured asm
664 // sources, always add the dependency. Otherwise, add the dependency only
665 // on the configuration axes and values that had asm sources.
666 if len(ca.asmSrcs.Value.Includes) > 0 {
667 return bazel.MakeLabelAttribute(":" + m.Name() + "_yasm")
668 }
669
670 ret := &bazel.LabelAttribute{}
671 for _, axis := range ca.asmSrcs.SortedConfigurationAxes() {
Sasha Smundak39a301c2022-12-29 17:11:49 -0800672 for cfg := range ca.asmSrcs.ConfigurableValues[axis] {
673 ret.SetSelectValue(axis, cfg, bazel.Label{Label: ":" + m.Name() + "_yasm"})
Cole Faust7071a052022-07-29 15:58:33 -0700674 }
675 }
676 return ret
677}
678
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000679// bp2BuildParseBaseProps returns all compiler, linker, library attributes of a cc module..
Liz Kammer12615db2021-09-28 09:19:17 -0400680func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) baseAttributes {
Liz Kammere6583482021-10-19 13:56:10 -0400681 archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
682 archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{})
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000683 archVariantLibraryProperties := module.GetArchVariantProperties(ctx, &LibraryProperties{})
Liz Kammere6583482021-10-19 13:56:10 -0400684
685 var implementationHdrs bazel.LabelListAttribute
686
687 axisToConfigs := map[bazel.ConfigurationAxis]map[string]bool{}
688 allAxesAndConfigs := func(cp android.ConfigurationAxisToArchVariantProperties) {
689 for axis, configMap := range cp {
690 if _, ok := axisToConfigs[axis]; !ok {
691 axisToConfigs[axis] = map[string]bool{}
692 }
Sasha Smundak39a301c2022-12-29 17:11:49 -0800693 for cfg := range configMap {
694 axisToConfigs[axis][cfg] = true
Chris Parsonsa967f252021-09-23 16:34:35 -0400695 }
696 }
697 }
Liz Kammere6583482021-10-19 13:56:10 -0400698 allAxesAndConfigs(archVariantCompilerProps)
699 allAxesAndConfigs(archVariantLinkerProps)
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000700 allAxesAndConfigs(archVariantLibraryProperties)
Chris Parsonsa967f252021-09-23 16:34:35 -0400701
Liz Kammere6583482021-10-19 13:56:10 -0400702 compilerAttrs := compilerAttributes{}
703 linkerAttrs := linkerAttributes{}
704
Chris Parsons7b3289b2023-01-26 17:30:44 -0500705 // Iterate through these axes in a deterministic order. This is required
706 // because processing certain dependencies may result in concatenating
707 // elements along other axes. (For example, processing NoConfig may result
708 // in elements being added to InApex). This is thus the only way to ensure
709 // that the order of entries in each list is in a predictable order.
710 for _, axis := range bazel.SortedConfigurationAxes(axisToConfigs) {
711 configs := axisToConfigs[axis]
Sasha Smundak39a301c2022-12-29 17:11:49 -0800712 for cfg := range configs {
Liz Kammere6583482021-10-19 13:56:10 -0400713 var allHdrs []string
Sasha Smundak39a301c2022-12-29 17:11:49 -0800714 if baseCompilerProps, ok := archVariantCompilerProps[axis][cfg].(*BaseCompilerProperties); ok {
Liz Kammere6583482021-10-19 13:56:10 -0400715 allHdrs = baseCompilerProps.Generated_headers
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000716 if baseCompilerProps.Lex != nil {
Sasha Smundak39a301c2022-12-29 17:11:49 -0800717 compilerAttrs.lexopts.SetSelectValue(axis, cfg, baseCompilerProps.Lex.Flags)
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000718 }
Sasha Smundak39a301c2022-12-29 17:11:49 -0800719 (&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, cfg, baseCompilerProps)
Liz Kammere6583482021-10-19 13:56:10 -0400720 }
721
722 var exportHdrs []string
723
Sasha Smundak39a301c2022-12-29 17:11:49 -0800724 if baseLinkerProps, ok := archVariantLinkerProps[axis][cfg].(*BaseLinkerProperties); ok {
Liz Kammere6583482021-10-19 13:56:10 -0400725 exportHdrs = baseLinkerProps.Export_generated_headers
726
Sasha Smundak39a301c2022-12-29 17:11:49 -0800727 (&linkerAttrs).bp2buildForAxisAndConfig(ctx, module.Binary(), axis, cfg, baseLinkerProps)
Liz Kammere6583482021-10-19 13:56:10 -0400728 }
729 headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportHdrs, android.BazelLabelForModuleDeps)
Sasha Smundak39a301c2022-12-29 17:11:49 -0800730 implementationHdrs.SetSelectValue(axis, cfg, headers.implementation)
731 compilerAttrs.hdrs.SetSelectValue(axis, cfg, headers.export)
Liz Kammer1263d9b2021-12-10 14:28:20 -0500732
733 exportIncludes, exportAbsoluteIncludes := includesFromLabelList(headers.export)
Sasha Smundak39a301c2022-12-29 17:11:49 -0800734 compilerAttrs.includes.Includes.SetSelectValue(axis, cfg, exportIncludes)
735 compilerAttrs.includes.AbsoluteIncludes.SetSelectValue(axis, cfg, exportAbsoluteIncludes)
Liz Kammer1263d9b2021-12-10 14:28:20 -0500736
737 includes, absoluteIncludes := includesFromLabelList(headers.implementation)
Sasha Smundak39a301c2022-12-29 17:11:49 -0800738 currAbsoluteIncludes := compilerAttrs.absoluteIncludes.SelectValue(axis, cfg)
Liz Kammer1263d9b2021-12-10 14:28:20 -0500739 currAbsoluteIncludes = android.FirstUniqueStrings(append(currAbsoluteIncludes, absoluteIncludes...))
Vinh Tran9f6796a2022-08-16 13:10:31 -0400740
Sasha Smundak39a301c2022-12-29 17:11:49 -0800741 compilerAttrs.absoluteIncludes.SetSelectValue(axis, cfg, currAbsoluteIncludes)
Vinh Tran9f6796a2022-08-16 13:10:31 -0400742
Sasha Smundak39a301c2022-12-29 17:11:49 -0800743 currIncludes := compilerAttrs.localIncludes.SelectValue(axis, cfg)
Liz Kammer1263d9b2021-12-10 14:28:20 -0500744 currIncludes = android.FirstUniqueStrings(append(currIncludes, includes...))
Vinh Tran9f6796a2022-08-16 13:10:31 -0400745
Sasha Smundak39a301c2022-12-29 17:11:49 -0800746 compilerAttrs.localIncludes.SetSelectValue(axis, cfg, currIncludes)
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000747
Sasha Smundak39a301c2022-12-29 17:11:49 -0800748 if libraryProps, ok := archVariantLibraryProperties[axis][cfg].(*LibraryProperties); ok {
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000749 if axis == bazel.NoConfigAxis {
750 compilerAttrs.stubsSymbolFile = libraryProps.Stubs.Symbol_file
Sasha Smundak39a301c2022-12-29 17:11:49 -0800751 compilerAttrs.stubsVersions.SetSelectValue(axis, cfg, libraryProps.Stubs.Versions)
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000752 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500753 if suffix := libraryProps.Suffix; suffix != nil {
Sasha Smundak39a301c2022-12-29 17:11:49 -0800754 compilerAttrs.suffix.SetSelectValue(axis, cfg, suffix)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -0500755 }
Jingwen Chen0ee88a62022-01-07 14:55:29 +0000756 }
Liz Kammere6583482021-10-19 13:56:10 -0400757 }
758 }
Vinh Tran9f6796a2022-08-16 13:10:31 -0400759
Liz Kammere6583482021-10-19 13:56:10 -0400760 compilerAttrs.convertStlProps(ctx, module)
761 (&linkerAttrs).convertStripProps(ctx, module)
762
Yu Liuf01a0f02022-12-07 15:45:30 -0800763 var nativeCoverage *bool
Yu Liu8d82ac52022-05-17 15:13:28 -0700764 if module.coverage != nil && module.coverage.Properties.Native_coverage != nil &&
765 !Bool(module.coverage.Properties.Native_coverage) {
Yu Liuf01a0f02022-12-07 15:45:30 -0800766 nativeCoverage = BoolPtr(false)
Yu Liu8d82ac52022-05-17 15:13:28 -0700767 }
768
Cole Faust912bc882023-03-08 12:29:50 -0800769 productVariableProps := android.ProductVariableProperties(ctx, ctx.Module())
Liz Kammere6583482021-10-19 13:56:10 -0400770
771 (&compilerAttrs).convertProductVariables(ctx, productVariableProps)
772 (&linkerAttrs).convertProductVariables(ctx, productVariableProps)
773
774 (&compilerAttrs).finalize(ctx, implementationHdrs)
Liz Kammer54309532021-12-14 12:21:22 -0500775 (&linkerAttrs).finalize(ctx)
Liz Kammere6583482021-10-19 13:56:10 -0400776
Cole Faust7071a052022-07-29 15:58:33 -0700777 (&compilerAttrs.srcs).Add(bp2BuildYasm(ctx, module, compilerAttrs))
778
Liz Kammer12615db2021-09-28 09:19:17 -0400779 protoDep := bp2buildProto(ctx, module, compilerAttrs.protoSrcs)
780
781 // bp2buildProto will only set wholeStaticLib or implementationWholeStaticLib, but we don't know
782 // which. This will add the newly generated proto library to the appropriate attribute and nothing
783 // to the other
784 (&linkerAttrs).wholeArchiveDeps.Add(protoDep.wholeStaticLib)
785 (&linkerAttrs).implementationWholeArchiveDeps.Add(protoDep.implementationWholeStaticLib)
Vinh Tranfde57eb2022-08-29 17:46:58 -0400786
Vinh Tran395a1e92022-09-16 18:27:29 -0400787 aidlDep := bp2buildCcAidlLibrary(ctx, module, compilerAttrs.aidlSrcs, linkerAttrs)
Vinh Tranfde57eb2022-08-29 17:46:58 -0400788 if aidlDep != nil {
789 if lib, ok := module.linker.(*libraryDecorator); ok {
790 if proptools.Bool(lib.Properties.Aidl.Export_aidl_headers) {
791 (&linkerAttrs).wholeArchiveDeps.Add(aidlDep)
792 } else {
793 (&linkerAttrs).implementationWholeArchiveDeps.Add(aidlDep)
794 }
795 }
796 }
Liz Kammer12615db2021-09-28 09:19:17 -0400797
Trevor Radcliffeef9c9002022-05-13 20:55:35 +0000798 convertedLSrcs := bp2BuildLex(ctx, module.Name(), compilerAttrs)
799 (&compilerAttrs).srcs.Add(&convertedLSrcs.srcName)
800 (&compilerAttrs).cSrcs.Add(&convertedLSrcs.cSrcName)
801
Vinh Tran99270ea2022-11-28 11:15:23 -0500802 if module.afdo != nil && module.afdo.Properties.Afdo {
803 fdoProfileDep := bp2buildFdoProfile(ctx, module)
804 if fdoProfileDep != nil {
805 (&compilerAttrs).fdoProfile.SetValue(*fdoProfileDep)
806 }
807 }
808
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000809 if !compilerAttrs.syspropSrcs.IsEmpty() {
810 (&linkerAttrs).wholeArchiveDeps.Add(bp2buildCcSysprop(ctx, module.Name(), module.Properties.Min_sdk_version, compilerAttrs.syspropSrcs))
811 }
812
Zi Wang9f609db2023-01-04 11:06:54 -0800813 linkerAttrs.wholeArchiveDeps.Prepend = true
814 linkerAttrs.deps.Prepend = true
815 compilerAttrs.localIncludes.Prepend = true
816 compilerAttrs.absoluteIncludes.Prepend = true
817 compilerAttrs.hdrs.Prepend = true
818
Trevor Radcliffedb7e0262022-10-28 16:48:18 +0000819 features := compilerAttrs.features.Clone().Append(linkerAttrs.features).Append(bp2buildSanitizerFeatures(ctx, module))
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +0000820 features = features.Append(bp2buildLtoFeatures(ctx, module))
Cole Faust5fa4e962022-08-22 14:31:04 -0700821 features.DeduplicateAxesFromBase()
822
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +0000823 addMuslSystemDynamicDeps(ctx, linkerAttrs)
824
Liz Kammere6583482021-10-19 13:56:10 -0400825 return baseAttributes{
826 compilerAttrs,
827 linkerAttrs,
Cole Faust5fa4e962022-08-22 14:31:04 -0700828 *features,
Liz Kammer12615db2021-09-28 09:19:17 -0400829 protoDep.protoDep,
Vinh Tran9f6796a2022-08-16 13:10:31 -0400830 aidlDep,
Yu Liuf01a0f02022-12-07 15:45:30 -0800831 nativeCoverage,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000832 }
833}
834
Trevor Radcliffe0d1b4022022-12-12 22:26:34 +0000835// As a workaround for b/261657184, we are manually adding the default value
836// of system_dynamic_deps for the linux_musl os.
837// TODO: Solve this properly
838func addMuslSystemDynamicDeps(ctx android.Bp2buildMutatorContext, attrs linkerAttributes) {
839 systemDynamicDeps := attrs.systemDynamicDeps.SelectValue(bazel.OsConfigurationAxis, "linux_musl")
840 if attrs.systemDynamicDeps.HasAxisSpecificValues(bazel.OsConfigurationAxis) && systemDynamicDeps.IsNil() {
841 attrs.systemDynamicDeps.SetSelectValue(bazel.OsConfigurationAxis, "linux_musl", android.BazelLabelForModuleDeps(ctx, config.MuslDefaultSharedLibraries))
842 }
843}
844
Vinh Tran99270ea2022-11-28 11:15:23 -0500845type fdoProfileAttributes struct {
846 Absolute_path_profile string
847}
848
849func bp2buildFdoProfile(
850 ctx android.Bp2buildMutatorContext,
851 m *Module,
852) *bazel.Label {
853 for _, project := range globalAfdoProfileProjects {
Vinh Tranbc9c8b42022-12-09 12:03:52 -0500854 // Ensure handcrafted BUILD file exists in the project
855 BUILDPath := android.ExistentPathForSource(ctx, project, "BUILD")
856 if BUILDPath.Valid() {
857 // We handcraft a BUILD file with fdo_profile targets that use the existing profiles in the project
858 // This implementation is assuming that every afdo profile in globalAfdoProfileProjects already has
859 // an associated fdo_profile target declared in the same package.
860 // TODO(b/260714900): Handle arch-specific afdo profiles (e.g. `<module-name>-arm<64>.afdo`)
861 path := android.ExistentPathForSource(ctx, project, m.Name()+".afdo")
862 if path.Valid() {
863 // FIXME: Some profiles only exist internally and are not released to AOSP.
864 // When generated BUILD files are checked in, we'll run into merge conflict.
865 // The cc_library_shared target in AOSP won't have reference to an fdo_profile target because
866 // the profile doesn't exist. Internally, the same cc_library_shared target will
867 // have reference to the fdo_profile.
868 // For more context, see b/258682955#comment2
869 fdoProfileLabel := "//" + strings.TrimSuffix(project, "/") + ":" + m.Name()
870 return &bazel.Label{
871 Label: fdoProfileLabel,
872 }
Vinh Tran99270ea2022-11-28 11:15:23 -0500873 }
874 }
875 }
876
877 return nil
878}
879
Vinh Tran9f6796a2022-08-16 13:10:31 -0400880func bp2buildCcAidlLibrary(
881 ctx android.Bp2buildMutatorContext,
882 m *Module,
Vinh Trana3b8b782022-09-14 11:40:24 -0400883 aidlLabelList bazel.LabelListAttribute,
Vinh Tran395a1e92022-09-16 18:27:29 -0400884 linkerAttrs linkerAttributes,
Vinh Tran9f6796a2022-08-16 13:10:31 -0400885) *bazel.LabelAttribute {
Vinh Trana3b8b782022-09-14 11:40:24 -0400886 if !aidlLabelList.IsEmpty() {
887 aidlLibs, aidlSrcs := aidlLabelList.Partition(func(src bazel.Label) bool {
888 if fg, ok := android.ToFileGroupAsLibrary(ctx, src.OriginalModuleName); ok &&
889 fg.ShouldConvertToAidlLibrary(ctx) {
890 return true
891 }
892 return false
893 })
Vinh Tran9f6796a2022-08-16 13:10:31 -0400894
Vinh Trana3b8b782022-09-14 11:40:24 -0400895 if !aidlSrcs.IsEmpty() {
896 aidlLibName := m.Name() + "_aidl_library"
897 ctx.CreateBazelTargetModule(
898 bazel.BazelTargetModuleProperties{
899 Rule_class: "aidl_library",
900 Bzl_load_location: "//build/bazel/rules/aidl:library.bzl",
901 },
902 android.CommonAttributes{Name: aidlLibName},
903 &aidlLibraryAttributes{
904 Srcs: aidlSrcs,
905 },
906 )
907 aidlLibs.Add(&bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + aidlLibName}})
908 }
Vinh Tran9f6796a2022-08-16 13:10:31 -0400909
Vinh Trana3b8b782022-09-14 11:40:24 -0400910 if !aidlLibs.IsEmpty() {
911 ccAidlLibrarylabel := m.Name() + "_cc_aidl_library"
Vinh Tran395a1e92022-09-16 18:27:29 -0400912 // Since cc_aidl_library only needs the dynamic deps (aka shared libs) from the parent cc library for compiling,
913 // we err on the side of not re-exporting the headers of the dynamic deps from cc_aidl_lirary
914 // because the parent cc library already has all the dynamic deps
915 implementationDynamicDeps := bazel.MakeLabelListAttribute(
916 bazel.AppendBazelLabelLists(
917 linkerAttrs.dynamicDeps.Value,
918 linkerAttrs.implementationDynamicDeps.Value,
919 ),
920 )
921
Vinh Trana3b8b782022-09-14 11:40:24 -0400922 ctx.CreateBazelTargetModule(
923 bazel.BazelTargetModuleProperties{
924 Rule_class: "cc_aidl_library",
925 Bzl_load_location: "//build/bazel/rules/cc:cc_aidl_library.bzl",
926 },
927 android.CommonAttributes{Name: ccAidlLibrarylabel},
928 &ccAidlLibraryAttributes{
Vinh Tran395a1e92022-09-16 18:27:29 -0400929 Deps: aidlLibs,
930 Implementation_dynamic_deps: implementationDynamicDeps,
Vinh Trana3b8b782022-09-14 11:40:24 -0400931 },
932 )
933 label := &bazel.LabelAttribute{
934 Value: &bazel.Label{
935 Label: ":" + ccAidlLibrarylabel,
936 },
937 }
938 return label
939 }
Vinh Tran9f6796a2022-08-16 13:10:31 -0400940 }
941
Vinh Trana3b8b782022-09-14 11:40:24 -0400942 return nil
Vinh Tran9f6796a2022-08-16 13:10:31 -0400943}
944
Yu Liufc603162022-03-01 15:44:08 -0800945func bp2BuildParseSdkAttributes(module *Module) sdkAttributes {
Trevor Radcliffe58ea4512022-04-07 20:36:39 +0000946 return sdkAttributes{
947 Sdk_version: module.Properties.Sdk_version,
Yu Liufc603162022-03-01 15:44:08 -0800948 Min_sdk_version: module.Properties.Min_sdk_version,
949 }
950}
951
952type sdkAttributes struct {
953 Sdk_version *string
954 Min_sdk_version *string
955}
956
Jingwen Chen107c0de2021-04-09 10:43:12 +0000957// Convenience struct to hold all attributes parsed from linker properties.
958type linkerAttributes struct {
Liz Kammer54309532021-12-14 12:21:22 -0500959 deps bazel.LabelListAttribute
960 implementationDeps bazel.LabelListAttribute
961 dynamicDeps bazel.LabelListAttribute
962 implementationDynamicDeps bazel.LabelListAttribute
Cole Faust6b29f592022-08-09 09:50:56 -0700963 runtimeDeps bazel.LabelListAttribute
Liz Kammer54309532021-12-14 12:21:22 -0500964 wholeArchiveDeps bazel.LabelListAttribute
965 implementationWholeArchiveDeps bazel.LabelListAttribute
966 systemDynamicDeps bazel.LabelListAttribute
967 usedSystemDynamicDepAsDynamicDep map[string]bool
Liz Kammer7a210ac2021-09-22 15:52:58 -0400968
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500969 useVersionLib bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000970 linkopts bazel.StringListAttribute
Liz Kammerd2871182021-10-04 13:54:37 -0400971 additionalLinkerInputs bazel.LabelListAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000972 stripKeepSymbols bazel.BoolAttribute
973 stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
974 stripKeepSymbolsList bazel.StringListAttribute
975 stripAll bazel.BoolAttribute
976 stripNone bazel.BoolAttribute
Liz Kammer0eae52e2021-10-06 10:32:26 -0400977 features bazel.StringListAttribute
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400978}
979
Liz Kammer54309532021-12-14 12:21:22 -0500980var (
981 soongSystemSharedLibs = []string{"libc", "libm", "libdl"}
Liz Kammerbaced712022-09-16 09:01:29 -0400982 versionLib = "libbuildversion"
Liz Kammer54309532021-12-14 12:21:22 -0500983)
984
Vinh Tran85fb07c2022-09-16 16:17:48 -0400985// resolveTargetApex re-adds the shared and static libs in target.apex.exclude_shared|static_libs props to non-apex variant
986// since all libs are already excluded by default
Liz Kammer748d7072023-01-25 12:07:43 -0500987func (la *linkerAttributes) resolveTargetApexProp(ctx android.BazelConversionPathContext, props *BaseLinkerProperties) {
988 excludeSharedLibs := bazelLabelForSharedDeps(ctx, props.Target.Apex.Exclude_shared_libs)
989 sharedExcludes := bazel.LabelList{Excludes: excludeSharedLibs.Includes}
990 sharedExcludesLabelList := bazel.LabelListAttribute{}
991 sharedExcludesLabelList.SetSelectValue(bazel.InApexAxis, bazel.InApex, sharedExcludes)
Vinh Tran85fb07c2022-09-16 16:17:48 -0400992
Liz Kammer748d7072023-01-25 12:07:43 -0500993 la.dynamicDeps.Append(sharedExcludesLabelList)
994 la.implementationDynamicDeps.Append(sharedExcludesLabelList)
995
996 excludeStaticLibs := bazelLabelForStaticDeps(ctx, props.Target.Apex.Exclude_static_libs)
997 staticExcludes := bazel.LabelList{Excludes: excludeStaticLibs.Includes}
998 staticExcludesLabelList := bazel.LabelListAttribute{}
999 staticExcludesLabelList.SetSelectValue(bazel.InApexAxis, bazel.InApex, staticExcludes)
1000
1001 la.deps.Append(staticExcludesLabelList)
1002 la.implementationDeps.Append(staticExcludesLabelList)
Vinh Tran85fb07c2022-09-16 16:17:48 -04001003}
1004
Jingwen Chen55bc8202021-11-02 06:40:51 +00001005func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, isBinary bool, axis bazel.ConfigurationAxis, config string, props *BaseLinkerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -04001006 // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module
1007 var axisFeatures []string
Liz Kammer7a210ac2021-09-22 15:52:58 -04001008
Liz Kammercc2c1ef2022-03-21 09:03:29 -04001009 wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs)
Liz Kammerbaced712022-09-16 09:01:29 -04001010 staticLibs := android.FirstUniqueStrings(android.RemoveListFromList(props.Static_libs, wholeStaticLibs))
1011 if axis == bazel.NoConfigAxis {
1012 la.useVersionLib.SetSelectValue(axis, config, props.Use_version_lib)
1013 if proptools.Bool(props.Use_version_lib) {
1014 versionLibAlreadyInDeps := android.InList(versionLib, wholeStaticLibs)
1015 // remove from static libs so there is no duplicate dependency
1016 _, staticLibs = android.RemoveFromList(versionLib, staticLibs)
1017 // only add the dep if it is not in progress
1018 if !versionLibAlreadyInDeps {
1019 if isBinary {
1020 wholeStaticLibs = append(wholeStaticLibs, versionLib)
1021 } else {
1022 la.implementationWholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, []string{versionLib}, props.Exclude_static_libs))
1023 }
1024 }
1025 }
1026 }
1027
Liz Kammere6583482021-10-19 13:56:10 -04001028 // Excludes to parallel Soong:
1029 // 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 -04001030 la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs))
Liz Kammercc2c1ef2022-03-21 09:03:29 -04001031
Vinh Tran85fb07c2022-09-16 16:17:48 -04001032 staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(
1033 ctx,
1034 !isBinary,
1035 staticLibs,
Liz Kammer748d7072023-01-25 12:07:43 -05001036 props.Exclude_static_libs,
Vinh Tran85fb07c2022-09-16 16:17:48 -04001037 props.Export_static_lib_headers,
1038 bazelLabelForStaticDepsExcludes,
1039 )
Liz Kammer7a210ac2021-09-22 15:52:58 -04001040
Liz Kammere6583482021-10-19 13:56:10 -04001041 headerLibs := android.FirstUniqueStrings(props.Header_libs)
1042 hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps)
Jingwen Chen63930982021-03-24 10:04:33 -04001043
Liz Kammere6583482021-10-19 13:56:10 -04001044 (&hDeps.export).Append(staticDeps.export)
1045 la.deps.SetSelectValue(axis, config, hDeps.export)
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001046
Liz Kammere6583482021-10-19 13:56:10 -04001047 (&hDeps.implementation).Append(staticDeps.implementation)
1048 la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation)
Liz Kammer0eae52e2021-10-06 10:32:26 -04001049
Liz Kammere6583482021-10-19 13:56:10 -04001050 systemSharedLibs := props.System_shared_libs
1051 // systemSharedLibs distinguishes between nil/empty list behavior:
1052 // nil -> use default values
1053 // empty list -> no values specified
1054 if len(systemSharedLibs) > 0 {
1055 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
1056 }
1057 la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
1058
1059 sharedLibs := android.FirstUniqueStrings(props.Shared_libs)
Liz Kammer54309532021-12-14 12:21:22 -05001060 excludeSharedLibs := props.Exclude_shared_libs
1061 usedSystem := android.FilterListPred(sharedLibs, func(s string) bool {
1062 return android.InList(s, soongSystemSharedLibs) && !android.InList(s, excludeSharedLibs)
1063 })
1064 for _, el := range usedSystem {
1065 if la.usedSystemDynamicDepAsDynamicDep == nil {
1066 la.usedSystemDynamicDepAsDynamicDep = map[string]bool{}
1067 }
1068 la.usedSystemDynamicDepAsDynamicDep[el] = true
1069 }
1070
Vinh Tran85fb07c2022-09-16 16:17:48 -04001071 sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(
1072 ctx,
1073 !isBinary,
1074 sharedLibs,
Liz Kammer748d7072023-01-25 12:07:43 -05001075 props.Exclude_shared_libs,
Vinh Tran85fb07c2022-09-16 16:17:48 -04001076 props.Export_shared_lib_headers,
1077 bazelLabelForSharedDepsExcludes,
1078 )
Liz Kammere6583482021-10-19 13:56:10 -04001079 la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export)
1080 la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation)
Liz Kammer748d7072023-01-25 12:07:43 -05001081 la.resolveTargetApexProp(ctx, props)
Vinh Tran85fb07c2022-09-16 16:17:48 -04001082
Wei Li81852ca2022-07-27 00:22:06 -07001083 if axis == bazel.NoConfigAxis || (axis == bazel.OsConfigurationAxis && config == bazel.OsAndroid) {
Yu Liu10174ff2023-02-21 12:05:26 -08001084 // If a dependency in la.implementationDynamicDeps or la.dynamicDeps has stubs, its
1085 // stub variant should be used when the dependency is linked in a APEX. The
1086 // dependencies in NoConfigAxis and OsConfigurationAxis/OsAndroid are grouped by
1087 // having stubs or not, so Bazel select() statement can be used to choose
1088 // source/stub variants of them.
1089 setStubsForDynamicDeps(ctx, axis, config, sharedDeps.export, &la.dynamicDeps, 0)
1090 setStubsForDynamicDeps(ctx, axis, config, sharedDeps.implementation, &la.implementationDynamicDeps, 1)
Wei Li81852ca2022-07-27 00:22:06 -07001091 }
Liz Kammere6583482021-10-19 13:56:10 -04001092
1093 if !BoolDefault(props.Pack_relocations, packRelocationsDefault) {
1094 axisFeatures = append(axisFeatures, "disable_pack_relocations")
1095 }
1096
1097 if Bool(props.Allow_undefined_symbols) {
1098 axisFeatures = append(axisFeatures, "-no_undefined_symbols")
1099 }
1100
1101 var linkerFlags []string
1102 if len(props.Ldflags) > 0 {
Liz Kammerf38a8372022-02-04 15:39:00 -05001103 linkerFlags = append(linkerFlags, proptools.NinjaEscapeList(props.Ldflags)...)
Liz Kammere6583482021-10-19 13:56:10 -04001104 // binaries remove static flag if -shared is in the linker flags
1105 if isBinary && android.InList("-shared", linkerFlags) {
1106 axisFeatures = append(axisFeatures, "-static_flag")
1107 }
1108 }
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +00001109
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux01ec55e2023-01-30 22:53:04 +00001110 if !props.libCrt() {
1111 axisFeatures = append(axisFeatures, "-use_libcrt")
1112 }
1113 if !props.crt() {
1114 axisFeatures = append(axisFeatures, "-link_crt")
1115 }
1116
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +00001117 // This must happen before the addition of flags for Version Script and
1118 // Dynamic List, as these flags must be split on spaces and those must not
1119 linkerFlags = parseCommandLineFlags(linkerFlags, filterOutClangUnknownCflags)
1120
Trevor Radcliffe37ec2f72022-09-27 01:46:01 +00001121 additionalLinkerInputs := bazel.LabelList{}
Liz Kammere6583482021-10-19 13:56:10 -04001122 if props.Version_script != nil {
1123 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script)
Trevor Radcliffe37ec2f72022-09-27 01:46:01 +00001124 additionalLinkerInputs.Add(&label)
Liz Kammere6583482021-10-19 13:56:10 -04001125 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label))
1126 }
Alix773adaa2022-04-27 17:49:34 +00001127
1128 if props.Dynamic_list != nil {
1129 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Dynamic_list)
Trevor Radcliffe37ec2f72022-09-27 01:46:01 +00001130 additionalLinkerInputs.Add(&label)
Alix773adaa2022-04-27 17:49:34 +00001131 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--dynamic-list,$(location %s)", label.Label))
1132 }
1133
Trevor Radcliffe37ec2f72022-09-27 01:46:01 +00001134 la.additionalLinkerInputs.SetSelectValue(axis, config, additionalLinkerInputs)
Trevor Radcliffeea6a45d2022-09-20 18:58:01 +00001135 la.linkopts.SetSelectValue(axis, config, linkerFlags)
Liz Kammere6583482021-10-19 13:56:10 -04001136
1137 if axisFeatures != nil {
1138 la.features.SetSelectValue(axis, config, axisFeatures)
1139 }
Cole Faust6b29f592022-08-09 09:50:56 -07001140
1141 runtimeDeps := android.BazelLabelForModuleDepsExcludes(ctx, props.Runtime_libs, props.Exclude_runtime_libs)
1142 if !runtimeDeps.IsEmpty() {
1143 la.runtimeDeps.SetSelectValue(axis, config, runtimeDeps)
1144 }
Liz Kammere6583482021-10-19 13:56:10 -04001145}
1146
Yu Liu10174ff2023-02-21 12:05:26 -08001147func setStubsForDynamicDeps(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis,
1148 config string, dynamicLibs bazel.LabelList, dynamicDeps *bazel.LabelListAttribute, ind int) {
1149 depsWithStubs := []bazel.Label{}
1150 for _, l := range dynamicLibs.Includes {
1151 dep, _ := ctx.ModuleFromName(l.OriginalModuleName)
1152 if m, ok := dep.(*Module); ok && m.HasStubsVariants() {
1153 depsWithStubs = append(depsWithStubs, l)
1154 }
1155 }
1156 if len(depsWithStubs) > 0 {
1157 implDynamicDeps := bazel.SubtractBazelLabelList(dynamicLibs, bazel.MakeLabelList(depsWithStubs))
1158 dynamicDeps.SetSelectValue(axis, config, implDynamicDeps)
1159
1160 stubLibLabels := []bazel.Label{}
1161 for _, l := range depsWithStubs {
1162 l.Label = l.Label + stubsSuffix
1163 stubLibLabels = append(stubLibLabels, l)
1164 }
1165 inApexSelectValue := dynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex)
1166 nonApexSelectValue := dynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex)
1167 defaultSelectValue := dynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey)
1168 if axis == bazel.NoConfigAxis {
1169 (&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels))
1170 (&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
1171 (&defaultSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
1172 dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, bazel.FirstUniqueBazelLabelList(inApexSelectValue))
1173 dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, bazel.FirstUniqueBazelLabelList(nonApexSelectValue))
1174 dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.FirstUniqueBazelLabelList(defaultSelectValue))
1175 } else if config == bazel.OsAndroid {
1176 (&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels))
1177 (&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs))
1178 dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, bazel.FirstUniqueBazelLabelList(inApexSelectValue))
1179 dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, bazel.FirstUniqueBazelLabelList(nonApexSelectValue))
1180 }
1181 }
1182}
1183
Jingwen Chen55bc8202021-11-02 06:40:51 +00001184func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) {
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001185 bp2BuildPropParseHelper(ctx, module, &StripProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1186 if stripProperties, ok := props.(*StripProperties); ok {
1187 la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols)
1188 la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list)
1189 la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame)
1190 la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All)
1191 la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None)
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001192 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001193 })
Liz Kammere6583482021-10-19 13:56:10 -04001194}
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001195
Jingwen Chen55bc8202021-11-02 06:40:51 +00001196func (la *linkerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Jingwen Chen6ada5892021-09-17 11:38:09 +00001197
Liz Kammer47535c52021-06-02 16:02:22 -04001198 type productVarDep struct {
1199 // the name of the corresponding excludes field, if one exists
1200 excludesField string
1201 // reference to the bazel attribute that should be set for the given product variable config
1202 attribute *bazel.LabelListAttribute
Liz Kammer2d7bbe32021-06-10 18:20:06 -04001203
Jingwen Chen55bc8202021-11-02 06:40:51 +00001204 depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList
Liz Kammer47535c52021-06-02 16:02:22 -04001205 }
1206
Zi Wang0a8a1292022-08-30 06:27:01 +00001207 // an intermediate attribute that holds Header_libs info, and will be appended to
1208 // implementationDeps at the end, to solve the confliction that both header_libs
1209 // and static_libs use implementationDeps.
1210 var headerDeps bazel.LabelListAttribute
1211
Liz Kammer47535c52021-06-02 16:02:22 -04001212 productVarToDepFields := map[string]productVarDep{
1213 // product variables do not support exclude_shared_libs
Jingwen Chen55bc8202021-11-02 06:40:51 +00001214 "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes},
1215 "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes},
1216 "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes},
Zi Wang0a8a1292022-08-30 06:27:01 +00001217 "Header_libs": {attribute: &headerDeps, depResolutionFunc: bazelLabelForHeaderDepsExcludes},
Liz Kammer47535c52021-06-02 16:02:22 -04001218 }
1219
Liz Kammer47535c52021-06-02 16:02:22 -04001220 for name, dep := range productVarToDepFields {
1221 props, exists := productVariableProps[name]
1222 excludeProps, excludesExists := productVariableProps[dep.excludesField]
Sasha Smundak39a301c2022-12-29 17:11:49 -08001223 // if neither an include nor excludes property exists, then skip it
Liz Kammer47535c52021-06-02 16:02:22 -04001224 if !exists && !excludesExists {
1225 continue
1226 }
Jingwen Chen25825ca2021-11-15 12:28:43 +00001227 // Collect all the configurations that an include or exclude property exists for.
1228 // We want to iterate all configurations rather than either the include or exclude because, for a
1229 // particular configuration, we may have either only an include or an exclude to handle.
1230 productConfigProps := make(map[android.ProductConfigProperty]bool, len(props)+len(excludeProps))
1231 for p := range props {
1232 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -04001233 }
Jingwen Chen25825ca2021-11-15 12:28:43 +00001234 for p := range excludeProps {
1235 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -04001236 }
1237
Jingwen Chen25825ca2021-11-15 12:28:43 +00001238 for productConfigProp := range productConfigProps {
1239 prop, includesExists := props[productConfigProp]
1240 excludesProp, excludesExists := excludeProps[productConfigProp]
Liz Kammer47535c52021-06-02 16:02:22 -04001241 var includes, excludes []string
1242 var ok bool
1243 // if there was no includes/excludes property, casting fails and that's expected
Jingwen Chen25825ca2021-11-15 12:28:43 +00001244 if includes, ok = prop.([]string); includesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -04001245 ctx.ModuleErrorf("Could not convert product variable %s property", name)
1246 }
Jingwen Chen25825ca2021-11-15 12:28:43 +00001247 if excludes, ok = excludesProp.([]string); excludesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -04001248 ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
1249 }
Liz Kammer2d7bbe32021-06-10 18:20:06 -04001250
Jingwen Chen58ff6802021-11-17 12:14:41 +00001251 dep.attribute.EmitEmptyList = productConfigProp.AlwaysEmit()
Jingwen Chen25825ca2021-11-15 12:28:43 +00001252 dep.attribute.SetSelectValue(
1253 productConfigProp.ConfigurationAxis(),
1254 productConfigProp.SelectKey(),
1255 dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes),
1256 )
Liz Kammer47535c52021-06-02 16:02:22 -04001257 }
1258 }
Zi Wang0a8a1292022-08-30 06:27:01 +00001259 la.implementationDeps.Append(headerDeps)
Liz Kammere6583482021-10-19 13:56:10 -04001260}
Liz Kammer47535c52021-06-02 16:02:22 -04001261
Liz Kammer54309532021-12-14 12:21:22 -05001262func (la *linkerAttributes) finalize(ctx android.BazelConversionPathContext) {
1263 // if system dynamic deps have the default value, any use of a system dynamic library used will
1264 // result in duplicate library errors for bionic OSes. Here, we explicitly exclude those libraries
Liz Kammer43345e22022-08-04 13:57:35 -04001265 // from bionic OSes and the no config case as these libraries only build for bionic OSes.
Liz Kammer54309532021-12-14 12:21:22 -05001266 if la.systemDynamicDeps.IsNil() && len(la.usedSystemDynamicDepAsDynamicDep) > 0 {
Cole Faust18994c72023-02-28 16:02:16 -08001267 toRemove := bazelLabelForSharedDeps(ctx, android.SortedKeys(la.usedSystemDynamicDepAsDynamicDep))
Liz Kammer43345e22022-08-04 13:57:35 -04001268 la.dynamicDeps.Exclude(bazel.NoConfigAxis, "", toRemove)
Liz Kammer54309532021-12-14 12:21:22 -05001269 la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove)
1270 la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove)
Liz Kammer91487d42022-09-13 11:27:11 -04001271 la.implementationDynamicDeps.Exclude(bazel.NoConfigAxis, "", toRemove)
Liz Kammer54309532021-12-14 12:21:22 -05001272 la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove)
1273 la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove)
Liz Kammer91487d42022-09-13 11:27:11 -04001274
1275 la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, toRemove)
1276 la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, toRemove)
1277 stubsToRemove := make([]bazel.Label, 0, len(la.usedSystemDynamicDepAsDynamicDep))
1278 for _, lib := range toRemove.Includes {
1279 lib.Label += stubsSuffix
1280 stubsToRemove = append(stubsToRemove, lib)
1281 }
1282 la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, bazel.MakeLabelList(stubsToRemove))
Liz Kammer54309532021-12-14 12:21:22 -05001283 }
1284
Liz Kammere6583482021-10-19 13:56:10 -04001285 la.deps.ResolveExcludes()
1286 la.implementationDeps.ResolveExcludes()
1287 la.dynamicDeps.ResolveExcludes()
1288 la.implementationDynamicDeps.ResolveExcludes()
1289 la.wholeArchiveDeps.ResolveExcludes()
1290 la.systemDynamicDeps.ForceSpecifyEmptyList = true
Liz Kammer54309532021-12-14 12:21:22 -05001291
Jingwen Chen91220d72021-03-24 02:18:33 -04001292}
1293
Jingwen Chened9c17d2021-04-13 07:14:55 +00001294// Relativize a list of root-relative paths with respect to the module's
1295// directory.
1296//
1297// include_dirs Soong prop are root-relative (b/183742505), but
1298// local_include_dirs, export_include_dirs and export_system_include_dirs are
1299// module dir relative. This function makes a list of paths entirely module dir
1300// relative.
1301//
1302// For the `include` attribute, Bazel wants the paths to be relative to the
1303// module.
1304func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +00001305 var relativePaths []string
1306 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +00001307 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
1308 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
1309 if err != nil {
1310 panic(err)
1311 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +00001312 relativePaths = append(relativePaths, relativePath)
1313 }
1314 return relativePaths
1315}
1316
Liz Kammer5fad5012021-09-09 14:08:21 -04001317// BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel
1318// attributes.
1319type BazelIncludes struct {
Liz Kammer1263d9b2021-12-10 14:28:20 -05001320 AbsoluteIncludes bazel.StringListAttribute
1321 Includes bazel.StringListAttribute
1322 SystemIncludes bazel.StringListAttribute
Liz Kammer5fad5012021-09-09 14:08:21 -04001323}
1324
Liz Kammer54549442022-05-11 13:55:06 -04001325func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module, includes *BazelIncludes) BazelIncludes {
Liz Kammer1263d9b2021-12-10 14:28:20 -05001326 var exported BazelIncludes
1327 if includes != nil {
1328 exported = *includes
1329 } else {
1330 exported = BazelIncludes{}
1331 }
Zi Wang1cb11802022-12-09 16:08:54 -08001332
1333 // cc library Export_include_dirs and Export_system_include_dirs are marked
1334 // "variant_prepend" in struct tag, set their prepend property to true to make
1335 // sure bp2build generates correct result.
1336 exported.Includes.Prepend = true
1337 exported.SystemIncludes.Prepend = true
1338
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001339 bp2BuildPropParseHelper(ctx, module, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1340 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
1341 if len(flagExporterProperties.Export_include_dirs) > 0 {
1342 exported.Includes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.Includes.SelectValue(axis, config), flagExporterProperties.Export_include_dirs...)))
1343 }
1344 if len(flagExporterProperties.Export_system_include_dirs) > 0 {
1345 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 -04001346 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -04001347 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001348 })
Liz Kammer1263d9b2021-12-10 14:28:20 -05001349 exported.AbsoluteIncludes.DeduplicateAxesFromBase()
Liz Kammer5fad5012021-09-09 14:08:21 -04001350 exported.Includes.DeduplicateAxesFromBase()
1351 exported.SystemIncludes.DeduplicateAxesFromBase()
Rupert Shuttleworth375451e2021-04-26 07:49:08 -04001352
Liz Kammer5fad5012021-09-09 14:08:21 -04001353 return exported
Jingwen Chen91220d72021-03-24 02:18:33 -04001354}
Chris Parsons953b3562021-09-20 15:14:39 -04001355
Trevor Radcliffecee4e052022-09-06 19:31:25 +00001356func BazelLabelNameForStaticModule(baseLabel string) string {
1357 return baseLabel + "_bp2build_cc_library_static"
1358}
1359
Jingwen Chen55bc8202021-11-02 06:40:51 +00001360func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001361 label := android.BazelModuleLabel(ctx, m)
Sasha Smundak39a301c2022-12-29 17:11:49 -08001362 if ccModule, ok := m.(*Module); ok && ccModule.typ() == fullLibrary {
Trevor Radcliffecee4e052022-09-06 19:31:25 +00001363 return BazelLabelNameForStaticModule(label)
Chris Parsons953b3562021-09-20 15:14:39 -04001364 }
1365 return label
1366}
1367
Jingwen Chen55bc8202021-11-02 06:40:51 +00001368func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001369 // cc_library, at it's root name, propagates the shared library, which depends on the static
1370 // library.
1371 return android.BazelModuleLabel(ctx, m)
1372}
1373
Jingwen Chen55bc8202021-11-02 06:40:51 +00001374func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -04001375 label := bazelLabelForStaticModule(ctx, m)
1376 if aModule, ok := m.(android.Module); ok {
1377 if android.IsModulePrebuilt(aModule) {
1378 label += "_alwayslink"
1379 }
1380 }
1381 return label
1382}
1383
Jingwen Chen55bc8202021-11-02 06:40:51 +00001384func bazelLabelForWholeDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001385 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps)
1386}
1387
Jingwen Chen55bc8202021-11-02 06:40:51 +00001388func bazelLabelForWholeDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001389 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps)
1390}
1391
Jingwen Chen55bc8202021-11-02 06:40:51 +00001392func bazelLabelForStaticDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001393 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule)
1394}
1395
Jingwen Chen55bc8202021-11-02 06:40:51 +00001396func bazelLabelForStaticDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001397 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule)
1398}
1399
Jingwen Chen55bc8202021-11-02 06:40:51 +00001400func bazelLabelForSharedDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001401 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule)
1402}
1403
Jingwen Chen55bc8202021-11-02 06:40:51 +00001404func bazelLabelForHeaderDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001405 // This is not elegant, but bp2build's shared library targets only propagate
1406 // their header information as part of the normal C++ provider.
1407 return bazelLabelForSharedDeps(ctx, modules)
1408}
1409
Zi Wang0a8a1292022-08-30 06:27:01 +00001410func bazelLabelForHeaderDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
1411 // This is only used when product_variable header_libs is processed, to follow
1412 // the pattern of depResolutionFunc
1413 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
1414}
1415
Jingwen Chen55bc8202021-11-02 06:40:51 +00001416func bazelLabelForSharedDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -04001417 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
1418}
Liz Kammer2b8004b2021-10-04 13:55:44 -04001419
1420type binaryLinkerAttrs struct {
1421 Linkshared *bool
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05001422 Suffix bazel.StringAttribute
Liz Kammer2b8004b2021-10-04 13:55:44 -04001423}
1424
Jingwen Chen55bc8202021-11-02 06:40:51 +00001425func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs {
Liz Kammer2b8004b2021-10-04 13:55:44 -04001426 attrs := binaryLinkerAttrs{}
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001427 bp2BuildPropParseHelper(ctx, m, &BinaryLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1428 linkerProps := props.(*BinaryLinkerProperties)
1429 staticExecutable := linkerProps.Static_executable
1430 if axis == bazel.NoConfigAxis {
1431 if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared {
1432 attrs.Linkshared = &linkBinaryShared
Liz Kammer2b8004b2021-10-04 13:55:44 -04001433 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001434 } else if staticExecutable != nil {
1435 // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a
1436 // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling
1437 ctx.ModuleErrorf("bp2build cannot migrate a module with arch/target-specific static_executable values")
Liz Kammer2b8004b2021-10-04 13:55:44 -04001438 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxa56e9702022-02-23 18:39:59 -05001439 if suffix := linkerProps.Suffix; suffix != nil {
1440 attrs.Suffix.SetSelectValue(axis, config, suffix)
1441 }
Trevor Radcliffe542954f2022-04-21 20:04:42 +00001442 })
Liz Kammer2b8004b2021-10-04 13:55:44 -04001443
1444 return attrs
1445}
Trevor Radcliffedb7e0262022-10-28 16:48:18 +00001446
1447func bp2buildSanitizerFeatures(ctx android.BazelConversionPathContext, m *Module) bazel.StringListAttribute {
1448 sanitizerFeatures := bazel.StringListAttribute{}
1449 bp2BuildPropParseHelper(ctx, m, &SanitizeProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1450 var features []string
1451 if sanitizerProps, ok := props.(*SanitizeProperties); ok {
1452 if sanitizerProps.Sanitize.Integer_overflow != nil && *sanitizerProps.Sanitize.Integer_overflow {
1453 features = append(features, "ubsan_integer_overflow")
1454 }
1455 for _, sanitizer := range sanitizerProps.Sanitize.Misc_undefined {
1456 features = append(features, "ubsan_"+sanitizer)
1457 }
1458 sanitizerFeatures.SetSelectValue(axis, config, features)
1459 }
1460 })
1461 return sanitizerFeatures
1462}
Trevor Radcliffe56b1a2b2023-02-06 21:58:30 +00001463
1464func bp2buildLtoFeatures(ctx android.BazelConversionPathContext, m *Module) bazel.StringListAttribute {
1465 lto_feature_name := "android_thin_lto"
1466 ltoBoolFeatures := bazel.BoolAttribute{}
1467 bp2BuildPropParseHelper(ctx, m, &LTOProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) {
1468 if ltoProps, ok := props.(*LTOProperties); ok {
1469 thinProp := ltoProps.Lto.Thin != nil && *ltoProps.Lto.Thin
1470 thinPropSetToFalse := ltoProps.Lto.Thin != nil && !*ltoProps.Lto.Thin
1471 neverProp := ltoProps.Lto.Never != nil && *ltoProps.Lto.Never
1472 if thinProp {
1473 ltoBoolFeatures.SetSelectValue(axis, config, BoolPtr(true))
1474 return
1475 }
1476 if neverProp || thinPropSetToFalse {
1477 if thinProp {
1478 ctx.ModuleErrorf("lto.thin and lto.never are mutually exclusive but were specified together")
1479 } else {
1480 ltoBoolFeatures.SetSelectValue(axis, config, BoolPtr(false))
1481 }
1482 return
1483 }
1484 }
1485 ltoBoolFeatures.SetSelectValue(axis, config, nil)
1486 })
1487
1488 props := m.GetArchVariantProperties(ctx, &LTOProperties{})
1489 ltoStringFeatures, err := ltoBoolFeatures.ToStringListAttribute(func(boolPtr *bool, axis bazel.ConfigurationAxis, config string) []string {
1490 if boolPtr == nil {
1491 return []string{}
1492 }
1493 if !*boolPtr {
1494 return []string{"-" + lto_feature_name}
1495 }
1496 features := []string{lto_feature_name}
1497 if ltoProps, ok := props[axis][config].(*LTOProperties); ok {
1498 if ltoProps.Whole_program_vtables != nil && *ltoProps.Whole_program_vtables {
1499 features = append(features, "android_thin_lto_whole_program_vtables")
1500 }
1501 }
1502 return features
1503 })
1504 if err != nil {
1505 ctx.ModuleErrorf("Error processing LTO attributes: %s", err)
1506 }
1507 return ltoStringFeatures
1508}