blob: fed993670508c69540e9d9c1afd7e609d4621b50 [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 Chened9c17d2021-04-13 07:14:55 +000017 "path/filepath"
Jingwen Chen3950cd62021-05-12 04:33:00 +000018 "strings"
Chris Parsons484e50a2021-05-13 15:13:04 -040019
20 "android/soong/android"
21 "android/soong/bazel"
Liz Kammerba7a9c52021-05-26 08:45:30 -040022
23 "github.com/google/blueprint/proptools"
Jingwen Chen91220d72021-03-24 02:18:33 -040024)
25
26// bp2build functions and helpers for converting cc_* modules to Bazel.
27
28func init() {
29 android.DepsBp2BuildMutators(RegisterDepsBp2Build)
30}
31
32func RegisterDepsBp2Build(ctx android.RegisterMutatorsContext) {
33 ctx.BottomUp("cc_bp2build_deps", depsBp2BuildMutator)
34}
35
36// A naive deps mutator to add deps on all modules across all combinations of
37// target props for cc modules. This is needed to make module -> bazel label
38// resolution work in the bp2build mutator later. This is probably
39// the wrong way to do it, but it works.
40//
41// TODO(jingwen): can we create a custom os mutator in depsBp2BuildMutator to do this?
42func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) {
43 module, ok := ctx.Module().(*Module)
44 if !ok {
45 // Not a cc module
46 return
47 }
48
49 if !module.ConvertWithBp2build(ctx) {
50 return
51 }
52
53 var allDeps []string
54
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040055 for _, osProps := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) {
56 // os base compiler props
57 if baseCompilerProps, ok := osProps.Properties.(*BaseCompilerProperties); ok {
Chris Parsons484e50a2021-05-13 15:13:04 -040058 allDeps = append(allDeps, baseCompilerProps.Generated_headers...)
59 allDeps = append(allDeps, baseCompilerProps.Generated_sources...)
60 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040061 // os + arch base compiler props
62 for _, archProps := range osProps.ArchProperties {
63 if baseCompilerProps, ok := archProps.(*BaseCompilerProperties); ok {
64 allDeps = append(allDeps, baseCompilerProps.Generated_headers...)
65 allDeps = append(allDeps, baseCompilerProps.Generated_sources...)
66 }
67 }
Chris Parsons484e50a2021-05-13 15:13:04 -040068 }
69
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040070 for _, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Chris Parsons484e50a2021-05-13 15:13:04 -040071 // arch specific compiler props
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040072 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Chris Parsons484e50a2021-05-13 15:13:04 -040073 allDeps = append(allDeps, baseCompilerProps.Generated_headers...)
74 allDeps = append(allDeps, baseCompilerProps.Generated_sources...)
75 }
76 }
77
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040078 for _, osProps := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) {
79 // os specific linker props
80 if baseLinkerProps, ok := osProps.Properties.(*BaseLinkerProperties); ok {
Jingwen Chen91220d72021-03-24 02:18:33 -040081 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
82 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +000083 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
84 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
85 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040086 // os + arch base compiler props
87 for _, archProps := range osProps.ArchProperties {
88 if baseLinkerProps, ok := archProps.(*BaseLinkerProperties); ok {
89 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
90 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
91 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
92 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
93 }
94 }
Jingwen Chened9c17d2021-04-13 07:14:55 +000095 }
96
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040097 for _, props := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) {
Jingwen Chened9c17d2021-04-13 07:14:55 +000098 // arch specific linker props
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040099 if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000100 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
101 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
102 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
103 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
Jingwen Chen91220d72021-03-24 02:18:33 -0400104 }
105 }
106
Jingwen Chen53681ef2021-04-29 08:15:13 +0000107 // Deps in the static: { .. } and shared: { .. } props of a cc_library.
108 if lib, ok := module.compiler.(*libraryDecorator); ok {
Jingwen Chenbcf53042021-05-26 04:42:42 +0000109 appendDeps := func(deps []string, p StaticOrSharedProperties) []string {
110 deps = append(deps, p.Static_libs...)
111 deps = append(deps, p.Whole_static_libs...)
112 deps = append(deps, p.Shared_libs...)
113 return deps
114 }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000115
Jingwen Chenbcf53042021-05-26 04:42:42 +0000116 allDeps = appendDeps(allDeps, lib.SharedProperties.Shared)
117 allDeps = appendDeps(allDeps, lib.StaticProperties.Static)
Jingwen Chen45dec102021-05-19 10:30:29 +0000118
119 // TODO(b/186024507, b/186489250): Temporarily exclude adding
120 // system_shared_libs deps until libc and libm builds.
121 // allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...)
122 // allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000123
124 // Deps in the target/arch nested static: { .. } and shared: { .. } props of a cc_library.
125 // target: { <target>: shared: { ... } }
126 for _, targetProps := range module.GetTargetProperties(ctx, &SharedProperties{}) {
127 if p, ok := targetProps.Properties.(*SharedProperties); ok {
128 allDeps = appendDeps(allDeps, p.Shared)
129 }
130 for _, archProperties := range targetProps.ArchProperties {
131 if p, ok := archProperties.(*SharedProperties); ok {
132 allDeps = appendDeps(allDeps, p.Shared)
133 }
134 }
135 }
136 // target: { <target>: static: { ... } }
137 for _, targetProps := range module.GetTargetProperties(ctx, &StaticProperties{}) {
138 if p, ok := targetProps.Properties.(*StaticProperties); ok {
139 allDeps = appendDeps(allDeps, p.Static)
140 }
141 for _, archProperties := range targetProps.ArchProperties {
142 if p, ok := archProperties.(*StaticProperties); ok {
143 allDeps = appendDeps(allDeps, p.Static)
144 }
145 }
146 }
147 // arch: { <arch>: shared: { ... } }
148 for _, properties := range module.GetArchProperties(ctx, &SharedProperties{}) {
149 if p, ok := properties.(*SharedProperties); ok {
150 allDeps = appendDeps(allDeps, p.Shared)
151 }
152 }
153 // arch: { <arch>: static: { ... } }
154 for _, properties := range module.GetArchProperties(ctx, &StaticProperties{}) {
155 if p, ok := properties.(*StaticProperties); ok {
156 allDeps = appendDeps(allDeps, p.Static)
157 }
158 }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000159 }
160
Jingwen Chen91220d72021-03-24 02:18:33 -0400161 ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...)
162}
163
Liz Kammer2222c6b2021-05-24 15:41:47 -0400164// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
Jingwen Chenbcf53042021-05-26 04:42:42 +0000165// properties which apply to either the shared or static version of a cc_library module.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400166type staticOrSharedAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400167 copts bazel.StringListAttribute
168 srcs bazel.LabelListAttribute
169 staticDeps bazel.LabelListAttribute
170 dynamicDeps bazel.LabelListAttribute
171 wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +0000172}
173
174// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400175func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000176 lib, ok := module.compiler.(*libraryDecorator)
177 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400178 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000179 }
180
Jingwen Chenbcf53042021-05-26 04:42:42 +0000181 return bp2buildParseStaticOrSharedProps(ctx, module, lib, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000182}
183
184// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400185func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000186 lib, ok := module.compiler.(*libraryDecorator)
187 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400188 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000189 }
190
Jingwen Chenbcf53042021-05-26 04:42:42 +0000191 return bp2buildParseStaticOrSharedProps(ctx, module, lib, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400192}
193
Jingwen Chenbcf53042021-05-26 04:42:42 +0000194func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes {
195 var props StaticOrSharedProperties
196 if isStatic {
197 props = lib.StaticProperties.Static
198 } else {
199 props = lib.SharedProperties.Shared
Jingwen Chen53681ef2021-04-29 08:15:13 +0000200 }
Jingwen Chenbcf53042021-05-26 04:42:42 +0000201
202 attrs := staticOrSharedAttributes{
203 copts: bazel.StringListAttribute{Value: props.Cflags},
204 srcs: bazel.LabelListAttribute{Value: android.BazelLabelForModuleSrc(ctx, props.Srcs)},
205 staticDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Static_libs)},
206 dynamicDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Shared_libs)},
207 wholeArchiveDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)},
208 }
209
210 setArchAttrs := func(arch string, props StaticOrSharedProperties) {
211 attrs.copts.SetValueForArch(arch, props.Cflags)
212 attrs.srcs.SetValueForArch(arch, android.BazelLabelForModuleSrc(ctx, props.Srcs))
213 attrs.staticDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Static_libs))
214 attrs.dynamicDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Shared_libs))
215 attrs.wholeArchiveDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs))
216 }
217
218 setTargetAttrs := func(target string, props StaticOrSharedProperties) {
219 attrs.copts.SetOsValueForTarget(target, props.Cflags)
220 attrs.srcs.SetOsValueForTarget(target, android.BazelLabelForModuleSrc(ctx, props.Srcs))
221 attrs.staticDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Static_libs))
222 attrs.dynamicDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Shared_libs))
223 attrs.wholeArchiveDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs))
224 }
225
226 setTargetArchAttrs := func(target, arch string, props StaticOrSharedProperties) {
227 attrs.copts.SetOsArchValueForTarget(target, arch, props.Cflags)
228 attrs.srcs.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleSrc(ctx, props.Srcs))
229 attrs.staticDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Static_libs))
230 attrs.dynamicDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Shared_libs))
231 attrs.wholeArchiveDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs))
232 }
233
234 if isStatic {
235 for arch, properties := range module.GetArchProperties(ctx, &StaticProperties{}) {
236 if staticOrSharedProps, ok := properties.(*StaticProperties); ok {
237 setArchAttrs(arch.Name, staticOrSharedProps.Static)
238 }
239 }
240 for target, p := range module.GetTargetProperties(ctx, &StaticProperties{}) {
241 if staticOrSharedProps, ok := p.Properties.(*StaticProperties); ok {
242 setTargetAttrs(target.Name, staticOrSharedProps.Static)
243 }
244 for arch, archProperties := range p.ArchProperties {
245 if staticOrSharedProps, ok := archProperties.(*StaticProperties); ok {
246 setTargetArchAttrs(target.Name, arch.Name, staticOrSharedProps.Static)
247 }
248 }
249 }
250 } else {
251 for arch, p := range module.GetArchProperties(ctx, &SharedProperties{}) {
252 if staticOrSharedProps, ok := p.(*SharedProperties); ok {
253 setArchAttrs(arch.Name, staticOrSharedProps.Shared)
254 }
255 }
256 for target, p := range module.GetTargetProperties(ctx, &SharedProperties{}) {
257 if staticOrSharedProps, ok := p.Properties.(*SharedProperties); ok {
258 setTargetAttrs(target.Name, staticOrSharedProps.Shared)
259 }
260 for arch, archProperties := range p.ArchProperties {
261 if staticOrSharedProps, ok := archProperties.(*SharedProperties); ok {
262 setTargetArchAttrs(target.Name, arch.Name, staticOrSharedProps.Shared)
263 }
264 }
265 }
266 }
267
268 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 {
287 srcLabelAttribute.Value = android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinker.properties.Srcs[0])
288 for arch, props := range module.GetArchProperties(ctx, &prebuiltLinkerProperties{}) {
289 if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok {
290 if len(prebuiltLinkerProperties.Srcs) > 1 {
291 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for arch %s\n", arch.Name)
292 }
293 if len(prebuiltLinkerProperties.Srcs) == 1 {
294 srcLabelAttribute.SetValueForArch(arch.Name, android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0]))
295 }
296 }
297 }
298 }
299
300 for os, targetProperties := range module.GetTargetProperties(ctx, &prebuiltLinkerProperties{}) {
301 if prebuiltLinkerProperties, ok := targetProperties.Properties.(*prebuiltLinkerProperties); ok {
302 if len(prebuiltLinkerProperties.Srcs) > 1 {
303 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for os %s\n", os.Name)
304
305 }
306
307 if len(prebuiltLinkerProperties.Srcs) == 1 {
308 srcLabelAttribute.SetOsValueForTarget(os.Name, android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0]))
309 }
310 }
311 for arch, archProperties := range targetProperties.ArchProperties {
312 if prebuiltLinkerProperties, ok := archProperties.(*prebuiltLinkerProperties); ok {
313 if len(prebuiltLinkerProperties.Srcs) > 1 {
314 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for os_arch %s_%s\n", os.Name, arch.Name)
315
316 }
317
318 if len(prebuiltLinkerProperties.Srcs) == 1 {
319 srcLabelAttribute.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0]))
320 }
321 }
322
323 }
324 }
325
326 return prebuiltAttributes{
327 Src: srcLabelAttribute,
328 }
329}
330
Jingwen Chen107c0de2021-04-09 10:43:12 +0000331// Convenience struct to hold all attributes parsed from compiler properties.
332type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400333 // Options for all languages
334 copts bazel.StringListAttribute
335 // Assembly options and sources
336 asFlags bazel.StringListAttribute
337 asSrcs bazel.LabelListAttribute
338 // C options and sources
339 conlyFlags bazel.StringListAttribute
340 cSrcs bazel.LabelListAttribute
341 // C++ options and sources
342 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000343 srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000344}
345
Jingwen Chen63930982021-03-24 10:04:33 -0400346// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000347func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
Jingwen Chen882bcc12021-04-27 05:54:20 +0000348 var srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000349 var copts bazel.StringListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400350 var asFlags bazel.StringListAttribute
351 var conlyFlags bazel.StringListAttribute
352 var cppFlags bazel.StringListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400353
Chris Parsons484e50a2021-05-13 15:13:04 -0400354 // Creates the -I flags for a directory, while making the directory relative
Jingwen Chened9c17d2021-04-13 07:14:55 +0000355 // to the exec root for Bazel to work.
Chris Parsons484e50a2021-05-13 15:13:04 -0400356 includeFlags := func(dir string) []string {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000357 // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements.
Chris Parsons484e50a2021-05-13 15:13:04 -0400358 moduleDirRootedPath := filepath.Join(ctx.ModuleDir(), dir)
359 return []string{
360 "-I" + moduleDirRootedPath,
361 // Include the bindir-rooted path (using make variable substitution). This most
362 // closely matches Bazel's native include path handling, which allows for dependency
363 // on generated headers in these directories.
364 // TODO(b/188084383): Handle local include directories in Bazel.
365 "-I$(BINDIR)/" + moduleDirRootedPath,
366 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000367 }
368
Jingwen Chened9c17d2021-04-13 07:14:55 +0000369 // Parse the list of module-relative include directories (-I).
370 parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string {
371 // include_dirs are root-relative, not module-relative.
372 includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
373 return append(includeDirs, baseCompilerProps.Local_include_dirs...)
374 }
375
Chris Parsons990c4f42021-05-25 12:10:58 -0400376 parseCommandLineFlags := func(soongFlags []string) []string {
377 var result []string
378 for _, flag := range soongFlags {
Colin Cross52aa4e12021-05-25 15:20:39 +0000379 // Soong's cflags can contain spaces, like `-include header.h`. For
380 // Bazel's copts, split them up to be compatible with the
381 // no_copts_tokenization feature.
Chris Parsons990c4f42021-05-25 12:10:58 -0400382 result = append(result, strings.Split(flag, " ")...)
Colin Cross52aa4e12021-05-25 15:20:39 +0000383 }
Chris Parsons990c4f42021-05-25 12:10:58 -0400384 return result
385 }
386
387 // Parse the list of copts.
388 parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string {
389 var copts []string
390 copts = append(copts, parseCommandLineFlags(baseCompilerProps.Cflags)...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000391 for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
Chris Parsons484e50a2021-05-13 15:13:04 -0400392 copts = append(copts, includeFlags(dir)...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000393 }
394 return copts
Jingwen Chen63930982021-03-24 10:04:33 -0400395 }
396
Jingwen Chene32e9e02021-04-23 09:17:24 +0000397 // baseSrcs contain the list of src files that are used for every configuration.
398 var baseSrcs []string
399 // baseExcludeSrcs contain the list of src files that are excluded for every configuration.
400 var baseExcludeSrcs []string
401 // baseSrcsLabelList is a clone of the base srcs LabelList, used for computing the
402 // arch or os specific srcs later.
403 var baseSrcsLabelList bazel.LabelList
404
405 // Parse srcs from an arch or OS's props value, taking the base srcs and
406 // exclude srcs into account.
407 parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList {
408 // Combine the base srcs and arch-specific srcs
409 allSrcs := append(baseSrcs, baseCompilerProps.Srcs...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400410 // Add srcs-like dependencies such as generated files.
411 // First create a LabelList containing these dependencies, then merge the values with srcs.
412 generatedHdrsAndSrcs := baseCompilerProps.Generated_headers
413 generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...)
414
415 generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs)
416
Jingwen Chene32e9e02021-04-23 09:17:24 +0000417 // Combine the base exclude_srcs and configuration-specific exclude_srcs
418 allExcludeSrcs := append(baseExcludeSrcs, baseCompilerProps.Exclude_srcs...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400419 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, allSrcs, allExcludeSrcs)
420 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000421 }
422
Jingwen Chenc1c26502021-04-05 10:35:13 +0000423 for _, props := range module.compiler.compilerProps() {
424 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000425 srcs.Value = parseSrcs(baseCompilerProps)
426 copts.Value = parseCopts(baseCompilerProps)
Chris Parsons990c4f42021-05-25 12:10:58 -0400427 asFlags.Value = parseCommandLineFlags(baseCompilerProps.Asflags)
428 conlyFlags.Value = parseCommandLineFlags(baseCompilerProps.Conlyflags)
429 cppFlags.Value = parseCommandLineFlags(baseCompilerProps.Cppflags)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000430
431 // Used for arch-specific srcs later.
432 baseSrcs = baseCompilerProps.Srcs
Jingwen Chene32e9e02021-04-23 09:17:24 +0000433 baseSrcsLabelList = parseSrcs(baseCompilerProps)
Chris Parsons484e50a2021-05-13 15:13:04 -0400434 baseExcludeSrcs = baseCompilerProps.Exclude_srcs
Jingwen Chenc1c26502021-04-05 10:35:13 +0000435 break
436 }
437 }
438
Jingwen Chene32e9e02021-04-23 09:17:24 +0000439 // Handle include_build_directory prop. If the property is true, then the
440 // target has access to all headers recursively in the package, and has
441 // "-I<module-dir>" in its copts.
Jingwen Chened9c17d2021-04-13 07:14:55 +0000442 if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400443 copts.Value = append(copts.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000444 } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400445 copts.Value = append(copts.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000446 }
447
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200448 for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000449 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000450 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
451 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
452 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
453 srcsList := parseSrcs(baseCompilerProps)
454 srcs.SetValueForArch(arch.Name, srcsList)
455 // The base srcs value should not contain any arch-specific excludes.
456 srcs.Value = bazel.SubtractBazelLabelList(srcs.Value, bazel.LabelList{Includes: srcsList.Excludes})
457 }
458
Jingwen Chened9c17d2021-04-13 07:14:55 +0000459 copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps))
Chris Parsons990c4f42021-05-25 12:10:58 -0400460 asFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Asflags))
461 conlyFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags))
462 cppFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Cppflags))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000463 }
464 }
465
Jingwen Chene32e9e02021-04-23 09:17:24 +0000466 // After going through all archs, delete the duplicate files in the arch
467 // values that are already in the base srcs.Value.
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200468 for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000469 if _, ok := props.(*BaseCompilerProperties); ok {
470 srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcs.GetValueForArch(arch.Name), srcs.Value))
471 }
472 }
473
474 // Now that the srcs.Value list is finalized, compare it with the original
475 // list, and put the difference into the default condition for the arch
476 // select.
477 defaultsSrcs := bazel.SubtractBazelLabelList(baseSrcsLabelList, srcs.Value)
478 // TODO(b/186153868): handle the case with multiple variant types, e.g. when arch and os are both used.
479 srcs.SetValueForArch(bazel.CONDITIONS_DEFAULT, defaultsSrcs)
480
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400481 // Handle target specific properties.
482 for os, osProps := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) {
483 if baseCompilerProps, ok := osProps.Properties.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000484 srcsList := parseSrcs(baseCompilerProps)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000485 // TODO(b/186153868): add support for os-specific srcs and exclude_srcs
Liz Kammer2b07ec72021-05-26 15:08:27 -0400486 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
487 srcs.SetOsValueForTarget(os.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList))
488 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400489 copts.SetOsValueForTarget(os.Name, parseCopts(baseCompilerProps))
490 asFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Asflags))
491 conlyFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags))
492 cppFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Cppflags))
493 }
494 for arch, archProps := range osProps.ArchProperties {
495 if baseCompilerProps, ok := archProps.(*BaseCompilerProperties); ok {
496 srcsList := parseSrcs(baseCompilerProps)
497 // TODO(b/186153868): add support for os-specific srcs and exclude_srcs
Liz Kammer2b07ec72021-05-26 15:08:27 -0400498 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
499 srcs.SetOsArchValueForTarget(os.Name, arch.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList))
500 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400501 copts.SetOsArchValueForTarget(os.Name, arch.Name, parseCopts(baseCompilerProps))
502 asFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Asflags))
503 conlyFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags))
504 cppFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Cppflags))
505 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000506 }
507 }
508
Liz Kammerba7a9c52021-05-26 08:45:30 -0400509 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
510 "Cflags": &copts,
511 "Asflags": &asFlags,
512 "CppFlags": &cppFlags,
513 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400514 productVariableProps := android.ProductVariableProperties(ctx)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400515 for propName, attr := range productVarPropNameToAttribute {
516 if props, exists := productVariableProps[propName]; exists {
517 for _, prop := range props {
518 flags, ok := prop.Property.([]string)
519 if !ok {
520 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
521 }
522 newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable)
523 attr.ProductValues = append(attr.ProductValues, bazel.ProductVariableValues{
524 ProductVariable: prop.ProductConfigVariable,
525 Values: newFlags,
526 })
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400527 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400528 }
529 }
530
Chris Parsons990c4f42021-05-25 12:10:58 -0400531 // Branch srcs into three language-specific groups.
532 // C++ is the "catch-all" group, and comprises generated sources because we don't
533 // know the language of these sources until the genrule is executed.
534 // TODO(b/): Handle language detection of sources in a Bazel rule.
535 isCSrc := func(s string) bool {
536 return strings.HasSuffix(s, ".c")
537 }
538 isAsmSrc := func(s string) bool {
539 return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s")
540 }
541 cSrcs := bazel.FilterLabelListAttribute(srcs, isCSrc)
542 asSrcs := bazel.FilterLabelListAttribute(srcs, isAsmSrc)
543 srcs = bazel.SubtractBazelLabelListAttribute(srcs, cSrcs)
544 srcs = bazel.SubtractBazelLabelListAttribute(srcs, asSrcs)
Jingwen Chen107c0de2021-04-09 10:43:12 +0000545 return compilerAttributes{
Chris Parsons990c4f42021-05-25 12:10:58 -0400546 copts: copts,
547 srcs: srcs,
548 asFlags: asFlags,
549 asSrcs: asSrcs,
550 cSrcs: cSrcs,
551 conlyFlags: conlyFlags,
552 cppFlags: cppFlags,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000553 }
554}
555
556// Convenience struct to hold all attributes parsed from linker properties.
557type linkerAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400558 deps bazel.LabelListAttribute
559 dynamicDeps bazel.LabelListAttribute
560 wholeArchiveDeps bazel.LabelListAttribute
Chris Parsonsd6358772021-05-18 18:35:24 -0400561 exportedDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400562 linkopts bazel.StringListAttribute
563 versionScript bazel.LabelAttribute
Jingwen Chenc1c26502021-04-05 10:35:13 +0000564}
565
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400566// FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here
567func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string {
568 flags := linkerProperties.Ldflags
569 if !BoolDefault(linkerProperties.Pack_relocations, true) {
570 flags = append(flags, "-Wl,--pack-dyn-relocs=none")
571 }
572 return flags
573}
574
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200575// bp2BuildParseLinkerProps parses the linker properties of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400576// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000577func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Jingwen Chen63930982021-03-24 10:04:33 -0400578 var deps bazel.LabelListAttribute
Chris Parsonsd6358772021-05-18 18:35:24 -0400579 var exportedDeps bazel.LabelListAttribute
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400580 var dynamicDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400581 var wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400582 var linkopts bazel.StringListAttribute
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200583 var versionScript bazel.LabelAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400584
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400585 getLibs := func(baseLinkerProps *BaseLinkerProperties) []string {
586 libs := baseLinkerProps.Header_libs
587 libs = append(libs, baseLinkerProps.Static_libs...)
588 libs = android.SortedUniqueStrings(libs)
589 return libs
590 }
591
Jingwen Chen91220d72021-03-24 02:18:33 -0400592 for _, linkerProps := range module.linker.linkerProps() {
593 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400594 libs := getLibs(baseLinkerProps)
Chris Parsonsd6358772021-05-18 18:35:24 -0400595 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400596 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chened9c17d2021-04-13 07:14:55 +0000597 deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs))
Chris Parsonsd6358772021-05-18 18:35:24 -0400598 exportedDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400599 linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps)
Chris Parsons08648312021-05-06 16:23:19 -0400600 wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200601
602 if baseLinkerProps.Version_script != nil {
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200603 versionScript.Value = android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200604 }
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400605
606 sharedLibs := baseLinkerProps.Shared_libs
607 dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, sharedLibs))
608
Jingwen Chen91220d72021-03-24 02:18:33 -0400609 break
610 }
611 }
612
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400613 for arch, props := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) {
614 if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
615 libs := getLibs(baseLinkerProps)
Chris Parsonsd6358772021-05-18 18:35:24 -0400616 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400617 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chen63930982021-03-24 10:04:33 -0400618 deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
Chris Parsonsd6358772021-05-18 18:35:24 -0400619 exportedDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400620 linkopts.SetValueForArch(arch.Name, getBp2BuildLinkerFlags(baseLinkerProps))
Chris Parsons08648312021-05-06 16:23:19 -0400621 wholeArchiveDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400622
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200623 if baseLinkerProps.Version_script != nil {
624 versionScript.SetValueForArch(arch.Name,
625 android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
626 }
Rupert Shuttleworth3b413d32021-05-10 18:41:51 -0400627
628 sharedLibs := baseLinkerProps.Shared_libs
629 dynamicDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
Jingwen Chen63930982021-03-24 10:04:33 -0400630 }
631 }
632
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400633 for os, targetProperties := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) {
634 if baseLinkerProps, ok := targetProperties.Properties.(*BaseLinkerProperties); ok {
635 libs := getLibs(baseLinkerProps)
Chris Parsonsd6358772021-05-18 18:35:24 -0400636 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400637 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400638 wholeArchiveDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
639 deps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
640 exportedDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400641
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400642 linkopts.SetOsValueForTarget(os.Name, getBp2BuildLinkerFlags(baseLinkerProps))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400643
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400644 if baseLinkerProps.Version_script != nil {
645 versionScript.SetOsValueForTarget(os.Name, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
646 }
647
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400648 sharedLibs := baseLinkerProps.Shared_libs
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400649 dynamicDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
650 }
651 for arch, archProperties := range targetProperties.ArchProperties {
652 if baseLinkerProps, ok := archProperties.(*BaseLinkerProperties); ok {
653 libs := getLibs(baseLinkerProps)
654 exportedLibs := baseLinkerProps.Export_header_lib_headers
655 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
656 wholeArchiveDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
657 deps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
658 exportedDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs))
659
660 linkopts.SetOsArchValueForTarget(os.Name, arch.Name, getBp2BuildLinkerFlags(baseLinkerProps))
661
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400662 if baseLinkerProps.Version_script != nil {
663 versionScript.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
664 }
665
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400666 sharedLibs := baseLinkerProps.Shared_libs
667 dynamicDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
668 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400669 }
670 }
671
Jingwen Chen107c0de2021-04-09 10:43:12 +0000672 return linkerAttributes{
Chris Parsons08648312021-05-06 16:23:19 -0400673 deps: deps,
Chris Parsonsd6358772021-05-18 18:35:24 -0400674 exportedDeps: exportedDeps,
Chris Parsons08648312021-05-06 16:23:19 -0400675 dynamicDeps: dynamicDeps,
676 wholeArchiveDeps: wholeArchiveDeps,
677 linkopts: linkopts,
678 versionScript: versionScript,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000679 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400680}
681
Jingwen Chened9c17d2021-04-13 07:14:55 +0000682// Relativize a list of root-relative paths with respect to the module's
683// directory.
684//
685// include_dirs Soong prop are root-relative (b/183742505), but
686// local_include_dirs, export_include_dirs and export_system_include_dirs are
687// module dir relative. This function makes a list of paths entirely module dir
688// relative.
689//
690// For the `include` attribute, Bazel wants the paths to be relative to the
691// module.
692func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000693 var relativePaths []string
694 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000695 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
696 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
697 if err != nil {
698 panic(err)
699 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000700 relativePaths = append(relativePaths, relativePath)
701 }
702 return relativePaths
703}
704
Jingwen Chen882bcc12021-04-27 05:54:20 +0000705func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
Jingwen Chen91220d72021-03-24 02:18:33 -0400706 libraryDecorator := module.linker.(*libraryDecorator)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400707 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
708}
Jingwen Chen91220d72021-03-24 02:18:33 -0400709
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400710func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
711 prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker)
712 libraryDecorator := prebuiltLibraryLinker.libraryDecorator
713 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
714}
715
716// bp2BuildParseExportedIncludes creates a string list attribute contains the
717// exported included directories of a module.
718func bp2BuildParseExportedIncludesHelper(ctx android.TopDownMutatorContext, module *Module, libraryDecorator *libraryDecorator) bazel.StringListAttribute {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000719 // Export_system_include_dirs and export_include_dirs are already module dir
720 // relative, so they don't need to be relativized like include_dirs, which
721 // are root-relative.
Jingwen Chen91220d72021-03-24 02:18:33 -0400722 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
723 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000724 includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
Jingwen Chen91220d72021-03-24 02:18:33 -0400725
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400726 getVariantIncludeDirs := func(includeDirs []string, flagExporterProperties *FlagExporterProperties) []string {
727 variantIncludeDirs := flagExporterProperties.Export_system_include_dirs
728 variantIncludeDirs = append(variantIncludeDirs, flagExporterProperties.Export_include_dirs...)
729
730 // To avoid duplicate includes when base includes + arch includes are combined
731 // TODO: This doesn't take conflicts between arch and os includes into account
732 variantIncludeDirs = bazel.SubtractStrings(variantIncludeDirs, includeDirs)
733 return variantIncludeDirs
734 }
735
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200736 for arch, props := range module.GetArchProperties(ctx, &FlagExporterProperties{}) {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000737 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400738 archIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000739 if len(archIncludeDirs) > 0 {
740 includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs)
741 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000742 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400743 }
744
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400745 for os, targetProperties := range module.GetTargetProperties(ctx, &FlagExporterProperties{}) {
746 if flagExporterProperties, ok := targetProperties.Properties.(*FlagExporterProperties); ok {
747 targetIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties)
748 if len(targetIncludeDirs) > 0 {
749 includeDirsAttribute.SetOsValueForTarget(os.Name, targetIncludeDirs)
750 }
751 }
752 for arch, archProperties := range targetProperties.ArchProperties {
753 if flagExporterProperties, ok := archProperties.(*FlagExporterProperties); ok {
754 targetIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties)
755 if len(targetIncludeDirs) > 0 {
756 includeDirsAttribute.SetOsArchValueForTarget(os.Name, arch.Name, targetIncludeDirs)
757 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400758 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400759 }
760 }
761
Jingwen Chen882bcc12021-04-27 05:54:20 +0000762 return includeDirsAttribute
Jingwen Chen91220d72021-03-24 02:18:33 -0400763}