blob: a4db79ff076acbfd4d40cbe67c3c6dee3c8e95b3 [file] [log] [blame]
Jingwen Chen91220d72021-03-24 02:18:33 -04001// Copyright 2021 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package cc
15
16import (
17 "android/soong/android"
18 "android/soong/bazel"
Jingwen Chened9c17d2021-04-13 07:14:55 +000019 "path/filepath"
Jingwen Chen91220d72021-03-24 02:18:33 -040020)
21
22// bp2build functions and helpers for converting cc_* modules to Bazel.
23
24func init() {
25 android.DepsBp2BuildMutators(RegisterDepsBp2Build)
26}
27
28func RegisterDepsBp2Build(ctx android.RegisterMutatorsContext) {
29 ctx.BottomUp("cc_bp2build_deps", depsBp2BuildMutator)
30}
31
32// A naive deps mutator to add deps on all modules across all combinations of
33// target props for cc modules. This is needed to make module -> bazel label
34// resolution work in the bp2build mutator later. This is probably
35// the wrong way to do it, but it works.
36//
37// TODO(jingwen): can we create a custom os mutator in depsBp2BuildMutator to do this?
38func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) {
39 module, ok := ctx.Module().(*Module)
40 if !ok {
41 // Not a cc module
42 return
43 }
44
45 if !module.ConvertWithBp2build(ctx) {
46 return
47 }
48
49 var allDeps []string
50
51 for _, p := range module.GetTargetProperties(&BaseLinkerProperties{}) {
52 // arch specific linker props
53 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
54 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
55 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +000056 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
57 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
58 }
59 }
60
61 for _, p := range module.GetArchProperties(&BaseLinkerProperties{}) {
62 // arch specific linker props
63 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
64 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
65 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
66 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
67 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
Jingwen Chen91220d72021-03-24 02:18:33 -040068 }
69 }
70
Jingwen Chen53681ef2021-04-29 08:15:13 +000071 // Deps in the static: { .. } and shared: { .. } props of a cc_library.
72 if lib, ok := module.compiler.(*libraryDecorator); ok {
73 allDeps = append(allDeps, lib.SharedProperties.Shared.Static_libs...)
74 allDeps = append(allDeps, lib.SharedProperties.Shared.Whole_static_libs...)
75 allDeps = append(allDeps, lib.SharedProperties.Shared.Shared_libs...)
76 allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...)
77
78 allDeps = append(allDeps, lib.StaticProperties.Static.Static_libs...)
79 allDeps = append(allDeps, lib.StaticProperties.Static.Whole_static_libs...)
80 allDeps = append(allDeps, lib.StaticProperties.Static.Shared_libs...)
81 allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...)
82 }
83
Jingwen Chen91220d72021-03-24 02:18:33 -040084 ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...)
85}
86
Jingwen Chen53681ef2021-04-29 08:15:13 +000087type sharedAttributes struct {
88 staticDeps bazel.LabelListAttribute
89}
90
91// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
92func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) sharedAttributes {
93 lib, ok := module.compiler.(*libraryDecorator)
94 if !ok {
95 return sharedAttributes{}
96 }
97
98 var staticDeps bazel.LabelListAttribute
99
100 staticDeps.Value = android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Whole_static_libs)
101
102 return sharedAttributes{
103 staticDeps: staticDeps,
104 }
105}
106
107type staticAttributes struct {
108 srcs bazel.LabelListAttribute
109}
110
111// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
112func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticAttributes {
113 lib, ok := module.compiler.(*libraryDecorator)
114 if !ok {
115 return staticAttributes{}
116 }
117
118 var srcs bazel.LabelListAttribute
119 srcs.Value = android.BazelLabelForModuleSrc(ctx, lib.StaticProperties.Static.Srcs)
120
121 return staticAttributes{
122 srcs: srcs,
123 }
124}
125
Jingwen Chen107c0de2021-04-09 10:43:12 +0000126// Convenience struct to hold all attributes parsed from compiler properties.
127type compilerAttributes struct {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000128 copts bazel.StringListAttribute
129 srcs bazel.LabelListAttribute
130 includes bazel.StringListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000131}
132
Jingwen Chen63930982021-03-24 10:04:33 -0400133// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000134func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
Jingwen Chen882bcc12021-04-27 05:54:20 +0000135 var srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000136 var copts bazel.StringListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400137
Jingwen Chened9c17d2021-04-13 07:14:55 +0000138 // Creates the -I flag for a directory, while making the directory relative
139 // to the exec root for Bazel to work.
140 includeFlag := func(dir string) string {
141 // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements.
142 return "-I" + filepath.Join(ctx.ModuleDir(), dir)
143 }
144
Jingwen Chened9c17d2021-04-13 07:14:55 +0000145 // Parse the list of module-relative include directories (-I).
146 parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string {
147 // include_dirs are root-relative, not module-relative.
148 includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
149 return append(includeDirs, baseCompilerProps.Local_include_dirs...)
150 }
151
152 // Parse the list of copts.
153 parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string {
154 copts := append([]string{}, baseCompilerProps.Cflags...)
155 for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
156 copts = append(copts, includeFlag(dir))
157 }
158 return copts
Jingwen Chen63930982021-03-24 10:04:33 -0400159 }
160
Jingwen Chene32e9e02021-04-23 09:17:24 +0000161 // baseSrcs contain the list of src files that are used for every configuration.
162 var baseSrcs []string
163 // baseExcludeSrcs contain the list of src files that are excluded for every configuration.
164 var baseExcludeSrcs []string
165 // baseSrcsLabelList is a clone of the base srcs LabelList, used for computing the
166 // arch or os specific srcs later.
167 var baseSrcsLabelList bazel.LabelList
168
169 // Parse srcs from an arch or OS's props value, taking the base srcs and
170 // exclude srcs into account.
171 parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList {
172 // Combine the base srcs and arch-specific srcs
173 allSrcs := append(baseSrcs, baseCompilerProps.Srcs...)
174 // Combine the base exclude_srcs and configuration-specific exclude_srcs
175 allExcludeSrcs := append(baseExcludeSrcs, baseCompilerProps.Exclude_srcs...)
176 return android.BazelLabelForModuleSrcExcludes(ctx, allSrcs, allExcludeSrcs)
177 }
178
Jingwen Chenc1c26502021-04-05 10:35:13 +0000179 for _, props := range module.compiler.compilerProps() {
180 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000181 srcs.Value = parseSrcs(baseCompilerProps)
182 copts.Value = parseCopts(baseCompilerProps)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000183
184 // Used for arch-specific srcs later.
185 baseSrcs = baseCompilerProps.Srcs
186 baseExcludeSrcs = baseCompilerProps.Exclude_srcs
187 baseSrcsLabelList = parseSrcs(baseCompilerProps)
Jingwen Chenc1c26502021-04-05 10:35:13 +0000188 break
189 }
190 }
191
Jingwen Chene32e9e02021-04-23 09:17:24 +0000192 // Handle include_build_directory prop. If the property is true, then the
193 // target has access to all headers recursively in the package, and has
194 // "-I<module-dir>" in its copts.
Jingwen Chened9c17d2021-04-13 07:14:55 +0000195 if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
196 copts.Value = append(copts.Value, includeFlag("."))
Jingwen Chened9c17d2021-04-13 07:14:55 +0000197 } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() {
198 copts.Value = append(copts.Value, includeFlag("."))
Jingwen Chened9c17d2021-04-13 07:14:55 +0000199 }
200
Jingwen Chenc1c26502021-04-05 10:35:13 +0000201 for arch, props := range module.GetArchProperties(&BaseCompilerProperties{}) {
202 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000203 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
204 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
205 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
206 srcsList := parseSrcs(baseCompilerProps)
207 srcs.SetValueForArch(arch.Name, srcsList)
208 // The base srcs value should not contain any arch-specific excludes.
209 srcs.Value = bazel.SubtractBazelLabelList(srcs.Value, bazel.LabelList{Includes: srcsList.Excludes})
210 }
211
Jingwen Chened9c17d2021-04-13 07:14:55 +0000212 copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000213 }
214 }
215
Jingwen Chene32e9e02021-04-23 09:17:24 +0000216 // After going through all archs, delete the duplicate files in the arch
217 // values that are already in the base srcs.Value.
218 for arch, props := range module.GetArchProperties(&BaseCompilerProperties{}) {
219 if _, ok := props.(*BaseCompilerProperties); ok {
220 srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcs.GetValueForArch(arch.Name), srcs.Value))
221 }
222 }
223
224 // Now that the srcs.Value list is finalized, compare it with the original
225 // list, and put the difference into the default condition for the arch
226 // select.
227 defaultsSrcs := bazel.SubtractBazelLabelList(baseSrcsLabelList, srcs.Value)
228 // TODO(b/186153868): handle the case with multiple variant types, e.g. when arch and os are both used.
229 srcs.SetValueForArch(bazel.CONDITIONS_DEFAULT, defaultsSrcs)
230
231 // Handle OS specific props.
Jingwen Chenc1c26502021-04-05 10:35:13 +0000232 for os, props := range module.GetTargetProperties(&BaseCompilerProperties{}) {
233 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000234 srcsList := parseSrcs(baseCompilerProps)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000235 // TODO(b/186153868): add support for os-specific srcs and exclude_srcs
236 srcs.SetValueForOS(os.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList))
Jingwen Chened9c17d2021-04-13 07:14:55 +0000237 copts.SetValueForOS(os.Name, parseCopts(baseCompilerProps))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000238 }
239 }
240
Jingwen Chen107c0de2021-04-09 10:43:12 +0000241 return compilerAttributes{
Jingwen Chen107c0de2021-04-09 10:43:12 +0000242 srcs: srcs,
243 copts: copts,
244 }
245}
246
247// Convenience struct to hold all attributes parsed from linker properties.
248type linkerAttributes struct {
249 deps bazel.LabelListAttribute
250 linkopts bazel.StringListAttribute
Jingwen Chenc1c26502021-04-05 10:35:13 +0000251}
252
Jingwen Chen63930982021-03-24 10:04:33 -0400253// bp2BuildParseLinkerProps creates a label list attribute containing the header library deps of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400254// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000255func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Jingwen Chen63930982021-03-24 10:04:33 -0400256 var deps bazel.LabelListAttribute
257 var linkopts bazel.StringListAttribute
258
Jingwen Chen91220d72021-03-24 02:18:33 -0400259 for _, linkerProps := range module.linker.linkerProps() {
260 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
261 libs := baseLinkerProps.Header_libs
262 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000263 libs = append(libs, baseLinkerProps.Static_libs...)
264 libs = append(libs, baseLinkerProps.Whole_static_libs...)
265 libs = android.SortedUniqueStrings(libs)
266 deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs))
Jingwen Chen63930982021-03-24 10:04:33 -0400267 linkopts.Value = baseLinkerProps.Ldflags
Jingwen Chen91220d72021-03-24 02:18:33 -0400268 break
269 }
270 }
271
Jingwen Chen63930982021-03-24 10:04:33 -0400272 for arch, p := range module.GetArchProperties(&BaseLinkerProperties{}) {
273 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
274 libs := baseLinkerProps.Header_libs
275 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000276 libs = append(libs, baseLinkerProps.Static_libs...)
277 libs = append(libs, baseLinkerProps.Whole_static_libs...)
Jingwen Chen63930982021-03-24 10:04:33 -0400278 libs = android.SortedUniqueStrings(libs)
279 deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
280 linkopts.SetValueForArch(arch.Name, baseLinkerProps.Ldflags)
281 }
282 }
283
Jingwen Chen91220d72021-03-24 02:18:33 -0400284 for os, p := range module.GetTargetProperties(&BaseLinkerProperties{}) {
285 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
286 libs := baseLinkerProps.Header_libs
287 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000288 libs = append(libs, baseLinkerProps.Static_libs...)
289 libs = append(libs, baseLinkerProps.Whole_static_libs...)
Jingwen Chen91220d72021-03-24 02:18:33 -0400290 libs = android.SortedUniqueStrings(libs)
Jingwen Chen63930982021-03-24 10:04:33 -0400291 deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
292 linkopts.SetValueForOS(os.Name, baseLinkerProps.Ldflags)
Jingwen Chen91220d72021-03-24 02:18:33 -0400293 }
294 }
295
Jingwen Chen107c0de2021-04-09 10:43:12 +0000296 return linkerAttributes{
297 deps: deps,
298 linkopts: linkopts,
299 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400300}
301
Jingwen Chened9c17d2021-04-13 07:14:55 +0000302// Relativize a list of root-relative paths with respect to the module's
303// directory.
304//
305// include_dirs Soong prop are root-relative (b/183742505), but
306// local_include_dirs, export_include_dirs and export_system_include_dirs are
307// module dir relative. This function makes a list of paths entirely module dir
308// relative.
309//
310// For the `include` attribute, Bazel wants the paths to be relative to the
311// module.
312func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000313 var relativePaths []string
314 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000315 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
316 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
317 if err != nil {
318 panic(err)
319 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000320 relativePaths = append(relativePaths, relativePath)
321 }
322 return relativePaths
323}
324
Jingwen Chened9c17d2021-04-13 07:14:55 +0000325// bp2BuildParseExportedIncludes creates a string list attribute contains the
Jingwen Chen882bcc12021-04-27 05:54:20 +0000326// exported included directories of a module.
327func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
Jingwen Chen91220d72021-03-24 02:18:33 -0400328 libraryDecorator := module.linker.(*libraryDecorator)
329
Jingwen Chened9c17d2021-04-13 07:14:55 +0000330 // Export_system_include_dirs and export_include_dirs are already module dir
331 // relative, so they don't need to be relativized like include_dirs, which
332 // are root-relative.
Jingwen Chen91220d72021-03-24 02:18:33 -0400333 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
334 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000335 includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
Jingwen Chen91220d72021-03-24 02:18:33 -0400336
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000337 for arch, props := range module.GetArchProperties(&FlagExporterProperties{}) {
338 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
339 archIncludeDirs := flagExporterProperties.Export_system_include_dirs
340 archIncludeDirs = append(archIncludeDirs, flagExporterProperties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000341
342 // To avoid duplicate includes when base includes + arch includes are combined
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400343 // FIXME: This doesn't take conflicts between arch and os includes into account
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000344 archIncludeDirs = bazel.SubtractStrings(archIncludeDirs, includeDirs)
345
346 if len(archIncludeDirs) > 0 {
347 includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs)
348 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000349 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400350 }
351
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400352 for os, props := range module.GetTargetProperties(&FlagExporterProperties{}) {
353 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
354 osIncludeDirs := flagExporterProperties.Export_system_include_dirs
355 osIncludeDirs = append(osIncludeDirs, flagExporterProperties.Export_include_dirs...)
356
357 // To avoid duplicate includes when base includes + os includes are combined
358 // FIXME: This doesn't take conflicts between arch and os includes into account
359 osIncludeDirs = bazel.SubtractStrings(osIncludeDirs, includeDirs)
360
361 if len(osIncludeDirs) > 0 {
362 includeDirsAttribute.SetValueForOS(os.Name, osIncludeDirs)
363 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400364 }
365 }
366
Jingwen Chen882bcc12021-04-27 05:54:20 +0000367 return includeDirsAttribute
Jingwen Chen91220d72021-03-24 02:18:33 -0400368}