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