blob: e64573c3f50a7f31afed7e6e47af5d7e43172ec7 [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 {
107 allDeps = append(allDeps, lib.SharedProperties.Shared.Static_libs...)
108 allDeps = append(allDeps, lib.SharedProperties.Shared.Whole_static_libs...)
109 allDeps = append(allDeps, lib.SharedProperties.Shared.Shared_libs...)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000110
111 allDeps = append(allDeps, lib.StaticProperties.Static.Static_libs...)
112 allDeps = append(allDeps, lib.StaticProperties.Static.Whole_static_libs...)
113 allDeps = append(allDeps, lib.StaticProperties.Static.Shared_libs...)
Jingwen Chen45dec102021-05-19 10:30:29 +0000114
115 // TODO(b/186024507, b/186489250): Temporarily exclude adding
116 // system_shared_libs deps until libc and libm builds.
117 // allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...)
118 // allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000119 }
120
Jingwen Chen91220d72021-03-24 02:18:33 -0400121 ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...)
122}
123
Liz Kammer2222c6b2021-05-24 15:41:47 -0400124// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
125// properities which apply to either the shared or static version of a cc_library module.
126type staticOrSharedAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400127 copts bazel.StringListAttribute
128 srcs bazel.LabelListAttribute
129 staticDeps bazel.LabelListAttribute
130 dynamicDeps bazel.LabelListAttribute
131 wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +0000132}
133
134// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400135func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000136 lib, ok := module.compiler.(*libraryDecorator)
137 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400138 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000139 }
140
Liz Kammer2222c6b2021-05-24 15:41:47 -0400141 return bp2buildParseStaticOrSharedProps(ctx, lib.SharedProperties.Shared)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000142}
143
144// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400145func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000146 lib, ok := module.compiler.(*libraryDecorator)
147 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400148 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000149 }
150
Liz Kammer2222c6b2021-05-24 15:41:47 -0400151 return bp2buildParseStaticOrSharedProps(ctx, lib.StaticProperties.Static)
152}
153
154func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, props StaticOrSharedProperties) staticOrSharedAttributes {
155 copts := bazel.StringListAttribute{Value: props.Cflags}
Chris Parsons08648312021-05-06 16:23:19 -0400156
157 srcs := bazel.LabelListAttribute{
Liz Kammer2222c6b2021-05-24 15:41:47 -0400158 Value: android.BazelLabelForModuleSrc(ctx, props.Srcs)}
Chris Parsons08648312021-05-06 16:23:19 -0400159
160 staticDeps := bazel.LabelListAttribute{
Liz Kammer2222c6b2021-05-24 15:41:47 -0400161 Value: android.BazelLabelForModuleDeps(ctx, props.Static_libs)}
Chris Parsons08648312021-05-06 16:23:19 -0400162
163 dynamicDeps := bazel.LabelListAttribute{
Liz Kammer2222c6b2021-05-24 15:41:47 -0400164 Value: android.BazelLabelForModuleDeps(ctx, props.Shared_libs)}
Chris Parsons08648312021-05-06 16:23:19 -0400165
166 wholeArchiveDeps := bazel.LabelListAttribute{
Liz Kammer2222c6b2021-05-24 15:41:47 -0400167 Value: android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000168
Liz Kammer2222c6b2021-05-24 15:41:47 -0400169 return staticOrSharedAttributes{
Chris Parsons08648312021-05-06 16:23:19 -0400170 copts: copts,
171 srcs: srcs,
172 staticDeps: staticDeps,
173 dynamicDeps: dynamicDeps,
174 wholeArchiveDeps: wholeArchiveDeps,
Jingwen Chen53681ef2021-04-29 08:15:13 +0000175 }
176}
177
Jingwen Chen107c0de2021-04-09 10:43:12 +0000178// Convenience struct to hold all attributes parsed from compiler properties.
179type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400180 // Options for all languages
181 copts bazel.StringListAttribute
182 // Assembly options and sources
183 asFlags bazel.StringListAttribute
184 asSrcs bazel.LabelListAttribute
185 // C options and sources
186 conlyFlags bazel.StringListAttribute
187 cSrcs bazel.LabelListAttribute
188 // C++ options and sources
189 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000190 srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000191}
192
Jingwen Chen63930982021-03-24 10:04:33 -0400193// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000194func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
Jingwen Chen882bcc12021-04-27 05:54:20 +0000195 var srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000196 var copts bazel.StringListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400197 var asFlags bazel.StringListAttribute
198 var conlyFlags bazel.StringListAttribute
199 var cppFlags bazel.StringListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400200
Chris Parsons484e50a2021-05-13 15:13:04 -0400201 // Creates the -I flags for a directory, while making the directory relative
Jingwen Chened9c17d2021-04-13 07:14:55 +0000202 // to the exec root for Bazel to work.
Chris Parsons484e50a2021-05-13 15:13:04 -0400203 includeFlags := func(dir string) []string {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000204 // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements.
Chris Parsons484e50a2021-05-13 15:13:04 -0400205 moduleDirRootedPath := filepath.Join(ctx.ModuleDir(), dir)
206 return []string{
207 "-I" + moduleDirRootedPath,
208 // Include the bindir-rooted path (using make variable substitution). This most
209 // closely matches Bazel's native include path handling, which allows for dependency
210 // on generated headers in these directories.
211 // TODO(b/188084383): Handle local include directories in Bazel.
212 "-I$(BINDIR)/" + moduleDirRootedPath,
213 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000214 }
215
Jingwen Chened9c17d2021-04-13 07:14:55 +0000216 // Parse the list of module-relative include directories (-I).
217 parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string {
218 // include_dirs are root-relative, not module-relative.
219 includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
220 return append(includeDirs, baseCompilerProps.Local_include_dirs...)
221 }
222
Chris Parsons990c4f42021-05-25 12:10:58 -0400223 parseCommandLineFlags := func(soongFlags []string) []string {
224 var result []string
225 for _, flag := range soongFlags {
Colin Cross52aa4e12021-05-25 15:20:39 +0000226 // Soong's cflags can contain spaces, like `-include header.h`. For
227 // Bazel's copts, split them up to be compatible with the
228 // no_copts_tokenization feature.
Chris Parsons990c4f42021-05-25 12:10:58 -0400229 result = append(result, strings.Split(flag, " ")...)
Colin Cross52aa4e12021-05-25 15:20:39 +0000230 }
Chris Parsons990c4f42021-05-25 12:10:58 -0400231 return result
232 }
233
234 // Parse the list of copts.
235 parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string {
236 var copts []string
237 copts = append(copts, parseCommandLineFlags(baseCompilerProps.Cflags)...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000238 for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
Chris Parsons484e50a2021-05-13 15:13:04 -0400239 copts = append(copts, includeFlags(dir)...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000240 }
241 return copts
Jingwen Chen63930982021-03-24 10:04:33 -0400242 }
243
Jingwen Chene32e9e02021-04-23 09:17:24 +0000244 // baseSrcs contain the list of src files that are used for every configuration.
245 var baseSrcs []string
246 // baseExcludeSrcs contain the list of src files that are excluded for every configuration.
247 var baseExcludeSrcs []string
248 // baseSrcsLabelList is a clone of the base srcs LabelList, used for computing the
249 // arch or os specific srcs later.
250 var baseSrcsLabelList bazel.LabelList
251
252 // Parse srcs from an arch or OS's props value, taking the base srcs and
253 // exclude srcs into account.
254 parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList {
255 // Combine the base srcs and arch-specific srcs
256 allSrcs := append(baseSrcs, baseCompilerProps.Srcs...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400257 // Add srcs-like dependencies such as generated files.
258 // First create a LabelList containing these dependencies, then merge the values with srcs.
259 generatedHdrsAndSrcs := baseCompilerProps.Generated_headers
260 generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...)
261
262 generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs)
263
Jingwen Chene32e9e02021-04-23 09:17:24 +0000264 // Combine the base exclude_srcs and configuration-specific exclude_srcs
265 allExcludeSrcs := append(baseExcludeSrcs, baseCompilerProps.Exclude_srcs...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400266 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, allSrcs, allExcludeSrcs)
267 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000268 }
269
Jingwen Chenc1c26502021-04-05 10:35:13 +0000270 for _, props := range module.compiler.compilerProps() {
271 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000272 srcs.Value = parseSrcs(baseCompilerProps)
273 copts.Value = parseCopts(baseCompilerProps)
Chris Parsons990c4f42021-05-25 12:10:58 -0400274 asFlags.Value = parseCommandLineFlags(baseCompilerProps.Asflags)
275 conlyFlags.Value = parseCommandLineFlags(baseCompilerProps.Conlyflags)
276 cppFlags.Value = parseCommandLineFlags(baseCompilerProps.Cppflags)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000277
278 // Used for arch-specific srcs later.
279 baseSrcs = baseCompilerProps.Srcs
Jingwen Chene32e9e02021-04-23 09:17:24 +0000280 baseSrcsLabelList = parseSrcs(baseCompilerProps)
Chris Parsons484e50a2021-05-13 15:13:04 -0400281 baseExcludeSrcs = baseCompilerProps.Exclude_srcs
Jingwen Chenc1c26502021-04-05 10:35:13 +0000282 break
283 }
284 }
285
Jingwen Chene32e9e02021-04-23 09:17:24 +0000286 // Handle include_build_directory prop. If the property is true, then the
287 // target has access to all headers recursively in the package, and has
288 // "-I<module-dir>" in its copts.
Jingwen Chened9c17d2021-04-13 07:14:55 +0000289 if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400290 copts.Value = append(copts.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000291 } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400292 copts.Value = append(copts.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000293 }
294
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200295 for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000296 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000297 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
298 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
299 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
300 srcsList := parseSrcs(baseCompilerProps)
301 srcs.SetValueForArch(arch.Name, srcsList)
302 // The base srcs value should not contain any arch-specific excludes.
303 srcs.Value = bazel.SubtractBazelLabelList(srcs.Value, bazel.LabelList{Includes: srcsList.Excludes})
304 }
305
Jingwen Chened9c17d2021-04-13 07:14:55 +0000306 copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps))
Chris Parsons990c4f42021-05-25 12:10:58 -0400307 asFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Asflags))
308 conlyFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags))
309 cppFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Cppflags))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000310 }
311 }
312
Jingwen Chene32e9e02021-04-23 09:17:24 +0000313 // After going through all archs, delete the duplicate files in the arch
314 // values that are already in the base srcs.Value.
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200315 for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000316 if _, ok := props.(*BaseCompilerProperties); ok {
317 srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcs.GetValueForArch(arch.Name), srcs.Value))
318 }
319 }
320
321 // Now that the srcs.Value list is finalized, compare it with the original
322 // list, and put the difference into the default condition for the arch
323 // select.
324 defaultsSrcs := bazel.SubtractBazelLabelList(baseSrcsLabelList, srcs.Value)
325 // TODO(b/186153868): handle the case with multiple variant types, e.g. when arch and os are both used.
326 srcs.SetValueForArch(bazel.CONDITIONS_DEFAULT, defaultsSrcs)
327
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400328 // Handle target specific properties.
329 for os, osProps := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) {
330 if baseCompilerProps, ok := osProps.Properties.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000331 srcsList := parseSrcs(baseCompilerProps)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000332 // TODO(b/186153868): add support for os-specific srcs and exclude_srcs
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400333 srcs.SetOsValueForTarget(os.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList))
334 copts.SetOsValueForTarget(os.Name, parseCopts(baseCompilerProps))
335 asFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Asflags))
336 conlyFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags))
337 cppFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Cppflags))
338 }
339 for arch, archProps := range osProps.ArchProperties {
340 if baseCompilerProps, ok := archProps.(*BaseCompilerProperties); ok {
341 srcsList := parseSrcs(baseCompilerProps)
342 // TODO(b/186153868): add support for os-specific srcs and exclude_srcs
343 srcs.SetOsArchValueForTarget(os.Name, arch.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList))
344 copts.SetOsArchValueForTarget(os.Name, arch.Name, parseCopts(baseCompilerProps))
345 asFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Asflags))
346 conlyFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags))
347 cppFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Cppflags))
348 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000349 }
350 }
351
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400352 productVariableProps := android.ProductVariableProperties(ctx)
353 if props, exists := productVariableProps["Cflags"]; exists {
354 for _, prop := range props {
355 flags, ok := prop.Property.([]string)
356 if !ok {
357 ctx.ModuleErrorf("Could not convert product variable cflag property")
358 }
359 newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable)
360 copts.ProductValues = append(copts.ProductValues, bazel.ProductVariableValues{
361 ProductVariable: prop.ProductConfigVariable,
362 Values: newFlags,
363 })
364 }
365 }
366
Chris Parsons990c4f42021-05-25 12:10:58 -0400367 // Branch srcs into three language-specific groups.
368 // C++ is the "catch-all" group, and comprises generated sources because we don't
369 // know the language of these sources until the genrule is executed.
370 // TODO(b/): Handle language detection of sources in a Bazel rule.
371 isCSrc := func(s string) bool {
372 return strings.HasSuffix(s, ".c")
373 }
374 isAsmSrc := func(s string) bool {
375 return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s")
376 }
377 cSrcs := bazel.FilterLabelListAttribute(srcs, isCSrc)
378 asSrcs := bazel.FilterLabelListAttribute(srcs, isAsmSrc)
379 srcs = bazel.SubtractBazelLabelListAttribute(srcs, cSrcs)
380 srcs = bazel.SubtractBazelLabelListAttribute(srcs, asSrcs)
Jingwen Chen107c0de2021-04-09 10:43:12 +0000381 return compilerAttributes{
Chris Parsons990c4f42021-05-25 12:10:58 -0400382 copts: copts,
383 srcs: srcs,
384 asFlags: asFlags,
385 asSrcs: asSrcs,
386 cSrcs: cSrcs,
387 conlyFlags: conlyFlags,
388 cppFlags: cppFlags,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000389 }
390}
391
392// Convenience struct to hold all attributes parsed from linker properties.
393type linkerAttributes struct {
Chris Parsons08648312021-05-06 16:23:19 -0400394 deps bazel.LabelListAttribute
395 dynamicDeps bazel.LabelListAttribute
396 wholeArchiveDeps bazel.LabelListAttribute
Chris Parsonsd6358772021-05-18 18:35:24 -0400397 exportedDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400398 linkopts bazel.StringListAttribute
399 versionScript bazel.LabelAttribute
Jingwen Chenc1c26502021-04-05 10:35:13 +0000400}
401
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400402// FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here
403func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string {
404 flags := linkerProperties.Ldflags
405 if !BoolDefault(linkerProperties.Pack_relocations, true) {
406 flags = append(flags, "-Wl,--pack-dyn-relocs=none")
407 }
408 return flags
409}
410
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200411// bp2BuildParseLinkerProps parses the linker properties of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400412// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000413func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Jingwen Chen63930982021-03-24 10:04:33 -0400414 var deps bazel.LabelListAttribute
Chris Parsonsd6358772021-05-18 18:35:24 -0400415 var exportedDeps bazel.LabelListAttribute
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400416 var dynamicDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400417 var wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400418 var linkopts bazel.StringListAttribute
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200419 var versionScript bazel.LabelAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400420
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400421 getLibs := func(baseLinkerProps *BaseLinkerProperties) []string {
422 libs := baseLinkerProps.Header_libs
423 libs = append(libs, baseLinkerProps.Static_libs...)
424 libs = android.SortedUniqueStrings(libs)
425 return libs
426 }
427
Jingwen Chen91220d72021-03-24 02:18:33 -0400428 for _, linkerProps := range module.linker.linkerProps() {
429 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400430 libs := getLibs(baseLinkerProps)
Chris Parsonsd6358772021-05-18 18:35:24 -0400431 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400432 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chened9c17d2021-04-13 07:14:55 +0000433 deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs))
Chris Parsonsd6358772021-05-18 18:35:24 -0400434 exportedDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400435 linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps)
Chris Parsons08648312021-05-06 16:23:19 -0400436 wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200437
438 if baseLinkerProps.Version_script != nil {
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200439 versionScript.Value = android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200440 }
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400441
442 sharedLibs := baseLinkerProps.Shared_libs
443 dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, sharedLibs))
444
Jingwen Chen91220d72021-03-24 02:18:33 -0400445 break
446 }
447 }
448
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400449 for arch, props := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) {
450 if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
451 libs := getLibs(baseLinkerProps)
Chris Parsonsd6358772021-05-18 18:35:24 -0400452 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400453 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Jingwen Chen63930982021-03-24 10:04:33 -0400454 deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
Chris Parsonsd6358772021-05-18 18:35:24 -0400455 exportedDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400456 linkopts.SetValueForArch(arch.Name, getBp2BuildLinkerFlags(baseLinkerProps))
Chris Parsons08648312021-05-06 16:23:19 -0400457 wholeArchiveDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400458
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200459 if baseLinkerProps.Version_script != nil {
460 versionScript.SetValueForArch(arch.Name,
461 android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
462 }
Rupert Shuttleworth3b413d32021-05-10 18:41:51 -0400463
464 sharedLibs := baseLinkerProps.Shared_libs
465 dynamicDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
Jingwen Chen63930982021-03-24 10:04:33 -0400466 }
467 }
468
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400469 for os, targetProperties := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) {
470 if baseLinkerProps, ok := targetProperties.Properties.(*BaseLinkerProperties); ok {
471 libs := getLibs(baseLinkerProps)
Chris Parsonsd6358772021-05-18 18:35:24 -0400472 exportedLibs := baseLinkerProps.Export_header_lib_headers
Chris Parsons08648312021-05-06 16:23:19 -0400473 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400474 wholeArchiveDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
475 deps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
476 exportedDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400477
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400478 linkopts.SetOsValueForTarget(os.Name, getBp2BuildLinkerFlags(baseLinkerProps))
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400479
480 sharedLibs := baseLinkerProps.Shared_libs
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400481 dynamicDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
482 }
483 for arch, archProperties := range targetProperties.ArchProperties {
484 if baseLinkerProps, ok := archProperties.(*BaseLinkerProperties); ok {
485 libs := getLibs(baseLinkerProps)
486 exportedLibs := baseLinkerProps.Export_header_lib_headers
487 wholeArchiveLibs := baseLinkerProps.Whole_static_libs
488 wholeArchiveDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs))
489 deps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
490 exportedDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs))
491
492 linkopts.SetOsArchValueForTarget(os.Name, arch.Name, getBp2BuildLinkerFlags(baseLinkerProps))
493
494 sharedLibs := baseLinkerProps.Shared_libs
495 dynamicDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs))
496 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400497 }
498 }
499
Jingwen Chen107c0de2021-04-09 10:43:12 +0000500 return linkerAttributes{
Chris Parsons08648312021-05-06 16:23:19 -0400501 deps: deps,
Chris Parsonsd6358772021-05-18 18:35:24 -0400502 exportedDeps: exportedDeps,
Chris Parsons08648312021-05-06 16:23:19 -0400503 dynamicDeps: dynamicDeps,
504 wholeArchiveDeps: wholeArchiveDeps,
505 linkopts: linkopts,
506 versionScript: versionScript,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000507 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400508}
509
Jingwen Chened9c17d2021-04-13 07:14:55 +0000510// Relativize a list of root-relative paths with respect to the module's
511// directory.
512//
513// include_dirs Soong prop are root-relative (b/183742505), but
514// local_include_dirs, export_include_dirs and export_system_include_dirs are
515// module dir relative. This function makes a list of paths entirely module dir
516// relative.
517//
518// For the `include` attribute, Bazel wants the paths to be relative to the
519// module.
520func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000521 var relativePaths []string
522 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000523 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
524 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
525 if err != nil {
526 panic(err)
527 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000528 relativePaths = append(relativePaths, relativePath)
529 }
530 return relativePaths
531}
532
Jingwen Chened9c17d2021-04-13 07:14:55 +0000533// bp2BuildParseExportedIncludes creates a string list attribute contains the
Jingwen Chen882bcc12021-04-27 05:54:20 +0000534// exported included directories of a module.
535func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
Jingwen Chen91220d72021-03-24 02:18:33 -0400536 libraryDecorator := module.linker.(*libraryDecorator)
537
Jingwen Chened9c17d2021-04-13 07:14:55 +0000538 // Export_system_include_dirs and export_include_dirs are already module dir
539 // relative, so they don't need to be relativized like include_dirs, which
540 // are root-relative.
Jingwen Chen91220d72021-03-24 02:18:33 -0400541 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
542 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000543 includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
Jingwen Chen91220d72021-03-24 02:18:33 -0400544
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400545 getVariantIncludeDirs := func(includeDirs []string, flagExporterProperties *FlagExporterProperties) []string {
546 variantIncludeDirs := flagExporterProperties.Export_system_include_dirs
547 variantIncludeDirs = append(variantIncludeDirs, flagExporterProperties.Export_include_dirs...)
548
549 // To avoid duplicate includes when base includes + arch includes are combined
550 // TODO: This doesn't take conflicts between arch and os includes into account
551 variantIncludeDirs = bazel.SubtractStrings(variantIncludeDirs, includeDirs)
552 return variantIncludeDirs
553 }
554
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200555 for arch, props := range module.GetArchProperties(ctx, &FlagExporterProperties{}) {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000556 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400557 archIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000558 if len(archIncludeDirs) > 0 {
559 includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs)
560 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000561 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400562 }
563
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400564 for os, targetProperties := range module.GetTargetProperties(ctx, &FlagExporterProperties{}) {
565 if flagExporterProperties, ok := targetProperties.Properties.(*FlagExporterProperties); ok {
566 targetIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties)
567 if len(targetIncludeDirs) > 0 {
568 includeDirsAttribute.SetOsValueForTarget(os.Name, targetIncludeDirs)
569 }
570 }
571 for arch, archProperties := range targetProperties.ArchProperties {
572 if flagExporterProperties, ok := archProperties.(*FlagExporterProperties); ok {
573 targetIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties)
574 if len(targetIncludeDirs) > 0 {
575 includeDirsAttribute.SetOsArchValueForTarget(os.Name, arch.Name, targetIncludeDirs)
576 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400577 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400578 }
579 }
580
Jingwen Chen882bcc12021-04-27 05:54:20 +0000581 return includeDirsAttribute
Jingwen Chen91220d72021-03-24 02:18:33 -0400582}