blob: 484e922d19829e4926610aef60864c055edf96ae [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 (
Jingwen Chen14a8bda2021-06-02 11:10:02 +000017 "fmt"
Jingwen Chened9c17d2021-04-13 07:14:55 +000018 "path/filepath"
Jingwen Chen3950cd62021-05-12 04:33:00 +000019 "strings"
Chris Parsons484e50a2021-05-13 15:13:04 -040020
21 "android/soong/android"
22 "android/soong/bazel"
Liz Kammerba7a9c52021-05-26 08:45:30 -040023
24 "github.com/google/blueprint/proptools"
Jingwen Chen91220d72021-03-24 02:18:33 -040025)
26
27// bp2build functions and helpers for converting cc_* modules to Bazel.
28
29func init() {
30 android.DepsBp2BuildMutators(RegisterDepsBp2Build)
31}
32
33func RegisterDepsBp2Build(ctx android.RegisterMutatorsContext) {
34 ctx.BottomUp("cc_bp2build_deps", depsBp2BuildMutator)
35}
36
37// A naive deps mutator to add deps on all modules across all combinations of
38// target props for cc modules. This is needed to make module -> bazel label
39// resolution work in the bp2build mutator later. This is probably
40// the wrong way to do it, but it works.
41//
42// TODO(jingwen): can we create a custom os mutator in depsBp2BuildMutator to do this?
43func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) {
44 module, ok := ctx.Module().(*Module)
45 if !ok {
46 // Not a cc module
47 return
48 }
49
50 if !module.ConvertWithBp2build(ctx) {
51 return
52 }
53
54 var allDeps []string
55
Liz Kammer9abd62d2021-05-21 08:37:59 -040056 for _, configToProps := range module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) {
57 for _, props := range configToProps {
58 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040059 allDeps = append(allDeps, baseCompilerProps.Generated_headers...)
60 allDeps = append(allDeps, baseCompilerProps.Generated_sources...)
61 }
62 }
Chris Parsons484e50a2021-05-13 15:13:04 -040063 }
64
Liz Kammer9abd62d2021-05-21 08:37:59 -040065 for _, configToProps := range module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) {
66 for _, props := range configToProps {
67 if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040068 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
69 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
70 allDeps = append(allDeps, baseLinkerProps.Static_libs...)
Liz Kammer9abd62d2021-05-21 08:37:59 -040071 allDeps = append(allDeps, baseLinkerProps.Exclude_static_libs...)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040072 allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...)
Liz Kammer9abd62d2021-05-21 08:37:59 -040073 allDeps = append(allDeps, baseLinkerProps.Shared_libs...)
74 allDeps = append(allDeps, baseLinkerProps.Exclude_shared_libs...)
Liz Kammer561923e2021-07-21 09:47:32 -040075 allDeps = append(allDeps, baseLinkerProps.System_shared_libs...)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040076 }
77 }
Jingwen Chened9c17d2021-04-13 07:14:55 +000078 }
79
Jingwen Chen53681ef2021-04-29 08:15:13 +000080 // Deps in the static: { .. } and shared: { .. } props of a cc_library.
81 if lib, ok := module.compiler.(*libraryDecorator); ok {
Liz Kammer561923e2021-07-21 09:47:32 -040082 appendDeps := func(deps []string, p StaticOrSharedProperties, system bool) []string {
Jingwen Chenbcf53042021-05-26 04:42:42 +000083 deps = append(deps, p.Static_libs...)
84 deps = append(deps, p.Whole_static_libs...)
85 deps = append(deps, p.Shared_libs...)
Liz Kammer561923e2021-07-21 09:47:32 -040086 // TODO(b/186024507, b/186489250): Temporarily exclude adding
87 // system_shared_libs deps until libc and libm builds.
88 if system {
89 allDeps = append(allDeps, p.System_shared_libs...)
90 }
Jingwen Chenbcf53042021-05-26 04:42:42 +000091 return deps
92 }
Jingwen Chen53681ef2021-04-29 08:15:13 +000093
Liz Kammer561923e2021-07-21 09:47:32 -040094 allDeps = appendDeps(allDeps, lib.SharedProperties.Shared, lib.shared())
95 allDeps = appendDeps(allDeps, lib.StaticProperties.Static, lib.static())
Jingwen Chenbcf53042021-05-26 04:42:42 +000096
97 // Deps in the target/arch nested static: { .. } and shared: { .. } props of a cc_library.
98 // target: { <target>: shared: { ... } }
Liz Kammer9abd62d2021-05-21 08:37:59 -040099 for _, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) {
100 for _, props := range configToProps {
101 if p, ok := props.(*SharedProperties); ok {
Liz Kammer561923e2021-07-21 09:47:32 -0400102 allDeps = appendDeps(allDeps, p.Shared, lib.shared())
Jingwen Chenbcf53042021-05-26 04:42:42 +0000103 }
104 }
105 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400106
107 for _, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) {
108 for _, props := range configToProps {
109 if p, ok := props.(*StaticProperties); ok {
Liz Kammer561923e2021-07-21 09:47:32 -0400110 allDeps = appendDeps(allDeps, p.Static, lib.static())
Jingwen Chenbcf53042021-05-26 04:42:42 +0000111 }
112 }
113 }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000114 }
115
Liz Kammer47535c52021-06-02 16:02:22 -0400116 // product variables only support a limited set of fields, this is the full list of field names
117 // related to cc module dependency management that are supported.
118 productVariableDepFields := [4]string{
119 "Shared_libs",
120 "Static_libs",
121 "Exclude_static_libs",
122 "Whole_static_libs",
123 }
124
125 productVariableProps := android.ProductVariableProperties(ctx)
126 for _, name := range productVariableDepFields {
127 props, exists := productVariableProps[name]
128 if !exists {
129 continue
130 }
131 for _, prop := range props {
132 if p, ok := prop.Property.([]string); !ok {
133 ctx.ModuleErrorf("Could not convert product variable %s property", name)
134 } else {
135 allDeps = append(allDeps, p...)
136 }
137 }
138 }
139
Jingwen Chen91220d72021-03-24 02:18:33 -0400140 ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...)
141}
142
Liz Kammer2222c6b2021-05-24 15:41:47 -0400143// staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties --
Jingwen Chenbcf53042021-05-26 04:42:42 +0000144// properties which apply to either the shared or static version of a cc_library module.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400145type staticOrSharedAttributes struct {
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000146 Srcs bazel.LabelListAttribute
147 Srcs_c bazel.LabelListAttribute
148 Srcs_as bazel.LabelListAttribute
149 Copts bazel.StringListAttribute
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000150
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000151 Static_deps bazel.LabelListAttribute
152 Dynamic_deps bazel.LabelListAttribute
153 Whole_archive_deps bazel.LabelListAttribute
Jingwen Chen53681ef2021-04-29 08:15:13 +0000154}
155
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000156func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) (cppSrcs, cSrcs, asSrcs bazel.LabelListAttribute) {
157 // Branch srcs into three language-specific groups.
158 // C++ is the "catch-all" group, and comprises generated sources because we don't
159 // know the language of these sources until the genrule is executed.
160 // TODO(b/190006308): Handle language detection of sources in a Bazel rule.
161 isCSrcOrFilegroup := func(s string) bool {
162 return strings.HasSuffix(s, ".c") || strings.HasSuffix(s, "_c_srcs")
163 }
164
165 isAsmSrcOrFilegroup := func(s string) bool {
166 return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s") || strings.HasSuffix(s, "_as_srcs")
167 }
168
169 // Check that a module is a filegroup type named <label>.
170 isFilegroupNamed := func(m android.Module, fullLabel string) bool {
171 if ctx.OtherModuleType(m) != "filegroup" {
172 return false
173 }
174 labelParts := strings.Split(fullLabel, ":")
175 if len(labelParts) > 2 {
176 // There should not be more than one colon in a label.
177 panic(fmt.Errorf("%s is not a valid Bazel label for a filegroup", fullLabel))
178 } else {
179 return m.Name() == labelParts[len(labelParts)-1]
180 }
181 }
182
183 // Convert the filegroup dependencies into the extension-specific filegroups
184 // filtered in the filegroup.bzl macro.
185 cppFilegroup := func(label string) string {
186 ctx.VisitDirectDeps(func(m android.Module) {
187 if isFilegroupNamed(m, label) {
188 label = label + "_cpp_srcs"
189 return
190 }
191 })
192 return label
193 }
194 cFilegroup := func(label string) string {
195 ctx.VisitDirectDeps(func(m android.Module) {
196 if isFilegroupNamed(m, label) {
197 label = label + "_c_srcs"
198 return
199 }
200 })
201 return label
202 }
203 asFilegroup := func(label string) string {
204 ctx.VisitDirectDeps(func(m android.Module) {
205 if isFilegroupNamed(m, label) {
206 label = label + "_as_srcs"
207 return
208 }
209 })
210 return label
211 }
212
213 cSrcs = bazel.MapLabelListAttribute(srcs, cFilegroup)
214 cSrcs = bazel.FilterLabelListAttribute(cSrcs, isCSrcOrFilegroup)
215
216 asSrcs = bazel.MapLabelListAttribute(srcs, asFilegroup)
217 asSrcs = bazel.FilterLabelListAttribute(asSrcs, isAsmSrcOrFilegroup)
218
219 cppSrcs = bazel.MapLabelListAttribute(srcs, cppFilegroup)
220 cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, cSrcs)
221 cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, asSrcs)
222 return
223}
224
Jingwen Chen53681ef2021-04-29 08:15:13 +0000225// bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400226func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000227 lib, ok := module.compiler.(*libraryDecorator)
228 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400229 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000230 }
231
Jingwen Chenbcf53042021-05-26 04:42:42 +0000232 return bp2buildParseStaticOrSharedProps(ctx, module, lib, false)
Jingwen Chen53681ef2021-04-29 08:15:13 +0000233}
234
235// bp2buildParseStaticProps returns the attributes for the static variant of a cc_library.
Liz Kammer2222c6b2021-05-24 15:41:47 -0400236func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes {
Jingwen Chen53681ef2021-04-29 08:15:13 +0000237 lib, ok := module.compiler.(*libraryDecorator)
238 if !ok {
Liz Kammer2222c6b2021-05-24 15:41:47 -0400239 return staticOrSharedAttributes{}
Jingwen Chen53681ef2021-04-29 08:15:13 +0000240 }
241
Jingwen Chenbcf53042021-05-26 04:42:42 +0000242 return bp2buildParseStaticOrSharedProps(ctx, module, lib, true)
Liz Kammer2222c6b2021-05-24 15:41:47 -0400243}
244
Jingwen Chenbcf53042021-05-26 04:42:42 +0000245func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes {
246 var props StaticOrSharedProperties
247 if isStatic {
248 props = lib.StaticProperties.Static
249 } else {
250 props = lib.SharedProperties.Shared
Jingwen Chen53681ef2021-04-29 08:15:13 +0000251 }
Jingwen Chenbcf53042021-05-26 04:42:42 +0000252
253 attrs := staticOrSharedAttributes{
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000254 Copts: bazel.StringListAttribute{Value: props.Cflags},
255 Srcs: bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, props.Srcs)),
256 Static_deps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Static_libs)),
Chris Parsons69fa9f92021-07-13 11:47:44 -0400257 Dynamic_deps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, append(props.Shared_libs, props.System_shared_libs...))),
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000258 Whole_archive_deps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleWholeDeps(ctx, props.Whole_static_libs)),
Jingwen Chenbcf53042021-05-26 04:42:42 +0000259 }
260
Liz Kammer9abd62d2021-05-21 08:37:59 -0400261 setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) {
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000262 attrs.Copts.SetSelectValue(axis, config, props.Cflags)
263 attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs))
264 attrs.Static_deps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Static_libs))
265 attrs.Dynamic_deps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Shared_libs))
266 attrs.Whole_archive_deps.SetSelectValue(axis, config, android.BazelLabelForModuleWholeDeps(ctx, props.Whole_static_libs))
Jingwen Chenbcf53042021-05-26 04:42:42 +0000267 }
268
269 if isStatic {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400270 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) {
271 for config, props := range configToProps {
272 if staticOrSharedProps, ok := props.(*StaticProperties); ok {
273 setAttrs(axis, config, staticOrSharedProps.Static)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000274 }
275 }
276 }
277 } else {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400278 for axis, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) {
279 for config, props := range configToProps {
280 if staticOrSharedProps, ok := props.(*SharedProperties); ok {
281 setAttrs(axis, config, staticOrSharedProps.Shared)
Jingwen Chenbcf53042021-05-26 04:42:42 +0000282 }
283 }
284 }
285 }
286
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000287 cppSrcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, attrs.Srcs)
288 attrs.Srcs = cppSrcs
289 attrs.Srcs_c = cSrcs
290 attrs.Srcs_as = asSrcs
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000291
Jingwen Chenbcf53042021-05-26 04:42:42 +0000292 return attrs
Jingwen Chen53681ef2021-04-29 08:15:13 +0000293}
294
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400295// Convenience struct to hold all attributes parsed from prebuilt properties.
296type prebuiltAttributes struct {
297 Src bazel.LabelAttribute
298}
299
300func Bp2BuildParsePrebuiltLibraryProps(ctx android.TopDownMutatorContext, module *Module) prebuiltAttributes {
301 prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker)
302 prebuiltLinker := prebuiltLibraryLinker.prebuiltLinker
303
304 var srcLabelAttribute bazel.LabelAttribute
305
306 if len(prebuiltLinker.properties.Srcs) > 1 {
307 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file\n")
308 }
309
310 if len(prebuiltLinker.properties.Srcs) == 1 {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400311 srcLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinker.properties.Srcs[0]))
312 }
313 for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltLinkerProperties{}) {
314 for config, props := range configToProps {
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400315 if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok {
316 if len(prebuiltLinkerProperties.Srcs) > 1 {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400317 ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for %s %s\n", axis, config)
318 continue
319 } else if len(prebuiltLinkerProperties.Srcs) == 0 {
320 continue
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400321 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400322 src := android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0])
323 srcLabelAttribute.SetSelectValue(axis, config, src)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400324 }
325 }
326 }
327
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400328 return prebuiltAttributes{
329 Src: srcLabelAttribute,
330 }
331}
332
Jingwen Chen107c0de2021-04-09 10:43:12 +0000333// Convenience struct to hold all attributes parsed from compiler properties.
334type compilerAttributes struct {
Chris Parsons990c4f42021-05-25 12:10:58 -0400335 // Options for all languages
336 copts bazel.StringListAttribute
337 // Assembly options and sources
338 asFlags bazel.StringListAttribute
339 asSrcs bazel.LabelListAttribute
340 // C options and sources
341 conlyFlags bazel.StringListAttribute
342 cSrcs bazel.LabelListAttribute
343 // C++ options and sources
344 cppFlags bazel.StringListAttribute
Jingwen Chened9c17d2021-04-13 07:14:55 +0000345 srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000346}
347
Jingwen Chen63930982021-03-24 10:04:33 -0400348// bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000349func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes {
Jingwen Chen882bcc12021-04-27 05:54:20 +0000350 var srcs bazel.LabelListAttribute
Jingwen Chen107c0de2021-04-09 10:43:12 +0000351 var copts bazel.StringListAttribute
Chris Parsons990c4f42021-05-25 12:10:58 -0400352 var asFlags bazel.StringListAttribute
353 var conlyFlags bazel.StringListAttribute
354 var cppFlags bazel.StringListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400355
Chris Parsons484e50a2021-05-13 15:13:04 -0400356 // Creates the -I flags for a directory, while making the directory relative
Jingwen Chened9c17d2021-04-13 07:14:55 +0000357 // to the exec root for Bazel to work.
Chris Parsons484e50a2021-05-13 15:13:04 -0400358 includeFlags := func(dir string) []string {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000359 // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements.
Chris Parsons484e50a2021-05-13 15:13:04 -0400360 moduleDirRootedPath := filepath.Join(ctx.ModuleDir(), dir)
361 return []string{
362 "-I" + moduleDirRootedPath,
363 // Include the bindir-rooted path (using make variable substitution). This most
364 // closely matches Bazel's native include path handling, which allows for dependency
365 // on generated headers in these directories.
366 // TODO(b/188084383): Handle local include directories in Bazel.
367 "-I$(BINDIR)/" + moduleDirRootedPath,
368 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000369 }
370
Jingwen Chened9c17d2021-04-13 07:14:55 +0000371 // Parse the list of module-relative include directories (-I).
372 parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string {
373 // include_dirs are root-relative, not module-relative.
374 includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs)
375 return append(includeDirs, baseCompilerProps.Local_include_dirs...)
376 }
377
Chris Parsons990c4f42021-05-25 12:10:58 -0400378 parseCommandLineFlags := func(soongFlags []string) []string {
379 var result []string
380 for _, flag := range soongFlags {
Colin Cross52aa4e12021-05-25 15:20:39 +0000381 // Soong's cflags can contain spaces, like `-include header.h`. For
382 // Bazel's copts, split them up to be compatible with the
383 // no_copts_tokenization feature.
Chris Parsons990c4f42021-05-25 12:10:58 -0400384 result = append(result, strings.Split(flag, " ")...)
Colin Cross52aa4e12021-05-25 15:20:39 +0000385 }
Chris Parsons990c4f42021-05-25 12:10:58 -0400386 return result
387 }
388
Liz Kammer74deed42021-06-02 13:02:03 -0400389 // Parse srcs from an arch or OS's props value.
Jingwen Chene32e9e02021-04-23 09:17:24 +0000390 parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList {
Chris Parsons484e50a2021-05-13 15:13:04 -0400391 // Add srcs-like dependencies such as generated files.
392 // First create a LabelList containing these dependencies, then merge the values with srcs.
393 generatedHdrsAndSrcs := baseCompilerProps.Generated_headers
394 generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...)
Chris Parsons484e50a2021-05-13 15:13:04 -0400395 generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs)
396
Liz Kammer74deed42021-06-02 13:02:03 -0400397 allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, baseCompilerProps.Srcs, baseCompilerProps.Exclude_srcs)
Chris Parsons484e50a2021-05-13 15:13:04 -0400398 return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000399 }
400
Jingwen Chenc1c26502021-04-05 10:35:13 +0000401 for _, props := range module.compiler.compilerProps() {
402 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400403 srcs.SetValue(parseSrcs(baseCompilerProps))
Chris Parsons69fa9f92021-07-13 11:47:44 -0400404 copts.Value = parseCommandLineFlags(baseCompilerProps.Cflags)
Chris Parsons990c4f42021-05-25 12:10:58 -0400405 asFlags.Value = parseCommandLineFlags(baseCompilerProps.Asflags)
406 conlyFlags.Value = parseCommandLineFlags(baseCompilerProps.Conlyflags)
407 cppFlags.Value = parseCommandLineFlags(baseCompilerProps.Cppflags)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000408
Chris Parsons69fa9f92021-07-13 11:47:44 -0400409 for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
410 copts.Value = append(copts.Value, includeFlags(dir)...)
411 asFlags.Value = append(asFlags.Value, includeFlags(dir)...)
412 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000413 break
414 }
415 }
416
Jingwen Chene32e9e02021-04-23 09:17:24 +0000417 // Handle include_build_directory prop. If the property is true, then the
418 // target has access to all headers recursively in the package, and has
419 // "-I<module-dir>" in its copts.
Jingwen Chened9c17d2021-04-13 07:14:55 +0000420 if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400421 copts.Value = append(copts.Value, includeFlags(".")...)
Chris Parsons69fa9f92021-07-13 11:47:44 -0400422 asFlags.Value = append(asFlags.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000423 } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() {
Chris Parsons484e50a2021-05-13 15:13:04 -0400424 copts.Value = append(copts.Value, includeFlags(".")...)
Chris Parsons69fa9f92021-07-13 11:47:44 -0400425 asFlags.Value = append(asFlags.Value, includeFlags(".")...)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000426 }
427
Liz Kammer9abd62d2021-05-21 08:37:59 -0400428 archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{})
Jingwen Chene32e9e02021-04-23 09:17:24 +0000429
Liz Kammer9abd62d2021-05-21 08:37:59 -0400430 for axis, configToProps := range archVariantCompilerProps {
431 for config, props := range configToProps {
432 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
433 // If there's arch specific srcs or exclude_srcs, generate a select entry for it.
434 // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too.
435 if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 {
436 srcsList := parseSrcs(baseCompilerProps)
437 srcs.SetSelectValue(axis, config, srcsList)
Liz Kammer9abd62d2021-05-21 08:37:59 -0400438 }
439
Chris Parsons69fa9f92021-07-13 11:47:44 -0400440 archVariantCopts := parseCommandLineFlags(baseCompilerProps.Cflags)
441 archVariantAsflags := parseCommandLineFlags(baseCompilerProps.Asflags)
442 for _, dir := range parseLocalIncludeDirs(baseCompilerProps) {
443 archVariantCopts = append(archVariantCopts, includeFlags(dir)...)
444 archVariantAsflags = append(archVariantAsflags, includeFlags(dir)...)
445 }
446
447 copts.SetSelectValue(axis, config, archVariantCopts)
448 asFlags.SetSelectValue(axis, config, archVariantAsflags)
Liz Kammer9abd62d2021-05-21 08:37:59 -0400449 conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Conlyflags))
450 cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Cppflags))
451 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000452 }
453 }
454
Liz Kammer74deed42021-06-02 13:02:03 -0400455 srcs.ResolveExcludes()
Jingwen Chenc1c26502021-04-05 10:35:13 +0000456
Liz Kammerba7a9c52021-05-26 08:45:30 -0400457 productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{
458 "Cflags": &copts,
459 "Asflags": &asFlags,
460 "CppFlags": &cppFlags,
461 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400462 productVariableProps := android.ProductVariableProperties(ctx)
Liz Kammerba7a9c52021-05-26 08:45:30 -0400463 for propName, attr := range productVarPropNameToAttribute {
464 if props, exists := productVariableProps[propName]; exists {
465 for _, prop := range props {
466 flags, ok := prop.Property.([]string)
467 if !ok {
468 ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName))
469 }
470 newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable)
Liz Kammer47535c52021-06-02 16:02:22 -0400471 attr.SetSelectValue(bazel.ProductVariableConfigurationAxis(prop.FullConfig), prop.FullConfig, newFlags)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400472 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400473 }
474 }
475
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000476 srcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, srcs)
477
Jingwen Chen107c0de2021-04-09 10:43:12 +0000478 return compilerAttributes{
Chris Parsons990c4f42021-05-25 12:10:58 -0400479 copts: copts,
480 srcs: srcs,
481 asFlags: asFlags,
482 asSrcs: asSrcs,
483 cSrcs: cSrcs,
484 conlyFlags: conlyFlags,
485 cppFlags: cppFlags,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000486 }
487}
488
489// Convenience struct to hold all attributes parsed from linker properties.
490type linkerAttributes struct {
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000491 deps bazel.LabelListAttribute
492 dynamicDeps bazel.LabelListAttribute
493 wholeArchiveDeps bazel.LabelListAttribute
494 exportedDeps bazel.LabelListAttribute
495 useLibcrt bazel.BoolAttribute
496 linkopts bazel.StringListAttribute
497 versionScript bazel.LabelAttribute
498 stripKeepSymbols bazel.BoolAttribute
499 stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
500 stripKeepSymbolsList bazel.StringListAttribute
501 stripAll bazel.BoolAttribute
502 stripNone bazel.BoolAttribute
Jingwen Chenc1c26502021-04-05 10:35:13 +0000503}
504
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400505// FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here
506func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string {
507 flags := linkerProperties.Ldflags
508 if !BoolDefault(linkerProperties.Pack_relocations, true) {
509 flags = append(flags, "-Wl,--pack-dyn-relocs=none")
510 }
511 return flags
512}
513
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200514// bp2BuildParseLinkerProps parses the linker properties of a module, including
Jingwen Chen91220d72021-03-24 02:18:33 -0400515// configurable attribute values.
Jingwen Chen107c0de2021-04-09 10:43:12 +0000516func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes {
Liz Kammer47535c52021-06-02 16:02:22 -0400517 var headerDeps bazel.LabelListAttribute
518 var staticDeps bazel.LabelListAttribute
Chris Parsonsd6358772021-05-18 18:35:24 -0400519 var exportedDeps bazel.LabelListAttribute
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400520 var dynamicDeps bazel.LabelListAttribute
Chris Parsons08648312021-05-06 16:23:19 -0400521 var wholeArchiveDeps bazel.LabelListAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400522 var linkopts bazel.StringListAttribute
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200523 var versionScript bazel.LabelAttribute
Liz Kammerd366c902021-06-03 13:43:01 -0400524 var useLibcrt bazel.BoolAttribute
Jingwen Chen63930982021-03-24 10:04:33 -0400525
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000526 var stripKeepSymbols bazel.BoolAttribute
527 var stripKeepSymbolsAndDebugFrame bazel.BoolAttribute
528 var stripKeepSymbolsList bazel.StringListAttribute
529 var stripAll bazel.BoolAttribute
530 var stripNone bazel.BoolAttribute
531
532 if libraryDecorator, ok := module.linker.(*libraryDecorator); ok {
533 stripProperties := libraryDecorator.stripper.StripProperties
534 stripKeepSymbols.Value = stripProperties.Strip.Keep_symbols
535 stripKeepSymbolsList.Value = stripProperties.Strip.Keep_symbols_list
536 stripKeepSymbolsAndDebugFrame.Value = stripProperties.Strip.Keep_symbols_and_debug_frame
537 stripAll.Value = stripProperties.Strip.All
538 stripNone.Value = stripProperties.Strip.None
539 }
540
541 for axis, configToProps := range module.GetArchVariantProperties(ctx, &StripProperties{}) {
542 for config, props := range configToProps {
543 if stripProperties, ok := props.(*StripProperties); ok {
544 stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols)
545 stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list)
546 stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame)
547 stripAll.SetSelectValue(axis, config, stripProperties.Strip.All)
548 stripNone.SetSelectValue(axis, config, stripProperties.Strip.None)
549 }
550 }
551 }
552
Jingwen Chen91220d72021-03-24 02:18:33 -0400553 for _, linkerProps := range module.linker.linkerProps() {
554 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
Liz Kammer47535c52021-06-02 16:02:22 -0400555 // Excludes to parallel Soong:
556 // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0
557 staticLibs := android.FirstUniqueStrings(baseLinkerProps.Static_libs)
558 staticDeps.Value = android.BazelLabelForModuleDepsExcludes(ctx, staticLibs, baseLinkerProps.Exclude_static_libs)
559 wholeArchiveLibs := android.FirstUniqueStrings(baseLinkerProps.Whole_static_libs)
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400560 wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleWholeDepsExcludes(ctx, wholeArchiveLibs, baseLinkerProps.Exclude_static_libs))
Chris Parsons69fa9f92021-07-13 11:47:44 -0400561 // TODO(b/186024507): Handle system_shared_libs as its own attribute, so that the appropriate default
562 // may be supported.
563 sharedLibs := android.FirstUniqueStrings(append(baseLinkerProps.Shared_libs, baseLinkerProps.System_shared_libs...))
Liz Kammer47535c52021-06-02 16:02:22 -0400564 dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDepsExcludes(ctx, sharedLibs, baseLinkerProps.Exclude_shared_libs))
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200565
Liz Kammer47535c52021-06-02 16:02:22 -0400566 headerLibs := android.FirstUniqueStrings(baseLinkerProps.Header_libs)
567 headerDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, headerLibs))
568 // TODO(b/188796939): also handle export_static_lib_headers, export_shared_lib_headers,
569 // export_generated_headers
570 exportedLibs := android.FirstUniqueStrings(baseLinkerProps.Export_header_lib_headers)
571 exportedDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, exportedLibs))
572
573 linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps)
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200574 if baseLinkerProps.Version_script != nil {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400575 versionScript.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200576 }
Liz Kammerd366c902021-06-03 13:43:01 -0400577 useLibcrt.Value = baseLinkerProps.libCrt()
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400578
Jingwen Chen91220d72021-03-24 02:18:33 -0400579 break
580 }
581 }
582
Liz Kammer9abd62d2021-05-21 08:37:59 -0400583 for axis, configToProps := range module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) {
584 for config, props := range configToProps {
585 if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok {
Liz Kammer47535c52021-06-02 16:02:22 -0400586 staticLibs := android.FirstUniqueStrings(baseLinkerProps.Static_libs)
587 staticDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDepsExcludes(ctx, staticLibs, baseLinkerProps.Exclude_static_libs))
588 wholeArchiveLibs := android.FirstUniqueStrings(baseLinkerProps.Whole_static_libs)
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400589 wholeArchiveDeps.SetSelectValue(axis, config, android.BazelLabelForModuleWholeDepsExcludes(ctx, wholeArchiveLibs, baseLinkerProps.Exclude_static_libs))
Chris Parsons69fa9f92021-07-13 11:47:44 -0400590 sharedLibs := android.FirstUniqueStrings(append(baseLinkerProps.Shared_libs, baseLinkerProps.System_shared_libs...))
Liz Kammer47535c52021-06-02 16:02:22 -0400591 dynamicDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDepsExcludes(ctx, sharedLibs, baseLinkerProps.Exclude_shared_libs))
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400592
Liz Kammer47535c52021-06-02 16:02:22 -0400593 headerLibs := android.FirstUniqueStrings(baseLinkerProps.Header_libs)
594 headerDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, headerLibs))
595 exportedLibs := android.FirstUniqueStrings(baseLinkerProps.Export_header_lib_headers)
596 exportedDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, exportedLibs))
597
598 linkopts.SetSelectValue(axis, config, getBp2BuildLinkerFlags(baseLinkerProps))
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400599 if baseLinkerProps.Version_script != nil {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400600 versionScript.SetSelectValue(axis, config, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script))
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -0400601 }
Liz Kammerd366c902021-06-03 13:43:01 -0400602 useLibcrt.SetSelectValue(axis, config, baseLinkerProps.libCrt())
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400603 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400604 }
605 }
606
Liz Kammer47535c52021-06-02 16:02:22 -0400607 type productVarDep struct {
608 // the name of the corresponding excludes field, if one exists
609 excludesField string
610 // reference to the bazel attribute that should be set for the given product variable config
611 attribute *bazel.LabelListAttribute
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400612
613 depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList
Liz Kammer47535c52021-06-02 16:02:22 -0400614 }
615
616 productVarToDepFields := map[string]productVarDep{
617 // product variables do not support exclude_shared_libs
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400618 "Shared_libs": productVarDep{attribute: &dynamicDeps, depResolutionFunc: android.BazelLabelForModuleDepsExcludes},
619 "Static_libs": productVarDep{"Exclude_static_libs", &staticDeps, android.BazelLabelForModuleDepsExcludes},
620 "Whole_static_libs": productVarDep{"Exclude_static_libs", &wholeArchiveDeps, android.BazelLabelForModuleWholeDepsExcludes},
Liz Kammer47535c52021-06-02 16:02:22 -0400621 }
622
623 productVariableProps := android.ProductVariableProperties(ctx)
624 for name, dep := range productVarToDepFields {
625 props, exists := productVariableProps[name]
626 excludeProps, excludesExists := productVariableProps[dep.excludesField]
627 // if neither an include or excludes property exists, then skip it
628 if !exists && !excludesExists {
629 continue
630 }
631 // collect all the configurations that an include or exclude property exists for.
632 // we want to iterate all configurations rather than either the include or exclude because for a
633 // particular configuration we may have only and include or only an exclude to handle
634 configs := make(map[string]bool, len(props)+len(excludeProps))
635 for config := range props {
636 configs[config] = true
637 }
638 for config := range excludeProps {
639 configs[config] = true
640 }
641
642 for config := range configs {
643 prop, includesExists := props[config]
644 excludesProp, excludesExists := excludeProps[config]
645 var includes, excludes []string
646 var ok bool
647 // if there was no includes/excludes property, casting fails and that's expected
648 if includes, ok = prop.Property.([]string); includesExists && !ok {
649 ctx.ModuleErrorf("Could not convert product variable %s property", name)
650 }
651 if excludes, ok = excludesProp.Property.([]string); excludesExists && !ok {
652 ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField)
653 }
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400654
655 dep.attribute.SetSelectValue(bazel.ProductVariableConfigurationAxis(config), config, dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes))
Liz Kammer47535c52021-06-02 16:02:22 -0400656 }
657 }
658
659 staticDeps.ResolveExcludes()
660 dynamicDeps.ResolveExcludes()
661 wholeArchiveDeps.ResolveExcludes()
662
663 headerDeps.Append(staticDeps)
664
Jingwen Chen107c0de2021-04-09 10:43:12 +0000665 return linkerAttributes{
Liz Kammer47535c52021-06-02 16:02:22 -0400666 deps: headerDeps,
Chris Parsonsd6358772021-05-18 18:35:24 -0400667 exportedDeps: exportedDeps,
Chris Parsons08648312021-05-06 16:23:19 -0400668 dynamicDeps: dynamicDeps,
669 wholeArchiveDeps: wholeArchiveDeps,
670 linkopts: linkopts,
Liz Kammerd366c902021-06-03 13:43:01 -0400671 useLibcrt: useLibcrt,
Chris Parsons08648312021-05-06 16:23:19 -0400672 versionScript: versionScript,
Jingwen Chen3d383bb2021-06-09 07:18:37 +0000673
674 // Strip properties
675 stripKeepSymbols: stripKeepSymbols,
676 stripKeepSymbolsAndDebugFrame: stripKeepSymbolsAndDebugFrame,
677 stripKeepSymbolsList: stripKeepSymbolsList,
678 stripAll: stripAll,
679 stripNone: stripNone,
Jingwen Chen107c0de2021-04-09 10:43:12 +0000680 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400681}
682
Jingwen Chened9c17d2021-04-13 07:14:55 +0000683// Relativize a list of root-relative paths with respect to the module's
684// directory.
685//
686// include_dirs Soong prop are root-relative (b/183742505), but
687// local_include_dirs, export_include_dirs and export_system_include_dirs are
688// module dir relative. This function makes a list of paths entirely module dir
689// relative.
690//
691// For the `include` attribute, Bazel wants the paths to be relative to the
692// module.
693func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string {
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000694 var relativePaths []string
695 for _, path := range paths {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000696 // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path
697 relativePath, err := filepath.Rel(ctx.ModuleDir(), path)
698 if err != nil {
699 panic(err)
700 }
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000701 relativePaths = append(relativePaths, relativePath)
702 }
703 return relativePaths
704}
705
Jingwen Chen882bcc12021-04-27 05:54:20 +0000706func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
Jingwen Chen91220d72021-03-24 02:18:33 -0400707 libraryDecorator := module.linker.(*libraryDecorator)
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400708 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
709}
Jingwen Chen91220d72021-03-24 02:18:33 -0400710
Rupert Shuttleworthffd45822021-05-14 03:02:34 -0400711func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
712 prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker)
713 libraryDecorator := prebuiltLibraryLinker.libraryDecorator
714 return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator)
715}
716
717// bp2BuildParseExportedIncludes creates a string list attribute contains the
718// exported included directories of a module.
719func bp2BuildParseExportedIncludesHelper(ctx android.TopDownMutatorContext, module *Module, libraryDecorator *libraryDecorator) bazel.StringListAttribute {
Jingwen Chened9c17d2021-04-13 07:14:55 +0000720 // Export_system_include_dirs and export_include_dirs are already module dir
721 // relative, so they don't need to be relativized like include_dirs, which
722 // are root-relative.
Jingwen Chen91220d72021-03-24 02:18:33 -0400723 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
724 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
Rupert Shuttleworthb8151682021-04-06 20:06:21 +0000725 includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs)
Jingwen Chen91220d72021-03-24 02:18:33 -0400726
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400727 getVariantIncludeDirs := func(includeDirs []string, flagExporterProperties *FlagExporterProperties) []string {
728 variantIncludeDirs := flagExporterProperties.Export_system_include_dirs
729 variantIncludeDirs = append(variantIncludeDirs, flagExporterProperties.Export_include_dirs...)
730
731 // To avoid duplicate includes when base includes + arch includes are combined
732 // TODO: This doesn't take conflicts between arch and os includes into account
733 variantIncludeDirs = bazel.SubtractStrings(variantIncludeDirs, includeDirs)
734 return variantIncludeDirs
735 }
736
Liz Kammer9abd62d2021-05-21 08:37:59 -0400737 for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) {
738 for config, props := range configToProps {
739 if flagExporterProperties, ok := props.(*FlagExporterProperties); ok {
740 archVariantIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties)
741 if len(archVariantIncludeDirs) > 0 {
742 includeDirsAttribute.SetSelectValue(axis, config, archVariantIncludeDirs)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400743 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400744 }
Rupert Shuttleworth375451e2021-04-26 07:49:08 -0400745 }
746 }
747
Jingwen Chen882bcc12021-04-27 05:54:20 +0000748 return includeDirsAttribute
Jingwen Chen91220d72021-03-24 02:18:33 -0400749}