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 | // |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 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" |
Alix | 1be00d4 | 2022-05-16 22:56:04 +0000 | [diff] [blame] | 23 | "android/soong/cc/config" |
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" |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 33 | asmSrcPartition = "asm" |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 34 | lSrcPartition = "l" |
| 35 | llSrcPartition = "ll" |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 36 | cppSrcPartition = "cpp" |
| 37 | protoSrcPartition = "proto" |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 38 | aidlSrcPartition = "aidl" |
Liz Kammer | 91487d4 | 2022-09-13 11:27:11 -0400 | [diff] [blame] | 39 | |
| 40 | stubsSuffix = "_stub_libs_current" |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 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 { |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 46 | Srcs bazel.LabelListAttribute |
| 47 | Srcs_c bazel.LabelListAttribute |
| 48 | Srcs_as bazel.LabelListAttribute |
| 49 | Srcs_aidl bazel.LabelListAttribute |
| 50 | Hdrs bazel.LabelListAttribute |
| 51 | Copts bazel.StringListAttribute |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 52 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 53 | Deps bazel.LabelListAttribute |
| 54 | Implementation_deps bazel.LabelListAttribute |
| 55 | Dynamic_deps bazel.LabelListAttribute |
| 56 | Implementation_dynamic_deps bazel.LabelListAttribute |
| 57 | Whole_archive_deps bazel.LabelListAttribute |
| 58 | Implementation_whole_archive_deps bazel.LabelListAttribute |
Cole Faust | 6b29f59 | 2022-08-09 09:50:56 -0700 | [diff] [blame] | 59 | Runtime_deps bazel.LabelListAttribute |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 60 | |
| 61 | System_dynamic_deps bazel.LabelListAttribute |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 62 | |
| 63 | Enabled bazel.BoolAttribute |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 64 | |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 65 | Native_coverage bazel.BoolAttribute |
| 66 | |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 67 | sdkAttributes |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 70 | // groupSrcsByExtension partitions `srcs` into groups based on file extension. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 71 | func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute { |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 72 | // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl |
| 73 | // macro. |
| 74 | addSuffixForFilegroup := func(suffix string) bazel.LabelMapper { |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 75 | return func(otherModuleCtx bazel.OtherModuleContext, label bazel.Label) (string, bool) { |
| 76 | |
| 77 | m, exists := otherModuleCtx.ModuleFromName(label.OriginalModuleName) |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 78 | labelStr := label.Label |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 79 | if !exists || !android.IsFilegroup(otherModuleCtx, m) { |
| 80 | return labelStr, false |
| 81 | } |
| 82 | // If the filegroup is already converted to aidl_library, skip creating |
| 83 | // _c_srcs, _as_srcs, _cpp_srcs filegroups |
| 84 | fg, _ := m.(android.Bp2buildAidlLibrary) |
| 85 | if fg.ShouldConvertToAidlLibrary(ctx) { |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 86 | return labelStr, false |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 87 | } |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 88 | return labelStr + suffix, true |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 89 | } |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 92 | // TODO(b/190006308): Handle language detection of sources in a Bazel rule. |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 93 | labels := bazel.LabelPartitions{ |
| 94 | protoSrcPartition: android.ProtoSrcLabelPartition, |
Liz Kammer | aabfb5d | 2021-12-08 15:25:06 -0500 | [diff] [blame] | 95 | cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")}, |
| 96 | asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")}, |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 97 | asmSrcPartition: bazel.LabelPartition{Extensions: []string{".asm"}}, |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 98 | aidlSrcPartition: android.AidlSrcLabelPartition, |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 99 | // TODO(http://b/231968910): If there is ever a filegroup target that |
| 100 | // contains .l or .ll files we will need to find a way to add a |
| 101 | // LabelMapper for these that identifies these filegroups and |
| 102 | // converts them appropriately |
| 103 | lSrcPartition: bazel.LabelPartition{Extensions: []string{".l"}}, |
| 104 | llSrcPartition: bazel.LabelPartition{Extensions: []string{".ll"}}, |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 105 | // C++ is the "catch-all" group, and comprises generated sources because we don't |
| 106 | // know the language of these sources until the genrule is executed. |
Liz Kammer | aabfb5d | 2021-12-08 15:25:06 -0500 | [diff] [blame] | 107 | 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] | 108 | } |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 109 | |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 110 | return bazel.PartitionLabelListAttribute(ctx, &srcs, labels) |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 113 | // bp2BuildParseLibProps returns the attributes for a variant of a cc_library. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 114 | func bp2BuildParseLibProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) staticOrSharedAttributes { |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 115 | lib, ok := module.compiler.(*libraryDecorator) |
| 116 | if !ok { |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 117 | return staticOrSharedAttributes{} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 118 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 119 | return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic) |
| 120 | } |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 121 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 122 | // bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 123 | 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] | 124 | return bp2BuildParseLibProps(ctx, module, false) |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // bp2buildParseStaticProps returns the attributes for the static variant of a cc_library. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 128 | 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] | 129 | return bp2BuildParseLibProps(ctx, module, true) |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 132 | type depsPartition struct { |
| 133 | export bazel.LabelList |
| 134 | implementation bazel.LabelList |
| 135 | } |
| 136 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 137 | type bazelLabelForDepsFn func(android.BazelConversionPathContext, []string) bazel.LabelList |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 138 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 139 | 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] | 140 | if !exportsDeps { |
| 141 | return depsPartition{ |
| 142 | implementation: fn(ctx, allDeps), |
| 143 | } |
| 144 | } |
| 145 | |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 146 | implementation, export := android.FilterList(allDeps, exportedDeps) |
| 147 | |
| 148 | return depsPartition{ |
| 149 | export: fn(ctx, export), |
| 150 | implementation: fn(ctx, implementation), |
| 151 | } |
| 152 | } |
| 153 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 154 | type bazelLabelForDepsExcludesFn func(android.BazelConversionPathContext, []string, []string) bazel.LabelList |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 155 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 156 | 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] | 157 | if !exportsDeps { |
| 158 | return depsPartition{ |
| 159 | implementation: fn(ctx, allDeps, excludes), |
| 160 | } |
| 161 | } |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 162 | implementation, export := android.FilterList(allDeps, exportedDeps) |
| 163 | |
| 164 | return depsPartition{ |
| 165 | export: fn(ctx, export, excludes), |
| 166 | implementation: fn(ctx, implementation, excludes), |
| 167 | } |
| 168 | } |
| 169 | |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 170 | // Parses properties common to static and shared libraries. Also used for prebuilt libraries. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 171 | func bp2buildParseStaticOrSharedProps(ctx android.BazelConversionPathContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes { |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 172 | attrs := staticOrSharedAttributes{} |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 173 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 174 | setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) { |
Alix | 1be00d4 | 2022-05-16 22:56:04 +0000 | [diff] [blame] | 175 | attrs.Copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, true, filterOutStdFlag)) |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 176 | attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs)) |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 177 | 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] | 178 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 179 | 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] | 180 | attrs.Deps.SetSelectValue(axis, config, staticDeps.export) |
| 181 | attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation) |
| 182 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 183 | 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] | 184 | attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export) |
| 185 | attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation) |
| 186 | |
| 187 | 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] | 188 | attrs.Enabled.SetSelectValue(axis, config, props.Enabled) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 189 | } |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 190 | // system_dynamic_deps distinguishes between nil/empty list behavior: |
| 191 | // nil -> use default values |
| 192 | // empty list -> no values specified |
| 193 | attrs.System_dynamic_deps.ForceSpecifyEmptyList = true |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 194 | |
| 195 | if isStatic { |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 196 | bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 197 | if staticOrSharedProps, ok := props.(*StaticProperties); ok { |
| 198 | setAttrs(axis, config, staticOrSharedProps.Static) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 199 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 200 | }) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 201 | } else { |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 202 | bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 203 | if staticOrSharedProps, ok := props.(*SharedProperties); ok { |
| 204 | setAttrs(axis, config, staticOrSharedProps.Shared) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 205 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 206 | }) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 209 | partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs) |
| 210 | attrs.Srcs = partitionedSrcs[cppSrcPartition] |
| 211 | attrs.Srcs_c = partitionedSrcs[cSrcPartition] |
| 212 | attrs.Srcs_as = partitionedSrcs[asSrcPartition] |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 213 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 214 | if !partitionedSrcs[protoSrcPartition].IsEmpty() { |
| 215 | // TODO(b/208815215): determine whether this is used and add support if necessary |
| 216 | ctx.ModuleErrorf("Migrating static/shared only proto srcs is not currently supported") |
| 217 | } |
| 218 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 219 | return attrs |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 222 | // Convenience struct to hold all attributes parsed from prebuilt properties. |
| 223 | type prebuiltAttributes struct { |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 224 | Src bazel.LabelAttribute |
| 225 | Enabled bazel.BoolAttribute |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 228 | // NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 229 | func Bp2BuildParsePrebuiltLibraryProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) prebuiltAttributes { |
| 230 | manySourceFileError := func(axis bazel.ConfigurationAxis, config string) { |
| 231 | ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most one source file for %s %s\n", axis, config) |
| 232 | } |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 233 | var srcLabelAttribute bazel.LabelAttribute |
| 234 | |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 235 | parseSrcs := func(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, srcs []string) { |
| 236 | if len(srcs) > 1 { |
| 237 | manySourceFileError(axis, config) |
| 238 | return |
| 239 | } else if len(srcs) == 0 { |
| 240 | return |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 241 | } |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 242 | if srcLabelAttribute.SelectValue(axis, config) != nil { |
| 243 | manySourceFileError(axis, config) |
| 244 | return |
| 245 | } |
| 246 | |
| 247 | src := android.BazelLabelForModuleSrcSingle(ctx, srcs[0]) |
| 248 | srcLabelAttribute.SetSelectValue(axis, config, src) |
| 249 | } |
| 250 | |
| 251 | bp2BuildPropParseHelper(ctx, module, &prebuiltLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 252 | if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok { |
| 253 | parseSrcs(ctx, axis, config, prebuiltLinkerProperties.Srcs) |
| 254 | } |
| 255 | }) |
| 256 | |
| 257 | var enabledLabelAttribute bazel.BoolAttribute |
| 258 | parseAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) { |
| 259 | if props.Enabled != nil { |
| 260 | enabledLabelAttribute.SetSelectValue(axis, config, props.Enabled) |
| 261 | } |
| 262 | parseSrcs(ctx, axis, config, props.Srcs) |
| 263 | } |
| 264 | |
| 265 | if isStatic { |
| 266 | bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 267 | if staticProperties, ok := props.(*StaticProperties); ok { |
| 268 | parseAttrs(axis, config, staticProperties.Static) |
| 269 | } |
| 270 | }) |
| 271 | } else { |
| 272 | bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 273 | if sharedProperties, ok := props.(*SharedProperties); ok { |
| 274 | parseAttrs(axis, config, sharedProperties.Shared) |
| 275 | } |
| 276 | }) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 277 | } |
| 278 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 279 | return prebuiltAttributes{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 280 | Src: srcLabelAttribute, |
| 281 | Enabled: enabledLabelAttribute, |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | func bp2BuildPropParseHelper(ctx android.ArchVariantContext, module *Module, propsType interface{}, parseFunc func(axis bazel.ConfigurationAxis, config string, props interface{})) { |
| 286 | for axis, configToProps := range module.GetArchVariantProperties(ctx, propsType) { |
| 287 | for config, props := range configToProps { |
| 288 | parseFunc(axis, config, props) |
| 289 | } |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 293 | type baseAttributes struct { |
| 294 | compilerAttributes |
| 295 | linkerAttributes |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 296 | |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 297 | // A combination of compilerAttributes.features and linkerAttributes.features |
| 298 | features bazel.StringListAttribute |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 299 | protoDependency *bazel.LabelAttribute |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 300 | aidlDependency *bazel.LabelAttribute |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 301 | } |
| 302 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 303 | // Convenience struct to hold all attributes parsed from compiler properties. |
| 304 | type compilerAttributes struct { |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 305 | // Options for all languages |
| 306 | copts bazel.StringListAttribute |
| 307 | // Assembly options and sources |
| 308 | asFlags bazel.StringListAttribute |
| 309 | asSrcs bazel.LabelListAttribute |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 310 | asmSrcs bazel.LabelListAttribute |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 311 | // C options and sources |
| 312 | conlyFlags bazel.StringListAttribute |
| 313 | cSrcs bazel.LabelListAttribute |
| 314 | // C++ options and sources |
| 315 | cppFlags bazel.StringListAttribute |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 316 | srcs bazel.LabelListAttribute |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 317 | |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 318 | // Lex sources and options |
| 319 | lSrcs bazel.LabelListAttribute |
| 320 | llSrcs bazel.LabelListAttribute |
| 321 | lexopts bazel.StringListAttribute |
| 322 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 323 | hdrs bazel.LabelListAttribute |
| 324 | |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 325 | rtti bazel.BoolAttribute |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 326 | |
| 327 | // Not affected by arch variants |
| 328 | stl *string |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 329 | cStd *string |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 330 | cppStd *string |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 331 | |
| 332 | localIncludes bazel.StringListAttribute |
| 333 | absoluteIncludes bazel.StringListAttribute |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 334 | |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 335 | includes BazelIncludes |
| 336 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 337 | protoSrcs bazel.LabelListAttribute |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 338 | aidlSrcs bazel.LabelListAttribute |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 339 | |
| 340 | stubsSymbolFile *string |
| 341 | stubsVersions bazel.StringListAttribute |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 342 | |
| 343 | features bazel.StringListAttribute |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a56e970 | 2022-02-23 18:39:59 -0500 | [diff] [blame] | 344 | |
| 345 | suffix bazel.StringAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Liz Kammer | cac7f69 | 2021-12-16 14:19:32 -0500 | [diff] [blame] | 348 | type filterOutFn func(string) bool |
| 349 | |
| 350 | func filterOutStdFlag(flag string) bool { |
| 351 | return strings.HasPrefix(flag, "-std=") |
| 352 | } |
| 353 | |
Alix | 1be00d4 | 2022-05-16 22:56:04 +0000 | [diff] [blame] | 354 | func filterOutClangUnknownCflags(flag string) bool { |
| 355 | for _, f := range config.ClangUnknownCflags { |
| 356 | if f == flag { |
| 357 | return true |
| 358 | } |
| 359 | } |
| 360 | return false |
| 361 | } |
| 362 | |
| 363 | func parseCommandLineFlags(soongFlags []string, noCoptsTokenization bool, filterOut ...filterOutFn) []string { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 364 | var result []string |
| 365 | for _, flag := range soongFlags { |
Alix | 1be00d4 | 2022-05-16 22:56:04 +0000 | [diff] [blame] | 366 | skipFlag := false |
| 367 | for _, filter := range filterOut { |
| 368 | if filter != nil && filter(flag) { |
| 369 | skipFlag = true |
| 370 | } |
| 371 | } |
| 372 | if skipFlag { |
Liz Kammer | cac7f69 | 2021-12-16 14:19:32 -0500 | [diff] [blame] | 373 | continue |
| 374 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 375 | // Soong's cflags can contain spaces, like `-include header.h`. For |
| 376 | // Bazel's copts, split them up to be compatible with the |
| 377 | // no_copts_tokenization feature. |
Alix | 1be00d4 | 2022-05-16 22:56:04 +0000 | [diff] [blame] | 378 | if noCoptsTokenization { |
| 379 | result = append(result, strings.Split(flag, " ")...) |
| 380 | } else { |
| 381 | // Soong's Version Script and Dynamic List Properties are added as flags |
| 382 | // to Bazel's linkopts using "($location label)" syntax. |
| 383 | // Splitting on spaces would separate this into two different flags |
| 384 | // "($ location" and "label)" |
| 385 | result = append(result, flag) |
| 386 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 387 | } |
| 388 | return result |
| 389 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 390 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 391 | 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] | 392 | // If there's arch specific srcs or exclude_srcs, generate a select entry for it. |
| 393 | // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too. |
| 394 | if srcsList, ok := parseSrcs(ctx, props); ok { |
| 395 | ca.srcs.SetSelectValue(axis, config, srcsList) |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 396 | } |
| 397 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 398 | localIncludeDirs := props.Local_include_dirs |
| 399 | if axis == bazel.NoConfigAxis { |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 400 | 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] | 401 | if includeBuildDirectory(props.Include_build_directory) { |
| 402 | localIncludeDirs = append(localIncludeDirs, ".") |
Liz Kammer | 222bdcf | 2021-10-11 14:15:51 -0400 | [diff] [blame] | 403 | } |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 406 | ca.absoluteIncludes.SetSelectValue(axis, config, props.Include_dirs) |
| 407 | ca.localIncludes.SetSelectValue(axis, config, localIncludeDirs) |
| 408 | |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 409 | instructionSet := proptools.StringDefault(props.Instruction_set, "") |
| 410 | if instructionSet == "arm" { |
| 411 | ca.features.SetSelectValue(axis, config, []string{"arm_isa_arm", "-arm_isa_thumb"}) |
| 412 | } else if instructionSet != "" && instructionSet != "thumb" { |
| 413 | ctx.ModuleErrorf("Unknown value for instruction_set: %s", instructionSet) |
| 414 | } |
| 415 | |
Liz Kammer | cac7f69 | 2021-12-16 14:19:32 -0500 | [diff] [blame] | 416 | // In Soong, cflags occur on the command line before -std=<val> flag, resulting in the value being |
| 417 | // overridden. In Bazel we always allow overriding, via flags; however, this can cause |
| 418 | // incompatibilities, so we remove "-std=" flags from Cflag properties while leaving it in other |
| 419 | // cases. |
Alix | 1be00d4 | 2022-05-16 22:56:04 +0000 | [diff] [blame] | 420 | ca.copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, true, filterOutStdFlag, filterOutClangUnknownCflags)) |
| 421 | ca.asFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Asflags, true, nil)) |
| 422 | ca.conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Conlyflags, true, filterOutClangUnknownCflags)) |
| 423 | ca.cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Cppflags, true, filterOutClangUnknownCflags)) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 424 | ca.rtti.SetSelectValue(axis, config, props.Rtti) |
| 425 | } |
| 426 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 427 | func (ca *compilerAttributes) convertStlProps(ctx android.ArchVariantContext, module *Module) { |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 428 | bp2BuildPropParseHelper(ctx, module, &StlProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 429 | if stlProps, ok := props.(*StlProperties); ok { |
| 430 | if stlProps.Stl == nil { |
| 431 | return |
| 432 | } |
| 433 | if ca.stl == nil { |
Liz Kammer | 7128d38 | 2022-05-12 11:42:33 -0400 | [diff] [blame] | 434 | stl := deduplicateStlInput(*stlProps.Stl) |
| 435 | ca.stl = &stl |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 436 | } else if ca.stl != stlProps.Stl { |
| 437 | ctx.ModuleErrorf("Unsupported conversion: module with different stl for different variants: %s and %s", *ca.stl, stlProps.Stl) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 438 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 439 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 440 | }) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 441 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 442 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 443 | func (ca *compilerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) { |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 444 | productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{ |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 445 | "Cflags": &ca.copts, |
| 446 | "Asflags": &ca.asFlags, |
| 447 | "CppFlags": &ca.cppFlags, |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 448 | } |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 449 | for propName, attr := range productVarPropNameToAttribute { |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 450 | if productConfigProps, exists := productVariableProps[propName]; exists { |
| 451 | for productConfigProp, prop := range productConfigProps { |
| 452 | flags, ok := prop.([]string) |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 453 | if !ok { |
| 454 | ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName)) |
| 455 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 456 | newFlags, _ := bazel.TryVariableSubstitutions(flags, productConfigProp.Name) |
| 457 | attr.SetSelectValue(productConfigProp.ConfigurationAxis(), productConfigProp.SelectKey(), newFlags) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 458 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 459 | } |
| 460 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 461 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 462 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 463 | func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, implementationHdrs bazel.LabelListAttribute) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 464 | ca.srcs.ResolveExcludes() |
| 465 | partitionedSrcs := groupSrcsByExtension(ctx, ca.srcs) |
| 466 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 467 | ca.protoSrcs = partitionedSrcs[protoSrcPartition] |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 468 | ca.aidlSrcs = partitionedSrcs[aidlSrcPartition] |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 469 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 470 | for p, lla := range partitionedSrcs { |
| 471 | // if there are no sources, there is no need for headers |
| 472 | if lla.IsEmpty() { |
| 473 | continue |
| 474 | } |
| 475 | lla.Append(implementationHdrs) |
| 476 | partitionedSrcs[p] = lla |
| 477 | } |
| 478 | |
| 479 | ca.srcs = partitionedSrcs[cppSrcPartition] |
| 480 | ca.cSrcs = partitionedSrcs[cSrcPartition] |
| 481 | ca.asSrcs = partitionedSrcs[asSrcPartition] |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 482 | ca.asmSrcs = partitionedSrcs[asmSrcPartition] |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 483 | ca.lSrcs = partitionedSrcs[lSrcPartition] |
| 484 | ca.llSrcs = partitionedSrcs[llSrcPartition] |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 485 | |
| 486 | ca.absoluteIncludes.DeduplicateAxesFromBase() |
| 487 | ca.localIncludes.DeduplicateAxesFromBase() |
| 488 | } |
| 489 | |
| 490 | // Parse srcs from an arch or OS's props value. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 491 | func parseSrcs(ctx android.BazelConversionPathContext, props *BaseCompilerProperties) (bazel.LabelList, bool) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 492 | anySrcs := false |
| 493 | // Add srcs-like dependencies such as generated files. |
| 494 | // First create a LabelList containing these dependencies, then merge the values with srcs. |
| 495 | generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources) |
| 496 | if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 { |
| 497 | anySrcs = true |
| 498 | } |
| 499 | |
| 500 | allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs) |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 501 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 502 | if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 { |
| 503 | anySrcs = true |
| 504 | } |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 505 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 506 | return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs |
| 507 | } |
| 508 | |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 509 | // Given a name in srcs prop, check to see if the name references a filegroup |
| 510 | // and the filegroup is converted to aidl_library |
| 511 | func isConvertedToAidlLibrary(ctx android.BazelConversionPathContext, name string) bool { |
| 512 | if module, ok := ctx.ModuleFromName(name); ok { |
| 513 | if android.IsFilegroup(ctx, module) { |
| 514 | if fg, ok := module.(android.Bp2buildAidlLibrary); ok { |
| 515 | return fg.ShouldConvertToAidlLibrary(ctx) |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | return false |
| 520 | } |
| 521 | |
Liz Kammer | a5a29de | 2022-05-25 23:19:37 -0400 | [diff] [blame] | 522 | func bp2buildStdVal(std *string, prefix string, useGnu bool) *string { |
| 523 | defaultVal := prefix + "_std_default" |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 524 | // If c{,pp}std properties are not specified, don't generate them in the BUILD file. |
| 525 | // Defaults are handled by the toolchain definition. |
| 526 | // However, if gnu_extensions is false, then the default gnu-to-c version must be specified. |
Liz Kammer | a5a29de | 2022-05-25 23:19:37 -0400 | [diff] [blame] | 527 | stdVal := proptools.StringDefault(std, defaultVal) |
| 528 | if stdVal == "experimental" || stdVal == defaultVal { |
| 529 | if stdVal == "experimental" { |
| 530 | stdVal = prefix + "_std_experimental" |
| 531 | } |
| 532 | if !useGnu { |
| 533 | stdVal += "_no_gnu" |
| 534 | } |
| 535 | } else if !useGnu { |
| 536 | stdVal = gnuToCReplacer.Replace(stdVal) |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 537 | } |
| 538 | |
Liz Kammer | a5a29de | 2022-05-25 23:19:37 -0400 | [diff] [blame] | 539 | if stdVal == defaultVal { |
| 540 | return nil |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 541 | } |
Liz Kammer | a5a29de | 2022-05-25 23:19:37 -0400 | [diff] [blame] | 542 | return &stdVal |
| 543 | } |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 544 | |
Liz Kammer | a5a29de | 2022-05-25 23:19:37 -0400 | [diff] [blame] | 545 | func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) { |
| 546 | useGnu := useGnuExtensions(gnu_extensions) |
| 547 | |
| 548 | return bp2buildStdVal(c_std, "c", useGnu), bp2buildStdVal(cpp_std, "cpp", useGnu) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 549 | } |
| 550 | |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 551 | // packageFromLabel extracts package from a fully-qualified or relative Label and whether the label |
| 552 | // is fully-qualified. |
| 553 | // e.g. fully-qualified "//a/b:foo" -> "a/b", true, relative: ":bar" -> ".", false |
| 554 | func packageFromLabel(label string) (string, bool) { |
| 555 | split := strings.Split(label, ":") |
| 556 | if len(split) != 2 { |
| 557 | return "", false |
| 558 | } |
| 559 | if split[0] == "" { |
| 560 | return ".", false |
| 561 | } |
| 562 | // remove leading "//" |
| 563 | return split[0][2:], true |
| 564 | } |
| 565 | |
| 566 | // includesFromLabelList extracts relative/absolute includes from a bazel.LabelList> |
| 567 | func includesFromLabelList(labelList bazel.LabelList) (relative, absolute []string) { |
| 568 | for _, hdr := range labelList.Includes { |
| 569 | if pkg, hasPkg := packageFromLabel(hdr.Label); hasPkg { |
| 570 | absolute = append(absolute, pkg) |
| 571 | } else if pkg != "" { |
| 572 | relative = append(relative, pkg) |
| 573 | } |
| 574 | } |
| 575 | return relative, absolute |
| 576 | } |
| 577 | |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 578 | type YasmAttributes struct { |
| 579 | Srcs bazel.LabelListAttribute |
| 580 | Flags bazel.StringListAttribute |
| 581 | Include_dirs bazel.StringListAttribute |
| 582 | } |
| 583 | |
| 584 | func bp2BuildYasm(ctx android.Bp2buildMutatorContext, m *Module, ca compilerAttributes) *bazel.LabelAttribute { |
| 585 | if ca.asmSrcs.IsEmpty() { |
| 586 | return nil |
| 587 | } |
| 588 | |
| 589 | // Yasm needs the include directories from both local_includes and |
| 590 | // export_include_dirs. We don't care about actually exporting them from the |
| 591 | // yasm rule though, because they will also be present on the cc_ rule that |
| 592 | // wraps this yasm rule. |
| 593 | includes := ca.localIncludes.Clone() |
| 594 | bp2BuildPropParseHelper(ctx, m, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 595 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
| 596 | if len(flagExporterProperties.Export_include_dirs) > 0 { |
| 597 | x := bazel.StringListAttribute{} |
| 598 | x.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs) |
| 599 | includes.Append(x) |
| 600 | } |
| 601 | } |
| 602 | }) |
| 603 | |
| 604 | ctx.CreateBazelTargetModule( |
| 605 | bazel.BazelTargetModuleProperties{ |
| 606 | Rule_class: "yasm", |
| 607 | Bzl_load_location: "//build/bazel/rules/cc:yasm.bzl", |
| 608 | }, |
| 609 | android.CommonAttributes{Name: m.Name() + "_yasm"}, |
| 610 | &YasmAttributes{ |
| 611 | Srcs: ca.asmSrcs, |
| 612 | Flags: ca.asFlags, |
| 613 | Include_dirs: *includes, |
| 614 | }) |
| 615 | |
| 616 | // We only want to add a dependency on the _yasm target if there are asm |
| 617 | // sources in the current configuration. If there are unconfigured asm |
| 618 | // sources, always add the dependency. Otherwise, add the dependency only |
| 619 | // on the configuration axes and values that had asm sources. |
| 620 | if len(ca.asmSrcs.Value.Includes) > 0 { |
| 621 | return bazel.MakeLabelAttribute(":" + m.Name() + "_yasm") |
| 622 | } |
| 623 | |
| 624 | ret := &bazel.LabelAttribute{} |
| 625 | for _, axis := range ca.asmSrcs.SortedConfigurationAxes() { |
| 626 | for config := range ca.asmSrcs.ConfigurableValues[axis] { |
| 627 | ret.SetSelectValue(axis, config, bazel.Label{Label: ":" + m.Name() + "_yasm"}) |
| 628 | } |
| 629 | } |
| 630 | return ret |
| 631 | } |
| 632 | |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 633 | // bp2BuildParseBaseProps returns all compiler, linker, library attributes of a cc module.. |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 634 | func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) baseAttributes { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 635 | archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) |
| 636 | archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 637 | archVariantLibraryProperties := module.GetArchVariantProperties(ctx, &LibraryProperties{}) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 638 | |
| 639 | var implementationHdrs bazel.LabelListAttribute |
| 640 | |
| 641 | axisToConfigs := map[bazel.ConfigurationAxis]map[string]bool{} |
| 642 | allAxesAndConfigs := func(cp android.ConfigurationAxisToArchVariantProperties) { |
| 643 | for axis, configMap := range cp { |
| 644 | if _, ok := axisToConfigs[axis]; !ok { |
| 645 | axisToConfigs[axis] = map[string]bool{} |
| 646 | } |
| 647 | for config, _ := range configMap { |
| 648 | axisToConfigs[axis][config] = true |
Chris Parsons | a967f25 | 2021-09-23 16:34:35 -0400 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 652 | allAxesAndConfigs(archVariantCompilerProps) |
| 653 | allAxesAndConfigs(archVariantLinkerProps) |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 654 | allAxesAndConfigs(archVariantLibraryProperties) |
Chris Parsons | a967f25 | 2021-09-23 16:34:35 -0400 | [diff] [blame] | 655 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 656 | compilerAttrs := compilerAttributes{} |
| 657 | linkerAttrs := linkerAttributes{} |
| 658 | |
| 659 | for axis, configs := range axisToConfigs { |
| 660 | for config, _ := range configs { |
| 661 | var allHdrs []string |
| 662 | if baseCompilerProps, ok := archVariantCompilerProps[axis][config].(*BaseCompilerProperties); ok { |
| 663 | allHdrs = baseCompilerProps.Generated_headers |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 664 | if baseCompilerProps.Lex != nil { |
| 665 | compilerAttrs.lexopts.SetSelectValue(axis, config, baseCompilerProps.Lex.Flags) |
| 666 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 667 | (&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, config, baseCompilerProps) |
| 668 | } |
| 669 | |
| 670 | var exportHdrs []string |
| 671 | |
| 672 | if baseLinkerProps, ok := archVariantLinkerProps[axis][config].(*BaseLinkerProperties); ok { |
| 673 | exportHdrs = baseLinkerProps.Export_generated_headers |
| 674 | |
| 675 | (&linkerAttrs).bp2buildForAxisAndConfig(ctx, module.Binary(), axis, config, baseLinkerProps) |
| 676 | } |
| 677 | headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportHdrs, android.BazelLabelForModuleDeps) |
| 678 | implementationHdrs.SetSelectValue(axis, config, headers.implementation) |
| 679 | compilerAttrs.hdrs.SetSelectValue(axis, config, headers.export) |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 680 | |
| 681 | exportIncludes, exportAbsoluteIncludes := includesFromLabelList(headers.export) |
| 682 | compilerAttrs.includes.Includes.SetSelectValue(axis, config, exportIncludes) |
| 683 | compilerAttrs.includes.AbsoluteIncludes.SetSelectValue(axis, config, exportAbsoluteIncludes) |
| 684 | |
| 685 | includes, absoluteIncludes := includesFromLabelList(headers.implementation) |
| 686 | currAbsoluteIncludes := compilerAttrs.absoluteIncludes.SelectValue(axis, config) |
| 687 | currAbsoluteIncludes = android.FirstUniqueStrings(append(currAbsoluteIncludes, absoluteIncludes...)) |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 688 | |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 689 | compilerAttrs.absoluteIncludes.SetSelectValue(axis, config, currAbsoluteIncludes) |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 690 | |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 691 | currIncludes := compilerAttrs.localIncludes.SelectValue(axis, config) |
| 692 | currIncludes = android.FirstUniqueStrings(append(currIncludes, includes...)) |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 693 | |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 694 | compilerAttrs.localIncludes.SetSelectValue(axis, config, currIncludes) |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 695 | |
| 696 | if libraryProps, ok := archVariantLibraryProperties[axis][config].(*LibraryProperties); ok { |
| 697 | if axis == bazel.NoConfigAxis { |
| 698 | compilerAttrs.stubsSymbolFile = libraryProps.Stubs.Symbol_file |
| 699 | compilerAttrs.stubsVersions.SetSelectValue(axis, config, libraryProps.Stubs.Versions) |
| 700 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a56e970 | 2022-02-23 18:39:59 -0500 | [diff] [blame] | 701 | if suffix := libraryProps.Suffix; suffix != nil { |
| 702 | compilerAttrs.suffix.SetSelectValue(axis, config, suffix) |
| 703 | } |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 704 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 705 | } |
| 706 | } |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 707 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 708 | compilerAttrs.convertStlProps(ctx, module) |
| 709 | (&linkerAttrs).convertStripProps(ctx, module) |
| 710 | |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 711 | if module.coverage != nil && module.coverage.Properties.Native_coverage != nil && |
| 712 | !Bool(module.coverage.Properties.Native_coverage) { |
| 713 | // Native_coverage is arch neutral |
| 714 | (&linkerAttrs).features.Append(bazel.MakeStringListAttribute([]string{"-coverage"})) |
| 715 | } |
| 716 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 717 | productVariableProps := android.ProductVariableProperties(ctx) |
| 718 | |
| 719 | (&compilerAttrs).convertProductVariables(ctx, productVariableProps) |
| 720 | (&linkerAttrs).convertProductVariables(ctx, productVariableProps) |
| 721 | |
| 722 | (&compilerAttrs).finalize(ctx, implementationHdrs) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 723 | (&linkerAttrs).finalize(ctx) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 724 | |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 725 | (&compilerAttrs.srcs).Add(bp2BuildYasm(ctx, module, compilerAttrs)) |
| 726 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 727 | protoDep := bp2buildProto(ctx, module, compilerAttrs.protoSrcs) |
| 728 | |
| 729 | // bp2buildProto will only set wholeStaticLib or implementationWholeStaticLib, but we don't know |
| 730 | // which. This will add the newly generated proto library to the appropriate attribute and nothing |
| 731 | // to the other |
| 732 | (&linkerAttrs).wholeArchiveDeps.Add(protoDep.wholeStaticLib) |
| 733 | (&linkerAttrs).implementationWholeArchiveDeps.Add(protoDep.implementationWholeStaticLib) |
Vinh Tran | fde57eb | 2022-08-29 17:46:58 -0400 | [diff] [blame] | 734 | |
| 735 | aidlDep := bp2buildCcAidlLibrary(ctx, module, compilerAttrs.aidlSrcs) |
| 736 | if aidlDep != nil { |
| 737 | if lib, ok := module.linker.(*libraryDecorator); ok { |
| 738 | if proptools.Bool(lib.Properties.Aidl.Export_aidl_headers) { |
| 739 | (&linkerAttrs).wholeArchiveDeps.Add(aidlDep) |
| 740 | } else { |
| 741 | (&linkerAttrs).implementationWholeArchiveDeps.Add(aidlDep) |
| 742 | } |
| 743 | } |
| 744 | } |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 745 | |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 746 | convertedLSrcs := bp2BuildLex(ctx, module.Name(), compilerAttrs) |
| 747 | (&compilerAttrs).srcs.Add(&convertedLSrcs.srcName) |
| 748 | (&compilerAttrs).cSrcs.Add(&convertedLSrcs.cSrcName) |
| 749 | |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 750 | features := compilerAttrs.features.Clone().Append(linkerAttrs.features) |
| 751 | features.DeduplicateAxesFromBase() |
| 752 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 753 | return baseAttributes{ |
| 754 | compilerAttrs, |
| 755 | linkerAttrs, |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 756 | *features, |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 757 | protoDep.protoDep, |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 758 | aidlDep, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 762 | func bp2buildAidlLibraries( |
| 763 | ctx android.Bp2buildMutatorContext, |
| 764 | m *Module, |
| 765 | aidlSrcs bazel.LabelListAttribute, |
| 766 | ) bazel.LabelList { |
| 767 | var aidlLibraries bazel.LabelList |
| 768 | var directAidlSrcs bazel.LabelList |
| 769 | |
| 770 | // Make a list of labels that correspond to filegroups that are already converted to aidl_library |
| 771 | for _, aidlSrc := range aidlSrcs.Value.Includes { |
| 772 | src := aidlSrc.OriginalModuleName |
| 773 | if isConvertedToAidlLibrary(ctx, src) { |
| 774 | module, _ := ctx.ModuleFromName(src) |
| 775 | fg, _ := module.(android.Bp2buildAidlLibrary) |
| 776 | aidlLibraries.Add(&bazel.Label{ |
| 777 | Label: fg.GetAidlLibraryLabel(ctx), |
| 778 | }) |
| 779 | } else { |
| 780 | directAidlSrcs.Add(&aidlSrc) |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | if len(directAidlSrcs.Includes) > 0 { |
| 785 | aidlLibraryLabel := m.Name() + "_aidl_library" |
| 786 | ctx.CreateBazelTargetModule( |
| 787 | bazel.BazelTargetModuleProperties{ |
| 788 | Rule_class: "aidl_library", |
| 789 | Bzl_load_location: "//build/bazel/rules/aidl:library.bzl", |
| 790 | }, |
| 791 | android.CommonAttributes{Name: aidlLibraryLabel}, |
| 792 | &aidlLibraryAttributes{ |
| 793 | Srcs: bazel.MakeLabelListAttribute(directAidlSrcs), |
| 794 | }, |
| 795 | ) |
| 796 | aidlLibraries.Add(&bazel.Label{ |
| 797 | Label: ":" + aidlLibraryLabel, |
| 798 | }) |
| 799 | } |
| 800 | return aidlLibraries |
| 801 | } |
| 802 | |
| 803 | func bp2buildCcAidlLibrary( |
| 804 | ctx android.Bp2buildMutatorContext, |
| 805 | m *Module, |
| 806 | aidlSrcs bazel.LabelListAttribute, |
| 807 | ) *bazel.LabelAttribute { |
| 808 | suffix := "_cc_aidl_library" |
| 809 | ccAidlLibrarylabel := m.Name() + suffix |
| 810 | |
| 811 | aidlLibraries := bp2buildAidlLibraries(ctx, m, aidlSrcs) |
| 812 | |
| 813 | if aidlLibraries.IsEmpty() { |
| 814 | return nil |
| 815 | } |
| 816 | |
| 817 | ctx.CreateBazelTargetModule( |
| 818 | bazel.BazelTargetModuleProperties{ |
| 819 | Rule_class: "cc_aidl_library", |
| 820 | Bzl_load_location: "//build/bazel/rules/cc:cc_aidl_library.bzl", |
| 821 | }, |
| 822 | android.CommonAttributes{Name: ccAidlLibrarylabel}, |
| 823 | &ccAidlLibraryAttributes{ |
| 824 | Deps: bazel.MakeLabelListAttribute(aidlLibraries), |
| 825 | }, |
| 826 | ) |
| 827 | |
| 828 | label := &bazel.LabelAttribute{ |
| 829 | Value: &bazel.Label{ |
| 830 | Label: ":" + ccAidlLibrarylabel, |
| 831 | }, |
| 832 | } |
| 833 | return label |
| 834 | } |
| 835 | |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 836 | func bp2BuildParseSdkAttributes(module *Module) sdkAttributes { |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 837 | return sdkAttributes{ |
| 838 | Sdk_version: module.Properties.Sdk_version, |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 839 | Min_sdk_version: module.Properties.Min_sdk_version, |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | type sdkAttributes struct { |
| 844 | Sdk_version *string |
| 845 | Min_sdk_version *string |
| 846 | } |
| 847 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 848 | // Convenience struct to hold all attributes parsed from linker properties. |
| 849 | type linkerAttributes struct { |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 850 | deps bazel.LabelListAttribute |
| 851 | implementationDeps bazel.LabelListAttribute |
| 852 | dynamicDeps bazel.LabelListAttribute |
| 853 | implementationDynamicDeps bazel.LabelListAttribute |
Cole Faust | 6b29f59 | 2022-08-09 09:50:56 -0700 | [diff] [blame] | 854 | runtimeDeps bazel.LabelListAttribute |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 855 | wholeArchiveDeps bazel.LabelListAttribute |
| 856 | implementationWholeArchiveDeps bazel.LabelListAttribute |
| 857 | systemDynamicDeps bazel.LabelListAttribute |
| 858 | usedSystemDynamicDepAsDynamicDep map[string]bool |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 859 | |
Jingwen Chen | 6ada589 | 2021-09-17 11:38:09 +0000 | [diff] [blame] | 860 | linkCrt bazel.BoolAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 861 | useLibcrt bazel.BoolAttribute |
Rupert Shuttleworth | 484aa25 | 2021-12-10 07:22:53 -0500 | [diff] [blame] | 862 | useVersionLib bazel.BoolAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 863 | linkopts bazel.StringListAttribute |
Liz Kammer | d287118 | 2021-10-04 13:54:37 -0400 | [diff] [blame] | 864 | additionalLinkerInputs bazel.LabelListAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 865 | stripKeepSymbols bazel.BoolAttribute |
| 866 | stripKeepSymbolsAndDebugFrame bazel.BoolAttribute |
| 867 | stripKeepSymbolsList bazel.StringListAttribute |
| 868 | stripAll bazel.BoolAttribute |
| 869 | stripNone bazel.BoolAttribute |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 870 | features bazel.StringListAttribute |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 871 | } |
| 872 | |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 873 | var ( |
| 874 | soongSystemSharedLibs = []string{"libc", "libm", "libdl"} |
| 875 | ) |
| 876 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 877 | 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] | 878 | // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module |
| 879 | var axisFeatures []string |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 880 | |
Liz Kammer | cc2c1ef | 2022-03-21 09:03:29 -0400 | [diff] [blame] | 881 | wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs) |
| 882 | la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs)) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 883 | // Excludes to parallel Soong: |
| 884 | // 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] | 885 | staticLibs := android.FirstUniqueStrings(android.RemoveListFromList(props.Static_libs, wholeStaticLibs)) |
| 886 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 887 | 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] | 888 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 889 | headerLibs := android.FirstUniqueStrings(props.Header_libs) |
| 890 | hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 891 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 892 | (&hDeps.export).Append(staticDeps.export) |
| 893 | la.deps.SetSelectValue(axis, config, hDeps.export) |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 894 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 895 | (&hDeps.implementation).Append(staticDeps.implementation) |
| 896 | la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation) |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 897 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 898 | systemSharedLibs := props.System_shared_libs |
| 899 | // systemSharedLibs distinguishes between nil/empty list behavior: |
| 900 | // nil -> use default values |
| 901 | // empty list -> no values specified |
| 902 | if len(systemSharedLibs) > 0 { |
| 903 | systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs) |
| 904 | } |
| 905 | la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs)) |
| 906 | |
| 907 | sharedLibs := android.FirstUniqueStrings(props.Shared_libs) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 908 | excludeSharedLibs := props.Exclude_shared_libs |
| 909 | usedSystem := android.FilterListPred(sharedLibs, func(s string) bool { |
| 910 | return android.InList(s, soongSystemSharedLibs) && !android.InList(s, excludeSharedLibs) |
| 911 | }) |
| 912 | for _, el := range usedSystem { |
| 913 | if la.usedSystemDynamicDepAsDynamicDep == nil { |
| 914 | la.usedSystemDynamicDepAsDynamicDep = map[string]bool{} |
| 915 | } |
| 916 | la.usedSystemDynamicDepAsDynamicDep[el] = true |
| 917 | } |
| 918 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 919 | sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, sharedLibs, props.Exclude_shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes) |
| 920 | la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export) |
| 921 | la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation) |
Wei Li | 81852ca | 2022-07-27 00:22:06 -0700 | [diff] [blame] | 922 | if axis == bazel.NoConfigAxis || (axis == bazel.OsConfigurationAxis && config == bazel.OsAndroid) { |
| 923 | // If a dependency in la.implementationDynamicDeps has stubs, its stub variant should be |
| 924 | // used when the dependency is linked in a APEX. The dependencies in NoConfigAxis and |
| 925 | // OsConfigurationAxis/OsAndroid are grouped by having stubs or not, so Bazel select() |
| 926 | // statement can be used to choose source/stub variants of them. |
| 927 | depsWithStubs := []bazel.Label{} |
| 928 | for _, l := range sharedDeps.implementation.Includes { |
| 929 | dep, _ := ctx.ModuleFromName(l.OriginalModuleName) |
| 930 | if m, ok := dep.(*Module); ok && m.HasStubsVariants() { |
| 931 | depsWithStubs = append(depsWithStubs, l) |
| 932 | } |
| 933 | } |
| 934 | if len(depsWithStubs) > 0 { |
| 935 | implDynamicDeps := bazel.SubtractBazelLabelList(sharedDeps.implementation, bazel.MakeLabelList(depsWithStubs)) |
| 936 | la.implementationDynamicDeps.SetSelectValue(axis, config, implDynamicDeps) |
| 937 | |
| 938 | stubLibLabels := []bazel.Label{} |
| 939 | for _, l := range depsWithStubs { |
Liz Kammer | 91487d4 | 2022-09-13 11:27:11 -0400 | [diff] [blame] | 940 | l.Label = l.Label + stubsSuffix |
Wei Li | 81852ca | 2022-07-27 00:22:06 -0700 | [diff] [blame] | 941 | stubLibLabels = append(stubLibLabels, l) |
| 942 | } |
| 943 | inApexSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex) |
| 944 | nonApexSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex) |
| 945 | defaultSelectValue := la.implementationDynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey) |
| 946 | if axis == bazel.NoConfigAxis { |
| 947 | (&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels)) |
| 948 | (&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs)) |
| 949 | (&defaultSelectValue).Append(bazel.MakeLabelList(depsWithStubs)) |
| 950 | la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, inApexSelectValue) |
| 951 | la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, nonApexSelectValue) |
| 952 | la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, defaultSelectValue) |
| 953 | } else if config == bazel.OsAndroid { |
| 954 | (&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels)) |
| 955 | (&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs)) |
| 956 | la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, inApexSelectValue) |
| 957 | la.implementationDynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, nonApexSelectValue) |
| 958 | } |
| 959 | } |
| 960 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 961 | |
| 962 | if !BoolDefault(props.Pack_relocations, packRelocationsDefault) { |
| 963 | axisFeatures = append(axisFeatures, "disable_pack_relocations") |
| 964 | } |
| 965 | |
| 966 | if Bool(props.Allow_undefined_symbols) { |
| 967 | axisFeatures = append(axisFeatures, "-no_undefined_symbols") |
| 968 | } |
| 969 | |
| 970 | var linkerFlags []string |
| 971 | if len(props.Ldflags) > 0 { |
Liz Kammer | f38a837 | 2022-02-04 15:39:00 -0500 | [diff] [blame] | 972 | linkerFlags = append(linkerFlags, proptools.NinjaEscapeList(props.Ldflags)...) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 973 | // binaries remove static flag if -shared is in the linker flags |
| 974 | if isBinary && android.InList("-shared", linkerFlags) { |
| 975 | axisFeatures = append(axisFeatures, "-static_flag") |
| 976 | } |
| 977 | } |
| 978 | if props.Version_script != nil { |
| 979 | label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script) |
| 980 | la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}}) |
| 981 | linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label)) |
| 982 | } |
Alix | 773adaa | 2022-04-27 17:49:34 +0000 | [diff] [blame] | 983 | |
| 984 | if props.Dynamic_list != nil { |
| 985 | label := android.BazelLabelForModuleSrcSingle(ctx, *props.Dynamic_list) |
| 986 | la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}}) |
| 987 | linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--dynamic-list,$(location %s)", label.Label)) |
| 988 | } |
| 989 | |
Alix | 1be00d4 | 2022-05-16 22:56:04 +0000 | [diff] [blame] | 990 | la.linkopts.SetSelectValue(axis, config, parseCommandLineFlags(linkerFlags, false, filterOutClangUnknownCflags)) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 991 | la.useLibcrt.SetSelectValue(axis, config, props.libCrt()) |
| 992 | |
Rupert Shuttleworth | 484aa25 | 2021-12-10 07:22:53 -0500 | [diff] [blame] | 993 | if axis == bazel.NoConfigAxis { |
| 994 | la.useVersionLib.SetSelectValue(axis, config, props.Use_version_lib) |
| 995 | } |
| 996 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 997 | // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it. |
| 998 | if props.crt() != nil { |
| 999 | if axis == bazel.NoConfigAxis { |
| 1000 | la.linkCrt.SetSelectValue(axis, config, props.crt()) |
| 1001 | } else if axis == bazel.ArchConfigurationAxis { |
| 1002 | ctx.ModuleErrorf("nocrt is not supported for arch variants") |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | if axisFeatures != nil { |
| 1007 | la.features.SetSelectValue(axis, config, axisFeatures) |
| 1008 | } |
Cole Faust | 6b29f59 | 2022-08-09 09:50:56 -0700 | [diff] [blame] | 1009 | |
| 1010 | runtimeDeps := android.BazelLabelForModuleDepsExcludes(ctx, props.Runtime_libs, props.Exclude_runtime_libs) |
| 1011 | if !runtimeDeps.IsEmpty() { |
| 1012 | la.runtimeDeps.SetSelectValue(axis, config, runtimeDeps) |
| 1013 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1014 | } |
| 1015 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1016 | func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) { |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1017 | bp2BuildPropParseHelper(ctx, module, &StripProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 1018 | if stripProperties, ok := props.(*StripProperties); ok { |
| 1019 | la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols) |
| 1020 | la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list) |
| 1021 | la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame) |
| 1022 | la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All) |
| 1023 | la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None) |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 1024 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1025 | }) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1026 | } |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 1027 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1028 | func (la *linkerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) { |
Jingwen Chen | 6ada589 | 2021-09-17 11:38:09 +0000 | [diff] [blame] | 1029 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1030 | type productVarDep struct { |
| 1031 | // the name of the corresponding excludes field, if one exists |
| 1032 | excludesField string |
| 1033 | // reference to the bazel attribute that should be set for the given product variable config |
| 1034 | attribute *bazel.LabelListAttribute |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 1035 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1036 | depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1037 | } |
| 1038 | |
Zi Wang | 0a8a129 | 2022-08-30 06:27:01 +0000 | [diff] [blame] | 1039 | // an intermediate attribute that holds Header_libs info, and will be appended to |
| 1040 | // implementationDeps at the end, to solve the confliction that both header_libs |
| 1041 | // and static_libs use implementationDeps. |
| 1042 | var headerDeps bazel.LabelListAttribute |
| 1043 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1044 | productVarToDepFields := map[string]productVarDep{ |
| 1045 | // product variables do not support exclude_shared_libs |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1046 | "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes}, |
| 1047 | "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes}, |
| 1048 | "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes}, |
Zi Wang | 0a8a129 | 2022-08-30 06:27:01 +0000 | [diff] [blame] | 1049 | "Header_libs": {attribute: &headerDeps, depResolutionFunc: bazelLabelForHeaderDepsExcludes}, |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1050 | } |
| 1051 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1052 | for name, dep := range productVarToDepFields { |
| 1053 | props, exists := productVariableProps[name] |
| 1054 | excludeProps, excludesExists := productVariableProps[dep.excludesField] |
| 1055 | // if neither an include or excludes property exists, then skip it |
| 1056 | if !exists && !excludesExists { |
| 1057 | continue |
| 1058 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1059 | // Collect all the configurations that an include or exclude property exists for. |
| 1060 | // We want to iterate all configurations rather than either the include or exclude because, for a |
| 1061 | // particular configuration, we may have either only an include or an exclude to handle. |
| 1062 | productConfigProps := make(map[android.ProductConfigProperty]bool, len(props)+len(excludeProps)) |
| 1063 | for p := range props { |
| 1064 | productConfigProps[p] = true |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1065 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1066 | for p := range excludeProps { |
| 1067 | productConfigProps[p] = true |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1068 | } |
| 1069 | |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1070 | for productConfigProp := range productConfigProps { |
| 1071 | prop, includesExists := props[productConfigProp] |
| 1072 | excludesProp, excludesExists := excludeProps[productConfigProp] |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1073 | var includes, excludes []string |
| 1074 | var ok bool |
| 1075 | // 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] | 1076 | if includes, ok = prop.([]string); includesExists && !ok { |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1077 | ctx.ModuleErrorf("Could not convert product variable %s property", name) |
| 1078 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1079 | if excludes, ok = excludesProp.([]string); excludesExists && !ok { |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1080 | ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField) |
| 1081 | } |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 1082 | |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 1083 | dep.attribute.EmitEmptyList = productConfigProp.AlwaysEmit() |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1084 | dep.attribute.SetSelectValue( |
| 1085 | productConfigProp.ConfigurationAxis(), |
| 1086 | productConfigProp.SelectKey(), |
| 1087 | dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes), |
| 1088 | ) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1089 | } |
| 1090 | } |
Zi Wang | 0a8a129 | 2022-08-30 06:27:01 +0000 | [diff] [blame] | 1091 | la.implementationDeps.Append(headerDeps) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1092 | } |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1093 | |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1094 | func (la *linkerAttributes) finalize(ctx android.BazelConversionPathContext) { |
| 1095 | // if system dynamic deps have the default value, any use of a system dynamic library used will |
| 1096 | // result in duplicate library errors for bionic OSes. Here, we explicitly exclude those libraries |
Liz Kammer | 43345e2 | 2022-08-04 13:57:35 -0400 | [diff] [blame] | 1097 | // from bionic OSes and the no config case as these libraries only build for bionic OSes. |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1098 | if la.systemDynamicDeps.IsNil() && len(la.usedSystemDynamicDepAsDynamicDep) > 0 { |
| 1099 | toRemove := bazelLabelForSharedDeps(ctx, android.SortedStringKeys(la.usedSystemDynamicDepAsDynamicDep)) |
Liz Kammer | 43345e2 | 2022-08-04 13:57:35 -0400 | [diff] [blame] | 1100 | la.dynamicDeps.Exclude(bazel.NoConfigAxis, "", toRemove) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1101 | la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove) |
| 1102 | la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove) |
Liz Kammer | 91487d4 | 2022-09-13 11:27:11 -0400 | [diff] [blame] | 1103 | la.implementationDynamicDeps.Exclude(bazel.NoConfigAxis, "", toRemove) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1104 | la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove) |
| 1105 | la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove) |
Liz Kammer | 91487d4 | 2022-09-13 11:27:11 -0400 | [diff] [blame] | 1106 | |
| 1107 | la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, toRemove) |
| 1108 | la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, toRemove) |
| 1109 | stubsToRemove := make([]bazel.Label, 0, len(la.usedSystemDynamicDepAsDynamicDep)) |
| 1110 | for _, lib := range toRemove.Includes { |
| 1111 | lib.Label += stubsSuffix |
| 1112 | stubsToRemove = append(stubsToRemove, lib) |
| 1113 | } |
| 1114 | la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, bazel.MakeLabelList(stubsToRemove)) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1115 | } |
| 1116 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1117 | la.deps.ResolveExcludes() |
| 1118 | la.implementationDeps.ResolveExcludes() |
| 1119 | la.dynamicDeps.ResolveExcludes() |
| 1120 | la.implementationDynamicDeps.ResolveExcludes() |
| 1121 | la.wholeArchiveDeps.ResolveExcludes() |
| 1122 | la.systemDynamicDeps.ForceSpecifyEmptyList = true |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1123 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 1124 | } |
| 1125 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 1126 | // Relativize a list of root-relative paths with respect to the module's |
| 1127 | // directory. |
| 1128 | // |
| 1129 | // include_dirs Soong prop are root-relative (b/183742505), but |
| 1130 | // local_include_dirs, export_include_dirs and export_system_include_dirs are |
| 1131 | // module dir relative. This function makes a list of paths entirely module dir |
| 1132 | // relative. |
| 1133 | // |
| 1134 | // For the `include` attribute, Bazel wants the paths to be relative to the |
| 1135 | // module. |
| 1136 | func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 1137 | var relativePaths []string |
| 1138 | for _, path := range paths { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 1139 | // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path |
| 1140 | relativePath, err := filepath.Rel(ctx.ModuleDir(), path) |
| 1141 | if err != nil { |
| 1142 | panic(err) |
| 1143 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 1144 | relativePaths = append(relativePaths, relativePath) |
| 1145 | } |
| 1146 | return relativePaths |
| 1147 | } |
| 1148 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 1149 | // BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel |
| 1150 | // attributes. |
| 1151 | type BazelIncludes struct { |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 1152 | AbsoluteIncludes bazel.StringListAttribute |
| 1153 | Includes bazel.StringListAttribute |
| 1154 | SystemIncludes bazel.StringListAttribute |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 1155 | } |
| 1156 | |
Liz Kammer | 5454944 | 2022-05-11 13:55:06 -0400 | [diff] [blame] | 1157 | func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module, includes *BazelIncludes) BazelIncludes { |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 1158 | var exported BazelIncludes |
| 1159 | if includes != nil { |
| 1160 | exported = *includes |
| 1161 | } else { |
| 1162 | exported = BazelIncludes{} |
| 1163 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1164 | bp2BuildPropParseHelper(ctx, module, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 1165 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
| 1166 | if len(flagExporterProperties.Export_include_dirs) > 0 { |
| 1167 | exported.Includes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.Includes.SelectValue(axis, config), flagExporterProperties.Export_include_dirs...))) |
| 1168 | } |
| 1169 | if len(flagExporterProperties.Export_system_include_dirs) > 0 { |
| 1170 | exported.SystemIncludes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.SystemIncludes.SelectValue(axis, config), flagExporterProperties.Export_system_include_dirs...))) |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 1171 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 1172 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1173 | }) |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 1174 | exported.AbsoluteIncludes.DeduplicateAxesFromBase() |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 1175 | exported.Includes.DeduplicateAxesFromBase() |
| 1176 | exported.SystemIncludes.DeduplicateAxesFromBase() |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 1177 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 1178 | return exported |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 1179 | } |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1180 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1181 | func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1182 | label := android.BazelModuleLabel(ctx, m) |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 1183 | if ccModule, ok := m.(*Module); ok && ccModule.typ() == fullLibrary && !android.GetBp2BuildAllowList().GenerateCcLibraryStaticOnly(m.Name()) { |
Liz Kammer | 35ca77e | 2021-12-22 15:31:40 -0500 | [diff] [blame] | 1184 | label += "_bp2build_cc_library_static" |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1185 | } |
| 1186 | return label |
| 1187 | } |
| 1188 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1189 | func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1190 | // cc_library, at it's root name, propagates the shared library, which depends on the static |
| 1191 | // library. |
| 1192 | return android.BazelModuleLabel(ctx, m) |
| 1193 | } |
| 1194 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1195 | func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1196 | label := bazelLabelForStaticModule(ctx, m) |
| 1197 | if aModule, ok := m.(android.Module); ok { |
| 1198 | if android.IsModulePrebuilt(aModule) { |
| 1199 | label += "_alwayslink" |
| 1200 | } |
| 1201 | } |
| 1202 | return label |
| 1203 | } |
| 1204 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1205 | func bazelLabelForWholeDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1206 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps) |
| 1207 | } |
| 1208 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1209 | func bazelLabelForWholeDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1210 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps) |
| 1211 | } |
| 1212 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1213 | func bazelLabelForStaticDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1214 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule) |
| 1215 | } |
| 1216 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1217 | func bazelLabelForStaticDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1218 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule) |
| 1219 | } |
| 1220 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1221 | func bazelLabelForSharedDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1222 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule) |
| 1223 | } |
| 1224 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1225 | func bazelLabelForHeaderDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1226 | // This is not elegant, but bp2build's shared library targets only propagate |
| 1227 | // their header information as part of the normal C++ provider. |
| 1228 | return bazelLabelForSharedDeps(ctx, modules) |
| 1229 | } |
| 1230 | |
Zi Wang | 0a8a129 | 2022-08-30 06:27:01 +0000 | [diff] [blame] | 1231 | func bazelLabelForHeaderDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
| 1232 | // This is only used when product_variable header_libs is processed, to follow |
| 1233 | // the pattern of depResolutionFunc |
| 1234 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule) |
| 1235 | } |
| 1236 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1237 | func bazelLabelForSharedDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1238 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule) |
| 1239 | } |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1240 | |
| 1241 | type binaryLinkerAttrs struct { |
| 1242 | Linkshared *bool |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a56e970 | 2022-02-23 18:39:59 -0500 | [diff] [blame] | 1243 | Suffix bazel.StringAttribute |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1244 | } |
| 1245 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1246 | func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs { |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1247 | attrs := binaryLinkerAttrs{} |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1248 | bp2BuildPropParseHelper(ctx, m, &BinaryLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 1249 | linkerProps := props.(*BinaryLinkerProperties) |
| 1250 | staticExecutable := linkerProps.Static_executable |
| 1251 | if axis == bazel.NoConfigAxis { |
| 1252 | if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared { |
| 1253 | attrs.Linkshared = &linkBinaryShared |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1254 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1255 | } else if staticExecutable != nil { |
| 1256 | // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a |
| 1257 | // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling |
| 1258 | ctx.ModuleErrorf("bp2build cannot migrate a module with arch/target-specific static_executable values") |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1259 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a56e970 | 2022-02-23 18:39:59 -0500 | [diff] [blame] | 1260 | if suffix := linkerProps.Suffix; suffix != nil { |
| 1261 | attrs.Suffix.SetSelectValue(axis, config, suffix) |
| 1262 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1263 | }) |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1264 | |
| 1265 | return attrs |
| 1266 | } |