blob: 497d227d34a1f0b5cf4d98dee712dd9e708a1c84 [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
Jingwen Chenc1c26502021-04-05 10:35:13 +000061// bp2buildParseCflags creates a label list attribute containing the cflags of a module, including
62func bp2BuildParseCflags(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute {
63 var ret bazel.StringListAttribute
64 for _, props := range module.compiler.compilerProps() {
65 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
66 ret.Value = baseCompilerProps.Cflags
67 break
68 }
69 }
70
71 for arch, props := range module.GetArchProperties(&BaseCompilerProperties{}) {
72 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
73 ret.SetValueForArch(arch.Name, baseCompilerProps.Cflags)
74 }
75 }
76
77 for os, props := range module.GetTargetProperties(&BaseCompilerProperties{}) {
78 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
79 ret.SetValueForOS(os.Name, baseCompilerProps.Cflags)
80 }
81 }
82
83 return ret
84}
85
Jingwen Chen91220d72021-03-24 02:18:33 -040086// bp2BuildParseHeaderLibs creates a label list attribute containing the header library deps of a module, including
87// configurable attribute values.
88func bp2BuildParseHeaderLibs(ctx android.TopDownMutatorContext, module *Module) bazel.LabelListAttribute {
89 var ret bazel.LabelListAttribute
90 for _, linkerProps := range module.linker.linkerProps() {
91 if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok {
92 libs := baseLinkerProps.Header_libs
93 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
94 ret = bazel.MakeLabelListAttribute(
95 android.BazelLabelForModuleDeps(ctx, android.SortedUniqueStrings(libs)))
96 break
97 }
98 }
99
100 for os, p := range module.GetTargetProperties(&BaseLinkerProperties{}) {
101 if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok {
102 libs := baseLinkerProps.Header_libs
103 libs = append(libs, baseLinkerProps.Export_header_lib_headers...)
104 libs = android.SortedUniqueStrings(libs)
105 ret.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs))
106 }
107 }
108
109 return ret
110}
111
112// bp2BuildParseExportedIncludes creates a label list attribute contains the
113// exported included directories of a module.
114func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) (bazel.LabelListAttribute, bazel.LabelListAttribute) {
115 libraryDecorator := module.linker.(*libraryDecorator)
116
117 includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs
118 includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...)
119
120 includeDirsLabels := android.BazelLabelForModuleSrc(ctx, includeDirs)
121
122 var includeDirGlobs []string
123 for _, includeDir := range includeDirs {
124 includeDirGlobs = append(includeDirGlobs, includeDir+"/**/*.h")
125 includeDirGlobs = append(includeDirGlobs, includeDir+"/**/*.inc")
126 includeDirGlobs = append(includeDirGlobs, includeDir+"/**/*.hpp")
127 }
128
129 headersLabels := android.BazelLabelForModuleSrc(ctx, includeDirGlobs)
130 return bazel.MakeLabelListAttribute(includeDirsLabels), bazel.MakeLabelListAttribute(headersLabels)
131}