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