blob: a156d54446a14502540c83e389aa61c48c650149 [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
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040056 for _, osProps := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) {
57 // os base compiler props
58 if baseCompilerProps, ok := osProps.Properties.(*BaseCompilerProperties); ok {
Chris Parsons484e50a2021-05-13 15:13:04 -040059 allDeps = append(allDeps, baseCompilerProps.Generated_headers...)
60 allDeps = append(allDeps, baseCompilerProps.Generated_sources...)
61 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040062 // os + arch base compiler props
63 for _, archProps := range osProps.ArchProperties {
64 if baseCompilerProps, ok := archProps.(*BaseCompilerProperties); ok {
65 allDeps = append(allDeps, baseCompilerProps.Generated_headers...)
66 allDeps = append(allDeps, baseCompilerProps.Generated_sources...)
67 }
68 }
Chris Parsons484e50a2021-05-13 15:13:04 -040069 }
70
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040071 for _, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Chris Parsons484e50a2021-05-13 15:13:04 -040072 // arch specific compiler props
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040073 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Chris Parsons484e50a2021-05-13 15:13:04 -040074 allDeps = append(allDeps, baseCompilerProps.Generated_headers...)
75 allDeps = append(allDeps, baseCompilerProps.Generated_sources...)
76 }
77 }
78
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040079 for _, osProps := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) {
80 // os specific linker props
81 if baseLinkerProps, ok := osProps.Properties.(*BaseLinkerProperties); ok {
Jingwen Chen91220d72021-03-24 02:18:33 -040082 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
83 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +000084 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
85 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
86 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040087 // os + arch base compiler props
88 for _, archProps := range osProps.ArchProperties {
89 if baseLinkerProps, ok := archProps.(*BaseLinkerProperties); ok {
90 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
91 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
92 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
93 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
94 }
95 }
Jingwen Chened9c17d2021-04-13 07:14:55 +000096 }
97
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040098 for _, props := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) {
Jingwen Chened9c17d2021-04-13 07:14:55 +000099 // arch specific linker props
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400100 if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000101 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
102 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
103 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
104 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
Jingwen Chen91220d72021-03-24 02:18:33 -0400105 }
106 }
107
Jingwen Chen53681ef2021-04-29 08:15:13 +0000108 // Deps in the static: { .. } and shared: { .. } props of a cc_library.
109 if lib, ok := module.compiler.(*libraryDecorator); ok {
Jingwen Chenbcf53042021-05-26 04:42:42 +0000110 appendDeps := func(deps []string, p StaticOrSharedProperties) []string {
111 deps = append(deps, p.Static_libs...)
112 deps = append(deps, p.Whole_static_libs...)
113 deps = append(deps, p.Shared_libs...)
114 return deps
115 }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000116
Jingwen Chenbcf53042021-05-26 04:42:42 +0000117 allDeps = appendDeps(allDeps, lib.SharedProperties.Shared)
118 allDeps = appendDeps(allDeps, lib.StaticProperties.Static)
Jingwen Chen45dec102021-05-19 10:30:29 +0000119
120 // TODO(b/186024507, b/186489250): Temporarily exclude adding
121 // system_shared_libs deps until libc and libm builds.
122 // allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...)
123 // allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000124
125 // Deps in the target/arch nested static: { .. } and shared: { .. } props of a cc_library.
126 // target: { <target>: shared: { ... } }
127 for _, targetProps := range module.GetTargetProperties(ctx, &SharedProperties{}) {
128 if p, ok := targetProps.Properties.(*SharedProperties); ok {
129 allDeps = appendDeps(allDeps, p.Shared)
130 }
131 for _, archProperties := range targetProps.ArchProperties {
132 if p, ok := archProperties.(*SharedProperties); ok {
133 allDeps = appendDeps(allDeps, p.Shared)
134 }
135 }
136 }
137 // target: { <target>: static: { ... } }
138 for _, targetProps := range module.GetTargetProperties(ctx, &StaticProperties{}) {
139 if p, ok := targetProps.Properties.(*StaticProperties); ok {
140 allDeps = appendDeps(allDeps, p.Static)
141 }
142 for _, archProperties := range targetProps.ArchProperties {
143 if p, ok := archProperties.(*StaticProperties); ok {
144 allDeps = appendDeps(allDeps, p.Static)
145 }
146 }
147 }
148 // arch: { <arch>: shared: { ... } }
149 for _, properties := range module.GetArchProperties(ctx, &SharedProperties{}) {
150 if p, ok := properties.(*SharedProperties); ok {
151 allDeps = appendDeps(allDeps, p.Shared)
152 }
153 }
154 // arch: { <arch>: static: { ... } }
155 for _, properties := range module.GetArchProperties(ctx, &StaticProperties{}) {
156 if p, ok := properties.(*StaticProperties); ok {
157 allDeps = appendDeps(allDeps, p.Static)
158 }
159 }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000160 }
161
Jingwen Chen91220d72021-03-24 02:18:33 -0400162 ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...)
163}
164
Liz Kammer2222c6b2021-05-24 15:41:47 -0400165// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
Jingwen Chenbcf53042021-05-26 04:42:42 +0000166// properties which apply to either the shared or static version of a cc_library module.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400167type staticOrSharedAttributes struct {
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000168 srcs bazel.LabelListAttribute
169 srcs_c bazel.LabelListAttribute
170 srcs_as bazel.LabelListAttribute
171
172 copts bazel.StringListAttribute
173
Chris Parsons08648312021-05-06 16:23:19 -0400174 staticDeps bazel.LabelListAttribute
175 dynamicDeps bazel.LabelListAttribute
176 wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +0000177}
178
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000179func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) (cppSrcs, cSrcs, asSrcs bazel.LabelListAttribute) {
180 // Branch srcs into three language-specific groups.
181 // C++ is the "catch-all" group, and comprises generated sources because we don't
182 // know the language of these sources until the genrule is executed.
183 // TODO(b/190006308): Handle language detection of sources in a Bazel rule.
184 isCSrcOrFilegroup := func(s string) bool {
185 return strings.HasSuffix(s, ".c") || strings.HasSuffix(s, "_c_srcs")
186 }
187
188 isAsmSrcOrFilegroup := func(s string) bool {
189 return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s") || strings.HasSuffix(s, "_as_srcs")
190 }
191
192 // Check that a module is a filegroup type named <label>.
193 isFilegroupNamed := func(m android.Module, fullLabel string) bool {
194 if ctx.OtherModuleType(m) != "filegroup" {
195 return false
196 }
197 labelParts := strings.Split(fullLabel, ":")
198 if len(labelParts) > 2 {
199 // There should not be more than one colon in a label.
200 panic(fmt.Errorf("%s is not a valid Bazel label for a filegroup", fullLabel))
201 } else {
202 return m.Name() == labelParts[len(labelParts)-1]
203 }
204 }
205
206 // Convert the filegroup dependencies into the extension-specific filegroups
207 // filtered in the filegroup.bzl macro.
208 cppFilegroup := func(label string) string {
209 ctx.VisitDirectDeps(func(m android.Module) {
210 if isFilegroupNamed(m, label) {
211 label = label + "_cpp_srcs"
212 return
213 }
214 })
215 return label
216 }
217 cFilegroup := func(label string) string {
218 ctx.VisitDirectDeps(func(m android.Module) {
219 if isFilegroupNamed(m, label) {
220 label = label + "_c_srcs"
221 return
222 }
223 })
224 return label
225 }
226 asFilegroup := func(label string) string {
227 ctx.VisitDirectDeps(func(m android.Module) {
228 if isFilegroupNamed(m, label) {
229 label = label + "_as_srcs"
230 return
231 }
232 })
233 return label
234 }
235
236 cSrcs = bazel.MapLabelListAttribute(srcs, cFilegroup)
237 cSrcs = bazel.FilterLabelListAttribute(cSrcs, isCSrcOrFilegroup)
238
239 asSrcs = bazel.MapLabelListAttribute(srcs, asFilegroup)
240 asSrcs = bazel.FilterLabelListAttribute(asSrcs, isAsmSrcOrFilegroup)
241
242 cppSrcs = bazel.MapLabelListAttribute(srcs, cppFilegroup)
243 cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, cSrcs)
244 cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, asSrcs)
245 return
246}
247
Jingwen Chen53681ef2021-04-29 08:15:13 +0000248// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400249func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000250 lib, ok := module.compiler.(*libraryDecorator)
251 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400252 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000253 }
254
Jingwen Chenbcf53042021-05-26 04:42:42 +0000255 return bp2buildParseStaticOrSharedProps(ctx, module, lib, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000256}
257
258// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400259func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000260 lib, ok := module.compiler.(*libraryDecorator)
261 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400262 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000263 }
264
Jingwen Chenbcf53042021-05-26 04:42:42 +0000265 return bp2buildParseStaticOrSharedProps(ctx, module, lib, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400266}
267
Jingwen Chenbcf53042021-05-26 04:42:42 +0000268func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes {
269 var props StaticOrSharedProperties
270 if isStatic {
271 props = lib.StaticProperties.Static
272 } else {
273 props = lib.SharedProperties.Shared
Jingwen Chen53681ef2021-04-29 08:15:13 +0000274 }
Jingwen Chenbcf53042021-05-26 04:42:42 +0000275
276 attrs := staticOrSharedAttributes{
277 copts: bazel.StringListAttribute{Value: props.Cflags},
278 srcs: bazel.LabelListAttribute{Value: android.BazelLabelForModuleSrc(ctx, props.Srcs)},
279 staticDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Static_libs)},
280 dynamicDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Shared_libs)},
281 wholeArchiveDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)},
282 }
283
284 setArchAttrs := func(arch string, props StaticOrSharedProperties) {
285 attrs.copts.SetValueForArch(arch, props.Cflags)
286 attrs.srcs.SetValueForArch(arch, android.BazelLabelForModuleSrc(ctx, props.Srcs))
287 attrs.staticDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Static_libs))
288 attrs.dynamicDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Shared_libs))
289 attrs.wholeArchiveDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs))
290 }
291
292 setTargetAttrs := func(target string, props StaticOrSharedProperties) {
293 attrs.copts.SetOsValueForTarget(target, props.Cflags)
294 attrs.srcs.SetOsValueForTarget(target, android.BazelLabelForModuleSrc(ctx, props.Srcs))
295 attrs.staticDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Static_libs))
296 attrs.dynamicDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Shared_libs))
297 attrs.wholeArchiveDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs))
298 }
299
300 setTargetArchAttrs := func(target, arch string, props StaticOrSharedProperties) {
301 attrs.copts.SetOsArchValueForTarget(target, arch, props.Cflags)
302 attrs.srcs.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleSrc(ctx, props.Srcs))
303 attrs.staticDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Static_libs))
304 attrs.dynamicDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Shared_libs))
305 attrs.wholeArchiveDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs))
306 }
307
308 if isStatic {
309 for arch, properties := range module.GetArchProperties(ctx, &StaticProperties{}) {
310 if staticOrSharedProps, ok := properties.(*StaticProperties); ok {
311 setArchAttrs(arch.Name, staticOrSharedProps.Static)
312 }
313 }
314 for target, p := range module.GetTargetProperties(ctx, &StaticProperties{}) {
315 if staticOrSharedProps, ok := p.Properties.(*StaticProperties); ok {
316 setTargetAttrs(target.Name, staticOrSharedProps.Static)
317 }
318 for arch, archProperties := range p.ArchProperties {
319 if staticOrSharedProps, ok := archProperties.(*StaticProperties); ok {
320 setTargetArchAttrs(target.Name, arch.Name, staticOrSharedProps.Static)
321 }
322 }
323 }
324 } else {
325 for arch, p := range module.GetArchProperties(ctx, &SharedProperties{}) {
326 if staticOrSharedProps, ok := p.(*SharedProperties); ok {
327 setArchAttrs(arch.Name, staticOrSharedProps.Shared)
328 }
329 }
330 for target, p := range module.GetTargetProperties(ctx, &SharedProperties{}) {
331 if staticOrSharedProps, ok := p.Properties.(*SharedProperties); ok {
332 setTargetAttrs(target.Name, staticOrSharedProps.Shared)
333 }
334 for arch, archProperties := range p.ArchProperties {
335 if staticOrSharedProps, ok := archProperties.(*SharedProperties); ok {
336 setTargetArchAttrs(target.Name, arch.Name, staticOrSharedProps.Shared)
337 }
338 }
339 }
340 }
341
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000342 cppSrcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, attrs.srcs)
343 attrs.srcs = cppSrcs
344 attrs.srcs_c = cSrcs
345 attrs.srcs_as = asSrcs
346
Jingwen Chenbcf53042021-05-26 04:42:42 +0000347 return attrs
Jingwen Chen53681ef2021-04-29 08:15:13 +0000348}
349
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400350// Convenience struct to hold all attributes parsed from prebuilt properties.
351type prebuiltAttributes struct {
352 Src bazel.LabelAttribute
353}
354
355func Bp2BuildParsePrebuiltLibraryProps(ctx android.TopDownMutatorContext, module *Module) prebuiltAttributes {
356 prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker)
357 prebuiltLinker := prebuiltLibraryLinker.prebuiltLinker
358
359 var srcLabelAttribute bazel.LabelAttribute
360
361 if len(prebuiltLinker.properties.Srcs) > 1 {
362 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file\n")
363 }
364
365 if len(prebuiltLinker.properties.Srcs) == 1 {
366 srcLabelAttribute.Value = android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinker.properties.Srcs[0])
367 for arch, props := range module.GetArchProperties(ctx, &prebuiltLinkerProperties{}) {
368 if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok {
369 if len(prebuiltLinkerProperties.Srcs) > 1 {
370 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for arch %s\n", arch.Name)
371 }
372 if len(prebuiltLinkerProperties.Srcs) == 1 {
373 srcLabelAttribute.SetValueForArch(arch.Name, android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0]))
374 }
375 }
376 }
377 }
378
379 for os, targetProperties := range module.GetTargetProperties(ctx, &prebuiltLinkerProperties{}) {
380 if prebuiltLinkerProperties, ok := targetProperties.Properties.(*prebuiltLinkerProperties); ok {
381 if len(prebuiltLinkerProperties.Srcs) > 1 {
382 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for os %s\n", os.Name)
383
384 }
385
386 if len(prebuiltLinkerProperties.Srcs) == 1 {
387 srcLabelAttribute.SetOsValueForTarget(os.Name, android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0]))
388 }
389 }
390 for arch, archProperties := range targetProperties.ArchProperties {
391 if prebuiltLinkerProperties, ok := archProperties.(*prebuiltLinkerProperties); ok {
392 if len(prebuiltLinkerProperties.Srcs) > 1 {
393 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for os_arch %s_%s\n", os.Name, arch.Name)
394
395 }
396
397 if len(prebuiltLinkerProperties.Srcs) == 1 {
398 srcLabelAttribute.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0]))
399 }
400 }
401
402 }
403 }
404
405 return prebuiltAttributes{
406 Src: srcLabelAttribute,
407 }
408}
409
Jingwen Chen107c0de2021-04-09 10:43:12 +0000410// Convenience struct to hold all attributes parsed from compiler properties.
411type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400412 // Options for all languages
413 copts bazel.StringListAttribute
414 // Assembly options and sources
415 asFlags bazel.StringListAttribute
416 asSrcs bazel.LabelListAttribute
417 // C options and sources
418 conlyFlags bazel.StringListAttribute
419 cSrcs bazel.LabelListAttribute
420 // C++ options and sources
421 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000422 srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000423}
424
Jingwen Chen63930982021-03-24 10:04:33 -0400425// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000426func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
Jingwen Chen882bcc12021-04-27 05:54:20 +0000427 var srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000428 var copts bazel.StringListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400429 var asFlags bazel.StringListAttribute
430 var conlyFlags bazel.StringListAttribute
431 var cppFlags bazel.StringListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400432
Chris Parsons484e50a2021-05-13 15:13:04 -0400433 // Creates the -I flags for a directory, while making the directory relative
Jingwen Chened9c17d2021-04-13 07:14:55 +0000434 // to the exec root for Bazel to work.
Chris Parsons484e50a2021-05-13 15:13:04 -0400435 includeFlags := func(dir string) []string {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000436 // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements.
Chris Parsons484e50a2021-05-13 15:13:04 -0400437 moduleDirRootedPath := filepath.Join(ctx.ModuleDir(), dir)
438 return []string{
439 "-I" + moduleDirRootedPath,
440 // Include the bindir-rooted path (using make variable substitution). This most
441 // closely matches Bazel's native include path handling, which allows for dependency
442 // on generated headers in these directories.
443 // TODO(b/188084383): Handle local include directories in Bazel.
444 "-I$(BINDIR)/" + moduleDirRootedPath,
445 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000446 }
447
Jingwen Chened9c17d2021-04-13 07:14:55 +0000448 // Parse the list of module-relative include directories (-I).
449 parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string {
450 // include_dirs are root-relative, not module-relative.
451 includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
452 return append(includeDirs, baseCompilerProps.Local_include_dirs...)
453 }
454
Chris Parsons990c4f42021-05-25 12:10:58 -0400455 parseCommandLineFlags := func(soongFlags []string) []string {
456 var result []string
457 for _, flag := range soongFlags {
Colin Cross52aa4e12021-05-25 15:20:39 +0000458 // Soong's cflags can contain spaces, like `-include header.h`. For
459 // Bazel's copts, split them up to be compatible with the
460 // no_copts_tokenization feature.
Chris Parsons990c4f42021-05-25 12:10:58 -0400461 result = append(result, strings.Split(flag, " ")...)
Colin Cross52aa4e12021-05-25 15:20:39 +0000462 }
Chris Parsons990c4f42021-05-25 12:10:58 -0400463 return result
464 }
465
466 // Parse the list of copts.
467 parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string {
468 var copts []string
469 copts = append(copts, parseCommandLineFlags(baseCompilerProps.Cflags)...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000470 for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
Chris Parsons484e50a2021-05-13 15:13:04 -0400471 copts = append(copts, includeFlags(dir)...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000472 }
473 return copts
Jingwen Chen63930982021-03-24 10:04:33 -0400474 }
475
Jingwen Chene32e9e02021-04-23 09:17:24 +0000476 // baseSrcs contain the list of src files that are used for every configuration.
477 var baseSrcs []string
478 // baseExcludeSrcs contain the list of src files that are excluded for every configuration.
479 var baseExcludeSrcs []string
480 // baseSrcsLabelList is a clone of the base srcs LabelList, used for computing the
481 // arch or os specific srcs later.
482 var baseSrcsLabelList bazel.LabelList
483
484 // Parse srcs from an arch or OS's props value, taking the base srcs and
485 // exclude srcs into account.
486 parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList {
487 // Combine the base srcs and arch-specific srcs
488 allSrcs := append(baseSrcs, baseCompilerProps.Srcs...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400489 // Add srcs-like dependencies such as generated files.
490 // First create a LabelList containing these dependencies, then merge the values with srcs.
491 generatedHdrsAndSrcs := baseCompilerProps.Generated_headers
492 generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...)
493
494 generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs)
495
Jingwen Chene32e9e02021-04-23 09:17:24 +0000496 // Combine the base exclude_srcs and configuration-specific exclude_srcs
497 allExcludeSrcs := append(baseExcludeSrcs, baseCompilerProps.Exclude_srcs...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400498 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, allSrcs, allExcludeSrcs)
499 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000500 }
501
Jingwen Chenc1c26502021-04-05 10:35:13 +0000502 for _, props := range module.compiler.compilerProps() {
503 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000504 srcs.Value = parseSrcs(baseCompilerProps)
505 copts.Value = parseCopts(baseCompilerProps)
Chris Parsons990c4f42021-05-25 12:10:58 -0400506 asFlags.Value = parseCommandLineFlags(baseCompilerProps.Asflags)
507 conlyFlags.Value = parseCommandLineFlags(baseCompilerProps.Conlyflags)
508 cppFlags.Value = parseCommandLineFlags(baseCompilerProps.Cppflags)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000509
510 // Used for arch-specific srcs later.
511 baseSrcs = baseCompilerProps.Srcs
Jingwen Chene32e9e02021-04-23 09:17:24 +0000512 baseSrcsLabelList = parseSrcs(baseCompilerProps)
Chris Parsons484e50a2021-05-13 15:13:04 -0400513 baseExcludeSrcs = baseCompilerProps.Exclude_srcs
Jingwen Chenc1c26502021-04-05 10:35:13 +0000514 break
515 }
516 }
517
Jingwen Chene32e9e02021-04-23 09:17:24 +0000518 // Handle include_build_directory prop. If the property is true, then the
519 // target has access to all headers recursively in the package, and has
520 // "-I<module-dir>" in its copts.
Jingwen Chened9c17d2021-04-13 07:14:55 +0000521 if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400522 copts.Value = append(copts.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000523 } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400524 copts.Value = append(copts.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000525 }
526
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200527 for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000528 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000529 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
530 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
531 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
532 srcsList := parseSrcs(baseCompilerProps)
533 srcs.SetValueForArch(arch.Name, srcsList)
534 // The base srcs value should not contain any arch-specific excludes.
535 srcs.Value = bazel.SubtractBazelLabelList(srcs.Value, bazel.LabelList{Includes: srcsList.Excludes})
536 }
537
Jingwen Chened9c17d2021-04-13 07:14:55 +0000538 copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps))
Chris Parsons990c4f42021-05-25 12:10:58 -0400539 asFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Asflags))
540 conlyFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags))
541 cppFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Cppflags))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000542 }
543 }
544
Jingwen Chene32e9e02021-04-23 09:17:24 +0000545 // After going through all archs, delete the duplicate files in the arch
546 // values that are already in the base srcs.Value.
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200547 for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000548 if _, ok := props.(*BaseCompilerProperties); ok {
549 srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcs.GetValueForArch(arch.Name), srcs.Value))
550 }
551 }
552
553 // Now that the srcs.Value list is finalized, compare it with the original
554 // list, and put the difference into the default condition for the arch
555 // select.
556 defaultsSrcs := bazel.SubtractBazelLabelList(baseSrcsLabelList, srcs.Value)
557 // TODO(b/186153868): handle the case with multiple variant types, e.g. when arch and os are both used.
558 srcs.SetValueForArch(bazel.CONDITIONS_DEFAULT, defaultsSrcs)
559
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400560 // Handle target specific properties.
561 for os, osProps := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) {
562 if baseCompilerProps, ok := osProps.Properties.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000563 srcsList := parseSrcs(baseCompilerProps)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000564 // TODO(b/186153868): add support for os-specific srcs and exclude_srcs
Liz Kammer2b07ec72021-05-26 15:08:27 -0400565 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
566 srcs.SetOsValueForTarget(os.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList))
567 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400568 copts.SetOsValueForTarget(os.Name, parseCopts(baseCompilerProps))
569 asFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Asflags))
570 conlyFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags))
571 cppFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Cppflags))
572 }
573 for arch, archProps := range osProps.ArchProperties {
574 if baseCompilerProps, ok := archProps.(*BaseCompilerProperties); ok {
575 srcsList := parseSrcs(baseCompilerProps)
576 // TODO(b/186153868): add support for os-specific srcs and exclude_srcs
Liz Kammer2b07ec72021-05-26 15:08:27 -0400577 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
578 srcs.SetOsArchValueForTarget(os.Name, arch.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList))
579 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400580 copts.SetOsArchValueForTarget(os.Name, arch.Name, parseCopts(baseCompilerProps))
581 asFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Asflags))
582 conlyFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags))
583 cppFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Cppflags))
584 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000585 }
586 }
587
Liz Kammerba7a9c52021-05-26 08:45:30 -0400588 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
589 "Cflags": &copts,
590 "Asflags": &asFlags,
591 "CppFlags": &cppFlags,
592 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400593 productVariableProps := android.ProductVariableProperties(ctx)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400594 for propName, attr := range productVarPropNameToAttribute {
595 if props, exists := productVariableProps[propName]; exists {
596 for _, prop := range props {
597 flags, ok := prop.Property.([]string)
598 if !ok {
599 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
600 }
601 newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable)
602 attr.ProductValues = append(attr.ProductValues, bazel.ProductVariableValues{
603 ProductVariable: prop.ProductConfigVariable,
604 Values: newFlags,
605 })
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400606 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400607 }
608 }
609
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000610 srcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, srcs)
611
Jingwen Chen107c0de2021-04-09 10:43:12 +0000612 return compilerAttributes{
Chris Parsons990c4f42021-05-25 12:10:58 -0400613 copts: copts,
614 srcs: srcs,
615 asFlags: asFlags,
616 asSrcs: asSrcs,
617 cSrcs: cSrcs,
618 conlyFlags: conlyFlags,
619 cppFlags: cppFlags,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000620 }
621}
622
623// Convenience struct to hold all attributes parsed from linker properties.
624type linkerAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400625 deps bazel.LabelListAttribute
626 dynamicDeps bazel.LabelListAttribute
627 wholeArchiveDeps bazel.LabelListAttribute
Chris Parsonsd6358772021-05-18 18:35:24 -0400628 exportedDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400629 linkopts bazel.StringListAttribute
630 versionScript bazel.LabelAttribute
Jingwen Chenc1c26502021-04-05 10:35:13 +0000631}
632
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400633// FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here
634func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string {
635 flags := linkerProperties.Ldflags
636 if !BoolDefault(linkerProperties.Pack_relocations, true) {
637 flags = append(flags, "-Wl,--pack-dyn-relocs=none")
638 }
639 return flags
640}
641
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200642// bp2BuildParseLinkerProps parses the linker properties of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400643// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000644func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Jingwen Chen63930982021-03-24 10:04:33 -0400645 var deps bazel.LabelListAttribute
Chris Parsonsd6358772021-05-18 18:35:24 -0400646 var exportedDeps bazel.LabelListAttribute
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400647 var dynamicDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400648 var wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400649 var linkopts bazel.StringListAttribute
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200650 var versionScript bazel.LabelAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400651
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400652 getLibs := func(baseLinkerProps *BaseLinkerProperties) []string {
653 libs := baseLinkerProps.Header_libs
654 libs = append(libs, baseLinkerProps.Static_libs...)
655 libs = android.SortedUniqueStrings(libs)
656 return libs
657 }
658
Jingwen Chen91220d72021-03-24 02:18:33 -0400659 for _, linkerProps := range module.linker.linkerProps() {
660 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400661 libs := getLibs(baseLinkerProps)
Chris Parsonsd6358772021-05-18 18:35:24 -0400662 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400663 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chened9c17d2021-04-13 07:14:55 +0000664 deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs))
Chris Parsonsd6358772021-05-18 18:35:24 -0400665 exportedDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400666 linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps)
Chris Parsons08648312021-05-06 16:23:19 -0400667 wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200668
669 if baseLinkerProps.Version_script != nil {
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200670 versionScript.Value = android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200671 }
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400672
673 sharedLibs := baseLinkerProps.Shared_libs
674 dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, sharedLibs))
675
Jingwen Chen91220d72021-03-24 02:18:33 -0400676 break
677 }
678 }
679
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400680 for arch, props := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) {
681 if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
682 libs := getLibs(baseLinkerProps)
Chris Parsonsd6358772021-05-18 18:35:24 -0400683 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400684 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chen63930982021-03-24 10:04:33 -0400685 deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
Chris Parsonsd6358772021-05-18 18:35:24 -0400686 exportedDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400687 linkopts.SetValueForArch(arch.Name, getBp2BuildLinkerFlags(baseLinkerProps))
Chris Parsons08648312021-05-06 16:23:19 -0400688 wholeArchiveDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400689
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200690 if baseLinkerProps.Version_script != nil {
691 versionScript.SetValueForArch(arch.Name,
692 android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
693 }
Rupert Shuttleworth3b413d32021-05-10 18:41:51 -0400694
695 sharedLibs := baseLinkerProps.Shared_libs
696 dynamicDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
Jingwen Chen63930982021-03-24 10:04:33 -0400697 }
698 }
699
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400700 for os, targetProperties := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) {
701 if baseLinkerProps, ok := targetProperties.Properties.(*BaseLinkerProperties); ok {
702 libs := getLibs(baseLinkerProps)
Chris Parsonsd6358772021-05-18 18:35:24 -0400703 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400704 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400705 wholeArchiveDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
706 deps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
707 exportedDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400708
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400709 linkopts.SetOsValueForTarget(os.Name, getBp2BuildLinkerFlags(baseLinkerProps))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400710
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400711 if baseLinkerProps.Version_script != nil {
712 versionScript.SetOsValueForTarget(os.Name, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
713 }
714
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400715 sharedLibs := baseLinkerProps.Shared_libs
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400716 dynamicDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
717 }
718 for arch, archProperties := range targetProperties.ArchProperties {
719 if baseLinkerProps, ok := archProperties.(*BaseLinkerProperties); ok {
720 libs := getLibs(baseLinkerProps)
721 exportedLibs := baseLinkerProps.Export_header_lib_headers
722 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
723 wholeArchiveDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
724 deps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
725 exportedDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs))
726
727 linkopts.SetOsArchValueForTarget(os.Name, arch.Name, getBp2BuildLinkerFlags(baseLinkerProps))
728
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400729 if baseLinkerProps.Version_script != nil {
730 versionScript.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
731 }
732
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400733 sharedLibs := baseLinkerProps.Shared_libs
734 dynamicDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
735 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400736 }
737 }
738
Jingwen Chen107c0de2021-04-09 10:43:12 +0000739 return linkerAttributes{
Chris Parsons08648312021-05-06 16:23:19 -0400740 deps: deps,
Chris Parsonsd6358772021-05-18 18:35:24 -0400741 exportedDeps: exportedDeps,
Chris Parsons08648312021-05-06 16:23:19 -0400742 dynamicDeps: dynamicDeps,
743 wholeArchiveDeps: wholeArchiveDeps,
744 linkopts: linkopts,
745 versionScript: versionScript,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000746 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400747}
748
Jingwen Chened9c17d2021-04-13 07:14:55 +0000749// Relativize a list of root-relative paths with respect to the module's
750// directory.
751//
752// include_dirs Soong prop are root-relative (b/183742505), but
753// local_include_dirs, export_include_dirs and export_system_include_dirs are
754// module dir relative. This function makes a list of paths entirely module dir
755// relative.
756//
757// For the `include` attribute, Bazel wants the paths to be relative to the
758// module.
759func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000760 var relativePaths []string
761 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000762 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
763 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
764 if err != nil {
765 panic(err)
766 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000767 relativePaths = append(relativePaths, relativePath)
768 }
769 return relativePaths
770}
771
Jingwen Chen882bcc12021-04-27 05:54:20 +0000772func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
Jingwen Chen91220d72021-03-24 02:18:33 -0400773 libraryDecorator := module.linker.(*libraryDecorator)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400774 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
775}
Jingwen Chen91220d72021-03-24 02:18:33 -0400776
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400777func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
778 prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker)
779 libraryDecorator := prebuiltLibraryLinker.libraryDecorator
780 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
781}
782
783// bp2BuildParseExportedIncludes creates a string list attribute contains the
784// exported included directories of a module.
785func bp2BuildParseExportedIncludesHelper(ctx android.TopDownMutatorContext, module *Module, libraryDecorator *libraryDecorator) bazel.StringListAttribute {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000786 // Export_system_include_dirs and export_include_dirs are already module dir
787 // relative, so they don't need to be relativized like include_dirs, which
788 // are root-relative.
Jingwen Chen91220d72021-03-24 02:18:33 -0400789 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
790 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000791 includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
Jingwen Chen91220d72021-03-24 02:18:33 -0400792
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400793 getVariantIncludeDirs := func(includeDirs []string, flagExporterProperties *FlagExporterProperties) []string {
794 variantIncludeDirs := flagExporterProperties.Export_system_include_dirs
795 variantIncludeDirs = append(variantIncludeDirs, flagExporterProperties.Export_include_dirs...)
796
797 // To avoid duplicate includes when base includes + arch includes are combined
798 // TODO: This doesn't take conflicts between arch and os includes into account
799 variantIncludeDirs = bazel.SubtractStrings(variantIncludeDirs, includeDirs)
800 return variantIncludeDirs
801 }
802
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200803 for arch, props := range module.GetArchProperties(ctx, &FlagExporterProperties{}) {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000804 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400805 archIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000806 if len(archIncludeDirs) > 0 {
807 includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs)
808 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000809 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400810 }
811
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400812 for os, targetProperties := range module.GetTargetProperties(ctx, &FlagExporterProperties{}) {
813 if flagExporterProperties, ok := targetProperties.Properties.(*FlagExporterProperties); ok {
814 targetIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties)
815 if len(targetIncludeDirs) > 0 {
816 includeDirsAttribute.SetOsValueForTarget(os.Name, targetIncludeDirs)
817 }
818 }
819 for arch, archProperties := range targetProperties.ArchProperties {
820 if flagExporterProperties, ok := archProperties.(*FlagExporterProperties); ok {
821 targetIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties)
822 if len(targetIncludeDirs) > 0 {
823 includeDirsAttribute.SetOsArchValueForTarget(os.Name, arch.Name, targetIncludeDirs)
824 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400825 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400826 }
827 }
828
Jingwen Chen882bcc12021-04-27 05:54:20 +0000829 return includeDirsAttribute
Jingwen Chen91220d72021-03-24 02:18:33 -0400830}