blob: e3b164d11ff63bf1243bbbb3e20160e2f9767cda [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 (
Jingwen Chen14a8bda2021-06-02 11:10:02 +000017 "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 Kammer2222c6b2021-05-24 15:41:47 -040029// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
Jingwen Chenbcf53042021-05-26 04:42:42 +000030// properties which apply to either the shared or static version of a cc_library module.
Liz Kammer2222c6b2021-05-24 15:41:47 -040031type staticOrSharedAttributes struct {
Jingwen Chenc4dc9b42021-06-11 12:51:48 +000032 Srcs bazel.LabelListAttribute
33 Srcs_c bazel.LabelListAttribute
34 Srcs_as bazel.LabelListAttribute
35 Copts bazel.StringListAttribute
Jingwen Chen14a8bda2021-06-02 11:10:02 +000036
Liz Kammer7a210ac2021-09-22 15:52:58 -040037 Deps bazel.LabelListAttribute
38 Implementation_deps bazel.LabelListAttribute
39 Dynamic_deps bazel.LabelListAttribute
40 Implementation_dynamic_deps bazel.LabelListAttribute
41 Whole_archive_deps bazel.LabelListAttribute
Chris Parsons51f8c392021-08-03 21:01:05 -040042
43 System_dynamic_deps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +000044}
45
Jingwen Chen14a8bda2021-06-02 11:10:02 +000046func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) (cppSrcs, cSrcs, asSrcs bazel.LabelListAttribute) {
47 // Branch srcs into three language-specific groups.
48 // C++ is the "catch-all" group, and comprises generated sources because we don't
49 // know the language of these sources until the genrule is executed.
50 // TODO(b/190006308): Handle language detection of sources in a Bazel rule.
51 isCSrcOrFilegroup := func(s string) bool {
52 return strings.HasSuffix(s, ".c") || strings.HasSuffix(s, "_c_srcs")
53 }
54
55 isAsmSrcOrFilegroup := func(s string) bool {
56 return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s") || strings.HasSuffix(s, "_as_srcs")
57 }
58
59 // Check that a module is a filegroup type named <label>.
60 isFilegroupNamed := func(m android.Module, fullLabel string) bool {
61 if ctx.OtherModuleType(m) != "filegroup" {
62 return false
63 }
64 labelParts := strings.Split(fullLabel, ":")
65 if len(labelParts) > 2 {
66 // There should not be more than one colon in a label.
67 panic(fmt.Errorf("%s is not a valid Bazel label for a filegroup", fullLabel))
68 } else {
69 return m.Name() == labelParts[len(labelParts)-1]
70 }
71 }
72
73 // Convert the filegroup dependencies into the extension-specific filegroups
74 // filtered in the filegroup.bzl macro.
75 cppFilegroup := func(label string) string {
Chris Parsons5a34ffb2021-07-21 14:34:58 -040076 m, exists := ctx.ModuleFromName(label)
77 if exists {
78 aModule, _ := m.(android.Module)
79 if isFilegroupNamed(aModule, label) {
Jingwen Chen14a8bda2021-06-02 11:10:02 +000080 label = label + "_cpp_srcs"
Jingwen Chen14a8bda2021-06-02 11:10:02 +000081 }
Chris Parsons5a34ffb2021-07-21 14:34:58 -040082 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +000083 return label
84 }
85 cFilegroup := func(label string) string {
Chris Parsons5a34ffb2021-07-21 14:34:58 -040086 m, exists := ctx.ModuleFromName(label)
87 if exists {
88 aModule, _ := m.(android.Module)
89 if isFilegroupNamed(aModule, label) {
Jingwen Chen14a8bda2021-06-02 11:10:02 +000090 label = label + "_c_srcs"
Jingwen Chen14a8bda2021-06-02 11:10:02 +000091 }
Chris Parsons5a34ffb2021-07-21 14:34:58 -040092 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +000093 return label
94 }
95 asFilegroup := func(label string) string {
Chris Parsons5a34ffb2021-07-21 14:34:58 -040096 m, exists := ctx.ModuleFromName(label)
97 if exists {
98 aModule, _ := m.(android.Module)
99 if isFilegroupNamed(aModule, label) {
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000100 label = label + "_as_srcs"
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000101 }
Chris Parsons5a34ffb2021-07-21 14:34:58 -0400102 }
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000103 return label
104 }
105
106 cSrcs = bazel.MapLabelListAttribute(srcs, cFilegroup)
107 cSrcs = bazel.FilterLabelListAttribute(cSrcs, isCSrcOrFilegroup)
108
109 asSrcs = bazel.MapLabelListAttribute(srcs, asFilegroup)
110 asSrcs = bazel.FilterLabelListAttribute(asSrcs, isAsmSrcOrFilegroup)
111
112 cppSrcs = bazel.MapLabelListAttribute(srcs, cppFilegroup)
113 cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, cSrcs)
114 cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, asSrcs)
115 return
116}
117
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000118// bp2BuildParseLibProps returns the attributes for a variant of a cc_library.
119func bp2BuildParseLibProps(ctx android.TopDownMutatorContext, module *Module, isStatic bool) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000120 lib, ok := module.compiler.(*libraryDecorator)
121 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400122 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000123 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000124 return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic)
125}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000126
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000127// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
128func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
129 return bp2BuildParseLibProps(ctx, module, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000130}
131
132// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400133func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000134 return bp2BuildParseLibProps(ctx, module, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400135}
136
Liz Kammer7a210ac2021-09-22 15:52:58 -0400137type depsPartition struct {
138 export bazel.LabelList
139 implementation bazel.LabelList
140}
141
142type bazelLabelForDepsFn func(android.TopDownMutatorContext, []string) bazel.LabelList
143
144func partitionExportedAndImplementationsDeps(ctx android.TopDownMutatorContext, allDeps, exportedDeps []string, fn bazelLabelForDepsFn) depsPartition {
145 implementation, export := android.FilterList(allDeps, exportedDeps)
146
147 return depsPartition{
148 export: fn(ctx, export),
149 implementation: fn(ctx, implementation),
150 }
151}
152
153type bazelLabelForDepsExcludesFn func(android.TopDownMutatorContext, []string, []string) bazel.LabelList
154
155func partitionExportedAndImplementationsDepsExcludes(ctx android.TopDownMutatorContext, allDeps, excludes, exportedDeps []string, fn bazelLabelForDepsExcludesFn) depsPartition {
156 implementation, export := android.FilterList(allDeps, exportedDeps)
157
158 return depsPartition{
159 export: fn(ctx, export, excludes),
160 implementation: fn(ctx, implementation, excludes),
161 }
162}
163
Jingwen Chenbcf53042021-05-26 04:42:42 +0000164func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes {
Liz Kammer135bf552021-08-11 10:46:06 -0400165 attrs := staticOrSharedAttributes{}
Jingwen Chenbcf53042021-05-26 04:42:42 +0000166
Liz Kammer9abd62d2021-05-21 08:37:59 -0400167 setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000168 attrs.Copts.SetSelectValue(axis, config, props.Cflags)
169 attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
Chris Parsons953b3562021-09-20 15:14:39 -0400170 attrs.System_dynamic_deps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, props.System_shared_libs))
Liz Kammer7a210ac2021-09-22 15:52:58 -0400171
172 staticDeps := partitionExportedAndImplementationsDeps(ctx, props.Static_libs, props.Export_static_lib_headers, bazelLabelForStaticDeps)
173 attrs.Deps.SetSelectValue(axis, config, staticDeps.export)
174 attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation)
175
176 sharedDeps := partitionExportedAndImplementationsDeps(ctx, props.Shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDeps)
177 attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export)
178 attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation)
179
180 attrs.Whole_archive_deps.SetSelectValue(axis, config, bazelLabelForWholeDeps(ctx, props.Whole_static_libs))
Jingwen Chenbcf53042021-05-26 04:42:42 +0000181 }
Liz Kammer135bf552021-08-11 10:46:06 -0400182 // system_dynamic_deps distinguishes between nil/empty list behavior:
183 // nil -> use default values
184 // empty list -> no values specified
185 attrs.System_dynamic_deps.ForceSpecifyEmptyList = true
Jingwen Chenbcf53042021-05-26 04:42:42 +0000186
187 if isStatic {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400188 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) {
189 for config, props := range configToProps {
190 if staticOrSharedProps, ok := props.(*StaticProperties); ok {
191 setAttrs(axis, config, staticOrSharedProps.Static)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000192 }
193 }
194 }
195 } else {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400196 for axis, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) {
197 for config, props := range configToProps {
198 if staticOrSharedProps, ok := props.(*SharedProperties); ok {
199 setAttrs(axis, config, staticOrSharedProps.Shared)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000200 }
201 }
202 }
203 }
204
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000205 cppSrcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, attrs.Srcs)
206 attrs.Srcs = cppSrcs
207 attrs.Srcs_c = cSrcs
208 attrs.Srcs_as = asSrcs
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000209
Jingwen Chenbcf53042021-05-26 04:42:42 +0000210 return attrs
Jingwen Chen53681ef2021-04-29 08:15:13 +0000211}
212
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400213// Convenience struct to hold all attributes parsed from prebuilt properties.
214type prebuiltAttributes struct {
215 Src bazel.LabelAttribute
216}
217
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxac5097f2021-09-01 21:22:09 +0000218// NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400219func Bp2BuildParsePrebuiltLibraryProps(ctx android.TopDownMutatorContext, module *Module) prebuiltAttributes {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400220 var srcLabelAttribute bazel.LabelAttribute
221
Liz Kammer9abd62d2021-05-21 08:37:59 -0400222 for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltLinkerProperties{}) {
223 for config, props := range configToProps {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400224 if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok {
225 if len(prebuiltLinkerProperties.Srcs) > 1 {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400226 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for %s %s\n", axis, config)
227 continue
228 } else if len(prebuiltLinkerProperties.Srcs) == 0 {
229 continue
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400230 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400231 src := android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0])
232 srcLabelAttribute.SetSelectValue(axis, config, src)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400233 }
234 }
235 }
236
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400237 return prebuiltAttributes{
238 Src: srcLabelAttribute,
239 }
240}
241
Jingwen Chen107c0de2021-04-09 10:43:12 +0000242// Convenience struct to hold all attributes parsed from compiler properties.
243type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400244 // Options for all languages
245 copts bazel.StringListAttribute
246 // Assembly options and sources
247 asFlags bazel.StringListAttribute
248 asSrcs bazel.LabelListAttribute
249 // C options and sources
250 conlyFlags bazel.StringListAttribute
251 cSrcs bazel.LabelListAttribute
252 // C++ options and sources
253 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000254 srcs bazel.LabelListAttribute
Chris Parsons2c788392021-08-10 11:58:07 -0400255
256 rtti bazel.BoolAttribute
Chris Parsonsa967f252021-09-23 16:34:35 -0400257 stl *string
Liz Kammer35687bc2021-09-10 10:07:07 -0400258
259 localIncludes bazel.StringListAttribute
260 absoluteIncludes bazel.StringListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000261}
262
Jingwen Chen63930982021-03-24 10:04:33 -0400263// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000264func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
Jingwen Chen882bcc12021-04-27 05:54:20 +0000265 var srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000266 var copts bazel.StringListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400267 var asFlags bazel.StringListAttribute
268 var conlyFlags bazel.StringListAttribute
269 var cppFlags bazel.StringListAttribute
Chris Parsons2c788392021-08-10 11:58:07 -0400270 var rtti bazel.BoolAttribute
Liz Kammer35687bc2021-09-10 10:07:07 -0400271 var localIncludes bazel.StringListAttribute
272 var absoluteIncludes bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000273
Chris Parsons990c4f42021-05-25 12:10:58 -0400274 parseCommandLineFlags := func(soongFlags []string) []string {
275 var result []string
276 for _, flag := range soongFlags {
Colin Cross52aa4e12021-05-25 15:20:39 +0000277 // Soong's cflags can contain spaces, like `-include header.h`. For
278 // Bazel's copts, split them up to be compatible with the
279 // no_copts_tokenization feature.
Chris Parsons990c4f42021-05-25 12:10:58 -0400280 result = append(result, strings.Split(flag, " ")...)
Colin Cross52aa4e12021-05-25 15:20:39 +0000281 }
Chris Parsons990c4f42021-05-25 12:10:58 -0400282 return result
283 }
284
Liz Kammer74deed42021-06-02 13:02:03 -0400285 // Parse srcs from an arch or OS's props value.
Jingwen Chene32e9e02021-04-23 09:17:24 +0000286 parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList {
Chris Parsons484e50a2021-05-13 15:13:04 -0400287 // Add srcs-like dependencies such as generated files.
288 // First create a LabelList containing these dependencies, then merge the values with srcs.
289 generatedHdrsAndSrcs := baseCompilerProps.Generated_headers
290 generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400291 generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs)
292
Liz Kammer74deed42021-06-02 13:02:03 -0400293 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, baseCompilerProps.Srcs, baseCompilerProps.Exclude_srcs)
Chris Parsons484e50a2021-05-13 15:13:04 -0400294 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000295 }
296
Liz Kammer9abd62d2021-05-21 08:37:59 -0400297 archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
Liz Kammer9abd62d2021-05-21 08:37:59 -0400298 for axis, configToProps := range archVariantCompilerProps {
299 for config, props := range configToProps {
300 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
301 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
302 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
303 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
304 srcsList := parseSrcs(baseCompilerProps)
305 srcs.SetSelectValue(axis, config, srcsList)
Liz Kammer9abd62d2021-05-21 08:37:59 -0400306 }
307
Chris Parsons69fa9f92021-07-13 11:47:44 -0400308 archVariantCopts := parseCommandLineFlags(baseCompilerProps.Cflags)
309 archVariantAsflags := parseCommandLineFlags(baseCompilerProps.Asflags)
Liz Kammer35687bc2021-09-10 10:07:07 -0400310
311 localIncludeDirs := baseCompilerProps.Local_include_dirs
312 if axis == bazel.NoConfigAxis && includeBuildDirectory(baseCompilerProps.Include_build_directory) {
313 localIncludeDirs = append(localIncludeDirs, ".")
Chris Parsons69fa9f92021-07-13 11:47:44 -0400314 }
315
Liz Kammer35687bc2021-09-10 10:07:07 -0400316 absoluteIncludes.SetSelectValue(axis, config, baseCompilerProps.Include_dirs)
317 localIncludes.SetSelectValue(axis, config, localIncludeDirs)
Liz Kammer135bf552021-08-11 10:46:06 -0400318
Chris Parsons69fa9f92021-07-13 11:47:44 -0400319 copts.SetSelectValue(axis, config, archVariantCopts)
320 asFlags.SetSelectValue(axis, config, archVariantAsflags)
Liz Kammer9abd62d2021-05-21 08:37:59 -0400321 conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Conlyflags))
322 cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Cppflags))
Chris Parsons2c788392021-08-10 11:58:07 -0400323 rtti.SetSelectValue(axis, config, baseCompilerProps.Rtti)
Liz Kammer9abd62d2021-05-21 08:37:59 -0400324 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000325 }
326 }
327
Liz Kammer74deed42021-06-02 13:02:03 -0400328 srcs.ResolveExcludes()
Liz Kammer35687bc2021-09-10 10:07:07 -0400329 absoluteIncludes.DeduplicateAxesFromBase()
330 localIncludes.DeduplicateAxesFromBase()
Jingwen Chenc1c26502021-04-05 10:35:13 +0000331
Liz Kammerba7a9c52021-05-26 08:45:30 -0400332 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
333 "Cflags": &copts,
334 "Asflags": &asFlags,
335 "CppFlags": &cppFlags,
336 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400337 productVariableProps := android.ProductVariableProperties(ctx)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400338 for propName, attr := range productVarPropNameToAttribute {
339 if props, exists := productVariableProps[propName]; exists {
340 for _, prop := range props {
341 flags, ok := prop.Property.([]string)
342 if !ok {
343 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
344 }
345 newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable)
Liz Kammer47535c52021-06-02 16:02:22 -0400346 attr.SetSelectValue(bazel.ProductVariableConfigurationAxis(prop.FullConfig), prop.FullConfig, newFlags)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400347 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400348 }
349 }
350
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000351 srcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, srcs)
352
Chris Parsonsa967f252021-09-23 16:34:35 -0400353 var stl *string = nil
354 stlPropsByArch := module.GetArchVariantProperties(ctx, &StlProperties{})
355 for _, configToProps := range stlPropsByArch {
356 for _, props := range configToProps {
357 if stlProps, ok := props.(*StlProperties); ok {
358 if stlProps.Stl != nil {
359 if stl == nil {
360 stl = stlProps.Stl
361 } else {
362 if stl != stlProps.Stl {
363 ctx.ModuleErrorf("Unsupported conversion: module with different stl for different variants: %s and %s", *stl, stlProps.Stl)
364 }
365 }
366 }
367 }
368 }
369 }
370
Jingwen Chen107c0de2021-04-09 10:43:12 +0000371 return compilerAttributes{
Liz Kammer35687bc2021-09-10 10:07:07 -0400372 copts: copts,
373 srcs: srcs,
374 asFlags: asFlags,
375 asSrcs: asSrcs,
376 cSrcs: cSrcs,
377 conlyFlags: conlyFlags,
378 cppFlags: cppFlags,
379 rtti: rtti,
Chris Parsonsa967f252021-09-23 16:34:35 -0400380 stl: stl,
Liz Kammer35687bc2021-09-10 10:07:07 -0400381 localIncludes: localIncludes,
382 absoluteIncludes: absoluteIncludes,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000383 }
384}
385
386// Convenience struct to hold all attributes parsed from linker properties.
387type linkerAttributes struct {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400388 deps bazel.LabelListAttribute
389 implementationDeps bazel.LabelListAttribute
390 dynamicDeps bazel.LabelListAttribute
391 implementationDynamicDeps bazel.LabelListAttribute
392 wholeArchiveDeps bazel.LabelListAttribute
393 systemDynamicDeps bazel.LabelListAttribute
394
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000395 useLibcrt bazel.BoolAttribute
396 linkopts bazel.StringListAttribute
397 versionScript bazel.LabelAttribute
398 stripKeepSymbols bazel.BoolAttribute
399 stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
400 stripKeepSymbolsList bazel.StringListAttribute
401 stripAll bazel.BoolAttribute
402 stripNone bazel.BoolAttribute
Jingwen Chenc1c26502021-04-05 10:35:13 +0000403}
404
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400405// FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here
406func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string {
407 flags := linkerProperties.Ldflags
408 if !BoolDefault(linkerProperties.Pack_relocations, true) {
409 flags = append(flags, "-Wl,--pack-dyn-relocs=none")
410 }
411 return flags
412}
413
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200414// bp2BuildParseLinkerProps parses the linker properties of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400415// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000416func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400417
Liz Kammer47535c52021-06-02 16:02:22 -0400418 var headerDeps bazel.LabelListAttribute
Liz Kammer7a210ac2021-09-22 15:52:58 -0400419 var implementationHeaderDeps bazel.LabelListAttribute
420 var deps bazel.LabelListAttribute
421 var implementationDeps bazel.LabelListAttribute
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400422 var dynamicDeps bazel.LabelListAttribute
Liz Kammer7a210ac2021-09-22 15:52:58 -0400423 var implementationDynamicDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400424 var wholeArchiveDeps bazel.LabelListAttribute
Liz Kammer135bf552021-08-11 10:46:06 -0400425 systemSharedDeps := bazel.LabelListAttribute{ForceSpecifyEmptyList: true}
Liz Kammer7a210ac2021-09-22 15:52:58 -0400426
Jingwen Chen63930982021-03-24 10:04:33 -0400427 var linkopts bazel.StringListAttribute
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200428 var versionScript bazel.LabelAttribute
Liz Kammerd366c902021-06-03 13:43:01 -0400429 var useLibcrt bazel.BoolAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400430
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000431 var stripKeepSymbols bazel.BoolAttribute
432 var stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
433 var stripKeepSymbolsList bazel.StringListAttribute
434 var stripAll bazel.BoolAttribute
435 var stripNone bazel.BoolAttribute
436
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000437 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StripProperties{}) {
438 for config, props := range configToProps {
439 if stripProperties, ok := props.(*StripProperties); ok {
440 stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols)
441 stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list)
442 stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame)
443 stripAll.SetSelectValue(axis, config, stripProperties.Strip.All)
444 stripNone.SetSelectValue(axis, config, stripProperties.Strip.None)
445 }
446 }
447 }
448
Liz Kammer9abd62d2021-05-21 08:37:59 -0400449 for axis, configToProps := range module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) {
450 for config, props := range configToProps {
451 if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
Liz Kammer7a210ac2021-09-22 15:52:58 -0400452
Liz Kammer135bf552021-08-11 10:46:06 -0400453 // Excludes to parallel Soong:
454 // 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 -0400455 staticLibs := android.FirstUniqueStrings(baseLinkerProps.Static_libs)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400456 staticDeps := partitionExportedAndImplementationsDepsExcludes(ctx, staticLibs, baseLinkerProps.Exclude_static_libs, baseLinkerProps.Export_static_lib_headers, bazelLabelForStaticDepsExcludes)
457 deps.SetSelectValue(axis, config, staticDeps.export)
458 implementationDeps.SetSelectValue(axis, config, staticDeps.implementation)
459
460 wholeStaticLibs := android.FirstUniqueStrings(baseLinkerProps.Whole_static_libs)
461 wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, baseLinkerProps.Exclude_static_libs))
Chris Parsons51f8c392021-08-03 21:01:05 -0400462
Liz Kammer135bf552021-08-11 10:46:06 -0400463 systemSharedLibs := baseLinkerProps.System_shared_libs
464 // systemSharedLibs distinguishes between nil/empty list behavior:
465 // nil -> use default values
466 // empty list -> no values specified
467 if len(systemSharedLibs) > 0 {
468 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
Chris Parsons51f8c392021-08-03 21:01:05 -0400469 }
Chris Parsons953b3562021-09-20 15:14:39 -0400470 systemSharedDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
Chris Parsons51f8c392021-08-03 21:01:05 -0400471
472 sharedLibs := android.FirstUniqueStrings(baseLinkerProps.Shared_libs)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400473 sharedDeps := partitionExportedAndImplementationsDepsExcludes(ctx, sharedLibs, baseLinkerProps.Exclude_shared_libs, baseLinkerProps.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes)
474 dynamicDeps.SetSelectValue(axis, config, sharedDeps.export)
475 implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400476
Liz Kammer47535c52021-06-02 16:02:22 -0400477 headerLibs := android.FirstUniqueStrings(baseLinkerProps.Header_libs)
Liz Kammer7a210ac2021-09-22 15:52:58 -0400478 hDeps := partitionExportedAndImplementationsDeps(ctx, headerLibs, baseLinkerProps.Export_header_lib_headers, bazelLabelForHeaderDeps)
479
480 headerDeps.SetSelectValue(axis, config, hDeps.export)
481 implementationHeaderDeps.SetSelectValue(axis, config, hDeps.implementation)
Liz Kammer47535c52021-06-02 16:02:22 -0400482
483 linkopts.SetSelectValue(axis, config, getBp2BuildLinkerFlags(baseLinkerProps))
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400484 if baseLinkerProps.Version_script != nil {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400485 versionScript.SetSelectValue(axis, config, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400486 }
Liz Kammerd366c902021-06-03 13:43:01 -0400487 useLibcrt.SetSelectValue(axis, config, baseLinkerProps.libCrt())
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400488 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400489 }
490 }
491
Liz Kammer47535c52021-06-02 16:02:22 -0400492 type productVarDep struct {
493 // the name of the corresponding excludes field, if one exists
494 excludesField string
495 // reference to the bazel attribute that should be set for the given product variable config
496 attribute *bazel.LabelListAttribute
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400497
Chris Parsons953b3562021-09-20 15:14:39 -0400498 depResolutionFunc func(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList
Liz Kammer47535c52021-06-02 16:02:22 -0400499 }
500
501 productVarToDepFields := map[string]productVarDep{
502 // product variables do not support exclude_shared_libs
Liz Kammer7a210ac2021-09-22 15:52:58 -0400503 "Shared_libs": productVarDep{attribute: &implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes},
504 "Static_libs": productVarDep{"Exclude_static_libs", &implementationDeps, bazelLabelForStaticDepsExcludes},
Chris Parsons953b3562021-09-20 15:14:39 -0400505 "Whole_static_libs": productVarDep{"Exclude_static_libs", &wholeArchiveDeps, bazelLabelForWholeDepsExcludes},
Liz Kammer47535c52021-06-02 16:02:22 -0400506 }
507
508 productVariableProps := android.ProductVariableProperties(ctx)
509 for name, dep := range productVarToDepFields {
510 props, exists := productVariableProps[name]
511 excludeProps, excludesExists := productVariableProps[dep.excludesField]
512 // if neither an include or excludes property exists, then skip it
513 if !exists && !excludesExists {
514 continue
515 }
516 // collect all the configurations that an include or exclude property exists for.
517 // we want to iterate all configurations rather than either the include or exclude because for a
518 // particular configuration we may have only and include or only an exclude to handle
519 configs := make(map[string]bool, len(props)+len(excludeProps))
520 for config := range props {
521 configs[config] = true
522 }
523 for config := range excludeProps {
524 configs[config] = true
525 }
526
527 for config := range configs {
528 prop, includesExists := props[config]
529 excludesProp, excludesExists := excludeProps[config]
530 var includes, excludes []string
531 var ok bool
532 // if there was no includes/excludes property, casting fails and that's expected
533 if includes, ok = prop.Property.([]string); includesExists && !ok {
534 ctx.ModuleErrorf("Could not convert product variable %s property", name)
535 }
536 if excludes, ok = excludesProp.Property.([]string); excludesExists && !ok {
537 ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
538 }
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400539
540 dep.attribute.SetSelectValue(bazel.ProductVariableConfigurationAxis(config), config, dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes))
Liz Kammer47535c52021-06-02 16:02:22 -0400541 }
542 }
543
Liz Kammer7a210ac2021-09-22 15:52:58 -0400544 headerDeps.Append(deps)
545 implementationHeaderDeps.Append(implementationDeps)
546
547 headerDeps.ResolveExcludes()
548 implementationHeaderDeps.ResolveExcludes()
Liz Kammer47535c52021-06-02 16:02:22 -0400549 dynamicDeps.ResolveExcludes()
Liz Kammer7a210ac2021-09-22 15:52:58 -0400550 implementationDynamicDeps.ResolveExcludes()
Liz Kammer47535c52021-06-02 16:02:22 -0400551 wholeArchiveDeps.ResolveExcludes()
552
Jingwen Chen107c0de2021-04-09 10:43:12 +0000553 return linkerAttributes{
Liz Kammer7a210ac2021-09-22 15:52:58 -0400554 deps: headerDeps,
555 implementationDeps: implementationHeaderDeps,
556 dynamicDeps: dynamicDeps,
557 implementationDynamicDeps: implementationDynamicDeps,
558 wholeArchiveDeps: wholeArchiveDeps,
559 systemDynamicDeps: systemSharedDeps,
560
561 linkopts: linkopts,
562 useLibcrt: useLibcrt,
563 versionScript: versionScript,
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000564
565 // Strip properties
566 stripKeepSymbols: stripKeepSymbols,
567 stripKeepSymbolsAndDebugFrame: stripKeepSymbolsAndDebugFrame,
568 stripKeepSymbolsList: stripKeepSymbolsList,
569 stripAll: stripAll,
570 stripNone: stripNone,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000571 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400572}
573
Jingwen Chened9c17d2021-04-13 07:14:55 +0000574// Relativize a list of root-relative paths with respect to the module's
575// directory.
576//
577// include_dirs Soong prop are root-relative (b/183742505), but
578// local_include_dirs, export_include_dirs and export_system_include_dirs are
579// module dir relative. This function makes a list of paths entirely module dir
580// relative.
581//
582// For the `include` attribute, Bazel wants the paths to be relative to the
583// module.
584func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000585 var relativePaths []string
586 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000587 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
588 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
589 if err != nil {
590 panic(err)
591 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000592 relativePaths = append(relativePaths, relativePath)
593 }
594 return relativePaths
595}
596
Liz Kammer5fad5012021-09-09 14:08:21 -0400597// BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel
598// attributes.
599type BazelIncludes struct {
600 Includes bazel.StringListAttribute
601 SystemIncludes bazel.StringListAttribute
602}
603
604func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) BazelIncludes {
Jingwen Chen91220d72021-03-24 02:18:33 -0400605 libraryDecorator := module.linker.(*libraryDecorator)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400606 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
607}
Jingwen Chen91220d72021-03-24 02:18:33 -0400608
Liz Kammer5fad5012021-09-09 14:08:21 -0400609// Bp2buildParseExportedIncludesForPrebuiltLibrary returns a BazelIncludes with Bazel-ified values
610// to export includes from the underlying module's properties.
611func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.TopDownMutatorContext, module *Module) BazelIncludes {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400612 prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker)
613 libraryDecorator := prebuiltLibraryLinker.libraryDecorator
614 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
615}
616
617// bp2BuildParseExportedIncludes creates a string list attribute contains the
618// exported included directories of a module.
Liz Kammer5fad5012021-09-09 14:08:21 -0400619func bp2BuildParseExportedIncludesHelper(ctx android.TopDownMutatorContext, module *Module, libraryDecorator *libraryDecorator) BazelIncludes {
620 exported := BazelIncludes{}
Liz Kammer9abd62d2021-05-21 08:37:59 -0400621 for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) {
622 for config, props := range configToProps {
623 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
Liz Kammer5fad5012021-09-09 14:08:21 -0400624 if len(flagExporterProperties.Export_include_dirs) > 0 {
625 exported.Includes.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs)
626 }
627 if len(flagExporterProperties.Export_system_include_dirs) > 0 {
628 exported.SystemIncludes.SetSelectValue(axis, config, flagExporterProperties.Export_system_include_dirs)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400629 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400630 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400631 }
632 }
Liz Kammer5fad5012021-09-09 14:08:21 -0400633 exported.Includes.DeduplicateAxesFromBase()
634 exported.SystemIncludes.DeduplicateAxesFromBase()
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400635
Liz Kammer5fad5012021-09-09 14:08:21 -0400636 return exported
Jingwen Chen91220d72021-03-24 02:18:33 -0400637}
Chris Parsons953b3562021-09-20 15:14:39 -0400638
639func bazelLabelForStaticModule(ctx android.TopDownMutatorContext, m blueprint.Module) string {
640 label := android.BazelModuleLabel(ctx, m)
641 if aModule, ok := m.(android.Module); ok {
642 if ctx.OtherModuleType(aModule) == "cc_library" && !android.GenerateCcLibraryStaticOnly(m.Name()) {
643 label += "_bp2build_cc_library_static"
644 }
645 }
646 return label
647}
648
649func bazelLabelForSharedModule(ctx android.TopDownMutatorContext, m blueprint.Module) string {
650 // cc_library, at it's root name, propagates the shared library, which depends on the static
651 // library.
652 return android.BazelModuleLabel(ctx, m)
653}
654
655func bazelLabelForStaticWholeModuleDeps(ctx android.TopDownMutatorContext, m blueprint.Module) string {
656 label := bazelLabelForStaticModule(ctx, m)
657 if aModule, ok := m.(android.Module); ok {
658 if android.IsModulePrebuilt(aModule) {
659 label += "_alwayslink"
660 }
661 }
662 return label
663}
664
665func bazelLabelForWholeDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList {
666 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps)
667}
668
669func bazelLabelForWholeDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList {
670 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps)
671}
672
673func bazelLabelForStaticDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList {
674 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule)
675}
676
677func bazelLabelForStaticDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList {
678 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule)
679}
680
681func bazelLabelForSharedDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList {
682 return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule)
683}
684
685func bazelLabelForHeaderDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList {
686 // This is not elegant, but bp2build's shared library targets only propagate
687 // their header information as part of the normal C++ provider.
688 return bazelLabelForSharedDeps(ctx, modules)
689}
690
691func bazelLabelForSharedDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList {
692 return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule)
693}