blob: f9bbe87525ce951e11feeadaff9cf2fc3a6e908f [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//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
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"
Liz Kammer7a210ac2021-09-22 15:52:58 -040023
Chris Parsons953b3562021-09-20 15:14:39 -040024 "github.com/google/blueprint"
Liz Kammerba7a9c52021-05-26 08:45:30 -040025
26 "github.com/google/blueprint/proptools"
Jingwen Chen91220d72021-03-24 02:18:33 -040027)
28
Liz Kammerae3994e2021-10-19 09:45:48 -040029const (
Liz Kammer12615db2021-09-28 09:19:17 -040030 cSrcPartition = "c"
31 asSrcPartition = "as"
32 cppSrcPartition = "cpp"
33 protoSrcPartition = "proto"
Liz Kammerae3994e2021-10-19 09:45:48 -040034)
35
Liz Kammer2222c6b2021-05-24 15:41:47 -040036// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
Jingwen Chenbcf53042021-05-26 04:42:42 +000037// properties which apply to either the shared or static version of a cc_library module.
Liz Kammer2222c6b2021-05-24 15:41:47 -040038type staticOrSharedAttributes struct {
Jingwen Chenc4dc9b42021-06-11 12:51:48 +000039 Srcs bazel.LabelListAttribute
40 Srcs_c bazel.LabelListAttribute
41 Srcs_as bazel.LabelListAttribute
Liz Kammere6583482021-10-19 13:56:10 -040042 Hdrs bazel.LabelListAttribute
Jingwen Chenc4dc9b42021-06-11 12:51:48 +000043 Copts bazel.StringListAttribute
Jingwen Chen14a8bda2021-06-02 11:10:02 +000044
Liz Kammer12615db2021-09-28 09:19:17 -040045 Deps bazel.LabelListAttribute
46 Implementation_deps bazel.LabelListAttribute
47 Dynamic_deps bazel.LabelListAttribute
48 Implementation_dynamic_deps bazel.LabelListAttribute
49 Whole_archive_deps bazel.LabelListAttribute
50 Implementation_whole_archive_deps bazel.LabelListAttribute
Chris Parsons51f8c392021-08-03 21:01:05 -040051
52 System_dynamic_deps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +000053}
54
Jingwen Chen55bc8202021-11-02 06:40:51 +000055func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute {
Liz Kammer12615db2021-09-28 09:19:17 -040056 // Check that a module is a filegroup type
57 isFilegroup := func(m blueprint.Module) bool {
58 return ctx.OtherModuleType(m) == "filegroup"
Jingwen Chen14a8bda2021-06-02 11:10:02 +000059 }
60
Liz Kammer57e2e7a2021-09-20 12:55:02 -040061 // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl
62 // macro.
63 addSuffixForFilegroup := func(suffix string) bazel.LabelMapper {
Liz Kammer12615db2021-09-28 09:19:17 -040064 return func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) {
65 m, exists := ctx.ModuleFromName(label.OriginalModuleName)
66 labelStr := label.Label
67 if !exists || !isFilegroup(m) {
68 return labelStr, false
Jingwen Chen14a8bda2021-06-02 11:10:02 +000069 }
Liz Kammer12615db2021-09-28 09:19:17 -040070 return labelStr + suffix, true
Chris Parsons5a34ffb2021-07-21 14:34:58 -040071 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +000072 }
73
Liz Kammer12615db2021-09-28 09:19:17 -040074 isProtoFilegroup := func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) {
75 m, exists := ctx.ModuleFromName(label.OriginalModuleName)
76 labelStr := label.Label
77 if !exists || !isFilegroup(m) {
78 return labelStr, false
79 }
80 likelyProtos := strings.HasSuffix(labelStr, "proto") || strings.HasSuffix(labelStr, "protos")
81 return labelStr, likelyProtos
82 }
83
Liz Kammer57e2e7a2021-09-20 12:55:02 -040084 // TODO(b/190006308): Handle language detection of sources in a Bazel rule.
85 partitioned := bazel.PartitionLabelListAttribute(ctx, &srcs, bazel.LabelPartitions{
Liz Kammerae3994e2021-10-19 09:45:48 -040086 cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")},
87 asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")},
Liz Kammer57e2e7a2021-09-20 12:55:02 -040088 // C++ is the "catch-all" group, and comprises generated sources because we don't
89 // know the language of these sources until the genrule is executed.
Liz Kammer12615db2021-09-28 09:19:17 -040090 cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true},
91 protoSrcPartition: bazel.LabelPartition{Extensions: []string{".proto"}, LabelMapper: isProtoFilegroup},
Liz Kammer57e2e7a2021-09-20 12:55:02 -040092 })
Jingwen Chen14a8bda2021-06-02 11:10:02 +000093
Liz Kammerae3994e2021-10-19 09:45:48 -040094 return partitioned
Jingwen Chen14a8bda2021-06-02 11:10:02 +000095}
96
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000097// bp2BuildParseLibProps returns the attributes for a variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +000098func bp2BuildParseLibProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +000099 lib, ok := module.compiler.(*libraryDecorator)
100 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400101 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000102 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000103 return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic)
104}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000105
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000106// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000107func bp2BuildParseSharedProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000108 return bp2BuildParseLibProps(ctx, module, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000109}
110
111// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000112func bp2BuildParseStaticProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000113 return bp2BuildParseLibProps(ctx, module, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400114}
115
Liz Kammer7a210ac2021-09-22 15:52:58 -0400116type depsPartition struct {
117 export bazel.LabelList
118 implementation bazel.LabelList
119}
120
Jingwen Chen55bc8202021-11-02 06:40:51 +0000121type bazelLabelForDepsFn func(android.BazelConversionPathContext, []string) bazel.LabelList
Liz Kammer7a210ac2021-09-22 15:52:58 -0400122
Jingwen Chen55bc8202021-11-02 06:40:51 +0000123func maybePartitionExportedAndImplementationsDeps(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, exportedDeps []string, fn bazelLabelForDepsFn) depsPartition {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400124 if !exportsDeps {
125 return depsPartition{
126 implementation: fn(ctx, allDeps),
127 }
128 }
129
Liz Kammer7a210ac2021-09-22 15:52:58 -0400130 implementation, export := android.FilterList(allDeps, exportedDeps)
131
132 return depsPartition{
133 export: fn(ctx, export),
134 implementation: fn(ctx, implementation),
135 }
136}
137
Jingwen Chen55bc8202021-11-02 06:40:51 +0000138type bazelLabelForDepsExcludesFn func(android.BazelConversionPathContext, []string, []string) bazel.LabelList
Liz Kammer7a210ac2021-09-22 15:52:58 -0400139
Jingwen Chen55bc8202021-11-02 06:40:51 +0000140func maybePartitionExportedAndImplementationsDepsExcludes(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, excludes, exportedDeps []string, fn bazelLabelForDepsExcludesFn) depsPartition {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400141 if !exportsDeps {
142 return depsPartition{
143 implementation: fn(ctx, allDeps, excludes),
144 }
145 }
Liz Kammer7a210ac2021-09-22 15:52:58 -0400146 implementation, export := android.FilterList(allDeps, exportedDeps)
147
148 return depsPartition{
149 export: fn(ctx, export, excludes),
150 implementation: fn(ctx, implementation, excludes),
151 }
152}
153
Jingwen Chen55bc8202021-11-02 06:40:51 +0000154func bp2buildParseStaticOrSharedProps(ctx android.BazelConversionPathContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes {
Liz Kammer135bf552021-08-11 10:46:06 -0400155 attrs := staticOrSharedAttributes{}
Jingwen Chenbcf53042021-05-26 04:42:42 +0000156
Liz Kammer9abd62d2021-05-21 08:37:59 -0400157 setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000158 attrs.Copts.SetSelectValue(axis, config, props.Cflags)
159 attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
Chris Parsons953b3562021-09-20 15:14:39 -0400160 attrs.System_dynamic_deps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, props.System_shared_libs))
Liz Kammer7a210ac2021-09-22 15:52:58 -0400161
Liz Kammer2b8004b2021-10-04 13:55:44 -0400162 staticDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Static_libs, props.Export_static_lib_headers, bazelLabelForStaticDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400163 attrs.Deps.SetSelectValue(axis, config, staticDeps.export)
164 attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation)
165
Liz Kammer2b8004b2021-10-04 13:55:44 -0400166 sharedDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400167 attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export)
168 attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation)
169
170 attrs.Whole_archive_deps.SetSelectValue(axis, config, bazelLabelForWholeDeps(ctx, props.Whole_static_libs))
Jingwen Chenbcf53042021-05-26 04:42:42 +0000171 }
Liz Kammer135bf552021-08-11 10:46:06 -0400172 // system_dynamic_deps distinguishes between nil/empty list behavior:
173 // nil -> use default values
174 // empty list -> no values specified
175 attrs.System_dynamic_deps.ForceSpecifyEmptyList = true
Jingwen Chenbcf53042021-05-26 04:42:42 +0000176
177 if isStatic {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400178 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) {
179 for config, props := range configToProps {
180 if staticOrSharedProps, ok := props.(*StaticProperties); ok {
181 setAttrs(axis, config, staticOrSharedProps.Static)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000182 }
183 }
184 }
185 } else {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400186 for axis, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) {
187 for config, props := range configToProps {
188 if staticOrSharedProps, ok := props.(*SharedProperties); ok {
189 setAttrs(axis, config, staticOrSharedProps.Shared)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000190 }
191 }
192 }
193 }
194
Liz Kammerae3994e2021-10-19 09:45:48 -0400195 partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs)
196 attrs.Srcs = partitionedSrcs[cppSrcPartition]
197 attrs.Srcs_c = partitionedSrcs[cSrcPartition]
198 attrs.Srcs_as = partitionedSrcs[asSrcPartition]
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000199
Liz Kammer12615db2021-09-28 09:19:17 -0400200 if !partitionedSrcs[protoSrcPartition].IsEmpty() {
201 // TODO(b/208815215): determine whether this is used and add support if necessary
202 ctx.ModuleErrorf("Migrating static/shared only proto srcs is not currently supported")
203 }
204
Jingwen Chenbcf53042021-05-26 04:42:42 +0000205 return attrs
Jingwen Chen53681ef2021-04-29 08:15:13 +0000206}
207
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400208// Convenience struct to hold all attributes parsed from prebuilt properties.
209type prebuiltAttributes struct {
210 Src bazel.LabelAttribute
211}
212
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000213// NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package
Jingwen Chen55bc8202021-11-02 06:40:51 +0000214func Bp2BuildParsePrebuiltLibraryProps(ctx android.BazelConversionPathContext, module *Module) prebuiltAttributes {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400215 var srcLabelAttribute bazel.LabelAttribute
216
Liz Kammer9abd62d2021-05-21 08:37:59 -0400217 for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltLinkerProperties{}) {
218 for config, props := range configToProps {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400219 if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok {
220 if len(prebuiltLinkerProperties.Srcs) > 1 {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400221 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for %s %s\n", axis, config)
222 continue
223 } else if len(prebuiltLinkerProperties.Srcs) == 0 {
224 continue
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400225 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400226 src := android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0])
227 srcLabelAttribute.SetSelectValue(axis, config, src)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400228 }
229 }
230 }
231
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400232 return prebuiltAttributes{
233 Src: srcLabelAttribute,
234 }
235}
236
Liz Kammere6583482021-10-19 13:56:10 -0400237type baseAttributes struct {
238 compilerAttributes
239 linkerAttributes
Liz Kammer12615db2021-09-28 09:19:17 -0400240
241 protoDependency *bazel.LabelAttribute
Liz Kammere6583482021-10-19 13:56:10 -0400242}
243
Jingwen Chen107c0de2021-04-09 10:43:12 +0000244// Convenience struct to hold all attributes parsed from compiler properties.
245type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400246 // Options for all languages
247 copts bazel.StringListAttribute
248 // Assembly options and sources
249 asFlags bazel.StringListAttribute
250 asSrcs bazel.LabelListAttribute
251 // C options and sources
252 conlyFlags bazel.StringListAttribute
253 cSrcs bazel.LabelListAttribute
254 // C++ options and sources
255 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000256 srcs bazel.LabelListAttribute
Chris Parsons2c788392021-08-10 11:58:07 -0400257
Liz Kammere6583482021-10-19 13:56:10 -0400258 hdrs bazel.LabelListAttribute
259
Chris Parsons2c788392021-08-10 11:58:07 -0400260 rtti bazel.BoolAttribute
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000261
262 // Not affected by arch variants
263 stl *string
Chris Parsons79bd2b72021-11-29 17:52:41 -0500264 cStd *string
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000265 cppStd *string
Liz Kammer35687bc2021-09-10 10:07:07 -0400266
267 localIncludes bazel.StringListAttribute
268 absoluteIncludes bazel.StringListAttribute
Liz Kammer12615db2021-09-28 09:19:17 -0400269
270 protoSrcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000271}
272
Liz Kammere6583482021-10-19 13:56:10 -0400273func parseCommandLineFlags(soongFlags []string) []string {
274 var result []string
275 for _, flag := range soongFlags {
276 // Soong's cflags can contain spaces, like `-include header.h`. For
277 // Bazel's copts, split them up to be compatible with the
278 // no_copts_tokenization feature.
279 result = append(result, strings.Split(flag, " ")...)
280 }
281 return result
282}
Jingwen Chened9c17d2021-04-13 07:14:55 +0000283
Jingwen Chen55bc8202021-11-02 06:40:51 +0000284func (ca *compilerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, props *BaseCompilerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400285 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
286 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
287 if srcsList, ok := parseSrcs(ctx, props); ok {
288 ca.srcs.SetSelectValue(axis, config, srcsList)
Chris Parsons990c4f42021-05-25 12:10:58 -0400289 }
290
Liz Kammere6583482021-10-19 13:56:10 -0400291 localIncludeDirs := props.Local_include_dirs
292 if axis == bazel.NoConfigAxis {
Chris Parsons79bd2b72021-11-29 17:52:41 -0500293 ca.cStd, ca.cppStd = bp2buildResolveCppStdValue(props.C_std, props.Cpp_std, props.Gnu_extensions)
Liz Kammere6583482021-10-19 13:56:10 -0400294 if includeBuildDirectory(props.Include_build_directory) {
295 localIncludeDirs = append(localIncludeDirs, ".")
Liz Kammer222bdcf2021-10-11 14:15:51 -0400296 }
Jingwen Chene32e9e02021-04-23 09:17:24 +0000297 }
298
Liz Kammere6583482021-10-19 13:56:10 -0400299 ca.absoluteIncludes.SetSelectValue(axis, config, props.Include_dirs)
300 ca.localIncludes.SetSelectValue(axis, config, localIncludeDirs)
301
302 ca.copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags))
303 ca.asFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Asflags))
304 ca.conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Conlyflags))
305 ca.cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Cppflags))
306 ca.rtti.SetSelectValue(axis, config, props.Rtti)
307}
308
Jingwen Chen55bc8202021-11-02 06:40:51 +0000309func (ca *compilerAttributes) convertStlProps(ctx android.ArchVariantContext, module *Module) {
Liz Kammere6583482021-10-19 13:56:10 -0400310 stlPropsByArch := module.GetArchVariantProperties(ctx, &StlProperties{})
311 for _, configToProps := range stlPropsByArch {
312 for _, props := range configToProps {
313 if stlProps, ok := props.(*StlProperties); ok {
314 if stlProps.Stl == nil {
315 continue
Liz Kammer9abd62d2021-05-21 08:37:59 -0400316 }
Liz Kammere6583482021-10-19 13:56:10 -0400317 if ca.stl == nil {
318 ca.stl = stlProps.Stl
319 } else if ca.stl != stlProps.Stl {
320 ctx.ModuleErrorf("Unsupported conversion: module with different stl for different variants: %s and %s", *ca.stl, stlProps.Stl)
Liz Kammerae3994e2021-10-19 09:45:48 -0400321 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400322 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000323 }
324 }
Liz Kammere6583482021-10-19 13:56:10 -0400325}
Jingwen Chenc1c26502021-04-05 10:35:13 +0000326
Jingwen Chen55bc8202021-11-02 06:40:51 +0000327func (ca *compilerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Liz Kammerba7a9c52021-05-26 08:45:30 -0400328 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
Liz Kammere6583482021-10-19 13:56:10 -0400329 "Cflags": &ca.copts,
330 "Asflags": &ca.asFlags,
331 "CppFlags": &ca.cppFlags,
Liz Kammerba7a9c52021-05-26 08:45:30 -0400332 }
Liz Kammerba7a9c52021-05-26 08:45:30 -0400333 for propName, attr := range productVarPropNameToAttribute {
Jingwen Chen25825ca2021-11-15 12:28:43 +0000334 if productConfigProps, exists := productVariableProps[propName]; exists {
335 for productConfigProp, prop := range productConfigProps {
336 flags, ok := prop.([]string)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400337 if !ok {
338 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
339 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000340 newFlags, _ := bazel.TryVariableSubstitutions(flags, productConfigProp.Name)
341 attr.SetSelectValue(productConfigProp.ConfigurationAxis(), productConfigProp.SelectKey(), newFlags)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400342 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400343 }
344 }
Liz Kammere6583482021-10-19 13:56:10 -0400345}
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400346
Jingwen Chen55bc8202021-11-02 06:40:51 +0000347func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, implementationHdrs bazel.LabelListAttribute) {
Liz Kammere6583482021-10-19 13:56:10 -0400348 ca.srcs.ResolveExcludes()
349 partitionedSrcs := groupSrcsByExtension(ctx, ca.srcs)
350
Liz Kammer12615db2021-09-28 09:19:17 -0400351 ca.protoSrcs = partitionedSrcs[protoSrcPartition]
352
Liz Kammere6583482021-10-19 13:56:10 -0400353 for p, lla := range partitionedSrcs {
354 // if there are no sources, there is no need for headers
355 if lla.IsEmpty() {
356 continue
357 }
358 lla.Append(implementationHdrs)
359 partitionedSrcs[p] = lla
360 }
361
362 ca.srcs = partitionedSrcs[cppSrcPartition]
363 ca.cSrcs = partitionedSrcs[cSrcPartition]
364 ca.asSrcs = partitionedSrcs[asSrcPartition]
365
366 ca.absoluteIncludes.DeduplicateAxesFromBase()
367 ca.localIncludes.DeduplicateAxesFromBase()
368}
369
370// Parse srcs from an arch or OS's props value.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000371func parseSrcs(ctx android.BazelConversionPathContext, props *BaseCompilerProperties) (bazel.LabelList, bool) {
Liz Kammere6583482021-10-19 13:56:10 -0400372 anySrcs := false
373 // Add srcs-like dependencies such as generated files.
374 // First create a LabelList containing these dependencies, then merge the values with srcs.
375 generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources)
376 if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 {
377 anySrcs = true
378 }
379
380 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs)
381 if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 {
382 anySrcs = true
383 }
384 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs
385}
386
Chris Parsons79bd2b72021-11-29 17:52:41 -0500387func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) {
388 var cStdVal, cppStdVal string
389 // If c{,pp}std properties are not specified, don't generate them in the BUILD file.
390 // Defaults are handled by the toolchain definition.
391 // However, if gnu_extensions is false, then the default gnu-to-c version must be specified.
Liz Kammere6583482021-10-19 13:56:10 -0400392 if cpp_std != nil {
Chris Parsons79bd2b72021-11-29 17:52:41 -0500393 cppStdVal = parseCppStd(cpp_std)
Liz Kammere6583482021-10-19 13:56:10 -0400394 } else if gnu_extensions != nil && !*gnu_extensions {
Chris Parsons79bd2b72021-11-29 17:52:41 -0500395 cppStdVal = "c++17"
Liz Kammere6583482021-10-19 13:56:10 -0400396 }
Chris Parsons79bd2b72021-11-29 17:52:41 -0500397 if c_std != nil {
398 cStdVal = parseCStd(c_std)
399 } else if gnu_extensions != nil && !*gnu_extensions {
400 cStdVal = "c99"
401 }
402
403 cStdVal, cppStdVal = maybeReplaceGnuToC(gnu_extensions, cStdVal, cppStdVal)
Liz Kammer46fb7ab2021-12-01 10:09:34 -0500404 var c_std_prop, cpp_std_prop *string
405 if cStdVal != "" {
406 c_std_prop = &cStdVal
407 }
408 if cppStdVal != "" {
409 cpp_std_prop = &cppStdVal
410 }
411
412 return c_std_prop, cpp_std_prop
Liz Kammere6583482021-10-19 13:56:10 -0400413}
414
415// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Liz Kammer12615db2021-09-28 09:19:17 -0400416func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) baseAttributes {
Liz Kammere6583482021-10-19 13:56:10 -0400417 archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
418 archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{})
419
420 var implementationHdrs bazel.LabelListAttribute
421
422 axisToConfigs := map[bazel.ConfigurationAxis]map[string]bool{}
423 allAxesAndConfigs := func(cp android.ConfigurationAxisToArchVariantProperties) {
424 for axis, configMap := range cp {
425 if _, ok := axisToConfigs[axis]; !ok {
426 axisToConfigs[axis] = map[string]bool{}
427 }
428 for config, _ := range configMap {
429 axisToConfigs[axis][config] = true
Chris Parsonsa967f252021-09-23 16:34:35 -0400430 }
431 }
432 }
Liz Kammere6583482021-10-19 13:56:10 -0400433 allAxesAndConfigs(archVariantCompilerProps)
434 allAxesAndConfigs(archVariantLinkerProps)
Chris Parsonsa967f252021-09-23 16:34:35 -0400435
Liz Kammere6583482021-10-19 13:56:10 -0400436 compilerAttrs := compilerAttributes{}
437 linkerAttrs := linkerAttributes{}
438
439 for axis, configs := range axisToConfigs {
440 for config, _ := range configs {
441 var allHdrs []string
442 if baseCompilerProps, ok := archVariantCompilerProps[axis][config].(*BaseCompilerProperties); ok {
443 allHdrs = baseCompilerProps.Generated_headers
444
445 (&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, config, baseCompilerProps)
446 }
447
448 var exportHdrs []string
449
450 if baseLinkerProps, ok := archVariantLinkerProps[axis][config].(*BaseLinkerProperties); ok {
451 exportHdrs = baseLinkerProps.Export_generated_headers
452
453 (&linkerAttrs).bp2buildForAxisAndConfig(ctx, module.Binary(), axis, config, baseLinkerProps)
454 }
455 headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportHdrs, android.BazelLabelForModuleDeps)
456 implementationHdrs.SetSelectValue(axis, config, headers.implementation)
457 compilerAttrs.hdrs.SetSelectValue(axis, config, headers.export)
458 }
459 }
460
461 compilerAttrs.convertStlProps(ctx, module)
462 (&linkerAttrs).convertStripProps(ctx, module)
463
464 productVariableProps := android.ProductVariableProperties(ctx)
465
466 (&compilerAttrs).convertProductVariables(ctx, productVariableProps)
467 (&linkerAttrs).convertProductVariables(ctx, productVariableProps)
468
469 (&compilerAttrs).finalize(ctx, implementationHdrs)
470 (&linkerAttrs).finalize()
471
Liz Kammer12615db2021-09-28 09:19:17 -0400472 protoDep := bp2buildProto(ctx, module, compilerAttrs.protoSrcs)
473
474 // bp2buildProto will only set wholeStaticLib or implementationWholeStaticLib, but we don't know
475 // which. This will add the newly generated proto library to the appropriate attribute and nothing
476 // to the other
477 (&linkerAttrs).wholeArchiveDeps.Add(protoDep.wholeStaticLib)
478 (&linkerAttrs).implementationWholeArchiveDeps.Add(protoDep.implementationWholeStaticLib)
479
Liz Kammere6583482021-10-19 13:56:10 -0400480 return baseAttributes{
481 compilerAttrs,
482 linkerAttrs,
Liz Kammer12615db2021-09-28 09:19:17 -0400483 protoDep.protoDep,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000484 }
485}
486
487// Convenience struct to hold all attributes parsed from linker properties.
488type linkerAttributes struct {
Liz Kammer12615db2021-09-28 09:19:17 -0400489 deps bazel.LabelListAttribute
490 implementationDeps bazel.LabelListAttribute
491 dynamicDeps bazel.LabelListAttribute
492 implementationDynamicDeps bazel.LabelListAttribute
493 wholeArchiveDeps bazel.LabelListAttribute
494 implementationWholeArchiveDeps bazel.LabelListAttribute
495 systemDynamicDeps bazel.LabelListAttribute
Liz Kammer7a210ac2021-09-22 15:52:58 -0400496
Jingwen Chen6ada5892021-09-17 11:38:09 +0000497 linkCrt bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000498 useLibcrt bazel.BoolAttribute
499 linkopts bazel.StringListAttribute
Liz Kammerd2871182021-10-04 13:54:37 -0400500 additionalLinkerInputs bazel.LabelListAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000501 stripKeepSymbols bazel.BoolAttribute
502 stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
503 stripKeepSymbolsList bazel.StringListAttribute
504 stripAll bazel.BoolAttribute
505 stripNone bazel.BoolAttribute
Liz Kammer0eae52e2021-10-06 10:32:26 -0400506 features bazel.StringListAttribute
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400507}
508
Jingwen Chen55bc8202021-11-02 06:40:51 +0000509func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, isBinary bool, axis bazel.ConfigurationAxis, config string, props *BaseLinkerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400510 // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module
511 var axisFeatures []string
Liz Kammer7a210ac2021-09-22 15:52:58 -0400512
Liz Kammere6583482021-10-19 13:56:10 -0400513 // Excludes to parallel Soong:
514 // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0
515 staticLibs := android.FirstUniqueStrings(props.Static_libs)
516 staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, staticLibs, props.Exclude_static_libs, props.Export_static_lib_headers, bazelLabelForStaticDepsExcludes)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400517
Liz Kammere6583482021-10-19 13:56:10 -0400518 headerLibs := android.FirstUniqueStrings(props.Header_libs)
519 hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps)
Jingwen Chen63930982021-03-24 10:04:33 -0400520
Liz Kammere6583482021-10-19 13:56:10 -0400521 (&hDeps.export).Append(staticDeps.export)
522 la.deps.SetSelectValue(axis, config, hDeps.export)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000523
Liz Kammere6583482021-10-19 13:56:10 -0400524 (&hDeps.implementation).Append(staticDeps.implementation)
525 la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation)
Liz Kammer0eae52e2021-10-06 10:32:26 -0400526
Liz Kammere6583482021-10-19 13:56:10 -0400527 wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs)
528 la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs))
529
530 systemSharedLibs := props.System_shared_libs
531 // systemSharedLibs distinguishes between nil/empty list behavior:
532 // nil -> use default values
533 // empty list -> no values specified
534 if len(systemSharedLibs) > 0 {
535 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
536 }
537 la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
538
539 sharedLibs := android.FirstUniqueStrings(props.Shared_libs)
540 sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, sharedLibs, props.Exclude_shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes)
541 la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export)
542 la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation)
543
544 if !BoolDefault(props.Pack_relocations, packRelocationsDefault) {
545 axisFeatures = append(axisFeatures, "disable_pack_relocations")
546 }
547
548 if Bool(props.Allow_undefined_symbols) {
549 axisFeatures = append(axisFeatures, "-no_undefined_symbols")
550 }
551
552 var linkerFlags []string
553 if len(props.Ldflags) > 0 {
554 linkerFlags = append(linkerFlags, props.Ldflags...)
555 // binaries remove static flag if -shared is in the linker flags
556 if isBinary && android.InList("-shared", linkerFlags) {
557 axisFeatures = append(axisFeatures, "-static_flag")
558 }
559 }
560 if props.Version_script != nil {
561 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script)
562 la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}})
563 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label))
564 }
565 la.linkopts.SetSelectValue(axis, config, linkerFlags)
566 la.useLibcrt.SetSelectValue(axis, config, props.libCrt())
567
568 // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it.
569 if props.crt() != nil {
570 if axis == bazel.NoConfigAxis {
571 la.linkCrt.SetSelectValue(axis, config, props.crt())
572 } else if axis == bazel.ArchConfigurationAxis {
573 ctx.ModuleErrorf("nocrt is not supported for arch variants")
574 }
575 }
576
577 if axisFeatures != nil {
578 la.features.SetSelectValue(axis, config, axisFeatures)
579 }
580}
581
Jingwen Chen55bc8202021-11-02 06:40:51 +0000582func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) {
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000583 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StripProperties{}) {
584 for config, props := range configToProps {
585 if stripProperties, ok := props.(*StripProperties); ok {
Liz Kammere6583482021-10-19 13:56:10 -0400586 la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols)
587 la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list)
588 la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame)
589 la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All)
590 la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000591 }
592 }
593 }
Liz Kammere6583482021-10-19 13:56:10 -0400594}
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000595
Jingwen Chen55bc8202021-11-02 06:40:51 +0000596func (la *linkerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Jingwen Chen6ada5892021-09-17 11:38:09 +0000597
Liz Kammer47535c52021-06-02 16:02:22 -0400598 type productVarDep struct {
599 // the name of the corresponding excludes field, if one exists
600 excludesField string
601 // reference to the bazel attribute that should be set for the given product variable config
602 attribute *bazel.LabelListAttribute
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400603
Jingwen Chen55bc8202021-11-02 06:40:51 +0000604 depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList
Liz Kammer47535c52021-06-02 16:02:22 -0400605 }
606
607 productVarToDepFields := map[string]productVarDep{
608 // product variables do not support exclude_shared_libs
Jingwen Chen55bc8202021-11-02 06:40:51 +0000609 "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes},
610 "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes},
611 "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes},
Liz Kammer47535c52021-06-02 16:02:22 -0400612 }
613
Liz Kammer47535c52021-06-02 16:02:22 -0400614 for name, dep := range productVarToDepFields {
615 props, exists := productVariableProps[name]
616 excludeProps, excludesExists := productVariableProps[dep.excludesField]
617 // if neither an include or excludes property exists, then skip it
618 if !exists && !excludesExists {
619 continue
620 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000621 // Collect all the configurations that an include or exclude property exists for.
622 // We want to iterate all configurations rather than either the include or exclude because, for a
623 // particular configuration, we may have either only an include or an exclude to handle.
624 productConfigProps := make(map[android.ProductConfigProperty]bool, len(props)+len(excludeProps))
625 for p := range props {
626 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -0400627 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000628 for p := range excludeProps {
629 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -0400630 }
631
Jingwen Chen25825ca2021-11-15 12:28:43 +0000632 for productConfigProp := range productConfigProps {
633 prop, includesExists := props[productConfigProp]
634 excludesProp, excludesExists := excludeProps[productConfigProp]
Liz Kammer47535c52021-06-02 16:02:22 -0400635 var includes, excludes []string
636 var ok bool
637 // if there was no includes/excludes property, casting fails and that's expected
Jingwen Chen25825ca2021-11-15 12:28:43 +0000638 if includes, ok = prop.([]string); includesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -0400639 ctx.ModuleErrorf("Could not convert product variable %s property", name)
640 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000641 if excludes, ok = excludesProp.([]string); excludesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -0400642 ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
643 }
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400644
Jingwen Chen58ff6802021-11-17 12:14:41 +0000645 dep.attribute.EmitEmptyList = productConfigProp.AlwaysEmit()
Jingwen Chen25825ca2021-11-15 12:28:43 +0000646 dep.attribute.SetSelectValue(
647 productConfigProp.ConfigurationAxis(),
648 productConfigProp.SelectKey(),
649 dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes),
650 )
Liz Kammer47535c52021-06-02 16:02:22 -0400651 }
652 }
Liz Kammere6583482021-10-19 13:56:10 -0400653}
Liz Kammer47535c52021-06-02 16:02:22 -0400654
Liz Kammere6583482021-10-19 13:56:10 -0400655func (la *linkerAttributes) finalize() {
656 la.deps.ResolveExcludes()
657 la.implementationDeps.ResolveExcludes()
658 la.dynamicDeps.ResolveExcludes()
659 la.implementationDynamicDeps.ResolveExcludes()
660 la.wholeArchiveDeps.ResolveExcludes()
661 la.systemDynamicDeps.ForceSpecifyEmptyList = true
Jingwen Chen91220d72021-03-24 02:18:33 -0400662}
663
Jingwen Chened9c17d2021-04-13 07:14:55 +0000664// Relativize a list of root-relative paths with respect to the module's
665// directory.
666//
667// include_dirs Soong prop are root-relative (b/183742505), but
668// local_include_dirs, export_include_dirs and export_system_include_dirs are
669// module dir relative. This function makes a list of paths entirely module dir
670// relative.
671//
672// For the `include` attribute, Bazel wants the paths to be relative to the
673// module.
674func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000675 var relativePaths []string
676 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000677 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
678 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
679 if err != nil {
680 panic(err)
681 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000682 relativePaths = append(relativePaths, relativePath)
683 }
684 return relativePaths
685}
686
Liz Kammer5fad5012021-09-09 14:08:21 -0400687// BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel
688// attributes.
689type BazelIncludes struct {
690 Includes bazel.StringListAttribute
691 SystemIncludes bazel.StringListAttribute
692}
693
Jingwen Chen55bc8202021-11-02 06:40:51 +0000694func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module) BazelIncludes {
Jingwen Chen91220d72021-03-24 02:18:33 -0400695 libraryDecorator := module.linker.(*libraryDecorator)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400696 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
697}
Jingwen Chen91220d72021-03-24 02:18:33 -0400698
Liz Kammer5fad5012021-09-09 14:08:21 -0400699// Bp2buildParseExportedIncludesForPrebuiltLibrary returns a BazelIncludes with Bazel-ified values
700// to export includes from the underlying module's properties.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000701func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.BazelConversionPathContext, module *Module) BazelIncludes {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400702 prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker)
703 libraryDecorator := prebuiltLibraryLinker.libraryDecorator
704 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
705}
706
707// bp2BuildParseExportedIncludes creates a string list attribute contains the
708// exported included directories of a module.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000709func bp2BuildParseExportedIncludesHelper(ctx android.BazelConversionPathContext, module *Module, libraryDecorator *libraryDecorator) BazelIncludes {
Liz Kammer5fad5012021-09-09 14:08:21 -0400710 exported := BazelIncludes{}
Liz Kammer9abd62d2021-05-21 08:37:59 -0400711 for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) {
712 for config, props := range configToProps {
713 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
Liz Kammer5fad5012021-09-09 14:08:21 -0400714 if len(flagExporterProperties.Export_include_dirs) > 0 {
715 exported.Includes.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs)
716 }
717 if len(flagExporterProperties.Export_system_include_dirs) > 0 {
718 exported.SystemIncludes.SetSelectValue(axis, config, flagExporterProperties.Export_system_include_dirs)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400719 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400720 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400721 }
722 }
Liz Kammer5fad5012021-09-09 14:08:21 -0400723 exported.Includes.DeduplicateAxesFromBase()
724 exported.SystemIncludes.DeduplicateAxesFromBase()
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400725
Liz Kammer5fad5012021-09-09 14:08:21 -0400726 return exported
Jingwen Chen91220d72021-03-24 02:18:33 -0400727}
Chris Parsons953b3562021-09-20 15:14:39 -0400728
Jingwen Chen55bc8202021-11-02 06:40:51 +0000729func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400730 label := android.BazelModuleLabel(ctx, m)
731 if aModule, ok := m.(android.Module); ok {
732 if ctx.OtherModuleType(aModule) == "cc_library" && !android.GenerateCcLibraryStaticOnly(m.Name()) {
733 label += "_bp2build_cc_library_static"
734 }
735 }
736 return label
737}
738
Jingwen Chen55bc8202021-11-02 06:40:51 +0000739func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400740 // cc_library, at it's root name, propagates the shared library, which depends on the static
741 // library.
742 return android.BazelModuleLabel(ctx, m)
743}
744
Jingwen Chen55bc8202021-11-02 06:40:51 +0000745func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400746 label := bazelLabelForStaticModule(ctx, m)
747 if aModule, ok := m.(android.Module); ok {
748 if android.IsModulePrebuilt(aModule) {
749 label += "_alwayslink"
750 }
751 }
752 return label
753}
754
Jingwen Chen55bc8202021-11-02 06:40:51 +0000755func bazelLabelForWholeDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400756 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps)
757}
758
Jingwen Chen55bc8202021-11-02 06:40:51 +0000759func bazelLabelForWholeDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400760 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps)
761}
762
Jingwen Chen55bc8202021-11-02 06:40:51 +0000763func bazelLabelForStaticDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400764 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule)
765}
766
Jingwen Chen55bc8202021-11-02 06:40:51 +0000767func bazelLabelForStaticDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400768 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule)
769}
770
Jingwen Chen55bc8202021-11-02 06:40:51 +0000771func bazelLabelForSharedDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400772 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule)
773}
774
Jingwen Chen55bc8202021-11-02 06:40:51 +0000775func bazelLabelForHeaderDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400776 // This is not elegant, but bp2build's shared library targets only propagate
777 // their header information as part of the normal C++ provider.
778 return bazelLabelForSharedDeps(ctx, modules)
779}
780
Jingwen Chen55bc8202021-11-02 06:40:51 +0000781func bazelLabelForSharedDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400782 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
783}
Liz Kammer2b8004b2021-10-04 13:55:44 -0400784
785type binaryLinkerAttrs struct {
786 Linkshared *bool
787}
788
Jingwen Chen55bc8202021-11-02 06:40:51 +0000789func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400790 attrs := binaryLinkerAttrs{}
791 archVariantProps := m.GetArchVariantProperties(ctx, &BinaryLinkerProperties{})
792 for axis, configToProps := range archVariantProps {
793 for _, p := range configToProps {
794 props := p.(*BinaryLinkerProperties)
795 staticExecutable := props.Static_executable
796 if axis == bazel.NoConfigAxis {
797 if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared {
798 attrs.Linkshared = &linkBinaryShared
799 }
800 } else if staticExecutable != nil {
801 // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a
802 // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling
803 ctx.ModuleErrorf("bp2build cannot migrate a module with arch/target-specific static_executable values")
804 }
805 }
806 }
807
808 return attrs
809}