blob: 9bf101e1e6f05abbbd35793925cb7f63ad39de0c [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
Liz Kammer2222c6b2021-05-24 15:41:47 -0400108// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
109// properities which apply to either the shared or static version of a cc_library module.
110type staticOrSharedAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400111 copts bazel.StringListAttribute
112 srcs bazel.LabelListAttribute
113 staticDeps bazel.LabelListAttribute
114 dynamicDeps bazel.LabelListAttribute
115 wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +0000116}
117
118// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400119func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000120 lib, ok := module.compiler.(*libraryDecorator)
121 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400122 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000123 }
124
Liz Kammer2222c6b2021-05-24 15:41:47 -0400125 return bp2buildParseStaticOrSharedProps(ctx, lib.SharedProperties.Shared)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000126}
127
128// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400129func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000130 lib, ok := module.compiler.(*libraryDecorator)
131 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400132 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000133 }
134
Liz Kammer2222c6b2021-05-24 15:41:47 -0400135 return bp2buildParseStaticOrSharedProps(ctx, lib.StaticProperties.Static)
136}
137
138func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, props StaticOrSharedProperties) staticOrSharedAttributes {
139 copts := bazel.StringListAttribute{Value: props.Cflags}
Chris Parsons08648312021-05-06 16:23:19 -0400140
141 srcs := bazel.LabelListAttribute{
Liz Kammer2222c6b2021-05-24 15:41:47 -0400142 Value: android.BazelLabelForModuleSrc(ctx, props.Srcs)}
Chris Parsons08648312021-05-06 16:23:19 -0400143
144 staticDeps := bazel.LabelListAttribute{
Liz Kammer2222c6b2021-05-24 15:41:47 -0400145 Value: android.BazelLabelForModuleDeps(ctx, props.Static_libs)}
Chris Parsons08648312021-05-06 16:23:19 -0400146
147 dynamicDeps := bazel.LabelListAttribute{
Liz Kammer2222c6b2021-05-24 15:41:47 -0400148 Value: android.BazelLabelForModuleDeps(ctx, props.Shared_libs)}
Chris Parsons08648312021-05-06 16:23:19 -0400149
150 wholeArchiveDeps := bazel.LabelListAttribute{
Liz Kammer2222c6b2021-05-24 15:41:47 -0400151 Value: android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000152
Liz Kammer2222c6b2021-05-24 15:41:47 -0400153 return staticOrSharedAttributes{
Chris Parsons08648312021-05-06 16:23:19 -0400154 copts: copts,
155 srcs: srcs,
156 staticDeps: staticDeps,
157 dynamicDeps: dynamicDeps,
158 wholeArchiveDeps: wholeArchiveDeps,
Jingwen Chen53681ef2021-04-29 08:15:13 +0000159 }
160}
161
Jingwen Chen107c0de2021-04-09 10:43:12 +0000162// Convenience struct to hold all attributes parsed from compiler properties.
163type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400164 // Options for all languages
165 copts bazel.StringListAttribute
166 // Assembly options and sources
167 asFlags bazel.StringListAttribute
168 asSrcs bazel.LabelListAttribute
169 // C options and sources
170 conlyFlags bazel.StringListAttribute
171 cSrcs bazel.LabelListAttribute
172 // C++ options and sources
173 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000174 srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000175}
176
Jingwen Chen63930982021-03-24 10:04:33 -0400177// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000178func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
Jingwen Chen882bcc12021-04-27 05:54:20 +0000179 var srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000180 var copts bazel.StringListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400181 var asFlags bazel.StringListAttribute
182 var conlyFlags bazel.StringListAttribute
183 var cppFlags bazel.StringListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400184
Chris Parsons484e50a2021-05-13 15:13:04 -0400185 // Creates the -I flags for a directory, while making the directory relative
Jingwen Chened9c17d2021-04-13 07:14:55 +0000186 // to the exec root for Bazel to work.
Chris Parsons484e50a2021-05-13 15:13:04 -0400187 includeFlags := func(dir string) []string {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000188 // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements.
Chris Parsons484e50a2021-05-13 15:13:04 -0400189 moduleDirRootedPath := filepath.Join(ctx.ModuleDir(), dir)
190 return []string{
191 "-I" + moduleDirRootedPath,
192 // Include the bindir-rooted path (using make variable substitution). This most
193 // closely matches Bazel's native include path handling, which allows for dependency
194 // on generated headers in these directories.
195 // TODO(b/188084383): Handle local include directories in Bazel.
196 "-I$(BINDIR)/" + moduleDirRootedPath,
197 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000198 }
199
Jingwen Chened9c17d2021-04-13 07:14:55 +0000200 // Parse the list of module-relative include directories (-I).
201 parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string {
202 // include_dirs are root-relative, not module-relative.
203 includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
204 return append(includeDirs, baseCompilerProps.Local_include_dirs...)
205 }
206
Chris Parsons990c4f42021-05-25 12:10:58 -0400207 parseCommandLineFlags := func(soongFlags []string) []string {
208 var result []string
209 for _, flag := range soongFlags {
Colin Cross52aa4e12021-05-25 15:20:39 +0000210 // Soong's cflags can contain spaces, like `-include header.h`. For
211 // Bazel's copts, split them up to be compatible with the
212 // no_copts_tokenization feature.
Chris Parsons990c4f42021-05-25 12:10:58 -0400213 result = append(result, strings.Split(flag, " ")...)
Colin Cross52aa4e12021-05-25 15:20:39 +0000214 }
Chris Parsons990c4f42021-05-25 12:10:58 -0400215 return result
216 }
217
218 // Parse the list of copts.
219 parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string {
220 var copts []string
221 copts = append(copts, parseCommandLineFlags(baseCompilerProps.Cflags)...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000222 for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
Chris Parsons484e50a2021-05-13 15:13:04 -0400223 copts = append(copts, includeFlags(dir)...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000224 }
225 return copts
Jingwen Chen63930982021-03-24 10:04:33 -0400226 }
227
Jingwen Chene32e9e02021-04-23 09:17:24 +0000228 // baseSrcs contain the list of src files that are used for every configuration.
229 var baseSrcs []string
230 // baseExcludeSrcs contain the list of src files that are excluded for every configuration.
231 var baseExcludeSrcs []string
232 // baseSrcsLabelList is a clone of the base srcs LabelList, used for computing the
233 // arch or os specific srcs later.
234 var baseSrcsLabelList bazel.LabelList
235
236 // Parse srcs from an arch or OS's props value, taking the base srcs and
237 // exclude srcs into account.
238 parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList {
239 // Combine the base srcs and arch-specific srcs
240 allSrcs := append(baseSrcs, baseCompilerProps.Srcs...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400241 // Add srcs-like dependencies such as generated files.
242 // First create a LabelList containing these dependencies, then merge the values with srcs.
243 generatedHdrsAndSrcs := baseCompilerProps.Generated_headers
244 generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...)
245
246 generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs)
247
Jingwen Chene32e9e02021-04-23 09:17:24 +0000248 // Combine the base exclude_srcs and configuration-specific exclude_srcs
249 allExcludeSrcs := append(baseExcludeSrcs, baseCompilerProps.Exclude_srcs...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400250 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, allSrcs, allExcludeSrcs)
251 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000252 }
253
Jingwen Chenc1c26502021-04-05 10:35:13 +0000254 for _, props := range module.compiler.compilerProps() {
255 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000256 srcs.Value = parseSrcs(baseCompilerProps)
257 copts.Value = parseCopts(baseCompilerProps)
Chris Parsons990c4f42021-05-25 12:10:58 -0400258 asFlags.Value = parseCommandLineFlags(baseCompilerProps.Asflags)
259 conlyFlags.Value = parseCommandLineFlags(baseCompilerProps.Conlyflags)
260 cppFlags.Value = parseCommandLineFlags(baseCompilerProps.Cppflags)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000261
262 // Used for arch-specific srcs later.
263 baseSrcs = baseCompilerProps.Srcs
Jingwen Chene32e9e02021-04-23 09:17:24 +0000264 baseSrcsLabelList = parseSrcs(baseCompilerProps)
Chris Parsons484e50a2021-05-13 15:13:04 -0400265 baseExcludeSrcs = baseCompilerProps.Exclude_srcs
Jingwen Chenc1c26502021-04-05 10:35:13 +0000266 break
267 }
268 }
269
Jingwen Chene32e9e02021-04-23 09:17:24 +0000270 // Handle include_build_directory prop. If the property is true, then the
271 // target has access to all headers recursively in the package, and has
272 // "-I<module-dir>" in its copts.
Jingwen Chened9c17d2021-04-13 07:14:55 +0000273 if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400274 copts.Value = append(copts.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000275 } else if c, ok := module.compiler.(*libraryDecorator); 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 }
278
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200279 for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000280 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000281 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
282 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
283 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
284 srcsList := parseSrcs(baseCompilerProps)
285 srcs.SetValueForArch(arch.Name, srcsList)
286 // The base srcs value should not contain any arch-specific excludes.
287 srcs.Value = bazel.SubtractBazelLabelList(srcs.Value, bazel.LabelList{Includes: srcsList.Excludes})
288 }
289
Jingwen Chened9c17d2021-04-13 07:14:55 +0000290 copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps))
Chris Parsons990c4f42021-05-25 12:10:58 -0400291 asFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Asflags))
292 conlyFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags))
293 cppFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Cppflags))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000294 }
295 }
296
Jingwen Chene32e9e02021-04-23 09:17:24 +0000297 // After going through all archs, delete the duplicate files in the arch
298 // values that are already in the base srcs.Value.
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200299 for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000300 if _, ok := props.(*BaseCompilerProperties); ok {
301 srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcs.GetValueForArch(arch.Name), srcs.Value))
302 }
303 }
304
305 // Now that the srcs.Value list is finalized, compare it with the original
306 // list, and put the difference into the default condition for the arch
307 // select.
308 defaultsSrcs := bazel.SubtractBazelLabelList(baseSrcsLabelList, srcs.Value)
309 // TODO(b/186153868): handle the case with multiple variant types, e.g. when arch and os are both used.
310 srcs.SetValueForArch(bazel.CONDITIONS_DEFAULT, defaultsSrcs)
311
312 // Handle OS specific props.
Lukacs T. Berki5f518392021-05-17 11:44:58 +0200313 for os, props := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000314 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000315 srcsList := parseSrcs(baseCompilerProps)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000316 // TODO(b/186153868): add support for os-specific srcs and exclude_srcs
317 srcs.SetValueForOS(os.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList))
Jingwen Chened9c17d2021-04-13 07:14:55 +0000318 copts.SetValueForOS(os.Name, parseCopts(baseCompilerProps))
Chris Parsons990c4f42021-05-25 12:10:58 -0400319 asFlags.SetValueForOS(os.Name, parseCommandLineFlags(baseCompilerProps.Asflags))
320 conlyFlags.SetValueForOS(os.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags))
321 cppFlags.SetValueForOS(os.Name, parseCommandLineFlags(baseCompilerProps.Cppflags))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000322 }
323 }
324
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400325 productVariableProps := android.ProductVariableProperties(ctx)
326 if props, exists := productVariableProps["Cflags"]; exists {
327 for _, prop := range props {
328 flags, ok := prop.Property.([]string)
329 if !ok {
330 ctx.ModuleErrorf("Could not convert product variable cflag property")
331 }
332 newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable)
333 copts.ProductValues = append(copts.ProductValues, bazel.ProductVariableValues{
334 ProductVariable: prop.ProductConfigVariable,
335 Values: newFlags,
336 })
337 }
338 }
339
Chris Parsons990c4f42021-05-25 12:10:58 -0400340 // Branch srcs into three language-specific groups.
341 // C++ is the "catch-all" group, and comprises generated sources because we don't
342 // know the language of these sources until the genrule is executed.
343 // TODO(b/): Handle language detection of sources in a Bazel rule.
344 isCSrc := func(s string) bool {
345 return strings.HasSuffix(s, ".c")
346 }
347 isAsmSrc := func(s string) bool {
348 return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s")
349 }
350 cSrcs := bazel.FilterLabelListAttribute(srcs, isCSrc)
351 asSrcs := bazel.FilterLabelListAttribute(srcs, isAsmSrc)
352 srcs = bazel.SubtractBazelLabelListAttribute(srcs, cSrcs)
353 srcs = bazel.SubtractBazelLabelListAttribute(srcs, asSrcs)
Jingwen Chen107c0de2021-04-09 10:43:12 +0000354 return compilerAttributes{
Chris Parsons990c4f42021-05-25 12:10:58 -0400355 copts: copts,
356 srcs: srcs,
357 asFlags: asFlags,
358 asSrcs: asSrcs,
359 cSrcs: cSrcs,
360 conlyFlags: conlyFlags,
361 cppFlags: cppFlags,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000362 }
363}
364
365// Convenience struct to hold all attributes parsed from linker properties.
366type linkerAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400367 deps bazel.LabelListAttribute
368 dynamicDeps bazel.LabelListAttribute
369 wholeArchiveDeps bazel.LabelListAttribute
Chris Parsonsd6358772021-05-18 18:35:24 -0400370 exportedDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400371 linkopts bazel.StringListAttribute
372 versionScript bazel.LabelAttribute
Jingwen Chenc1c26502021-04-05 10:35:13 +0000373}
374
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400375// FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here
376func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string {
377 flags := linkerProperties.Ldflags
378 if !BoolDefault(linkerProperties.Pack_relocations, true) {
379 flags = append(flags, "-Wl,--pack-dyn-relocs=none")
380 }
381 return flags
382}
383
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200384// bp2BuildParseLinkerProps parses the linker properties of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400385// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000386func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Jingwen Chen63930982021-03-24 10:04:33 -0400387 var deps bazel.LabelListAttribute
Chris Parsonsd6358772021-05-18 18:35:24 -0400388 var exportedDeps bazel.LabelListAttribute
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400389 var dynamicDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400390 var wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400391 var linkopts bazel.StringListAttribute
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200392 var versionScript bazel.LabelAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400393
Jingwen Chen91220d72021-03-24 02:18:33 -0400394 for _, linkerProps := range module.linker.linkerProps() {
395 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
396 libs := baseLinkerProps.Header_libs
Jingwen Chened9c17d2021-04-13 07:14:55 +0000397 libs = append(libs, baseLinkerProps.Static_libs...)
Chris Parsonsd6358772021-05-18 18:35:24 -0400398 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400399 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chened9c17d2021-04-13 07:14:55 +0000400 libs = android.SortedUniqueStrings(libs)
401 deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs))
Chris Parsonsd6358772021-05-18 18:35:24 -0400402 exportedDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400403 linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps)
Chris Parsons08648312021-05-06 16:23:19 -0400404 wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200405
406 if baseLinkerProps.Version_script != nil {
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200407 versionScript.Value = android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200408 }
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400409
410 sharedLibs := baseLinkerProps.Shared_libs
411 dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, sharedLibs))
412
Jingwen Chen91220d72021-03-24 02:18:33 -0400413 break
414 }
415 }
416
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200417 for arch, p := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) {
Jingwen Chen63930982021-03-24 10:04:33 -0400418 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
419 libs := baseLinkerProps.Header_libs
Jingwen Chened9c17d2021-04-13 07:14:55 +0000420 libs = append(libs, baseLinkerProps.Static_libs...)
Chris Parsonsd6358772021-05-18 18:35:24 -0400421 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400422 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chen63930982021-03-24 10:04:33 -0400423 libs = android.SortedUniqueStrings(libs)
424 deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
Chris Parsonsd6358772021-05-18 18:35:24 -0400425 exportedDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400426 linkopts.SetValueForArch(arch.Name, getBp2BuildLinkerFlags(baseLinkerProps))
Chris Parsons08648312021-05-06 16:23:19 -0400427 wholeArchiveDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400428
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200429 if baseLinkerProps.Version_script != nil {
430 versionScript.SetValueForArch(arch.Name,
431 android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
432 }
Rupert Shuttleworth3b413d32021-05-10 18:41:51 -0400433
434 sharedLibs := baseLinkerProps.Shared_libs
435 dynamicDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
Jingwen Chen63930982021-03-24 10:04:33 -0400436 }
437 }
438
Lukacs T. Berki5f518392021-05-17 11:44:58 +0200439 for os, p := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) {
Jingwen Chen91220d72021-03-24 02:18:33 -0400440 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
441 libs := baseLinkerProps.Header_libs
Jingwen Chened9c17d2021-04-13 07:14:55 +0000442 libs = append(libs, baseLinkerProps.Static_libs...)
Chris Parsonsd6358772021-05-18 18:35:24 -0400443 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400444 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chen91220d72021-03-24 02:18:33 -0400445 libs = android.SortedUniqueStrings(libs)
Chris Parsons08648312021-05-06 16:23:19 -0400446 wholeArchiveDeps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Jingwen Chen63930982021-03-24 10:04:33 -0400447 deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
Chris Parsonsd6358772021-05-18 18:35:24 -0400448 exportedDeps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400449
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400450 linkopts.SetValueForOS(os.Name, getBp2BuildLinkerFlags(baseLinkerProps))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400451
452 sharedLibs := baseLinkerProps.Shared_libs
453 dynamicDeps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
Jingwen Chen91220d72021-03-24 02:18:33 -0400454 }
455 }
456
Jingwen Chen107c0de2021-04-09 10:43:12 +0000457 return linkerAttributes{
Chris Parsons08648312021-05-06 16:23:19 -0400458 deps: deps,
Chris Parsonsd6358772021-05-18 18:35:24 -0400459 exportedDeps: exportedDeps,
Chris Parsons08648312021-05-06 16:23:19 -0400460 dynamicDeps: dynamicDeps,
461 wholeArchiveDeps: wholeArchiveDeps,
462 linkopts: linkopts,
463 versionScript: versionScript,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000464 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400465}
466
Jingwen Chened9c17d2021-04-13 07:14:55 +0000467// Relativize a list of root-relative paths with respect to the module's
468// directory.
469//
470// include_dirs Soong prop are root-relative (b/183742505), but
471// local_include_dirs, export_include_dirs and export_system_include_dirs are
472// module dir relative. This function makes a list of paths entirely module dir
473// relative.
474//
475// For the `include` attribute, Bazel wants the paths to be relative to the
476// module.
477func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000478 var relativePaths []string
479 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000480 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
481 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
482 if err != nil {
483 panic(err)
484 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000485 relativePaths = append(relativePaths, relativePath)
486 }
487 return relativePaths
488}
489
Jingwen Chened9c17d2021-04-13 07:14:55 +0000490// bp2BuildParseExportedIncludes creates a string list attribute contains the
Jingwen Chen882bcc12021-04-27 05:54:20 +0000491// exported included directories of a module.
492func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
Jingwen Chen91220d72021-03-24 02:18:33 -0400493 libraryDecorator := module.linker.(*libraryDecorator)
494
Jingwen Chened9c17d2021-04-13 07:14:55 +0000495 // Export_system_include_dirs and export_include_dirs are already module dir
496 // relative, so they don't need to be relativized like include_dirs, which
497 // are root-relative.
Jingwen Chen91220d72021-03-24 02:18:33 -0400498 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
499 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000500 includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
Jingwen Chen91220d72021-03-24 02:18:33 -0400501
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200502 for arch, props := range module.GetArchProperties(ctx, &FlagExporterProperties{}) {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000503 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
504 archIncludeDirs := flagExporterProperties.Export_system_include_dirs
505 archIncludeDirs = append(archIncludeDirs, flagExporterProperties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000506
507 // To avoid duplicate includes when base includes + arch includes are combined
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400508 // FIXME: This doesn't take conflicts between arch and os includes into account
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000509 archIncludeDirs = bazel.SubtractStrings(archIncludeDirs, includeDirs)
510
511 if len(archIncludeDirs) > 0 {
512 includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs)
513 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000514 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400515 }
516
Lukacs T. Berki5f518392021-05-17 11:44:58 +0200517 for os, props := range module.GetTargetProperties(ctx, &FlagExporterProperties{}) {
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400518 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
519 osIncludeDirs := flagExporterProperties.Export_system_include_dirs
520 osIncludeDirs = append(osIncludeDirs, flagExporterProperties.Export_include_dirs...)
521
522 // To avoid duplicate includes when base includes + os includes are combined
523 // FIXME: This doesn't take conflicts between arch and os includes into account
524 osIncludeDirs = bazel.SubtractStrings(osIncludeDirs, includeDirs)
525
526 if len(osIncludeDirs) > 0 {
527 includeDirsAttribute.SetValueForOS(os.Name, osIncludeDirs)
528 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400529 }
530 }
531
Jingwen Chen882bcc12021-04-27 05:54:20 +0000532 return includeDirsAttribute
Jingwen Chen91220d72021-03-24 02:18:33 -0400533}