blob: e7e4aa8b005d73facd8ab4e3b4327d86dc75d9d3 [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"
Rupert Shuttleworthb8151682021-04-06 20:06:21 +000019 "strings"
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...)
56 }
57 }
58
59 ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...)
60}
61
Jingwen Chen63930982021-03-24 10:04:33 -040062// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
63func bp2BuildParseCompilerProps(
64 ctx android.TopDownMutatorContext,
65 module *Module) (
66 copts bazel.StringListAttribute,
67 srcs bazel.LabelListAttribute,
68 hdrs bazel.LabelListAttribute) {
69
70 hdrsAndSrcs := func(baseCompilerProps *BaseCompilerProperties) (bazel.LabelList, bazel.LabelList) {
71 srcsList := android.BazelLabelForModuleSrcExcludes(
72 ctx, baseCompilerProps.Srcs, baseCompilerProps.Exclude_srcs)
73 hdrsList := android.BazelLabelForModuleSrc(ctx, srcsList.LooseHdrsGlobs(headerExts))
74 return hdrsList, srcsList
75 }
76
Jingwen Chenc1c26502021-04-05 10:35:13 +000077 for _, props := range module.compiler.compilerProps() {
78 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chen63930982021-03-24 10:04:33 -040079 hdrs.Value, srcs.Value = hdrsAndSrcs(baseCompilerProps)
80 copts.Value = baseCompilerProps.Cflags
Jingwen Chenc1c26502021-04-05 10:35:13 +000081 break
82 }
83 }
84
85 for arch, props := range module.GetArchProperties(&BaseCompilerProperties{}) {
86 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chen63930982021-03-24 10:04:33 -040087 hdrsList, srcsList := hdrsAndSrcs(baseCompilerProps)
88 hdrs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(hdrsList, hdrs.Value))
89 srcs.SetValueForArch(arch.Name, srcsList)
90 copts.SetValueForArch(arch.Name, baseCompilerProps.Cflags)
Jingwen Chenc1c26502021-04-05 10:35:13 +000091 }
92 }
93
94 for os, props := range module.GetTargetProperties(&BaseCompilerProperties{}) {
95 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chen63930982021-03-24 10:04:33 -040096 hdrsList, srcsList := hdrsAndSrcs(baseCompilerProps)
97 hdrs.SetValueForOS(os.Name, bazel.SubtractBazelLabelList(hdrsList, hdrs.Value))
98 srcs.SetValueForOS(os.Name, srcsList)
99 copts.SetValueForOS(os.Name, baseCompilerProps.Cflags)
Jingwen Chenc1c26502021-04-05 10:35:13 +0000100 }
101 }
102
Jingwen Chen63930982021-03-24 10:04:33 -0400103 return copts, srcs, hdrs
Jingwen Chenc1c26502021-04-05 10:35:13 +0000104}
105
Jingwen Chen63930982021-03-24 10:04:33 -0400106// bp2BuildParseLinkerProps creates a label list attribute containing the header library deps of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400107// configurable attribute values.
Jingwen Chen63930982021-03-24 10:04:33 -0400108func bp2BuildParseLinkerProps(
109 ctx android.TopDownMutatorContext, module *Module) (bazel.LabelListAttribute, bazel.StringListAttribute) {
110
111 var deps bazel.LabelListAttribute
112 var linkopts bazel.StringListAttribute
113
Jingwen Chen91220d72021-03-24 02:18:33 -0400114 for _, linkerProps := range module.linker.linkerProps() {
115 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
116 libs := baseLinkerProps.Header_libs
117 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
Jingwen Chen63930982021-03-24 10:04:33 -0400118 deps = bazel.MakeLabelListAttribute(
Jingwen Chen91220d72021-03-24 02:18:33 -0400119 android.BazelLabelForModuleDeps(ctx, android.SortedUniqueStrings(libs)))
Jingwen Chen63930982021-03-24 10:04:33 -0400120 linkopts.Value = baseLinkerProps.Ldflags
Jingwen Chen91220d72021-03-24 02:18:33 -0400121 break
122 }
123 }
124
Jingwen Chen63930982021-03-24 10:04:33 -0400125 for arch, p := range module.GetArchProperties(&BaseLinkerProperties{}) {
126 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
127 libs := baseLinkerProps.Header_libs
128 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
129 libs = android.SortedUniqueStrings(libs)
130 deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs))
131 linkopts.SetValueForArch(arch.Name, baseLinkerProps.Ldflags)
132 }
133 }
134
Jingwen Chen91220d72021-03-24 02:18:33 -0400135 for os, p := range module.GetTargetProperties(&BaseLinkerProperties{}) {
136 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
137 libs := baseLinkerProps.Header_libs
138 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
139 libs = android.SortedUniqueStrings(libs)
Jingwen Chen63930982021-03-24 10:04:33 -0400140 deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
141 linkopts.SetValueForOS(os.Name, baseLinkerProps.Ldflags)
Jingwen Chen91220d72021-03-24 02:18:33 -0400142 }
143 }
144
Jingwen Chen63930982021-03-24 10:04:33 -0400145 return deps, linkopts
Jingwen Chen91220d72021-03-24 02:18:33 -0400146}
147
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000148func bp2BuildListHeadersInDir(ctx android.TopDownMutatorContext, includeDir string) bazel.LabelList {
Jingwen Chen63930982021-03-24 10:04:33 -0400149 globs := bazel.GlobsInDir(includeDir, includeDir != ".", headerExts)
150 return android.BazelLabelForModuleSrc(ctx, globs)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000151}
152
153// Bazel wants include paths to be relative to the module
154func bp2BuildMakePathsRelativeToModule(ctx android.TopDownMutatorContext, paths []string) []string {
155 var relativePaths []string
156 for _, path := range paths {
157 relativePath := strings.TrimPrefix(path, ctx.ModuleDir()+"/")
158 relativePaths = append(relativePaths, relativePath)
159 }
160 return relativePaths
161}
162
Jingwen Chen91220d72021-03-24 02:18:33 -0400163// bp2BuildParseExportedIncludes creates a label list attribute contains the
164// exported included directories of a module.
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000165func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) (bazel.StringListAttribute, bazel.LabelListAttribute) {
Jingwen Chen91220d72021-03-24 02:18:33 -0400166 libraryDecorator := module.linker.(*libraryDecorator)
167
168 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
169 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000170 includeDirs = bp2BuildMakePathsRelativeToModule(ctx, includeDirs)
171 includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
Jingwen Chen91220d72021-03-24 02:18:33 -0400172
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000173 var headersAttribute bazel.LabelListAttribute
174 var headers bazel.LabelList
Jingwen Chen91220d72021-03-24 02:18:33 -0400175 for _, includeDir := range includeDirs {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000176 headers.Append(bp2BuildListHeadersInDir(ctx, includeDir))
177 }
178 headers = bazel.UniqueBazelLabelList(headers)
179 headersAttribute.Value = headers
180
181 for arch, props := range module.GetArchProperties(&FlagExporterProperties{}) {
182 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
183 archIncludeDirs := flagExporterProperties.Export_system_include_dirs
184 archIncludeDirs = append(archIncludeDirs, flagExporterProperties.Export_include_dirs...)
185 archIncludeDirs = bp2BuildMakePathsRelativeToModule(ctx, archIncludeDirs)
186
187 // To avoid duplicate includes when base includes + arch includes are combined
188 archIncludeDirs = bazel.SubtractStrings(archIncludeDirs, includeDirs)
189
190 if len(archIncludeDirs) > 0 {
191 includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs)
192 }
193
194 var archHeaders bazel.LabelList
195 for _, archIncludeDir := range archIncludeDirs {
196 archHeaders.Append(bp2BuildListHeadersInDir(ctx, archIncludeDir))
197 }
198 archHeaders = bazel.UniqueBazelLabelList(archHeaders)
199
200 // To avoid duplicate headers when base headers + arch headers are combined
201 archHeaders = bazel.SubtractBazelLabelList(archHeaders, headers)
202
203 if len(archHeaders.Includes) > 0 || len(archHeaders.Excludes) > 0 {
204 headersAttribute.SetValueForArch(arch.Name, archHeaders)
205 }
206 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400207 }
208
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000209 return includeDirsAttribute, headersAttribute
Jingwen Chen91220d72021-03-24 02:18:33 -0400210}