blob: 2a590eb8400c527ce4e2ea7373e7fffcf417c792 [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"
19)
20
21// bp2build functions and helpers for converting cc_* modules to Bazel.
22
23func init() {
24 android.DepsBp2BuildMutators(RegisterDepsBp2Build)
25}
26
27func RegisterDepsBp2Build(ctx android.RegisterMutatorsContext) {
28 ctx.BottomUp("cc_bp2build_deps", depsBp2BuildMutator)
29}
30
31// A naive deps mutator to add deps on all modules across all combinations of
32// target props for cc modules. This is needed to make module -> bazel label
33// resolution work in the bp2build mutator later. This is probably
34// the wrong way to do it, but it works.
35//
36// TODO(jingwen): can we create a custom os mutator in depsBp2BuildMutator to do this?
37func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) {
38 module, ok := ctx.Module().(*Module)
39 if !ok {
40 // Not a cc module
41 return
42 }
43
44 if !module.ConvertWithBp2build(ctx) {
45 return
46 }
47
48 var allDeps []string
49
50 for _, p := range module.GetTargetProperties(&BaseLinkerProperties{}) {
51 // arch specific linker props
52 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
53 allDeps = append(allDeps, baseLinkerProps.Header_libs...)
54 allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...)
55 }
56 }
57
58 ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...)
59}
60
61// bp2BuildParseHeaderLibs creates a label list attribute containing the header library deps of a module, including
62// configurable attribute values.
63func bp2BuildParseHeaderLibs(ctx android.TopDownMutatorContext, module *Module) bazel.LabelListAttribute {
64 var ret bazel.LabelListAttribute
65 for _, linkerProps := range module.linker.linkerProps() {
66 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
67 libs := baseLinkerProps.Header_libs
68 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
69 ret = bazel.MakeLabelListAttribute(
70 android.BazelLabelForModuleDeps(ctx, android.SortedUniqueStrings(libs)))
71 break
72 }
73 }
74
75 for os, p := range module.GetTargetProperties(&BaseLinkerProperties{}) {
76 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
77 libs := baseLinkerProps.Header_libs
78 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
79 libs = android.SortedUniqueStrings(libs)
80 ret.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
81 }
82 }
83
84 return ret
85}
86
87// bp2BuildParseExportedIncludes creates a label list attribute contains the
88// exported included directories of a module.
89func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) (bazel.LabelListAttribute, bazel.LabelListAttribute) {
90 libraryDecorator := module.linker.(*libraryDecorator)
91
92 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
93 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
94
95 includeDirsLabels := android.BazelLabelForModuleSrc(ctx, includeDirs)
96
97 var includeDirGlobs []string
98 for _, includeDir := range includeDirs {
99 includeDirGlobs = append(includeDirGlobs, includeDir+"/**/*.h")
100 includeDirGlobs = append(includeDirGlobs, includeDir+"/**/*.inc")
101 includeDirGlobs = append(includeDirGlobs, includeDir+"/**/*.hpp")
102 }
103
104 headersLabels := android.BazelLabelForModuleSrc(ctx, includeDirGlobs)
105 return bazel.MakeLabelListAttribute(includeDirsLabels), bazel.MakeLabelListAttribute(headersLabels)
106}