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