blob: 75543acf2dce0c8a894690743159c90fe0c9679c [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"
Jingwen Chen91220d72021-03-24 02:18:33 -040022)
23
24// bp2build functions and helpers for converting cc_* modules to Bazel.
25
26func init() {
27 android.DepsBp2BuildMutators(RegisterDepsBp2Build)
28}
29
30func RegisterDepsBp2Build(ctx android.RegisterMutatorsContext) {
31 ctx.BottomUp("cc_bp2build_deps", depsBp2BuildMutator)
32}
33
34// A naive deps mutator to add deps on all modules across all combinations of
35// target props for cc modules. This is needed to make module -> bazel label
36// resolution work in the bp2build mutator later. This is probably
37// the wrong way to do it, but it works.
38//
39// TODO(jingwen): can we create a custom os mutator in depsBp2BuildMutator to do this?
40func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) {
41 module, ok := ctx.Module().(*Module)
42 if !ok {
43 // Not a cc module
44 return
45 }
46
47 if !module.ConvertWithBp2build(ctx) {
48 return
49 }
50
51 var allDeps []string
52
Chris Parsons484e50a2021-05-13 15:13:04 -040053 for _, p := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) {
54 // base compiler props
55 if baseCompilerProps, ok := p.(*BaseCompilerProperties); ok {
56 allDeps = append(allDeps, baseCompilerProps.Generated_headers...)
57 allDeps = append(allDeps, baseCompilerProps.Generated_sources...)
58 }
59 }
60
61 for _, p := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
62 // arch specific compiler props
63 if baseCompilerProps, ok := p.(*BaseCompilerProperties); ok {
64 allDeps = append(allDeps, baseCompilerProps.Generated_headers...)
65 allDeps = append(allDeps, baseCompilerProps.Generated_sources...)
66 }
67 }
68
Lukacs T. Berki5f518392021-05-17 11:44:58 +020069 for _, p := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) {
Jingwen Chen91220d72021-03-24 02:18:33 -040070 // arch specific linker props
71 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
72 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
73 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +000074 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
75 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
76 }
77 }
78
Lukacs T. Berki598dd002021-05-05 09:00:01 +020079 for _, p := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) {
Jingwen Chened9c17d2021-04-13 07:14:55 +000080 // arch specific linker props
81 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
82 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
83 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
84 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
85 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
Jingwen Chen91220d72021-03-24 02:18:33 -040086 }
87 }
88
Jingwen Chen53681ef2021-04-29 08:15:13 +000089 // Deps in the static: { .. } and shared: { .. } props of a cc_library.
90 if lib, ok := module.compiler.(*libraryDecorator); ok {
91 allDeps = append(allDeps, lib.SharedProperties.Shared.Static_libs...)
92 allDeps = append(allDeps, lib.SharedProperties.Shared.Whole_static_libs...)
93 allDeps = append(allDeps, lib.SharedProperties.Shared.Shared_libs...)
Jingwen Chen53681ef2021-04-29 08:15:13 +000094
95 allDeps = append(allDeps, lib.StaticProperties.Static.Static_libs...)
96 allDeps = append(allDeps, lib.StaticProperties.Static.Whole_static_libs...)
97 allDeps = append(allDeps, lib.StaticProperties.Static.Shared_libs...)
Jingwen Chen45dec102021-05-19 10:30:29 +000098
99 // TODO(b/186024507, b/186489250): Temporarily exclude adding
100 // system_shared_libs deps until libc and libm builds.
101 // allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...)
102 // allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000103 }
104
Jingwen Chen91220d72021-03-24 02:18:33 -0400105 ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...)
106}
107
Jingwen Chen53681ef2021-04-29 08:15:13 +0000108type sharedAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400109 copts bazel.StringListAttribute
110 srcs bazel.LabelListAttribute
111 staticDeps bazel.LabelListAttribute
112 dynamicDeps bazel.LabelListAttribute
113 wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +0000114}
115
116// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
117func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) sharedAttributes {
118 lib, ok := module.compiler.(*libraryDecorator)
119 if !ok {
120 return sharedAttributes{}
121 }
122
Chris Parsons08648312021-05-06 16:23:19 -0400123 copts := bazel.StringListAttribute{Value: lib.SharedProperties.Shared.Cflags}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000124
Chris Parsons08648312021-05-06 16:23:19 -0400125 srcs := bazel.LabelListAttribute{
126 Value: android.BazelLabelForModuleSrc(ctx, lib.SharedProperties.Shared.Srcs)}
127
128 staticDeps := bazel.LabelListAttribute{
129 Value: android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Static_libs)}
130
131 dynamicDeps := bazel.LabelListAttribute{
132 Value: android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Shared_libs)}
133
134 wholeArchiveDeps := bazel.LabelListAttribute{
135 Value: android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Whole_static_libs)}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000136
137 return sharedAttributes{
Chris Parsons08648312021-05-06 16:23:19 -0400138 copts: copts,
139 srcs: srcs,
140 staticDeps: staticDeps,
141 dynamicDeps: dynamicDeps,
142 wholeArchiveDeps: wholeArchiveDeps,
Jingwen Chen53681ef2021-04-29 08:15:13 +0000143 }
144}
145
146type staticAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400147 copts bazel.StringListAttribute
148 srcs bazel.LabelListAttribute
149 staticDeps bazel.LabelListAttribute
150 dynamicDeps bazel.LabelListAttribute
151 wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +0000152}
153
154// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
155func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticAttributes {
156 lib, ok := module.compiler.(*libraryDecorator)
157 if !ok {
158 return staticAttributes{}
159 }
160
Chris Parsons08648312021-05-06 16:23:19 -0400161 copts := bazel.StringListAttribute{Value: lib.StaticProperties.Static.Cflags}
162
163 srcs := bazel.LabelListAttribute{
164 Value: android.BazelLabelForModuleSrc(ctx, lib.StaticProperties.Static.Srcs)}
165
166 staticDeps := bazel.LabelListAttribute{
167 Value: android.BazelLabelForModuleDeps(ctx, lib.StaticProperties.Static.Static_libs)}
168
169 dynamicDeps := bazel.LabelListAttribute{
170 Value: android.BazelLabelForModuleDeps(ctx, lib.StaticProperties.Static.Shared_libs)}
171
172 wholeArchiveDeps := bazel.LabelListAttribute{
173 Value: android.BazelLabelForModuleDeps(ctx, lib.StaticProperties.Static.Whole_static_libs)}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000174
175 return staticAttributes{
Chris Parsons08648312021-05-06 16:23:19 -0400176 copts: copts,
177 srcs: srcs,
178 staticDeps: staticDeps,
179 dynamicDeps: dynamicDeps,
180 wholeArchiveDeps: wholeArchiveDeps,
Jingwen Chen53681ef2021-04-29 08:15:13 +0000181 }
182}
183
Jingwen Chen107c0de2021-04-09 10:43:12 +0000184// Convenience struct to hold all attributes parsed from compiler properties.
185type compilerAttributes struct {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000186 copts bazel.StringListAttribute
187 srcs bazel.LabelListAttribute
188 includes bazel.StringListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000189}
190
Jingwen Chen63930982021-03-24 10:04:33 -0400191// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000192func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
Jingwen Chen882bcc12021-04-27 05:54:20 +0000193 var srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000194 var copts bazel.StringListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400195
Chris Parsons484e50a2021-05-13 15:13:04 -0400196 // Creates the -I flags for a directory, while making the directory relative
Jingwen Chened9c17d2021-04-13 07:14:55 +0000197 // to the exec root for Bazel to work.
Chris Parsons484e50a2021-05-13 15:13:04 -0400198 includeFlags := func(dir string) []string {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000199 // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements.
Chris Parsons484e50a2021-05-13 15:13:04 -0400200 moduleDirRootedPath := filepath.Join(ctx.ModuleDir(), dir)
201 return []string{
202 "-I" + moduleDirRootedPath,
203 // Include the bindir-rooted path (using make variable substitution). This most
204 // closely matches Bazel's native include path handling, which allows for dependency
205 // on generated headers in these directories.
206 // TODO(b/188084383): Handle local include directories in Bazel.
207 "-I$(BINDIR)/" + moduleDirRootedPath,
208 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000209 }
210
Jingwen Chened9c17d2021-04-13 07:14:55 +0000211 // Parse the list of module-relative include directories (-I).
212 parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string {
213 // include_dirs are root-relative, not module-relative.
214 includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
215 return append(includeDirs, baseCompilerProps.Local_include_dirs...)
216 }
217
218 // Parse the list of copts.
219 parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string {
Jingwen Chen3950cd62021-05-12 04:33:00 +0000220 var copts []string
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000221 for _, flag := range append(baseCompilerProps.Cflags, baseCompilerProps.Cppflags...) {
Jingwen Chen3950cd62021-05-12 04:33:00 +0000222 // Soong's cflags can contain spaces, like `-include header.h`. For
223 // Bazel's copts, split them up to be compatible with the
224 // no_copts_tokenization feature.
225 copts = append(copts, strings.Split(flag, " ")...)
226 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000227 for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
Chris Parsons484e50a2021-05-13 15:13:04 -0400228 copts = append(copts, includeFlags(dir)...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000229 }
230 return copts
Jingwen Chen63930982021-03-24 10:04:33 -0400231 }
232
Jingwen Chene32e9e02021-04-23 09:17:24 +0000233 // baseSrcs contain the list of src files that are used for every configuration.
234 var baseSrcs []string
235 // baseExcludeSrcs contain the list of src files that are excluded for every configuration.
236 var baseExcludeSrcs []string
237 // baseSrcsLabelList is a clone of the base srcs LabelList, used for computing the
238 // arch or os specific srcs later.
239 var baseSrcsLabelList bazel.LabelList
240
241 // Parse srcs from an arch or OS's props value, taking the base srcs and
242 // exclude srcs into account.
243 parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList {
244 // Combine the base srcs and arch-specific srcs
245 allSrcs := append(baseSrcs, baseCompilerProps.Srcs...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400246 // Add srcs-like dependencies such as generated files.
247 // First create a LabelList containing these dependencies, then merge the values with srcs.
248 generatedHdrsAndSrcs := baseCompilerProps.Generated_headers
249 generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...)
250
251 generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs)
252
Jingwen Chene32e9e02021-04-23 09:17:24 +0000253 // Combine the base exclude_srcs and configuration-specific exclude_srcs
254 allExcludeSrcs := append(baseExcludeSrcs, baseCompilerProps.Exclude_srcs...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400255 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, allSrcs, allExcludeSrcs)
256 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000257 }
258
Jingwen Chenc1c26502021-04-05 10:35:13 +0000259 for _, props := range module.compiler.compilerProps() {
260 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000261 srcs.Value = parseSrcs(baseCompilerProps)
262 copts.Value = parseCopts(baseCompilerProps)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000263
264 // Used for arch-specific srcs later.
265 baseSrcs = baseCompilerProps.Srcs
Jingwen Chene32e9e02021-04-23 09:17:24 +0000266 baseSrcsLabelList = parseSrcs(baseCompilerProps)
Chris Parsons484e50a2021-05-13 15:13:04 -0400267 baseExcludeSrcs = baseCompilerProps.Exclude_srcs
Jingwen Chenc1c26502021-04-05 10:35:13 +0000268 break
269 }
270 }
271
Jingwen Chene32e9e02021-04-23 09:17:24 +0000272 // Handle include_build_directory prop. If the property is true, then the
273 // target has access to all headers recursively in the package, and has
274 // "-I<module-dir>" in its copts.
Jingwen Chened9c17d2021-04-13 07:14:55 +0000275 if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400276 copts.Value = append(copts.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000277 } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400278 copts.Value = append(copts.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000279 }
280
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200281 for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000282 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000283 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
284 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
285 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
286 srcsList := parseSrcs(baseCompilerProps)
287 srcs.SetValueForArch(arch.Name, srcsList)
288 // The base srcs value should not contain any arch-specific excludes.
289 srcs.Value = bazel.SubtractBazelLabelList(srcs.Value, bazel.LabelList{Includes: srcsList.Excludes})
290 }
291
Jingwen Chened9c17d2021-04-13 07:14:55 +0000292 copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000293 }
294 }
295
Jingwen Chene32e9e02021-04-23 09:17:24 +0000296 // After going through all archs, delete the duplicate files in the arch
297 // values that are already in the base srcs.Value.
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200298 for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000299 if _, ok := props.(*BaseCompilerProperties); ok {
300 srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcs.GetValueForArch(arch.Name), srcs.Value))
301 }
302 }
303
304 // Now that the srcs.Value list is finalized, compare it with the original
305 // list, and put the difference into the default condition for the arch
306 // select.
307 defaultsSrcs := bazel.SubtractBazelLabelList(baseSrcsLabelList, srcs.Value)
308 // TODO(b/186153868): handle the case with multiple variant types, e.g. when arch and os are both used.
309 srcs.SetValueForArch(bazel.CONDITIONS_DEFAULT, defaultsSrcs)
310
311 // Handle OS specific props.
Lukacs T. Berki5f518392021-05-17 11:44:58 +0200312 for os, props := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000313 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000314 srcsList := parseSrcs(baseCompilerProps)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000315 // TODO(b/186153868): add support for os-specific srcs and exclude_srcs
316 srcs.SetValueForOS(os.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList))
Jingwen Chened9c17d2021-04-13 07:14:55 +0000317 copts.SetValueForOS(os.Name, parseCopts(baseCompilerProps))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000318 }
319 }
320
Jingwen Chen107c0de2021-04-09 10:43:12 +0000321 return compilerAttributes{
Jingwen Chen107c0de2021-04-09 10:43:12 +0000322 srcs: srcs,
323 copts: copts,
324 }
325}
326
327// Convenience struct to hold all attributes parsed from linker properties.
328type linkerAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400329 deps bazel.LabelListAttribute
330 dynamicDeps bazel.LabelListAttribute
331 wholeArchiveDeps bazel.LabelListAttribute
332 linkopts bazel.StringListAttribute
333 versionScript bazel.LabelAttribute
Jingwen Chenc1c26502021-04-05 10:35:13 +0000334}
335
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400336// FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here
337func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string {
338 flags := linkerProperties.Ldflags
339 if !BoolDefault(linkerProperties.Pack_relocations, true) {
340 flags = append(flags, "-Wl,--pack-dyn-relocs=none")
341 }
342 return flags
343}
344
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200345// bp2BuildParseLinkerProps parses the linker properties of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400346// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000347func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Jingwen Chen63930982021-03-24 10:04:33 -0400348 var deps bazel.LabelListAttribute
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400349 var dynamicDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400350 var wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400351 var linkopts bazel.StringListAttribute
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200352 var versionScript bazel.LabelAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400353
Jingwen Chen91220d72021-03-24 02:18:33 -0400354 for _, linkerProps := range module.linker.linkerProps() {
355 if baseLinkerProps, ok := linkerProps.(*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 Chened9c17d2021-04-13 07:14:55 +0000360 libs = android.SortedUniqueStrings(libs)
361 deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400362 linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps)
Chris Parsons08648312021-05-06 16:23:19 -0400363 wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200364
365 if baseLinkerProps.Version_script != nil {
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200366 versionScript.Value = android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200367 }
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400368
369 sharedLibs := baseLinkerProps.Shared_libs
370 dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, sharedLibs))
371
Jingwen Chen91220d72021-03-24 02:18:33 -0400372 break
373 }
374 }
375
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200376 for arch, p := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) {
Jingwen Chen63930982021-03-24 10:04:33 -0400377 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
378 libs := baseLinkerProps.Header_libs
379 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000380 libs = append(libs, baseLinkerProps.Static_libs...)
Chris Parsons08648312021-05-06 16:23:19 -0400381 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chen63930982021-03-24 10:04:33 -0400382 libs = android.SortedUniqueStrings(libs)
383 deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400384 linkopts.SetValueForArch(arch.Name, getBp2BuildLinkerFlags(baseLinkerProps))
Chris Parsons08648312021-05-06 16:23:19 -0400385 wholeArchiveDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400386
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200387 if baseLinkerProps.Version_script != nil {
388 versionScript.SetValueForArch(arch.Name,
389 android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
390 }
Rupert Shuttleworth3b413d32021-05-10 18:41:51 -0400391
392 sharedLibs := baseLinkerProps.Shared_libs
393 dynamicDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
Jingwen Chen63930982021-03-24 10:04:33 -0400394 }
395 }
396
Lukacs T. Berki5f518392021-05-17 11:44:58 +0200397 for os, p := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) {
Jingwen Chen91220d72021-03-24 02:18:33 -0400398 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
399 libs := baseLinkerProps.Header_libs
400 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000401 libs = append(libs, baseLinkerProps.Static_libs...)
Chris Parsons08648312021-05-06 16:23:19 -0400402 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chen91220d72021-03-24 02:18:33 -0400403 libs = android.SortedUniqueStrings(libs)
Chris Parsons08648312021-05-06 16:23:19 -0400404 wholeArchiveDeps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Jingwen Chen63930982021-03-24 10:04:33 -0400405 deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400406
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400407 linkopts.SetValueForOS(os.Name, getBp2BuildLinkerFlags(baseLinkerProps))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400408
409 sharedLibs := baseLinkerProps.Shared_libs
410 dynamicDeps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
Jingwen Chen91220d72021-03-24 02:18:33 -0400411 }
412 }
413
Jingwen Chen107c0de2021-04-09 10:43:12 +0000414 return linkerAttributes{
Chris Parsons08648312021-05-06 16:23:19 -0400415 deps: deps,
416 dynamicDeps: dynamicDeps,
417 wholeArchiveDeps: wholeArchiveDeps,
418 linkopts: linkopts,
419 versionScript: versionScript,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000420 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400421}
422
Jingwen Chened9c17d2021-04-13 07:14:55 +0000423// Relativize a list of root-relative paths with respect to the module's
424// directory.
425//
426// include_dirs Soong prop are root-relative (b/183742505), but
427// local_include_dirs, export_include_dirs and export_system_include_dirs are
428// module dir relative. This function makes a list of paths entirely module dir
429// relative.
430//
431// For the `include` attribute, Bazel wants the paths to be relative to the
432// module.
433func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000434 var relativePaths []string
435 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000436 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
437 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
438 if err != nil {
439 panic(err)
440 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000441 relativePaths = append(relativePaths, relativePath)
442 }
443 return relativePaths
444}
445
Jingwen Chened9c17d2021-04-13 07:14:55 +0000446// bp2BuildParseExportedIncludes creates a string list attribute contains the
Jingwen Chen882bcc12021-04-27 05:54:20 +0000447// exported included directories of a module.
448func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
Jingwen Chen91220d72021-03-24 02:18:33 -0400449 libraryDecorator := module.linker.(*libraryDecorator)
450
Jingwen Chened9c17d2021-04-13 07:14:55 +0000451 // Export_system_include_dirs and export_include_dirs are already module dir
452 // relative, so they don't need to be relativized like include_dirs, which
453 // are root-relative.
Jingwen Chen91220d72021-03-24 02:18:33 -0400454 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
455 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000456 includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
Jingwen Chen91220d72021-03-24 02:18:33 -0400457
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200458 for arch, props := range module.GetArchProperties(ctx, &FlagExporterProperties{}) {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000459 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
460 archIncludeDirs := flagExporterProperties.Export_system_include_dirs
461 archIncludeDirs = append(archIncludeDirs, flagExporterProperties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000462
463 // To avoid duplicate includes when base includes + arch includes are combined
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400464 // FIXME: This doesn't take conflicts between arch and os includes into account
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000465 archIncludeDirs = bazel.SubtractStrings(archIncludeDirs, includeDirs)
466
467 if len(archIncludeDirs) > 0 {
468 includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs)
469 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000470 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400471 }
472
Lukacs T. Berki5f518392021-05-17 11:44:58 +0200473 for os, props := range module.GetTargetProperties(ctx, &FlagExporterProperties{}) {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400474 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
475 osIncludeDirs := flagExporterProperties.Export_system_include_dirs
476 osIncludeDirs = append(osIncludeDirs, flagExporterProperties.Export_include_dirs...)
477
478 // To avoid duplicate includes when base includes + os includes are combined
479 // FIXME: This doesn't take conflicts between arch and os includes into account
480 osIncludeDirs = bazel.SubtractStrings(osIncludeDirs, includeDirs)
481
482 if len(osIncludeDirs) > 0 {
483 includeDirsAttribute.SetValueForOS(os.Name, osIncludeDirs)
484 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400485 }
486 }
487
Jingwen Chen882bcc12021-04-27 05:54:20 +0000488 return includeDirsAttribute
Jingwen Chen91220d72021-03-24 02:18:33 -0400489}