blob: 5d024f8651be1189a58c483cb09803b2485497e2 [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
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040053 for _, osProps := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) {
54 // os base compiler props
55 if baseCompilerProps, ok := osProps.Properties.(*BaseCompilerProperties); ok {
Chris Parsons484e50a2021-05-13 15:13:04 -040056 allDeps = append(allDeps, baseCompilerProps.Generated_headers...)
57 allDeps = append(allDeps, baseCompilerProps.Generated_sources...)
58 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040059 // os + arch base compiler props
60 for _, archProps := range osProps.ArchProperties {
61 if baseCompilerProps, ok := archProps.(*BaseCompilerProperties); ok {
62 allDeps = append(allDeps, baseCompilerProps.Generated_headers...)
63 allDeps = append(allDeps, baseCompilerProps.Generated_sources...)
64 }
65 }
Chris Parsons484e50a2021-05-13 15:13:04 -040066 }
67
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040068 for _, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Chris Parsons484e50a2021-05-13 15:13:04 -040069 // arch specific compiler props
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040070 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Chris Parsons484e50a2021-05-13 15:13:04 -040071 allDeps = append(allDeps, baseCompilerProps.Generated_headers...)
72 allDeps = append(allDeps, baseCompilerProps.Generated_sources...)
73 }
74 }
75
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040076 for _, osProps := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) {
77 // os specific linker props
78 if baseLinkerProps, ok := osProps.Properties.(*BaseLinkerProperties); ok {
Jingwen Chen91220d72021-03-24 02:18:33 -040079 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
80 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +000081 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
82 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
83 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040084 // os + arch base compiler props
85 for _, archProps := range osProps.ArchProperties {
86 if baseLinkerProps, ok := archProps.(*BaseLinkerProperties); ok {
87 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
88 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
89 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
90 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
91 }
92 }
Jingwen Chened9c17d2021-04-13 07:14:55 +000093 }
94
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040095 for _, props := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) {
Jingwen Chened9c17d2021-04-13 07:14:55 +000096 // arch specific linker props
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040097 if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +000098 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
99 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
100 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
101 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
Jingwen Chen91220d72021-03-24 02:18:33 -0400102 }
103 }
104
Jingwen Chen53681ef2021-04-29 08:15:13 +0000105 // Deps in the static: { .. } and shared: { .. } props of a cc_library.
106 if lib, ok := module.compiler.(*libraryDecorator); ok {
Jingwen Chenbcf53042021-05-26 04:42:42 +0000107 appendDeps := func(deps []string, p StaticOrSharedProperties) []string {
108 deps = append(deps, p.Static_libs...)
109 deps = append(deps, p.Whole_static_libs...)
110 deps = append(deps, p.Shared_libs...)
111 return deps
112 }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000113
Jingwen Chenbcf53042021-05-26 04:42:42 +0000114 allDeps = appendDeps(allDeps, lib.SharedProperties.Shared)
115 allDeps = appendDeps(allDeps, lib.StaticProperties.Static)
Jingwen Chen45dec102021-05-19 10:30:29 +0000116
117 // TODO(b/186024507, b/186489250): Temporarily exclude adding
118 // system_shared_libs deps until libc and libm builds.
119 // allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...)
120 // allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000121
122 // Deps in the target/arch nested static: { .. } and shared: { .. } props of a cc_library.
123 // target: { <target>: shared: { ... } }
124 for _, targetProps := range module.GetTargetProperties(ctx, &SharedProperties{}) {
125 if p, ok := targetProps.Properties.(*SharedProperties); ok {
126 allDeps = appendDeps(allDeps, p.Shared)
127 }
128 for _, archProperties := range targetProps.ArchProperties {
129 if p, ok := archProperties.(*SharedProperties); ok {
130 allDeps = appendDeps(allDeps, p.Shared)
131 }
132 }
133 }
134 // target: { <target>: static: { ... } }
135 for _, targetProps := range module.GetTargetProperties(ctx, &StaticProperties{}) {
136 if p, ok := targetProps.Properties.(*StaticProperties); ok {
137 allDeps = appendDeps(allDeps, p.Static)
138 }
139 for _, archProperties := range targetProps.ArchProperties {
140 if p, ok := archProperties.(*StaticProperties); ok {
141 allDeps = appendDeps(allDeps, p.Static)
142 }
143 }
144 }
145 // arch: { <arch>: shared: { ... } }
146 for _, properties := range module.GetArchProperties(ctx, &SharedProperties{}) {
147 if p, ok := properties.(*SharedProperties); ok {
148 allDeps = appendDeps(allDeps, p.Shared)
149 }
150 }
151 // arch: { <arch>: static: { ... } }
152 for _, properties := range module.GetArchProperties(ctx, &StaticProperties{}) {
153 if p, ok := properties.(*StaticProperties); ok {
154 allDeps = appendDeps(allDeps, p.Static)
155 }
156 }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000157 }
158
Jingwen Chen91220d72021-03-24 02:18:33 -0400159 ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...)
160}
161
Liz Kammer2222c6b2021-05-24 15:41:47 -0400162// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
Jingwen Chenbcf53042021-05-26 04:42:42 +0000163// properties which apply to either the shared or static version of a cc_library module.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400164type staticOrSharedAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400165 copts bazel.StringListAttribute
166 srcs bazel.LabelListAttribute
167 staticDeps bazel.LabelListAttribute
168 dynamicDeps bazel.LabelListAttribute
169 wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +0000170}
171
172// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400173func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000174 lib, ok := module.compiler.(*libraryDecorator)
175 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400176 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000177 }
178
Jingwen Chenbcf53042021-05-26 04:42:42 +0000179 return bp2buildParseStaticOrSharedProps(ctx, module, lib, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000180}
181
182// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400183func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000184 lib, ok := module.compiler.(*libraryDecorator)
185 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400186 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000187 }
188
Jingwen Chenbcf53042021-05-26 04:42:42 +0000189 return bp2buildParseStaticOrSharedProps(ctx, module, lib, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400190}
191
Jingwen Chenbcf53042021-05-26 04:42:42 +0000192func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes {
193 var props StaticOrSharedProperties
194 if isStatic {
195 props = lib.StaticProperties.Static
196 } else {
197 props = lib.SharedProperties.Shared
Jingwen Chen53681ef2021-04-29 08:15:13 +0000198 }
Jingwen Chenbcf53042021-05-26 04:42:42 +0000199
200 attrs := staticOrSharedAttributes{
201 copts: bazel.StringListAttribute{Value: props.Cflags},
202 srcs: bazel.LabelListAttribute{Value: android.BazelLabelForModuleSrc(ctx, props.Srcs)},
203 staticDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Static_libs)},
204 dynamicDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Shared_libs)},
205 wholeArchiveDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)},
206 }
207
208 setArchAttrs := func(arch string, props StaticOrSharedProperties) {
209 attrs.copts.SetValueForArch(arch, props.Cflags)
210 attrs.srcs.SetValueForArch(arch, android.BazelLabelForModuleSrc(ctx, props.Srcs))
211 attrs.staticDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Static_libs))
212 attrs.dynamicDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Shared_libs))
213 attrs.wholeArchiveDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs))
214 }
215
216 setTargetAttrs := func(target string, props StaticOrSharedProperties) {
217 attrs.copts.SetOsValueForTarget(target, props.Cflags)
218 attrs.srcs.SetOsValueForTarget(target, android.BazelLabelForModuleSrc(ctx, props.Srcs))
219 attrs.staticDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Static_libs))
220 attrs.dynamicDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Shared_libs))
221 attrs.wholeArchiveDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs))
222 }
223
224 setTargetArchAttrs := func(target, arch string, props StaticOrSharedProperties) {
225 attrs.copts.SetOsArchValueForTarget(target, arch, props.Cflags)
226 attrs.srcs.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleSrc(ctx, props.Srcs))
227 attrs.staticDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Static_libs))
228 attrs.dynamicDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Shared_libs))
229 attrs.wholeArchiveDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs))
230 }
231
232 if isStatic {
233 for arch, properties := range module.GetArchProperties(ctx, &StaticProperties{}) {
234 if staticOrSharedProps, ok := properties.(*StaticProperties); ok {
235 setArchAttrs(arch.Name, staticOrSharedProps.Static)
236 }
237 }
238 for target, p := range module.GetTargetProperties(ctx, &StaticProperties{}) {
239 if staticOrSharedProps, ok := p.Properties.(*StaticProperties); ok {
240 setTargetAttrs(target.Name, staticOrSharedProps.Static)
241 }
242 for arch, archProperties := range p.ArchProperties {
243 if staticOrSharedProps, ok := archProperties.(*StaticProperties); ok {
244 setTargetArchAttrs(target.Name, arch.Name, staticOrSharedProps.Static)
245 }
246 }
247 }
248 } else {
249 for arch, p := range module.GetArchProperties(ctx, &SharedProperties{}) {
250 if staticOrSharedProps, ok := p.(*SharedProperties); ok {
251 setArchAttrs(arch.Name, staticOrSharedProps.Shared)
252 }
253 }
254 for target, p := range module.GetTargetProperties(ctx, &SharedProperties{}) {
255 if staticOrSharedProps, ok := p.Properties.(*SharedProperties); ok {
256 setTargetAttrs(target.Name, staticOrSharedProps.Shared)
257 }
258 for arch, archProperties := range p.ArchProperties {
259 if staticOrSharedProps, ok := archProperties.(*SharedProperties); ok {
260 setTargetArchAttrs(target.Name, arch.Name, staticOrSharedProps.Shared)
261 }
262 }
263 }
264 }
265
266 return attrs
Jingwen Chen53681ef2021-04-29 08:15:13 +0000267}
268
Jingwen Chen107c0de2021-04-09 10:43:12 +0000269// Convenience struct to hold all attributes parsed from compiler properties.
270type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400271 // Options for all languages
272 copts bazel.StringListAttribute
273 // Assembly options and sources
274 asFlags bazel.StringListAttribute
275 asSrcs bazel.LabelListAttribute
276 // C options and sources
277 conlyFlags bazel.StringListAttribute
278 cSrcs bazel.LabelListAttribute
279 // C++ options and sources
280 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000281 srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000282}
283
Jingwen Chen63930982021-03-24 10:04:33 -0400284// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000285func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
Jingwen Chen882bcc12021-04-27 05:54:20 +0000286 var srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000287 var copts bazel.StringListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400288 var asFlags bazel.StringListAttribute
289 var conlyFlags bazel.StringListAttribute
290 var cppFlags bazel.StringListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400291
Chris Parsons484e50a2021-05-13 15:13:04 -0400292 // Creates the -I flags for a directory, while making the directory relative
Jingwen Chened9c17d2021-04-13 07:14:55 +0000293 // to the exec root for Bazel to work.
Chris Parsons484e50a2021-05-13 15:13:04 -0400294 includeFlags := func(dir string) []string {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000295 // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements.
Chris Parsons484e50a2021-05-13 15:13:04 -0400296 moduleDirRootedPath := filepath.Join(ctx.ModuleDir(), dir)
297 return []string{
298 "-I" + moduleDirRootedPath,
299 // Include the bindir-rooted path (using make variable substitution). This most
300 // closely matches Bazel's native include path handling, which allows for dependency
301 // on generated headers in these directories.
302 // TODO(b/188084383): Handle local include directories in Bazel.
303 "-I$(BINDIR)/" + moduleDirRootedPath,
304 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000305 }
306
Jingwen Chened9c17d2021-04-13 07:14:55 +0000307 // Parse the list of module-relative include directories (-I).
308 parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string {
309 // include_dirs are root-relative, not module-relative.
310 includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
311 return append(includeDirs, baseCompilerProps.Local_include_dirs...)
312 }
313
Chris Parsons990c4f42021-05-25 12:10:58 -0400314 parseCommandLineFlags := func(soongFlags []string) []string {
315 var result []string
316 for _, flag := range soongFlags {
Colin Cross52aa4e12021-05-25 15:20:39 +0000317 // Soong's cflags can contain spaces, like `-include header.h`. For
318 // Bazel's copts, split them up to be compatible with the
319 // no_copts_tokenization feature.
Chris Parsons990c4f42021-05-25 12:10:58 -0400320 result = append(result, strings.Split(flag, " ")...)
Colin Cross52aa4e12021-05-25 15:20:39 +0000321 }
Chris Parsons990c4f42021-05-25 12:10:58 -0400322 return result
323 }
324
325 // Parse the list of copts.
326 parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string {
327 var copts []string
328 copts = append(copts, parseCommandLineFlags(baseCompilerProps.Cflags)...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000329 for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
Chris Parsons484e50a2021-05-13 15:13:04 -0400330 copts = append(copts, includeFlags(dir)...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000331 }
332 return copts
Jingwen Chen63930982021-03-24 10:04:33 -0400333 }
334
Jingwen Chene32e9e02021-04-23 09:17:24 +0000335 // baseSrcs contain the list of src files that are used for every configuration.
336 var baseSrcs []string
337 // baseExcludeSrcs contain the list of src files that are excluded for every configuration.
338 var baseExcludeSrcs []string
339 // baseSrcsLabelList is a clone of the base srcs LabelList, used for computing the
340 // arch or os specific srcs later.
341 var baseSrcsLabelList bazel.LabelList
342
343 // Parse srcs from an arch or OS's props value, taking the base srcs and
344 // exclude srcs into account.
345 parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList {
346 // Combine the base srcs and arch-specific srcs
347 allSrcs := append(baseSrcs, baseCompilerProps.Srcs...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400348 // Add srcs-like dependencies such as generated files.
349 // First create a LabelList containing these dependencies, then merge the values with srcs.
350 generatedHdrsAndSrcs := baseCompilerProps.Generated_headers
351 generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...)
352
353 generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs)
354
Jingwen Chene32e9e02021-04-23 09:17:24 +0000355 // Combine the base exclude_srcs and configuration-specific exclude_srcs
356 allExcludeSrcs := append(baseExcludeSrcs, baseCompilerProps.Exclude_srcs...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400357 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, allSrcs, allExcludeSrcs)
358 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000359 }
360
Jingwen Chenc1c26502021-04-05 10:35:13 +0000361 for _, props := range module.compiler.compilerProps() {
362 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000363 srcs.Value = parseSrcs(baseCompilerProps)
364 copts.Value = parseCopts(baseCompilerProps)
Chris Parsons990c4f42021-05-25 12:10:58 -0400365 asFlags.Value = parseCommandLineFlags(baseCompilerProps.Asflags)
366 conlyFlags.Value = parseCommandLineFlags(baseCompilerProps.Conlyflags)
367 cppFlags.Value = parseCommandLineFlags(baseCompilerProps.Cppflags)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000368
369 // Used for arch-specific srcs later.
370 baseSrcs = baseCompilerProps.Srcs
Jingwen Chene32e9e02021-04-23 09:17:24 +0000371 baseSrcsLabelList = parseSrcs(baseCompilerProps)
Chris Parsons484e50a2021-05-13 15:13:04 -0400372 baseExcludeSrcs = baseCompilerProps.Exclude_srcs
Jingwen Chenc1c26502021-04-05 10:35:13 +0000373 break
374 }
375 }
376
Jingwen Chene32e9e02021-04-23 09:17:24 +0000377 // Handle include_build_directory prop. If the property is true, then the
378 // target has access to all headers recursively in the package, and has
379 // "-I<module-dir>" in its copts.
Jingwen Chened9c17d2021-04-13 07:14:55 +0000380 if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400381 copts.Value = append(copts.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000382 } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400383 copts.Value = append(copts.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000384 }
385
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200386 for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000387 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000388 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
389 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
390 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
391 srcsList := parseSrcs(baseCompilerProps)
392 srcs.SetValueForArch(arch.Name, srcsList)
393 // The base srcs value should not contain any arch-specific excludes.
394 srcs.Value = bazel.SubtractBazelLabelList(srcs.Value, bazel.LabelList{Includes: srcsList.Excludes})
395 }
396
Jingwen Chened9c17d2021-04-13 07:14:55 +0000397 copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps))
Chris Parsons990c4f42021-05-25 12:10:58 -0400398 asFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Asflags))
399 conlyFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags))
400 cppFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Cppflags))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000401 }
402 }
403
Jingwen Chene32e9e02021-04-23 09:17:24 +0000404 // After going through all archs, delete the duplicate files in the arch
405 // values that are already in the base srcs.Value.
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200406 for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000407 if _, ok := props.(*BaseCompilerProperties); ok {
408 srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcs.GetValueForArch(arch.Name), srcs.Value))
409 }
410 }
411
412 // Now that the srcs.Value list is finalized, compare it with the original
413 // list, and put the difference into the default condition for the arch
414 // select.
415 defaultsSrcs := bazel.SubtractBazelLabelList(baseSrcsLabelList, srcs.Value)
416 // TODO(b/186153868): handle the case with multiple variant types, e.g. when arch and os are both used.
417 srcs.SetValueForArch(bazel.CONDITIONS_DEFAULT, defaultsSrcs)
418
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400419 // Handle target specific properties.
420 for os, osProps := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) {
421 if baseCompilerProps, ok := osProps.Properties.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000422 srcsList := parseSrcs(baseCompilerProps)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000423 // TODO(b/186153868): add support for os-specific srcs and exclude_srcs
Liz Kammer2b07ec72021-05-26 15:08:27 -0400424 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
425 srcs.SetOsValueForTarget(os.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList))
426 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400427 copts.SetOsValueForTarget(os.Name, parseCopts(baseCompilerProps))
428 asFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Asflags))
429 conlyFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags))
430 cppFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Cppflags))
431 }
432 for arch, archProps := range osProps.ArchProperties {
433 if baseCompilerProps, ok := archProps.(*BaseCompilerProperties); ok {
434 srcsList := parseSrcs(baseCompilerProps)
435 // TODO(b/186153868): add support for os-specific srcs and exclude_srcs
Liz Kammer2b07ec72021-05-26 15:08:27 -0400436 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
437 srcs.SetOsArchValueForTarget(os.Name, arch.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList))
438 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400439 copts.SetOsArchValueForTarget(os.Name, arch.Name, parseCopts(baseCompilerProps))
440 asFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Asflags))
441 conlyFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags))
442 cppFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Cppflags))
443 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000444 }
445 }
446
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400447 productVariableProps := android.ProductVariableProperties(ctx)
448 if props, exists := productVariableProps["Cflags"]; exists {
449 for _, prop := range props {
450 flags, ok := prop.Property.([]string)
451 if !ok {
452 ctx.ModuleErrorf("Could not convert product variable cflag property")
453 }
454 newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable)
455 copts.ProductValues = append(copts.ProductValues, bazel.ProductVariableValues{
456 ProductVariable: prop.ProductConfigVariable,
457 Values: newFlags,
458 })
459 }
460 }
461
Chris Parsons990c4f42021-05-25 12:10:58 -0400462 // Branch srcs into three language-specific groups.
463 // C++ is the "catch-all" group, and comprises generated sources because we don't
464 // know the language of these sources until the genrule is executed.
465 // TODO(b/): Handle language detection of sources in a Bazel rule.
466 isCSrc := func(s string) bool {
467 return strings.HasSuffix(s, ".c")
468 }
469 isAsmSrc := func(s string) bool {
470 return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s")
471 }
472 cSrcs := bazel.FilterLabelListAttribute(srcs, isCSrc)
473 asSrcs := bazel.FilterLabelListAttribute(srcs, isAsmSrc)
474 srcs = bazel.SubtractBazelLabelListAttribute(srcs, cSrcs)
475 srcs = bazel.SubtractBazelLabelListAttribute(srcs, asSrcs)
Jingwen Chen107c0de2021-04-09 10:43:12 +0000476 return compilerAttributes{
Chris Parsons990c4f42021-05-25 12:10:58 -0400477 copts: copts,
478 srcs: srcs,
479 asFlags: asFlags,
480 asSrcs: asSrcs,
481 cSrcs: cSrcs,
482 conlyFlags: conlyFlags,
483 cppFlags: cppFlags,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000484 }
485}
486
487// Convenience struct to hold all attributes parsed from linker properties.
488type linkerAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400489 deps bazel.LabelListAttribute
490 dynamicDeps bazel.LabelListAttribute
491 wholeArchiveDeps bazel.LabelListAttribute
Chris Parsonsd6358772021-05-18 18:35:24 -0400492 exportedDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400493 linkopts bazel.StringListAttribute
494 versionScript bazel.LabelAttribute
Jingwen Chenc1c26502021-04-05 10:35:13 +0000495}
496
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400497// FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here
498func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string {
499 flags := linkerProperties.Ldflags
500 if !BoolDefault(linkerProperties.Pack_relocations, true) {
501 flags = append(flags, "-Wl,--pack-dyn-relocs=none")
502 }
503 return flags
504}
505
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200506// bp2BuildParseLinkerProps parses the linker properties of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400507// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000508func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Jingwen Chen63930982021-03-24 10:04:33 -0400509 var deps bazel.LabelListAttribute
Chris Parsonsd6358772021-05-18 18:35:24 -0400510 var exportedDeps bazel.LabelListAttribute
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400511 var dynamicDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400512 var wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400513 var linkopts bazel.StringListAttribute
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200514 var versionScript bazel.LabelAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400515
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400516 getLibs := func(baseLinkerProps *BaseLinkerProperties) []string {
517 libs := baseLinkerProps.Header_libs
518 libs = append(libs, baseLinkerProps.Static_libs...)
519 libs = android.SortedUniqueStrings(libs)
520 return libs
521 }
522
Jingwen Chen91220d72021-03-24 02:18:33 -0400523 for _, linkerProps := range module.linker.linkerProps() {
524 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400525 libs := getLibs(baseLinkerProps)
Chris Parsonsd6358772021-05-18 18:35:24 -0400526 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400527 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chened9c17d2021-04-13 07:14:55 +0000528 deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs))
Chris Parsonsd6358772021-05-18 18:35:24 -0400529 exportedDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400530 linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps)
Chris Parsons08648312021-05-06 16:23:19 -0400531 wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200532
533 if baseLinkerProps.Version_script != nil {
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200534 versionScript.Value = android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200535 }
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400536
537 sharedLibs := baseLinkerProps.Shared_libs
538 dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, sharedLibs))
539
Jingwen Chen91220d72021-03-24 02:18:33 -0400540 break
541 }
542 }
543
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400544 for arch, props := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) {
545 if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
546 libs := getLibs(baseLinkerProps)
Chris Parsonsd6358772021-05-18 18:35:24 -0400547 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400548 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chen63930982021-03-24 10:04:33 -0400549 deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
Chris Parsonsd6358772021-05-18 18:35:24 -0400550 exportedDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400551 linkopts.SetValueForArch(arch.Name, getBp2BuildLinkerFlags(baseLinkerProps))
Chris Parsons08648312021-05-06 16:23:19 -0400552 wholeArchiveDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400553
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200554 if baseLinkerProps.Version_script != nil {
555 versionScript.SetValueForArch(arch.Name,
556 android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
557 }
Rupert Shuttleworth3b413d32021-05-10 18:41:51 -0400558
559 sharedLibs := baseLinkerProps.Shared_libs
560 dynamicDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
Jingwen Chen63930982021-03-24 10:04:33 -0400561 }
562 }
563
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400564 for os, targetProperties := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) {
565 if baseLinkerProps, ok := targetProperties.Properties.(*BaseLinkerProperties); ok {
566 libs := getLibs(baseLinkerProps)
Chris Parsonsd6358772021-05-18 18:35:24 -0400567 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400568 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400569 wholeArchiveDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
570 deps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
571 exportedDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400572
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400573 linkopts.SetOsValueForTarget(os.Name, getBp2BuildLinkerFlags(baseLinkerProps))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400574
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400575 if baseLinkerProps.Version_script != nil {
576 versionScript.SetOsValueForTarget(os.Name, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
577 }
578
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400579 sharedLibs := baseLinkerProps.Shared_libs
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400580 dynamicDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
581 }
582 for arch, archProperties := range targetProperties.ArchProperties {
583 if baseLinkerProps, ok := archProperties.(*BaseLinkerProperties); ok {
584 libs := getLibs(baseLinkerProps)
585 exportedLibs := baseLinkerProps.Export_header_lib_headers
586 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
587 wholeArchiveDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
588 deps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
589 exportedDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs))
590
591 linkopts.SetOsArchValueForTarget(os.Name, arch.Name, getBp2BuildLinkerFlags(baseLinkerProps))
592
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400593 if baseLinkerProps.Version_script != nil {
594 versionScript.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
595 }
596
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400597 sharedLibs := baseLinkerProps.Shared_libs
598 dynamicDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
599 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400600 }
601 }
602
Jingwen Chen107c0de2021-04-09 10:43:12 +0000603 return linkerAttributes{
Chris Parsons08648312021-05-06 16:23:19 -0400604 deps: deps,
Chris Parsonsd6358772021-05-18 18:35:24 -0400605 exportedDeps: exportedDeps,
Chris Parsons08648312021-05-06 16:23:19 -0400606 dynamicDeps: dynamicDeps,
607 wholeArchiveDeps: wholeArchiveDeps,
608 linkopts: linkopts,
609 versionScript: versionScript,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000610 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400611}
612
Jingwen Chened9c17d2021-04-13 07:14:55 +0000613// Relativize a list of root-relative paths with respect to the module's
614// directory.
615//
616// include_dirs Soong prop are root-relative (b/183742505), but
617// local_include_dirs, export_include_dirs and export_system_include_dirs are
618// module dir relative. This function makes a list of paths entirely module dir
619// relative.
620//
621// For the `include` attribute, Bazel wants the paths to be relative to the
622// module.
623func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000624 var relativePaths []string
625 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000626 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
627 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
628 if err != nil {
629 panic(err)
630 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000631 relativePaths = append(relativePaths, relativePath)
632 }
633 return relativePaths
634}
635
Jingwen Chened9c17d2021-04-13 07:14:55 +0000636// bp2BuildParseExportedIncludes creates a string list attribute contains the
Jingwen Chen882bcc12021-04-27 05:54:20 +0000637// exported included directories of a module.
638func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
Jingwen Chen91220d72021-03-24 02:18:33 -0400639 libraryDecorator := module.linker.(*libraryDecorator)
640
Jingwen Chened9c17d2021-04-13 07:14:55 +0000641 // Export_system_include_dirs and export_include_dirs are already module dir
642 // relative, so they don't need to be relativized like include_dirs, which
643 // are root-relative.
Jingwen Chen91220d72021-03-24 02:18:33 -0400644 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
645 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000646 includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
Jingwen Chen91220d72021-03-24 02:18:33 -0400647
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400648 getVariantIncludeDirs := func(includeDirs []string, flagExporterProperties *FlagExporterProperties) []string {
649 variantIncludeDirs := flagExporterProperties.Export_system_include_dirs
650 variantIncludeDirs = append(variantIncludeDirs, flagExporterProperties.Export_include_dirs...)
651
652 // To avoid duplicate includes when base includes + arch includes are combined
653 // TODO: This doesn't take conflicts between arch and os includes into account
654 variantIncludeDirs = bazel.SubtractStrings(variantIncludeDirs, includeDirs)
655 return variantIncludeDirs
656 }
657
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200658 for arch, props := range module.GetArchProperties(ctx, &FlagExporterProperties{}) {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000659 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400660 archIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000661 if len(archIncludeDirs) > 0 {
662 includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs)
663 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000664 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400665 }
666
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400667 for os, targetProperties := range module.GetTargetProperties(ctx, &FlagExporterProperties{}) {
668 if flagExporterProperties, ok := targetProperties.Properties.(*FlagExporterProperties); ok {
669 targetIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties)
670 if len(targetIncludeDirs) > 0 {
671 includeDirsAttribute.SetOsValueForTarget(os.Name, targetIncludeDirs)
672 }
673 }
674 for arch, archProperties := range targetProperties.ArchProperties {
675 if flagExporterProperties, ok := archProperties.(*FlagExporterProperties); ok {
676 targetIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties)
677 if len(targetIncludeDirs) > 0 {
678 includeDirsAttribute.SetOsArchValueForTarget(os.Name, arch.Name, targetIncludeDirs)
679 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400680 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400681 }
682 }
683
Jingwen Chen882bcc12021-04-27 05:54:20 +0000684 return includeDirsAttribute
Jingwen Chen91220d72021-03-24 02:18:33 -0400685}