blob: 3c238b5d26d49a8660597e245b25a6dab535f4ed [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 (
30 cSrcPartition = "c"
31 asSrcPartition = "as"
32 cppSrcPartition = "cpp"
33)
34
Liz Kammer2222c6b2021-05-24 15:41:47 -040035// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
Jingwen Chenbcf53042021-05-26 04:42:42 +000036// properties which apply to either the shared or static version of a cc_library module.
Liz Kammer2222c6b2021-05-24 15:41:47 -040037type staticOrSharedAttributes struct {
Jingwen Chenc4dc9b42021-06-11 12:51:48 +000038 Srcs bazel.LabelListAttribute
39 Srcs_c bazel.LabelListAttribute
40 Srcs_as bazel.LabelListAttribute
41 Copts bazel.StringListAttribute
Jingwen Chen14a8bda2021-06-02 11:10:02 +000042
Liz Kammer7a210ac2021-09-22 15:52:58 -040043 Deps bazel.LabelListAttribute
44 Implementation_deps bazel.LabelListAttribute
45 Dynamic_deps bazel.LabelListAttribute
46 Implementation_dynamic_deps bazel.LabelListAttribute
47 Whole_archive_deps bazel.LabelListAttribute
Chris Parsons51f8c392021-08-03 21:01:05 -040048
49 System_dynamic_deps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +000050}
51
Liz Kammerae3994e2021-10-19 09:45:48 -040052func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute {
Jingwen Chen14a8bda2021-06-02 11:10:02 +000053 // Check that a module is a filegroup type named <label>.
54 isFilegroupNamed := func(m android.Module, fullLabel string) bool {
55 if ctx.OtherModuleType(m) != "filegroup" {
56 return false
57 }
58 labelParts := strings.Split(fullLabel, ":")
59 if len(labelParts) > 2 {
60 // There should not be more than one colon in a label.
Liz Kammer57e2e7a2021-09-20 12:55:02 -040061 ctx.ModuleErrorf("%s is not a valid Bazel label for a filegroup", fullLabel)
Jingwen Chen14a8bda2021-06-02 11:10:02 +000062 }
Liz Kammer57e2e7a2021-09-20 12:55:02 -040063 return m.Name() == labelParts[len(labelParts)-1]
Jingwen Chen14a8bda2021-06-02 11:10:02 +000064 }
65
Liz Kammer57e2e7a2021-09-20 12:55:02 -040066 // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl
67 // macro.
68 addSuffixForFilegroup := func(suffix string) bazel.LabelMapper {
69 return func(ctx bazel.OtherModuleContext, label string) (string, bool) {
70 m, exists := ctx.ModuleFromName(label)
71 if !exists {
72 return label, false
Jingwen Chen14a8bda2021-06-02 11:10:02 +000073 }
Chris Parsons5a34ffb2021-07-21 14:34:58 -040074 aModule, _ := m.(android.Module)
Liz Kammer57e2e7a2021-09-20 12:55:02 -040075 if !isFilegroupNamed(aModule, label) {
76 return label, false
Jingwen Chen14a8bda2021-06-02 11:10:02 +000077 }
Liz Kammer57e2e7a2021-09-20 12:55:02 -040078 return label + suffix, true
Chris Parsons5a34ffb2021-07-21 14:34:58 -040079 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +000080 }
81
Liz Kammer57e2e7a2021-09-20 12:55:02 -040082 // TODO(b/190006308): Handle language detection of sources in a Bazel rule.
83 partitioned := bazel.PartitionLabelListAttribute(ctx, &srcs, bazel.LabelPartitions{
Liz Kammerae3994e2021-10-19 09:45:48 -040084 cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")},
85 asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")},
Liz Kammer57e2e7a2021-09-20 12:55:02 -040086 // C++ is the "catch-all" group, and comprises generated sources because we don't
87 // know the language of these sources until the genrule is executed.
Liz Kammerae3994e2021-10-19 09:45:48 -040088 cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true},
Liz Kammer57e2e7a2021-09-20 12:55:02 -040089 })
Jingwen Chen14a8bda2021-06-02 11:10:02 +000090
Liz Kammerae3994e2021-10-19 09:45:48 -040091 return partitioned
Jingwen Chen14a8bda2021-06-02 11:10:02 +000092}
93
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +000094// bp2BuildParseLibProps returns the attributes for a variant of a cc_library.
95func bp2BuildParseLibProps(ctx android.TopDownMutatorContext, module *Module, isStatic bool) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +000096 lib, ok := module.compiler.(*libraryDecorator)
97 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -040098 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +000099 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000100 return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic)
101}
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// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
104func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
105 return bp2BuildParseLibProps(ctx, module, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000106}
107
108// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400109func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000110 return bp2BuildParseLibProps(ctx, module, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400111}
112
Liz Kammer7a210ac2021-09-22 15:52:58 -0400113type depsPartition struct {
114 export bazel.LabelList
115 implementation bazel.LabelList
116}
117
118type bazelLabelForDepsFn func(android.TopDownMutatorContext, []string) bazel.LabelList
119
Liz Kammer2b8004b2021-10-04 13:55:44 -0400120func maybePartitionExportedAndImplementationsDeps(ctx android.TopDownMutatorContext, exportsDeps bool, allDeps, exportedDeps []string, fn bazelLabelForDepsFn) depsPartition {
121 if !exportsDeps {
122 return depsPartition{
123 implementation: fn(ctx, allDeps),
124 }
125 }
126
Liz Kammer7a210ac2021-09-22 15:52:58 -0400127 implementation, export := android.FilterList(allDeps, exportedDeps)
128
129 return depsPartition{
130 export: fn(ctx, export),
131 implementation: fn(ctx, implementation),
132 }
133}
134
135type bazelLabelForDepsExcludesFn func(android.TopDownMutatorContext, []string, []string) bazel.LabelList
136
Liz Kammer2b8004b2021-10-04 13:55:44 -0400137func maybePartitionExportedAndImplementationsDepsExcludes(ctx android.TopDownMutatorContext, exportsDeps bool, allDeps, excludes, exportedDeps []string, fn bazelLabelForDepsExcludesFn) depsPartition {
138 if !exportsDeps {
139 return depsPartition{
140 implementation: fn(ctx, allDeps, excludes),
141 }
142 }
Liz Kammer7a210ac2021-09-22 15:52:58 -0400143 implementation, export := android.FilterList(allDeps, exportedDeps)
144
145 return depsPartition{
146 export: fn(ctx, export, excludes),
147 implementation: fn(ctx, implementation, excludes),
148 }
149}
150
Jingwen Chenbcf53042021-05-26 04:42:42 +0000151func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes {
Liz Kammer135bf552021-08-11 10:46:06 -0400152 attrs := staticOrSharedAttributes{}
Jingwen Chenbcf53042021-05-26 04:42:42 +0000153
Liz Kammer9abd62d2021-05-21 08:37:59 -0400154 setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000155 attrs.Copts.SetSelectValue(axis, config, props.Cflags)
156 attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
Chris Parsons953b3562021-09-20 15:14:39 -0400157 attrs.System_dynamic_deps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, props.System_shared_libs))
Liz Kammer7a210ac2021-09-22 15:52:58 -0400158
Liz Kammer2b8004b2021-10-04 13:55:44 -0400159 staticDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Static_libs, props.Export_static_lib_headers, bazelLabelForStaticDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400160 attrs.Deps.SetSelectValue(axis, config, staticDeps.export)
161 attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation)
162
Liz Kammer2b8004b2021-10-04 13:55:44 -0400163 sharedDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400164 attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export)
165 attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation)
166
167 attrs.Whole_archive_deps.SetSelectValue(axis, config, bazelLabelForWholeDeps(ctx, props.Whole_static_libs))
Jingwen Chenbcf53042021-05-26 04:42:42 +0000168 }
Liz Kammer135bf552021-08-11 10:46:06 -0400169 // system_dynamic_deps distinguishes between nil/empty list behavior:
170 // nil -> use default values
171 // empty list -> no values specified
172 attrs.System_dynamic_deps.ForceSpecifyEmptyList = true
Jingwen Chenbcf53042021-05-26 04:42:42 +0000173
174 if isStatic {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400175 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) {
176 for config, props := range configToProps {
177 if staticOrSharedProps, ok := props.(*StaticProperties); ok {
178 setAttrs(axis, config, staticOrSharedProps.Static)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000179 }
180 }
181 }
182 } else {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400183 for axis, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) {
184 for config, props := range configToProps {
185 if staticOrSharedProps, ok := props.(*SharedProperties); ok {
186 setAttrs(axis, config, staticOrSharedProps.Shared)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000187 }
188 }
189 }
190 }
191
Liz Kammerae3994e2021-10-19 09:45:48 -0400192 partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs)
193 attrs.Srcs = partitionedSrcs[cppSrcPartition]
194 attrs.Srcs_c = partitionedSrcs[cSrcPartition]
195 attrs.Srcs_as = partitionedSrcs[asSrcPartition]
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000196
Jingwen Chenbcf53042021-05-26 04:42:42 +0000197 return attrs
Jingwen Chen53681ef2021-04-29 08:15:13 +0000198}
199
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400200// Convenience struct to hold all attributes parsed from prebuilt properties.
201type prebuiltAttributes struct {
202 Src bazel.LabelAttribute
203}
204
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000205// NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400206func Bp2BuildParsePrebuiltLibraryProps(ctx android.TopDownMutatorContext, module *Module) prebuiltAttributes {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400207 var srcLabelAttribute bazel.LabelAttribute
208
Liz Kammer9abd62d2021-05-21 08:37:59 -0400209 for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltLinkerProperties{}) {
210 for config, props := range configToProps {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400211 if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok {
212 if len(prebuiltLinkerProperties.Srcs) > 1 {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400213 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for %s %s\n", axis, config)
214 continue
215 } else if len(prebuiltLinkerProperties.Srcs) == 0 {
216 continue
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400217 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400218 src := android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0])
219 srcLabelAttribute.SetSelectValue(axis, config, src)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400220 }
221 }
222 }
223
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400224 return prebuiltAttributes{
225 Src: srcLabelAttribute,
226 }
227}
228
Jingwen Chen107c0de2021-04-09 10:43:12 +0000229// Convenience struct to hold all attributes parsed from compiler properties.
230type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400231 // Options for all languages
232 copts bazel.StringListAttribute
233 // Assembly options and sources
234 asFlags bazel.StringListAttribute
235 asSrcs bazel.LabelListAttribute
236 // C options and sources
237 conlyFlags bazel.StringListAttribute
238 cSrcs bazel.LabelListAttribute
239 // C++ options and sources
240 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000241 srcs bazel.LabelListAttribute
Chris Parsons2c788392021-08-10 11:58:07 -0400242
243 rtti bazel.BoolAttribute
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000244
245 // Not affected by arch variants
246 stl *string
247 cppStd *string
Liz Kammer35687bc2021-09-10 10:07:07 -0400248
249 localIncludes bazel.StringListAttribute
250 absoluteIncludes bazel.StringListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000251}
252
Jingwen Chen63930982021-03-24 10:04:33 -0400253// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000254func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
Jingwen Chen882bcc12021-04-27 05:54:20 +0000255 var srcs bazel.LabelListAttribute
Liz Kammerae3994e2021-10-19 09:45:48 -0400256 var implementationHdrs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000257 var copts bazel.StringListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400258 var asFlags bazel.StringListAttribute
259 var conlyFlags bazel.StringListAttribute
260 var cppFlags bazel.StringListAttribute
Chris Parsons2c788392021-08-10 11:58:07 -0400261 var rtti bazel.BoolAttribute
Liz Kammer35687bc2021-09-10 10:07:07 -0400262 var localIncludes bazel.StringListAttribute
263 var absoluteIncludes bazel.StringListAttribute
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000264 var stl *string = nil
265 var cppStd *string = nil
Jingwen Chened9c17d2021-04-13 07:14:55 +0000266
Chris Parsons990c4f42021-05-25 12:10:58 -0400267 parseCommandLineFlags := func(soongFlags []string) []string {
268 var result []string
269 for _, flag := range soongFlags {
Colin Cross52aa4e12021-05-25 15:20:39 +0000270 // Soong's cflags can contain spaces, like `-include header.h`. For
271 // Bazel's copts, split them up to be compatible with the
272 // no_copts_tokenization feature.
Chris Parsons990c4f42021-05-25 12:10:58 -0400273 result = append(result, strings.Split(flag, " ")...)
Colin Cross52aa4e12021-05-25 15:20:39 +0000274 }
Chris Parsons990c4f42021-05-25 12:10:58 -0400275 return result
276 }
277
Liz Kammer74deed42021-06-02 13:02:03 -0400278 // Parse srcs from an arch or OS's props value.
Liz Kammer222bdcf2021-10-11 14:15:51 -0400279 parseSrcs := func(props *BaseCompilerProperties) (bazel.LabelList, bool) {
280 anySrcs := false
Chris Parsons484e50a2021-05-13 15:13:04 -0400281 // Add srcs-like dependencies such as generated files.
282 // First create a LabelList containing these dependencies, then merge the values with srcs.
Liz Kammerae3994e2021-10-19 09:45:48 -0400283 generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources)
284 if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 {
Liz Kammer222bdcf2021-10-11 14:15:51 -0400285 anySrcs = true
286 }
Chris Parsons484e50a2021-05-13 15:13:04 -0400287
Liz Kammer222bdcf2021-10-11 14:15:51 -0400288 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs)
289 if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 {
290 anySrcs = true
291 }
Liz Kammerae3994e2021-10-19 09:45:48 -0400292 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs
Jingwen Chene32e9e02021-04-23 09:17:24 +0000293 }
294
Liz Kammer9abd62d2021-05-21 08:37:59 -0400295 archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
Liz Kammer9abd62d2021-05-21 08:37:59 -0400296 for axis, configToProps := range archVariantCompilerProps {
297 for config, props := range configToProps {
298 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
299 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
300 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
Liz Kammer222bdcf2021-10-11 14:15:51 -0400301 if srcsList, ok := parseSrcs(baseCompilerProps); ok {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400302 srcs.SetSelectValue(axis, config, srcsList)
Liz Kammer9abd62d2021-05-21 08:37:59 -0400303 }
Liz Kammerae3994e2021-10-19 09:45:48 -0400304 if len(baseCompilerProps.Generated_headers) > 0 {
305 implementationHdrs.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, baseCompilerProps.Generated_headers))
306 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400307
Jingwen Chen97b85312021-10-08 10:41:31 +0000308 if axis == bazel.NoConfigAxis {
309 // If cpp_std is not specified, don't generate it in the
310 // BUILD file. For readability purposes, cpp_std and gnu_extensions are
311 // combined into a single -std=<version> copt, except in the
312 // default case where cpp_std is nil and gnu_extensions is true or unspecified,
313 // then the toolchain's default "gnu++17" will be used.
314 if baseCompilerProps.Cpp_std != nil {
315 // TODO(b/202491296): Handle C_std.
316 // These transformations are shared with compiler.go.
317 cppStdVal := parseCppStd(baseCompilerProps.Cpp_std)
318 _, cppStdVal = maybeReplaceGnuToC(baseCompilerProps.Gnu_extensions, "", cppStdVal)
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000319 cppStd = &cppStdVal
Jingwen Chen97b85312021-10-08 10:41:31 +0000320 } else if baseCompilerProps.Gnu_extensions != nil && !*baseCompilerProps.Gnu_extensions {
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000321 cppStdVal := "c++17"
322 cppStd = &cppStdVal
Jingwen Chen97b85312021-10-08 10:41:31 +0000323 }
324 }
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000325
326 var archVariantCopts []string
Jingwen Chen97b85312021-10-08 10:41:31 +0000327 archVariantCopts = append(archVariantCopts, parseCommandLineFlags(baseCompilerProps.Cflags)...)
Chris Parsons69fa9f92021-07-13 11:47:44 -0400328 archVariantAsflags := parseCommandLineFlags(baseCompilerProps.Asflags)
Liz Kammer35687bc2021-09-10 10:07:07 -0400329
330 localIncludeDirs := baseCompilerProps.Local_include_dirs
331 if axis == bazel.NoConfigAxis && includeBuildDirectory(baseCompilerProps.Include_build_directory) {
332 localIncludeDirs = append(localIncludeDirs, ".")
Chris Parsons69fa9f92021-07-13 11:47:44 -0400333 }
334
Liz Kammer35687bc2021-09-10 10:07:07 -0400335 absoluteIncludes.SetSelectValue(axis, config, baseCompilerProps.Include_dirs)
336 localIncludes.SetSelectValue(axis, config, localIncludeDirs)
Liz Kammer135bf552021-08-11 10:46:06 -0400337
Chris Parsons69fa9f92021-07-13 11:47:44 -0400338 copts.SetSelectValue(axis, config, archVariantCopts)
339 asFlags.SetSelectValue(axis, config, archVariantAsflags)
Liz Kammer9abd62d2021-05-21 08:37:59 -0400340 conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Conlyflags))
341 cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Cppflags))
Chris Parsons2c788392021-08-10 11:58:07 -0400342 rtti.SetSelectValue(axis, config, baseCompilerProps.Rtti)
Liz Kammer9abd62d2021-05-21 08:37:59 -0400343 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000344 }
345 }
346
Liz Kammer74deed42021-06-02 13:02:03 -0400347 srcs.ResolveExcludes()
Liz Kammerae3994e2021-10-19 09:45:48 -0400348 partitionedSrcs := groupSrcsByExtension(ctx, srcs)
349
350 for p, lla := range partitionedSrcs {
351 // if there are no sources, there is no need for headers
352 if lla.IsEmpty() {
353 continue
354 }
355 lla.Append(implementationHdrs)
356 partitionedSrcs[p] = lla
357 }
358
359 srcs = partitionedSrcs[cppSrcPartition]
360 cSrcs := partitionedSrcs[cSrcPartition]
361 asSrcs := partitionedSrcs[asSrcPartition]
362
Liz Kammer35687bc2021-09-10 10:07:07 -0400363 absoluteIncludes.DeduplicateAxesFromBase()
364 localIncludes.DeduplicateAxesFromBase()
Jingwen Chenc1c26502021-04-05 10:35:13 +0000365
Liz Kammerba7a9c52021-05-26 08:45:30 -0400366 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
367 "Cflags": &copts,
368 "Asflags": &asFlags,
369 "CppFlags": &cppFlags,
370 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400371 productVariableProps := android.ProductVariableProperties(ctx)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400372 for propName, attr := range productVarPropNameToAttribute {
373 if props, exists := productVariableProps[propName]; exists {
374 for _, prop := range props {
375 flags, ok := prop.Property.([]string)
376 if !ok {
377 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
378 }
379 newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable)
Liz Kammer47535c52021-06-02 16:02:22 -0400380 attr.SetSelectValue(bazel.ProductVariableConfigurationAxis(prop.FullConfig), prop.FullConfig, newFlags)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400381 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400382 }
383 }
384
Chris Parsonsa967f252021-09-23 16:34:35 -0400385 stlPropsByArch := module.GetArchVariantProperties(ctx, &StlProperties{})
386 for _, configToProps := range stlPropsByArch {
387 for _, props := range configToProps {
388 if stlProps, ok := props.(*StlProperties); ok {
389 if stlProps.Stl != nil {
390 if stl == nil {
391 stl = stlProps.Stl
392 } else {
393 if stl != stlProps.Stl {
394 ctx.ModuleErrorf("Unsupported conversion: module with different stl for different variants: %s and %s", *stl, stlProps.Stl)
395 }
396 }
397 }
398 }
399 }
400 }
401
Jingwen Chen107c0de2021-04-09 10:43:12 +0000402 return compilerAttributes{
Liz Kammer35687bc2021-09-10 10:07:07 -0400403 copts: copts,
404 srcs: srcs,
405 asFlags: asFlags,
406 asSrcs: asSrcs,
407 cSrcs: cSrcs,
408 conlyFlags: conlyFlags,
409 cppFlags: cppFlags,
410 rtti: rtti,
Chris Parsonsa967f252021-09-23 16:34:35 -0400411 stl: stl,
Jingwen Chen5b11ab12021-10-11 17:44:33 +0000412 cppStd: cppStd,
Liz Kammer35687bc2021-09-10 10:07:07 -0400413 localIncludes: localIncludes,
414 absoluteIncludes: absoluteIncludes,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000415 }
416}
417
418// Convenience struct to hold all attributes parsed from linker properties.
419type linkerAttributes struct {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400420 deps bazel.LabelListAttribute
421 implementationDeps bazel.LabelListAttribute
422 dynamicDeps bazel.LabelListAttribute
423 implementationDynamicDeps bazel.LabelListAttribute
424 wholeArchiveDeps bazel.LabelListAttribute
425 systemDynamicDeps bazel.LabelListAttribute
426
Jingwen Chen6ada5892021-09-17 11:38:09 +0000427 linkCrt bazel.BoolAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000428 useLibcrt bazel.BoolAttribute
429 linkopts bazel.StringListAttribute
Liz Kammerd2871182021-10-04 13:54:37 -0400430 additionalLinkerInputs bazel.LabelListAttribute
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000431 stripKeepSymbols bazel.BoolAttribute
432 stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
433 stripKeepSymbolsList bazel.StringListAttribute
434 stripAll bazel.BoolAttribute
435 stripNone bazel.BoolAttribute
Liz Kammer0eae52e2021-10-06 10:32:26 -0400436 features bazel.StringListAttribute
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400437}
438
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200439// bp2BuildParseLinkerProps parses the linker properties of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400440// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000441func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400442
Liz Kammer47535c52021-06-02 16:02:22 -0400443 var headerDeps bazel.LabelListAttribute
Liz Kammer7a210ac2021-09-22 15:52:58 -0400444 var implementationHeaderDeps bazel.LabelListAttribute
445 var deps bazel.LabelListAttribute
446 var implementationDeps bazel.LabelListAttribute
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400447 var dynamicDeps bazel.LabelListAttribute
Liz Kammer7a210ac2021-09-22 15:52:58 -0400448 var implementationDynamicDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400449 var wholeArchiveDeps bazel.LabelListAttribute
Liz Kammer135bf552021-08-11 10:46:06 -0400450 systemSharedDeps := bazel.LabelListAttribute{ForceSpecifyEmptyList: true}
Liz Kammer7a210ac2021-09-22 15:52:58 -0400451
Jingwen Chen63930982021-03-24 10:04:33 -0400452 var linkopts bazel.StringListAttribute
Jingwen Chen6ada5892021-09-17 11:38:09 +0000453 var linkCrt bazel.BoolAttribute
Liz Kammerd2871182021-10-04 13:54:37 -0400454 var additionalLinkerInputs bazel.LabelListAttribute
Liz Kammerd366c902021-06-03 13:43:01 -0400455 var useLibcrt bazel.BoolAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400456
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000457 var stripKeepSymbols bazel.BoolAttribute
458 var stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
459 var stripKeepSymbolsList bazel.StringListAttribute
460 var stripAll bazel.BoolAttribute
461 var stripNone bazel.BoolAttribute
462
Liz Kammer0eae52e2021-10-06 10:32:26 -0400463 var features bazel.StringListAttribute
464
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000465 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StripProperties{}) {
466 for config, props := range configToProps {
467 if stripProperties, ok := props.(*StripProperties); ok {
468 stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols)
469 stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list)
470 stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame)
471 stripAll.SetSelectValue(axis, config, stripProperties.Strip.All)
472 stripNone.SetSelectValue(axis, config, stripProperties.Strip.None)
473 }
474 }
475 }
476
Jingwen Chen6ada5892021-09-17 11:38:09 +0000477 // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module
478 var disallowedArchVariantCrt bool
Liz Kammer2b8004b2021-10-04 13:55:44 -0400479 isBinary := module.Binary()
Jingwen Chen6ada5892021-09-17 11:38:09 +0000480
Liz Kammer9abd62d2021-05-21 08:37:59 -0400481 for axis, configToProps := range module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) {
482 for config, props := range configToProps {
483 if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
Liz Kammer0eae52e2021-10-06 10:32:26 -0400484 var axisFeatures []string
Liz Kammer7a210ac2021-09-22 15:52:58 -0400485
Liz Kammer135bf552021-08-11 10:46:06 -0400486 // Excludes to parallel Soong:
487 // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0
Liz Kammer47535c52021-06-02 16:02:22 -0400488 staticLibs := android.FirstUniqueStrings(baseLinkerProps.Static_libs)
Liz Kammer2b8004b2021-10-04 13:55:44 -0400489 staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, staticLibs, baseLinkerProps.Exclude_static_libs, baseLinkerProps.Export_static_lib_headers, bazelLabelForStaticDepsExcludes)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400490 deps.SetSelectValue(axis, config, staticDeps.export)
491 implementationDeps.SetSelectValue(axis, config, staticDeps.implementation)
492
493 wholeStaticLibs := android.FirstUniqueStrings(baseLinkerProps.Whole_static_libs)
494 wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, baseLinkerProps.Exclude_static_libs))
Chris Parsons51f8c392021-08-03 21:01:05 -0400495
Liz Kammer135bf552021-08-11 10:46:06 -0400496 systemSharedLibs := baseLinkerProps.System_shared_libs
497 // systemSharedLibs distinguishes between nil/empty list behavior:
498 // nil -> use default values
499 // empty list -> no values specified
500 if len(systemSharedLibs) > 0 {
501 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
Chris Parsons51f8c392021-08-03 21:01:05 -0400502 }
Chris Parsons953b3562021-09-20 15:14:39 -0400503 systemSharedDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
Chris Parsons51f8c392021-08-03 21:01:05 -0400504
505 sharedLibs := android.FirstUniqueStrings(baseLinkerProps.Shared_libs)
Liz Kammer2b8004b2021-10-04 13:55:44 -0400506 sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, sharedLibs, baseLinkerProps.Exclude_shared_libs, baseLinkerProps.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400507 dynamicDeps.SetSelectValue(axis, config, sharedDeps.export)
508 implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400509
Liz Kammer47535c52021-06-02 16:02:22 -0400510 headerLibs := android.FirstUniqueStrings(baseLinkerProps.Header_libs)
Liz Kammer2b8004b2021-10-04 13:55:44 -0400511 hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, baseLinkerProps.Export_header_lib_headers, bazelLabelForHeaderDeps)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400512
513 headerDeps.SetSelectValue(axis, config, hDeps.export)
514 implementationHeaderDeps.SetSelectValue(axis, config, hDeps.implementation)
Liz Kammer47535c52021-06-02 16:02:22 -0400515
Liz Kammer0eae52e2021-10-06 10:32:26 -0400516 if !BoolDefault(baseLinkerProps.Pack_relocations, packRelocationsDefault) {
517 axisFeatures = append(axisFeatures, "disable_pack_relocations")
518 }
519
520 if Bool(baseLinkerProps.Allow_undefined_symbols) {
521 axisFeatures = append(axisFeatures, "-no_undefined_symbols")
522 }
523
Liz Kammerd2871182021-10-04 13:54:37 -0400524 var linkerFlags []string
525 if len(baseLinkerProps.Ldflags) > 0 {
526 linkerFlags = append(linkerFlags, baseLinkerProps.Ldflags...)
Liz Kammer2b8004b2021-10-04 13:55:44 -0400527 // binaries remove static flag if -shared is in the linker flags
528 if module.Binary() && android.InList("-shared", linkerFlags) {
529 axisFeatures = append(axisFeatures, "-static_flag")
530 }
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400531 }
Liz Kammerd2871182021-10-04 13:54:37 -0400532 if baseLinkerProps.Version_script != nil {
533 label := android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)
534 additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}})
535 linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label))
536 }
537 linkopts.SetSelectValue(axis, config, linkerFlags)
Liz Kammerd366c902021-06-03 13:43:01 -0400538 useLibcrt.SetSelectValue(axis, config, baseLinkerProps.libCrt())
Jingwen Chen6ada5892021-09-17 11:38:09 +0000539
540 // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it.
541 if baseLinkerProps.crt() != nil {
542 if axis == bazel.NoConfigAxis {
543 linkCrt.SetSelectValue(axis, config, baseLinkerProps.crt())
544 } else if axis == bazel.ArchConfigurationAxis {
545 disallowedArchVariantCrt = true
546 }
547 }
Liz Kammer0eae52e2021-10-06 10:32:26 -0400548
549 if axisFeatures != nil {
550 features.SetSelectValue(axis, config, axisFeatures)
551 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400552 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400553 }
554 }
555
Jingwen Chen6ada5892021-09-17 11:38:09 +0000556 if disallowedArchVariantCrt {
557 ctx.ModuleErrorf("nocrt is not supported for arch variants")
558 }
559
Liz Kammer47535c52021-06-02 16:02:22 -0400560 type productVarDep struct {
561 // the name of the corresponding excludes field, if one exists
562 excludesField string
563 // reference to the bazel attribute that should be set for the given product variable config
564 attribute *bazel.LabelListAttribute
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400565
Chris Parsons953b3562021-09-20 15:14:39 -0400566 depResolutionFunc func(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList
Liz Kammer47535c52021-06-02 16:02:22 -0400567 }
568
569 productVarToDepFields := map[string]productVarDep{
570 // product variables do not support exclude_shared_libs
Liz Kammer7a210ac2021-09-22 15:52:58 -0400571 "Shared_libs": productVarDep{attribute: &implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes},
572 "Static_libs": productVarDep{"Exclude_static_libs", &implementationDeps, bazelLabelForStaticDepsExcludes},
Chris Parsons953b3562021-09-20 15:14:39 -0400573 "Whole_static_libs": productVarDep{"Exclude_static_libs", &wholeArchiveDeps, bazelLabelForWholeDepsExcludes},
Liz Kammer47535c52021-06-02 16:02:22 -0400574 }
575
576 productVariableProps := android.ProductVariableProperties(ctx)
577 for name, dep := range productVarToDepFields {
578 props, exists := productVariableProps[name]
579 excludeProps, excludesExists := productVariableProps[dep.excludesField]
580 // if neither an include or excludes property exists, then skip it
581 if !exists && !excludesExists {
582 continue
583 }
584 // collect all the configurations that an include or exclude property exists for.
585 // we want to iterate all configurations rather than either the include or exclude because for a
586 // particular configuration we may have only and include or only an exclude to handle
587 configs := make(map[string]bool, len(props)+len(excludeProps))
588 for config := range props {
589 configs[config] = true
590 }
591 for config := range excludeProps {
592 configs[config] = true
593 }
594
595 for config := range configs {
596 prop, includesExists := props[config]
597 excludesProp, excludesExists := excludeProps[config]
598 var includes, excludes []string
599 var ok bool
600 // if there was no includes/excludes property, casting fails and that's expected
601 if includes, ok = prop.Property.([]string); includesExists && !ok {
602 ctx.ModuleErrorf("Could not convert product variable %s property", name)
603 }
604 if excludes, ok = excludesProp.Property.([]string); excludesExists && !ok {
605 ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
606 }
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400607
608 dep.attribute.SetSelectValue(bazel.ProductVariableConfigurationAxis(config), config, dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes))
Liz Kammer47535c52021-06-02 16:02:22 -0400609 }
610 }
611
Liz Kammer7a210ac2021-09-22 15:52:58 -0400612 headerDeps.Append(deps)
613 implementationHeaderDeps.Append(implementationDeps)
614
615 headerDeps.ResolveExcludes()
616 implementationHeaderDeps.ResolveExcludes()
Liz Kammer47535c52021-06-02 16:02:22 -0400617 dynamicDeps.ResolveExcludes()
Liz Kammer7a210ac2021-09-22 15:52:58 -0400618 implementationDynamicDeps.ResolveExcludes()
Liz Kammer47535c52021-06-02 16:02:22 -0400619 wholeArchiveDeps.ResolveExcludes()
620
Jingwen Chen107c0de2021-04-09 10:43:12 +0000621 return linkerAttributes{
Liz Kammer7a210ac2021-09-22 15:52:58 -0400622 deps: headerDeps,
623 implementationDeps: implementationHeaderDeps,
624 dynamicDeps: dynamicDeps,
625 implementationDynamicDeps: implementationDynamicDeps,
626 wholeArchiveDeps: wholeArchiveDeps,
627 systemDynamicDeps: systemSharedDeps,
628
Liz Kammerd2871182021-10-04 13:54:37 -0400629 linkCrt: linkCrt,
630 linkopts: linkopts,
631 useLibcrt: useLibcrt,
632 additionalLinkerInputs: additionalLinkerInputs,
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000633
634 // Strip properties
635 stripKeepSymbols: stripKeepSymbols,
636 stripKeepSymbolsAndDebugFrame: stripKeepSymbolsAndDebugFrame,
637 stripKeepSymbolsList: stripKeepSymbolsList,
638 stripAll: stripAll,
639 stripNone: stripNone,
Liz Kammer0eae52e2021-10-06 10:32:26 -0400640
641 features: features,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000642 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400643}
644
Jingwen Chened9c17d2021-04-13 07:14:55 +0000645// Relativize a list of root-relative paths with respect to the module's
646// directory.
647//
648// include_dirs Soong prop are root-relative (b/183742505), but
649// local_include_dirs, export_include_dirs and export_system_include_dirs are
650// module dir relative. This function makes a list of paths entirely module dir
651// relative.
652//
653// For the `include` attribute, Bazel wants the paths to be relative to the
654// module.
655func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000656 var relativePaths []string
657 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000658 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
659 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
660 if err != nil {
661 panic(err)
662 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000663 relativePaths = append(relativePaths, relativePath)
664 }
665 return relativePaths
666}
667
Liz Kammer5fad5012021-09-09 14:08:21 -0400668// BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel
669// attributes.
670type BazelIncludes struct {
671 Includes bazel.StringListAttribute
672 SystemIncludes bazel.StringListAttribute
673}
674
675func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) BazelIncludes {
Jingwen Chen91220d72021-03-24 02:18:33 -0400676 libraryDecorator := module.linker.(*libraryDecorator)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400677 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
678}
Jingwen Chen91220d72021-03-24 02:18:33 -0400679
Liz Kammer5fad5012021-09-09 14:08:21 -0400680// Bp2buildParseExportedIncludesForPrebuiltLibrary returns a BazelIncludes with Bazel-ified values
681// to export includes from the underlying module's properties.
682func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.TopDownMutatorContext, module *Module) BazelIncludes {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400683 prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker)
684 libraryDecorator := prebuiltLibraryLinker.libraryDecorator
685 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
686}
687
688// bp2BuildParseExportedIncludes creates a string list attribute contains the
689// exported included directories of a module.
Liz Kammer5fad5012021-09-09 14:08:21 -0400690func bp2BuildParseExportedIncludesHelper(ctx android.TopDownMutatorContext, module *Module, libraryDecorator *libraryDecorator) BazelIncludes {
691 exported := BazelIncludes{}
Liz Kammer9abd62d2021-05-21 08:37:59 -0400692 for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) {
693 for config, props := range configToProps {
694 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
Liz Kammer5fad5012021-09-09 14:08:21 -0400695 if len(flagExporterProperties.Export_include_dirs) > 0 {
696 exported.Includes.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs)
697 }
698 if len(flagExporterProperties.Export_system_include_dirs) > 0 {
699 exported.SystemIncludes.SetSelectValue(axis, config, flagExporterProperties.Export_system_include_dirs)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400700 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400701 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400702 }
703 }
Liz Kammer5fad5012021-09-09 14:08:21 -0400704 exported.Includes.DeduplicateAxesFromBase()
705 exported.SystemIncludes.DeduplicateAxesFromBase()
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400706
Liz Kammer5fad5012021-09-09 14:08:21 -0400707 return exported
Jingwen Chen91220d72021-03-24 02:18:33 -0400708}
Chris Parsons953b3562021-09-20 15:14:39 -0400709
710func bazelLabelForStaticModule(ctx android.TopDownMutatorContext, m blueprint.Module) string {
711 label := android.BazelModuleLabel(ctx, m)
712 if aModule, ok := m.(android.Module); ok {
713 if ctx.OtherModuleType(aModule) == "cc_library" && !android.GenerateCcLibraryStaticOnly(m.Name()) {
714 label += "_bp2build_cc_library_static"
715 }
716 }
717 return label
718}
719
720func bazelLabelForSharedModule(ctx android.TopDownMutatorContext, m blueprint.Module) string {
721 // cc_library, at it's root name, propagates the shared library, which depends on the static
722 // library.
723 return android.BazelModuleLabel(ctx, m)
724}
725
726func bazelLabelForStaticWholeModuleDeps(ctx android.TopDownMutatorContext, m blueprint.Module) string {
727 label := bazelLabelForStaticModule(ctx, m)
728 if aModule, ok := m.(android.Module); ok {
729 if android.IsModulePrebuilt(aModule) {
730 label += "_alwayslink"
731 }
732 }
733 return label
734}
735
736func bazelLabelForWholeDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList {
737 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps)
738}
739
740func bazelLabelForWholeDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList {
741 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps)
742}
743
744func bazelLabelForStaticDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList {
745 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule)
746}
747
748func bazelLabelForStaticDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList {
749 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule)
750}
751
752func bazelLabelForSharedDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList {
753 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule)
754}
755
756func bazelLabelForHeaderDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList {
757 // This is not elegant, but bp2build's shared library targets only propagate
758 // their header information as part of the normal C++ provider.
759 return bazelLabelForSharedDeps(ctx, modules)
760}
761
762func bazelLabelForSharedDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList {
763 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
764}
Liz Kammer2b8004b2021-10-04 13:55:44 -0400765
766type binaryLinkerAttrs struct {
767 Linkshared *bool
768}
769
770func bp2buildBinaryLinkerProps(ctx android.TopDownMutatorContext, m *Module) binaryLinkerAttrs {
771 attrs := binaryLinkerAttrs{}
772 archVariantProps := m.GetArchVariantProperties(ctx, &BinaryLinkerProperties{})
773 for axis, configToProps := range archVariantProps {
774 for _, p := range configToProps {
775 props := p.(*BinaryLinkerProperties)
776 staticExecutable := props.Static_executable
777 if axis == bazel.NoConfigAxis {
778 if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared {
779 attrs.Linkshared = &linkBinaryShared
780 }
781 } else if staticExecutable != nil {
782 // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a
783 // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling
784 ctx.ModuleErrorf("bp2build cannot migrate a module with arch/target-specific static_executable values")
785 }
786 }
787 }
788
789 return attrs
790}