blob: d52b817b24fdb997197db84a05b5dca70b6bdd80 [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 {
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200249 deps bazel.LabelListAttribute
250 linkopts bazel.StringListAttribute
251 versionScript bazel.LabelAttribute
Jingwen Chenc1c26502021-04-05 10:35:13 +0000252}
253
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200254// bp2BuildParseLinkerProps parses the linker properties of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400255// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000256func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Jingwen Chen63930982021-03-24 10:04:33 -0400257 var deps bazel.LabelListAttribute
258 var linkopts bazel.StringListAttribute
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200259 var versionScript bazel.LabelAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400260
Jingwen Chen91220d72021-03-24 02:18:33 -0400261 for _, linkerProps := range module.linker.linkerProps() {
262 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
263 libs := baseLinkerProps.Header_libs
264 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000265 libs = append(libs, baseLinkerProps.Static_libs...)
266 libs = append(libs, baseLinkerProps.Whole_static_libs...)
267 libs = android.SortedUniqueStrings(libs)
268 deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs))
Jingwen Chen63930982021-03-24 10:04:33 -0400269 linkopts.Value = baseLinkerProps.Ldflags
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200270
271 if baseLinkerProps.Version_script != nil {
272 versionScript = bazel.LabelAttribute{
273 Value: android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script),
274 }
275 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400276 break
277 }
278 }
279
Jingwen Chen63930982021-03-24 10:04:33 -0400280 for arch, p := range module.GetArchProperties(&BaseLinkerProperties{}) {
281 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
282 libs := baseLinkerProps.Header_libs
283 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000284 libs = append(libs, baseLinkerProps.Static_libs...)
285 libs = append(libs, baseLinkerProps.Whole_static_libs...)
Jingwen Chen63930982021-03-24 10:04:33 -0400286 libs = android.SortedUniqueStrings(libs)
287 deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
288 linkopts.SetValueForArch(arch.Name, baseLinkerProps.Ldflags)
289 }
290 }
291
Jingwen Chen91220d72021-03-24 02:18:33 -0400292 for os, p := range module.GetTargetProperties(&BaseLinkerProperties{}) {
293 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
294 libs := baseLinkerProps.Header_libs
295 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000296 libs = append(libs, baseLinkerProps.Static_libs...)
297 libs = append(libs, baseLinkerProps.Whole_static_libs...)
Jingwen Chen91220d72021-03-24 02:18:33 -0400298 libs = android.SortedUniqueStrings(libs)
Jingwen Chen63930982021-03-24 10:04:33 -0400299 deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
300 linkopts.SetValueForOS(os.Name, baseLinkerProps.Ldflags)
Jingwen Chen91220d72021-03-24 02:18:33 -0400301 }
302 }
303
Jingwen Chen107c0de2021-04-09 10:43:12 +0000304 return linkerAttributes{
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200305 deps: deps,
306 linkopts: linkopts,
307 versionScript: versionScript,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000308 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400309}
310
Jingwen Chened9c17d2021-04-13 07:14:55 +0000311// Relativize a list of root-relative paths with respect to the module's
312// directory.
313//
314// include_dirs Soong prop are root-relative (b/183742505), but
315// local_include_dirs, export_include_dirs and export_system_include_dirs are
316// module dir relative. This function makes a list of paths entirely module dir
317// relative.
318//
319// For the `include` attribute, Bazel wants the paths to be relative to the
320// module.
321func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000322 var relativePaths []string
323 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000324 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
325 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
326 if err != nil {
327 panic(err)
328 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000329 relativePaths = append(relativePaths, relativePath)
330 }
331 return relativePaths
332}
333
Jingwen Chened9c17d2021-04-13 07:14:55 +0000334// bp2BuildParseExportedIncludes creates a string list attribute contains the
Jingwen Chen882bcc12021-04-27 05:54:20 +0000335// exported included directories of a module.
336func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
Jingwen Chen91220d72021-03-24 02:18:33 -0400337 libraryDecorator := module.linker.(*libraryDecorator)
338
Jingwen Chened9c17d2021-04-13 07:14:55 +0000339 // Export_system_include_dirs and export_include_dirs are already module dir
340 // relative, so they don't need to be relativized like include_dirs, which
341 // are root-relative.
Jingwen Chen91220d72021-03-24 02:18:33 -0400342 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
343 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000344 includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
Jingwen Chen91220d72021-03-24 02:18:33 -0400345
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000346 for arch, props := range module.GetArchProperties(&FlagExporterProperties{}) {
347 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
348 archIncludeDirs := flagExporterProperties.Export_system_include_dirs
349 archIncludeDirs = append(archIncludeDirs, flagExporterProperties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000350
351 // To avoid duplicate includes when base includes + arch includes are combined
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400352 // FIXME: This doesn't take conflicts between arch and os includes into account
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000353 archIncludeDirs = bazel.SubtractStrings(archIncludeDirs, includeDirs)
354
355 if len(archIncludeDirs) > 0 {
356 includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs)
357 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000358 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400359 }
360
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400361 for os, props := range module.GetTargetProperties(&FlagExporterProperties{}) {
362 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
363 osIncludeDirs := flagExporterProperties.Export_system_include_dirs
364 osIncludeDirs = append(osIncludeDirs, flagExporterProperties.Export_include_dirs...)
365
366 // To avoid duplicate includes when base includes + os includes are combined
367 // FIXME: This doesn't take conflicts between arch and os includes into account
368 osIncludeDirs = bazel.SubtractStrings(osIncludeDirs, includeDirs)
369
370 if len(osIncludeDirs) > 0 {
371 includeDirsAttribute.SetValueForOS(os.Name, osIncludeDirs)
372 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400373 }
374 }
375
Jingwen Chen882bcc12021-04-27 05:54:20 +0000376 return includeDirsAttribute
Jingwen Chen91220d72021-03-24 02:18:33 -0400377}