Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 1 | // 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. |
| 14 | package cc |
| 15 | |
| 16 | import ( |
| 17 | "android/soong/android" |
| 18 | "android/soong/bazel" |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 19 | "strings" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | // bp2build functions and helpers for converting cc_* modules to Bazel. |
| 23 | |
| 24 | func init() { |
| 25 | android.DepsBp2BuildMutators(RegisterDepsBp2Build) |
| 26 | } |
| 27 | |
| 28 | func 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? |
| 38 | func 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 Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame^] | 62 | // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. |
| 63 | func 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 Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 77 | for _, props := range module.compiler.compilerProps() { |
| 78 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame^] | 79 | hdrs.Value, srcs.Value = hdrsAndSrcs(baseCompilerProps) |
| 80 | copts.Value = baseCompilerProps.Cflags |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 81 | break |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | for arch, props := range module.GetArchProperties(&BaseCompilerProperties{}) { |
| 86 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame^] | 87 | 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 Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | for os, props := range module.GetTargetProperties(&BaseCompilerProperties{}) { |
| 95 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame^] | 96 | 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 Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame^] | 103 | return copts, srcs, hdrs |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame^] | 106 | // bp2BuildParseLinkerProps creates a label list attribute containing the header library deps of a module, including |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 107 | // configurable attribute values. |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame^] | 108 | func bp2BuildParseLinkerProps( |
| 109 | ctx android.TopDownMutatorContext, module *Module) (bazel.LabelListAttribute, bazel.StringListAttribute) { |
| 110 | |
| 111 | var deps bazel.LabelListAttribute |
| 112 | var linkopts bazel.StringListAttribute |
| 113 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 114 | 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 Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame^] | 118 | deps = bazel.MakeLabelListAttribute( |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 119 | android.BazelLabelForModuleDeps(ctx, android.SortedUniqueStrings(libs))) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame^] | 120 | linkopts.Value = baseLinkerProps.Ldflags |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 121 | break |
| 122 | } |
| 123 | } |
| 124 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame^] | 125 | 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 Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 135 | 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 Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame^] | 140 | deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs)) |
| 141 | linkopts.SetValueForOS(os.Name, baseLinkerProps.Ldflags) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame^] | 145 | return deps, linkopts |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 146 | } |
| 147 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 148 | func bp2BuildListHeadersInDir(ctx android.TopDownMutatorContext, includeDir string) bazel.LabelList { |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame^] | 149 | globs := bazel.GlobsInDir(includeDir, includeDir != ".", headerExts) |
| 150 | return android.BazelLabelForModuleSrc(ctx, globs) |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Bazel wants include paths to be relative to the module |
| 154 | func 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 Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 163 | // bp2BuildParseExportedIncludes creates a label list attribute contains the |
| 164 | // exported included directories of a module. |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 165 | func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) (bazel.StringListAttribute, bazel.LabelListAttribute) { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 166 | 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 Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 170 | includeDirs = bp2BuildMakePathsRelativeToModule(ctx, includeDirs) |
| 171 | includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 172 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 173 | var headersAttribute bazel.LabelListAttribute |
| 174 | var headers bazel.LabelList |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 175 | for _, includeDir := range includeDirs { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 176 | 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 Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 209 | return includeDirsAttribute, headersAttribute |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 210 | } |