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 ( |
Liz Kammer | d287118 | 2021-10-04 13:54:37 -0400 | [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 | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 23 | |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 24 | "github.com/google/blueprint" |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 25 | |
| 26 | "github.com/google/blueprint/proptools" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 27 | ) |
| 28 | |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 29 | const ( |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 30 | cSrcPartition = "c" |
| 31 | asSrcPartition = "as" |
| 32 | cppSrcPartition = "cpp" |
| 33 | protoSrcPartition = "proto" |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 34 | ) |
| 35 | |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 36 | // staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties -- |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 37 | // 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] | 38 | type staticOrSharedAttributes struct { |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 39 | Srcs bazel.LabelListAttribute |
| 40 | Srcs_c bazel.LabelListAttribute |
| 41 | Srcs_as bazel.LabelListAttribute |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 42 | Hdrs bazel.LabelListAttribute |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 43 | Copts bazel.StringListAttribute |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 44 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 45 | Deps bazel.LabelListAttribute |
| 46 | Implementation_deps bazel.LabelListAttribute |
| 47 | Dynamic_deps bazel.LabelListAttribute |
| 48 | Implementation_dynamic_deps bazel.LabelListAttribute |
| 49 | Whole_archive_deps bazel.LabelListAttribute |
| 50 | Implementation_whole_archive_deps bazel.LabelListAttribute |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 51 | |
| 52 | System_dynamic_deps bazel.LabelListAttribute |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 55 | func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute { |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 56 | // Check that a module is a filegroup type |
| 57 | isFilegroup := func(m blueprint.Module) bool { |
| 58 | return ctx.OtherModuleType(m) == "filegroup" |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 61 | // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl |
| 62 | // macro. |
| 63 | addSuffixForFilegroup := func(suffix string) bazel.LabelMapper { |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 64 | return func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) { |
| 65 | m, exists := ctx.ModuleFromName(label.OriginalModuleName) |
| 66 | labelStr := label.Label |
| 67 | if !exists || !isFilegroup(m) { |
| 68 | return labelStr, false |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 69 | } |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 70 | return labelStr + suffix, true |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 71 | } |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 74 | isProtoFilegroup := func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) { |
| 75 | m, exists := ctx.ModuleFromName(label.OriginalModuleName) |
| 76 | labelStr := label.Label |
| 77 | if !exists || !isFilegroup(m) { |
| 78 | return labelStr, false |
| 79 | } |
| 80 | likelyProtos := strings.HasSuffix(labelStr, "proto") || strings.HasSuffix(labelStr, "protos") |
| 81 | return labelStr, likelyProtos |
| 82 | } |
| 83 | |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 84 | // TODO(b/190006308): Handle language detection of sources in a Bazel rule. |
| 85 | partitioned := bazel.PartitionLabelListAttribute(ctx, &srcs, bazel.LabelPartitions{ |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 86 | cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")}, |
| 87 | asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")}, |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 88 | // C++ is the "catch-all" group, and comprises generated sources because we don't |
| 89 | // know the language of these sources until the genrule is executed. |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 90 | cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true}, |
| 91 | protoSrcPartition: bazel.LabelPartition{Extensions: []string{".proto"}, LabelMapper: isProtoFilegroup}, |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 92 | }) |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 93 | |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 94 | return partitioned |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 97 | // bp2BuildParseLibProps returns the attributes for a variant of a cc_library. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 98 | func bp2BuildParseLibProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) staticOrSharedAttributes { |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 99 | lib, ok := module.compiler.(*libraryDecorator) |
| 100 | if !ok { |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 101 | return staticOrSharedAttributes{} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 102 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 103 | return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic) |
| 104 | } |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 105 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 106 | // bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 107 | func bp2BuildParseSharedProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 108 | return bp2BuildParseLibProps(ctx, module, false) |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // bp2buildParseStaticProps returns the attributes for the static variant of a cc_library. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 112 | func bp2BuildParseStaticProps(ctx android.BazelConversionPathContext, module *Module) staticOrSharedAttributes { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 113 | return bp2BuildParseLibProps(ctx, module, true) |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 114 | } |
| 115 | |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 116 | type depsPartition struct { |
| 117 | export bazel.LabelList |
| 118 | implementation bazel.LabelList |
| 119 | } |
| 120 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 121 | type bazelLabelForDepsFn func(android.BazelConversionPathContext, []string) bazel.LabelList |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 122 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 123 | func maybePartitionExportedAndImplementationsDeps(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, exportedDeps []string, fn bazelLabelForDepsFn) depsPartition { |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 124 | if !exportsDeps { |
| 125 | return depsPartition{ |
| 126 | implementation: fn(ctx, allDeps), |
| 127 | } |
| 128 | } |
| 129 | |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 130 | implementation, export := android.FilterList(allDeps, exportedDeps) |
| 131 | |
| 132 | return depsPartition{ |
| 133 | export: fn(ctx, export), |
| 134 | implementation: fn(ctx, implementation), |
| 135 | } |
| 136 | } |
| 137 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 138 | type bazelLabelForDepsExcludesFn func(android.BazelConversionPathContext, []string, []string) bazel.LabelList |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 139 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 140 | func maybePartitionExportedAndImplementationsDepsExcludes(ctx android.BazelConversionPathContext, exportsDeps bool, allDeps, excludes, exportedDeps []string, fn bazelLabelForDepsExcludesFn) depsPartition { |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 141 | if !exportsDeps { |
| 142 | return depsPartition{ |
| 143 | implementation: fn(ctx, allDeps, excludes), |
| 144 | } |
| 145 | } |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 146 | implementation, export := android.FilterList(allDeps, exportedDeps) |
| 147 | |
| 148 | return depsPartition{ |
| 149 | export: fn(ctx, export, excludes), |
| 150 | implementation: fn(ctx, implementation, excludes), |
| 151 | } |
| 152 | } |
| 153 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 154 | func bp2buildParseStaticOrSharedProps(ctx android.BazelConversionPathContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes { |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 155 | attrs := staticOrSharedAttributes{} |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 156 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 157 | setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) { |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 158 | attrs.Copts.SetSelectValue(axis, config, props.Cflags) |
| 159 | attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs)) |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 160 | attrs.System_dynamic_deps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, props.System_shared_libs)) |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 161 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 162 | staticDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Static_libs, props.Export_static_lib_headers, bazelLabelForStaticDeps) |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 163 | attrs.Deps.SetSelectValue(axis, config, staticDeps.export) |
| 164 | attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation) |
| 165 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 166 | sharedDeps := maybePartitionExportedAndImplementationsDeps(ctx, true, props.Shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDeps) |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 167 | attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export) |
| 168 | attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation) |
| 169 | |
| 170 | attrs.Whole_archive_deps.SetSelectValue(axis, config, bazelLabelForWholeDeps(ctx, props.Whole_static_libs)) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 171 | } |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 172 | // system_dynamic_deps distinguishes between nil/empty list behavior: |
| 173 | // nil -> use default values |
| 174 | // empty list -> no values specified |
| 175 | attrs.System_dynamic_deps.ForceSpecifyEmptyList = true |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 176 | |
| 177 | if isStatic { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 178 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) { |
| 179 | for config, props := range configToProps { |
| 180 | if staticOrSharedProps, ok := props.(*StaticProperties); ok { |
| 181 | setAttrs(axis, config, staticOrSharedProps.Static) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | } |
| 185 | } else { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 186 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) { |
| 187 | for config, props := range configToProps { |
| 188 | if staticOrSharedProps, ok := props.(*SharedProperties); ok { |
| 189 | setAttrs(axis, config, staticOrSharedProps.Shared) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 195 | partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs) |
| 196 | attrs.Srcs = partitionedSrcs[cppSrcPartition] |
| 197 | attrs.Srcs_c = partitionedSrcs[cSrcPartition] |
| 198 | attrs.Srcs_as = partitionedSrcs[asSrcPartition] |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 199 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 200 | if !partitionedSrcs[protoSrcPartition].IsEmpty() { |
| 201 | // TODO(b/208815215): determine whether this is used and add support if necessary |
| 202 | ctx.ModuleErrorf("Migrating static/shared only proto srcs is not currently supported") |
| 203 | } |
| 204 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 205 | return attrs |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 208 | // Convenience struct to hold all attributes parsed from prebuilt properties. |
| 209 | type prebuiltAttributes struct { |
| 210 | Src bazel.LabelAttribute |
| 211 | } |
| 212 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 213 | // NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 214 | func Bp2BuildParsePrebuiltLibraryProps(ctx android.BazelConversionPathContext, module *Module) prebuiltAttributes { |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 215 | var srcLabelAttribute bazel.LabelAttribute |
| 216 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 217 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltLinkerProperties{}) { |
| 218 | for config, props := range configToProps { |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 219 | if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok { |
| 220 | if len(prebuiltLinkerProperties.Srcs) > 1 { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 221 | ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for %s %s\n", axis, config) |
| 222 | continue |
| 223 | } else if len(prebuiltLinkerProperties.Srcs) == 0 { |
| 224 | continue |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 225 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 226 | src := android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0]) |
| 227 | srcLabelAttribute.SetSelectValue(axis, config, src) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 232 | return prebuiltAttributes{ |
| 233 | Src: srcLabelAttribute, |
| 234 | } |
| 235 | } |
| 236 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 237 | type baseAttributes struct { |
| 238 | compilerAttributes |
| 239 | linkerAttributes |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 240 | |
| 241 | protoDependency *bazel.LabelAttribute |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 244 | // Convenience struct to hold all attributes parsed from compiler properties. |
| 245 | type compilerAttributes struct { |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 246 | // Options for all languages |
| 247 | copts bazel.StringListAttribute |
| 248 | // Assembly options and sources |
| 249 | asFlags bazel.StringListAttribute |
| 250 | asSrcs bazel.LabelListAttribute |
| 251 | // C options and sources |
| 252 | conlyFlags bazel.StringListAttribute |
| 253 | cSrcs bazel.LabelListAttribute |
| 254 | // C++ options and sources |
| 255 | cppFlags bazel.StringListAttribute |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 256 | srcs bazel.LabelListAttribute |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 257 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 258 | hdrs bazel.LabelListAttribute |
| 259 | |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 260 | rtti bazel.BoolAttribute |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 261 | |
| 262 | // Not affected by arch variants |
| 263 | stl *string |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 264 | cStd *string |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 265 | cppStd *string |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 266 | |
| 267 | localIncludes bazel.StringListAttribute |
| 268 | absoluteIncludes bazel.StringListAttribute |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 269 | |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame^] | 270 | includes BazelIncludes |
| 271 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 272 | protoSrcs bazel.LabelListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 275 | func parseCommandLineFlags(soongFlags []string) []string { |
| 276 | var result []string |
| 277 | for _, flag := range soongFlags { |
| 278 | // Soong's cflags can contain spaces, like `-include header.h`. For |
| 279 | // Bazel's copts, split them up to be compatible with the |
| 280 | // no_copts_tokenization feature. |
| 281 | result = append(result, strings.Split(flag, " ")...) |
| 282 | } |
| 283 | return result |
| 284 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 285 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 286 | func (ca *compilerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, props *BaseCompilerProperties) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 287 | // If there's arch specific srcs or exclude_srcs, generate a select entry for it. |
| 288 | // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too. |
| 289 | if srcsList, ok := parseSrcs(ctx, props); ok { |
| 290 | ca.srcs.SetSelectValue(axis, config, srcsList) |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 293 | localIncludeDirs := props.Local_include_dirs |
| 294 | if axis == bazel.NoConfigAxis { |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 295 | ca.cStd, ca.cppStd = bp2buildResolveCppStdValue(props.C_std, props.Cpp_std, props.Gnu_extensions) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 296 | if includeBuildDirectory(props.Include_build_directory) { |
| 297 | localIncludeDirs = append(localIncludeDirs, ".") |
Liz Kammer | 222bdcf | 2021-10-11 14:15:51 -0400 | [diff] [blame] | 298 | } |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 301 | ca.absoluteIncludes.SetSelectValue(axis, config, props.Include_dirs) |
| 302 | ca.localIncludes.SetSelectValue(axis, config, localIncludeDirs) |
| 303 | |
| 304 | ca.copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags)) |
| 305 | ca.asFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Asflags)) |
| 306 | ca.conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Conlyflags)) |
| 307 | ca.cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Cppflags)) |
| 308 | ca.rtti.SetSelectValue(axis, config, props.Rtti) |
| 309 | } |
| 310 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 311 | func (ca *compilerAttributes) convertStlProps(ctx android.ArchVariantContext, module *Module) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 312 | stlPropsByArch := module.GetArchVariantProperties(ctx, &StlProperties{}) |
| 313 | for _, configToProps := range stlPropsByArch { |
| 314 | for _, props := range configToProps { |
| 315 | if stlProps, ok := props.(*StlProperties); ok { |
| 316 | if stlProps.Stl == nil { |
| 317 | continue |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 318 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 319 | if ca.stl == nil { |
| 320 | ca.stl = stlProps.Stl |
| 321 | } else if ca.stl != stlProps.Stl { |
| 322 | ctx.ModuleErrorf("Unsupported conversion: module with different stl for different variants: %s and %s", *ca.stl, stlProps.Stl) |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 323 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 324 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 327 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 328 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 329 | func (ca *compilerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) { |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 330 | productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{ |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 331 | "Cflags": &ca.copts, |
| 332 | "Asflags": &ca.asFlags, |
| 333 | "CppFlags": &ca.cppFlags, |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 334 | } |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 335 | for propName, attr := range productVarPropNameToAttribute { |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 336 | if productConfigProps, exists := productVariableProps[propName]; exists { |
| 337 | for productConfigProp, prop := range productConfigProps { |
| 338 | flags, ok := prop.([]string) |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 339 | if !ok { |
| 340 | ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName)) |
| 341 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 342 | newFlags, _ := bazel.TryVariableSubstitutions(flags, productConfigProp.Name) |
| 343 | attr.SetSelectValue(productConfigProp.ConfigurationAxis(), productConfigProp.SelectKey(), newFlags) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 344 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 345 | } |
| 346 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 347 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 348 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 349 | func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, implementationHdrs bazel.LabelListAttribute) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 350 | ca.srcs.ResolveExcludes() |
| 351 | partitionedSrcs := groupSrcsByExtension(ctx, ca.srcs) |
| 352 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 353 | ca.protoSrcs = partitionedSrcs[protoSrcPartition] |
| 354 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 355 | for p, lla := range partitionedSrcs { |
| 356 | // if there are no sources, there is no need for headers |
| 357 | if lla.IsEmpty() { |
| 358 | continue |
| 359 | } |
| 360 | lla.Append(implementationHdrs) |
| 361 | partitionedSrcs[p] = lla |
| 362 | } |
| 363 | |
| 364 | ca.srcs = partitionedSrcs[cppSrcPartition] |
| 365 | ca.cSrcs = partitionedSrcs[cSrcPartition] |
| 366 | ca.asSrcs = partitionedSrcs[asSrcPartition] |
| 367 | |
| 368 | ca.absoluteIncludes.DeduplicateAxesFromBase() |
| 369 | ca.localIncludes.DeduplicateAxesFromBase() |
| 370 | } |
| 371 | |
| 372 | // Parse srcs from an arch or OS's props value. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 373 | func parseSrcs(ctx android.BazelConversionPathContext, props *BaseCompilerProperties) (bazel.LabelList, bool) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 374 | anySrcs := false |
| 375 | // Add srcs-like dependencies such as generated files. |
| 376 | // First create a LabelList containing these dependencies, then merge the values with srcs. |
| 377 | generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources) |
| 378 | if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 { |
| 379 | anySrcs = true |
| 380 | } |
| 381 | |
| 382 | allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs) |
| 383 | if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 { |
| 384 | anySrcs = true |
| 385 | } |
| 386 | return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs |
| 387 | } |
| 388 | |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 389 | func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) { |
| 390 | var cStdVal, cppStdVal string |
| 391 | // If c{,pp}std properties are not specified, don't generate them in the BUILD file. |
| 392 | // Defaults are handled by the toolchain definition. |
| 393 | // However, if gnu_extensions is false, then the default gnu-to-c version must be specified. |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 394 | if cpp_std != nil { |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 395 | cppStdVal = parseCppStd(cpp_std) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 396 | } else if gnu_extensions != nil && !*gnu_extensions { |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 397 | cppStdVal = "c++17" |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 398 | } |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 399 | if c_std != nil { |
| 400 | cStdVal = parseCStd(c_std) |
| 401 | } else if gnu_extensions != nil && !*gnu_extensions { |
| 402 | cStdVal = "c99" |
| 403 | } |
| 404 | |
| 405 | cStdVal, cppStdVal = maybeReplaceGnuToC(gnu_extensions, cStdVal, cppStdVal) |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 406 | var c_std_prop, cpp_std_prop *string |
| 407 | if cStdVal != "" { |
| 408 | c_std_prop = &cStdVal |
| 409 | } |
| 410 | if cppStdVal != "" { |
| 411 | cpp_std_prop = &cppStdVal |
| 412 | } |
| 413 | |
| 414 | return c_std_prop, cpp_std_prop |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 415 | } |
| 416 | |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame^] | 417 | // packageFromLabel extracts package from a fully-qualified or relative Label and whether the label |
| 418 | // is fully-qualified. |
| 419 | // e.g. fully-qualified "//a/b:foo" -> "a/b", true, relative: ":bar" -> ".", false |
| 420 | func packageFromLabel(label string) (string, bool) { |
| 421 | split := strings.Split(label, ":") |
| 422 | if len(split) != 2 { |
| 423 | return "", false |
| 424 | } |
| 425 | if split[0] == "" { |
| 426 | return ".", false |
| 427 | } |
| 428 | // remove leading "//" |
| 429 | return split[0][2:], true |
| 430 | } |
| 431 | |
| 432 | // includesFromLabelList extracts relative/absolute includes from a bazel.LabelList> |
| 433 | func includesFromLabelList(labelList bazel.LabelList) (relative, absolute []string) { |
| 434 | for _, hdr := range labelList.Includes { |
| 435 | if pkg, hasPkg := packageFromLabel(hdr.Label); hasPkg { |
| 436 | absolute = append(absolute, pkg) |
| 437 | } else if pkg != "" { |
| 438 | relative = append(relative, pkg) |
| 439 | } |
| 440 | } |
| 441 | return relative, absolute |
| 442 | } |
| 443 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 444 | // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 445 | func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) baseAttributes { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 446 | archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) |
| 447 | archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) |
| 448 | |
| 449 | var implementationHdrs bazel.LabelListAttribute |
| 450 | |
| 451 | axisToConfigs := map[bazel.ConfigurationAxis]map[string]bool{} |
| 452 | allAxesAndConfigs := func(cp android.ConfigurationAxisToArchVariantProperties) { |
| 453 | for axis, configMap := range cp { |
| 454 | if _, ok := axisToConfigs[axis]; !ok { |
| 455 | axisToConfigs[axis] = map[string]bool{} |
| 456 | } |
| 457 | for config, _ := range configMap { |
| 458 | axisToConfigs[axis][config] = true |
Chris Parsons | a967f25 | 2021-09-23 16:34:35 -0400 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 462 | allAxesAndConfigs(archVariantCompilerProps) |
| 463 | allAxesAndConfigs(archVariantLinkerProps) |
Chris Parsons | a967f25 | 2021-09-23 16:34:35 -0400 | [diff] [blame] | 464 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 465 | compilerAttrs := compilerAttributes{} |
| 466 | linkerAttrs := linkerAttributes{} |
| 467 | |
| 468 | for axis, configs := range axisToConfigs { |
| 469 | for config, _ := range configs { |
| 470 | var allHdrs []string |
| 471 | if baseCompilerProps, ok := archVariantCompilerProps[axis][config].(*BaseCompilerProperties); ok { |
| 472 | allHdrs = baseCompilerProps.Generated_headers |
| 473 | |
| 474 | (&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, config, baseCompilerProps) |
| 475 | } |
| 476 | |
| 477 | var exportHdrs []string |
| 478 | |
| 479 | if baseLinkerProps, ok := archVariantLinkerProps[axis][config].(*BaseLinkerProperties); ok { |
| 480 | exportHdrs = baseLinkerProps.Export_generated_headers |
| 481 | |
| 482 | (&linkerAttrs).bp2buildForAxisAndConfig(ctx, module.Binary(), axis, config, baseLinkerProps) |
| 483 | } |
| 484 | headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportHdrs, android.BazelLabelForModuleDeps) |
| 485 | implementationHdrs.SetSelectValue(axis, config, headers.implementation) |
| 486 | compilerAttrs.hdrs.SetSelectValue(axis, config, headers.export) |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame^] | 487 | |
| 488 | exportIncludes, exportAbsoluteIncludes := includesFromLabelList(headers.export) |
| 489 | compilerAttrs.includes.Includes.SetSelectValue(axis, config, exportIncludes) |
| 490 | compilerAttrs.includes.AbsoluteIncludes.SetSelectValue(axis, config, exportAbsoluteIncludes) |
| 491 | |
| 492 | includes, absoluteIncludes := includesFromLabelList(headers.implementation) |
| 493 | currAbsoluteIncludes := compilerAttrs.absoluteIncludes.SelectValue(axis, config) |
| 494 | currAbsoluteIncludes = android.FirstUniqueStrings(append(currAbsoluteIncludes, absoluteIncludes...)) |
| 495 | compilerAttrs.absoluteIncludes.SetSelectValue(axis, config, currAbsoluteIncludes) |
| 496 | currIncludes := compilerAttrs.localIncludes.SelectValue(axis, config) |
| 497 | currIncludes = android.FirstUniqueStrings(append(currIncludes, includes...)) |
| 498 | compilerAttrs.localIncludes.SetSelectValue(axis, config, currIncludes) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
| 502 | compilerAttrs.convertStlProps(ctx, module) |
| 503 | (&linkerAttrs).convertStripProps(ctx, module) |
| 504 | |
| 505 | productVariableProps := android.ProductVariableProperties(ctx) |
| 506 | |
| 507 | (&compilerAttrs).convertProductVariables(ctx, productVariableProps) |
| 508 | (&linkerAttrs).convertProductVariables(ctx, productVariableProps) |
| 509 | |
| 510 | (&compilerAttrs).finalize(ctx, implementationHdrs) |
| 511 | (&linkerAttrs).finalize() |
| 512 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 513 | protoDep := bp2buildProto(ctx, module, compilerAttrs.protoSrcs) |
| 514 | |
| 515 | // bp2buildProto will only set wholeStaticLib or implementationWholeStaticLib, but we don't know |
| 516 | // which. This will add the newly generated proto library to the appropriate attribute and nothing |
| 517 | // to the other |
| 518 | (&linkerAttrs).wholeArchiveDeps.Add(protoDep.wholeStaticLib) |
| 519 | (&linkerAttrs).implementationWholeArchiveDeps.Add(protoDep.implementationWholeStaticLib) |
| 520 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 521 | return baseAttributes{ |
| 522 | compilerAttrs, |
| 523 | linkerAttrs, |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 524 | protoDep.protoDep, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | |
| 528 | // Convenience struct to hold all attributes parsed from linker properties. |
| 529 | type linkerAttributes struct { |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 530 | deps bazel.LabelListAttribute |
| 531 | implementationDeps bazel.LabelListAttribute |
| 532 | dynamicDeps bazel.LabelListAttribute |
| 533 | implementationDynamicDeps bazel.LabelListAttribute |
| 534 | wholeArchiveDeps bazel.LabelListAttribute |
| 535 | implementationWholeArchiveDeps bazel.LabelListAttribute |
| 536 | systemDynamicDeps bazel.LabelListAttribute |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 537 | |
Jingwen Chen | 6ada589 | 2021-09-17 11:38:09 +0000 | [diff] [blame] | 538 | linkCrt bazel.BoolAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 539 | useLibcrt bazel.BoolAttribute |
Rupert Shuttleworth | 484aa25 | 2021-12-10 07:22:53 -0500 | [diff] [blame] | 540 | useVersionLib bazel.BoolAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 541 | linkopts bazel.StringListAttribute |
Liz Kammer | d287118 | 2021-10-04 13:54:37 -0400 | [diff] [blame] | 542 | additionalLinkerInputs bazel.LabelListAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 543 | stripKeepSymbols bazel.BoolAttribute |
| 544 | stripKeepSymbolsAndDebugFrame bazel.BoolAttribute |
| 545 | stripKeepSymbolsList bazel.StringListAttribute |
| 546 | stripAll bazel.BoolAttribute |
| 547 | stripNone bazel.BoolAttribute |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 548 | features bazel.StringListAttribute |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 549 | } |
| 550 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 551 | func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.BazelConversionPathContext, isBinary bool, axis bazel.ConfigurationAxis, config string, props *BaseLinkerProperties) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 552 | // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module |
| 553 | var axisFeatures []string |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 554 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 555 | // Excludes to parallel Soong: |
| 556 | // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0 |
| 557 | staticLibs := android.FirstUniqueStrings(props.Static_libs) |
| 558 | staticDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, staticLibs, props.Exclude_static_libs, props.Export_static_lib_headers, bazelLabelForStaticDepsExcludes) |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 559 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 560 | headerLibs := android.FirstUniqueStrings(props.Header_libs) |
| 561 | hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 562 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 563 | (&hDeps.export).Append(staticDeps.export) |
| 564 | la.deps.SetSelectValue(axis, config, hDeps.export) |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 565 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 566 | (&hDeps.implementation).Append(staticDeps.implementation) |
| 567 | la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation) |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 568 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 569 | wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs) |
| 570 | la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs)) |
| 571 | |
| 572 | systemSharedLibs := props.System_shared_libs |
| 573 | // systemSharedLibs distinguishes between nil/empty list behavior: |
| 574 | // nil -> use default values |
| 575 | // empty list -> no values specified |
| 576 | if len(systemSharedLibs) > 0 { |
| 577 | systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs) |
| 578 | } |
| 579 | la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs)) |
| 580 | |
| 581 | sharedLibs := android.FirstUniqueStrings(props.Shared_libs) |
| 582 | sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, sharedLibs, props.Exclude_shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes) |
| 583 | la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export) |
| 584 | la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation) |
| 585 | |
| 586 | if !BoolDefault(props.Pack_relocations, packRelocationsDefault) { |
| 587 | axisFeatures = append(axisFeatures, "disable_pack_relocations") |
| 588 | } |
| 589 | |
| 590 | if Bool(props.Allow_undefined_symbols) { |
| 591 | axisFeatures = append(axisFeatures, "-no_undefined_symbols") |
| 592 | } |
| 593 | |
| 594 | var linkerFlags []string |
| 595 | if len(props.Ldflags) > 0 { |
| 596 | linkerFlags = append(linkerFlags, props.Ldflags...) |
| 597 | // binaries remove static flag if -shared is in the linker flags |
| 598 | if isBinary && android.InList("-shared", linkerFlags) { |
| 599 | axisFeatures = append(axisFeatures, "-static_flag") |
| 600 | } |
| 601 | } |
| 602 | if props.Version_script != nil { |
| 603 | label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script) |
| 604 | la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}}) |
| 605 | linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label)) |
| 606 | } |
| 607 | la.linkopts.SetSelectValue(axis, config, linkerFlags) |
| 608 | la.useLibcrt.SetSelectValue(axis, config, props.libCrt()) |
| 609 | |
Rupert Shuttleworth | 484aa25 | 2021-12-10 07:22:53 -0500 | [diff] [blame] | 610 | if axis == bazel.NoConfigAxis { |
| 611 | la.useVersionLib.SetSelectValue(axis, config, props.Use_version_lib) |
| 612 | } |
| 613 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 614 | // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it. |
| 615 | if props.crt() != nil { |
| 616 | if axis == bazel.NoConfigAxis { |
| 617 | la.linkCrt.SetSelectValue(axis, config, props.crt()) |
| 618 | } else if axis == bazel.ArchConfigurationAxis { |
| 619 | ctx.ModuleErrorf("nocrt is not supported for arch variants") |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if axisFeatures != nil { |
| 624 | la.features.SetSelectValue(axis, config, axisFeatures) |
| 625 | } |
| 626 | } |
| 627 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 628 | func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) { |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 629 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &StripProperties{}) { |
| 630 | for config, props := range configToProps { |
| 631 | if stripProperties, ok := props.(*StripProperties); ok { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 632 | la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols) |
| 633 | la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list) |
| 634 | la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame) |
| 635 | la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All) |
| 636 | la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None) |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 640 | } |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 641 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 642 | func (la *linkerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) { |
Jingwen Chen | 6ada589 | 2021-09-17 11:38:09 +0000 | [diff] [blame] | 643 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 644 | type productVarDep struct { |
| 645 | // the name of the corresponding excludes field, if one exists |
| 646 | excludesField string |
| 647 | // reference to the bazel attribute that should be set for the given product variable config |
| 648 | attribute *bazel.LabelListAttribute |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 649 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 650 | depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | productVarToDepFields := map[string]productVarDep{ |
| 654 | // product variables do not support exclude_shared_libs |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 655 | "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes}, |
| 656 | "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes}, |
| 657 | "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes}, |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 658 | } |
| 659 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 660 | for name, dep := range productVarToDepFields { |
| 661 | props, exists := productVariableProps[name] |
| 662 | excludeProps, excludesExists := productVariableProps[dep.excludesField] |
| 663 | // if neither an include or excludes property exists, then skip it |
| 664 | if !exists && !excludesExists { |
| 665 | continue |
| 666 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 667 | // Collect all the configurations that an include or exclude property exists for. |
| 668 | // We want to iterate all configurations rather than either the include or exclude because, for a |
| 669 | // particular configuration, we may have either only an include or an exclude to handle. |
| 670 | productConfigProps := make(map[android.ProductConfigProperty]bool, len(props)+len(excludeProps)) |
| 671 | for p := range props { |
| 672 | productConfigProps[p] = true |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 673 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 674 | for p := range excludeProps { |
| 675 | productConfigProps[p] = true |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 676 | } |
| 677 | |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 678 | for productConfigProp := range productConfigProps { |
| 679 | prop, includesExists := props[productConfigProp] |
| 680 | excludesProp, excludesExists := excludeProps[productConfigProp] |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 681 | var includes, excludes []string |
| 682 | var ok bool |
| 683 | // if there was no includes/excludes property, casting fails and that's expected |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 684 | if includes, ok = prop.([]string); includesExists && !ok { |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 685 | ctx.ModuleErrorf("Could not convert product variable %s property", name) |
| 686 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 687 | if excludes, ok = excludesProp.([]string); excludesExists && !ok { |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 688 | ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField) |
| 689 | } |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 690 | |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 691 | dep.attribute.EmitEmptyList = productConfigProp.AlwaysEmit() |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 692 | dep.attribute.SetSelectValue( |
| 693 | productConfigProp.ConfigurationAxis(), |
| 694 | productConfigProp.SelectKey(), |
| 695 | dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes), |
| 696 | ) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 697 | } |
| 698 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 699 | } |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 700 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 701 | func (la *linkerAttributes) finalize() { |
| 702 | la.deps.ResolveExcludes() |
| 703 | la.implementationDeps.ResolveExcludes() |
| 704 | la.dynamicDeps.ResolveExcludes() |
| 705 | la.implementationDynamicDeps.ResolveExcludes() |
| 706 | la.wholeArchiveDeps.ResolveExcludes() |
| 707 | la.systemDynamicDeps.ForceSpecifyEmptyList = true |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 708 | } |
| 709 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 710 | // Relativize a list of root-relative paths with respect to the module's |
| 711 | // directory. |
| 712 | // |
| 713 | // include_dirs Soong prop are root-relative (b/183742505), but |
| 714 | // local_include_dirs, export_include_dirs and export_system_include_dirs are |
| 715 | // module dir relative. This function makes a list of paths entirely module dir |
| 716 | // relative. |
| 717 | // |
| 718 | // For the `include` attribute, Bazel wants the paths to be relative to the |
| 719 | // module. |
| 720 | func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 721 | var relativePaths []string |
| 722 | for _, path := range paths { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 723 | // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path |
| 724 | relativePath, err := filepath.Rel(ctx.ModuleDir(), path) |
| 725 | if err != nil { |
| 726 | panic(err) |
| 727 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 728 | relativePaths = append(relativePaths, relativePath) |
| 729 | } |
| 730 | return relativePaths |
| 731 | } |
| 732 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 733 | // BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel |
| 734 | // attributes. |
| 735 | type BazelIncludes struct { |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame^] | 736 | AbsoluteIncludes bazel.StringListAttribute |
| 737 | Includes bazel.StringListAttribute |
| 738 | SystemIncludes bazel.StringListAttribute |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 739 | } |
| 740 | |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame^] | 741 | func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module, existingIncludes BazelIncludes) BazelIncludes { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 742 | libraryDecorator := module.linker.(*libraryDecorator) |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame^] | 743 | return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator, &existingIncludes) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 744 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 745 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 746 | // Bp2buildParseExportedIncludesForPrebuiltLibrary returns a BazelIncludes with Bazel-ified values |
| 747 | // to export includes from the underlying module's properties. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 748 | func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.BazelConversionPathContext, module *Module) BazelIncludes { |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 749 | prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker) |
| 750 | libraryDecorator := prebuiltLibraryLinker.libraryDecorator |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame^] | 751 | return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator, nil) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | // bp2BuildParseExportedIncludes creates a string list attribute contains the |
| 755 | // exported included directories of a module. |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame^] | 756 | func bp2BuildParseExportedIncludesHelper(ctx android.BazelConversionPathContext, module *Module, libraryDecorator *libraryDecorator, includes *BazelIncludes) BazelIncludes { |
| 757 | var exported BazelIncludes |
| 758 | if includes != nil { |
| 759 | exported = *includes |
| 760 | } else { |
| 761 | exported = BazelIncludes{} |
| 762 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 763 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) { |
| 764 | for config, props := range configToProps { |
| 765 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 766 | if len(flagExporterProperties.Export_include_dirs) > 0 { |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame^] | 767 | exported.Includes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.Includes.SelectValue(axis, config), flagExporterProperties.Export_include_dirs...))) |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 768 | } |
| 769 | if len(flagExporterProperties.Export_system_include_dirs) > 0 { |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame^] | 770 | exported.SystemIncludes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.SystemIncludes.SelectValue(axis, config), flagExporterProperties.Export_system_include_dirs...))) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 771 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 772 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 773 | } |
| 774 | } |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame^] | 775 | exported.AbsoluteIncludes.DeduplicateAxesFromBase() |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 776 | exported.Includes.DeduplicateAxesFromBase() |
| 777 | exported.SystemIncludes.DeduplicateAxesFromBase() |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 778 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 779 | return exported |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 780 | } |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 781 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 782 | func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 783 | label := android.BazelModuleLabel(ctx, m) |
| 784 | if aModule, ok := m.(android.Module); ok { |
| 785 | if ctx.OtherModuleType(aModule) == "cc_library" && !android.GenerateCcLibraryStaticOnly(m.Name()) { |
| 786 | label += "_bp2build_cc_library_static" |
| 787 | } |
| 788 | } |
| 789 | return label |
| 790 | } |
| 791 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 792 | func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 793 | // cc_library, at it's root name, propagates the shared library, which depends on the static |
| 794 | // library. |
| 795 | return android.BazelModuleLabel(ctx, m) |
| 796 | } |
| 797 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 798 | func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 799 | label := bazelLabelForStaticModule(ctx, m) |
| 800 | if aModule, ok := m.(android.Module); ok { |
| 801 | if android.IsModulePrebuilt(aModule) { |
| 802 | label += "_alwayslink" |
| 803 | } |
| 804 | } |
| 805 | return label |
| 806 | } |
| 807 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 808 | func bazelLabelForWholeDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 809 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps) |
| 810 | } |
| 811 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 812 | func bazelLabelForWholeDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 813 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps) |
| 814 | } |
| 815 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 816 | func bazelLabelForStaticDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 817 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule) |
| 818 | } |
| 819 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 820 | func bazelLabelForStaticDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 821 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule) |
| 822 | } |
| 823 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 824 | func bazelLabelForSharedDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 825 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule) |
| 826 | } |
| 827 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 828 | func bazelLabelForHeaderDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 829 | // This is not elegant, but bp2build's shared library targets only propagate |
| 830 | // their header information as part of the normal C++ provider. |
| 831 | return bazelLabelForSharedDeps(ctx, modules) |
| 832 | } |
| 833 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 834 | func bazelLabelForSharedDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 835 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule) |
| 836 | } |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 837 | |
| 838 | type binaryLinkerAttrs struct { |
| 839 | Linkshared *bool |
| 840 | } |
| 841 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 842 | func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs { |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 843 | attrs := binaryLinkerAttrs{} |
| 844 | archVariantProps := m.GetArchVariantProperties(ctx, &BinaryLinkerProperties{}) |
| 845 | for axis, configToProps := range archVariantProps { |
| 846 | for _, p := range configToProps { |
| 847 | props := p.(*BinaryLinkerProperties) |
| 848 | staticExecutable := props.Static_executable |
| 849 | if axis == bazel.NoConfigAxis { |
| 850 | if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared { |
| 851 | attrs.Linkshared = &linkBinaryShared |
| 852 | } |
| 853 | } else if staticExecutable != nil { |
| 854 | // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a |
| 855 | // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling |
| 856 | ctx.ModuleErrorf("bp2build cannot migrate a module with arch/target-specific static_executable values") |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | return attrs |
| 862 | } |