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