blob: 09aa1363c554ad2c2c8f51f91ccd5622e933fa9d [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
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500499 useVersionLib bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000500 linkopts bazel.StringListAttribute
Liz Kammerd2871182021-10-04 13:54:37 -0400501 additionalLinkerInputs bazel.LabelListAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000502 stripKeepSymbols bazel.BoolAttribute
503 stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
504 stripKeepSymbolsList bazel.StringListAttribute
505 stripAll bazel.BoolAttribute
506 stripNone bazel.BoolAttribute
Liz Kammer0eae52e2021-10-06 10:32:26 -0400507 features bazel.StringListAttribute
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400508}
509
Jingwen Chen55bc8202021-11-02 06:40:51 +0000510func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, isBinary bool, axis bazel.ConfigurationAxis, config string, props *BaseLinkerProperties) {
Liz Kammere6583482021-10-19 13:56:10 -0400511 // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module
512 var axisFeatures []string
Liz Kammer7a210ac2021-09-22 15:52:58 -0400513
Liz Kammere6583482021-10-19 13:56:10 -0400514 // Excludes to parallel Soong:
515 // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0
516 staticLibs := android.FirstUniqueStrings(props.Static_libs)
517 staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, staticLibs, props.Exclude_static_libs, props.Export_static_lib_headers, bazelLabelForStaticDepsExcludes)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400518
Liz Kammere6583482021-10-19 13:56:10 -0400519 headerLibs := android.FirstUniqueStrings(props.Header_libs)
520 hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps)
Jingwen Chen63930982021-03-24 10:04:33 -0400521
Liz Kammere6583482021-10-19 13:56:10 -0400522 (&hDeps.export).Append(staticDeps.export)
523 la.deps.SetSelectValue(axis, config, hDeps.export)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000524
Liz Kammere6583482021-10-19 13:56:10 -0400525 (&hDeps.implementation).Append(staticDeps.implementation)
526 la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation)
Liz Kammer0eae52e2021-10-06 10:32:26 -0400527
Liz Kammere6583482021-10-19 13:56:10 -0400528 wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs)
529 la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs))
530
531 systemSharedLibs := props.System_shared_libs
532 // systemSharedLibs distinguishes between nil/empty list behavior:
533 // nil -> use default values
534 // empty list -> no values specified
535 if len(systemSharedLibs) > 0 {
536 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
537 }
538 la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
539
540 sharedLibs := android.FirstUniqueStrings(props.Shared_libs)
541 sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, sharedLibs, props.Exclude_shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes)
542 la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export)
543 la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation)
544
545 if !BoolDefault(props.Pack_relocations, packRelocationsDefault) {
546 axisFeatures = append(axisFeatures, "disable_pack_relocations")
547 }
548
549 if Bool(props.Allow_undefined_symbols) {
550 axisFeatures = append(axisFeatures, "-no_undefined_symbols")
551 }
552
553 var linkerFlags []string
554 if len(props.Ldflags) > 0 {
555 linkerFlags = append(linkerFlags, props.Ldflags...)
556 // binaries remove static flag if -shared is in the linker flags
557 if isBinary && android.InList("-shared", linkerFlags) {
558 axisFeatures = append(axisFeatures, "-static_flag")
559 }
560 }
561 if props.Version_script != nil {
562 label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script)
563 la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}})
564 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label))
565 }
566 la.linkopts.SetSelectValue(axis, config, linkerFlags)
567 la.useLibcrt.SetSelectValue(axis, config, props.libCrt())
568
Rupert Shuttleworth484aa252021-12-10 07:22:53 -0500569 if axis == bazel.NoConfigAxis {
570 la.useVersionLib.SetSelectValue(axis, config, props.Use_version_lib)
571 }
572
Liz Kammere6583482021-10-19 13:56:10 -0400573 // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it.
574 if props.crt() != nil {
575 if axis == bazel.NoConfigAxis {
576 la.linkCrt.SetSelectValue(axis, config, props.crt())
577 } else if axis == bazel.ArchConfigurationAxis {
578 ctx.ModuleErrorf("nocrt is not supported for arch variants")
579 }
580 }
581
582 if axisFeatures != nil {
583 la.features.SetSelectValue(axis, config, axisFeatures)
584 }
585}
586
Jingwen Chen55bc8202021-11-02 06:40:51 +0000587func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) {
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000588 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StripProperties{}) {
589 for config, props := range configToProps {
590 if stripProperties, ok := props.(*StripProperties); ok {
Liz Kammere6583482021-10-19 13:56:10 -0400591 la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols)
592 la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list)
593 la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame)
594 la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All)
595 la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None)
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000596 }
597 }
598 }
Liz Kammere6583482021-10-19 13:56:10 -0400599}
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000600
Jingwen Chen55bc8202021-11-02 06:40:51 +0000601func (la *linkerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) {
Jingwen Chen6ada5892021-09-17 11:38:09 +0000602
Liz Kammer47535c52021-06-02 16:02:22 -0400603 type productVarDep struct {
604 // the name of the corresponding excludes field, if one exists
605 excludesField string
606 // reference to the bazel attribute that should be set for the given product variable config
607 attribute *bazel.LabelListAttribute
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400608
Jingwen Chen55bc8202021-11-02 06:40:51 +0000609 depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList
Liz Kammer47535c52021-06-02 16:02:22 -0400610 }
611
612 productVarToDepFields := map[string]productVarDep{
613 // product variables do not support exclude_shared_libs
Jingwen Chen55bc8202021-11-02 06:40:51 +0000614 "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes},
615 "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes},
616 "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes},
Liz Kammer47535c52021-06-02 16:02:22 -0400617 }
618
Liz Kammer47535c52021-06-02 16:02:22 -0400619 for name, dep := range productVarToDepFields {
620 props, exists := productVariableProps[name]
621 excludeProps, excludesExists := productVariableProps[dep.excludesField]
622 // if neither an include or excludes property exists, then skip it
623 if !exists && !excludesExists {
624 continue
625 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000626 // Collect all the configurations that an include or exclude property exists for.
627 // We want to iterate all configurations rather than either the include or exclude because, for a
628 // particular configuration, we may have either only an include or an exclude to handle.
629 productConfigProps := make(map[android.ProductConfigProperty]bool, len(props)+len(excludeProps))
630 for p := range props {
631 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -0400632 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000633 for p := range excludeProps {
634 productConfigProps[p] = true
Liz Kammer47535c52021-06-02 16:02:22 -0400635 }
636
Jingwen Chen25825ca2021-11-15 12:28:43 +0000637 for productConfigProp := range productConfigProps {
638 prop, includesExists := props[productConfigProp]
639 excludesProp, excludesExists := excludeProps[productConfigProp]
Liz Kammer47535c52021-06-02 16:02:22 -0400640 var includes, excludes []string
641 var ok bool
642 // if there was no includes/excludes property, casting fails and that's expected
Jingwen Chen25825ca2021-11-15 12:28:43 +0000643 if includes, ok = prop.([]string); includesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -0400644 ctx.ModuleErrorf("Could not convert product variable %s property", name)
645 }
Jingwen Chen25825ca2021-11-15 12:28:43 +0000646 if excludes, ok = excludesProp.([]string); excludesExists && !ok {
Liz Kammer47535c52021-06-02 16:02:22 -0400647 ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
648 }
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400649
Jingwen Chen58ff6802021-11-17 12:14:41 +0000650 dep.attribute.EmitEmptyList = productConfigProp.AlwaysEmit()
Jingwen Chen25825ca2021-11-15 12:28:43 +0000651 dep.attribute.SetSelectValue(
652 productConfigProp.ConfigurationAxis(),
653 productConfigProp.SelectKey(),
654 dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes),
655 )
Liz Kammer47535c52021-06-02 16:02:22 -0400656 }
657 }
Liz Kammere6583482021-10-19 13:56:10 -0400658}
Liz Kammer47535c52021-06-02 16:02:22 -0400659
Liz Kammere6583482021-10-19 13:56:10 -0400660func (la *linkerAttributes) finalize() {
661 la.deps.ResolveExcludes()
662 la.implementationDeps.ResolveExcludes()
663 la.dynamicDeps.ResolveExcludes()
664 la.implementationDynamicDeps.ResolveExcludes()
665 la.wholeArchiveDeps.ResolveExcludes()
666 la.systemDynamicDeps.ForceSpecifyEmptyList = true
Jingwen Chen91220d72021-03-24 02:18:33 -0400667}
668
Jingwen Chened9c17d2021-04-13 07:14:55 +0000669// Relativize a list of root-relative paths with respect to the module's
670// directory.
671//
672// include_dirs Soong prop are root-relative (b/183742505), but
673// local_include_dirs, export_include_dirs and export_system_include_dirs are
674// module dir relative. This function makes a list of paths entirely module dir
675// relative.
676//
677// For the `include` attribute, Bazel wants the paths to be relative to the
678// module.
679func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000680 var relativePaths []string
681 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000682 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
683 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
684 if err != nil {
685 panic(err)
686 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000687 relativePaths = append(relativePaths, relativePath)
688 }
689 return relativePaths
690}
691
Liz Kammer5fad5012021-09-09 14:08:21 -0400692// BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel
693// attributes.
694type BazelIncludes struct {
695 Includes bazel.StringListAttribute
696 SystemIncludes bazel.StringListAttribute
697}
698
Jingwen Chen55bc8202021-11-02 06:40:51 +0000699func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module) BazelIncludes {
Jingwen Chen91220d72021-03-24 02:18:33 -0400700 libraryDecorator := module.linker.(*libraryDecorator)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400701 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
702}
Jingwen Chen91220d72021-03-24 02:18:33 -0400703
Liz Kammer5fad5012021-09-09 14:08:21 -0400704// Bp2buildParseExportedIncludesForPrebuiltLibrary returns a BazelIncludes with Bazel-ified values
705// to export includes from the underlying module's properties.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000706func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.BazelConversionPathContext, module *Module) BazelIncludes {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400707 prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker)
708 libraryDecorator := prebuiltLibraryLinker.libraryDecorator
709 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
710}
711
712// bp2BuildParseExportedIncludes creates a string list attribute contains the
713// exported included directories of a module.
Jingwen Chen55bc8202021-11-02 06:40:51 +0000714func bp2BuildParseExportedIncludesHelper(ctx android.BazelConversionPathContext, module *Module, libraryDecorator *libraryDecorator) BazelIncludes {
Liz Kammer5fad5012021-09-09 14:08:21 -0400715 exported := BazelIncludes{}
Liz Kammer9abd62d2021-05-21 08:37:59 -0400716 for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) {
717 for config, props := range configToProps {
718 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
Liz Kammer5fad5012021-09-09 14:08:21 -0400719 if len(flagExporterProperties.Export_include_dirs) > 0 {
720 exported.Includes.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs)
721 }
722 if len(flagExporterProperties.Export_system_include_dirs) > 0 {
723 exported.SystemIncludes.SetSelectValue(axis, config, flagExporterProperties.Export_system_include_dirs)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400724 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400725 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400726 }
727 }
Liz Kammer5fad5012021-09-09 14:08:21 -0400728 exported.Includes.DeduplicateAxesFromBase()
729 exported.SystemIncludes.DeduplicateAxesFromBase()
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400730
Liz Kammer5fad5012021-09-09 14:08:21 -0400731 return exported
Jingwen Chen91220d72021-03-24 02:18:33 -0400732}
Chris Parsons953b3562021-09-20 15:14:39 -0400733
Jingwen Chen55bc8202021-11-02 06:40:51 +0000734func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400735 label := android.BazelModuleLabel(ctx, m)
736 if aModule, ok := m.(android.Module); ok {
737 if ctx.OtherModuleType(aModule) == "cc_library" && !android.GenerateCcLibraryStaticOnly(m.Name()) {
738 label += "_bp2build_cc_library_static"
739 }
740 }
741 return label
742}
743
Jingwen Chen55bc8202021-11-02 06:40:51 +0000744func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400745 // cc_library, at it's root name, propagates the shared library, which depends on the static
746 // library.
747 return android.BazelModuleLabel(ctx, m)
748}
749
Jingwen Chen55bc8202021-11-02 06:40:51 +0000750func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string {
Chris Parsons953b3562021-09-20 15:14:39 -0400751 label := bazelLabelForStaticModule(ctx, m)
752 if aModule, ok := m.(android.Module); ok {
753 if android.IsModulePrebuilt(aModule) {
754 label += "_alwayslink"
755 }
756 }
757 return label
758}
759
Jingwen Chen55bc8202021-11-02 06:40:51 +0000760func bazelLabelForWholeDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400761 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps)
762}
763
Jingwen Chen55bc8202021-11-02 06:40:51 +0000764func bazelLabelForWholeDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400765 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps)
766}
767
Jingwen Chen55bc8202021-11-02 06:40:51 +0000768func bazelLabelForStaticDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400769 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule)
770}
771
Jingwen Chen55bc8202021-11-02 06:40:51 +0000772func bazelLabelForStaticDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400773 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule)
774}
775
Jingwen Chen55bc8202021-11-02 06:40:51 +0000776func bazelLabelForSharedDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400777 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule)
778}
779
Jingwen Chen55bc8202021-11-02 06:40:51 +0000780func bazelLabelForHeaderDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400781 // This is not elegant, but bp2build's shared library targets only propagate
782 // their header information as part of the normal C++ provider.
783 return bazelLabelForSharedDeps(ctx, modules)
784}
785
Jingwen Chen55bc8202021-11-02 06:40:51 +0000786func bazelLabelForSharedDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList {
Chris Parsons953b3562021-09-20 15:14:39 -0400787 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
788}
Liz Kammer2b8004b2021-10-04 13:55:44 -0400789
790type binaryLinkerAttrs struct {
791 Linkshared *bool
792}
793
Jingwen Chen55bc8202021-11-02 06:40:51 +0000794func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs {
Liz Kammer2b8004b2021-10-04 13:55:44 -0400795 attrs := binaryLinkerAttrs{}
796 archVariantProps := m.GetArchVariantProperties(ctx, &BinaryLinkerProperties{})
797 for axis, configToProps := range archVariantProps {
798 for _, p := range configToProps {
799 props := p.(*BinaryLinkerProperties)
800 staticExecutable := props.Static_executable
801 if axis == bazel.NoConfigAxis {
802 if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared {
803 attrs.Linkshared = &linkBinaryShared
804 }
805 } else if staticExecutable != nil {
806 // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a
807 // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling
808 ctx.ModuleErrorf("bp2build cannot migrate a module with arch/target-specific static_executable values")
809 }
810 }
811 }
812
813 return attrs
814}