blob: 26f6d302ec40f70500beefb13cb97393ff670e87 [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 (
17 "android/soong/android"
18 "android/soong/bazel"
Jingwen Chened9c17d2021-04-13 07:14:55 +000019 "path/filepath"
Jingwen Chen91220d72021-03-24 02:18:33 -040020)
21
22// bp2build functions and helpers for converting cc_* modules to Bazel.
23
24func init() {
25 android.DepsBp2BuildMutators(RegisterDepsBp2Build)
26}
27
28func RegisterDepsBp2Build(ctx android.RegisterMutatorsContext) {
29 ctx.BottomUp("cc_bp2build_deps", depsBp2BuildMutator)
30}
31
32// A naive deps mutator to add deps on all modules across all combinations of
33// target props for cc modules. This is needed to make module -> bazel label
34// resolution work in the bp2build mutator later. This is probably
35// the wrong way to do it, but it works.
36//
37// TODO(jingwen): can we create a custom os mutator in depsBp2BuildMutator to do this?
38func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) {
39 module, ok := ctx.Module().(*Module)
40 if !ok {
41 // Not a cc module
42 return
43 }
44
45 if !module.ConvertWithBp2build(ctx) {
46 return
47 }
48
49 var allDeps []string
50
51 for _, p := range module.GetTargetProperties(&BaseLinkerProperties{}) {
52 // arch specific linker props
53 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
54 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
55 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +000056 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
57 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
58 }
59 }
60
Lukacs T. Berki598dd002021-05-05 09:00:01 +020061 for _, p := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) {
Jingwen Chened9c17d2021-04-13 07:14:55 +000062 // arch specific linker props
63 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
64 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
65 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
66 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
67 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
Jingwen Chen91220d72021-03-24 02:18:33 -040068 }
69 }
70
Jingwen Chen53681ef2021-04-29 08:15:13 +000071 // Deps in the static: { .. } and shared: { .. } props of a cc_library.
72 if lib, ok := module.compiler.(*libraryDecorator); ok {
73 allDeps = append(allDeps, lib.SharedProperties.Shared.Static_libs...)
74 allDeps = append(allDeps, lib.SharedProperties.Shared.Whole_static_libs...)
75 allDeps = append(allDeps, lib.SharedProperties.Shared.Shared_libs...)
76 allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...)
77
78 allDeps = append(allDeps, lib.StaticProperties.Static.Static_libs...)
79 allDeps = append(allDeps, lib.StaticProperties.Static.Whole_static_libs...)
80 allDeps = append(allDeps, lib.StaticProperties.Static.Shared_libs...)
81 allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...)
82 }
83
Jingwen Chen91220d72021-03-24 02:18:33 -040084 ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...)
85}
86
Jingwen Chen53681ef2021-04-29 08:15:13 +000087type sharedAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -040088 copts bazel.StringListAttribute
89 srcs bazel.LabelListAttribute
90 staticDeps bazel.LabelListAttribute
91 dynamicDeps bazel.LabelListAttribute
92 wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +000093}
94
95// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
96func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) sharedAttributes {
97 lib, ok := module.compiler.(*libraryDecorator)
98 if !ok {
99 return sharedAttributes{}
100 }
101
Chris Parsons08648312021-05-06 16:23:19 -0400102 copts := bazel.StringListAttribute{Value: lib.SharedProperties.Shared.Cflags}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000103
Chris Parsons08648312021-05-06 16:23:19 -0400104 srcs := bazel.LabelListAttribute{
105 Value: android.BazelLabelForModuleSrc(ctx, lib.SharedProperties.Shared.Srcs)}
106
107 staticDeps := bazel.LabelListAttribute{
108 Value: android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Static_libs)}
109
110 dynamicDeps := bazel.LabelListAttribute{
111 Value: android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Shared_libs)}
112
113 wholeArchiveDeps := bazel.LabelListAttribute{
114 Value: android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Whole_static_libs)}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000115
116 return sharedAttributes{
Chris Parsons08648312021-05-06 16:23:19 -0400117 copts: copts,
118 srcs: srcs,
119 staticDeps: staticDeps,
120 dynamicDeps: dynamicDeps,
121 wholeArchiveDeps: wholeArchiveDeps,
Jingwen Chen53681ef2021-04-29 08:15:13 +0000122 }
123}
124
125type staticAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400126 copts bazel.StringListAttribute
127 srcs bazel.LabelListAttribute
128 staticDeps bazel.LabelListAttribute
129 dynamicDeps bazel.LabelListAttribute
130 wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +0000131}
132
133// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
134func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticAttributes {
135 lib, ok := module.compiler.(*libraryDecorator)
136 if !ok {
137 return staticAttributes{}
138 }
139
Chris Parsons08648312021-05-06 16:23:19 -0400140 copts := bazel.StringListAttribute{Value: lib.StaticProperties.Static.Cflags}
141
142 srcs := bazel.LabelListAttribute{
143 Value: android.BazelLabelForModuleSrc(ctx, lib.StaticProperties.Static.Srcs)}
144
145 staticDeps := bazel.LabelListAttribute{
146 Value: android.BazelLabelForModuleDeps(ctx, lib.StaticProperties.Static.Static_libs)}
147
148 dynamicDeps := bazel.LabelListAttribute{
149 Value: android.BazelLabelForModuleDeps(ctx, lib.StaticProperties.Static.Shared_libs)}
150
151 wholeArchiveDeps := bazel.LabelListAttribute{
152 Value: android.BazelLabelForModuleDeps(ctx, lib.StaticProperties.Static.Whole_static_libs)}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000153
154 return staticAttributes{
Chris Parsons08648312021-05-06 16:23:19 -0400155 copts: copts,
156 srcs: srcs,
157 staticDeps: staticDeps,
158 dynamicDeps: dynamicDeps,
159 wholeArchiveDeps: wholeArchiveDeps,
Jingwen Chen53681ef2021-04-29 08:15:13 +0000160 }
161}
162
Jingwen Chen107c0de2021-04-09 10:43:12 +0000163// Convenience struct to hold all attributes parsed from compiler properties.
164type compilerAttributes struct {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000165 copts bazel.StringListAttribute
166 srcs bazel.LabelListAttribute
167 includes bazel.StringListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000168}
169
Jingwen Chen63930982021-03-24 10:04:33 -0400170// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000171func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
Jingwen Chen882bcc12021-04-27 05:54:20 +0000172 var srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000173 var copts bazel.StringListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400174
Jingwen Chened9c17d2021-04-13 07:14:55 +0000175 // Creates the -I flag for a directory, while making the directory relative
176 // to the exec root for Bazel to work.
177 includeFlag := func(dir string) string {
178 // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements.
179 return "-I" + filepath.Join(ctx.ModuleDir(), dir)
180 }
181
Jingwen Chened9c17d2021-04-13 07:14:55 +0000182 // Parse the list of module-relative include directories (-I).
183 parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string {
184 // include_dirs are root-relative, not module-relative.
185 includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
186 return append(includeDirs, baseCompilerProps.Local_include_dirs...)
187 }
188
189 // Parse the list of copts.
190 parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string {
191 copts := append([]string{}, baseCompilerProps.Cflags...)
192 for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
193 copts = append(copts, includeFlag(dir))
194 }
195 return copts
Jingwen Chen63930982021-03-24 10:04:33 -0400196 }
197
Jingwen Chene32e9e02021-04-23 09:17:24 +0000198 // baseSrcs contain the list of src files that are used for every configuration.
199 var baseSrcs []string
200 // baseExcludeSrcs contain the list of src files that are excluded for every configuration.
201 var baseExcludeSrcs []string
202 // baseSrcsLabelList is a clone of the base srcs LabelList, used for computing the
203 // arch or os specific srcs later.
204 var baseSrcsLabelList bazel.LabelList
205
206 // Parse srcs from an arch or OS's props value, taking the base srcs and
207 // exclude srcs into account.
208 parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList {
209 // Combine the base srcs and arch-specific srcs
210 allSrcs := append(baseSrcs, baseCompilerProps.Srcs...)
211 // Combine the base exclude_srcs and configuration-specific exclude_srcs
212 allExcludeSrcs := append(baseExcludeSrcs, baseCompilerProps.Exclude_srcs...)
213 return android.BazelLabelForModuleSrcExcludes(ctx, allSrcs, allExcludeSrcs)
214 }
215
Jingwen Chenc1c26502021-04-05 10:35:13 +0000216 for _, props := range module.compiler.compilerProps() {
217 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000218 srcs.Value = parseSrcs(baseCompilerProps)
219 copts.Value = parseCopts(baseCompilerProps)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000220
221 // Used for arch-specific srcs later.
222 baseSrcs = baseCompilerProps.Srcs
223 baseExcludeSrcs = baseCompilerProps.Exclude_srcs
224 baseSrcsLabelList = parseSrcs(baseCompilerProps)
Jingwen Chenc1c26502021-04-05 10:35:13 +0000225 break
226 }
227 }
228
Jingwen Chene32e9e02021-04-23 09:17:24 +0000229 // Handle include_build_directory prop. If the property is true, then the
230 // target has access to all headers recursively in the package, and has
231 // "-I<module-dir>" in its copts.
Jingwen Chened9c17d2021-04-13 07:14:55 +0000232 if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
233 copts.Value = append(copts.Value, includeFlag("."))
Jingwen Chened9c17d2021-04-13 07:14:55 +0000234 } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() {
235 copts.Value = append(copts.Value, includeFlag("."))
Jingwen Chened9c17d2021-04-13 07:14:55 +0000236 }
237
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200238 for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000239 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000240 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
241 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
242 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
243 srcsList := parseSrcs(baseCompilerProps)
244 srcs.SetValueForArch(arch.Name, srcsList)
245 // The base srcs value should not contain any arch-specific excludes.
246 srcs.Value = bazel.SubtractBazelLabelList(srcs.Value, bazel.LabelList{Includes: srcsList.Excludes})
247 }
248
Jingwen Chened9c17d2021-04-13 07:14:55 +0000249 copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000250 }
251 }
252
Jingwen Chene32e9e02021-04-23 09:17:24 +0000253 // After going through all archs, delete the duplicate files in the arch
254 // values that are already in the base srcs.Value.
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200255 for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000256 if _, ok := props.(*BaseCompilerProperties); ok {
257 srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcs.GetValueForArch(arch.Name), srcs.Value))
258 }
259 }
260
261 // Now that the srcs.Value list is finalized, compare it with the original
262 // list, and put the difference into the default condition for the arch
263 // select.
264 defaultsSrcs := bazel.SubtractBazelLabelList(baseSrcsLabelList, srcs.Value)
265 // TODO(b/186153868): handle the case with multiple variant types, e.g. when arch and os are both used.
266 srcs.SetValueForArch(bazel.CONDITIONS_DEFAULT, defaultsSrcs)
267
268 // Handle OS specific props.
Jingwen Chenc1c26502021-04-05 10:35:13 +0000269 for os, props := range module.GetTargetProperties(&BaseCompilerProperties{}) {
270 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000271 srcsList := parseSrcs(baseCompilerProps)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000272 // TODO(b/186153868): add support for os-specific srcs and exclude_srcs
273 srcs.SetValueForOS(os.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList))
Jingwen Chened9c17d2021-04-13 07:14:55 +0000274 copts.SetValueForOS(os.Name, parseCopts(baseCompilerProps))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000275 }
276 }
277
Jingwen Chen107c0de2021-04-09 10:43:12 +0000278 return compilerAttributes{
Jingwen Chen107c0de2021-04-09 10:43:12 +0000279 srcs: srcs,
280 copts: copts,
281 }
282}
283
284// Convenience struct to hold all attributes parsed from linker properties.
285type linkerAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400286 deps bazel.LabelListAttribute
287 dynamicDeps bazel.LabelListAttribute
288 wholeArchiveDeps bazel.LabelListAttribute
289 linkopts bazel.StringListAttribute
290 versionScript bazel.LabelAttribute
Jingwen Chenc1c26502021-04-05 10:35:13 +0000291}
292
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400293// FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here
294func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string {
295 flags := linkerProperties.Ldflags
296 if !BoolDefault(linkerProperties.Pack_relocations, true) {
297 flags = append(flags, "-Wl,--pack-dyn-relocs=none")
298 }
299 return flags
300}
301
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200302// bp2BuildParseLinkerProps parses the linker properties of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400303// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000304func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Jingwen Chen63930982021-03-24 10:04:33 -0400305 var deps bazel.LabelListAttribute
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400306 var dynamicDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400307 var wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400308 var linkopts bazel.StringListAttribute
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200309 var versionScript bazel.LabelAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400310
Jingwen Chen91220d72021-03-24 02:18:33 -0400311 for _, linkerProps := range module.linker.linkerProps() {
312 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
313 libs := baseLinkerProps.Header_libs
314 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000315 libs = append(libs, baseLinkerProps.Static_libs...)
Chris Parsons08648312021-05-06 16:23:19 -0400316 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chened9c17d2021-04-13 07:14:55 +0000317 libs = android.SortedUniqueStrings(libs)
318 deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400319 linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps)
Chris Parsons08648312021-05-06 16:23:19 -0400320 wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200321
322 if baseLinkerProps.Version_script != nil {
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200323 versionScript.Value = android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200324 }
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400325
326 sharedLibs := baseLinkerProps.Shared_libs
327 dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, sharedLibs))
328
Jingwen Chen91220d72021-03-24 02:18:33 -0400329 break
330 }
331 }
332
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200333 for arch, p := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) {
Jingwen Chen63930982021-03-24 10:04:33 -0400334 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
335 libs := baseLinkerProps.Header_libs
336 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000337 libs = append(libs, baseLinkerProps.Static_libs...)
Chris Parsons08648312021-05-06 16:23:19 -0400338 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chen63930982021-03-24 10:04:33 -0400339 libs = android.SortedUniqueStrings(libs)
340 deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400341 linkopts.SetValueForArch(arch.Name, getBp2BuildLinkerFlags(baseLinkerProps))
Chris Parsons08648312021-05-06 16:23:19 -0400342 wholeArchiveDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400343
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200344 if baseLinkerProps.Version_script != nil {
345 versionScript.SetValueForArch(arch.Name,
346 android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400347
348 sharedLibs := baseLinkerProps.Shared_libs
349 dynamicDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200350 }
Jingwen Chen63930982021-03-24 10:04:33 -0400351 }
352 }
353
Jingwen Chen91220d72021-03-24 02:18:33 -0400354 for os, p := range module.GetTargetProperties(&BaseLinkerProperties{}) {
355 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
356 libs := baseLinkerProps.Header_libs
357 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000358 libs = append(libs, baseLinkerProps.Static_libs...)
Chris Parsons08648312021-05-06 16:23:19 -0400359 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chen91220d72021-03-24 02:18:33 -0400360 libs = android.SortedUniqueStrings(libs)
Chris Parsons08648312021-05-06 16:23:19 -0400361 wholeArchiveDeps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Jingwen Chen63930982021-03-24 10:04:33 -0400362 deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400363
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400364 linkopts.SetValueForOS(os.Name, getBp2BuildLinkerFlags(baseLinkerProps))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400365
366 sharedLibs := baseLinkerProps.Shared_libs
367 dynamicDeps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
Jingwen Chen91220d72021-03-24 02:18:33 -0400368 }
369 }
370
Jingwen Chen107c0de2021-04-09 10:43:12 +0000371 return linkerAttributes{
Chris Parsons08648312021-05-06 16:23:19 -0400372 deps: deps,
373 dynamicDeps: dynamicDeps,
374 wholeArchiveDeps: wholeArchiveDeps,
375 linkopts: linkopts,
376 versionScript: versionScript,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000377 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400378}
379
Jingwen Chened9c17d2021-04-13 07:14:55 +0000380// Relativize a list of root-relative paths with respect to the module's
381// directory.
382//
383// include_dirs Soong prop are root-relative (b/183742505), but
384// local_include_dirs, export_include_dirs and export_system_include_dirs are
385// module dir relative. This function makes a list of paths entirely module dir
386// relative.
387//
388// For the `include` attribute, Bazel wants the paths to be relative to the
389// module.
390func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000391 var relativePaths []string
392 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000393 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
394 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
395 if err != nil {
396 panic(err)
397 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000398 relativePaths = append(relativePaths, relativePath)
399 }
400 return relativePaths
401}
402
Jingwen Chened9c17d2021-04-13 07:14:55 +0000403// bp2BuildParseExportedIncludes creates a string list attribute contains the
Jingwen Chen882bcc12021-04-27 05:54:20 +0000404// exported included directories of a module.
405func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
Jingwen Chen91220d72021-03-24 02:18:33 -0400406 libraryDecorator := module.linker.(*libraryDecorator)
407
Jingwen Chened9c17d2021-04-13 07:14:55 +0000408 // Export_system_include_dirs and export_include_dirs are already module dir
409 // relative, so they don't need to be relativized like include_dirs, which
410 // are root-relative.
Jingwen Chen91220d72021-03-24 02:18:33 -0400411 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
412 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000413 includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
Jingwen Chen91220d72021-03-24 02:18:33 -0400414
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200415 for arch, props := range module.GetArchProperties(ctx, &FlagExporterProperties{}) {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000416 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
417 archIncludeDirs := flagExporterProperties.Export_system_include_dirs
418 archIncludeDirs = append(archIncludeDirs, flagExporterProperties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000419
420 // To avoid duplicate includes when base includes + arch includes are combined
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400421 // FIXME: This doesn't take conflicts between arch and os includes into account
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000422 archIncludeDirs = bazel.SubtractStrings(archIncludeDirs, includeDirs)
423
424 if len(archIncludeDirs) > 0 {
425 includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs)
426 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000427 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400428 }
429
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400430 for os, props := range module.GetTargetProperties(&FlagExporterProperties{}) {
431 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
432 osIncludeDirs := flagExporterProperties.Export_system_include_dirs
433 osIncludeDirs = append(osIncludeDirs, flagExporterProperties.Export_include_dirs...)
434
435 // To avoid duplicate includes when base includes + os includes are combined
436 // FIXME: This doesn't take conflicts between arch and os includes into account
437 osIncludeDirs = bazel.SubtractStrings(osIncludeDirs, includeDirs)
438
439 if len(osIncludeDirs) > 0 {
440 includeDirsAttribute.SetValueForOS(os.Name, osIncludeDirs)
441 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400442 }
443 }
444
Jingwen Chen882bcc12021-04-27 05:54:20 +0000445 return includeDirsAttribute
Jingwen Chen91220d72021-03-24 02:18:33 -0400446}