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 ( |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 17 | "fmt" |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 18 | "path/filepath" |
Jingwen Chen | 3950cd6 | 2021-05-12 04:33:00 +0000 | [diff] [blame] | 19 | "strings" |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 20 | |
| 21 | "android/soong/android" |
| 22 | "android/soong/bazel" |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 23 | |
| 24 | "github.com/google/blueprint/proptools" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | // bp2build functions and helpers for converting cc_* modules to Bazel. |
| 28 | |
| 29 | func init() { |
| 30 | android.DepsBp2BuildMutators(RegisterDepsBp2Build) |
| 31 | } |
| 32 | |
| 33 | func 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? |
| 43 | func 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 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 56 | for _, osProps := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) { |
| 57 | // os base compiler props |
| 58 | if baseCompilerProps, ok := osProps.Properties.(*BaseCompilerProperties); ok { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 59 | allDeps = append(allDeps, baseCompilerProps.Generated_headers...) |
| 60 | allDeps = append(allDeps, baseCompilerProps.Generated_sources...) |
| 61 | } |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 62 | // os + arch base compiler props |
| 63 | for _, archProps := range osProps.ArchProperties { |
| 64 | if baseCompilerProps, ok := archProps.(*BaseCompilerProperties); ok { |
| 65 | allDeps = append(allDeps, baseCompilerProps.Generated_headers...) |
| 66 | allDeps = append(allDeps, baseCompilerProps.Generated_sources...) |
| 67 | } |
| 68 | } |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 71 | for _, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 72 | // arch specific compiler props |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 73 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 74 | allDeps = append(allDeps, baseCompilerProps.Generated_headers...) |
| 75 | allDeps = append(allDeps, baseCompilerProps.Generated_sources...) |
| 76 | } |
| 77 | } |
| 78 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 79 | for _, osProps := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) { |
| 80 | // os specific linker props |
| 81 | if baseLinkerProps, ok := osProps.Properties.(*BaseLinkerProperties); ok { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 82 | allDeps = append(allDeps, baseLinkerProps.Header_libs...) |
| 83 | allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 84 | allDeps = append(allDeps, baseLinkerProps.Static_libs...) |
| 85 | allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...) |
| 86 | } |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 87 | // os + arch base compiler props |
| 88 | for _, archProps := range osProps.ArchProperties { |
| 89 | if baseLinkerProps, ok := archProps.(*BaseLinkerProperties); ok { |
| 90 | allDeps = append(allDeps, baseLinkerProps.Header_libs...) |
| 91 | allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...) |
| 92 | allDeps = append(allDeps, baseLinkerProps.Static_libs...) |
| 93 | allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...) |
| 94 | } |
| 95 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 98 | for _, props := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 99 | // arch specific linker props |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 100 | if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 101 | allDeps = append(allDeps, baseLinkerProps.Header_libs...) |
| 102 | allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...) |
| 103 | allDeps = append(allDeps, baseLinkerProps.Static_libs...) |
| 104 | allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 108 | // Deps in the static: { .. } and shared: { .. } props of a cc_library. |
| 109 | if lib, ok := module.compiler.(*libraryDecorator); ok { |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 110 | appendDeps := func(deps []string, p StaticOrSharedProperties) []string { |
| 111 | deps = append(deps, p.Static_libs...) |
| 112 | deps = append(deps, p.Whole_static_libs...) |
| 113 | deps = append(deps, p.Shared_libs...) |
| 114 | return deps |
| 115 | } |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 116 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 117 | allDeps = appendDeps(allDeps, lib.SharedProperties.Shared) |
| 118 | allDeps = appendDeps(allDeps, lib.StaticProperties.Static) |
Jingwen Chen | 45dec10 | 2021-05-19 10:30:29 +0000 | [diff] [blame] | 119 | |
| 120 | // TODO(b/186024507, b/186489250): Temporarily exclude adding |
| 121 | // system_shared_libs deps until libc and libm builds. |
| 122 | // allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...) |
| 123 | // allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 124 | |
| 125 | // Deps in the target/arch nested static: { .. } and shared: { .. } props of a cc_library. |
| 126 | // target: { <target>: shared: { ... } } |
| 127 | for _, targetProps := range module.GetTargetProperties(ctx, &SharedProperties{}) { |
| 128 | if p, ok := targetProps.Properties.(*SharedProperties); ok { |
| 129 | allDeps = appendDeps(allDeps, p.Shared) |
| 130 | } |
| 131 | for _, archProperties := range targetProps.ArchProperties { |
| 132 | if p, ok := archProperties.(*SharedProperties); ok { |
| 133 | allDeps = appendDeps(allDeps, p.Shared) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | // target: { <target>: static: { ... } } |
| 138 | for _, targetProps := range module.GetTargetProperties(ctx, &StaticProperties{}) { |
| 139 | if p, ok := targetProps.Properties.(*StaticProperties); ok { |
| 140 | allDeps = appendDeps(allDeps, p.Static) |
| 141 | } |
| 142 | for _, archProperties := range targetProps.ArchProperties { |
| 143 | if p, ok := archProperties.(*StaticProperties); ok { |
| 144 | allDeps = appendDeps(allDeps, p.Static) |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | // arch: { <arch>: shared: { ... } } |
| 149 | for _, properties := range module.GetArchProperties(ctx, &SharedProperties{}) { |
| 150 | if p, ok := properties.(*SharedProperties); ok { |
| 151 | allDeps = appendDeps(allDeps, p.Shared) |
| 152 | } |
| 153 | } |
| 154 | // arch: { <arch>: static: { ... } } |
| 155 | for _, properties := range module.GetArchProperties(ctx, &StaticProperties{}) { |
| 156 | if p, ok := properties.(*StaticProperties); ok { |
| 157 | allDeps = appendDeps(allDeps, p.Static) |
| 158 | } |
| 159 | } |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 162 | ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...) |
| 163 | } |
| 164 | |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 165 | // staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties -- |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 166 | // properties which apply to either the shared or static version of a cc_library module. |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 167 | type staticOrSharedAttributes struct { |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 168 | srcs bazel.LabelListAttribute |
| 169 | srcs_c bazel.LabelListAttribute |
| 170 | srcs_as bazel.LabelListAttribute |
| 171 | |
| 172 | copts bazel.StringListAttribute |
| 173 | |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 174 | staticDeps bazel.LabelListAttribute |
| 175 | dynamicDeps bazel.LabelListAttribute |
| 176 | wholeArchiveDeps bazel.LabelListAttribute |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 179 | func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) (cppSrcs, cSrcs, asSrcs bazel.LabelListAttribute) { |
| 180 | // Branch srcs into three language-specific groups. |
| 181 | // C++ is the "catch-all" group, and comprises generated sources because we don't |
| 182 | // know the language of these sources until the genrule is executed. |
| 183 | // TODO(b/190006308): Handle language detection of sources in a Bazel rule. |
| 184 | isCSrcOrFilegroup := func(s string) bool { |
| 185 | return strings.HasSuffix(s, ".c") || strings.HasSuffix(s, "_c_srcs") |
| 186 | } |
| 187 | |
| 188 | isAsmSrcOrFilegroup := func(s string) bool { |
| 189 | return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s") || strings.HasSuffix(s, "_as_srcs") |
| 190 | } |
| 191 | |
| 192 | // Check that a module is a filegroup type named <label>. |
| 193 | isFilegroupNamed := func(m android.Module, fullLabel string) bool { |
| 194 | if ctx.OtherModuleType(m) != "filegroup" { |
| 195 | return false |
| 196 | } |
| 197 | labelParts := strings.Split(fullLabel, ":") |
| 198 | if len(labelParts) > 2 { |
| 199 | // There should not be more than one colon in a label. |
| 200 | panic(fmt.Errorf("%s is not a valid Bazel label for a filegroup", fullLabel)) |
| 201 | } else { |
| 202 | return m.Name() == labelParts[len(labelParts)-1] |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Convert the filegroup dependencies into the extension-specific filegroups |
| 207 | // filtered in the filegroup.bzl macro. |
| 208 | cppFilegroup := func(label string) string { |
| 209 | ctx.VisitDirectDeps(func(m android.Module) { |
| 210 | if isFilegroupNamed(m, label) { |
| 211 | label = label + "_cpp_srcs" |
| 212 | return |
| 213 | } |
| 214 | }) |
| 215 | return label |
| 216 | } |
| 217 | cFilegroup := func(label string) string { |
| 218 | ctx.VisitDirectDeps(func(m android.Module) { |
| 219 | if isFilegroupNamed(m, label) { |
| 220 | label = label + "_c_srcs" |
| 221 | return |
| 222 | } |
| 223 | }) |
| 224 | return label |
| 225 | } |
| 226 | asFilegroup := func(label string) string { |
| 227 | ctx.VisitDirectDeps(func(m android.Module) { |
| 228 | if isFilegroupNamed(m, label) { |
| 229 | label = label + "_as_srcs" |
| 230 | return |
| 231 | } |
| 232 | }) |
| 233 | return label |
| 234 | } |
| 235 | |
| 236 | cSrcs = bazel.MapLabelListAttribute(srcs, cFilegroup) |
| 237 | cSrcs = bazel.FilterLabelListAttribute(cSrcs, isCSrcOrFilegroup) |
| 238 | |
| 239 | asSrcs = bazel.MapLabelListAttribute(srcs, asFilegroup) |
| 240 | asSrcs = bazel.FilterLabelListAttribute(asSrcs, isAsmSrcOrFilegroup) |
| 241 | |
| 242 | cppSrcs = bazel.MapLabelListAttribute(srcs, cppFilegroup) |
| 243 | cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, cSrcs) |
| 244 | cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, asSrcs) |
| 245 | return |
| 246 | } |
| 247 | |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 248 | // bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library. |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 249 | func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes { |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 250 | lib, ok := module.compiler.(*libraryDecorator) |
| 251 | if !ok { |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 252 | return staticOrSharedAttributes{} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 255 | return bp2buildParseStaticOrSharedProps(ctx, module, lib, false) |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | // bp2buildParseStaticProps returns the attributes for the static variant of a cc_library. |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 259 | func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes { |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 260 | lib, ok := module.compiler.(*libraryDecorator) |
| 261 | if !ok { |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 262 | return staticOrSharedAttributes{} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 265 | return bp2buildParseStaticOrSharedProps(ctx, module, lib, true) |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 266 | } |
| 267 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 268 | func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes { |
| 269 | var props StaticOrSharedProperties |
| 270 | if isStatic { |
| 271 | props = lib.StaticProperties.Static |
| 272 | } else { |
| 273 | props = lib.SharedProperties.Shared |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 274 | } |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 275 | |
| 276 | attrs := staticOrSharedAttributes{ |
| 277 | copts: bazel.StringListAttribute{Value: props.Cflags}, |
| 278 | srcs: bazel.LabelListAttribute{Value: android.BazelLabelForModuleSrc(ctx, props.Srcs)}, |
| 279 | staticDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Static_libs)}, |
| 280 | dynamicDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Shared_libs)}, |
| 281 | wholeArchiveDeps: bazel.LabelListAttribute{Value: android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)}, |
| 282 | } |
| 283 | |
| 284 | setArchAttrs := func(arch string, props StaticOrSharedProperties) { |
| 285 | attrs.copts.SetValueForArch(arch, props.Cflags) |
| 286 | attrs.srcs.SetValueForArch(arch, android.BazelLabelForModuleSrc(ctx, props.Srcs)) |
| 287 | attrs.staticDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Static_libs)) |
| 288 | attrs.dynamicDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Shared_libs)) |
| 289 | attrs.wholeArchiveDeps.SetValueForArch(arch, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)) |
| 290 | } |
| 291 | |
| 292 | setTargetAttrs := func(target string, props StaticOrSharedProperties) { |
| 293 | attrs.copts.SetOsValueForTarget(target, props.Cflags) |
| 294 | attrs.srcs.SetOsValueForTarget(target, android.BazelLabelForModuleSrc(ctx, props.Srcs)) |
| 295 | attrs.staticDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Static_libs)) |
| 296 | attrs.dynamicDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Shared_libs)) |
| 297 | attrs.wholeArchiveDeps.SetOsValueForTarget(target, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)) |
| 298 | } |
| 299 | |
| 300 | setTargetArchAttrs := func(target, arch string, props StaticOrSharedProperties) { |
| 301 | attrs.copts.SetOsArchValueForTarget(target, arch, props.Cflags) |
| 302 | attrs.srcs.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleSrc(ctx, props.Srcs)) |
| 303 | attrs.staticDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Static_libs)) |
| 304 | attrs.dynamicDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Shared_libs)) |
| 305 | attrs.wholeArchiveDeps.SetOsArchValueForTarget(target, arch, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)) |
| 306 | } |
| 307 | |
| 308 | if isStatic { |
| 309 | for arch, properties := range module.GetArchProperties(ctx, &StaticProperties{}) { |
| 310 | if staticOrSharedProps, ok := properties.(*StaticProperties); ok { |
| 311 | setArchAttrs(arch.Name, staticOrSharedProps.Static) |
| 312 | } |
| 313 | } |
| 314 | for target, p := range module.GetTargetProperties(ctx, &StaticProperties{}) { |
| 315 | if staticOrSharedProps, ok := p.Properties.(*StaticProperties); ok { |
| 316 | setTargetAttrs(target.Name, staticOrSharedProps.Static) |
| 317 | } |
| 318 | for arch, archProperties := range p.ArchProperties { |
| 319 | if staticOrSharedProps, ok := archProperties.(*StaticProperties); ok { |
| 320 | setTargetArchAttrs(target.Name, arch.Name, staticOrSharedProps.Static) |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } else { |
| 325 | for arch, p := range module.GetArchProperties(ctx, &SharedProperties{}) { |
| 326 | if staticOrSharedProps, ok := p.(*SharedProperties); ok { |
| 327 | setArchAttrs(arch.Name, staticOrSharedProps.Shared) |
| 328 | } |
| 329 | } |
| 330 | for target, p := range module.GetTargetProperties(ctx, &SharedProperties{}) { |
| 331 | if staticOrSharedProps, ok := p.Properties.(*SharedProperties); ok { |
| 332 | setTargetAttrs(target.Name, staticOrSharedProps.Shared) |
| 333 | } |
| 334 | for arch, archProperties := range p.ArchProperties { |
| 335 | if staticOrSharedProps, ok := archProperties.(*SharedProperties); ok { |
| 336 | setTargetArchAttrs(target.Name, arch.Name, staticOrSharedProps.Shared) |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 342 | cppSrcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, attrs.srcs) |
| 343 | attrs.srcs = cppSrcs |
| 344 | attrs.srcs_c = cSrcs |
| 345 | attrs.srcs_as = asSrcs |
| 346 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 347 | return attrs |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 350 | // Convenience struct to hold all attributes parsed from prebuilt properties. |
| 351 | type prebuiltAttributes struct { |
| 352 | Src bazel.LabelAttribute |
| 353 | } |
| 354 | |
| 355 | func Bp2BuildParsePrebuiltLibraryProps(ctx android.TopDownMutatorContext, module *Module) prebuiltAttributes { |
| 356 | prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker) |
| 357 | prebuiltLinker := prebuiltLibraryLinker.prebuiltLinker |
| 358 | |
| 359 | var srcLabelAttribute bazel.LabelAttribute |
| 360 | |
| 361 | if len(prebuiltLinker.properties.Srcs) > 1 { |
| 362 | ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file\n") |
| 363 | } |
| 364 | |
| 365 | if len(prebuiltLinker.properties.Srcs) == 1 { |
| 366 | srcLabelAttribute.Value = android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinker.properties.Srcs[0]) |
| 367 | for arch, props := range module.GetArchProperties(ctx, &prebuiltLinkerProperties{}) { |
| 368 | if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok { |
| 369 | if len(prebuiltLinkerProperties.Srcs) > 1 { |
| 370 | ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for arch %s\n", arch.Name) |
| 371 | } |
| 372 | if len(prebuiltLinkerProperties.Srcs) == 1 { |
| 373 | srcLabelAttribute.SetValueForArch(arch.Name, android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0])) |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | for os, targetProperties := range module.GetTargetProperties(ctx, &prebuiltLinkerProperties{}) { |
| 380 | if prebuiltLinkerProperties, ok := targetProperties.Properties.(*prebuiltLinkerProperties); ok { |
| 381 | if len(prebuiltLinkerProperties.Srcs) > 1 { |
| 382 | ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for os %s\n", os.Name) |
| 383 | |
| 384 | } |
| 385 | |
| 386 | if len(prebuiltLinkerProperties.Srcs) == 1 { |
| 387 | srcLabelAttribute.SetOsValueForTarget(os.Name, android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0])) |
| 388 | } |
| 389 | } |
| 390 | for arch, archProperties := range targetProperties.ArchProperties { |
| 391 | if prebuiltLinkerProperties, ok := archProperties.(*prebuiltLinkerProperties); ok { |
| 392 | if len(prebuiltLinkerProperties.Srcs) > 1 { |
| 393 | ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for os_arch %s_%s\n", os.Name, arch.Name) |
| 394 | |
| 395 | } |
| 396 | |
| 397 | if len(prebuiltLinkerProperties.Srcs) == 1 { |
| 398 | srcLabelAttribute.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0])) |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | return prebuiltAttributes{ |
| 406 | Src: srcLabelAttribute, |
| 407 | } |
| 408 | } |
| 409 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 410 | // Convenience struct to hold all attributes parsed from compiler properties. |
| 411 | type compilerAttributes struct { |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 412 | // Options for all languages |
| 413 | copts bazel.StringListAttribute |
| 414 | // Assembly options and sources |
| 415 | asFlags bazel.StringListAttribute |
| 416 | asSrcs bazel.LabelListAttribute |
| 417 | // C options and sources |
| 418 | conlyFlags bazel.StringListAttribute |
| 419 | cSrcs bazel.LabelListAttribute |
| 420 | // C++ options and sources |
| 421 | cppFlags bazel.StringListAttribute |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 422 | srcs bazel.LabelListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 425 | // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 426 | func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes { |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 427 | var srcs bazel.LabelListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 428 | var copts bazel.StringListAttribute |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 429 | var asFlags bazel.StringListAttribute |
| 430 | var conlyFlags bazel.StringListAttribute |
| 431 | var cppFlags bazel.StringListAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 432 | |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 433 | // Creates the -I flags for a directory, while making the directory relative |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 434 | // to the exec root for Bazel to work. |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 435 | includeFlags := func(dir string) []string { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 436 | // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements. |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 437 | moduleDirRootedPath := filepath.Join(ctx.ModuleDir(), dir) |
| 438 | return []string{ |
| 439 | "-I" + moduleDirRootedPath, |
| 440 | // Include the bindir-rooted path (using make variable substitution). This most |
| 441 | // closely matches Bazel's native include path handling, which allows for dependency |
| 442 | // on generated headers in these directories. |
| 443 | // TODO(b/188084383): Handle local include directories in Bazel. |
| 444 | "-I$(BINDIR)/" + moduleDirRootedPath, |
| 445 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 448 | // Parse the list of module-relative include directories (-I). |
| 449 | parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string { |
| 450 | // include_dirs are root-relative, not module-relative. |
| 451 | includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs) |
| 452 | return append(includeDirs, baseCompilerProps.Local_include_dirs...) |
| 453 | } |
| 454 | |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 455 | parseCommandLineFlags := func(soongFlags []string) []string { |
| 456 | var result []string |
| 457 | for _, flag := range soongFlags { |
Colin Cross | 52aa4e1 | 2021-05-25 15:20:39 +0000 | [diff] [blame] | 458 | // Soong's cflags can contain spaces, like `-include header.h`. For |
| 459 | // Bazel's copts, split them up to be compatible with the |
| 460 | // no_copts_tokenization feature. |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 461 | result = append(result, strings.Split(flag, " ")...) |
Colin Cross | 52aa4e1 | 2021-05-25 15:20:39 +0000 | [diff] [blame] | 462 | } |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 463 | return result |
| 464 | } |
| 465 | |
| 466 | // Parse the list of copts. |
| 467 | parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string { |
| 468 | var copts []string |
| 469 | copts = append(copts, parseCommandLineFlags(baseCompilerProps.Cflags)...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 470 | for _, dir := range parseLocalIncludeDirs(baseCompilerProps) { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 471 | copts = append(copts, includeFlags(dir)...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 472 | } |
| 473 | return copts |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 474 | } |
| 475 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 476 | // baseSrcs contain the list of src files that are used for every configuration. |
| 477 | var baseSrcs []string |
| 478 | // baseExcludeSrcs contain the list of src files that are excluded for every configuration. |
| 479 | var baseExcludeSrcs []string |
| 480 | // baseSrcsLabelList is a clone of the base srcs LabelList, used for computing the |
| 481 | // arch or os specific srcs later. |
| 482 | var baseSrcsLabelList bazel.LabelList |
| 483 | |
| 484 | // Parse srcs from an arch or OS's props value, taking the base srcs and |
| 485 | // exclude srcs into account. |
| 486 | parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList { |
| 487 | // Combine the base srcs and arch-specific srcs |
| 488 | allSrcs := append(baseSrcs, baseCompilerProps.Srcs...) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 489 | // Add srcs-like dependencies such as generated files. |
| 490 | // First create a LabelList containing these dependencies, then merge the values with srcs. |
| 491 | generatedHdrsAndSrcs := baseCompilerProps.Generated_headers |
| 492 | generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...) |
| 493 | |
| 494 | generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs) |
| 495 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 496 | // Combine the base exclude_srcs and configuration-specific exclude_srcs |
| 497 | allExcludeSrcs := append(baseExcludeSrcs, baseCompilerProps.Exclude_srcs...) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 498 | allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, allSrcs, allExcludeSrcs) |
| 499 | return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 502 | for _, props := range module.compiler.compilerProps() { |
| 503 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 504 | srcs.Value = parseSrcs(baseCompilerProps) |
| 505 | copts.Value = parseCopts(baseCompilerProps) |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 506 | asFlags.Value = parseCommandLineFlags(baseCompilerProps.Asflags) |
| 507 | conlyFlags.Value = parseCommandLineFlags(baseCompilerProps.Conlyflags) |
| 508 | cppFlags.Value = parseCommandLineFlags(baseCompilerProps.Cppflags) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 509 | |
| 510 | // Used for arch-specific srcs later. |
| 511 | baseSrcs = baseCompilerProps.Srcs |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 512 | baseSrcsLabelList = parseSrcs(baseCompilerProps) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 513 | baseExcludeSrcs = baseCompilerProps.Exclude_srcs |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 514 | break |
| 515 | } |
| 516 | } |
| 517 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 518 | // Handle include_build_directory prop. If the property is true, then the |
| 519 | // target has access to all headers recursively in the package, and has |
| 520 | // "-I<module-dir>" in its copts. |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 521 | if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 522 | copts.Value = append(copts.Value, includeFlags(".")...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 523 | } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 524 | copts.Value = append(copts.Value, includeFlags(".")...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 527 | for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 528 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 529 | // If there's arch specific srcs or exclude_srcs, generate a select entry for it. |
| 530 | // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too. |
| 531 | if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 { |
| 532 | srcsList := parseSrcs(baseCompilerProps) |
| 533 | srcs.SetValueForArch(arch.Name, srcsList) |
| 534 | // The base srcs value should not contain any arch-specific excludes. |
| 535 | srcs.Value = bazel.SubtractBazelLabelList(srcs.Value, bazel.LabelList{Includes: srcsList.Excludes}) |
| 536 | } |
| 537 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 538 | copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps)) |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 539 | asFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Asflags)) |
| 540 | conlyFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags)) |
| 541 | cppFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Cppflags)) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 545 | // After going through all archs, delete the duplicate files in the arch |
| 546 | // values that are already in the base srcs.Value. |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 547 | for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 548 | if _, ok := props.(*BaseCompilerProperties); ok { |
| 549 | srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcs.GetValueForArch(arch.Name), srcs.Value)) |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | // Now that the srcs.Value list is finalized, compare it with the original |
| 554 | // list, and put the difference into the default condition for the arch |
| 555 | // select. |
| 556 | defaultsSrcs := bazel.SubtractBazelLabelList(baseSrcsLabelList, srcs.Value) |
| 557 | // TODO(b/186153868): handle the case with multiple variant types, e.g. when arch and os are both used. |
| 558 | srcs.SetValueForArch(bazel.CONDITIONS_DEFAULT, defaultsSrcs) |
| 559 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 560 | // Handle target specific properties. |
| 561 | for os, osProps := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) { |
| 562 | if baseCompilerProps, ok := osProps.Properties.(*BaseCompilerProperties); ok { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 563 | srcsList := parseSrcs(baseCompilerProps) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 564 | // TODO(b/186153868): add support for os-specific srcs and exclude_srcs |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 565 | if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 { |
| 566 | srcs.SetOsValueForTarget(os.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList)) |
| 567 | } |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 568 | copts.SetOsValueForTarget(os.Name, parseCopts(baseCompilerProps)) |
| 569 | asFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Asflags)) |
| 570 | conlyFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags)) |
| 571 | cppFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Cppflags)) |
| 572 | } |
| 573 | for arch, archProps := range osProps.ArchProperties { |
| 574 | if baseCompilerProps, ok := archProps.(*BaseCompilerProperties); ok { |
| 575 | srcsList := parseSrcs(baseCompilerProps) |
| 576 | // TODO(b/186153868): add support for os-specific srcs and exclude_srcs |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 577 | if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 { |
| 578 | srcs.SetOsArchValueForTarget(os.Name, arch.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList)) |
| 579 | } |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 580 | copts.SetOsArchValueForTarget(os.Name, arch.Name, parseCopts(baseCompilerProps)) |
| 581 | asFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Asflags)) |
| 582 | conlyFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags)) |
| 583 | cppFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Cppflags)) |
| 584 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 588 | productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{ |
| 589 | "Cflags": &copts, |
| 590 | "Asflags": &asFlags, |
| 591 | "CppFlags": &cppFlags, |
| 592 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 593 | productVariableProps := android.ProductVariableProperties(ctx) |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 594 | for propName, attr := range productVarPropNameToAttribute { |
| 595 | if props, exists := productVariableProps[propName]; exists { |
| 596 | for _, prop := range props { |
| 597 | flags, ok := prop.Property.([]string) |
| 598 | if !ok { |
| 599 | ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName)) |
| 600 | } |
| 601 | newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable) |
| 602 | attr.ProductValues = append(attr.ProductValues, bazel.ProductVariableValues{ |
| 603 | ProductVariable: prop.ProductConfigVariable, |
| 604 | Values: newFlags, |
| 605 | }) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 606 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 610 | srcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, srcs) |
| 611 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 612 | return compilerAttributes{ |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 613 | copts: copts, |
| 614 | srcs: srcs, |
| 615 | asFlags: asFlags, |
| 616 | asSrcs: asSrcs, |
| 617 | cSrcs: cSrcs, |
| 618 | conlyFlags: conlyFlags, |
| 619 | cppFlags: cppFlags, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
| 623 | // Convenience struct to hold all attributes parsed from linker properties. |
| 624 | type linkerAttributes struct { |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 625 | deps bazel.LabelListAttribute |
| 626 | dynamicDeps bazel.LabelListAttribute |
| 627 | wholeArchiveDeps bazel.LabelListAttribute |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 628 | exportedDeps bazel.LabelListAttribute |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 629 | linkopts bazel.StringListAttribute |
| 630 | versionScript bazel.LabelAttribute |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 633 | // FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here |
| 634 | func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string { |
| 635 | flags := linkerProperties.Ldflags |
| 636 | if !BoolDefault(linkerProperties.Pack_relocations, true) { |
| 637 | flags = append(flags, "-Wl,--pack-dyn-relocs=none") |
| 638 | } |
| 639 | return flags |
| 640 | } |
| 641 | |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 642 | // bp2BuildParseLinkerProps parses the linker properties of a module, including |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 643 | // configurable attribute values. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 644 | func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes { |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 645 | var deps bazel.LabelListAttribute |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 646 | var exportedDeps bazel.LabelListAttribute |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 647 | var dynamicDeps bazel.LabelListAttribute |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 648 | var wholeArchiveDeps bazel.LabelListAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 649 | var linkopts bazel.StringListAttribute |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 650 | var versionScript bazel.LabelAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 651 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 652 | getLibs := func(baseLinkerProps *BaseLinkerProperties) []string { |
| 653 | libs := baseLinkerProps.Header_libs |
| 654 | libs = append(libs, baseLinkerProps.Static_libs...) |
| 655 | libs = android.SortedUniqueStrings(libs) |
| 656 | return libs |
| 657 | } |
| 658 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 659 | for _, linkerProps := range module.linker.linkerProps() { |
| 660 | if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok { |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 661 | libs := getLibs(baseLinkerProps) |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 662 | exportedLibs := baseLinkerProps.Export_header_lib_headers |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 663 | wholeArchiveLibs := baseLinkerProps.Whole_static_libs |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 664 | deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs)) |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 665 | exportedDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, exportedLibs)) |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 666 | linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps) |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 667 | wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs)) |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 668 | |
| 669 | if baseLinkerProps.Version_script != nil { |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 670 | versionScript.Value = android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script) |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 671 | } |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 672 | |
| 673 | sharedLibs := baseLinkerProps.Shared_libs |
| 674 | dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, sharedLibs)) |
| 675 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 676 | break |
| 677 | } |
| 678 | } |
| 679 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 680 | for arch, props := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) { |
| 681 | if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok { |
| 682 | libs := getLibs(baseLinkerProps) |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 683 | exportedLibs := baseLinkerProps.Export_header_lib_headers |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 684 | wholeArchiveLibs := baseLinkerProps.Whole_static_libs |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 685 | deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs)) |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 686 | exportedDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs)) |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 687 | linkopts.SetValueForArch(arch.Name, getBp2BuildLinkerFlags(baseLinkerProps)) |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 688 | wholeArchiveDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs)) |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 689 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 690 | if baseLinkerProps.Version_script != nil { |
| 691 | versionScript.SetValueForArch(arch.Name, |
| 692 | android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)) |
| 693 | } |
Rupert Shuttleworth | 3b413d3 | 2021-05-10 18:41:51 -0400 | [diff] [blame] | 694 | |
| 695 | sharedLibs := baseLinkerProps.Shared_libs |
| 696 | dynamicDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs)) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 700 | for os, targetProperties := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) { |
| 701 | if baseLinkerProps, ok := targetProperties.Properties.(*BaseLinkerProperties); ok { |
| 702 | libs := getLibs(baseLinkerProps) |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 703 | exportedLibs := baseLinkerProps.Export_header_lib_headers |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 704 | wholeArchiveLibs := baseLinkerProps.Whole_static_libs |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 705 | wholeArchiveDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs)) |
| 706 | deps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, libs)) |
| 707 | exportedDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs)) |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 708 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 709 | linkopts.SetOsValueForTarget(os.Name, getBp2BuildLinkerFlags(baseLinkerProps)) |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 710 | |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 711 | if baseLinkerProps.Version_script != nil { |
| 712 | versionScript.SetOsValueForTarget(os.Name, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)) |
| 713 | } |
| 714 | |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 715 | sharedLibs := baseLinkerProps.Shared_libs |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 716 | dynamicDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs)) |
| 717 | } |
| 718 | for arch, archProperties := range targetProperties.ArchProperties { |
| 719 | if baseLinkerProps, ok := archProperties.(*BaseLinkerProperties); ok { |
| 720 | libs := getLibs(baseLinkerProps) |
| 721 | exportedLibs := baseLinkerProps.Export_header_lib_headers |
| 722 | wholeArchiveLibs := baseLinkerProps.Whole_static_libs |
| 723 | wholeArchiveDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs)) |
| 724 | deps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, libs)) |
| 725 | exportedDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs)) |
| 726 | |
| 727 | linkopts.SetOsArchValueForTarget(os.Name, arch.Name, getBp2BuildLinkerFlags(baseLinkerProps)) |
| 728 | |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 729 | if baseLinkerProps.Version_script != nil { |
| 730 | versionScript.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)) |
| 731 | } |
| 732 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 733 | sharedLibs := baseLinkerProps.Shared_libs |
| 734 | dynamicDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs)) |
| 735 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 739 | return linkerAttributes{ |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 740 | deps: deps, |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 741 | exportedDeps: exportedDeps, |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 742 | dynamicDeps: dynamicDeps, |
| 743 | wholeArchiveDeps: wholeArchiveDeps, |
| 744 | linkopts: linkopts, |
| 745 | versionScript: versionScript, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 746 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 747 | } |
| 748 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 749 | // Relativize a list of root-relative paths with respect to the module's |
| 750 | // directory. |
| 751 | // |
| 752 | // include_dirs Soong prop are root-relative (b/183742505), but |
| 753 | // local_include_dirs, export_include_dirs and export_system_include_dirs are |
| 754 | // module dir relative. This function makes a list of paths entirely module dir |
| 755 | // relative. |
| 756 | // |
| 757 | // For the `include` attribute, Bazel wants the paths to be relative to the |
| 758 | // module. |
| 759 | func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 760 | var relativePaths []string |
| 761 | for _, path := range paths { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 762 | // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path |
| 763 | relativePath, err := filepath.Rel(ctx.ModuleDir(), path) |
| 764 | if err != nil { |
| 765 | panic(err) |
| 766 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 767 | relativePaths = append(relativePaths, relativePath) |
| 768 | } |
| 769 | return relativePaths |
| 770 | } |
| 771 | |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 772 | func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 773 | libraryDecorator := module.linker.(*libraryDecorator) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 774 | return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator) |
| 775 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 776 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 777 | func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute { |
| 778 | prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker) |
| 779 | libraryDecorator := prebuiltLibraryLinker.libraryDecorator |
| 780 | return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator) |
| 781 | } |
| 782 | |
| 783 | // bp2BuildParseExportedIncludes creates a string list attribute contains the |
| 784 | // exported included directories of a module. |
| 785 | func bp2BuildParseExportedIncludesHelper(ctx android.TopDownMutatorContext, module *Module, libraryDecorator *libraryDecorator) bazel.StringListAttribute { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 786 | // Export_system_include_dirs and export_include_dirs are already module dir |
| 787 | // relative, so they don't need to be relativized like include_dirs, which |
| 788 | // are root-relative. |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 789 | includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs |
| 790 | includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...) |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 791 | includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 792 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 793 | getVariantIncludeDirs := func(includeDirs []string, flagExporterProperties *FlagExporterProperties) []string { |
| 794 | variantIncludeDirs := flagExporterProperties.Export_system_include_dirs |
| 795 | variantIncludeDirs = append(variantIncludeDirs, flagExporterProperties.Export_include_dirs...) |
| 796 | |
| 797 | // To avoid duplicate includes when base includes + arch includes are combined |
| 798 | // TODO: This doesn't take conflicts between arch and os includes into account |
| 799 | variantIncludeDirs = bazel.SubtractStrings(variantIncludeDirs, includeDirs) |
| 800 | return variantIncludeDirs |
| 801 | } |
| 802 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 803 | for arch, props := range module.GetArchProperties(ctx, &FlagExporterProperties{}) { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 804 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 805 | archIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties) |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 806 | if len(archIncludeDirs) > 0 { |
| 807 | includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs) |
| 808 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 809 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 810 | } |
| 811 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 812 | for os, targetProperties := range module.GetTargetProperties(ctx, &FlagExporterProperties{}) { |
| 813 | if flagExporterProperties, ok := targetProperties.Properties.(*FlagExporterProperties); ok { |
| 814 | targetIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties) |
| 815 | if len(targetIncludeDirs) > 0 { |
| 816 | includeDirsAttribute.SetOsValueForTarget(os.Name, targetIncludeDirs) |
| 817 | } |
| 818 | } |
| 819 | for arch, archProperties := range targetProperties.ArchProperties { |
| 820 | if flagExporterProperties, ok := archProperties.(*FlagExporterProperties); ok { |
| 821 | targetIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties) |
| 822 | if len(targetIncludeDirs) > 0 { |
| 823 | includeDirsAttribute.SetOsArchValueForTarget(os.Name, arch.Name, targetIncludeDirs) |
| 824 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 825 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 829 | return includeDirsAttribute |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 830 | } |