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