blob: 9c4bf6e5426c50e348bf85a20c78eb068f1c84f9 [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
71 ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...)
72}
73
Jingwen Chen107c0de2021-04-09 10:43:12 +000074// Convenience struct to hold all attributes parsed from compiler properties.
75type compilerAttributes struct {
Jingwen Chened9c17d2021-04-13 07:14:55 +000076 copts bazel.StringListAttribute
77 srcs bazel.LabelListAttribute
78 includes bazel.StringListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +000079}
80
Jingwen Chen63930982021-03-24 10:04:33 -040081// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen107c0de2021-04-09 10:43:12 +000082func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
Jingwen Chen882bcc12021-04-27 05:54:20 +000083 var srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +000084 var copts bazel.StringListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -040085
Jingwen Chened9c17d2021-04-13 07:14:55 +000086 // Creates the -I flag for a directory, while making the directory relative
87 // to the exec root for Bazel to work.
88 includeFlag := func(dir string) string {
89 // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements.
90 return "-I" + filepath.Join(ctx.ModuleDir(), dir)
91 }
92
Jingwen Chened9c17d2021-04-13 07:14:55 +000093 // Parse the list of module-relative include directories (-I).
94 parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string {
95 // include_dirs are root-relative, not module-relative.
96 includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
97 return append(includeDirs, baseCompilerProps.Local_include_dirs...)
98 }
99
100 // Parse the list of copts.
101 parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string {
102 copts := append([]string{}, baseCompilerProps.Cflags...)
103 for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
104 copts = append(copts, includeFlag(dir))
105 }
106 return copts
Jingwen Chen63930982021-03-24 10:04:33 -0400107 }
108
Jingwen Chene32e9e02021-04-23 09:17:24 +0000109 // baseSrcs contain the list of src files that are used for every configuration.
110 var baseSrcs []string
111 // baseExcludeSrcs contain the list of src files that are excluded for every configuration.
112 var baseExcludeSrcs []string
113 // baseSrcsLabelList is a clone of the base srcs LabelList, used for computing the
114 // arch or os specific srcs later.
115 var baseSrcsLabelList bazel.LabelList
116
117 // Parse srcs from an arch or OS's props value, taking the base srcs and
118 // exclude srcs into account.
119 parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList {
120 // Combine the base srcs and arch-specific srcs
121 allSrcs := append(baseSrcs, baseCompilerProps.Srcs...)
122 // Combine the base exclude_srcs and configuration-specific exclude_srcs
123 allExcludeSrcs := append(baseExcludeSrcs, baseCompilerProps.Exclude_srcs...)
124 return android.BazelLabelForModuleSrcExcludes(ctx, allSrcs, allExcludeSrcs)
125 }
126
Jingwen Chenc1c26502021-04-05 10:35:13 +0000127 for _, props := range module.compiler.compilerProps() {
128 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000129 srcs.Value = parseSrcs(baseCompilerProps)
130 copts.Value = parseCopts(baseCompilerProps)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000131
132 // Used for arch-specific srcs later.
133 baseSrcs = baseCompilerProps.Srcs
134 baseExcludeSrcs = baseCompilerProps.Exclude_srcs
135 baseSrcsLabelList = parseSrcs(baseCompilerProps)
Jingwen Chenc1c26502021-04-05 10:35:13 +0000136 break
137 }
138 }
139
Jingwen Chene32e9e02021-04-23 09:17:24 +0000140 // Handle include_build_directory prop. If the property is true, then the
141 // target has access to all headers recursively in the package, and has
142 // "-I<module-dir>" in its copts.
Jingwen Chened9c17d2021-04-13 07:14:55 +0000143 if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
144 copts.Value = append(copts.Value, includeFlag("."))
Jingwen Chened9c17d2021-04-13 07:14:55 +0000145 } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() {
146 copts.Value = append(copts.Value, includeFlag("."))
Jingwen Chened9c17d2021-04-13 07:14:55 +0000147 }
148
Jingwen Chenc1c26502021-04-05 10:35:13 +0000149 for arch, props := range module.GetArchProperties(&BaseCompilerProperties{}) {
150 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000151 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
152 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
153 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
154 srcsList := parseSrcs(baseCompilerProps)
155 srcs.SetValueForArch(arch.Name, srcsList)
156 // The base srcs value should not contain any arch-specific excludes.
157 srcs.Value = bazel.SubtractBazelLabelList(srcs.Value, bazel.LabelList{Includes: srcsList.Excludes})
158 }
159
Jingwen Chened9c17d2021-04-13 07:14:55 +0000160 copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000161 }
162 }
163
Jingwen Chene32e9e02021-04-23 09:17:24 +0000164 // After going through all archs, delete the duplicate files in the arch
165 // values that are already in the base srcs.Value.
166 for arch, props := range module.GetArchProperties(&BaseCompilerProperties{}) {
167 if _, ok := props.(*BaseCompilerProperties); ok {
168 srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcs.GetValueForArch(arch.Name), srcs.Value))
169 }
170 }
171
172 // Now that the srcs.Value list is finalized, compare it with the original
173 // list, and put the difference into the default condition for the arch
174 // select.
175 defaultsSrcs := bazel.SubtractBazelLabelList(baseSrcsLabelList, srcs.Value)
176 // TODO(b/186153868): handle the case with multiple variant types, e.g. when arch and os are both used.
177 srcs.SetValueForArch(bazel.CONDITIONS_DEFAULT, defaultsSrcs)
178
179 // Handle OS specific props.
Jingwen Chenc1c26502021-04-05 10:35:13 +0000180 for os, props := range module.GetTargetProperties(&BaseCompilerProperties{}) {
181 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000182 srcsList := parseSrcs(baseCompilerProps)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000183 // TODO(b/186153868): add support for os-specific srcs and exclude_srcs
184 srcs.SetValueForOS(os.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList))
Jingwen Chened9c17d2021-04-13 07:14:55 +0000185 copts.SetValueForOS(os.Name, parseCopts(baseCompilerProps))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000186 }
187 }
188
Jingwen Chen107c0de2021-04-09 10:43:12 +0000189 return compilerAttributes{
Jingwen Chen107c0de2021-04-09 10:43:12 +0000190 srcs: srcs,
191 copts: copts,
192 }
193}
194
195// Convenience struct to hold all attributes parsed from linker properties.
196type linkerAttributes struct {
197 deps bazel.LabelListAttribute
198 linkopts bazel.StringListAttribute
Jingwen Chenc1c26502021-04-05 10:35:13 +0000199}
200
Jingwen Chen63930982021-03-24 10:04:33 -0400201// bp2BuildParseLinkerProps creates a label list attribute containing the header library deps of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400202// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000203func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Jingwen Chen63930982021-03-24 10:04:33 -0400204 var deps bazel.LabelListAttribute
205 var linkopts bazel.StringListAttribute
206
Jingwen Chen91220d72021-03-24 02:18:33 -0400207 for _, linkerProps := range module.linker.linkerProps() {
208 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
209 libs := baseLinkerProps.Header_libs
210 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000211 libs = append(libs, baseLinkerProps.Static_libs...)
212 libs = append(libs, baseLinkerProps.Whole_static_libs...)
213 libs = android.SortedUniqueStrings(libs)
214 deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs))
Jingwen Chen63930982021-03-24 10:04:33 -0400215 linkopts.Value = baseLinkerProps.Ldflags
Jingwen Chen91220d72021-03-24 02:18:33 -0400216 break
217 }
218 }
219
Jingwen Chen63930982021-03-24 10:04:33 -0400220 for arch, p := range module.GetArchProperties(&BaseLinkerProperties{}) {
221 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
222 libs := baseLinkerProps.Header_libs
223 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000224 libs = append(libs, baseLinkerProps.Static_libs...)
225 libs = append(libs, baseLinkerProps.Whole_static_libs...)
Jingwen Chen63930982021-03-24 10:04:33 -0400226 libs = android.SortedUniqueStrings(libs)
227 deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
228 linkopts.SetValueForArch(arch.Name, baseLinkerProps.Ldflags)
229 }
230 }
231
Jingwen Chen91220d72021-03-24 02:18:33 -0400232 for os, p := range module.GetTargetProperties(&BaseLinkerProperties{}) {
233 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
234 libs := baseLinkerProps.Header_libs
235 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000236 libs = append(libs, baseLinkerProps.Static_libs...)
237 libs = append(libs, baseLinkerProps.Whole_static_libs...)
Jingwen Chen91220d72021-03-24 02:18:33 -0400238 libs = android.SortedUniqueStrings(libs)
Jingwen Chen63930982021-03-24 10:04:33 -0400239 deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
240 linkopts.SetValueForOS(os.Name, baseLinkerProps.Ldflags)
Jingwen Chen91220d72021-03-24 02:18:33 -0400241 }
242 }
243
Jingwen Chen107c0de2021-04-09 10:43:12 +0000244 return linkerAttributes{
245 deps: deps,
246 linkopts: linkopts,
247 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400248}
249
Jingwen Chened9c17d2021-04-13 07:14:55 +0000250// Relativize a list of root-relative paths with respect to the module's
251// directory.
252//
253// include_dirs Soong prop are root-relative (b/183742505), but
254// local_include_dirs, export_include_dirs and export_system_include_dirs are
255// module dir relative. This function makes a list of paths entirely module dir
256// relative.
257//
258// For the `include` attribute, Bazel wants the paths to be relative to the
259// module.
260func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000261 var relativePaths []string
262 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000263 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
264 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
265 if err != nil {
266 panic(err)
267 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000268 relativePaths = append(relativePaths, relativePath)
269 }
270 return relativePaths
271}
272
Jingwen Chened9c17d2021-04-13 07:14:55 +0000273// bp2BuildParseExportedIncludes creates a string list attribute contains the
Jingwen Chen882bcc12021-04-27 05:54:20 +0000274// exported included directories of a module.
275func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
Jingwen Chen91220d72021-03-24 02:18:33 -0400276 libraryDecorator := module.linker.(*libraryDecorator)
277
Jingwen Chened9c17d2021-04-13 07:14:55 +0000278 // Export_system_include_dirs and export_include_dirs are already module dir
279 // relative, so they don't need to be relativized like include_dirs, which
280 // are root-relative.
Jingwen Chen91220d72021-03-24 02:18:33 -0400281 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
282 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000283 includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
Jingwen Chen91220d72021-03-24 02:18:33 -0400284
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000285 for arch, props := range module.GetArchProperties(&FlagExporterProperties{}) {
286 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
287 archIncludeDirs := flagExporterProperties.Export_system_include_dirs
288 archIncludeDirs = append(archIncludeDirs, flagExporterProperties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000289
290 // To avoid duplicate includes when base includes + arch includes are combined
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400291 // FIXME: This doesn't take conflicts between arch and os includes into account
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000292 archIncludeDirs = bazel.SubtractStrings(archIncludeDirs, includeDirs)
293
294 if len(archIncludeDirs) > 0 {
295 includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs)
296 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000297 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400298 }
299
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400300 for os, props := range module.GetTargetProperties(&FlagExporterProperties{}) {
301 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
302 osIncludeDirs := flagExporterProperties.Export_system_include_dirs
303 osIncludeDirs = append(osIncludeDirs, flagExporterProperties.Export_include_dirs...)
304
305 // To avoid duplicate includes when base includes + os includes are combined
306 // FIXME: This doesn't take conflicts between arch and os includes into account
307 osIncludeDirs = bazel.SubtractStrings(osIncludeDirs, includeDirs)
308
309 if len(osIncludeDirs) > 0 {
310 includeDirsAttribute.SetValueForOS(os.Name, osIncludeDirs)
311 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400312 }
313 }
314
Jingwen Chen882bcc12021-04-27 05:54:20 +0000315 return includeDirsAttribute
Jingwen Chen91220d72021-03-24 02:18:33 -0400316}