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 ( |
Trevor Radcliffe | cee4e05 | 2022-09-06 19:31:25 +0000 | [diff] [blame] | 31 | cSrcPartition = "c" |
| 32 | asSrcPartition = "as" |
| 33 | asmSrcPartition = "asm" |
| 34 | lSrcPartition = "l" |
| 35 | llSrcPartition = "ll" |
| 36 | cppSrcPartition = "cpp" |
| 37 | protoSrcPartition = "proto" |
| 38 | aidlSrcPartition = "aidl" |
| 39 | syspropSrcPartition = "sysprop" |
Liz Kammer | 91487d4 | 2022-09-13 11:27:11 -0400 | [diff] [blame] | 40 | |
| 41 | stubsSuffix = "_stub_libs_current" |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 42 | ) |
| 43 | |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 44 | // staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties -- |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 45 | // 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] | 46 | type staticOrSharedAttributes struct { |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 47 | Srcs bazel.LabelListAttribute |
| 48 | Srcs_c bazel.LabelListAttribute |
| 49 | Srcs_as bazel.LabelListAttribute |
| 50 | Srcs_aidl bazel.LabelListAttribute |
| 51 | Hdrs bazel.LabelListAttribute |
| 52 | Copts bazel.StringListAttribute |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 53 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 54 | Deps bazel.LabelListAttribute |
| 55 | Implementation_deps bazel.LabelListAttribute |
| 56 | Dynamic_deps bazel.LabelListAttribute |
| 57 | Implementation_dynamic_deps bazel.LabelListAttribute |
| 58 | Whole_archive_deps bazel.LabelListAttribute |
| 59 | Implementation_whole_archive_deps bazel.LabelListAttribute |
Cole Faust | 6b29f59 | 2022-08-09 09:50:56 -0700 | [diff] [blame] | 60 | Runtime_deps bazel.LabelListAttribute |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 61 | |
| 62 | System_dynamic_deps bazel.LabelListAttribute |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 63 | |
| 64 | Enabled bazel.BoolAttribute |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 65 | |
Yu Liu | f01a0f0 | 2022-12-07 15:45:30 -0800 | [diff] [blame] | 66 | Native_coverage *bool |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 67 | |
Liz Kammer | aceec25 | 2023-03-24 09:46:36 -0400 | [diff] [blame^] | 68 | Apex_available []string |
| 69 | |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 70 | sdkAttributes |
Sam Delmerico | fb3bb32 | 2022-10-21 10:42:24 -0400 | [diff] [blame] | 71 | |
| 72 | tidyAttributes |
| 73 | } |
| 74 | |
| 75 | type tidyAttributes struct { |
Sam Delmerico | 63f0c93 | 2023-03-14 14:05:28 -0400 | [diff] [blame] | 76 | Tidy *string |
Sam Delmerico | fb3bb32 | 2022-10-21 10:42:24 -0400 | [diff] [blame] | 77 | Tidy_flags []string |
| 78 | Tidy_checks []string |
| 79 | Tidy_checks_as_errors []string |
Sam Delmerico | c9b8fbd | 2022-10-25 15:47:17 -0400 | [diff] [blame] | 80 | Tidy_disabled_srcs bazel.LabelListAttribute |
Sam Delmerico | 4c902d6 | 2022-11-02 14:17:15 -0400 | [diff] [blame] | 81 | Tidy_timeout_srcs bazel.LabelListAttribute |
Sam Delmerico | fb3bb32 | 2022-10-21 10:42:24 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Sam Delmerico | c9b8fbd | 2022-10-25 15:47:17 -0400 | [diff] [blame] | 84 | func (m *Module) convertTidyAttributes(ctx android.BaseMutatorContext, moduleAttrs *tidyAttributes) { |
Sam Delmerico | fb3bb32 | 2022-10-21 10:42:24 -0400 | [diff] [blame] | 85 | for _, f := range m.features { |
| 86 | if tidy, ok := f.(*tidyFeature); ok { |
Sam Delmerico | 63f0c93 | 2023-03-14 14:05:28 -0400 | [diff] [blame] | 87 | var tidyAttr *string |
| 88 | if tidy.Properties.Tidy != nil { |
| 89 | if *tidy.Properties.Tidy { |
| 90 | tidyAttr = proptools.StringPtr("local") |
| 91 | } else { |
| 92 | tidyAttr = proptools.StringPtr("never") |
| 93 | } |
| 94 | } |
| 95 | moduleAttrs.Tidy = tidyAttr |
Sam Delmerico | fb3bb32 | 2022-10-21 10:42:24 -0400 | [diff] [blame] | 96 | moduleAttrs.Tidy_flags = tidy.Properties.Tidy_flags |
| 97 | moduleAttrs.Tidy_checks = tidy.Properties.Tidy_checks |
| 98 | moduleAttrs.Tidy_checks_as_errors = tidy.Properties.Tidy_checks_as_errors |
| 99 | } |
Sam Delmerico | c9b8fbd | 2022-10-25 15:47:17 -0400 | [diff] [blame] | 100 | |
| 101 | } |
Sam Delmerico | c9b8fbd | 2022-10-25 15:47:17 -0400 | [diff] [blame] | 102 | archVariantProps := m.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) |
| 103 | for axis, configToProps := range archVariantProps { |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 104 | for cfg, _props := range configToProps { |
Sam Delmerico | c9b8fbd | 2022-10-25 15:47:17 -0400 | [diff] [blame] | 105 | if archProps, ok := _props.(*BaseCompilerProperties); ok { |
| 106 | archDisabledSrcs := android.BazelLabelForModuleSrc(ctx, archProps.Tidy_disabled_srcs) |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 107 | moduleAttrs.Tidy_disabled_srcs.SetSelectValue(axis, cfg, archDisabledSrcs) |
Sam Delmerico | 4c902d6 | 2022-11-02 14:17:15 -0400 | [diff] [blame] | 108 | archTimeoutSrcs := android.BazelLabelForModuleSrc(ctx, archProps.Tidy_timeout_srcs) |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 109 | moduleAttrs.Tidy_timeout_srcs.SetSelectValue(axis, cfg, archTimeoutSrcs) |
Sam Delmerico | c9b8fbd | 2022-10-25 15:47:17 -0400 | [diff] [blame] | 110 | } |
| 111 | } |
Sam Delmerico | fb3bb32 | 2022-10-21 10:42:24 -0400 | [diff] [blame] | 112 | } |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 115 | // groupSrcsByExtension partitions `srcs` into groups based on file extension. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 116 | func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute { |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 117 | // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl |
| 118 | // macro. |
| 119 | addSuffixForFilegroup := func(suffix string) bazel.LabelMapper { |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 120 | return func(otherModuleCtx bazel.OtherModuleContext, label bazel.Label) (string, bool) { |
| 121 | |
| 122 | m, exists := otherModuleCtx.ModuleFromName(label.OriginalModuleName) |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 123 | labelStr := label.Label |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 124 | if !exists || !android.IsFilegroup(otherModuleCtx, m) { |
| 125 | return labelStr, false |
| 126 | } |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 127 | // If the filegroup is already converted to aidl_library or proto_library, |
| 128 | // skip creating _c_srcs, _as_srcs, _cpp_srcs filegroups |
| 129 | fg, _ := m.(android.FileGroupAsLibrary) |
| 130 | if fg.ShouldConvertToAidlLibrary(ctx) || fg.ShouldConvertToProtoLibrary(ctx) { |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 131 | return labelStr, false |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 132 | } |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 133 | return labelStr + suffix, true |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 134 | } |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 137 | // TODO(b/190006308): Handle language detection of sources in a Bazel rule. |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 138 | labels := bazel.LabelPartitions{ |
| 139 | protoSrcPartition: android.ProtoSrcLabelPartition, |
Liz Kammer | aabfb5d | 2021-12-08 15:25:06 -0500 | [diff] [blame] | 140 | cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")}, |
| 141 | asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")}, |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 142 | asmSrcPartition: bazel.LabelPartition{Extensions: []string{".asm"}}, |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 143 | aidlSrcPartition: android.AidlSrcLabelPartition, |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 144 | // TODO(http://b/231968910): If there is ever a filegroup target that |
| 145 | // contains .l or .ll files we will need to find a way to add a |
| 146 | // LabelMapper for these that identifies these filegroups and |
| 147 | // converts them appropriately |
| 148 | lSrcPartition: bazel.LabelPartition{Extensions: []string{".l"}}, |
| 149 | llSrcPartition: bazel.LabelPartition{Extensions: []string{".ll"}}, |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 150 | // C++ is the "catch-all" group, and comprises generated sources because we don't |
| 151 | // know the language of these sources until the genrule is executed. |
Trevor Radcliffe | cee4e05 | 2022-09-06 19:31:25 +0000 | [diff] [blame] | 152 | cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true}, |
| 153 | syspropSrcPartition: bazel.LabelPartition{Extensions: []string{".sysprop"}}, |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 154 | } |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 155 | |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 156 | return bazel.PartitionLabelListAttribute(ctx, &srcs, labels) |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 159 | // bp2BuildParseLibProps returns the attributes for a variant of a cc_library. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 160 | func bp2BuildParseLibProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) staticOrSharedAttributes { |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 161 | lib, ok := module.compiler.(*libraryDecorator) |
| 162 | if !ok { |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 163 | return staticOrSharedAttributes{} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 164 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 165 | return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic) |
| 166 | } |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 167 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 168 | // bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 169 | 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] | 170 | return bp2BuildParseLibProps(ctx, module, false) |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | // bp2buildParseStaticProps returns the attributes for the static variant of a cc_library. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 174 | 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] | 175 | return bp2BuildParseLibProps(ctx, module, true) |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 176 | } |
| 177 | |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 178 | type depsPartition struct { |
| 179 | export bazel.LabelList |
| 180 | implementation bazel.LabelList |
| 181 | } |
| 182 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 183 | type bazelLabelForDepsFn func(android.BazelConversionPathContext, []string) bazel.LabelList |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 184 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 185 | 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] | 186 | if !exportsDeps { |
| 187 | return depsPartition{ |
| 188 | implementation: fn(ctx, allDeps), |
| 189 | } |
| 190 | } |
| 191 | |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 192 | implementation, export := android.FilterList(allDeps, exportedDeps) |
| 193 | |
| 194 | return depsPartition{ |
| 195 | export: fn(ctx, export), |
| 196 | implementation: fn(ctx, implementation), |
| 197 | } |
| 198 | } |
| 199 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 200 | type bazelLabelForDepsExcludesFn func(android.BazelConversionPathContext, []string, []string) bazel.LabelList |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 201 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 202 | 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] | 203 | if !exportsDeps { |
| 204 | return depsPartition{ |
| 205 | implementation: fn(ctx, allDeps, excludes), |
| 206 | } |
| 207 | } |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 208 | implementation, export := android.FilterList(allDeps, exportedDeps) |
| 209 | |
| 210 | return depsPartition{ |
| 211 | export: fn(ctx, export, excludes), |
| 212 | implementation: fn(ctx, implementation, excludes), |
| 213 | } |
| 214 | } |
| 215 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 216 | func bp2BuildPropParseHelper(ctx android.ArchVariantContext, module *Module, propsType interface{}, parseFunc func(axis bazel.ConfigurationAxis, config string, props interface{})) { |
| 217 | for axis, configToProps := range module.GetArchVariantProperties(ctx, propsType) { |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 218 | for cfg, props := range configToProps { |
| 219 | parseFunc(axis, cfg, props) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 224 | // Parses properties common to static and shared libraries. Also used for prebuilt libraries. |
Liz Kammer | aceec25 | 2023-03-24 09:46:36 -0400 | [diff] [blame^] | 225 | func bp2buildParseStaticOrSharedProps(ctx android.BazelConversionPathContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes { |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 226 | attrs := staticOrSharedAttributes{} |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 227 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 228 | setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) { |
Trevor Radcliffe | ea6a45d | 2022-09-20 18:58:01 +0000 | [diff] [blame] | 229 | attrs.Copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, filterOutStdFlag)) |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 230 | attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs)) |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 231 | 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] | 232 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 233 | 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] | 234 | attrs.Deps.SetSelectValue(axis, config, staticDeps.export) |
| 235 | attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation) |
| 236 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 237 | 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] | 238 | attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export) |
| 239 | attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation) |
| 240 | |
| 241 | 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] | 242 | attrs.Enabled.SetSelectValue(axis, config, props.Enabled) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 243 | } |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 244 | // system_dynamic_deps distinguishes between nil/empty list behavior: |
| 245 | // nil -> use default values |
| 246 | // empty list -> no values specified |
| 247 | attrs.System_dynamic_deps.ForceSpecifyEmptyList = true |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 248 | |
Liz Kammer | aceec25 | 2023-03-24 09:46:36 -0400 | [diff] [blame^] | 249 | var apexAvailable []string |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 250 | if isStatic { |
Liz Kammer | aceec25 | 2023-03-24 09:46:36 -0400 | [diff] [blame^] | 251 | apexAvailable = lib.StaticProperties.Static.Apex_available |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 252 | bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 253 | if staticOrSharedProps, ok := props.(*StaticProperties); ok { |
| 254 | setAttrs(axis, config, staticOrSharedProps.Static) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 255 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 256 | }) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 257 | } else { |
Liz Kammer | aceec25 | 2023-03-24 09:46:36 -0400 | [diff] [blame^] | 258 | apexAvailable = lib.SharedProperties.Shared.Apex_available |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 259 | bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 260 | if staticOrSharedProps, ok := props.(*SharedProperties); ok { |
| 261 | setAttrs(axis, config, staticOrSharedProps.Shared) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 262 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 263 | }) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 266 | partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs) |
| 267 | attrs.Srcs = partitionedSrcs[cppSrcPartition] |
| 268 | attrs.Srcs_c = partitionedSrcs[cSrcPartition] |
| 269 | attrs.Srcs_as = partitionedSrcs[asSrcPartition] |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 270 | |
Liz Kammer | aceec25 | 2023-03-24 09:46:36 -0400 | [diff] [blame^] | 271 | attrs.Apex_available = android.ConvertApexAvailableToTags(apexAvailable) |
| 272 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 273 | if !partitionedSrcs[protoSrcPartition].IsEmpty() { |
| 274 | // TODO(b/208815215): determine whether this is used and add support if necessary |
| 275 | ctx.ModuleErrorf("Migrating static/shared only proto srcs is not currently supported") |
| 276 | } |
| 277 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 278 | return attrs |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 281 | // Convenience struct to hold all attributes parsed from prebuilt properties. |
| 282 | type prebuiltAttributes struct { |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 283 | Src bazel.LabelAttribute |
| 284 | Enabled bazel.BoolAttribute |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 285 | } |
| 286 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 287 | func parseSrc(ctx android.BazelConversionPathContext, srcLabelAttribute *bazel.LabelAttribute, axis bazel.ConfigurationAxis, config string, srcs []string) { |
| 288 | srcFileError := func() { |
| 289 | ctx.ModuleErrorf("parseSrc: Expected at most one source file for %s %s\n", axis, config) |
| 290 | } |
| 291 | if len(srcs) > 1 { |
| 292 | srcFileError() |
| 293 | return |
| 294 | } else if len(srcs) == 0 { |
| 295 | return |
| 296 | } |
| 297 | if srcLabelAttribute.SelectValue(axis, config) != nil { |
| 298 | srcFileError() |
| 299 | return |
| 300 | } |
| 301 | srcLabelAttribute.SetSelectValue(axis, config, android.BazelLabelForModuleSrcSingle(ctx, srcs[0])) |
| 302 | } |
| 303 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 304 | // 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] | 305 | func Bp2BuildParsePrebuiltLibraryProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) prebuiltAttributes { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 306 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 307 | var srcLabelAttribute bazel.LabelAttribute |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 308 | bp2BuildPropParseHelper(ctx, module, &prebuiltLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 309 | if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 310 | parseSrc(ctx, &srcLabelAttribute, axis, config, prebuiltLinkerProperties.Srcs) |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 311 | } |
| 312 | }) |
| 313 | |
| 314 | var enabledLabelAttribute bazel.BoolAttribute |
| 315 | parseAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) { |
| 316 | if props.Enabled != nil { |
| 317 | enabledLabelAttribute.SetSelectValue(axis, config, props.Enabled) |
| 318 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 319 | parseSrc(ctx, &srcLabelAttribute, axis, config, props.Srcs) |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | if isStatic { |
| 323 | bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 324 | if staticProperties, ok := props.(*StaticProperties); ok { |
| 325 | parseAttrs(axis, config, staticProperties.Static) |
| 326 | } |
| 327 | }) |
| 328 | } else { |
| 329 | bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 330 | if sharedProperties, ok := props.(*SharedProperties); ok { |
| 331 | parseAttrs(axis, config, sharedProperties.Shared) |
| 332 | } |
| 333 | }) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 334 | } |
| 335 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 336 | return prebuiltAttributes{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 337 | Src: srcLabelAttribute, |
| 338 | Enabled: enabledLabelAttribute, |
| 339 | } |
| 340 | } |
| 341 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 342 | func bp2BuildParsePrebuiltBinaryProps(ctx android.BazelConversionPathContext, module *Module) prebuiltAttributes { |
| 343 | var srcLabelAttribute bazel.LabelAttribute |
| 344 | bp2BuildPropParseHelper(ctx, module, &prebuiltLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 345 | if props, ok := props.(*prebuiltLinkerProperties); ok { |
| 346 | parseSrc(ctx, &srcLabelAttribute, axis, config, props.Srcs) |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 347 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 348 | }) |
| 349 | |
| 350 | return prebuiltAttributes{ |
| 351 | Src: srcLabelAttribute, |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Colin Cross | c5075e9 | 2022-12-05 16:46:39 -0800 | [diff] [blame] | 355 | func bp2BuildParsePrebuiltObjectProps(ctx android.BazelConversionPathContext, module *Module) prebuiltAttributes { |
| 356 | var srcLabelAttribute bazel.LabelAttribute |
| 357 | bp2BuildPropParseHelper(ctx, module, &prebuiltObjectProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 358 | if props, ok := props.(*prebuiltObjectProperties); ok { |
| 359 | parseSrc(ctx, &srcLabelAttribute, axis, config, props.Srcs) |
| 360 | } |
| 361 | }) |
| 362 | |
| 363 | return prebuiltAttributes{ |
| 364 | Src: srcLabelAttribute, |
| 365 | } |
| 366 | } |
| 367 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 368 | type baseAttributes struct { |
| 369 | compilerAttributes |
| 370 | linkerAttributes |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 371 | |
Trevor Radcliffe | db7e026 | 2022-10-28 16:48:18 +0000 | [diff] [blame] | 372 | // A combination of compilerAttributes.features and linkerAttributes.features, as well as sanitizer features |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 373 | features bazel.StringListAttribute |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 374 | protoDependency *bazel.LabelAttribute |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 375 | aidlDependency *bazel.LabelAttribute |
Yu Liu | f01a0f0 | 2022-12-07 15:45:30 -0800 | [diff] [blame] | 376 | Native_coverage *bool |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 377 | } |
| 378 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 379 | // Convenience struct to hold all attributes parsed from compiler properties. |
| 380 | type compilerAttributes struct { |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 381 | // Options for all languages |
| 382 | copts bazel.StringListAttribute |
| 383 | // Assembly options and sources |
| 384 | asFlags bazel.StringListAttribute |
| 385 | asSrcs bazel.LabelListAttribute |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 386 | asmSrcs bazel.LabelListAttribute |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 387 | // C options and sources |
| 388 | conlyFlags bazel.StringListAttribute |
| 389 | cSrcs bazel.LabelListAttribute |
| 390 | // C++ options and sources |
| 391 | cppFlags bazel.StringListAttribute |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 392 | srcs bazel.LabelListAttribute |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 393 | |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 394 | // Lex sources and options |
| 395 | lSrcs bazel.LabelListAttribute |
| 396 | llSrcs bazel.LabelListAttribute |
| 397 | lexopts bazel.StringListAttribute |
| 398 | |
Trevor Radcliffe | cee4e05 | 2022-09-06 19:31:25 +0000 | [diff] [blame] | 399 | // Sysprop sources |
| 400 | syspropSrcs bazel.LabelListAttribute |
| 401 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 402 | hdrs bazel.LabelListAttribute |
| 403 | |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 404 | rtti bazel.BoolAttribute |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 405 | |
| 406 | // Not affected by arch variants |
| 407 | stl *string |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 408 | cStd *string |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 409 | cppStd *string |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 410 | |
| 411 | localIncludes bazel.StringListAttribute |
| 412 | absoluteIncludes bazel.StringListAttribute |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 413 | |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 414 | includes BazelIncludes |
| 415 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 416 | protoSrcs bazel.LabelListAttribute |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 417 | aidlSrcs bazel.LabelListAttribute |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 418 | |
| 419 | stubsSymbolFile *string |
| 420 | stubsVersions bazel.StringListAttribute |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 421 | |
| 422 | 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] | 423 | |
| 424 | suffix bazel.StringAttribute |
Vinh Tran | 99270ea | 2022-11-28 11:15:23 -0500 | [diff] [blame] | 425 | |
| 426 | fdoProfile bazel.LabelAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Liz Kammer | cac7f69 | 2021-12-16 14:19:32 -0500 | [diff] [blame] | 429 | type filterOutFn func(string) bool |
| 430 | |
| 431 | func filterOutStdFlag(flag string) bool { |
| 432 | return strings.HasPrefix(flag, "-std=") |
| 433 | } |
| 434 | |
Alix | 1be00d4 | 2022-05-16 22:56:04 +0000 | [diff] [blame] | 435 | func filterOutClangUnknownCflags(flag string) bool { |
| 436 | for _, f := range config.ClangUnknownCflags { |
| 437 | if f == flag { |
| 438 | return true |
| 439 | } |
| 440 | } |
| 441 | return false |
| 442 | } |
| 443 | |
Trevor Radcliffe | ea6a45d | 2022-09-20 18:58:01 +0000 | [diff] [blame] | 444 | func parseCommandLineFlags(soongFlags []string, filterOut ...filterOutFn) []string { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 445 | var result []string |
| 446 | for _, flag := range soongFlags { |
Alix | 1be00d4 | 2022-05-16 22:56:04 +0000 | [diff] [blame] | 447 | skipFlag := false |
| 448 | for _, filter := range filterOut { |
| 449 | if filter != nil && filter(flag) { |
| 450 | skipFlag = true |
| 451 | } |
| 452 | } |
| 453 | if skipFlag { |
Liz Kammer | cac7f69 | 2021-12-16 14:19:32 -0500 | [diff] [blame] | 454 | continue |
| 455 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 456 | // Soong's cflags can contain spaces, like `-include header.h`. For |
| 457 | // Bazel's copts, split them up to be compatible with the |
| 458 | // no_copts_tokenization feature. |
Trevor Radcliffe | ea6a45d | 2022-09-20 18:58:01 +0000 | [diff] [blame] | 459 | result = append(result, strings.Split(flag, " ")...) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 460 | } |
| 461 | return result |
| 462 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 463 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 464 | 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] | 465 | // If there's arch specific srcs or exclude_srcs, generate a select entry for it. |
| 466 | // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too. |
| 467 | if srcsList, ok := parseSrcs(ctx, props); ok { |
| 468 | ca.srcs.SetSelectValue(axis, config, srcsList) |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 469 | } |
| 470 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 471 | localIncludeDirs := props.Local_include_dirs |
| 472 | if axis == bazel.NoConfigAxis { |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 473 | 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] | 474 | if includeBuildDirectory(props.Include_build_directory) { |
| 475 | localIncludeDirs = append(localIncludeDirs, ".") |
Liz Kammer | 222bdcf | 2021-10-11 14:15:51 -0400 | [diff] [blame] | 476 | } |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 479 | ca.absoluteIncludes.SetSelectValue(axis, config, props.Include_dirs) |
| 480 | ca.localIncludes.SetSelectValue(axis, config, localIncludeDirs) |
| 481 | |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 482 | instructionSet := proptools.StringDefault(props.Instruction_set, "") |
| 483 | if instructionSet == "arm" { |
| 484 | ca.features.SetSelectValue(axis, config, []string{"arm_isa_arm", "-arm_isa_thumb"}) |
| 485 | } else if instructionSet != "" && instructionSet != "thumb" { |
| 486 | ctx.ModuleErrorf("Unknown value for instruction_set: %s", instructionSet) |
| 487 | } |
| 488 | |
Liz Kammer | cac7f69 | 2021-12-16 14:19:32 -0500 | [diff] [blame] | 489 | // In Soong, cflags occur on the command line before -std=<val> flag, resulting in the value being |
| 490 | // overridden. In Bazel we always allow overriding, via flags; however, this can cause |
| 491 | // incompatibilities, so we remove "-std=" flags from Cflag properties while leaving it in other |
| 492 | // cases. |
Trevor Radcliffe | ea6a45d | 2022-09-20 18:58:01 +0000 | [diff] [blame] | 493 | ca.copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, filterOutStdFlag, filterOutClangUnknownCflags)) |
| 494 | ca.asFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Asflags, nil)) |
| 495 | ca.conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Conlyflags, filterOutClangUnknownCflags)) |
| 496 | ca.cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Cppflags, filterOutClangUnknownCflags)) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 497 | ca.rtti.SetSelectValue(axis, config, props.Rtti) |
| 498 | } |
| 499 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 500 | func (ca *compilerAttributes) convertStlProps(ctx android.ArchVariantContext, module *Module) { |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 501 | bp2BuildPropParseHelper(ctx, module, &StlProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 502 | if stlProps, ok := props.(*StlProperties); ok { |
| 503 | if stlProps.Stl == nil { |
| 504 | return |
| 505 | } |
| 506 | if ca.stl == nil { |
Liz Kammer | 7128d38 | 2022-05-12 11:42:33 -0400 | [diff] [blame] | 507 | stl := deduplicateStlInput(*stlProps.Stl) |
| 508 | ca.stl = &stl |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 509 | } else if ca.stl != stlProps.Stl { |
| 510 | 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] | 511 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 512 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 513 | }) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 514 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 515 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 516 | func (ca *compilerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) { |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 517 | productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{ |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 518 | "Cflags": &ca.copts, |
| 519 | "Asflags": &ca.asFlags, |
| 520 | "CppFlags": &ca.cppFlags, |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 521 | } |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 522 | for propName, attr := range productVarPropNameToAttribute { |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 523 | if productConfigProps, exists := productVariableProps[propName]; exists { |
| 524 | for productConfigProp, prop := range productConfigProps { |
| 525 | flags, ok := prop.([]string) |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 526 | if !ok { |
| 527 | ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName)) |
| 528 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 529 | newFlags, _ := bazel.TryVariableSubstitutions(flags, productConfigProp.Name) |
| 530 | attr.SetSelectValue(productConfigProp.ConfigurationAxis(), productConfigProp.SelectKey(), newFlags) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 531 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 532 | } |
| 533 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 534 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 535 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 536 | func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, implementationHdrs bazel.LabelListAttribute) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 537 | ca.srcs.ResolveExcludes() |
| 538 | partitionedSrcs := groupSrcsByExtension(ctx, ca.srcs) |
| 539 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 540 | ca.protoSrcs = partitionedSrcs[protoSrcPartition] |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 541 | ca.aidlSrcs = partitionedSrcs[aidlSrcPartition] |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 542 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 543 | for p, lla := range partitionedSrcs { |
| 544 | // if there are no sources, there is no need for headers |
| 545 | if lla.IsEmpty() { |
| 546 | continue |
| 547 | } |
| 548 | lla.Append(implementationHdrs) |
| 549 | partitionedSrcs[p] = lla |
| 550 | } |
| 551 | |
| 552 | ca.srcs = partitionedSrcs[cppSrcPartition] |
| 553 | ca.cSrcs = partitionedSrcs[cSrcPartition] |
| 554 | ca.asSrcs = partitionedSrcs[asSrcPartition] |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 555 | ca.asmSrcs = partitionedSrcs[asmSrcPartition] |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 556 | ca.lSrcs = partitionedSrcs[lSrcPartition] |
| 557 | ca.llSrcs = partitionedSrcs[llSrcPartition] |
Trevor Radcliffe | cee4e05 | 2022-09-06 19:31:25 +0000 | [diff] [blame] | 558 | ca.syspropSrcs = partitionedSrcs[syspropSrcPartition] |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 559 | |
| 560 | ca.absoluteIncludes.DeduplicateAxesFromBase() |
| 561 | ca.localIncludes.DeduplicateAxesFromBase() |
| 562 | } |
| 563 | |
| 564 | // Parse srcs from an arch or OS's props value. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 565 | func parseSrcs(ctx android.BazelConversionPathContext, props *BaseCompilerProperties) (bazel.LabelList, bool) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 566 | anySrcs := false |
| 567 | // Add srcs-like dependencies such as generated files. |
| 568 | // First create a LabelList containing these dependencies, then merge the values with srcs. |
| 569 | generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources) |
| 570 | if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 { |
| 571 | anySrcs = true |
| 572 | } |
| 573 | |
| 574 | allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs) |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 575 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 576 | if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 { |
| 577 | anySrcs = true |
| 578 | } |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 579 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 580 | return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs |
| 581 | } |
| 582 | |
Liz Kammer | a5a29de | 2022-05-25 23:19:37 -0400 | [diff] [blame] | 583 | func bp2buildStdVal(std *string, prefix string, useGnu bool) *string { |
| 584 | defaultVal := prefix + "_std_default" |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 585 | // If c{,pp}std properties are not specified, don't generate them in the BUILD file. |
| 586 | // Defaults are handled by the toolchain definition. |
| 587 | // 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] | 588 | stdVal := proptools.StringDefault(std, defaultVal) |
| 589 | if stdVal == "experimental" || stdVal == defaultVal { |
| 590 | if stdVal == "experimental" { |
| 591 | stdVal = prefix + "_std_experimental" |
| 592 | } |
| 593 | if !useGnu { |
| 594 | stdVal += "_no_gnu" |
| 595 | } |
| 596 | } else if !useGnu { |
| 597 | stdVal = gnuToCReplacer.Replace(stdVal) |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 598 | } |
| 599 | |
Liz Kammer | a5a29de | 2022-05-25 23:19:37 -0400 | [diff] [blame] | 600 | if stdVal == defaultVal { |
| 601 | return nil |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 602 | } |
Liz Kammer | a5a29de | 2022-05-25 23:19:37 -0400 | [diff] [blame] | 603 | return &stdVal |
| 604 | } |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 605 | |
Liz Kammer | a5a29de | 2022-05-25 23:19:37 -0400 | [diff] [blame] | 606 | func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) { |
| 607 | useGnu := useGnuExtensions(gnu_extensions) |
| 608 | |
| 609 | return bp2buildStdVal(c_std, "c", useGnu), bp2buildStdVal(cpp_std, "cpp", useGnu) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 610 | } |
| 611 | |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 612 | // packageFromLabel extracts package from a fully-qualified or relative Label and whether the label |
| 613 | // is fully-qualified. |
| 614 | // e.g. fully-qualified "//a/b:foo" -> "a/b", true, relative: ":bar" -> ".", false |
| 615 | func packageFromLabel(label string) (string, bool) { |
| 616 | split := strings.Split(label, ":") |
| 617 | if len(split) != 2 { |
| 618 | return "", false |
| 619 | } |
| 620 | if split[0] == "" { |
| 621 | return ".", false |
| 622 | } |
| 623 | // remove leading "//" |
| 624 | return split[0][2:], true |
| 625 | } |
| 626 | |
| 627 | // includesFromLabelList extracts relative/absolute includes from a bazel.LabelList> |
| 628 | func includesFromLabelList(labelList bazel.LabelList) (relative, absolute []string) { |
| 629 | for _, hdr := range labelList.Includes { |
| 630 | if pkg, hasPkg := packageFromLabel(hdr.Label); hasPkg { |
| 631 | absolute = append(absolute, pkg) |
| 632 | } else if pkg != "" { |
| 633 | relative = append(relative, pkg) |
| 634 | } |
| 635 | } |
| 636 | return relative, absolute |
| 637 | } |
| 638 | |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 639 | type YasmAttributes struct { |
| 640 | Srcs bazel.LabelListAttribute |
| 641 | Flags bazel.StringListAttribute |
| 642 | Include_dirs bazel.StringListAttribute |
| 643 | } |
| 644 | |
| 645 | func bp2BuildYasm(ctx android.Bp2buildMutatorContext, m *Module, ca compilerAttributes) *bazel.LabelAttribute { |
| 646 | if ca.asmSrcs.IsEmpty() { |
| 647 | return nil |
| 648 | } |
| 649 | |
| 650 | // Yasm needs the include directories from both local_includes and |
| 651 | // export_include_dirs. We don't care about actually exporting them from the |
| 652 | // yasm rule though, because they will also be present on the cc_ rule that |
| 653 | // wraps this yasm rule. |
| 654 | includes := ca.localIncludes.Clone() |
| 655 | bp2BuildPropParseHelper(ctx, m, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 656 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
| 657 | if len(flagExporterProperties.Export_include_dirs) > 0 { |
| 658 | x := bazel.StringListAttribute{} |
| 659 | x.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs) |
| 660 | includes.Append(x) |
| 661 | } |
| 662 | } |
| 663 | }) |
| 664 | |
| 665 | ctx.CreateBazelTargetModule( |
| 666 | bazel.BazelTargetModuleProperties{ |
| 667 | Rule_class: "yasm", |
| 668 | Bzl_load_location: "//build/bazel/rules/cc:yasm.bzl", |
| 669 | }, |
| 670 | android.CommonAttributes{Name: m.Name() + "_yasm"}, |
| 671 | &YasmAttributes{ |
| 672 | Srcs: ca.asmSrcs, |
| 673 | Flags: ca.asFlags, |
| 674 | Include_dirs: *includes, |
| 675 | }) |
| 676 | |
| 677 | // We only want to add a dependency on the _yasm target if there are asm |
| 678 | // sources in the current configuration. If there are unconfigured asm |
| 679 | // sources, always add the dependency. Otherwise, add the dependency only |
| 680 | // on the configuration axes and values that had asm sources. |
| 681 | if len(ca.asmSrcs.Value.Includes) > 0 { |
| 682 | return bazel.MakeLabelAttribute(":" + m.Name() + "_yasm") |
| 683 | } |
| 684 | |
| 685 | ret := &bazel.LabelAttribute{} |
| 686 | for _, axis := range ca.asmSrcs.SortedConfigurationAxes() { |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 687 | for cfg := range ca.asmSrcs.ConfigurableValues[axis] { |
| 688 | ret.SetSelectValue(axis, cfg, bazel.Label{Label: ":" + m.Name() + "_yasm"}) |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | return ret |
| 692 | } |
| 693 | |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 694 | // bp2BuildParseBaseProps returns all compiler, linker, library attributes of a cc module.. |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 695 | func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) baseAttributes { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 696 | archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) |
| 697 | archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 698 | archVariantLibraryProperties := module.GetArchVariantProperties(ctx, &LibraryProperties{}) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 699 | |
| 700 | var implementationHdrs bazel.LabelListAttribute |
| 701 | |
| 702 | axisToConfigs := map[bazel.ConfigurationAxis]map[string]bool{} |
| 703 | allAxesAndConfigs := func(cp android.ConfigurationAxisToArchVariantProperties) { |
| 704 | for axis, configMap := range cp { |
| 705 | if _, ok := axisToConfigs[axis]; !ok { |
| 706 | axisToConfigs[axis] = map[string]bool{} |
| 707 | } |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 708 | for cfg := range configMap { |
| 709 | axisToConfigs[axis][cfg] = true |
Chris Parsons | a967f25 | 2021-09-23 16:34:35 -0400 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 713 | allAxesAndConfigs(archVariantCompilerProps) |
| 714 | allAxesAndConfigs(archVariantLinkerProps) |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 715 | allAxesAndConfigs(archVariantLibraryProperties) |
Chris Parsons | a967f25 | 2021-09-23 16:34:35 -0400 | [diff] [blame] | 716 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 717 | compilerAttrs := compilerAttributes{} |
| 718 | linkerAttrs := linkerAttributes{} |
| 719 | |
Chris Parsons | 7b3289b | 2023-01-26 17:30:44 -0500 | [diff] [blame] | 720 | // Iterate through these axes in a deterministic order. This is required |
| 721 | // because processing certain dependencies may result in concatenating |
| 722 | // elements along other axes. (For example, processing NoConfig may result |
| 723 | // in elements being added to InApex). This is thus the only way to ensure |
| 724 | // that the order of entries in each list is in a predictable order. |
| 725 | for _, axis := range bazel.SortedConfigurationAxes(axisToConfigs) { |
| 726 | configs := axisToConfigs[axis] |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 727 | for cfg := range configs { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 728 | var allHdrs []string |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 729 | if baseCompilerProps, ok := archVariantCompilerProps[axis][cfg].(*BaseCompilerProperties); ok { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 730 | allHdrs = baseCompilerProps.Generated_headers |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 731 | if baseCompilerProps.Lex != nil { |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 732 | compilerAttrs.lexopts.SetSelectValue(axis, cfg, baseCompilerProps.Lex.Flags) |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 733 | } |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 734 | (&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, cfg, baseCompilerProps) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | var exportHdrs []string |
| 738 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 739 | if baseLinkerProps, ok := archVariantLinkerProps[axis][cfg].(*BaseLinkerProperties); ok { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 740 | exportHdrs = baseLinkerProps.Export_generated_headers |
| 741 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 742 | (&linkerAttrs).bp2buildForAxisAndConfig(ctx, module.Binary(), axis, cfg, baseLinkerProps) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 743 | } |
| 744 | headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportHdrs, android.BazelLabelForModuleDeps) |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 745 | implementationHdrs.SetSelectValue(axis, cfg, headers.implementation) |
| 746 | compilerAttrs.hdrs.SetSelectValue(axis, cfg, headers.export) |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 747 | |
| 748 | exportIncludes, exportAbsoluteIncludes := includesFromLabelList(headers.export) |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 749 | compilerAttrs.includes.Includes.SetSelectValue(axis, cfg, exportIncludes) |
| 750 | compilerAttrs.includes.AbsoluteIncludes.SetSelectValue(axis, cfg, exportAbsoluteIncludes) |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 751 | |
| 752 | includes, absoluteIncludes := includesFromLabelList(headers.implementation) |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 753 | currAbsoluteIncludes := compilerAttrs.absoluteIncludes.SelectValue(axis, cfg) |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 754 | currAbsoluteIncludes = android.FirstUniqueStrings(append(currAbsoluteIncludes, absoluteIncludes...)) |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 755 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 756 | compilerAttrs.absoluteIncludes.SetSelectValue(axis, cfg, currAbsoluteIncludes) |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 757 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 758 | currIncludes := compilerAttrs.localIncludes.SelectValue(axis, cfg) |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 759 | currIncludes = android.FirstUniqueStrings(append(currIncludes, includes...)) |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 760 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 761 | compilerAttrs.localIncludes.SetSelectValue(axis, cfg, currIncludes) |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 762 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 763 | if libraryProps, ok := archVariantLibraryProperties[axis][cfg].(*LibraryProperties); ok { |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 764 | if axis == bazel.NoConfigAxis { |
| 765 | compilerAttrs.stubsSymbolFile = libraryProps.Stubs.Symbol_file |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 766 | compilerAttrs.stubsVersions.SetSelectValue(axis, cfg, libraryProps.Stubs.Versions) |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 767 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a56e970 | 2022-02-23 18:39:59 -0500 | [diff] [blame] | 768 | if suffix := libraryProps.Suffix; suffix != nil { |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 769 | compilerAttrs.suffix.SetSelectValue(axis, cfg, suffix) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a56e970 | 2022-02-23 18:39:59 -0500 | [diff] [blame] | 770 | } |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 771 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 772 | } |
| 773 | } |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 774 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 775 | compilerAttrs.convertStlProps(ctx, module) |
| 776 | (&linkerAttrs).convertStripProps(ctx, module) |
| 777 | |
Yu Liu | f01a0f0 | 2022-12-07 15:45:30 -0800 | [diff] [blame] | 778 | var nativeCoverage *bool |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 779 | if module.coverage != nil && module.coverage.Properties.Native_coverage != nil && |
| 780 | !Bool(module.coverage.Properties.Native_coverage) { |
Yu Liu | f01a0f0 | 2022-12-07 15:45:30 -0800 | [diff] [blame] | 781 | nativeCoverage = BoolPtr(false) |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Cole Faust | 912bc88 | 2023-03-08 12:29:50 -0800 | [diff] [blame] | 784 | productVariableProps := android.ProductVariableProperties(ctx, ctx.Module()) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 785 | |
| 786 | (&compilerAttrs).convertProductVariables(ctx, productVariableProps) |
| 787 | (&linkerAttrs).convertProductVariables(ctx, productVariableProps) |
| 788 | |
| 789 | (&compilerAttrs).finalize(ctx, implementationHdrs) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 790 | (&linkerAttrs).finalize(ctx) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 791 | |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 792 | (&compilerAttrs.srcs).Add(bp2BuildYasm(ctx, module, compilerAttrs)) |
| 793 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 794 | protoDep := bp2buildProto(ctx, module, compilerAttrs.protoSrcs) |
| 795 | |
| 796 | // bp2buildProto will only set wholeStaticLib or implementationWholeStaticLib, but we don't know |
| 797 | // which. This will add the newly generated proto library to the appropriate attribute and nothing |
| 798 | // to the other |
| 799 | (&linkerAttrs).wholeArchiveDeps.Add(protoDep.wholeStaticLib) |
| 800 | (&linkerAttrs).implementationWholeArchiveDeps.Add(protoDep.implementationWholeStaticLib) |
Vinh Tran | fde57eb | 2022-08-29 17:46:58 -0400 | [diff] [blame] | 801 | |
Vinh Tran | 395a1e9 | 2022-09-16 18:27:29 -0400 | [diff] [blame] | 802 | aidlDep := bp2buildCcAidlLibrary(ctx, module, compilerAttrs.aidlSrcs, linkerAttrs) |
Vinh Tran | fde57eb | 2022-08-29 17:46:58 -0400 | [diff] [blame] | 803 | if aidlDep != nil { |
| 804 | if lib, ok := module.linker.(*libraryDecorator); ok { |
| 805 | if proptools.Bool(lib.Properties.Aidl.Export_aidl_headers) { |
| 806 | (&linkerAttrs).wholeArchiveDeps.Add(aidlDep) |
| 807 | } else { |
| 808 | (&linkerAttrs).implementationWholeArchiveDeps.Add(aidlDep) |
| 809 | } |
| 810 | } |
| 811 | } |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 812 | |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 813 | convertedLSrcs := bp2BuildLex(ctx, module.Name(), compilerAttrs) |
| 814 | (&compilerAttrs).srcs.Add(&convertedLSrcs.srcName) |
| 815 | (&compilerAttrs).cSrcs.Add(&convertedLSrcs.cSrcName) |
| 816 | |
Vinh Tran | 99270ea | 2022-11-28 11:15:23 -0500 | [diff] [blame] | 817 | if module.afdo != nil && module.afdo.Properties.Afdo { |
| 818 | fdoProfileDep := bp2buildFdoProfile(ctx, module) |
| 819 | if fdoProfileDep != nil { |
| 820 | (&compilerAttrs).fdoProfile.SetValue(*fdoProfileDep) |
| 821 | } |
| 822 | } |
| 823 | |
Trevor Radcliffe | cee4e05 | 2022-09-06 19:31:25 +0000 | [diff] [blame] | 824 | if !compilerAttrs.syspropSrcs.IsEmpty() { |
| 825 | (&linkerAttrs).wholeArchiveDeps.Add(bp2buildCcSysprop(ctx, module.Name(), module.Properties.Min_sdk_version, compilerAttrs.syspropSrcs)) |
| 826 | } |
| 827 | |
Zi Wang | 9f609db | 2023-01-04 11:06:54 -0800 | [diff] [blame] | 828 | linkerAttrs.wholeArchiveDeps.Prepend = true |
| 829 | linkerAttrs.deps.Prepend = true |
| 830 | compilerAttrs.localIncludes.Prepend = true |
| 831 | compilerAttrs.absoluteIncludes.Prepend = true |
| 832 | compilerAttrs.hdrs.Prepend = true |
| 833 | |
Trevor Radcliffe | db7e026 | 2022-10-28 16:48:18 +0000 | [diff] [blame] | 834 | features := compilerAttrs.features.Clone().Append(linkerAttrs.features).Append(bp2buildSanitizerFeatures(ctx, module)) |
Trevor Radcliffe | 56b1a2b | 2023-02-06 21:58:30 +0000 | [diff] [blame] | 835 | features = features.Append(bp2buildLtoFeatures(ctx, module)) |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 836 | features.DeduplicateAxesFromBase() |
| 837 | |
Trevor Radcliffe | 0d1b402 | 2022-12-12 22:26:34 +0000 | [diff] [blame] | 838 | addMuslSystemDynamicDeps(ctx, linkerAttrs) |
| 839 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 840 | return baseAttributes{ |
| 841 | compilerAttrs, |
| 842 | linkerAttrs, |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 843 | *features, |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 844 | protoDep.protoDep, |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 845 | aidlDep, |
Yu Liu | f01a0f0 | 2022-12-07 15:45:30 -0800 | [diff] [blame] | 846 | nativeCoverage, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | |
Trevor Radcliffe | 0d1b402 | 2022-12-12 22:26:34 +0000 | [diff] [blame] | 850 | // As a workaround for b/261657184, we are manually adding the default value |
| 851 | // of system_dynamic_deps for the linux_musl os. |
| 852 | // TODO: Solve this properly |
| 853 | func addMuslSystemDynamicDeps(ctx android.Bp2buildMutatorContext, attrs linkerAttributes) { |
| 854 | systemDynamicDeps := attrs.systemDynamicDeps.SelectValue(bazel.OsConfigurationAxis, "linux_musl") |
| 855 | if attrs.systemDynamicDeps.HasAxisSpecificValues(bazel.OsConfigurationAxis) && systemDynamicDeps.IsNil() { |
| 856 | attrs.systemDynamicDeps.SetSelectValue(bazel.OsConfigurationAxis, "linux_musl", android.BazelLabelForModuleDeps(ctx, config.MuslDefaultSharedLibraries)) |
| 857 | } |
| 858 | } |
| 859 | |
Vinh Tran | 99270ea | 2022-11-28 11:15:23 -0500 | [diff] [blame] | 860 | type fdoProfileAttributes struct { |
| 861 | Absolute_path_profile string |
| 862 | } |
| 863 | |
| 864 | func bp2buildFdoProfile( |
| 865 | ctx android.Bp2buildMutatorContext, |
| 866 | m *Module, |
| 867 | ) *bazel.Label { |
| 868 | for _, project := range globalAfdoProfileProjects { |
Vinh Tran | bc9c8b4 | 2022-12-09 12:03:52 -0500 | [diff] [blame] | 869 | // Ensure handcrafted BUILD file exists in the project |
| 870 | BUILDPath := android.ExistentPathForSource(ctx, project, "BUILD") |
| 871 | if BUILDPath.Valid() { |
| 872 | // We handcraft a BUILD file with fdo_profile targets that use the existing profiles in the project |
| 873 | // This implementation is assuming that every afdo profile in globalAfdoProfileProjects already has |
| 874 | // an associated fdo_profile target declared in the same package. |
| 875 | // TODO(b/260714900): Handle arch-specific afdo profiles (e.g. `<module-name>-arm<64>.afdo`) |
| 876 | path := android.ExistentPathForSource(ctx, project, m.Name()+".afdo") |
| 877 | if path.Valid() { |
| 878 | // FIXME: Some profiles only exist internally and are not released to AOSP. |
| 879 | // When generated BUILD files are checked in, we'll run into merge conflict. |
| 880 | // The cc_library_shared target in AOSP won't have reference to an fdo_profile target because |
| 881 | // the profile doesn't exist. Internally, the same cc_library_shared target will |
| 882 | // have reference to the fdo_profile. |
| 883 | // For more context, see b/258682955#comment2 |
| 884 | fdoProfileLabel := "//" + strings.TrimSuffix(project, "/") + ":" + m.Name() |
| 885 | return &bazel.Label{ |
| 886 | Label: fdoProfileLabel, |
| 887 | } |
Vinh Tran | 99270ea | 2022-11-28 11:15:23 -0500 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | return nil |
| 893 | } |
| 894 | |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 895 | func bp2buildCcAidlLibrary( |
| 896 | ctx android.Bp2buildMutatorContext, |
| 897 | m *Module, |
Vinh Tran | a3b8b78 | 2022-09-14 11:40:24 -0400 | [diff] [blame] | 898 | aidlLabelList bazel.LabelListAttribute, |
Vinh Tran | 395a1e9 | 2022-09-16 18:27:29 -0400 | [diff] [blame] | 899 | linkerAttrs linkerAttributes, |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 900 | ) *bazel.LabelAttribute { |
Vinh Tran | a3b8b78 | 2022-09-14 11:40:24 -0400 | [diff] [blame] | 901 | if !aidlLabelList.IsEmpty() { |
| 902 | aidlLibs, aidlSrcs := aidlLabelList.Partition(func(src bazel.Label) bool { |
| 903 | if fg, ok := android.ToFileGroupAsLibrary(ctx, src.OriginalModuleName); ok && |
| 904 | fg.ShouldConvertToAidlLibrary(ctx) { |
| 905 | return true |
| 906 | } |
| 907 | return false |
| 908 | }) |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 909 | |
Vinh Tran | a3b8b78 | 2022-09-14 11:40:24 -0400 | [diff] [blame] | 910 | if !aidlSrcs.IsEmpty() { |
| 911 | aidlLibName := m.Name() + "_aidl_library" |
| 912 | ctx.CreateBazelTargetModule( |
| 913 | bazel.BazelTargetModuleProperties{ |
| 914 | Rule_class: "aidl_library", |
| 915 | Bzl_load_location: "//build/bazel/rules/aidl:library.bzl", |
| 916 | }, |
| 917 | android.CommonAttributes{Name: aidlLibName}, |
| 918 | &aidlLibraryAttributes{ |
| 919 | Srcs: aidlSrcs, |
| 920 | }, |
| 921 | ) |
| 922 | aidlLibs.Add(&bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + aidlLibName}}) |
| 923 | } |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 924 | |
Vinh Tran | a3b8b78 | 2022-09-14 11:40:24 -0400 | [diff] [blame] | 925 | if !aidlLibs.IsEmpty() { |
| 926 | ccAidlLibrarylabel := m.Name() + "_cc_aidl_library" |
Sam Delmerico | 512437b | 2023-03-17 11:34:15 -0400 | [diff] [blame] | 927 | // Since parent cc_library already has these dependencies, we can add them as implementation |
| 928 | // deps so that they don't re-export |
| 929 | implementationDeps := linkerAttrs.deps.Clone() |
| 930 | implementationDeps.Append(linkerAttrs.implementationDeps) |
| 931 | implementationDynamicDeps := linkerAttrs.dynamicDeps.Clone() |
| 932 | implementationDynamicDeps.Append(linkerAttrs.implementationDynamicDeps) |
Vinh Tran | 395a1e9 | 2022-09-16 18:27:29 -0400 | [diff] [blame] | 933 | |
Vinh Tran | a3b8b78 | 2022-09-14 11:40:24 -0400 | [diff] [blame] | 934 | ctx.CreateBazelTargetModule( |
| 935 | bazel.BazelTargetModuleProperties{ |
| 936 | Rule_class: "cc_aidl_library", |
| 937 | Bzl_load_location: "//build/bazel/rules/cc:cc_aidl_library.bzl", |
| 938 | }, |
| 939 | android.CommonAttributes{Name: ccAidlLibrarylabel}, |
| 940 | &ccAidlLibraryAttributes{ |
Vinh Tran | 395a1e9 | 2022-09-16 18:27:29 -0400 | [diff] [blame] | 941 | Deps: aidlLibs, |
Sam Delmerico | 512437b | 2023-03-17 11:34:15 -0400 | [diff] [blame] | 942 | Implementation_deps: *implementationDeps, |
| 943 | Implementation_dynamic_deps: *implementationDynamicDeps, |
Vinh Tran | a3b8b78 | 2022-09-14 11:40:24 -0400 | [diff] [blame] | 944 | }, |
| 945 | ) |
| 946 | label := &bazel.LabelAttribute{ |
| 947 | Value: &bazel.Label{ |
| 948 | Label: ":" + ccAidlLibrarylabel, |
| 949 | }, |
| 950 | } |
| 951 | return label |
| 952 | } |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 953 | } |
| 954 | |
Vinh Tran | a3b8b78 | 2022-09-14 11:40:24 -0400 | [diff] [blame] | 955 | return nil |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 956 | } |
| 957 | |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 958 | func bp2BuildParseSdkAttributes(module *Module) sdkAttributes { |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 959 | return sdkAttributes{ |
| 960 | Sdk_version: module.Properties.Sdk_version, |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 961 | Min_sdk_version: module.Properties.Min_sdk_version, |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | type sdkAttributes struct { |
| 966 | Sdk_version *string |
| 967 | Min_sdk_version *string |
| 968 | } |
| 969 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 970 | // Convenience struct to hold all attributes parsed from linker properties. |
| 971 | type linkerAttributes struct { |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 972 | deps bazel.LabelListAttribute |
| 973 | implementationDeps bazel.LabelListAttribute |
| 974 | dynamicDeps bazel.LabelListAttribute |
| 975 | implementationDynamicDeps bazel.LabelListAttribute |
Cole Faust | 6b29f59 | 2022-08-09 09:50:56 -0700 | [diff] [blame] | 976 | runtimeDeps bazel.LabelListAttribute |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 977 | wholeArchiveDeps bazel.LabelListAttribute |
| 978 | implementationWholeArchiveDeps bazel.LabelListAttribute |
| 979 | systemDynamicDeps bazel.LabelListAttribute |
| 980 | usedSystemDynamicDepAsDynamicDep map[string]bool |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 981 | |
Rupert Shuttleworth | 484aa25 | 2021-12-10 07:22:53 -0500 | [diff] [blame] | 982 | useVersionLib bazel.BoolAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 983 | linkopts bazel.StringListAttribute |
Liz Kammer | d287118 | 2021-10-04 13:54:37 -0400 | [diff] [blame] | 984 | additionalLinkerInputs bazel.LabelListAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 985 | stripKeepSymbols bazel.BoolAttribute |
| 986 | stripKeepSymbolsAndDebugFrame bazel.BoolAttribute |
| 987 | stripKeepSymbolsList bazel.StringListAttribute |
| 988 | stripAll bazel.BoolAttribute |
| 989 | stripNone bazel.BoolAttribute |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 990 | features bazel.StringListAttribute |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 991 | } |
| 992 | |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 993 | var ( |
| 994 | soongSystemSharedLibs = []string{"libc", "libm", "libdl"} |
Liz Kammer | baced71 | 2022-09-16 09:01:29 -0400 | [diff] [blame] | 995 | versionLib = "libbuildversion" |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 996 | ) |
| 997 | |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 998 | // resolveTargetApex re-adds the shared and static libs in target.apex.exclude_shared|static_libs props to non-apex variant |
| 999 | // since all libs are already excluded by default |
Liz Kammer | 748d707 | 2023-01-25 12:07:43 -0500 | [diff] [blame] | 1000 | func (la *linkerAttributes) resolveTargetApexProp(ctx android.BazelConversionPathContext, props *BaseLinkerProperties) { |
| 1001 | excludeSharedLibs := bazelLabelForSharedDeps(ctx, props.Target.Apex.Exclude_shared_libs) |
| 1002 | sharedExcludes := bazel.LabelList{Excludes: excludeSharedLibs.Includes} |
| 1003 | sharedExcludesLabelList := bazel.LabelListAttribute{} |
| 1004 | sharedExcludesLabelList.SetSelectValue(bazel.InApexAxis, bazel.InApex, sharedExcludes) |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1005 | |
Liz Kammer | 748d707 | 2023-01-25 12:07:43 -0500 | [diff] [blame] | 1006 | la.dynamicDeps.Append(sharedExcludesLabelList) |
| 1007 | la.implementationDynamicDeps.Append(sharedExcludesLabelList) |
| 1008 | |
| 1009 | excludeStaticLibs := bazelLabelForStaticDeps(ctx, props.Target.Apex.Exclude_static_libs) |
| 1010 | staticExcludes := bazel.LabelList{Excludes: excludeStaticLibs.Includes} |
| 1011 | staticExcludesLabelList := bazel.LabelListAttribute{} |
| 1012 | staticExcludesLabelList.SetSelectValue(bazel.InApexAxis, bazel.InApex, staticExcludes) |
| 1013 | |
| 1014 | la.deps.Append(staticExcludesLabelList) |
| 1015 | la.implementationDeps.Append(staticExcludesLabelList) |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1016 | } |
| 1017 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1018 | 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] | 1019 | // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module |
| 1020 | var axisFeatures []string |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 1021 | |
Liz Kammer | cc2c1ef | 2022-03-21 09:03:29 -0400 | [diff] [blame] | 1022 | wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs) |
Liz Kammer | baced71 | 2022-09-16 09:01:29 -0400 | [diff] [blame] | 1023 | staticLibs := android.FirstUniqueStrings(android.RemoveListFromList(props.Static_libs, wholeStaticLibs)) |
| 1024 | if axis == bazel.NoConfigAxis { |
| 1025 | la.useVersionLib.SetSelectValue(axis, config, props.Use_version_lib) |
| 1026 | if proptools.Bool(props.Use_version_lib) { |
| 1027 | versionLibAlreadyInDeps := android.InList(versionLib, wholeStaticLibs) |
| 1028 | // remove from static libs so there is no duplicate dependency |
| 1029 | _, staticLibs = android.RemoveFromList(versionLib, staticLibs) |
| 1030 | // only add the dep if it is not in progress |
| 1031 | if !versionLibAlreadyInDeps { |
| 1032 | if isBinary { |
| 1033 | wholeStaticLibs = append(wholeStaticLibs, versionLib) |
| 1034 | } else { |
| 1035 | la.implementationWholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, []string{versionLib}, props.Exclude_static_libs)) |
| 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1041 | // Excludes to parallel Soong: |
| 1042 | // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0 |
Liz Kammer | baced71 | 2022-09-16 09:01:29 -0400 | [diff] [blame] | 1043 | la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs)) |
Liz Kammer | cc2c1ef | 2022-03-21 09:03:29 -0400 | [diff] [blame] | 1044 | |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1045 | staticDeps := maybePartitionExportedAndImplementationsDepsExcludes( |
| 1046 | ctx, |
| 1047 | !isBinary, |
| 1048 | staticLibs, |
Liz Kammer | 748d707 | 2023-01-25 12:07:43 -0500 | [diff] [blame] | 1049 | props.Exclude_static_libs, |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1050 | props.Export_static_lib_headers, |
| 1051 | bazelLabelForStaticDepsExcludes, |
| 1052 | ) |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 1053 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1054 | headerLibs := android.FirstUniqueStrings(props.Header_libs) |
| 1055 | hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 1056 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1057 | (&hDeps.export).Append(staticDeps.export) |
| 1058 | la.deps.SetSelectValue(axis, config, hDeps.export) |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 1059 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1060 | (&hDeps.implementation).Append(staticDeps.implementation) |
| 1061 | la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation) |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 1062 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1063 | systemSharedLibs := props.System_shared_libs |
| 1064 | // systemSharedLibs distinguishes between nil/empty list behavior: |
| 1065 | // nil -> use default values |
| 1066 | // empty list -> no values specified |
| 1067 | if len(systemSharedLibs) > 0 { |
| 1068 | systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs) |
| 1069 | } |
| 1070 | la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs)) |
| 1071 | |
| 1072 | sharedLibs := android.FirstUniqueStrings(props.Shared_libs) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1073 | excludeSharedLibs := props.Exclude_shared_libs |
| 1074 | usedSystem := android.FilterListPred(sharedLibs, func(s string) bool { |
| 1075 | return android.InList(s, soongSystemSharedLibs) && !android.InList(s, excludeSharedLibs) |
| 1076 | }) |
| 1077 | for _, el := range usedSystem { |
| 1078 | if la.usedSystemDynamicDepAsDynamicDep == nil { |
| 1079 | la.usedSystemDynamicDepAsDynamicDep = map[string]bool{} |
| 1080 | } |
| 1081 | la.usedSystemDynamicDepAsDynamicDep[el] = true |
| 1082 | } |
| 1083 | |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1084 | sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes( |
| 1085 | ctx, |
| 1086 | !isBinary, |
| 1087 | sharedLibs, |
Liz Kammer | 748d707 | 2023-01-25 12:07:43 -0500 | [diff] [blame] | 1088 | props.Exclude_shared_libs, |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1089 | props.Export_shared_lib_headers, |
| 1090 | bazelLabelForSharedDepsExcludes, |
| 1091 | ) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1092 | la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export) |
| 1093 | la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation) |
Liz Kammer | 748d707 | 2023-01-25 12:07:43 -0500 | [diff] [blame] | 1094 | la.resolveTargetApexProp(ctx, props) |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1095 | |
Wei Li | 81852ca | 2022-07-27 00:22:06 -0700 | [diff] [blame] | 1096 | if axis == bazel.NoConfigAxis || (axis == bazel.OsConfigurationAxis && config == bazel.OsAndroid) { |
Yu Liu | 10174ff | 2023-02-21 12:05:26 -0800 | [diff] [blame] | 1097 | // If a dependency in la.implementationDynamicDeps or la.dynamicDeps has stubs, its |
| 1098 | // stub variant should be used when the dependency is linked in a APEX. The |
| 1099 | // dependencies in NoConfigAxis and OsConfigurationAxis/OsAndroid are grouped by |
| 1100 | // having stubs or not, so Bazel select() statement can be used to choose |
| 1101 | // source/stub variants of them. |
| 1102 | setStubsForDynamicDeps(ctx, axis, config, sharedDeps.export, &la.dynamicDeps, 0) |
| 1103 | setStubsForDynamicDeps(ctx, axis, config, sharedDeps.implementation, &la.implementationDynamicDeps, 1) |
Wei Li | 81852ca | 2022-07-27 00:22:06 -0700 | [diff] [blame] | 1104 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1105 | |
| 1106 | if !BoolDefault(props.Pack_relocations, packRelocationsDefault) { |
| 1107 | axisFeatures = append(axisFeatures, "disable_pack_relocations") |
| 1108 | } |
| 1109 | |
| 1110 | if Bool(props.Allow_undefined_symbols) { |
| 1111 | axisFeatures = append(axisFeatures, "-no_undefined_symbols") |
| 1112 | } |
| 1113 | |
| 1114 | var linkerFlags []string |
| 1115 | if len(props.Ldflags) > 0 { |
Liz Kammer | f38a837 | 2022-02-04 15:39:00 -0500 | [diff] [blame] | 1116 | linkerFlags = append(linkerFlags, proptools.NinjaEscapeList(props.Ldflags)...) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1117 | // binaries remove static flag if -shared is in the linker flags |
| 1118 | if isBinary && android.InList("-shared", linkerFlags) { |
| 1119 | axisFeatures = append(axisFeatures, "-static_flag") |
| 1120 | } |
| 1121 | } |
Trevor Radcliffe | ea6a45d | 2022-09-20 18:58:01 +0000 | [diff] [blame] | 1122 | |
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | 01ec55e | 2023-01-30 22:53:04 +0000 | [diff] [blame] | 1123 | if !props.libCrt() { |
| 1124 | axisFeatures = append(axisFeatures, "-use_libcrt") |
| 1125 | } |
| 1126 | if !props.crt() { |
| 1127 | axisFeatures = append(axisFeatures, "-link_crt") |
| 1128 | } |
| 1129 | |
Trevor Radcliffe | ea6a45d | 2022-09-20 18:58:01 +0000 | [diff] [blame] | 1130 | // This must happen before the addition of flags for Version Script and |
| 1131 | // Dynamic List, as these flags must be split on spaces and those must not |
| 1132 | linkerFlags = parseCommandLineFlags(linkerFlags, filterOutClangUnknownCflags) |
| 1133 | |
Trevor Radcliffe | 37ec2f7 | 2022-09-27 01:46:01 +0000 | [diff] [blame] | 1134 | additionalLinkerInputs := bazel.LabelList{} |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1135 | if props.Version_script != nil { |
| 1136 | label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script) |
Trevor Radcliffe | 37ec2f7 | 2022-09-27 01:46:01 +0000 | [diff] [blame] | 1137 | additionalLinkerInputs.Add(&label) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1138 | linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label)) |
| 1139 | } |
Alix | 773adaa | 2022-04-27 17:49:34 +0000 | [diff] [blame] | 1140 | |
| 1141 | if props.Dynamic_list != nil { |
| 1142 | label := android.BazelLabelForModuleSrcSingle(ctx, *props.Dynamic_list) |
Trevor Radcliffe | 37ec2f7 | 2022-09-27 01:46:01 +0000 | [diff] [blame] | 1143 | additionalLinkerInputs.Add(&label) |
Alix | 773adaa | 2022-04-27 17:49:34 +0000 | [diff] [blame] | 1144 | linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--dynamic-list,$(location %s)", label.Label)) |
| 1145 | } |
| 1146 | |
Trevor Radcliffe | 37ec2f7 | 2022-09-27 01:46:01 +0000 | [diff] [blame] | 1147 | la.additionalLinkerInputs.SetSelectValue(axis, config, additionalLinkerInputs) |
Trevor Radcliffe | ea6a45d | 2022-09-20 18:58:01 +0000 | [diff] [blame] | 1148 | la.linkopts.SetSelectValue(axis, config, linkerFlags) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1149 | |
| 1150 | if axisFeatures != nil { |
| 1151 | la.features.SetSelectValue(axis, config, axisFeatures) |
| 1152 | } |
Cole Faust | 6b29f59 | 2022-08-09 09:50:56 -0700 | [diff] [blame] | 1153 | |
| 1154 | runtimeDeps := android.BazelLabelForModuleDepsExcludes(ctx, props.Runtime_libs, props.Exclude_runtime_libs) |
| 1155 | if !runtimeDeps.IsEmpty() { |
| 1156 | la.runtimeDeps.SetSelectValue(axis, config, runtimeDeps) |
| 1157 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1158 | } |
| 1159 | |
Spandan Das | 2518c02 | 2023-03-17 03:02:32 +0000 | [diff] [blame] | 1160 | var ( |
| 1161 | apiSurfaceModuleLibCurrentPackage = "@api_surfaces//" + android.ModuleLibApi.String() + "/current:" |
| 1162 | ) |
| 1163 | |
Yu Liu | 10174ff | 2023-02-21 12:05:26 -0800 | [diff] [blame] | 1164 | func setStubsForDynamicDeps(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, |
| 1165 | config string, dynamicLibs bazel.LabelList, dynamicDeps *bazel.LabelListAttribute, ind int) { |
| 1166 | depsWithStubs := []bazel.Label{} |
| 1167 | for _, l := range dynamicLibs.Includes { |
| 1168 | dep, _ := ctx.ModuleFromName(l.OriginalModuleName) |
| 1169 | if m, ok := dep.(*Module); ok && m.HasStubsVariants() { |
| 1170 | depsWithStubs = append(depsWithStubs, l) |
| 1171 | } |
| 1172 | } |
| 1173 | if len(depsWithStubs) > 0 { |
| 1174 | implDynamicDeps := bazel.SubtractBazelLabelList(dynamicLibs, bazel.MakeLabelList(depsWithStubs)) |
| 1175 | dynamicDeps.SetSelectValue(axis, config, implDynamicDeps) |
| 1176 | |
| 1177 | stubLibLabels := []bazel.Label{} |
| 1178 | for _, l := range depsWithStubs { |
Spandan Das | 2518c02 | 2023-03-17 03:02:32 +0000 | [diff] [blame] | 1179 | stubLabelInApiSurfaces := bazel.Label{ |
| 1180 | Label: apiSurfaceModuleLibCurrentPackage + l.OriginalModuleName, |
| 1181 | } |
| 1182 | stubLibLabels = append(stubLibLabels, stubLabelInApiSurfaces) |
Yu Liu | 10174ff | 2023-02-21 12:05:26 -0800 | [diff] [blame] | 1183 | } |
| 1184 | inApexSelectValue := dynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex) |
| 1185 | nonApexSelectValue := dynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex) |
| 1186 | defaultSelectValue := dynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey) |
| 1187 | if axis == bazel.NoConfigAxis { |
| 1188 | (&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels)) |
| 1189 | (&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs)) |
| 1190 | (&defaultSelectValue).Append(bazel.MakeLabelList(depsWithStubs)) |
| 1191 | dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, bazel.FirstUniqueBazelLabelList(inApexSelectValue)) |
| 1192 | dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, bazel.FirstUniqueBazelLabelList(nonApexSelectValue)) |
| 1193 | dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.FirstUniqueBazelLabelList(defaultSelectValue)) |
| 1194 | } else if config == bazel.OsAndroid { |
| 1195 | (&inApexSelectValue).Append(bazel.MakeLabelList(stubLibLabels)) |
| 1196 | (&nonApexSelectValue).Append(bazel.MakeLabelList(depsWithStubs)) |
| 1197 | dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, bazel.FirstUniqueBazelLabelList(inApexSelectValue)) |
| 1198 | dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, bazel.FirstUniqueBazelLabelList(nonApexSelectValue)) |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1203 | func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) { |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1204 | bp2BuildPropParseHelper(ctx, module, &StripProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 1205 | if stripProperties, ok := props.(*StripProperties); ok { |
| 1206 | la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols) |
| 1207 | la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list) |
| 1208 | la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame) |
| 1209 | la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All) |
| 1210 | la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None) |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 1211 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1212 | }) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1213 | } |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 1214 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1215 | func (la *linkerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) { |
Jingwen Chen | 6ada589 | 2021-09-17 11:38:09 +0000 | [diff] [blame] | 1216 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1217 | type productVarDep struct { |
| 1218 | // the name of the corresponding excludes field, if one exists |
| 1219 | excludesField string |
| 1220 | // reference to the bazel attribute that should be set for the given product variable config |
| 1221 | attribute *bazel.LabelListAttribute |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 1222 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1223 | depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1224 | } |
| 1225 | |
Zi Wang | 0a8a129 | 2022-08-30 06:27:01 +0000 | [diff] [blame] | 1226 | // an intermediate attribute that holds Header_libs info, and will be appended to |
| 1227 | // implementationDeps at the end, to solve the confliction that both header_libs |
| 1228 | // and static_libs use implementationDeps. |
| 1229 | var headerDeps bazel.LabelListAttribute |
| 1230 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1231 | productVarToDepFields := map[string]productVarDep{ |
| 1232 | // product variables do not support exclude_shared_libs |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1233 | "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes}, |
| 1234 | "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes}, |
| 1235 | "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes}, |
Zi Wang | 0a8a129 | 2022-08-30 06:27:01 +0000 | [diff] [blame] | 1236 | "Header_libs": {attribute: &headerDeps, depResolutionFunc: bazelLabelForHeaderDepsExcludes}, |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1237 | } |
| 1238 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1239 | for name, dep := range productVarToDepFields { |
| 1240 | props, exists := productVariableProps[name] |
| 1241 | excludeProps, excludesExists := productVariableProps[dep.excludesField] |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 1242 | // if neither an include nor excludes property exists, then skip it |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1243 | if !exists && !excludesExists { |
| 1244 | continue |
| 1245 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1246 | // Collect all the configurations that an include or exclude property exists for. |
| 1247 | // We want to iterate all configurations rather than either the include or exclude because, for a |
| 1248 | // particular configuration, we may have either only an include or an exclude to handle. |
| 1249 | productConfigProps := make(map[android.ProductConfigProperty]bool, len(props)+len(excludeProps)) |
| 1250 | for p := range props { |
| 1251 | productConfigProps[p] = true |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1252 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1253 | for p := range excludeProps { |
| 1254 | productConfigProps[p] = true |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1255 | } |
| 1256 | |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1257 | for productConfigProp := range productConfigProps { |
| 1258 | prop, includesExists := props[productConfigProp] |
| 1259 | excludesProp, excludesExists := excludeProps[productConfigProp] |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1260 | var includes, excludes []string |
| 1261 | var ok bool |
| 1262 | // 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] | 1263 | if includes, ok = prop.([]string); includesExists && !ok { |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1264 | ctx.ModuleErrorf("Could not convert product variable %s property", name) |
| 1265 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1266 | if excludes, ok = excludesProp.([]string); excludesExists && !ok { |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1267 | ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField) |
| 1268 | } |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 1269 | |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 1270 | dep.attribute.EmitEmptyList = productConfigProp.AlwaysEmit() |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1271 | dep.attribute.SetSelectValue( |
| 1272 | productConfigProp.ConfigurationAxis(), |
| 1273 | productConfigProp.SelectKey(), |
| 1274 | dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes), |
| 1275 | ) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1276 | } |
| 1277 | } |
Zi Wang | 0a8a129 | 2022-08-30 06:27:01 +0000 | [diff] [blame] | 1278 | la.implementationDeps.Append(headerDeps) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1279 | } |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1280 | |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1281 | func (la *linkerAttributes) finalize(ctx android.BazelConversionPathContext) { |
| 1282 | // if system dynamic deps have the default value, any use of a system dynamic library used will |
| 1283 | // 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] | 1284 | // 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] | 1285 | if la.systemDynamicDeps.IsNil() && len(la.usedSystemDynamicDepAsDynamicDep) > 0 { |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 1286 | toRemove := bazelLabelForSharedDeps(ctx, android.SortedKeys(la.usedSystemDynamicDepAsDynamicDep)) |
Liz Kammer | 43345e2 | 2022-08-04 13:57:35 -0400 | [diff] [blame] | 1287 | la.dynamicDeps.Exclude(bazel.NoConfigAxis, "", toRemove) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1288 | la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove) |
| 1289 | la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove) |
Liz Kammer | 91487d4 | 2022-09-13 11:27:11 -0400 | [diff] [blame] | 1290 | la.implementationDynamicDeps.Exclude(bazel.NoConfigAxis, "", toRemove) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1291 | la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove) |
| 1292 | la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove) |
Liz Kammer | 91487d4 | 2022-09-13 11:27:11 -0400 | [diff] [blame] | 1293 | |
| 1294 | la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, toRemove) |
| 1295 | la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.AndroidAndNonApex, toRemove) |
| 1296 | stubsToRemove := make([]bazel.Label, 0, len(la.usedSystemDynamicDepAsDynamicDep)) |
| 1297 | for _, lib := range toRemove.Includes { |
Spandan Das | 2518c02 | 2023-03-17 03:02:32 +0000 | [diff] [blame] | 1298 | stubLabelInApiSurfaces := bazel.Label{ |
| 1299 | Label: apiSurfaceModuleLibCurrentPackage + lib.OriginalModuleName, |
| 1300 | } |
| 1301 | stubsToRemove = append(stubsToRemove, stubLabelInApiSurfaces) |
Liz Kammer | 91487d4 | 2022-09-13 11:27:11 -0400 | [diff] [blame] | 1302 | } |
| 1303 | la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.AndroidAndInApex, bazel.MakeLabelList(stubsToRemove)) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1304 | } |
| 1305 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1306 | la.deps.ResolveExcludes() |
| 1307 | la.implementationDeps.ResolveExcludes() |
| 1308 | la.dynamicDeps.ResolveExcludes() |
| 1309 | la.implementationDynamicDeps.ResolveExcludes() |
| 1310 | la.wholeArchiveDeps.ResolveExcludes() |
| 1311 | la.systemDynamicDeps.ForceSpecifyEmptyList = true |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1312 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 1313 | } |
| 1314 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 1315 | // Relativize a list of root-relative paths with respect to the module's |
| 1316 | // directory. |
| 1317 | // |
| 1318 | // include_dirs Soong prop are root-relative (b/183742505), but |
| 1319 | // local_include_dirs, export_include_dirs and export_system_include_dirs are |
| 1320 | // module dir relative. This function makes a list of paths entirely module dir |
| 1321 | // relative. |
| 1322 | // |
| 1323 | // For the `include` attribute, Bazel wants the paths to be relative to the |
| 1324 | // module. |
| 1325 | func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 1326 | var relativePaths []string |
| 1327 | for _, path := range paths { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 1328 | // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path |
| 1329 | relativePath, err := filepath.Rel(ctx.ModuleDir(), path) |
| 1330 | if err != nil { |
| 1331 | panic(err) |
| 1332 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 1333 | relativePaths = append(relativePaths, relativePath) |
| 1334 | } |
| 1335 | return relativePaths |
| 1336 | } |
| 1337 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 1338 | // BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel |
| 1339 | // attributes. |
| 1340 | type BazelIncludes struct { |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 1341 | AbsoluteIncludes bazel.StringListAttribute |
| 1342 | Includes bazel.StringListAttribute |
| 1343 | SystemIncludes bazel.StringListAttribute |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 1344 | } |
| 1345 | |
Liz Kammer | 5454944 | 2022-05-11 13:55:06 -0400 | [diff] [blame] | 1346 | func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module, includes *BazelIncludes) BazelIncludes { |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 1347 | var exported BazelIncludes |
| 1348 | if includes != nil { |
| 1349 | exported = *includes |
| 1350 | } else { |
| 1351 | exported = BazelIncludes{} |
| 1352 | } |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 1353 | |
| 1354 | // cc library Export_include_dirs and Export_system_include_dirs are marked |
| 1355 | // "variant_prepend" in struct tag, set their prepend property to true to make |
| 1356 | // sure bp2build generates correct result. |
| 1357 | exported.Includes.Prepend = true |
| 1358 | exported.SystemIncludes.Prepend = true |
| 1359 | |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1360 | bp2BuildPropParseHelper(ctx, module, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 1361 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
| 1362 | if len(flagExporterProperties.Export_include_dirs) > 0 { |
| 1363 | exported.Includes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.Includes.SelectValue(axis, config), flagExporterProperties.Export_include_dirs...))) |
| 1364 | } |
| 1365 | if len(flagExporterProperties.Export_system_include_dirs) > 0 { |
| 1366 | 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] | 1367 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 1368 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1369 | }) |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 1370 | exported.AbsoluteIncludes.DeduplicateAxesFromBase() |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 1371 | exported.Includes.DeduplicateAxesFromBase() |
| 1372 | exported.SystemIncludes.DeduplicateAxesFromBase() |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 1373 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 1374 | return exported |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 1375 | } |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1376 | |
Trevor Radcliffe | cee4e05 | 2022-09-06 19:31:25 +0000 | [diff] [blame] | 1377 | func BazelLabelNameForStaticModule(baseLabel string) string { |
| 1378 | return baseLabel + "_bp2build_cc_library_static" |
| 1379 | } |
| 1380 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1381 | func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1382 | label := android.BazelModuleLabel(ctx, m) |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 1383 | if ccModule, ok := m.(*Module); ok && ccModule.typ() == fullLibrary { |
Trevor Radcliffe | cee4e05 | 2022-09-06 19:31:25 +0000 | [diff] [blame] | 1384 | return BazelLabelNameForStaticModule(label) |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1385 | } |
| 1386 | return label |
| 1387 | } |
| 1388 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1389 | func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1390 | // cc_library, at it's root name, propagates the shared library, which depends on the static |
| 1391 | // library. |
| 1392 | return android.BazelModuleLabel(ctx, m) |
| 1393 | } |
| 1394 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1395 | func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1396 | label := bazelLabelForStaticModule(ctx, m) |
| 1397 | if aModule, ok := m.(android.Module); ok { |
| 1398 | if android.IsModulePrebuilt(aModule) { |
| 1399 | label += "_alwayslink" |
| 1400 | } |
| 1401 | } |
| 1402 | return label |
| 1403 | } |
| 1404 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1405 | func bazelLabelForWholeDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1406 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps) |
| 1407 | } |
| 1408 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1409 | func bazelLabelForWholeDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1410 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps) |
| 1411 | } |
| 1412 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1413 | func bazelLabelForStaticDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1414 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule) |
| 1415 | } |
| 1416 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1417 | func bazelLabelForStaticDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1418 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule) |
| 1419 | } |
| 1420 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1421 | func bazelLabelForSharedDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1422 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule) |
| 1423 | } |
| 1424 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1425 | func bazelLabelForHeaderDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1426 | // This is not elegant, but bp2build's shared library targets only propagate |
| 1427 | // their header information as part of the normal C++ provider. |
| 1428 | return bazelLabelForSharedDeps(ctx, modules) |
| 1429 | } |
| 1430 | |
Zi Wang | 0a8a129 | 2022-08-30 06:27:01 +0000 | [diff] [blame] | 1431 | func bazelLabelForHeaderDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
| 1432 | // This is only used when product_variable header_libs is processed, to follow |
| 1433 | // the pattern of depResolutionFunc |
| 1434 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule) |
| 1435 | } |
| 1436 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1437 | func bazelLabelForSharedDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1438 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule) |
| 1439 | } |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1440 | |
| 1441 | type binaryLinkerAttrs struct { |
| 1442 | Linkshared *bool |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a56e970 | 2022-02-23 18:39:59 -0500 | [diff] [blame] | 1443 | Suffix bazel.StringAttribute |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1444 | } |
| 1445 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1446 | func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs { |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1447 | attrs := binaryLinkerAttrs{} |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1448 | bp2BuildPropParseHelper(ctx, m, &BinaryLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 1449 | linkerProps := props.(*BinaryLinkerProperties) |
| 1450 | staticExecutable := linkerProps.Static_executable |
| 1451 | if axis == bazel.NoConfigAxis { |
| 1452 | if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared { |
| 1453 | attrs.Linkshared = &linkBinaryShared |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1454 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1455 | } else if staticExecutable != nil { |
| 1456 | // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a |
| 1457 | // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling |
| 1458 | 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] | 1459 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a56e970 | 2022-02-23 18:39:59 -0500 | [diff] [blame] | 1460 | if suffix := linkerProps.Suffix; suffix != nil { |
| 1461 | attrs.Suffix.SetSelectValue(axis, config, suffix) |
| 1462 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1463 | }) |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1464 | |
| 1465 | return attrs |
| 1466 | } |
Trevor Radcliffe | db7e026 | 2022-10-28 16:48:18 +0000 | [diff] [blame] | 1467 | |
| 1468 | func bp2buildSanitizerFeatures(ctx android.BazelConversionPathContext, m *Module) bazel.StringListAttribute { |
| 1469 | sanitizerFeatures := bazel.StringListAttribute{} |
| 1470 | bp2BuildPropParseHelper(ctx, m, &SanitizeProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 1471 | var features []string |
| 1472 | if sanitizerProps, ok := props.(*SanitizeProperties); ok { |
| 1473 | if sanitizerProps.Sanitize.Integer_overflow != nil && *sanitizerProps.Sanitize.Integer_overflow { |
| 1474 | features = append(features, "ubsan_integer_overflow") |
| 1475 | } |
| 1476 | for _, sanitizer := range sanitizerProps.Sanitize.Misc_undefined { |
| 1477 | features = append(features, "ubsan_"+sanitizer) |
| 1478 | } |
| 1479 | sanitizerFeatures.SetSelectValue(axis, config, features) |
| 1480 | } |
| 1481 | }) |
| 1482 | return sanitizerFeatures |
| 1483 | } |
Trevor Radcliffe | 56b1a2b | 2023-02-06 21:58:30 +0000 | [diff] [blame] | 1484 | |
| 1485 | func bp2buildLtoFeatures(ctx android.BazelConversionPathContext, m *Module) bazel.StringListAttribute { |
| 1486 | lto_feature_name := "android_thin_lto" |
| 1487 | ltoBoolFeatures := bazel.BoolAttribute{} |
| 1488 | bp2BuildPropParseHelper(ctx, m, <OProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 1489 | if ltoProps, ok := props.(*LTOProperties); ok { |
| 1490 | thinProp := ltoProps.Lto.Thin != nil && *ltoProps.Lto.Thin |
| 1491 | thinPropSetToFalse := ltoProps.Lto.Thin != nil && !*ltoProps.Lto.Thin |
| 1492 | neverProp := ltoProps.Lto.Never != nil && *ltoProps.Lto.Never |
| 1493 | if thinProp { |
| 1494 | ltoBoolFeatures.SetSelectValue(axis, config, BoolPtr(true)) |
| 1495 | return |
| 1496 | } |
| 1497 | if neverProp || thinPropSetToFalse { |
| 1498 | if thinProp { |
| 1499 | ctx.ModuleErrorf("lto.thin and lto.never are mutually exclusive but were specified together") |
| 1500 | } else { |
| 1501 | ltoBoolFeatures.SetSelectValue(axis, config, BoolPtr(false)) |
| 1502 | } |
| 1503 | return |
| 1504 | } |
| 1505 | } |
| 1506 | ltoBoolFeatures.SetSelectValue(axis, config, nil) |
| 1507 | }) |
| 1508 | |
| 1509 | props := m.GetArchVariantProperties(ctx, <OProperties{}) |
| 1510 | ltoStringFeatures, err := ltoBoolFeatures.ToStringListAttribute(func(boolPtr *bool, axis bazel.ConfigurationAxis, config string) []string { |
| 1511 | if boolPtr == nil { |
| 1512 | return []string{} |
| 1513 | } |
| 1514 | if !*boolPtr { |
| 1515 | return []string{"-" + lto_feature_name} |
| 1516 | } |
| 1517 | features := []string{lto_feature_name} |
| 1518 | if ltoProps, ok := props[axis][config].(*LTOProperties); ok { |
| 1519 | if ltoProps.Whole_program_vtables != nil && *ltoProps.Whole_program_vtables { |
| 1520 | features = append(features, "android_thin_lto_whole_program_vtables") |
| 1521 | } |
| 1522 | } |
| 1523 | return features |
| 1524 | }) |
| 1525 | if err != nil { |
| 1526 | ctx.ModuleErrorf("Error processing LTO attributes: %s", err) |
| 1527 | } |
| 1528 | return ltoStringFeatures |
| 1529 | } |