blob: 1de45baede83a47bf8dead18311163e969182f75 [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 Kammerba7a9c52021-05-26 08:45:30 -040023
24 "github.com/google/blueprint/proptools"
Jingwen Chen91220d72021-03-24 02:18:33 -040025)
26
27// bp2build functions and helpers for converting cc_* modules to Bazel.
28
29func init() {
30 android.DepsBp2BuildMutators(RegisterDepsBp2Build)
31}
32
33func RegisterDepsBp2Build(ctx android.RegisterMutatorsContext) {
34 ctx.BottomUp("cc_bp2build_deps", depsBp2BuildMutator)
35}
36
37// A naive deps mutator to add deps on all modules across all combinations of
38// target props for cc modules. This is needed to make module -> bazel label
39// resolution work in the bp2build mutator later. This is probably
40// the wrong way to do it, but it works.
41//
42// TODO(jingwen): can we create a custom os mutator in depsBp2BuildMutator to do this?
43func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) {
44 module, ok := ctx.Module().(*Module)
45 if !ok {
46 // Not a cc module
47 return
48 }
49
50 if !module.ConvertWithBp2build(ctx) {
51 return
52 }
53
54 var allDeps []string
55
Liz Kammer9abd62d2021-05-21 08:37:59 -040056 for _, configToProps := range module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) {
57 for _, props := range configToProps {
58 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040059 allDeps = append(allDeps, baseCompilerProps.Generated_headers...)
60 allDeps = append(allDeps, baseCompilerProps.Generated_sources...)
61 }
62 }
Chris Parsons484e50a2021-05-13 15:13:04 -040063 }
64
Liz Kammer9abd62d2021-05-21 08:37:59 -040065 for _, configToProps := range module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) {
66 for _, props := range configToProps {
67 if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040068 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
69 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
70 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
Liz Kammer9abd62d2021-05-21 08:37:59 -040071 allDeps = append(allDeps, baseLinkerProps.Exclude_static_libs...)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040072 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
Liz Kammer9abd62d2021-05-21 08:37:59 -040073 allDeps = append(allDeps, baseLinkerProps.Shared_libs...)
74 allDeps = append(allDeps, baseLinkerProps.Exclude_shared_libs...)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040075 }
76 }
Jingwen Chened9c17d2021-04-13 07:14:55 +000077 }
78
Jingwen Chen53681ef2021-04-29 08:15:13 +000079 // Deps in the static: { .. } and shared: { .. } props of a cc_library.
80 if lib, ok := module.compiler.(*libraryDecorator); ok {
Jingwen Chenbcf53042021-05-26 04:42:42 +000081 appendDeps := func(deps []string, p StaticOrSharedProperties) []string {
82 deps = append(deps, p.Static_libs...)
83 deps = append(deps, p.Whole_static_libs...)
84 deps = append(deps, p.Shared_libs...)
85 return deps
86 }
Jingwen Chen53681ef2021-04-29 08:15:13 +000087
Jingwen Chenbcf53042021-05-26 04:42:42 +000088 allDeps = appendDeps(allDeps, lib.SharedProperties.Shared)
89 allDeps = appendDeps(allDeps, lib.StaticProperties.Static)
Jingwen Chen45dec102021-05-19 10:30:29 +000090
91 // TODO(b/186024507, b/186489250): Temporarily exclude adding
92 // system_shared_libs deps until libc and libm builds.
93 // allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...)
94 // allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...)
Jingwen Chenbcf53042021-05-26 04:42:42 +000095
96 // Deps in the target/arch nested static: { .. } and shared: { .. } props of a cc_library.
97 // target: { <target>: shared: { ... } }
Liz Kammer9abd62d2021-05-21 08:37:59 -040098 for _, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) {
99 for _, props := range configToProps {
100 if p, ok := props.(*SharedProperties); ok {
Jingwen Chenbcf53042021-05-26 04:42:42 +0000101 allDeps = appendDeps(allDeps, p.Shared)
102 }
103 }
104 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400105
106 for _, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) {
107 for _, props := range configToProps {
108 if p, ok := props.(*StaticProperties); ok {
Jingwen Chenbcf53042021-05-26 04:42:42 +0000109 allDeps = appendDeps(allDeps, p.Static)
110 }
111 }
112 }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000113 }
114
Jingwen Chen91220d72021-03-24 02:18:33 -0400115 ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...)
116}
117
Liz Kammer2222c6b2021-05-24 15:41:47 -0400118// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
Jingwen Chenbcf53042021-05-26 04:42:42 +0000119// properties which apply to either the shared or static version of a cc_library module.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400120type staticOrSharedAttributes struct {
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000121 srcs bazel.LabelListAttribute
122 srcs_c bazel.LabelListAttribute
123 srcs_as bazel.LabelListAttribute
124
125 copts bazel.StringListAttribute
126
Chris Parsons08648312021-05-06 16:23:19 -0400127 staticDeps bazel.LabelListAttribute
128 dynamicDeps bazel.LabelListAttribute
129 wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +0000130}
131
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000132func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) (cppSrcs, cSrcs, asSrcs bazel.LabelListAttribute) {
133 // Branch srcs into three language-specific groups.
134 // C++ is the "catch-all" group, and comprises generated sources because we don't
135 // know the language of these sources until the genrule is executed.
136 // TODO(b/190006308): Handle language detection of sources in a Bazel rule.
137 isCSrcOrFilegroup := func(s string) bool {
138 return strings.HasSuffix(s, ".c") || strings.HasSuffix(s, "_c_srcs")
139 }
140
141 isAsmSrcOrFilegroup := func(s string) bool {
142 return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s") || strings.HasSuffix(s, "_as_srcs")
143 }
144
145 // Check that a module is a filegroup type named <label>.
146 isFilegroupNamed := func(m android.Module, fullLabel string) bool {
147 if ctx.OtherModuleType(m) != "filegroup" {
148 return false
149 }
150 labelParts := strings.Split(fullLabel, ":")
151 if len(labelParts) > 2 {
152 // There should not be more than one colon in a label.
153 panic(fmt.Errorf("%s is not a valid Bazel label for a filegroup", fullLabel))
154 } else {
155 return m.Name() == labelParts[len(labelParts)-1]
156 }
157 }
158
159 // Convert the filegroup dependencies into the extension-specific filegroups
160 // filtered in the filegroup.bzl macro.
161 cppFilegroup := func(label string) string {
162 ctx.VisitDirectDeps(func(m android.Module) {
163 if isFilegroupNamed(m, label) {
164 label = label + "_cpp_srcs"
165 return
166 }
167 })
168 return label
169 }
170 cFilegroup := func(label string) string {
171 ctx.VisitDirectDeps(func(m android.Module) {
172 if isFilegroupNamed(m, label) {
173 label = label + "_c_srcs"
174 return
175 }
176 })
177 return label
178 }
179 asFilegroup := func(label string) string {
180 ctx.VisitDirectDeps(func(m android.Module) {
181 if isFilegroupNamed(m, label) {
182 label = label + "_as_srcs"
183 return
184 }
185 })
186 return label
187 }
188
189 cSrcs = bazel.MapLabelListAttribute(srcs, cFilegroup)
190 cSrcs = bazel.FilterLabelListAttribute(cSrcs, isCSrcOrFilegroup)
191
192 asSrcs = bazel.MapLabelListAttribute(srcs, asFilegroup)
193 asSrcs = bazel.FilterLabelListAttribute(asSrcs, isAsmSrcOrFilegroup)
194
195 cppSrcs = bazel.MapLabelListAttribute(srcs, cppFilegroup)
196 cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, cSrcs)
197 cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, asSrcs)
198 return
199}
200
Jingwen Chen53681ef2021-04-29 08:15:13 +0000201// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400202func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000203 lib, ok := module.compiler.(*libraryDecorator)
204 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400205 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000206 }
207
Jingwen Chenbcf53042021-05-26 04:42:42 +0000208 return bp2buildParseStaticOrSharedProps(ctx, module, lib, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000209}
210
211// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400212func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000213 lib, ok := module.compiler.(*libraryDecorator)
214 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400215 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000216 }
217
Jingwen Chenbcf53042021-05-26 04:42:42 +0000218 return bp2buildParseStaticOrSharedProps(ctx, module, lib, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400219}
220
Jingwen Chenbcf53042021-05-26 04:42:42 +0000221func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes {
222 var props StaticOrSharedProperties
223 if isStatic {
224 props = lib.StaticProperties.Static
225 } else {
226 props = lib.SharedProperties.Shared
Jingwen Chen53681ef2021-04-29 08:15:13 +0000227 }
Jingwen Chenbcf53042021-05-26 04:42:42 +0000228
229 attrs := staticOrSharedAttributes{
230 copts: bazel.StringListAttribute{Value: props.Cflags},
Liz Kammer9abd62d2021-05-21 08:37:59 -0400231 srcs: bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, props.Srcs)),
232 staticDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Static_libs)),
233 dynamicDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Shared_libs)),
234 wholeArchiveDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)),
Jingwen Chenbcf53042021-05-26 04:42:42 +0000235 }
236
Liz Kammer9abd62d2021-05-21 08:37:59 -0400237 setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
238 attrs.copts.SetSelectValue(axis, config, props.Cflags)
239 attrs.srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
240 attrs.staticDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Static_libs))
241 attrs.dynamicDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Shared_libs))
242 attrs.wholeArchiveDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs))
Jingwen Chenbcf53042021-05-26 04:42:42 +0000243 }
244
245 if isStatic {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400246 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) {
247 for config, props := range configToProps {
248 if staticOrSharedProps, ok := props.(*StaticProperties); ok {
249 setAttrs(axis, config, staticOrSharedProps.Static)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000250 }
251 }
252 }
253 } else {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400254 for axis, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) {
255 for config, props := range configToProps {
256 if staticOrSharedProps, ok := props.(*SharedProperties); ok {
257 setAttrs(axis, config, staticOrSharedProps.Shared)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000258 }
259 }
260 }
261 }
262
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000263 cppSrcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, attrs.srcs)
264 attrs.srcs = cppSrcs
265 attrs.srcs_c = cSrcs
266 attrs.srcs_as = asSrcs
267
Jingwen Chenbcf53042021-05-26 04:42:42 +0000268 return attrs
Jingwen Chen53681ef2021-04-29 08:15:13 +0000269}
270
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400271// Convenience struct to hold all attributes parsed from prebuilt properties.
272type prebuiltAttributes struct {
273 Src bazel.LabelAttribute
274}
275
276func Bp2BuildParsePrebuiltLibraryProps(ctx android.TopDownMutatorContext, module *Module) prebuiltAttributes {
277 prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker)
278 prebuiltLinker := prebuiltLibraryLinker.prebuiltLinker
279
280 var srcLabelAttribute bazel.LabelAttribute
281
282 if len(prebuiltLinker.properties.Srcs) > 1 {
283 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file\n")
284 }
285
286 if len(prebuiltLinker.properties.Srcs) == 1 {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400287 srcLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinker.properties.Srcs[0]))
288 }
289 for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltLinkerProperties{}) {
290 for config, props := range configToProps {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400291 if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok {
292 if len(prebuiltLinkerProperties.Srcs) > 1 {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400293 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for %s %s\n", axis, config)
294 continue
295 } else if len(prebuiltLinkerProperties.Srcs) == 0 {
296 continue
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400297 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400298 src := android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0])
299 srcLabelAttribute.SetSelectValue(axis, config, src)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400300 }
301 }
302 }
303
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400304 return prebuiltAttributes{
305 Src: srcLabelAttribute,
306 }
307}
308
Jingwen Chen107c0de2021-04-09 10:43:12 +0000309// Convenience struct to hold all attributes parsed from compiler properties.
310type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400311 // Options for all languages
312 copts bazel.StringListAttribute
313 // Assembly options and sources
314 asFlags bazel.StringListAttribute
315 asSrcs bazel.LabelListAttribute
316 // C options and sources
317 conlyFlags bazel.StringListAttribute
318 cSrcs bazel.LabelListAttribute
319 // C++ options and sources
320 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000321 srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000322}
323
Jingwen Chen63930982021-03-24 10:04:33 -0400324// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000325func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
Jingwen Chen882bcc12021-04-27 05:54:20 +0000326 var srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000327 var copts bazel.StringListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400328 var asFlags bazel.StringListAttribute
329 var conlyFlags bazel.StringListAttribute
330 var cppFlags bazel.StringListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400331
Chris Parsons484e50a2021-05-13 15:13:04 -0400332 // Creates the -I flags for a directory, while making the directory relative
Jingwen Chened9c17d2021-04-13 07:14:55 +0000333 // to the exec root for Bazel to work.
Chris Parsons484e50a2021-05-13 15:13:04 -0400334 includeFlags := func(dir string) []string {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000335 // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements.
Chris Parsons484e50a2021-05-13 15:13:04 -0400336 moduleDirRootedPath := filepath.Join(ctx.ModuleDir(), dir)
337 return []string{
338 "-I" + moduleDirRootedPath,
339 // Include the bindir-rooted path (using make variable substitution). This most
340 // closely matches Bazel's native include path handling, which allows for dependency
341 // on generated headers in these directories.
342 // TODO(b/188084383): Handle local include directories in Bazel.
343 "-I$(BINDIR)/" + moduleDirRootedPath,
344 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000345 }
346
Jingwen Chened9c17d2021-04-13 07:14:55 +0000347 // Parse the list of module-relative include directories (-I).
348 parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string {
349 // include_dirs are root-relative, not module-relative.
350 includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
351 return append(includeDirs, baseCompilerProps.Local_include_dirs...)
352 }
353
Chris Parsons990c4f42021-05-25 12:10:58 -0400354 parseCommandLineFlags := func(soongFlags []string) []string {
355 var result []string
356 for _, flag := range soongFlags {
Colin Cross52aa4e12021-05-25 15:20:39 +0000357 // Soong's cflags can contain spaces, like `-include header.h`. For
358 // Bazel's copts, split them up to be compatible with the
359 // no_copts_tokenization feature.
Chris Parsons990c4f42021-05-25 12:10:58 -0400360 result = append(result, strings.Split(flag, " ")...)
Colin Cross52aa4e12021-05-25 15:20:39 +0000361 }
Chris Parsons990c4f42021-05-25 12:10:58 -0400362 return result
363 }
364
365 // Parse the list of copts.
366 parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string {
367 var copts []string
368 copts = append(copts, parseCommandLineFlags(baseCompilerProps.Cflags)...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000369 for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
Chris Parsons484e50a2021-05-13 15:13:04 -0400370 copts = append(copts, includeFlags(dir)...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000371 }
372 return copts
Jingwen Chen63930982021-03-24 10:04:33 -0400373 }
374
Liz Kammer74deed42021-06-02 13:02:03 -0400375 // Parse srcs from an arch or OS's props value.
Jingwen Chene32e9e02021-04-23 09:17:24 +0000376 parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList {
Chris Parsons484e50a2021-05-13 15:13:04 -0400377 // Add srcs-like dependencies such as generated files.
378 // First create a LabelList containing these dependencies, then merge the values with srcs.
379 generatedHdrsAndSrcs := baseCompilerProps.Generated_headers
380 generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400381 generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs)
382
Liz Kammer74deed42021-06-02 13:02:03 -0400383 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, baseCompilerProps.Srcs, baseCompilerProps.Exclude_srcs)
Chris Parsons484e50a2021-05-13 15:13:04 -0400384 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000385 }
386
Jingwen Chenc1c26502021-04-05 10:35:13 +0000387 for _, props := range module.compiler.compilerProps() {
388 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400389 srcs.SetValue(parseSrcs(baseCompilerProps))
Jingwen Chened9c17d2021-04-13 07:14:55 +0000390 copts.Value = parseCopts(baseCompilerProps)
Chris Parsons990c4f42021-05-25 12:10:58 -0400391 asFlags.Value = parseCommandLineFlags(baseCompilerProps.Asflags)
392 conlyFlags.Value = parseCommandLineFlags(baseCompilerProps.Conlyflags)
393 cppFlags.Value = parseCommandLineFlags(baseCompilerProps.Cppflags)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000394
Jingwen Chenc1c26502021-04-05 10:35:13 +0000395 break
396 }
397 }
398
Jingwen Chene32e9e02021-04-23 09:17:24 +0000399 // Handle include_build_directory prop. If the property is true, then the
400 // target has access to all headers recursively in the package, and has
401 // "-I<module-dir>" in its copts.
Jingwen Chened9c17d2021-04-13 07:14:55 +0000402 if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400403 copts.Value = append(copts.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000404 } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400405 copts.Value = append(copts.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000406 }
407
Liz Kammer9abd62d2021-05-21 08:37:59 -0400408 archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
Jingwen Chene32e9e02021-04-23 09:17:24 +0000409
Liz Kammer9abd62d2021-05-21 08:37:59 -0400410 for axis, configToProps := range archVariantCompilerProps {
411 for config, props := range configToProps {
412 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
413 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
414 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
415 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
416 srcsList := parseSrcs(baseCompilerProps)
417 srcs.SetSelectValue(axis, config, srcsList)
Liz Kammer9abd62d2021-05-21 08:37:59 -0400418 }
419
420 copts.SetSelectValue(axis, config, parseCopts(baseCompilerProps))
421 asFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Asflags))
422 conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Conlyflags))
423 cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Cppflags))
424 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000425 }
426 }
427
Liz Kammer74deed42021-06-02 13:02:03 -0400428 srcs.ResolveExcludes()
Jingwen Chenc1c26502021-04-05 10:35:13 +0000429
Liz Kammerba7a9c52021-05-26 08:45:30 -0400430 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
431 "Cflags": &copts,
432 "Asflags": &asFlags,
433 "CppFlags": &cppFlags,
434 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400435 productVariableProps := android.ProductVariableProperties(ctx)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400436 for propName, attr := range productVarPropNameToAttribute {
437 if props, exists := productVariableProps[propName]; exists {
438 for _, prop := range props {
439 flags, ok := prop.Property.([]string)
440 if !ok {
441 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
442 }
443 newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable)
Liz Kammer9abd62d2021-05-21 08:37:59 -0400444 attr.SetSelectValue(bazel.ProductVariableConfigurationAxis(prop.ProductConfigVariable), prop.ProductConfigVariable, newFlags)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400445 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400446 }
447 }
448
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000449 srcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, srcs)
450
Jingwen Chen107c0de2021-04-09 10:43:12 +0000451 return compilerAttributes{
Chris Parsons990c4f42021-05-25 12:10:58 -0400452 copts: copts,
453 srcs: srcs,
454 asFlags: asFlags,
455 asSrcs: asSrcs,
456 cSrcs: cSrcs,
457 conlyFlags: conlyFlags,
458 cppFlags: cppFlags,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000459 }
460}
461
462// Convenience struct to hold all attributes parsed from linker properties.
463type linkerAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400464 deps bazel.LabelListAttribute
465 dynamicDeps bazel.LabelListAttribute
466 wholeArchiveDeps bazel.LabelListAttribute
Chris Parsonsd6358772021-05-18 18:35:24 -0400467 exportedDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400468 linkopts bazel.StringListAttribute
469 versionScript bazel.LabelAttribute
Jingwen Chenc1c26502021-04-05 10:35:13 +0000470}
471
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400472// FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here
473func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string {
474 flags := linkerProperties.Ldflags
475 if !BoolDefault(linkerProperties.Pack_relocations, true) {
476 flags = append(flags, "-Wl,--pack-dyn-relocs=none")
477 }
478 return flags
479}
480
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200481// bp2BuildParseLinkerProps parses the linker properties of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400482// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000483func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Jingwen Chen63930982021-03-24 10:04:33 -0400484 var deps bazel.LabelListAttribute
Chris Parsonsd6358772021-05-18 18:35:24 -0400485 var exportedDeps bazel.LabelListAttribute
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400486 var dynamicDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400487 var wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400488 var linkopts bazel.StringListAttribute
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200489 var versionScript bazel.LabelAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400490
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400491 getLibs := func(baseLinkerProps *BaseLinkerProperties) []string {
492 libs := baseLinkerProps.Header_libs
493 libs = append(libs, baseLinkerProps.Static_libs...)
494 libs = android.SortedUniqueStrings(libs)
495 return libs
496 }
497
Jingwen Chen91220d72021-03-24 02:18:33 -0400498 for _, linkerProps := range module.linker.linkerProps() {
499 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400500 libs := getLibs(baseLinkerProps)
Chris Parsonsd6358772021-05-18 18:35:24 -0400501 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400502 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chened9c17d2021-04-13 07:14:55 +0000503 deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs))
Chris Parsonsd6358772021-05-18 18:35:24 -0400504 exportedDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400505 linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps)
Chris Parsons08648312021-05-06 16:23:19 -0400506 wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200507
508 if baseLinkerProps.Version_script != nil {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400509 versionScript.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200510 }
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400511
512 sharedLibs := baseLinkerProps.Shared_libs
513 dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, sharedLibs))
514
Jingwen Chen91220d72021-03-24 02:18:33 -0400515 break
516 }
517 }
518
Liz Kammer9abd62d2021-05-21 08:37:59 -0400519 for axis, configToProps := range module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) {
520 for config, props := range configToProps {
521 if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400522 libs := getLibs(baseLinkerProps)
523 exportedLibs := baseLinkerProps.Export_header_lib_headers
524 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Liz Kammer9abd62d2021-05-21 08:37:59 -0400525 deps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, libs))
526 exportedDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, exportedLibs))
527 linkopts.SetSelectValue(axis, config, getBp2BuildLinkerFlags(baseLinkerProps))
528 wholeArchiveDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400529
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400530 if baseLinkerProps.Version_script != nil {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400531 versionScript.SetSelectValue(axis, config, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400532 }
533
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400534 sharedLibs := baseLinkerProps.Shared_libs
Liz Kammer9abd62d2021-05-21 08:37:59 -0400535 dynamicDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, sharedLibs))
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400536 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400537 }
538 }
539
Jingwen Chen107c0de2021-04-09 10:43:12 +0000540 return linkerAttributes{
Chris Parsons08648312021-05-06 16:23:19 -0400541 deps: deps,
Chris Parsonsd6358772021-05-18 18:35:24 -0400542 exportedDeps: exportedDeps,
Chris Parsons08648312021-05-06 16:23:19 -0400543 dynamicDeps: dynamicDeps,
544 wholeArchiveDeps: wholeArchiveDeps,
545 linkopts: linkopts,
546 versionScript: versionScript,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000547 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400548}
549
Jingwen Chened9c17d2021-04-13 07:14:55 +0000550// Relativize a list of root-relative paths with respect to the module's
551// directory.
552//
553// include_dirs Soong prop are root-relative (b/183742505), but
554// local_include_dirs, export_include_dirs and export_system_include_dirs are
555// module dir relative. This function makes a list of paths entirely module dir
556// relative.
557//
558// For the `include` attribute, Bazel wants the paths to be relative to the
559// module.
560func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000561 var relativePaths []string
562 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000563 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
564 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
565 if err != nil {
566 panic(err)
567 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000568 relativePaths = append(relativePaths, relativePath)
569 }
570 return relativePaths
571}
572
Jingwen Chen882bcc12021-04-27 05:54:20 +0000573func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
Jingwen Chen91220d72021-03-24 02:18:33 -0400574 libraryDecorator := module.linker.(*libraryDecorator)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400575 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
576}
Jingwen Chen91220d72021-03-24 02:18:33 -0400577
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400578func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
579 prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker)
580 libraryDecorator := prebuiltLibraryLinker.libraryDecorator
581 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
582}
583
584// bp2BuildParseExportedIncludes creates a string list attribute contains the
585// exported included directories of a module.
586func bp2BuildParseExportedIncludesHelper(ctx android.TopDownMutatorContext, module *Module, libraryDecorator *libraryDecorator) bazel.StringListAttribute {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000587 // Export_system_include_dirs and export_include_dirs are already module dir
588 // relative, so they don't need to be relativized like include_dirs, which
589 // are root-relative.
Jingwen Chen91220d72021-03-24 02:18:33 -0400590 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
591 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000592 includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
Jingwen Chen91220d72021-03-24 02:18:33 -0400593
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400594 getVariantIncludeDirs := func(includeDirs []string, flagExporterProperties *FlagExporterProperties) []string {
595 variantIncludeDirs := flagExporterProperties.Export_system_include_dirs
596 variantIncludeDirs = append(variantIncludeDirs, flagExporterProperties.Export_include_dirs...)
597
598 // To avoid duplicate includes when base includes + arch includes are combined
599 // TODO: This doesn't take conflicts between arch and os includes into account
600 variantIncludeDirs = bazel.SubtractStrings(variantIncludeDirs, includeDirs)
601 return variantIncludeDirs
602 }
603
Liz Kammer9abd62d2021-05-21 08:37:59 -0400604 for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) {
605 for config, props := range configToProps {
606 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
607 archVariantIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties)
608 if len(archVariantIncludeDirs) > 0 {
609 includeDirsAttribute.SetSelectValue(axis, config, archVariantIncludeDirs)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400610 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400611 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400612 }
613 }
614
Jingwen Chen882bcc12021-04-27 05:54:20 +0000615 return includeDirsAttribute
Jingwen Chen91220d72021-03-24 02:18:33 -0400616}