blob: b11602dd488f1b0bf371483e1e3049ea88ab3d89 [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
93 // Parse the list of srcs, excluding files from exclude_srcs.
94 parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList {
95 return android.BazelLabelForModuleSrcExcludes(ctx, baseCompilerProps.Srcs, baseCompilerProps.Exclude_srcs)
96 }
97
98 // Parse the list of module-relative include directories (-I).
99 parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string {
100 // include_dirs are root-relative, not module-relative.
101 includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
102 return append(includeDirs, baseCompilerProps.Local_include_dirs...)
103 }
104
105 // Parse the list of copts.
106 parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string {
107 copts := append([]string{}, baseCompilerProps.Cflags...)
108 for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
109 copts = append(copts, includeFlag(dir))
110 }
111 return copts
Jingwen Chen63930982021-03-24 10:04:33 -0400112 }
113
Jingwen Chenc1c26502021-04-05 10:35:13 +0000114 for _, props := range module.compiler.compilerProps() {
115 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000116 srcs.Value = parseSrcs(baseCompilerProps)
117 copts.Value = parseCopts(baseCompilerProps)
Jingwen Chenc1c26502021-04-05 10:35:13 +0000118 break
119 }
120 }
121
Jingwen Chened9c17d2021-04-13 07:14:55 +0000122 if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
123 copts.Value = append(copts.Value, includeFlag("."))
Jingwen Chened9c17d2021-04-13 07:14:55 +0000124 } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() {
125 copts.Value = append(copts.Value, includeFlag("."))
Jingwen Chened9c17d2021-04-13 07:14:55 +0000126 }
127
Jingwen Chenc1c26502021-04-05 10:35:13 +0000128 for arch, props := range module.GetArchProperties(&BaseCompilerProperties{}) {
129 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000130 srcsList := parseSrcs(baseCompilerProps)
131 srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcsList, srcs.Value))
132 copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000133 }
134 }
135
136 for os, props := range module.GetTargetProperties(&BaseCompilerProperties{}) {
137 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000138 srcsList := parseSrcs(baseCompilerProps)
139 srcs.SetValueForOS(os.Name, bazel.SubtractBazelLabelList(srcsList, srcs.Value))
140 copts.SetValueForOS(os.Name, parseCopts(baseCompilerProps))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000141 }
142 }
143
Jingwen Chen107c0de2021-04-09 10:43:12 +0000144 return compilerAttributes{
Jingwen Chen107c0de2021-04-09 10:43:12 +0000145 srcs: srcs,
146 copts: copts,
147 }
148}
149
150// Convenience struct to hold all attributes parsed from linker properties.
151type linkerAttributes struct {
152 deps bazel.LabelListAttribute
153 linkopts bazel.StringListAttribute
Jingwen Chenc1c26502021-04-05 10:35:13 +0000154}
155
Jingwen Chen63930982021-03-24 10:04:33 -0400156// bp2BuildParseLinkerProps creates a label list attribute containing the header library deps of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400157// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000158func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Jingwen Chen63930982021-03-24 10:04:33 -0400159 var deps bazel.LabelListAttribute
160 var linkopts bazel.StringListAttribute
161
Jingwen Chen91220d72021-03-24 02:18:33 -0400162 for _, linkerProps := range module.linker.linkerProps() {
163 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
164 libs := baseLinkerProps.Header_libs
165 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000166 libs = append(libs, baseLinkerProps.Static_libs...)
167 libs = append(libs, baseLinkerProps.Whole_static_libs...)
168 libs = android.SortedUniqueStrings(libs)
169 deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs))
Jingwen Chen63930982021-03-24 10:04:33 -0400170 linkopts.Value = baseLinkerProps.Ldflags
Jingwen Chen91220d72021-03-24 02:18:33 -0400171 break
172 }
173 }
174
Jingwen Chen63930982021-03-24 10:04:33 -0400175 for arch, p := range module.GetArchProperties(&BaseLinkerProperties{}) {
176 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
177 libs := baseLinkerProps.Header_libs
178 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000179 libs = append(libs, baseLinkerProps.Static_libs...)
180 libs = append(libs, baseLinkerProps.Whole_static_libs...)
Jingwen Chen63930982021-03-24 10:04:33 -0400181 libs = android.SortedUniqueStrings(libs)
182 deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
183 linkopts.SetValueForArch(arch.Name, baseLinkerProps.Ldflags)
184 }
185 }
186
Jingwen Chen91220d72021-03-24 02:18:33 -0400187 for os, p := range module.GetTargetProperties(&BaseLinkerProperties{}) {
188 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
189 libs := baseLinkerProps.Header_libs
190 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000191 libs = append(libs, baseLinkerProps.Static_libs...)
192 libs = append(libs, baseLinkerProps.Whole_static_libs...)
Jingwen Chen91220d72021-03-24 02:18:33 -0400193 libs = android.SortedUniqueStrings(libs)
Jingwen Chen63930982021-03-24 10:04:33 -0400194 deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
195 linkopts.SetValueForOS(os.Name, baseLinkerProps.Ldflags)
Jingwen Chen91220d72021-03-24 02:18:33 -0400196 }
197 }
198
Jingwen Chen107c0de2021-04-09 10:43:12 +0000199 return linkerAttributes{
200 deps: deps,
201 linkopts: linkopts,
202 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400203}
204
Jingwen Chened9c17d2021-04-13 07:14:55 +0000205// Relativize a list of root-relative paths with respect to the module's
206// directory.
207//
208// include_dirs Soong prop are root-relative (b/183742505), but
209// local_include_dirs, export_include_dirs and export_system_include_dirs are
210// module dir relative. This function makes a list of paths entirely module dir
211// relative.
212//
213// For the `include` attribute, Bazel wants the paths to be relative to the
214// module.
215func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000216 var relativePaths []string
217 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000218 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
219 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
220 if err != nil {
221 panic(err)
222 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000223 relativePaths = append(relativePaths, relativePath)
224 }
225 return relativePaths
226}
227
Jingwen Chened9c17d2021-04-13 07:14:55 +0000228// bp2BuildParseExportedIncludes creates a string list attribute contains the
Jingwen Chen882bcc12021-04-27 05:54:20 +0000229// exported included directories of a module.
230func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
Jingwen Chen91220d72021-03-24 02:18:33 -0400231 libraryDecorator := module.linker.(*libraryDecorator)
232
Jingwen Chened9c17d2021-04-13 07:14:55 +0000233 // Export_system_include_dirs and export_include_dirs are already module dir
234 // relative, so they don't need to be relativized like include_dirs, which
235 // are root-relative.
Jingwen Chen91220d72021-03-24 02:18:33 -0400236 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
237 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000238 includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
Jingwen Chen91220d72021-03-24 02:18:33 -0400239
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000240 for arch, props := range module.GetArchProperties(&FlagExporterProperties{}) {
241 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
242 archIncludeDirs := flagExporterProperties.Export_system_include_dirs
243 archIncludeDirs = append(archIncludeDirs, flagExporterProperties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000244
245 // To avoid duplicate includes when base includes + arch includes are combined
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400246 // FIXME: This doesn't take conflicts between arch and os includes into account
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000247 archIncludeDirs = bazel.SubtractStrings(archIncludeDirs, includeDirs)
248
249 if len(archIncludeDirs) > 0 {
250 includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs)
251 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000252 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400253 }
254
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400255 for os, props := range module.GetTargetProperties(&FlagExporterProperties{}) {
256 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
257 osIncludeDirs := flagExporterProperties.Export_system_include_dirs
258 osIncludeDirs = append(osIncludeDirs, flagExporterProperties.Export_include_dirs...)
259
260 // To avoid duplicate includes when base includes + os includes are combined
261 // FIXME: This doesn't take conflicts between arch and os includes into account
262 osIncludeDirs = bazel.SubtractStrings(osIncludeDirs, includeDirs)
263
264 if len(osIncludeDirs) > 0 {
265 includeDirsAttribute.SetValueForOS(os.Name, osIncludeDirs)
266 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400267 }
268 }
269
Jingwen Chen882bcc12021-04-27 05:54:20 +0000270 return includeDirsAttribute
Jingwen Chen91220d72021-03-24 02:18:33 -0400271}