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" |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 20 | "sync" |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 21 | |
| 22 | "android/soong/android" |
| 23 | "android/soong/bazel" |
Alix | 1be00d4 | 2022-05-16 22:56:04 +0000 | [diff] [blame] | 24 | "android/soong/cc/config" |
Liz Kammer | 0db0e34 | 2023-07-18 11:39:30 -0400 | [diff] [blame] | 25 | "android/soong/genrule" |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 26 | |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 27 | "github.com/google/blueprint" |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 28 | |
| 29 | "github.com/google/blueprint/proptools" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 30 | ) |
| 31 | |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 32 | const ( |
Trevor Radcliffe | cee4e05 | 2022-09-06 19:31:25 +0000 | [diff] [blame] | 33 | cSrcPartition = "c" |
| 34 | asSrcPartition = "as" |
| 35 | asmSrcPartition = "asm" |
| 36 | lSrcPartition = "l" |
| 37 | llSrcPartition = "ll" |
| 38 | cppSrcPartition = "cpp" |
| 39 | protoSrcPartition = "proto" |
| 40 | aidlSrcPartition = "aidl" |
| 41 | syspropSrcPartition = "sysprop" |
Alix | e266787 | 2023-04-24 14:57:32 +0000 | [diff] [blame] | 42 | |
| 43 | yaccSrcPartition = "yacc" |
| 44 | |
| 45 | rScriptSrcPartition = "renderScript" |
Liz Kammer | 91487d4 | 2022-09-13 11:27:11 -0400 | [diff] [blame] | 46 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 47 | xsdSrcPartition = "xsd" |
| 48 | |
Liz Kammer | 0db0e34 | 2023-07-18 11:39:30 -0400 | [diff] [blame] | 49 | genrulePartition = "genrule" |
| 50 | |
Spandan Das | a99348d | 2023-08-01 23:10:05 +0000 | [diff] [blame] | 51 | protoFromGenPartition = "proto_gen" |
| 52 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 53 | hdrPartition = "hdr" |
| 54 | |
Liz Kammer | 91487d4 | 2022-09-13 11:27:11 -0400 | [diff] [blame] | 55 | stubsSuffix = "_stub_libs_current" |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 56 | ) |
| 57 | |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 58 | // staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties -- |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 59 | // 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] | 60 | type staticOrSharedAttributes struct { |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 61 | Srcs bazel.LabelListAttribute |
| 62 | Srcs_c bazel.LabelListAttribute |
| 63 | Srcs_as bazel.LabelListAttribute |
| 64 | Srcs_aidl bazel.LabelListAttribute |
| 65 | Hdrs bazel.LabelListAttribute |
| 66 | Copts bazel.StringListAttribute |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 67 | |
Trevor Radcliffe | d9b7f17 | 2023-08-09 22:21:38 +0000 | [diff] [blame] | 68 | Additional_compiler_inputs bazel.LabelListAttribute |
| 69 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 70 | Deps bazel.LabelListAttribute |
| 71 | Implementation_deps bazel.LabelListAttribute |
| 72 | Dynamic_deps bazel.LabelListAttribute |
| 73 | Implementation_dynamic_deps bazel.LabelListAttribute |
| 74 | Whole_archive_deps bazel.LabelListAttribute |
| 75 | Implementation_whole_archive_deps bazel.LabelListAttribute |
Cole Faust | 6b29f59 | 2022-08-09 09:50:56 -0700 | [diff] [blame] | 76 | Runtime_deps bazel.LabelListAttribute |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 77 | |
| 78 | System_dynamic_deps bazel.LabelListAttribute |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 79 | |
| 80 | Enabled bazel.BoolAttribute |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 81 | |
Yu Liu | f01a0f0 | 2022-12-07 15:45:30 -0800 | [diff] [blame] | 82 | Native_coverage *bool |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 83 | |
Liz Kammer | aceec25 | 2023-03-24 09:46:36 -0400 | [diff] [blame] | 84 | Apex_available []string |
| 85 | |
Trevor Radcliffe | a8b4416 | 2023-04-14 18:25:24 +0000 | [diff] [blame] | 86 | Features bazel.StringListAttribute |
| 87 | |
Yu Liu | f11b7c3 | 2023-10-20 19:15:51 +0000 | [diff] [blame^] | 88 | SdkAttributes |
Sam Delmerico | fb3bb32 | 2022-10-21 10:42:24 -0400 | [diff] [blame] | 89 | |
| 90 | tidyAttributes |
| 91 | } |
| 92 | |
| 93 | type tidyAttributes struct { |
Sam Delmerico | 63f0c93 | 2023-03-14 14:05:28 -0400 | [diff] [blame] | 94 | Tidy *string |
Sam Delmerico | fb3bb32 | 2022-10-21 10:42:24 -0400 | [diff] [blame] | 95 | Tidy_flags []string |
| 96 | Tidy_checks []string |
| 97 | Tidy_checks_as_errors []string |
Sam Delmerico | c9b8fbd | 2022-10-25 15:47:17 -0400 | [diff] [blame] | 98 | Tidy_disabled_srcs bazel.LabelListAttribute |
Sam Delmerico | 4c902d6 | 2022-11-02 14:17:15 -0400 | [diff] [blame] | 99 | Tidy_timeout_srcs bazel.LabelListAttribute |
Sam Delmerico | fb3bb32 | 2022-10-21 10:42:24 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 102 | func (m *Module) convertTidyAttributes(ctx android.Bp2buildMutatorContext, moduleAttrs *tidyAttributes) { |
Sam Delmerico | fb3bb32 | 2022-10-21 10:42:24 -0400 | [diff] [blame] | 103 | for _, f := range m.features { |
| 104 | if tidy, ok := f.(*tidyFeature); ok { |
Sam Delmerico | 63f0c93 | 2023-03-14 14:05:28 -0400 | [diff] [blame] | 105 | var tidyAttr *string |
| 106 | if tidy.Properties.Tidy != nil { |
| 107 | if *tidy.Properties.Tidy { |
| 108 | tidyAttr = proptools.StringPtr("local") |
| 109 | } else { |
| 110 | tidyAttr = proptools.StringPtr("never") |
| 111 | } |
| 112 | } |
| 113 | moduleAttrs.Tidy = tidyAttr |
Sam Delmerico | fb3bb32 | 2022-10-21 10:42:24 -0400 | [diff] [blame] | 114 | moduleAttrs.Tidy_flags = tidy.Properties.Tidy_flags |
| 115 | moduleAttrs.Tidy_checks = tidy.Properties.Tidy_checks |
| 116 | moduleAttrs.Tidy_checks_as_errors = tidy.Properties.Tidy_checks_as_errors |
| 117 | } |
Sam Delmerico | c9b8fbd | 2022-10-25 15:47:17 -0400 | [diff] [blame] | 118 | |
| 119 | } |
Sam Delmerico | c9b8fbd | 2022-10-25 15:47:17 -0400 | [diff] [blame] | 120 | archVariantProps := m.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) |
| 121 | for axis, configToProps := range archVariantProps { |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 122 | for cfg, _props := range configToProps { |
Sam Delmerico | c9b8fbd | 2022-10-25 15:47:17 -0400 | [diff] [blame] | 123 | if archProps, ok := _props.(*BaseCompilerProperties); ok { |
| 124 | archDisabledSrcs := android.BazelLabelForModuleSrc(ctx, archProps.Tidy_disabled_srcs) |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 125 | moduleAttrs.Tidy_disabled_srcs.SetSelectValue(axis, cfg, archDisabledSrcs) |
Sam Delmerico | 4c902d6 | 2022-11-02 14:17:15 -0400 | [diff] [blame] | 126 | archTimeoutSrcs := android.BazelLabelForModuleSrc(ctx, archProps.Tidy_timeout_srcs) |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 127 | moduleAttrs.Tidy_timeout_srcs.SetSelectValue(axis, cfg, archTimeoutSrcs) |
Sam Delmerico | c9b8fbd | 2022-10-25 15:47:17 -0400 | [diff] [blame] | 128 | } |
| 129 | } |
Sam Delmerico | fb3bb32 | 2022-10-21 10:42:24 -0400 | [diff] [blame] | 130 | } |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Spandan Das | a99348d | 2023-08-01 23:10:05 +0000 | [diff] [blame] | 133 | // Returns an unchanged label and a bool indicating whether the dep is a genrule that produces .proto files |
| 134 | func protoFromGenPartitionMapper(pathCtx android.BazelConversionContext) bazel.LabelMapper { |
| 135 | return func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) { |
| 136 | mod, exists := ctx.ModuleFromName(label.OriginalModuleName) |
| 137 | if !exists { |
| 138 | return label.Label, false |
| 139 | } |
| 140 | gen, isGen := mod.(*genrule.Module) |
| 141 | if !isGen { |
| 142 | return label.Label, false |
| 143 | } |
| 144 | if containsProto(ctx, gen.RawOutputFiles(pathCtx)) { |
| 145 | // Return unmodified label + true |
| 146 | // True will ensure that this module gets dropped from `srcs` of the generated cc_* target. `srcs` is reserved for .cpp files |
| 147 | return label.Label, true |
| 148 | } |
| 149 | return label.Label, false |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Returns true if srcs contains only .proto files |
| 154 | // Raises an exception if there is a combination of .proto and non .proto srcs |
| 155 | func containsProto(ctx bazel.OtherModuleContext, srcs []string) bool { |
| 156 | onlyProtos := false |
| 157 | for _, src := range srcs { |
| 158 | if strings.HasSuffix(src, ".proto") { |
| 159 | onlyProtos = true |
| 160 | } else if onlyProtos { |
| 161 | // This is not a proto file, and we have encountered a .proto file previously |
| 162 | ctx.ModuleErrorf("TOOD: Add bp2build support combination of .proto and non .proto files in outs of genrule") |
| 163 | } |
| 164 | } |
| 165 | return onlyProtos |
| 166 | } |
| 167 | |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 168 | // groupSrcsByExtension partitions `srcs` into groups based on file extension. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 169 | func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute { |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 170 | // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl |
| 171 | // macro. |
| 172 | addSuffixForFilegroup := func(suffix string) bazel.LabelMapper { |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 173 | return func(otherModuleCtx bazel.OtherModuleContext, label bazel.Label) (string, bool) { |
| 174 | |
| 175 | m, exists := otherModuleCtx.ModuleFromName(label.OriginalModuleName) |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 176 | labelStr := label.Label |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 177 | if !exists || !android.IsFilegroup(otherModuleCtx, m) { |
| 178 | return labelStr, false |
| 179 | } |
Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 180 | // If the filegroup is already converted to aidl_library or proto_library, |
| 181 | // skip creating _c_srcs, _as_srcs, _cpp_srcs filegroups |
| 182 | fg, _ := m.(android.FileGroupAsLibrary) |
| 183 | if fg.ShouldConvertToAidlLibrary(ctx) || fg.ShouldConvertToProtoLibrary(ctx) { |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 184 | return labelStr, false |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 185 | } |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 186 | return labelStr + suffix, true |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 187 | } |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 190 | // TODO(b/190006308): Handle language detection of sources in a Bazel rule. |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 191 | labels := bazel.LabelPartitions{ |
| 192 | protoSrcPartition: android.ProtoSrcLabelPartition, |
Liz Kammer | aabfb5d | 2021-12-08 15:25:06 -0500 | [diff] [blame] | 193 | cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")}, |
| 194 | asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")}, |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 195 | asmSrcPartition: bazel.LabelPartition{Extensions: []string{".asm"}}, |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 196 | aidlSrcPartition: android.AidlSrcLabelPartition, |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 197 | // TODO(http://b/231968910): If there is ever a filegroup target that |
| 198 | // contains .l or .ll files we will need to find a way to add a |
| 199 | // LabelMapper for these that identifies these filegroups and |
| 200 | // converts them appropriately |
Spandan Das | a99348d | 2023-08-01 23:10:05 +0000 | [diff] [blame] | 201 | lSrcPartition: bazel.LabelPartition{Extensions: []string{".l"}}, |
| 202 | llSrcPartition: bazel.LabelPartition{Extensions: []string{".ll"}}, |
| 203 | rScriptSrcPartition: bazel.LabelPartition{Extensions: []string{".fs", ".rscript"}}, |
| 204 | xsdSrcPartition: bazel.LabelPartition{LabelMapper: android.XsdLabelMapper(xsdConfigCppTarget)}, |
| 205 | protoFromGenPartition: bazel.LabelPartition{LabelMapper: protoFromGenPartitionMapper(ctx)}, |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 206 | // C++ is the "catch-all" group, and comprises generated sources because we don't |
| 207 | // know the language of these sources until the genrule is executed. |
Trevor Radcliffe | cee4e05 | 2022-09-06 19:31:25 +0000 | [diff] [blame] | 208 | cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true}, |
| 209 | syspropSrcPartition: bazel.LabelPartition{Extensions: []string{".sysprop"}}, |
Spandan Das | df4c213 | 2023-05-09 23:58:52 +0000 | [diff] [blame] | 210 | yaccSrcPartition: bazel.LabelPartition{Extensions: []string{".y", "yy"}}, |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 211 | } |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 212 | |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 213 | return bazel.PartitionLabelListAttribute(ctx, &srcs, labels) |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 216 | func partitionHeaders(ctx android.BazelConversionPathContext, hdrs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute { |
| 217 | labels := bazel.LabelPartitions{ |
Liz Kammer | 0db0e34 | 2023-07-18 11:39:30 -0400 | [diff] [blame] | 218 | xsdSrcPartition: bazel.LabelPartition{LabelMapper: android.XsdLabelMapper(xsdConfigCppTarget)}, |
| 219 | genrulePartition: bazel.LabelPartition{LabelMapper: genrule.GenruleCcHeaderLabelMapper}, |
| 220 | hdrPartition: bazel.LabelPartition{Keep_remainder: true}, |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 221 | } |
| 222 | return bazel.PartitionLabelListAttribute(ctx, &hdrs, labels) |
| 223 | } |
| 224 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 225 | // bp2BuildParseLibProps returns the attributes for a variant of a cc_library. |
Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 226 | func bp2BuildParseLibProps(ctx android.Bp2buildMutatorContext, module *Module, isStatic bool) staticOrSharedAttributes { |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 227 | lib, ok := module.compiler.(*libraryDecorator) |
| 228 | if !ok { |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 229 | return staticOrSharedAttributes{} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 230 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 231 | return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic) |
| 232 | } |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 233 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 234 | // bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library. |
Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 235 | func bp2BuildParseSharedProps(ctx android.Bp2buildMutatorContext, 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] | 236 | return bp2BuildParseLibProps(ctx, module, false) |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // bp2buildParseStaticProps returns the attributes for the static variant of a cc_library. |
Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 240 | func bp2BuildParseStaticProps(ctx android.Bp2buildMutatorContext, 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] | 241 | return bp2BuildParseLibProps(ctx, module, true) |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 244 | type depsPartition struct { |
| 245 | export bazel.LabelList |
| 246 | implementation bazel.LabelList |
| 247 | } |
| 248 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 249 | type bazelLabelForDepsFn func(android.Bp2buildMutatorContext, []string) bazel.LabelList |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 250 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 251 | func maybePartitionExportedAndImplementationsDeps(ctx android.Bp2buildMutatorContext, exportsDeps bool, allDeps, exportedDeps []string, fn bazelLabelForDepsFn) depsPartition { |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 252 | if !exportsDeps { |
| 253 | return depsPartition{ |
| 254 | implementation: fn(ctx, allDeps), |
| 255 | } |
| 256 | } |
| 257 | |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 258 | implementation, export := android.FilterList(allDeps, exportedDeps) |
| 259 | |
| 260 | return depsPartition{ |
| 261 | export: fn(ctx, export), |
| 262 | implementation: fn(ctx, implementation), |
| 263 | } |
| 264 | } |
| 265 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 266 | type bazelLabelForDepsExcludesFn func(android.Bp2buildMutatorContext, []string, []string) bazel.LabelList |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 267 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 268 | func maybePartitionExportedAndImplementationsDepsExcludes(ctx android.Bp2buildMutatorContext, exportsDeps bool, allDeps, excludes, exportedDeps []string, fn bazelLabelForDepsExcludesFn) depsPartition { |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 269 | if !exportsDeps { |
| 270 | return depsPartition{ |
| 271 | implementation: fn(ctx, allDeps, excludes), |
| 272 | } |
| 273 | } |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 274 | implementation, export := android.FilterList(allDeps, exportedDeps) |
| 275 | |
| 276 | return depsPartition{ |
| 277 | export: fn(ctx, export, excludes), |
| 278 | implementation: fn(ctx, implementation, excludes), |
| 279 | } |
| 280 | } |
| 281 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 282 | func bp2BuildPropParseHelper(ctx android.ArchVariantContext, module *Module, propsType interface{}, parseFunc func(axis bazel.ConfigurationAxis, config string, props interface{})) { |
| 283 | for axis, configToProps := range module.GetArchVariantProperties(ctx, propsType) { |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 284 | for cfg, props := range configToProps { |
| 285 | 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] | 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 290 | // Parses properties common to static and shared libraries. Also used for prebuilt libraries. |
Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 291 | func bp2buildParseStaticOrSharedProps(ctx android.Bp2buildMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes { |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 292 | attrs := staticOrSharedAttributes{} |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 293 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 294 | setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) { |
Trevor Radcliffe | a8b4416 | 2023-04-14 18:25:24 +0000 | [diff] [blame] | 295 | attrs.Copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, filterOutStdFlag, filterOutHiddenVisibility)) |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 296 | attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs)) |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 297 | 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] | 298 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 299 | 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] | 300 | attrs.Deps.SetSelectValue(axis, config, staticDeps.export) |
| 301 | attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation) |
| 302 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 303 | 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] | 304 | attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export) |
| 305 | attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation) |
| 306 | |
| 307 | 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] | 308 | attrs.Enabled.SetSelectValue(axis, config, props.Enabled) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 309 | } |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 310 | // system_dynamic_deps distinguishes between nil/empty list behavior: |
| 311 | // nil -> use default values |
| 312 | // empty list -> no values specified |
| 313 | attrs.System_dynamic_deps.ForceSpecifyEmptyList = true |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 314 | |
Liz Kammer | aceec25 | 2023-03-24 09:46:36 -0400 | [diff] [blame] | 315 | var apexAvailable []string |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 316 | if isStatic { |
Liz Kammer | aceec25 | 2023-03-24 09:46:36 -0400 | [diff] [blame] | 317 | apexAvailable = lib.StaticProperties.Static.Apex_available |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 318 | bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 319 | if staticOrSharedProps, ok := props.(*StaticProperties); ok { |
| 320 | setAttrs(axis, config, staticOrSharedProps.Static) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 321 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 322 | }) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 323 | } else { |
Liz Kammer | aceec25 | 2023-03-24 09:46:36 -0400 | [diff] [blame] | 324 | apexAvailable = lib.SharedProperties.Shared.Apex_available |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 325 | bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 326 | if staticOrSharedProps, ok := props.(*SharedProperties); ok { |
| 327 | setAttrs(axis, config, staticOrSharedProps.Shared) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 328 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 329 | }) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 332 | partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs) |
| 333 | attrs.Srcs = partitionedSrcs[cppSrcPartition] |
| 334 | attrs.Srcs_c = partitionedSrcs[cSrcPartition] |
| 335 | attrs.Srcs_as = partitionedSrcs[asSrcPartition] |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 336 | |
Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 337 | attrs.Apex_available = android.ConvertApexAvailableToTagsWithoutTestApexes(ctx, apexAvailable) |
Liz Kammer | aceec25 | 2023-03-24 09:46:36 -0400 | [diff] [blame] | 338 | |
Trevor Radcliffe | a8b4416 | 2023-04-14 18:25:24 +0000 | [diff] [blame] | 339 | attrs.Features.Append(convertHiddenVisibilityToFeatureStaticOrShared(ctx, module, isStatic)) |
| 340 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 341 | if !partitionedSrcs[protoSrcPartition].IsEmpty() { |
| 342 | // TODO(b/208815215): determine whether this is used and add support if necessary |
| 343 | ctx.ModuleErrorf("Migrating static/shared only proto srcs is not currently supported") |
| 344 | } |
| 345 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 346 | return attrs |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 349 | // Convenience struct to hold all attributes parsed from prebuilt properties. |
| 350 | type prebuiltAttributes struct { |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 351 | Src bazel.LabelAttribute |
| 352 | Enabled bazel.BoolAttribute |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 353 | } |
| 354 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 355 | func parseSrc(ctx android.Bp2buildMutatorContext, srcLabelAttribute *bazel.LabelAttribute, axis bazel.ConfigurationAxis, config string, srcs []string) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 356 | srcFileError := func() { |
| 357 | ctx.ModuleErrorf("parseSrc: Expected at most one source file for %s %s\n", axis, config) |
| 358 | } |
| 359 | if len(srcs) > 1 { |
| 360 | srcFileError() |
| 361 | return |
| 362 | } else if len(srcs) == 0 { |
| 363 | return |
| 364 | } |
| 365 | if srcLabelAttribute.SelectValue(axis, config) != nil { |
| 366 | srcFileError() |
| 367 | return |
| 368 | } |
| 369 | srcLabelAttribute.SetSelectValue(axis, config, android.BazelLabelForModuleSrcSingle(ctx, srcs[0])) |
| 370 | } |
| 371 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 372 | // NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 373 | func Bp2BuildParsePrebuiltLibraryProps(ctx android.Bp2buildMutatorContext, 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] | 374 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 375 | var srcLabelAttribute bazel.LabelAttribute |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 376 | bp2BuildPropParseHelper(ctx, module, &prebuiltLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 377 | 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] | 378 | parseSrc(ctx, &srcLabelAttribute, axis, config, prebuiltLinkerProperties.Srcs) |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 379 | } |
| 380 | }) |
| 381 | |
| 382 | var enabledLabelAttribute bazel.BoolAttribute |
| 383 | parseAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) { |
| 384 | if props.Enabled != nil { |
| 385 | enabledLabelAttribute.SetSelectValue(axis, config, props.Enabled) |
| 386 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 387 | parseSrc(ctx, &srcLabelAttribute, axis, config, props.Srcs) |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | if isStatic { |
| 391 | bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 392 | if staticProperties, ok := props.(*StaticProperties); ok { |
| 393 | parseAttrs(axis, config, staticProperties.Static) |
| 394 | } |
| 395 | }) |
| 396 | } else { |
| 397 | bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 398 | if sharedProperties, ok := props.(*SharedProperties); ok { |
| 399 | parseAttrs(axis, config, sharedProperties.Shared) |
| 400 | } |
| 401 | }) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 402 | } |
| 403 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 404 | return prebuiltAttributes{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 405 | Src: srcLabelAttribute, |
| 406 | Enabled: enabledLabelAttribute, |
| 407 | } |
| 408 | } |
| 409 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 410 | func bp2BuildParsePrebuiltBinaryProps(ctx android.Bp2buildMutatorContext, module *Module) prebuiltAttributes { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 411 | var srcLabelAttribute bazel.LabelAttribute |
| 412 | bp2BuildPropParseHelper(ctx, module, &prebuiltLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 413 | if props, ok := props.(*prebuiltLinkerProperties); ok { |
| 414 | parseSrc(ctx, &srcLabelAttribute, axis, config, props.Srcs) |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 415 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 416 | }) |
| 417 | |
| 418 | return prebuiltAttributes{ |
| 419 | Src: srcLabelAttribute, |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 423 | func bp2BuildParsePrebuiltObjectProps(ctx android.Bp2buildMutatorContext, module *Module) prebuiltAttributes { |
Colin Cross | c5075e9 | 2022-12-05 16:46:39 -0800 | [diff] [blame] | 424 | var srcLabelAttribute bazel.LabelAttribute |
| 425 | bp2BuildPropParseHelper(ctx, module, &prebuiltObjectProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 426 | if props, ok := props.(*prebuiltObjectProperties); ok { |
| 427 | parseSrc(ctx, &srcLabelAttribute, axis, config, props.Srcs) |
| 428 | } |
| 429 | }) |
| 430 | |
| 431 | return prebuiltAttributes{ |
| 432 | Src: srcLabelAttribute, |
| 433 | } |
| 434 | } |
| 435 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 436 | type baseAttributes struct { |
| 437 | compilerAttributes |
| 438 | linkerAttributes |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 439 | |
Trevor Radcliffe | db7e026 | 2022-10-28 16:48:18 +0000 | [diff] [blame] | 440 | // 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] | 441 | features bazel.StringListAttribute |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 442 | protoDependency *bazel.LabelAttribute |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 443 | aidlDependency *bazel.LabelAttribute |
Yu Liu | f01a0f0 | 2022-12-07 15:45:30 -0800 | [diff] [blame] | 444 | Native_coverage *bool |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 445 | } |
| 446 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 447 | // Convenience struct to hold all attributes parsed from compiler properties. |
| 448 | type compilerAttributes struct { |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 449 | // Options for all languages |
| 450 | copts bazel.StringListAttribute |
| 451 | // Assembly options and sources |
| 452 | asFlags bazel.StringListAttribute |
| 453 | asSrcs bazel.LabelListAttribute |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 454 | asmSrcs bazel.LabelListAttribute |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 455 | // C options and sources |
| 456 | conlyFlags bazel.StringListAttribute |
| 457 | cSrcs bazel.LabelListAttribute |
| 458 | // C++ options and sources |
| 459 | cppFlags bazel.StringListAttribute |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 460 | srcs bazel.LabelListAttribute |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 461 | |
Liz Kammer | 084d6a9 | 2023-06-22 16:23:53 -0400 | [diff] [blame] | 462 | // xsd config sources |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 463 | xsdSrcs bazel.LabelListAttribute |
| 464 | exportXsdSrcs bazel.LabelListAttribute |
Liz Kammer | 084d6a9 | 2023-06-22 16:23:53 -0400 | [diff] [blame] | 465 | |
Liz Kammer | 0db0e34 | 2023-07-18 11:39:30 -0400 | [diff] [blame] | 466 | // genrule headers |
| 467 | genruleHeaders bazel.LabelListAttribute |
| 468 | exportGenruleHeaders bazel.LabelListAttribute |
| 469 | |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 470 | // Lex sources and options |
| 471 | lSrcs bazel.LabelListAttribute |
| 472 | llSrcs bazel.LabelListAttribute |
| 473 | lexopts bazel.StringListAttribute |
| 474 | |
Trevor Radcliffe | cee4e05 | 2022-09-06 19:31:25 +0000 | [diff] [blame] | 475 | // Sysprop sources |
| 476 | syspropSrcs bazel.LabelListAttribute |
| 477 | |
Spandan Das | df4c213 | 2023-05-09 23:58:52 +0000 | [diff] [blame] | 478 | // Yacc sources |
| 479 | yaccSrc *bazel.LabelAttribute |
| 480 | yaccFlags bazel.StringListAttribute |
| 481 | yaccGenLocationHeader bazel.BoolAttribute |
| 482 | yaccGenPositionHeader bazel.BoolAttribute |
| 483 | |
Alix | e266787 | 2023-04-24 14:57:32 +0000 | [diff] [blame] | 484 | rsSrcs bazel.LabelListAttribute |
| 485 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 486 | hdrs bazel.LabelListAttribute |
| 487 | |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 488 | rtti bazel.BoolAttribute |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 489 | |
| 490 | // Not affected by arch variants |
| 491 | stl *string |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 492 | cStd *string |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 493 | cppStd *string |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 494 | |
| 495 | localIncludes bazel.StringListAttribute |
| 496 | absoluteIncludes bazel.StringListAttribute |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 497 | |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 498 | includes BazelIncludes |
| 499 | |
Alix | e266787 | 2023-04-24 14:57:32 +0000 | [diff] [blame] | 500 | protoSrcs bazel.LabelListAttribute |
| 501 | aidlSrcs bazel.LabelListAttribute |
| 502 | rscriptSrcs bazel.LabelListAttribute |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 503 | |
| 504 | stubsSymbolFile *string |
| 505 | stubsVersions bazel.StringListAttribute |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 506 | |
| 507 | 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] | 508 | |
Spandan Das | 39ccf93 | 2023-05-26 18:03:39 +0000 | [diff] [blame] | 509 | stem bazel.StringAttribute |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a56e970 | 2022-02-23 18:39:59 -0500 | [diff] [blame] | 510 | suffix bazel.StringAttribute |
Vinh Tran | 99270ea | 2022-11-28 11:15:23 -0500 | [diff] [blame] | 511 | |
| 512 | fdoProfile bazel.LabelAttribute |
Trevor Radcliffe | d9b7f17 | 2023-08-09 22:21:38 +0000 | [diff] [blame] | 513 | |
| 514 | additionalCompilerInputs bazel.LabelListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Liz Kammer | cac7f69 | 2021-12-16 14:19:32 -0500 | [diff] [blame] | 517 | type filterOutFn func(string) bool |
| 518 | |
Trevor Radcliffe | a8b4416 | 2023-04-14 18:25:24 +0000 | [diff] [blame] | 519 | // filterOutHiddenVisibility removes the flag specifying hidden visibility as |
| 520 | // this flag is converted to a toolchain feature |
| 521 | func filterOutHiddenVisibility(flag string) bool { |
| 522 | return flag == config.VisibilityHiddenFlag |
| 523 | } |
| 524 | |
Liz Kammer | cac7f69 | 2021-12-16 14:19:32 -0500 | [diff] [blame] | 525 | func filterOutStdFlag(flag string) bool { |
| 526 | return strings.HasPrefix(flag, "-std=") |
| 527 | } |
| 528 | |
Alix | 1be00d4 | 2022-05-16 22:56:04 +0000 | [diff] [blame] | 529 | func filterOutClangUnknownCflags(flag string) bool { |
| 530 | for _, f := range config.ClangUnknownCflags { |
| 531 | if f == flag { |
| 532 | return true |
| 533 | } |
| 534 | } |
| 535 | return false |
| 536 | } |
| 537 | |
Trevor Radcliffe | ea6a45d | 2022-09-20 18:58:01 +0000 | [diff] [blame] | 538 | func parseCommandLineFlags(soongFlags []string, filterOut ...filterOutFn) []string { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 539 | var result []string |
| 540 | for _, flag := range soongFlags { |
Alix | 1be00d4 | 2022-05-16 22:56:04 +0000 | [diff] [blame] | 541 | skipFlag := false |
| 542 | for _, filter := range filterOut { |
| 543 | if filter != nil && filter(flag) { |
| 544 | skipFlag = true |
| 545 | } |
| 546 | } |
| 547 | if skipFlag { |
Liz Kammer | cac7f69 | 2021-12-16 14:19:32 -0500 | [diff] [blame] | 548 | continue |
| 549 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 550 | // Soong's cflags can contain spaces, like `-include header.h`. For |
| 551 | // Bazel's copts, split them up to be compatible with the |
| 552 | // no_copts_tokenization feature. |
Trevor Radcliffe | ea6a45d | 2022-09-20 18:58:01 +0000 | [diff] [blame] | 553 | result = append(result, strings.Split(flag, " ")...) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 554 | } |
| 555 | return result |
| 556 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 557 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 558 | func (ca *compilerAttributes) bp2buildForAxisAndConfig(ctx android.Bp2buildMutatorContext, axis bazel.ConfigurationAxis, config string, props *BaseCompilerProperties) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 559 | // If there's arch specific srcs or exclude_srcs, generate a select entry for it. |
| 560 | // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too. |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 561 | srcsList, ok := parseSrcs(ctx, props) |
Liz Kammer | 084d6a9 | 2023-06-22 16:23:53 -0400 | [diff] [blame] | 562 | |
| 563 | if ok { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 564 | ca.srcs.SetSelectValue(axis, config, srcsList) |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 565 | } |
| 566 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 567 | localIncludeDirs := props.Local_include_dirs |
| 568 | if axis == bazel.NoConfigAxis { |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 569 | 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] | 570 | if includeBuildDirectory(props.Include_build_directory) { |
| 571 | localIncludeDirs = append(localIncludeDirs, ".") |
Liz Kammer | 222bdcf | 2021-10-11 14:15:51 -0400 | [diff] [blame] | 572 | } |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 575 | ca.absoluteIncludes.SetSelectValue(axis, config, props.Include_dirs) |
| 576 | ca.localIncludes.SetSelectValue(axis, config, localIncludeDirs) |
| 577 | |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 578 | instructionSet := proptools.StringDefault(props.Instruction_set, "") |
| 579 | if instructionSet == "arm" { |
Trevor Radcliffe | 5f0c2ac | 2023-05-15 18:00:59 +0000 | [diff] [blame] | 580 | ca.features.SetSelectValue(axis, config, []string{"arm_isa_arm"}) |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 581 | } else if instructionSet != "" && instructionSet != "thumb" { |
| 582 | ctx.ModuleErrorf("Unknown value for instruction_set: %s", instructionSet) |
| 583 | } |
| 584 | |
Liz Kammer | cac7f69 | 2021-12-16 14:19:32 -0500 | [diff] [blame] | 585 | // In Soong, cflags occur on the command line before -std=<val> flag, resulting in the value being |
| 586 | // overridden. In Bazel we always allow overriding, via flags; however, this can cause |
| 587 | // incompatibilities, so we remove "-std=" flags from Cflag properties while leaving it in other |
| 588 | // cases. |
Trevor Radcliffe | a8b4416 | 2023-04-14 18:25:24 +0000 | [diff] [blame] | 589 | ca.copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, filterOutStdFlag, filterOutClangUnknownCflags, filterOutHiddenVisibility)) |
Trevor Radcliffe | ea6a45d | 2022-09-20 18:58:01 +0000 | [diff] [blame] | 590 | ca.asFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Asflags, nil)) |
| 591 | ca.conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Conlyflags, filterOutClangUnknownCflags)) |
| 592 | ca.cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Cppflags, filterOutClangUnknownCflags)) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 593 | ca.rtti.SetSelectValue(axis, config, props.Rtti) |
| 594 | } |
| 595 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 596 | func (ca *compilerAttributes) convertStlProps(ctx android.ArchVariantContext, module *Module) { |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 597 | bp2BuildPropParseHelper(ctx, module, &StlProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 598 | if stlProps, ok := props.(*StlProperties); ok { |
| 599 | if stlProps.Stl == nil { |
| 600 | return |
| 601 | } |
| 602 | if ca.stl == nil { |
Liz Kammer | 7128d38 | 2022-05-12 11:42:33 -0400 | [diff] [blame] | 603 | stl := deduplicateStlInput(*stlProps.Stl) |
| 604 | ca.stl = &stl |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 605 | } else if ca.stl != stlProps.Stl { |
| 606 | 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] | 607 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 608 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 609 | }) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 610 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 611 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 612 | func (ca *compilerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) { |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 613 | productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{ |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 614 | "Cflags": &ca.copts, |
| 615 | "Asflags": &ca.asFlags, |
Yu Liu | 93893ba | 2023-05-01 13:49:52 -0700 | [diff] [blame] | 616 | "Cppflags": &ca.cppFlags, |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 617 | } |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 618 | for propName, attr := range productVarPropNameToAttribute { |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 619 | if productConfigProps, exists := productVariableProps[propName]; exists { |
| 620 | for productConfigProp, prop := range productConfigProps { |
| 621 | flags, ok := prop.([]string) |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 622 | if !ok { |
| 623 | ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName)) |
| 624 | } |
Cole Faust | 150f9a5 | 2023-04-26 10:52:24 -0700 | [diff] [blame] | 625 | newFlags, _ := bazel.TryVariableSubstitutions(flags, productConfigProp.Name()) |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 626 | attr.SetSelectValue(productConfigProp.ConfigurationAxis(), productConfigProp.SelectKey(), newFlags) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 627 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 628 | } |
| 629 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 630 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 631 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 632 | func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, implementationHdrs, exportHdrs bazel.LabelListAttribute) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 633 | ca.srcs.ResolveExcludes() |
| 634 | partitionedSrcs := groupSrcsByExtension(ctx, ca.srcs) |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 635 | partitionedImplHdrs := partitionHeaders(ctx, implementationHdrs) |
| 636 | partitionedHdrs := partitionHeaders(ctx, exportHdrs) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 637 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 638 | ca.protoSrcs = partitionedSrcs[protoSrcPartition] |
Spandan Das | a99348d | 2023-08-01 23:10:05 +0000 | [diff] [blame] | 639 | ca.protoSrcs.Append(partitionedSrcs[protoFromGenPartition]) |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 640 | ca.aidlSrcs = partitionedSrcs[aidlSrcPartition] |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 641 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 642 | for p, lla := range partitionedSrcs { |
| 643 | // if there are no sources, there is no need for headers |
| 644 | if lla.IsEmpty() { |
| 645 | continue |
| 646 | } |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 647 | lla.Append(partitionedImplHdrs[hdrPartition]) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 648 | partitionedSrcs[p] = lla |
| 649 | } |
| 650 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 651 | ca.hdrs = partitionedHdrs[hdrPartition] |
| 652 | |
| 653 | ca.includesFromHeaders(ctx, partitionedImplHdrs[hdrPartition], partitionedHdrs[hdrPartition]) |
| 654 | |
| 655 | xsdSrcs := bazel.SubtractBazelLabelListAttribute(partitionedSrcs[xsdSrcPartition], partitionedHdrs[xsdSrcPartition]) |
| 656 | xsdSrcs.Append(partitionedImplHdrs[xsdSrcPartition]) |
| 657 | ca.exportXsdSrcs = partitionedHdrs[xsdSrcPartition] |
| 658 | ca.xsdSrcs = bazel.FirstUniqueBazelLabelListAttribute(xsdSrcs) |
| 659 | |
Liz Kammer | 0db0e34 | 2023-07-18 11:39:30 -0400 | [diff] [blame] | 660 | ca.genruleHeaders = partitionedImplHdrs[genrulePartition] |
| 661 | ca.exportGenruleHeaders = partitionedHdrs[genrulePartition] |
| 662 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 663 | ca.srcs = partitionedSrcs[cppSrcPartition] |
| 664 | ca.cSrcs = partitionedSrcs[cSrcPartition] |
| 665 | ca.asSrcs = partitionedSrcs[asSrcPartition] |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 666 | ca.asmSrcs = partitionedSrcs[asmSrcPartition] |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 667 | ca.lSrcs = partitionedSrcs[lSrcPartition] |
| 668 | ca.llSrcs = partitionedSrcs[llSrcPartition] |
Spandan Das | df4c213 | 2023-05-09 23:58:52 +0000 | [diff] [blame] | 669 | if yacc := partitionedSrcs[yaccSrcPartition]; !yacc.IsEmpty() { |
| 670 | if len(yacc.Value.Includes) > 1 { |
| 671 | ctx.PropertyErrorf("srcs", "Found multiple yacc (.y/.yy) files in library") |
| 672 | } |
| 673 | ca.yaccSrc = bazel.MakeLabelAttribute(yacc.Value.Includes[0].Label) |
| 674 | } |
Trevor Radcliffe | cee4e05 | 2022-09-06 19:31:25 +0000 | [diff] [blame] | 675 | ca.syspropSrcs = partitionedSrcs[syspropSrcPartition] |
Alix | e266787 | 2023-04-24 14:57:32 +0000 | [diff] [blame] | 676 | ca.rscriptSrcs = partitionedSrcs[rScriptSrcPartition] |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 677 | |
| 678 | ca.absoluteIncludes.DeduplicateAxesFromBase() |
| 679 | ca.localIncludes.DeduplicateAxesFromBase() |
| 680 | } |
| 681 | |
| 682 | // Parse srcs from an arch or OS's props value. |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 683 | func parseSrcs(ctx android.Bp2buildMutatorContext, props *BaseCompilerProperties) (bazel.LabelList, bool) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 684 | anySrcs := false |
| 685 | // Add srcs-like dependencies such as generated files. |
| 686 | // First create a LabelList containing these dependencies, then merge the values with srcs. |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 687 | genSrcs := props.Generated_sources |
Spandan Das | 922171d | 2023-05-31 23:32:40 +0000 | [diff] [blame] | 688 | generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, genSrcs, props.Exclude_generated_sources) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 689 | if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 { |
| 690 | anySrcs = true |
| 691 | } |
| 692 | |
| 693 | allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs) |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 694 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 695 | if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 { |
| 696 | anySrcs = true |
| 697 | } |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 698 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 699 | return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 700 | } |
| 701 | |
Liz Kammer | a5a29de | 2022-05-25 23:19:37 -0400 | [diff] [blame] | 702 | func bp2buildStdVal(std *string, prefix string, useGnu bool) *string { |
| 703 | defaultVal := prefix + "_std_default" |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 704 | // If c{,pp}std properties are not specified, don't generate them in the BUILD file. |
| 705 | // Defaults are handled by the toolchain definition. |
| 706 | // 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] | 707 | stdVal := proptools.StringDefault(std, defaultVal) |
| 708 | if stdVal == "experimental" || stdVal == defaultVal { |
| 709 | if stdVal == "experimental" { |
| 710 | stdVal = prefix + "_std_experimental" |
| 711 | } |
| 712 | if !useGnu { |
| 713 | stdVal += "_no_gnu" |
| 714 | } |
| 715 | } else if !useGnu { |
| 716 | stdVal = gnuToCReplacer.Replace(stdVal) |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 717 | } |
| 718 | |
Liz Kammer | a5a29de | 2022-05-25 23:19:37 -0400 | [diff] [blame] | 719 | if stdVal == defaultVal { |
| 720 | return nil |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 721 | } |
Liz Kammer | a5a29de | 2022-05-25 23:19:37 -0400 | [diff] [blame] | 722 | return &stdVal |
| 723 | } |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 724 | |
Liz Kammer | a5a29de | 2022-05-25 23:19:37 -0400 | [diff] [blame] | 725 | func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) { |
| 726 | useGnu := useGnuExtensions(gnu_extensions) |
| 727 | |
| 728 | return bp2buildStdVal(c_std, "c", useGnu), bp2buildStdVal(cpp_std, "cpp", useGnu) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 729 | } |
| 730 | |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 731 | // packageFromLabel extracts package from a fully-qualified or relative Label and whether the label |
| 732 | // is fully-qualified. |
| 733 | // e.g. fully-qualified "//a/b:foo" -> "a/b", true, relative: ":bar" -> ".", false |
| 734 | func packageFromLabel(label string) (string, bool) { |
| 735 | split := strings.Split(label, ":") |
| 736 | if len(split) != 2 { |
| 737 | return "", false |
| 738 | } |
| 739 | if split[0] == "" { |
| 740 | return ".", false |
| 741 | } |
| 742 | // remove leading "//" |
| 743 | return split[0][2:], true |
| 744 | } |
| 745 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 746 | // includesFromHeaders gets the include directories needed from generated headers |
| 747 | func (ca *compilerAttributes) includesFromHeaders(ctx android.BazelConversionPathContext, implHdrs, hdrs bazel.LabelListAttribute) { |
| 748 | local, absolute := includesFromLabelListAttribute(implHdrs, ca.localIncludes, ca.absoluteIncludes) |
| 749 | localExport, absoluteExport := includesFromLabelListAttribute(hdrs, ca.includes.Includes, ca.includes.AbsoluteIncludes) |
| 750 | |
| 751 | ca.localIncludes = local |
| 752 | ca.absoluteIncludes = absolute |
| 753 | |
| 754 | ca.includes.Includes = localExport |
| 755 | ca.includes.AbsoluteIncludes = absoluteExport |
| 756 | } |
| 757 | |
| 758 | // includesFromLabelList extracts the packages from a LabelListAttribute that should be includes and |
| 759 | // combines them with existing local/absolute includes. |
| 760 | func includesFromLabelListAttribute(attr bazel.LabelListAttribute, existingLocal, existingAbsolute bazel.StringListAttribute) (bazel.StringListAttribute, bazel.StringListAttribute) { |
| 761 | localAttr := existingLocal.Clone() |
| 762 | absoluteAttr := existingAbsolute.Clone() |
| 763 | if !attr.Value.IsEmpty() { |
| 764 | l, a := includesFromLabelList(attr.Value, existingLocal.Value, existingAbsolute.Value) |
| 765 | localAttr.SetSelectValue(bazel.NoConfigAxis, "", l) |
| 766 | absoluteAttr.SetSelectValue(bazel.NoConfigAxis, "", a) |
| 767 | } |
| 768 | for axis, configToLabels := range attr.ConfigurableValues { |
| 769 | for c, labels := range configToLabels { |
| 770 | local := existingLocal.SelectValue(axis, c) |
| 771 | absolute := existingAbsolute.SelectValue(axis, c) |
| 772 | l, a := includesFromLabelList(labels, local, absolute) |
| 773 | localAttr.SetSelectValue(axis, c, l) |
| 774 | absoluteAttr.SetSelectValue(axis, c, a) |
| 775 | } |
| 776 | } |
| 777 | return *localAttr, *absoluteAttr |
| 778 | } |
| 779 | |
| 780 | // includesFromLabelList extracts relative/absolute includes from a bazel.LabelList. |
| 781 | func includesFromLabelList(labelList bazel.LabelList, existingRel, existingAbs []string) ([]string, []string) { |
| 782 | var relative, absolute []string |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 783 | for _, hdr := range labelList.Includes { |
| 784 | if pkg, hasPkg := packageFromLabel(hdr.Label); hasPkg { |
| 785 | absolute = append(absolute, pkg) |
| 786 | } else if pkg != "" { |
| 787 | relative = append(relative, pkg) |
| 788 | } |
| 789 | } |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 790 | if len(relative)+len(existingRel) != 0 { |
| 791 | relative = android.FirstUniqueStrings(append(append([]string{}, existingRel...), relative...)) |
| 792 | } |
| 793 | if len(absolute)+len(existingAbs) != 0 { |
| 794 | absolute = android.FirstUniqueStrings(append(append([]string{}, existingAbs...), absolute...)) |
| 795 | } |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 796 | return relative, absolute |
| 797 | } |
| 798 | |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 799 | type YasmAttributes struct { |
| 800 | Srcs bazel.LabelListAttribute |
| 801 | Flags bazel.StringListAttribute |
| 802 | Include_dirs bazel.StringListAttribute |
| 803 | } |
| 804 | |
| 805 | func bp2BuildYasm(ctx android.Bp2buildMutatorContext, m *Module, ca compilerAttributes) *bazel.LabelAttribute { |
| 806 | if ca.asmSrcs.IsEmpty() { |
| 807 | return nil |
| 808 | } |
| 809 | |
| 810 | // Yasm needs the include directories from both local_includes and |
| 811 | // export_include_dirs. We don't care about actually exporting them from the |
| 812 | // yasm rule though, because they will also be present on the cc_ rule that |
| 813 | // wraps this yasm rule. |
| 814 | includes := ca.localIncludes.Clone() |
| 815 | bp2BuildPropParseHelper(ctx, m, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 816 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
| 817 | if len(flagExporterProperties.Export_include_dirs) > 0 { |
| 818 | x := bazel.StringListAttribute{} |
| 819 | x.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs) |
| 820 | includes.Append(x) |
| 821 | } |
| 822 | } |
| 823 | }) |
| 824 | |
| 825 | ctx.CreateBazelTargetModule( |
| 826 | bazel.BazelTargetModuleProperties{ |
| 827 | Rule_class: "yasm", |
| 828 | Bzl_load_location: "//build/bazel/rules/cc:yasm.bzl", |
| 829 | }, |
| 830 | android.CommonAttributes{Name: m.Name() + "_yasm"}, |
| 831 | &YasmAttributes{ |
| 832 | Srcs: ca.asmSrcs, |
| 833 | Flags: ca.asFlags, |
| 834 | Include_dirs: *includes, |
| 835 | }) |
| 836 | |
| 837 | // We only want to add a dependency on the _yasm target if there are asm |
| 838 | // sources in the current configuration. If there are unconfigured asm |
| 839 | // sources, always add the dependency. Otherwise, add the dependency only |
| 840 | // on the configuration axes and values that had asm sources. |
| 841 | if len(ca.asmSrcs.Value.Includes) > 0 { |
| 842 | return bazel.MakeLabelAttribute(":" + m.Name() + "_yasm") |
| 843 | } |
| 844 | |
| 845 | ret := &bazel.LabelAttribute{} |
| 846 | for _, axis := range ca.asmSrcs.SortedConfigurationAxes() { |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 847 | for cfg := range ca.asmSrcs.ConfigurableValues[axis] { |
| 848 | ret.SetSelectValue(axis, cfg, bazel.Label{Label: ":" + m.Name() + "_yasm"}) |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | return ret |
| 852 | } |
| 853 | |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame] | 854 | // bp2BuildParseBaseProps returns all compiler, linker, library attributes of a cc module. |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 855 | func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) baseAttributes { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 856 | archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) |
| 857 | archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 858 | archVariantLibraryProperties := module.GetArchVariantProperties(ctx, &LibraryProperties{}) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 859 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 860 | axisToConfigs := map[bazel.ConfigurationAxis]map[string]bool{} |
| 861 | allAxesAndConfigs := func(cp android.ConfigurationAxisToArchVariantProperties) { |
| 862 | for axis, configMap := range cp { |
| 863 | if _, ok := axisToConfigs[axis]; !ok { |
| 864 | axisToConfigs[axis] = map[string]bool{} |
| 865 | } |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 866 | for cfg := range configMap { |
| 867 | axisToConfigs[axis][cfg] = true |
Chris Parsons | a967f25 | 2021-09-23 16:34:35 -0400 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 871 | allAxesAndConfigs(archVariantCompilerProps) |
| 872 | allAxesAndConfigs(archVariantLinkerProps) |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 873 | allAxesAndConfigs(archVariantLibraryProperties) |
Chris Parsons | a967f25 | 2021-09-23 16:34:35 -0400 | [diff] [blame] | 874 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 875 | compilerAttrs := compilerAttributes{} |
| 876 | linkerAttrs := linkerAttributes{} |
| 877 | |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 878 | var aidlLibs bazel.LabelList |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 879 | var implementationHdrs, exportHdrs bazel.LabelListAttribute |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 880 | |
Chris Parsons | 7b3289b | 2023-01-26 17:30:44 -0500 | [diff] [blame] | 881 | // Iterate through these axes in a deterministic order. This is required |
| 882 | // because processing certain dependencies may result in concatenating |
| 883 | // elements along other axes. (For example, processing NoConfig may result |
| 884 | // in elements being added to InApex). This is thus the only way to ensure |
| 885 | // that the order of entries in each list is in a predictable order. |
| 886 | for _, axis := range bazel.SortedConfigurationAxes(axisToConfigs) { |
| 887 | configs := axisToConfigs[axis] |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 888 | for cfg := range configs { |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 889 | var allHdrs []string |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 890 | if baseCompilerProps, ok := archVariantCompilerProps[axis][cfg].(*BaseCompilerProperties); ok { |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 891 | allHdrs = baseCompilerProps.Generated_headers |
Spandan Das | 922171d | 2023-05-31 23:32:40 +0000 | [diff] [blame] | 892 | |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 893 | if baseCompilerProps.Lex != nil { |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 894 | compilerAttrs.lexopts.SetSelectValue(axis, cfg, baseCompilerProps.Lex.Flags) |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 895 | } |
Spandan Das | df4c213 | 2023-05-09 23:58:52 +0000 | [diff] [blame] | 896 | if baseCompilerProps.Yacc != nil { |
| 897 | compilerAttrs.yaccFlags.SetSelectValue(axis, cfg, baseCompilerProps.Yacc.Flags) |
| 898 | compilerAttrs.yaccGenLocationHeader.SetSelectValue(axis, cfg, baseCompilerProps.Yacc.Gen_location_hh) |
| 899 | compilerAttrs.yaccGenPositionHeader.SetSelectValue(axis, cfg, baseCompilerProps.Yacc.Gen_position_hh) |
| 900 | } |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 901 | (&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, cfg, baseCompilerProps) |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 902 | aidlLibs.Append(android.BazelLabelForModuleDeps(ctx, baseCompilerProps.Aidl.Libs)) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 903 | } |
| 904 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 905 | var exportedHdrs []string |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 906 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 907 | if baseLinkerProps, ok := archVariantLinkerProps[axis][cfg].(*BaseLinkerProperties); ok { |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 908 | exportedHdrs = baseLinkerProps.Export_generated_headers |
Liz Kammer | 48cdbeb | 2023-03-17 10:17:50 -0400 | [diff] [blame] | 909 | (&linkerAttrs).bp2buildForAxisAndConfig(ctx, module, axis, cfg, baseLinkerProps) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 910 | } |
Liz Kammer | 084d6a9 | 2023-06-22 16:23:53 -0400 | [diff] [blame] | 911 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 912 | headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportedHdrs, android.BazelLabelForModuleDeps) |
Liz Kammer | 084d6a9 | 2023-06-22 16:23:53 -0400 | [diff] [blame] | 913 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 914 | implementationHdrs.SetSelectValue(axis, cfg, headers.implementation) |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 915 | exportHdrs.SetSelectValue(axis, cfg, headers.export) |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 916 | |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 917 | if libraryProps, ok := archVariantLibraryProperties[axis][cfg].(*LibraryProperties); ok { |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 918 | if axis == bazel.NoConfigAxis { |
Sam Delmerico | 75dbca2 | 2023-04-20 13:13:25 +0000 | [diff] [blame] | 919 | if libraryProps.Stubs.Symbol_file != nil { |
| 920 | compilerAttrs.stubsSymbolFile = libraryProps.Stubs.Symbol_file |
| 921 | versions := android.CopyOf(libraryProps.Stubs.Versions) |
| 922 | normalizeVersions(ctx, versions) |
| 923 | versions = addCurrentVersionIfNotPresent(versions) |
| 924 | compilerAttrs.stubsVersions.SetSelectValue(axis, cfg, versions) |
| 925 | } |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 926 | } |
Spandan Das | 39ccf93 | 2023-05-26 18:03:39 +0000 | [diff] [blame] | 927 | if stem := libraryProps.Stem; stem != nil { |
| 928 | compilerAttrs.stem.SetSelectValue(axis, cfg, stem) |
| 929 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a56e970 | 2022-02-23 18:39:59 -0500 | [diff] [blame] | 930 | if suffix := libraryProps.Suffix; suffix != nil { |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 931 | 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] | 932 | } |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 933 | } |
Liz Kammer | 084d6a9 | 2023-06-22 16:23:53 -0400 | [diff] [blame] | 934 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 935 | } |
| 936 | } |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 937 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 938 | compilerAttrs.convertStlProps(ctx, module) |
| 939 | (&linkerAttrs).convertStripProps(ctx, module) |
| 940 | |
Yu Liu | f01a0f0 | 2022-12-07 15:45:30 -0800 | [diff] [blame] | 941 | var nativeCoverage *bool |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 942 | if module.coverage != nil && module.coverage.Properties.Native_coverage != nil && |
| 943 | !Bool(module.coverage.Properties.Native_coverage) { |
Yu Liu | f01a0f0 | 2022-12-07 15:45:30 -0800 | [diff] [blame] | 944 | nativeCoverage = BoolPtr(false) |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Liz Kammer | 9e2a5a7 | 2023-09-19 08:41:14 -0400 | [diff] [blame] | 947 | productVariableProps, errs := android.ProductVariableProperties(ctx, ctx.Module()) |
| 948 | for _, err := range errs { |
| 949 | ctx.ModuleErrorf("ProductVariableProperties error: %s", err) |
| 950 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 951 | |
| 952 | (&compilerAttrs).convertProductVariables(ctx, productVariableProps) |
| 953 | (&linkerAttrs).convertProductVariables(ctx, productVariableProps) |
| 954 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 955 | (&compilerAttrs).finalize(ctx, implementationHdrs, exportHdrs) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 956 | (&linkerAttrs).finalize(ctx) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 957 | |
Cole Faust | 7071a05 | 2022-07-29 15:58:33 -0700 | [diff] [blame] | 958 | (&compilerAttrs.srcs).Add(bp2BuildYasm(ctx, module, compilerAttrs)) |
| 959 | |
Liz Kammer | 0db0e34 | 2023-07-18 11:39:30 -0400 | [diff] [blame] | 960 | (&linkerAttrs).deps.Append(compilerAttrs.exportGenruleHeaders) |
| 961 | (&linkerAttrs).implementationDeps.Append(compilerAttrs.genruleHeaders) |
| 962 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 963 | (&linkerAttrs).wholeArchiveDeps.Append(compilerAttrs.exportXsdSrcs) |
| 964 | (&linkerAttrs).implementationWholeArchiveDeps.Append(compilerAttrs.xsdSrcs) |
| 965 | |
Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 966 | protoDep := bp2buildProto(ctx, module, compilerAttrs.protoSrcs, linkerAttrs) |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 967 | |
| 968 | // bp2buildProto will only set wholeStaticLib or implementationWholeStaticLib, but we don't know |
| 969 | // which. This will add the newly generated proto library to the appropriate attribute and nothing |
| 970 | // to the other |
| 971 | (&linkerAttrs).wholeArchiveDeps.Add(protoDep.wholeStaticLib) |
| 972 | (&linkerAttrs).implementationWholeArchiveDeps.Add(protoDep.implementationWholeStaticLib) |
Vinh Tran | fde57eb | 2022-08-29 17:46:58 -0400 | [diff] [blame] | 973 | |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 974 | aidlDep := bp2buildCcAidlLibrary( |
| 975 | ctx, module, |
| 976 | compilerAttrs.aidlSrcs, |
| 977 | bazel.LabelListAttribute{ |
| 978 | Value: aidlLibs, |
| 979 | }, |
| 980 | linkerAttrs, |
Vinh Tran | e684294 | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 981 | compilerAttrs, |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 982 | ) |
Vinh Tran | fde57eb | 2022-08-29 17:46:58 -0400 | [diff] [blame] | 983 | if aidlDep != nil { |
| 984 | if lib, ok := module.linker.(*libraryDecorator); ok { |
| 985 | if proptools.Bool(lib.Properties.Aidl.Export_aidl_headers) { |
| 986 | (&linkerAttrs).wholeArchiveDeps.Add(aidlDep) |
| 987 | } else { |
| 988 | (&linkerAttrs).implementationWholeArchiveDeps.Add(aidlDep) |
| 989 | } |
| 990 | } |
| 991 | } |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 992 | |
Spandan Das | df4c213 | 2023-05-09 23:58:52 +0000 | [diff] [blame] | 993 | // Create a cc_yacc_static_library if srcs contains .y/.yy files |
| 994 | // This internal target will produce an .a file that will be statically linked to the parent library |
| 995 | if yaccDep := bp2buildCcYaccLibrary(ctx, compilerAttrs, linkerAttrs); yaccDep != nil { |
| 996 | (&linkerAttrs).implementationWholeArchiveDeps.Add(yaccDep) |
| 997 | } |
| 998 | |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame] | 999 | convertedLSrcs := bp2BuildLex(ctx, module.Name(), compilerAttrs) |
| 1000 | (&compilerAttrs).srcs.Add(&convertedLSrcs.srcName) |
| 1001 | (&compilerAttrs).cSrcs.Add(&convertedLSrcs.cSrcName) |
| 1002 | |
Vinh Tran | 99270ea | 2022-11-28 11:15:23 -0500 | [diff] [blame] | 1003 | if module.afdo != nil && module.afdo.Properties.Afdo { |
| 1004 | fdoProfileDep := bp2buildFdoProfile(ctx, module) |
| 1005 | if fdoProfileDep != nil { |
Vinh Tran | ce40b92 | 2023-06-05 12:57:55 -0400 | [diff] [blame] | 1006 | // TODO(b/276287371): Only set fdo_profile for android platform |
| 1007 | // https://cs.android.com/android/platform/superproject/main/+/main:build/soong/cc/afdo.go;l=105;drc=2dbe160d1af445de32725098570ec594e3944fc5 |
Vinh Tran | 99270ea | 2022-11-28 11:15:23 -0500 | [diff] [blame] | 1008 | (&compilerAttrs).fdoProfile.SetValue(*fdoProfileDep) |
| 1009 | } |
| 1010 | } |
| 1011 | |
Trevor Radcliffe | cee4e05 | 2022-09-06 19:31:25 +0000 | [diff] [blame] | 1012 | if !compilerAttrs.syspropSrcs.IsEmpty() { |
| 1013 | (&linkerAttrs).wholeArchiveDeps.Add(bp2buildCcSysprop(ctx, module.Name(), module.Properties.Min_sdk_version, compilerAttrs.syspropSrcs)) |
| 1014 | } |
| 1015 | |
Zi Wang | 9f609db | 2023-01-04 11:06:54 -0800 | [diff] [blame] | 1016 | linkerAttrs.wholeArchiveDeps.Prepend = true |
| 1017 | linkerAttrs.deps.Prepend = true |
| 1018 | compilerAttrs.localIncludes.Prepend = true |
| 1019 | compilerAttrs.absoluteIncludes.Prepend = true |
| 1020 | compilerAttrs.hdrs.Prepend = true |
| 1021 | |
Alix | e266787 | 2023-04-24 14:57:32 +0000 | [diff] [blame] | 1022 | convertedRsSrcs, rsAbsIncludes, rsLocalIncludes := bp2buildRScript(ctx, module, compilerAttrs) |
| 1023 | (&compilerAttrs).srcs.Add(&convertedRsSrcs) |
| 1024 | (&compilerAttrs).absoluteIncludes.Append(rsAbsIncludes) |
| 1025 | (&compilerAttrs).localIncludes.Append(rsLocalIncludes) |
| 1026 | (&compilerAttrs).localIncludes.Value = android.FirstUniqueStrings(compilerAttrs.localIncludes.Value) |
| 1027 | |
Trevor Radcliffe | d9b7f17 | 2023-08-09 22:21:38 +0000 | [diff] [blame] | 1028 | sanitizerValues := bp2buildSanitizerFeatures(ctx, module) |
| 1029 | |
| 1030 | features := compilerAttrs.features.Clone().Append(linkerAttrs.features).Append(sanitizerValues.features) |
Trevor Radcliffe | 56b1a2b | 2023-02-06 21:58:30 +0000 | [diff] [blame] | 1031 | features = features.Append(bp2buildLtoFeatures(ctx, module)) |
Trevor Radcliffe | a8b4416 | 2023-04-14 18:25:24 +0000 | [diff] [blame] | 1032 | features = features.Append(convertHiddenVisibilityToFeatureBase(ctx, module)) |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 1033 | features.DeduplicateAxesFromBase() |
| 1034 | |
Trevor Radcliffe | d9b7f17 | 2023-08-09 22:21:38 +0000 | [diff] [blame] | 1035 | compilerAttrs.copts = *compilerAttrs.copts.Append(sanitizerValues.copts) |
| 1036 | compilerAttrs.additionalCompilerInputs = *compilerAttrs.additionalCompilerInputs.Append(sanitizerValues.additionalCompilerInputs) |
| 1037 | |
Trevor Radcliffe | 0d1b402 | 2022-12-12 22:26:34 +0000 | [diff] [blame] | 1038 | addMuslSystemDynamicDeps(ctx, linkerAttrs) |
| 1039 | |
Spandan Das | f3ab29b | 2023-08-17 00:23:59 +0000 | [diff] [blame] | 1040 | // Dedupe all deps. |
| 1041 | (&linkerAttrs).deps.Value = bazel.FirstUniqueBazelLabelList((&linkerAttrs).deps.Value) |
| 1042 | (&linkerAttrs).implementationDeps.Value = bazel.FirstUniqueBazelLabelList((&linkerAttrs).implementationDeps.Value) |
| 1043 | (&linkerAttrs).implementationDynamicDeps.Value = bazel.FirstUniqueBazelLabelList((&linkerAttrs).implementationDynamicDeps.Value) |
| 1044 | (&linkerAttrs).wholeArchiveDeps.Value = bazel.FirstUniqueBazelLabelList((&linkerAttrs).wholeArchiveDeps.Value) |
| 1045 | (&linkerAttrs).implementationWholeArchiveDeps.Value = bazel.FirstUniqueBazelLabelList((&linkerAttrs).implementationWholeArchiveDeps.Value) |
| 1046 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1047 | return baseAttributes{ |
| 1048 | compilerAttrs, |
| 1049 | linkerAttrs, |
Cole Faust | 5fa4e96 | 2022-08-22 14:31:04 -0700 | [diff] [blame] | 1050 | *features, |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 1051 | protoDep.protoDep, |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 1052 | aidlDep, |
Yu Liu | f01a0f0 | 2022-12-07 15:45:30 -0800 | [diff] [blame] | 1053 | nativeCoverage, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 1054 | } |
| 1055 | } |
| 1056 | |
Spandan Das | df4c213 | 2023-05-09 23:58:52 +0000 | [diff] [blame] | 1057 | type ccYaccLibraryAttributes struct { |
| 1058 | Src bazel.LabelAttribute |
| 1059 | Flags bazel.StringListAttribute |
| 1060 | Gen_location_hh bazel.BoolAttribute |
| 1061 | Gen_position_hh bazel.BoolAttribute |
| 1062 | Local_includes bazel.StringListAttribute |
| 1063 | Implementation_deps bazel.LabelListAttribute |
| 1064 | Implementation_dynamic_deps bazel.LabelListAttribute |
| 1065 | } |
| 1066 | |
| 1067 | func bp2buildCcYaccLibrary(ctx android.Bp2buildMutatorContext, ca compilerAttributes, la linkerAttributes) *bazel.LabelAttribute { |
| 1068 | if ca.yaccSrc == nil { |
| 1069 | return nil |
| 1070 | } |
| 1071 | yaccLibraryLabel := ctx.Module().Name() + "_yacc" |
| 1072 | ctx.CreateBazelTargetModule( |
| 1073 | bazel.BazelTargetModuleProperties{ |
| 1074 | Rule_class: "cc_yacc_static_library", |
| 1075 | Bzl_load_location: "//build/bazel/rules/cc:cc_yacc_library.bzl", |
| 1076 | }, |
| 1077 | android.CommonAttributes{ |
| 1078 | Name: yaccLibraryLabel, |
| 1079 | }, |
| 1080 | &ccYaccLibraryAttributes{ |
| 1081 | Src: *ca.yaccSrc, |
| 1082 | Flags: ca.yaccFlags, |
| 1083 | Gen_location_hh: ca.yaccGenLocationHeader, |
| 1084 | Gen_position_hh: ca.yaccGenPositionHeader, |
| 1085 | Local_includes: ca.localIncludes, |
| 1086 | Implementation_deps: la.implementationDeps, |
| 1087 | Implementation_dynamic_deps: la.implementationDynamicDeps, |
| 1088 | }, |
| 1089 | ) |
| 1090 | |
| 1091 | yaccLibrary := &bazel.LabelAttribute{ |
| 1092 | Value: &bazel.Label{ |
| 1093 | Label: ":" + yaccLibraryLabel, |
| 1094 | }, |
| 1095 | } |
| 1096 | return yaccLibrary |
| 1097 | } |
| 1098 | |
Trevor Radcliffe | 0d1b402 | 2022-12-12 22:26:34 +0000 | [diff] [blame] | 1099 | // As a workaround for b/261657184, we are manually adding the default value |
| 1100 | // of system_dynamic_deps for the linux_musl os. |
| 1101 | // TODO: Solve this properly |
| 1102 | func addMuslSystemDynamicDeps(ctx android.Bp2buildMutatorContext, attrs linkerAttributes) { |
| 1103 | systemDynamicDeps := attrs.systemDynamicDeps.SelectValue(bazel.OsConfigurationAxis, "linux_musl") |
| 1104 | if attrs.systemDynamicDeps.HasAxisSpecificValues(bazel.OsConfigurationAxis) && systemDynamicDeps.IsNil() { |
| 1105 | attrs.systemDynamicDeps.SetSelectValue(bazel.OsConfigurationAxis, "linux_musl", android.BazelLabelForModuleDeps(ctx, config.MuslDefaultSharedLibraries)) |
| 1106 | } |
| 1107 | } |
| 1108 | |
Vinh Tran | 99270ea | 2022-11-28 11:15:23 -0500 | [diff] [blame] | 1109 | type fdoProfileAttributes struct { |
| 1110 | Absolute_path_profile string |
| 1111 | } |
| 1112 | |
| 1113 | func bp2buildFdoProfile( |
| 1114 | ctx android.Bp2buildMutatorContext, |
| 1115 | m *Module, |
| 1116 | ) *bazel.Label { |
Vinh Tran | ce40b92 | 2023-06-05 12:57:55 -0400 | [diff] [blame] | 1117 | // TODO(b/267229066): Convert to afdo boolean attribute and let Bazel handles finding |
| 1118 | // fdo_profile target from AfdoProfiles product var |
Vinh Tran | 99270ea | 2022-11-28 11:15:23 -0500 | [diff] [blame] | 1119 | for _, project := range globalAfdoProfileProjects { |
Vinh Tran | ce40b92 | 2023-06-05 12:57:55 -0400 | [diff] [blame] | 1120 | // Ensure it's a Soong package |
| 1121 | bpPath := android.ExistentPathForSource(ctx, project, "Android.bp") |
| 1122 | if bpPath.Valid() { |
Vinh Tran | bc9c8b4 | 2022-12-09 12:03:52 -0500 | [diff] [blame] | 1123 | // TODO(b/260714900): Handle arch-specific afdo profiles (e.g. `<module-name>-arm<64>.afdo`) |
| 1124 | path := android.ExistentPathForSource(ctx, project, m.Name()+".afdo") |
| 1125 | if path.Valid() { |
Vinh Tran | bc9c8b4 | 2022-12-09 12:03:52 -0500 | [diff] [blame] | 1126 | fdoProfileLabel := "//" + strings.TrimSuffix(project, "/") + ":" + m.Name() |
| 1127 | return &bazel.Label{ |
| 1128 | Label: fdoProfileLabel, |
| 1129 | } |
Vinh Tran | 99270ea | 2022-11-28 11:15:23 -0500 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | return nil |
| 1135 | } |
| 1136 | |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 1137 | func bp2buildCcAidlLibrary( |
| 1138 | ctx android.Bp2buildMutatorContext, |
| 1139 | m *Module, |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1140 | aidlSrcs bazel.LabelListAttribute, |
| 1141 | aidlLibs bazel.LabelListAttribute, |
Vinh Tran | 395a1e9 | 2022-09-16 18:27:29 -0400 | [diff] [blame] | 1142 | linkerAttrs linkerAttributes, |
Vinh Tran | e684294 | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1143 | compilerAttrs compilerAttributes, |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 1144 | ) *bazel.LabelAttribute { |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1145 | var aidlLibsFromSrcs, aidlFiles bazel.LabelListAttribute |
Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 1146 | apexAvailableTags := android.ApexAvailableTagsWithoutTestApexes(ctx, ctx.Module()) |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1147 | |
| 1148 | if !aidlSrcs.IsEmpty() { |
| 1149 | aidlLibsFromSrcs, aidlFiles = aidlSrcs.Partition(func(src bazel.Label) bool { |
Vinh Tran | a3b8b78 | 2022-09-14 11:40:24 -0400 | [diff] [blame] | 1150 | if fg, ok := android.ToFileGroupAsLibrary(ctx, src.OriginalModuleName); ok && |
| 1151 | fg.ShouldConvertToAidlLibrary(ctx) { |
| 1152 | return true |
| 1153 | } |
| 1154 | return false |
| 1155 | }) |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 1156 | |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1157 | if !aidlFiles.IsEmpty() { |
Vinh Tran | a3b8b78 | 2022-09-14 11:40:24 -0400 | [diff] [blame] | 1158 | aidlLibName := m.Name() + "_aidl_library" |
| 1159 | ctx.CreateBazelTargetModule( |
| 1160 | bazel.BazelTargetModuleProperties{ |
| 1161 | Rule_class: "aidl_library", |
Sam Delmerico | e55bf08 | 2023-03-31 09:47:28 -0400 | [diff] [blame] | 1162 | Bzl_load_location: "//build/bazel/rules/aidl:aidl_library.bzl", |
Vinh Tran | a3b8b78 | 2022-09-14 11:40:24 -0400 | [diff] [blame] | 1163 | }, |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1164 | android.CommonAttributes{ |
| 1165 | Name: aidlLibName, |
Liz Kammer | 2b3f56e | 2023-03-23 11:51:49 -0400 | [diff] [blame] | 1166 | Tags: apexAvailableTags, |
Vinh Tran | a3b8b78 | 2022-09-14 11:40:24 -0400 | [diff] [blame] | 1167 | }, |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1168 | &aidlLibraryAttributes{ |
| 1169 | Srcs: aidlFiles, |
Vinh Tran | a3b8b78 | 2022-09-14 11:40:24 -0400 | [diff] [blame] | 1170 | }, |
| 1171 | ) |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1172 | aidlLibsFromSrcs.Add(&bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + aidlLibName}}) |
Vinh Tran | a3b8b78 | 2022-09-14 11:40:24 -0400 | [diff] [blame] | 1173 | } |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 1174 | } |
| 1175 | |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1176 | allAidlLibs := aidlLibs.Clone() |
| 1177 | allAidlLibs.Append(aidlLibsFromSrcs) |
| 1178 | |
| 1179 | if !allAidlLibs.IsEmpty() { |
| 1180 | ccAidlLibrarylabel := m.Name() + "_cc_aidl_library" |
| 1181 | // Since parent cc_library already has these dependencies, we can add them as implementation |
| 1182 | // deps so that they don't re-export |
| 1183 | implementationDeps := linkerAttrs.deps.Clone() |
| 1184 | implementationDeps.Append(linkerAttrs.implementationDeps) |
| 1185 | implementationDynamicDeps := linkerAttrs.dynamicDeps.Clone() |
| 1186 | implementationDynamicDeps.Append(linkerAttrs.implementationDynamicDeps) |
| 1187 | |
Yu Liu | f11b7c3 | 2023-10-20 19:15:51 +0000 | [diff] [blame^] | 1188 | sdkAttrs := Bp2BuildParseSdkAttributes(m) |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1189 | |
Vinh Tran | e684294 | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1190 | exportedIncludes := bp2BuildParseExportedIncludes(ctx, m, &compilerAttrs.includes) |
| 1191 | includeAttrs := includesAttributes{ |
| 1192 | Export_includes: exportedIncludes.Includes, |
| 1193 | Export_absolute_includes: exportedIncludes.AbsoluteIncludes, |
| 1194 | Export_system_includes: exportedIncludes.SystemIncludes, |
| 1195 | Local_includes: compilerAttrs.localIncludes, |
| 1196 | Absolute_includes: compilerAttrs.absoluteIncludes, |
| 1197 | } |
| 1198 | |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1199 | ctx.CreateBazelTargetModule( |
| 1200 | bazel.BazelTargetModuleProperties{ |
| 1201 | Rule_class: "cc_aidl_library", |
| 1202 | Bzl_load_location: "//build/bazel/rules/cc:cc_aidl_library.bzl", |
| 1203 | }, |
| 1204 | android.CommonAttributes{Name: ccAidlLibrarylabel}, |
| 1205 | &ccAidlLibraryAttributes{ |
| 1206 | Deps: *allAidlLibs, |
| 1207 | Implementation_deps: *implementationDeps, |
| 1208 | Implementation_dynamic_deps: *implementationDynamicDeps, |
| 1209 | Tags: apexAvailableTags, |
Yu Liu | f11b7c3 | 2023-10-20 19:15:51 +0000 | [diff] [blame^] | 1210 | SdkAttributes: sdkAttrs, |
Vinh Tran | e684294 | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1211 | includesAttributes: includeAttrs, |
Vinh Tran | 367d89d | 2023-04-28 11:21:25 -0400 | [diff] [blame] | 1212 | }, |
| 1213 | ) |
| 1214 | label := &bazel.LabelAttribute{ |
| 1215 | Value: &bazel.Label{ |
| 1216 | Label: ":" + ccAidlLibrarylabel, |
| 1217 | }, |
| 1218 | } |
| 1219 | return label |
| 1220 | } |
| 1221 | |
Vinh Tran | a3b8b78 | 2022-09-14 11:40:24 -0400 | [diff] [blame] | 1222 | return nil |
Vinh Tran | 9f6796a | 2022-08-16 13:10:31 -0400 | [diff] [blame] | 1223 | } |
| 1224 | |
Yu Liu | f11b7c3 | 2023-10-20 19:15:51 +0000 | [diff] [blame^] | 1225 | func Bp2BuildParseSdkAttributes(module *Module) SdkAttributes { |
| 1226 | return SdkAttributes{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 1227 | Sdk_version: module.Properties.Sdk_version, |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 1228 | Min_sdk_version: module.Properties.Min_sdk_version, |
| 1229 | } |
| 1230 | } |
| 1231 | |
Yu Liu | f11b7c3 | 2023-10-20 19:15:51 +0000 | [diff] [blame^] | 1232 | type SdkAttributes struct { |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 1233 | Sdk_version *string |
| 1234 | Min_sdk_version *string |
| 1235 | } |
| 1236 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 1237 | // Convenience struct to hold all attributes parsed from linker properties. |
| 1238 | type linkerAttributes struct { |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1239 | deps bazel.LabelListAttribute |
| 1240 | implementationDeps bazel.LabelListAttribute |
| 1241 | dynamicDeps bazel.LabelListAttribute |
| 1242 | implementationDynamicDeps bazel.LabelListAttribute |
Cole Faust | 6b29f59 | 2022-08-09 09:50:56 -0700 | [diff] [blame] | 1243 | runtimeDeps bazel.LabelListAttribute |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1244 | wholeArchiveDeps bazel.LabelListAttribute |
| 1245 | implementationWholeArchiveDeps bazel.LabelListAttribute |
| 1246 | systemDynamicDeps bazel.LabelListAttribute |
Liz Kammer | b492843 | 2023-06-02 18:43:36 -0400 | [diff] [blame] | 1247 | usedSystemDynamicDepAsStaticDep map[string]bool |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1248 | usedSystemDynamicDepAsDynamicDep map[string]bool |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 1249 | |
Rupert Shuttleworth | 484aa25 | 2021-12-10 07:22:53 -0500 | [diff] [blame] | 1250 | useVersionLib bazel.BoolAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 1251 | linkopts bazel.StringListAttribute |
Liz Kammer | d287118 | 2021-10-04 13:54:37 -0400 | [diff] [blame] | 1252 | additionalLinkerInputs bazel.LabelListAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 1253 | stripKeepSymbols bazel.BoolAttribute |
| 1254 | stripKeepSymbolsAndDebugFrame bazel.BoolAttribute |
| 1255 | stripKeepSymbolsList bazel.StringListAttribute |
| 1256 | stripAll bazel.BoolAttribute |
| 1257 | stripNone bazel.BoolAttribute |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 1258 | features bazel.StringListAttribute |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 1259 | } |
| 1260 | |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1261 | var ( |
| 1262 | soongSystemSharedLibs = []string{"libc", "libm", "libdl"} |
Liz Kammer | baced71 | 2022-09-16 09:01:29 -0400 | [diff] [blame] | 1263 | versionLib = "libbuildversion" |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1264 | ) |
| 1265 | |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1266 | // resolveTargetApex re-adds the shared and static libs in target.apex.exclude_shared|static_libs props to non-apex variant |
| 1267 | // since all libs are already excluded by default |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 1268 | func (la *linkerAttributes) resolveTargetApexProp(ctx android.Bp2buildMutatorContext, props *BaseLinkerProperties) { |
Liz Kammer | 748d707 | 2023-01-25 12:07:43 -0500 | [diff] [blame] | 1269 | excludeSharedLibs := bazelLabelForSharedDeps(ctx, props.Target.Apex.Exclude_shared_libs) |
| 1270 | sharedExcludes := bazel.LabelList{Excludes: excludeSharedLibs.Includes} |
| 1271 | sharedExcludesLabelList := bazel.LabelListAttribute{} |
| 1272 | sharedExcludesLabelList.SetSelectValue(bazel.InApexAxis, bazel.InApex, sharedExcludes) |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1273 | |
Liz Kammer | 748d707 | 2023-01-25 12:07:43 -0500 | [diff] [blame] | 1274 | la.dynamicDeps.Append(sharedExcludesLabelList) |
| 1275 | la.implementationDynamicDeps.Append(sharedExcludesLabelList) |
| 1276 | |
| 1277 | excludeStaticLibs := bazelLabelForStaticDeps(ctx, props.Target.Apex.Exclude_static_libs) |
| 1278 | staticExcludes := bazel.LabelList{Excludes: excludeStaticLibs.Includes} |
| 1279 | staticExcludesLabelList := bazel.LabelListAttribute{} |
| 1280 | staticExcludesLabelList.SetSelectValue(bazel.InApexAxis, bazel.InApex, staticExcludes) |
| 1281 | |
| 1282 | la.deps.Append(staticExcludesLabelList) |
| 1283 | la.implementationDeps.Append(staticExcludesLabelList) |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1284 | } |
| 1285 | |
Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 1286 | func (la *linkerAttributes) bp2buildForAxisAndConfig(ctx android.Bp2buildMutatorContext, module *Module, axis bazel.ConfigurationAxis, config string, props *BaseLinkerProperties) { |
Liz Kammer | 48cdbeb | 2023-03-17 10:17:50 -0400 | [diff] [blame] | 1287 | isBinary := module.Binary() |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1288 | // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module |
| 1289 | var axisFeatures []string |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 1290 | |
Liz Kammer | cc2c1ef | 2022-03-21 09:03:29 -0400 | [diff] [blame] | 1291 | wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs) |
Liz Kammer | baced71 | 2022-09-16 09:01:29 -0400 | [diff] [blame] | 1292 | staticLibs := android.FirstUniqueStrings(android.RemoveListFromList(props.Static_libs, wholeStaticLibs)) |
| 1293 | if axis == bazel.NoConfigAxis { |
| 1294 | la.useVersionLib.SetSelectValue(axis, config, props.Use_version_lib) |
| 1295 | if proptools.Bool(props.Use_version_lib) { |
| 1296 | versionLibAlreadyInDeps := android.InList(versionLib, wholeStaticLibs) |
| 1297 | // remove from static libs so there is no duplicate dependency |
| 1298 | _, staticLibs = android.RemoveFromList(versionLib, staticLibs) |
| 1299 | // only add the dep if it is not in progress |
| 1300 | if !versionLibAlreadyInDeps { |
Yu Liu | fe978fd | 2023-04-24 16:37:18 -0700 | [diff] [blame] | 1301 | wholeStaticLibs = append(wholeStaticLibs, versionLib) |
Liz Kammer | baced71 | 2022-09-16 09:01:29 -0400 | [diff] [blame] | 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1306 | // Excludes to parallel Soong: |
| 1307 | // 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] | 1308 | la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs)) |
Liz Kammer | cc2c1ef | 2022-03-21 09:03:29 -0400 | [diff] [blame] | 1309 | |
Liz Kammer | b492843 | 2023-06-02 18:43:36 -0400 | [diff] [blame] | 1310 | if isBinary && module.StaticExecutable() { |
| 1311 | usedSystemStatic := android.FilterListPred(staticLibs, func(s string) bool { |
| 1312 | return android.InList(s, soongSystemSharedLibs) && !android.InList(s, props.Exclude_static_libs) |
| 1313 | }) |
| 1314 | |
| 1315 | for _, el := range usedSystemStatic { |
| 1316 | if la.usedSystemDynamicDepAsStaticDep == nil { |
| 1317 | la.usedSystemDynamicDepAsStaticDep = map[string]bool{} |
| 1318 | } |
| 1319 | la.usedSystemDynamicDepAsStaticDep[el] = true |
| 1320 | } |
| 1321 | } |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1322 | staticDeps := maybePartitionExportedAndImplementationsDepsExcludes( |
| 1323 | ctx, |
| 1324 | !isBinary, |
| 1325 | staticLibs, |
Liz Kammer | 748d707 | 2023-01-25 12:07:43 -0500 | [diff] [blame] | 1326 | props.Exclude_static_libs, |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1327 | props.Export_static_lib_headers, |
| 1328 | bazelLabelForStaticDepsExcludes, |
| 1329 | ) |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 1330 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1331 | headerLibs := android.FirstUniqueStrings(props.Header_libs) |
| 1332 | hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 1333 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1334 | (&hDeps.export).Append(staticDeps.export) |
| 1335 | la.deps.SetSelectValue(axis, config, hDeps.export) |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 1336 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1337 | (&hDeps.implementation).Append(staticDeps.implementation) |
| 1338 | la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation) |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 1339 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1340 | systemSharedLibs := props.System_shared_libs |
| 1341 | // systemSharedLibs distinguishes between nil/empty list behavior: |
| 1342 | // nil -> use default values |
| 1343 | // empty list -> no values specified |
| 1344 | if len(systemSharedLibs) > 0 { |
| 1345 | systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs) |
| 1346 | } |
| 1347 | la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs)) |
| 1348 | |
| 1349 | sharedLibs := android.FirstUniqueStrings(props.Shared_libs) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1350 | excludeSharedLibs := props.Exclude_shared_libs |
| 1351 | usedSystem := android.FilterListPred(sharedLibs, func(s string) bool { |
| 1352 | return android.InList(s, soongSystemSharedLibs) && !android.InList(s, excludeSharedLibs) |
| 1353 | }) |
Liz Kammer | b492843 | 2023-06-02 18:43:36 -0400 | [diff] [blame] | 1354 | |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1355 | for _, el := range usedSystem { |
| 1356 | if la.usedSystemDynamicDepAsDynamicDep == nil { |
| 1357 | la.usedSystemDynamicDepAsDynamicDep = map[string]bool{} |
| 1358 | } |
| 1359 | la.usedSystemDynamicDepAsDynamicDep[el] = true |
| 1360 | } |
| 1361 | |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1362 | sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes( |
| 1363 | ctx, |
| 1364 | !isBinary, |
| 1365 | sharedLibs, |
Liz Kammer | 748d707 | 2023-01-25 12:07:43 -0500 | [diff] [blame] | 1366 | props.Exclude_shared_libs, |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1367 | props.Export_shared_lib_headers, |
| 1368 | bazelLabelForSharedDepsExcludes, |
| 1369 | ) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1370 | la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export) |
| 1371 | la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation) |
Liz Kammer | 748d707 | 2023-01-25 12:07:43 -0500 | [diff] [blame] | 1372 | la.resolveTargetApexProp(ctx, props) |
Vinh Tran | 85fb07c | 2022-09-16 16:17:48 -0400 | [diff] [blame] | 1373 | |
Wei Li | 81852ca | 2022-07-27 00:22:06 -0700 | [diff] [blame] | 1374 | if axis == bazel.NoConfigAxis || (axis == bazel.OsConfigurationAxis && config == bazel.OsAndroid) { |
Yu Liu | 10174ff | 2023-02-21 12:05:26 -0800 | [diff] [blame] | 1375 | // If a dependency in la.implementationDynamicDeps or la.dynamicDeps has stubs, its |
| 1376 | // stub variant should be used when the dependency is linked in a APEX. The |
| 1377 | // dependencies in NoConfigAxis and OsConfigurationAxis/OsAndroid are grouped by |
| 1378 | // having stubs or not, so Bazel select() statement can be used to choose |
| 1379 | // source/stub variants of them. |
Liz Kammer | 48cdbeb | 2023-03-17 10:17:50 -0400 | [diff] [blame] | 1380 | apexAvailable := module.ApexAvailable() |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 1381 | SetStubsForDynamicDeps(ctx, axis, config, apexAvailable, sharedDeps.export, &la.dynamicDeps, &la.deps, 0, false) |
| 1382 | SetStubsForDynamicDeps(ctx, axis, config, apexAvailable, sharedDeps.implementation, &la.implementationDynamicDeps, &la.deps, 1, false) |
Spandan Das | ac693b2 | 2023-04-24 00:07:38 +0000 | [diff] [blame] | 1383 | if len(systemSharedLibs) > 0 { |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 1384 | SetStubsForDynamicDeps(ctx, axis, config, apexAvailable, bazelLabelForSharedDeps(ctx, systemSharedLibs), &la.systemDynamicDeps, &la.deps, 2, true) |
Spandan Das | ac693b2 | 2023-04-24 00:07:38 +0000 | [diff] [blame] | 1385 | } |
Wei Li | 81852ca | 2022-07-27 00:22:06 -0700 | [diff] [blame] | 1386 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1387 | |
| 1388 | if !BoolDefault(props.Pack_relocations, packRelocationsDefault) { |
| 1389 | axisFeatures = append(axisFeatures, "disable_pack_relocations") |
| 1390 | } |
| 1391 | |
| 1392 | if Bool(props.Allow_undefined_symbols) { |
| 1393 | axisFeatures = append(axisFeatures, "-no_undefined_symbols") |
| 1394 | } |
| 1395 | |
| 1396 | var linkerFlags []string |
| 1397 | if len(props.Ldflags) > 0 { |
Liz Kammer | f38a837 | 2022-02-04 15:39:00 -0500 | [diff] [blame] | 1398 | linkerFlags = append(linkerFlags, proptools.NinjaEscapeList(props.Ldflags)...) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1399 | // binaries remove static flag if -shared is in the linker flags |
| 1400 | if isBinary && android.InList("-shared", linkerFlags) { |
| 1401 | axisFeatures = append(axisFeatures, "-static_flag") |
| 1402 | } |
| 1403 | } |
Trevor Radcliffe | ea6a45d | 2022-09-20 18:58:01 +0000 | [diff] [blame] | 1404 | |
Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | 01ec55e | 2023-01-30 22:53:04 +0000 | [diff] [blame] | 1405 | if !props.libCrt() { |
| 1406 | axisFeatures = append(axisFeatures, "-use_libcrt") |
| 1407 | } |
| 1408 | if !props.crt() { |
| 1409 | axisFeatures = append(axisFeatures, "-link_crt") |
| 1410 | } |
| 1411 | |
Trevor Radcliffe | ea6a45d | 2022-09-20 18:58:01 +0000 | [diff] [blame] | 1412 | // This must happen before the addition of flags for Version Script and |
| 1413 | // Dynamic List, as these flags must be split on spaces and those must not |
| 1414 | linkerFlags = parseCommandLineFlags(linkerFlags, filterOutClangUnknownCflags) |
| 1415 | |
Trevor Radcliffe | 37ec2f7 | 2022-09-27 01:46:01 +0000 | [diff] [blame] | 1416 | additionalLinkerInputs := bazel.LabelList{} |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1417 | if props.Version_script != nil { |
| 1418 | label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script) |
Trevor Radcliffe | 37ec2f7 | 2022-09-27 01:46:01 +0000 | [diff] [blame] | 1419 | additionalLinkerInputs.Add(&label) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1420 | linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label)) |
Trevor Radcliffe | f06dd91 | 2023-05-19 14:51:41 +0000 | [diff] [blame] | 1421 | axisFeatures = append(axisFeatures, "android_cfi_exports_map") |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1422 | } |
Alix | 773adaa | 2022-04-27 17:49:34 +0000 | [diff] [blame] | 1423 | |
| 1424 | if props.Dynamic_list != nil { |
| 1425 | label := android.BazelLabelForModuleSrcSingle(ctx, *props.Dynamic_list) |
Trevor Radcliffe | 37ec2f7 | 2022-09-27 01:46:01 +0000 | [diff] [blame] | 1426 | additionalLinkerInputs.Add(&label) |
Alix | 773adaa | 2022-04-27 17:49:34 +0000 | [diff] [blame] | 1427 | linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--dynamic-list,$(location %s)", label.Label)) |
| 1428 | } |
| 1429 | |
Trevor Radcliffe | 37ec2f7 | 2022-09-27 01:46:01 +0000 | [diff] [blame] | 1430 | la.additionalLinkerInputs.SetSelectValue(axis, config, additionalLinkerInputs) |
Spandan Das | fb04c41 | 2023-05-15 18:35:36 +0000 | [diff] [blame] | 1431 | if axis == bazel.OsConfigurationAxis && (config == bazel.OsDarwin || config == bazel.OsLinux || config == bazel.OsWindows) { |
| 1432 | linkerFlags = append(linkerFlags, props.Host_ldlibs...) |
| 1433 | } |
Trevor Radcliffe | ea6a45d | 2022-09-20 18:58:01 +0000 | [diff] [blame] | 1434 | la.linkopts.SetSelectValue(axis, config, linkerFlags) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1435 | |
| 1436 | if axisFeatures != nil { |
| 1437 | la.features.SetSelectValue(axis, config, axisFeatures) |
| 1438 | } |
Cole Faust | 6b29f59 | 2022-08-09 09:50:56 -0700 | [diff] [blame] | 1439 | |
| 1440 | runtimeDeps := android.BazelLabelForModuleDepsExcludes(ctx, props.Runtime_libs, props.Exclude_runtime_libs) |
| 1441 | if !runtimeDeps.IsEmpty() { |
| 1442 | la.runtimeDeps.SetSelectValue(axis, config, runtimeDeps) |
| 1443 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1444 | } |
| 1445 | |
Spandan Das | 2518c02 | 2023-03-17 03:02:32 +0000 | [diff] [blame] | 1446 | var ( |
| 1447 | apiSurfaceModuleLibCurrentPackage = "@api_surfaces//" + android.ModuleLibApi.String() + "/current:" |
| 1448 | ) |
| 1449 | |
Liz Kammer | 48cdbeb | 2023-03-17 10:17:50 -0400 | [diff] [blame] | 1450 | func availableToSameApexes(a, b []string) bool { |
| 1451 | if len(a) == 0 && len(b) == 0 { |
| 1452 | return true |
| 1453 | } |
| 1454 | differ, _, _ := android.ListSetDifference(a, b) |
| 1455 | return !differ |
| 1456 | } |
| 1457 | |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1458 | var ( |
Spandan Das | 9cad90f | 2023-05-04 17:15:44 +0000 | [diff] [blame] | 1459 | apiDomainConfigSettingKey = android.NewOnceKey("apiDomainConfigSettingKey") |
| 1460 | apiDomainConfigSettingLock sync.Mutex |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1461 | ) |
| 1462 | |
Spandan Das | 9cad90f | 2023-05-04 17:15:44 +0000 | [diff] [blame] | 1463 | func getApiDomainConfigSettingMap(config android.Config) *map[string]bool { |
| 1464 | return config.Once(apiDomainConfigSettingKey, func() interface{} { |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1465 | return &map[string]bool{} |
| 1466 | }).(*map[string]bool) |
| 1467 | } |
| 1468 | |
Spandan Das | 9cad90f | 2023-05-04 17:15:44 +0000 | [diff] [blame] | 1469 | var ( |
| 1470 | testApexNameToApiDomain = map[string]string{ |
| 1471 | "test_broken_com.android.art": "com.android.art", |
| 1472 | } |
| 1473 | ) |
| 1474 | |
Spandan Das | a43ae13 | 2023-05-08 18:33:16 +0000 | [diff] [blame] | 1475 | // GetApiDomain returns the canonical name of the apex. This is synonymous to the apex_name definition. |
| 1476 | // https://cs.android.com/android/_/android/platform/build/soong/+/e3f0281b8897da1fe23b2f4f3a05f1dc87bcc902:apex/prebuilt.go;l=81-83;drc=2dc7244af985a6ad701b22f1271e606cabba527f;bpv=1;bpt=0 |
| 1477 | // For test apexes, it uses a naming convention heuristic to determine the api domain. |
| 1478 | // TODO (b/281548611): Move this build/soong/android |
| 1479 | func GetApiDomain(apexName string) string { |
Spandan Das | 9cad90f | 2023-05-04 17:15:44 +0000 | [diff] [blame] | 1480 | if apiDomain, exists := testApexNameToApiDomain[apexName]; exists { |
| 1481 | return apiDomain |
| 1482 | } |
| 1483 | // Remove `test_` prefix |
| 1484 | return strings.TrimPrefix(apexName, "test_") |
| 1485 | } |
| 1486 | |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1487 | // Create a config setting for this apex in build/bazel/rules/apex |
| 1488 | // The use case for this is stub/impl selection in cc libraries |
| 1489 | // Long term, these config_setting(s) should be colocated with the respective apex definitions. |
| 1490 | // Note that this is an anti-pattern: The config_setting should be created from the apex definition |
| 1491 | // and not from a cc_library. |
| 1492 | // This anti-pattern is needed today since not all apexes have been allowlisted. |
Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 1493 | func createInApexConfigSetting(ctx android.Bp2buildMutatorContext, apexName string) { |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1494 | if apexName == android.AvailableToPlatform || apexName == android.AvailableToAnyApex { |
| 1495 | // These correspond to android-non_apex and android-in_apex |
| 1496 | return |
| 1497 | } |
Spandan Das | 9cad90f | 2023-05-04 17:15:44 +0000 | [diff] [blame] | 1498 | apiDomainConfigSettingLock.Lock() |
| 1499 | defer apiDomainConfigSettingLock.Unlock() |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1500 | |
| 1501 | // Return if a config_setting has already been created |
Spandan Das | a43ae13 | 2023-05-08 18:33:16 +0000 | [diff] [blame] | 1502 | apiDomain := GetApiDomain(apexName) |
Spandan Das | 9cad90f | 2023-05-04 17:15:44 +0000 | [diff] [blame] | 1503 | acsm := getApiDomainConfigSettingMap(ctx.Config()) |
| 1504 | if _, exists := (*acsm)[apiDomain]; exists { |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1505 | return |
| 1506 | } |
Spandan Das | 9cad90f | 2023-05-04 17:15:44 +0000 | [diff] [blame] | 1507 | (*acsm)[apiDomain] = true |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1508 | |
| 1509 | csa := bazel.ConfigSettingAttributes{ |
| 1510 | Flag_values: bazel.StringMapAttribute{ |
Spandan Das | 9cad90f | 2023-05-04 17:15:44 +0000 | [diff] [blame] | 1511 | "//build/bazel/rules/apex:api_domain": apiDomain, |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1512 | }, |
Spandan Das | 9cad90f | 2023-05-04 17:15:44 +0000 | [diff] [blame] | 1513 | // Constraint this to android |
| 1514 | Constraint_values: bazel.MakeLabelListAttribute( |
| 1515 | bazel.MakeLabelList( |
| 1516 | []bazel.Label{ |
Jingwen Chen | 9c2e3ee | 2023-10-11 10:51:28 +0000 | [diff] [blame] | 1517 | bazel.Label{Label: "//build/bazel_common_rules/platforms/os:android"}, |
Spandan Das | 9cad90f | 2023-05-04 17:15:44 +0000 | [diff] [blame] | 1518 | }, |
| 1519 | ), |
| 1520 | ), |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1521 | } |
| 1522 | ca := android.CommonAttributes{ |
Spandan Das | 9cad90f | 2023-05-04 17:15:44 +0000 | [diff] [blame] | 1523 | Name: apiDomain, |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1524 | } |
| 1525 | ctx.CreateBazelConfigSetting( |
| 1526 | csa, |
| 1527 | ca, |
| 1528 | "build/bazel/rules/apex", |
| 1529 | ) |
| 1530 | } |
| 1531 | |
| 1532 | func inApexConfigSetting(apexAvailable string) string { |
| 1533 | if apexAvailable == android.AvailableToPlatform { |
Spandan Das | 6d4d9da | 2023-04-18 06:20:40 +0000 | [diff] [blame] | 1534 | return bazel.AndroidPlatform |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1535 | } |
| 1536 | if apexAvailable == android.AvailableToAnyApex { |
| 1537 | return bazel.AndroidAndInApex |
| 1538 | } |
Spandan Das | a43ae13 | 2023-05-08 18:33:16 +0000 | [diff] [blame] | 1539 | apiDomain := GetApiDomain(apexAvailable) |
Spandan Das | 9cad90f | 2023-05-04 17:15:44 +0000 | [diff] [blame] | 1540 | return "//build/bazel/rules/apex:" + apiDomain |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Spandan Das | 6d4d9da | 2023-04-18 06:20:40 +0000 | [diff] [blame] | 1543 | // Inputs to stub vs impl selection. |
| 1544 | type stubSelectionInfo struct { |
| 1545 | // Label of the implementation library (e.g. //bionic/libc:libc) |
| 1546 | impl bazel.Label |
| 1547 | // Axis containing the implementation library |
| 1548 | axis bazel.ConfigurationAxis |
| 1549 | // Axis key containing the implementation library |
| 1550 | config string |
| 1551 | // API domain of the apex |
| 1552 | // For test apexes (test_com.android.foo), this will be the source apex (com.android.foo) |
| 1553 | apiDomain string |
| 1554 | // List of dep labels |
| 1555 | dynamicDeps *bazel.LabelListAttribute |
| 1556 | // Boolean value for determining if the dep is in the same api domain |
| 1557 | // If false, the label will be rewritten to to the stub label |
| 1558 | sameApiDomain bool |
| 1559 | } |
| 1560 | |
| 1561 | func useStubOrImplInApexWithName(ssi stubSelectionInfo) { |
| 1562 | lib := ssi.impl |
| 1563 | if !ssi.sameApiDomain { |
| 1564 | lib = bazel.Label{ |
| 1565 | Label: apiSurfaceModuleLibCurrentPackage + strings.TrimPrefix(lib.OriginalModuleName, ":"), |
| 1566 | } |
| 1567 | } |
| 1568 | // Create a select statement specific to this apex |
| 1569 | inApexSelectValue := ssi.dynamicDeps.SelectValue(bazel.OsAndInApexAxis, inApexConfigSetting(ssi.apiDomain)) |
| 1570 | (&inApexSelectValue).Append(bazel.MakeLabelList([]bazel.Label{lib})) |
| 1571 | ssi.dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, inApexConfigSetting(ssi.apiDomain), bazel.FirstUniqueBazelLabelList(inApexSelectValue)) |
| 1572 | // Delete the library from the common config for this apex |
| 1573 | implDynamicDeps := ssi.dynamicDeps.SelectValue(ssi.axis, ssi.config) |
| 1574 | implDynamicDeps = bazel.SubtractBazelLabelList(implDynamicDeps, bazel.MakeLabelList([]bazel.Label{ssi.impl})) |
| 1575 | ssi.dynamicDeps.SetSelectValue(ssi.axis, ssi.config, implDynamicDeps) |
| 1576 | if ssi.axis == bazel.NoConfigAxis { |
| 1577 | // Set defaults. Defaults (i.e. host) should use impl and not stubs. |
| 1578 | defaultSelectValue := ssi.dynamicDeps.SelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey) |
| 1579 | (&defaultSelectValue).Append(bazel.MakeLabelList([]bazel.Label{ssi.impl})) |
| 1580 | ssi.dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, bazel.FirstUniqueBazelLabelList(defaultSelectValue)) |
| 1581 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1582 | } |
| 1583 | |
Spandan Das | 1f65f9e | 2023-09-15 01:08:23 +0000 | [diff] [blame] | 1584 | // hasNdkStubs returns true for libfoo if there exists a libfoo.ndk of type ndk_library |
| 1585 | func hasNdkStubs(ctx android.BazelConversionPathContext, c *Module) bool { |
| 1586 | mod, exists := ctx.ModuleFromName(c.Name() + ndkLibrarySuffix) |
| 1587 | return exists && ctx.OtherModuleType(mod) == "ndk_library" |
| 1588 | } |
| 1589 | |
Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 1590 | func SetStubsForDynamicDeps(ctx android.Bp2buildMutatorContext, axis bazel.ConfigurationAxis, |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 1591 | config string, apexAvailable []string, dynamicLibs bazel.LabelList, dynamicDeps *bazel.LabelListAttribute, deps *bazel.LabelListAttribute, ind int, buildNonApexWithStubs bool) { |
Liz Kammer | 48cdbeb | 2023-03-17 10:17:50 -0400 | [diff] [blame] | 1592 | |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1593 | // Create a config_setting for each apex_available. |
| 1594 | // This will be used to select impl of a dep if dep is available to the same apex. |
| 1595 | for _, aa := range apexAvailable { |
Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 1596 | createInApexConfigSetting(ctx, aa) |
Spandan Das | 4242f10 | 2023-04-19 22:31:54 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Spandan Das | 6d4d9da | 2023-04-18 06:20:40 +0000 | [diff] [blame] | 1599 | apiDomainForSelects := []string{} |
| 1600 | for _, apex := range apexAvailable { |
| 1601 | apiDomainForSelects = append(apiDomainForSelects, GetApiDomain(apex)) |
| 1602 | } |
| 1603 | // Always emit a select statement for the platform variant. |
| 1604 | // This ensures that b build //foo --config=android works |
| 1605 | // Soong always creates a platform variant even when the library might not be available to platform. |
| 1606 | if !android.InList(android.AvailableToPlatform, apiDomainForSelects) { |
| 1607 | apiDomainForSelects = append(apiDomainForSelects, android.AvailableToPlatform) |
| 1608 | } |
| 1609 | apiDomainForSelects = android.SortedUniqueStrings(apiDomainForSelects) |
| 1610 | |
| 1611 | // Create a select for each apex this library could be included in. |
| 1612 | for _, l := range dynamicLibs.Includes { |
| 1613 | dep, _ := ctx.ModuleFromName(l.OriginalModuleName) |
| 1614 | if c, ok := dep.(*Module); !ok || !c.HasStubsVariants() { |
| 1615 | continue |
| 1616 | } |
| 1617 | // TODO (b/280339069): Decrease the verbosity of the generated BUILD files |
| 1618 | for _, apiDomain := range apiDomainForSelects { |
| 1619 | var sameApiDomain bool |
| 1620 | if apiDomain == android.AvailableToPlatform { |
| 1621 | // Platform variants in Soong use equality of apex_available for stub/impl selection. |
| 1622 | // https://cs.android.com/android/_/android/platform/build/soong/+/316b0158fe57ee7764235923e7c6f3d530da39c6:cc/cc.go;l=3393-3404;drc=176271a426496fa2688efe2b40d5c74340c63375;bpv=1;bpt=0 |
| 1623 | // One of the factors behind this design choice is cc_test |
| 1624 | // Tests only have a platform variant, and using equality of apex_available ensures |
| 1625 | // that tests of an apex library gets its implementation and not stubs. |
| 1626 | // TODO (b/280343104): Discuss if we can drop this special handling for platform variants. |
| 1627 | sameApiDomain = availableToSameApexes(apexAvailable, dep.(*Module).ApexAvailable()) |
Spandan Das | 6aaab9d | 2023-05-04 17:27:30 +0000 | [diff] [blame] | 1628 | if linkable, ok := ctx.Module().(LinkableInterface); ok && linkable.Bootstrap() { |
| 1629 | sameApiDomain = true |
| 1630 | } |
Spandan Das | f7bae9a | 2023-09-06 22:07:15 +0000 | [diff] [blame] | 1631 | // If dependency has `apex_available: ["//apex_available:platform]`, then the platform variant of rdep should link against its impl. |
| 1632 | // https://cs.android.com/android/_/android/platform/build/soong/+/main:cc/cc.go;l=3617;bpv=1;bpt=0;drc=c6a93d853b37ec90786e745b8d282145e6d3b589 |
| 1633 | if depApexAvailable := dep.(*Module).ApexAvailable(); len(depApexAvailable) == 1 && depApexAvailable[0] == android.AvailableToPlatform { |
| 1634 | sameApiDomain = true |
| 1635 | } |
Spandan Das | 6d4d9da | 2023-04-18 06:20:40 +0000 | [diff] [blame] | 1636 | } else { |
| 1637 | sameApiDomain = android.InList(apiDomain, dep.(*Module).ApexAvailable()) |
| 1638 | } |
| 1639 | ssi := stubSelectionInfo{ |
| 1640 | impl: l, |
| 1641 | axis: axis, |
| 1642 | config: config, |
| 1643 | apiDomain: apiDomain, |
| 1644 | dynamicDeps: dynamicDeps, |
| 1645 | sameApiDomain: sameApiDomain, |
| 1646 | } |
| 1647 | useStubOrImplInApexWithName(ssi) |
| 1648 | } |
| 1649 | } |
Spandan Das | 1f65f9e | 2023-09-15 01:08:23 +0000 | [diff] [blame] | 1650 | |
| 1651 | // If the library has an sdk variant, create additional selects to build this variant against the ndk |
| 1652 | // The config setting for this variant will be //build/bazel/rules/apex:unbundled_app |
| 1653 | if c, ok := ctx.Module().(*Module); ok && c.Properties.Sdk_version != nil { |
| 1654 | for _, l := range dynamicLibs.Includes { |
| 1655 | dep, _ := ctx.ModuleFromName(l.OriginalModuleName) |
| 1656 | label := l // use the implementation by default |
| 1657 | if depC, ok := dep.(*Module); ok && hasNdkStubs(ctx, depC) { |
| 1658 | // If the dependency has ndk stubs, build against the ndk stubs |
| 1659 | // https://cs.android.com/android/_/android/platform/build/soong/+/main:cc/cc.go;l=2642-2643;drc=e12d252e22dd8afa654325790d3298a0d67bd9d6;bpv=1;bpt=0 |
Spandan Das | 9d47a82 | 2023-09-19 18:23:26 +0000 | [diff] [blame] | 1660 | ver := proptools.String(c.Properties.Sdk_version) |
| 1661 | // TODO - b/298085502: Add bp2build support for sdk_version: "minimum" |
Spandan Das | 1f65f9e | 2023-09-15 01:08:23 +0000 | [diff] [blame] | 1662 | ndkLibModule, _ := ctx.ModuleFromName(dep.Name() + ndkLibrarySuffix) |
| 1663 | label = bazel.Label{ |
Spandan Das | 9d47a82 | 2023-09-19 18:23:26 +0000 | [diff] [blame] | 1664 | Label: "//" + ctx.OtherModuleDir(ndkLibModule) + ":" + ndkLibModule.Name() + "_stub_libs-" + ver, |
Spandan Das | 1f65f9e | 2023-09-15 01:08:23 +0000 | [diff] [blame] | 1665 | } |
| 1666 | } |
| 1667 | // add the ndk lib label to this axis |
| 1668 | existingValue := dynamicDeps.SelectValue(bazel.OsAndInApexAxis, "unbundled_app") |
| 1669 | existingValue.Append(bazel.MakeLabelList([]bazel.Label{label})) |
| 1670 | dynamicDeps.SetSelectValue(bazel.OsAndInApexAxis, "unbundled_app", bazel.FirstUniqueBazelLabelList(existingValue)) |
| 1671 | } |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 1672 | |
| 1673 | // Add ndk_sysroot to deps. |
| 1674 | // ndk_sysroot has a dependency edge on all ndk_headers, and will provide the .h files of _every_ ndk library |
| 1675 | existingValue := deps.SelectValue(bazel.OsAndInApexAxis, "unbundled_app") |
| 1676 | existingValue.Append(bazel.MakeLabelList([]bazel.Label{ndkSysrootLabel})) |
| 1677 | deps.SetSelectValue(bazel.OsAndInApexAxis, "unbundled_app", bazel.FirstUniqueBazelLabelList(existingValue)) |
Spandan Das | 1f65f9e | 2023-09-15 01:08:23 +0000 | [diff] [blame] | 1678 | } |
Yu Liu | 10174ff | 2023-02-21 12:05:26 -0800 | [diff] [blame] | 1679 | } |
| 1680 | |
Spandan Das | af72583 | 2023-09-19 19:51:52 +0000 | [diff] [blame] | 1681 | var ( |
| 1682 | ndkSysrootLabel = bazel.Label{ |
| 1683 | Label: "//build/bazel/rules/cc:ndk_sysroot", |
| 1684 | } |
| 1685 | ) |
| 1686 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1687 | func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) { |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1688 | bp2BuildPropParseHelper(ctx, module, &StripProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 1689 | if stripProperties, ok := props.(*StripProperties); ok { |
| 1690 | la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols) |
| 1691 | la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list) |
| 1692 | la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame) |
| 1693 | la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All) |
| 1694 | la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None) |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 1695 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1696 | }) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1697 | } |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 1698 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 1699 | func (la *linkerAttributes) convertProductVariables(ctx android.Bp2buildMutatorContext, productVariableProps android.ProductConfigProperties) { |
Jingwen Chen | 6ada589 | 2021-09-17 11:38:09 +0000 | [diff] [blame] | 1700 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1701 | type productVarDep struct { |
| 1702 | // the name of the corresponding excludes field, if one exists |
| 1703 | excludesField string |
| 1704 | // reference to the bazel attribute that should be set for the given product variable config |
| 1705 | attribute *bazel.LabelListAttribute |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 1706 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 1707 | depResolutionFunc func(ctx android.Bp2buildMutatorContext, modules, excludes []string) bazel.LabelList |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1708 | } |
| 1709 | |
Zi Wang | 0a8a129 | 2022-08-30 06:27:01 +0000 | [diff] [blame] | 1710 | // an intermediate attribute that holds Header_libs info, and will be appended to |
| 1711 | // implementationDeps at the end, to solve the confliction that both header_libs |
| 1712 | // and static_libs use implementationDeps. |
| 1713 | var headerDeps bazel.LabelListAttribute |
| 1714 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1715 | productVarToDepFields := map[string]productVarDep{ |
| 1716 | // product variables do not support exclude_shared_libs |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1717 | "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes}, |
| 1718 | "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes}, |
| 1719 | "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes}, |
Zi Wang | 0a8a129 | 2022-08-30 06:27:01 +0000 | [diff] [blame] | 1720 | "Header_libs": {attribute: &headerDeps, depResolutionFunc: bazelLabelForHeaderDepsExcludes}, |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1721 | } |
| 1722 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1723 | for name, dep := range productVarToDepFields { |
| 1724 | props, exists := productVariableProps[name] |
| 1725 | excludeProps, excludesExists := productVariableProps[dep.excludesField] |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 1726 | // if neither an include nor excludes property exists, then skip it |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1727 | if !exists && !excludesExists { |
| 1728 | continue |
| 1729 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1730 | // Collect all the configurations that an include or exclude property exists for. |
| 1731 | // We want to iterate all configurations rather than either the include or exclude because, for a |
| 1732 | // particular configuration, we may have either only an include or an exclude to handle. |
Cole Faust | 150f9a5 | 2023-04-26 10:52:24 -0700 | [diff] [blame] | 1733 | productConfigProps := make(map[android.ProductConfigOrSoongConfigProperty]bool, len(props)+len(excludeProps)) |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1734 | for p := range props { |
| 1735 | productConfigProps[p] = true |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1736 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1737 | for p := range excludeProps { |
| 1738 | productConfigProps[p] = true |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1739 | } |
| 1740 | |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1741 | for productConfigProp := range productConfigProps { |
| 1742 | prop, includesExists := props[productConfigProp] |
| 1743 | excludesProp, excludesExists := excludeProps[productConfigProp] |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1744 | var includes, excludes []string |
| 1745 | var ok bool |
| 1746 | // 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] | 1747 | if includes, ok = prop.([]string); includesExists && !ok { |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1748 | ctx.ModuleErrorf("Could not convert product variable %s property", name) |
| 1749 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1750 | if excludes, ok = excludesProp.([]string); excludesExists && !ok { |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1751 | ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField) |
| 1752 | } |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 1753 | |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 1754 | dep.attribute.EmitEmptyList = productConfigProp.AlwaysEmit() |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 1755 | dep.attribute.SetSelectValue( |
| 1756 | productConfigProp.ConfigurationAxis(), |
| 1757 | productConfigProp.SelectKey(), |
| 1758 | dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes), |
| 1759 | ) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1760 | } |
| 1761 | } |
Zi Wang | 0a8a129 | 2022-08-30 06:27:01 +0000 | [diff] [blame] | 1762 | la.implementationDeps.Append(headerDeps) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1763 | } |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 1764 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 1765 | func (la *linkerAttributes) finalize(ctx android.Bp2buildMutatorContext) { |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1766 | // if system dynamic deps have the default value, any use of a system dynamic library used will |
| 1767 | // 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] | 1768 | // 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] | 1769 | if la.systemDynamicDeps.IsNil() && len(la.usedSystemDynamicDepAsDynamicDep) > 0 { |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 1770 | toRemove := bazelLabelForSharedDeps(ctx, android.SortedKeys(la.usedSystemDynamicDepAsDynamicDep)) |
Liz Kammer | 43345e2 | 2022-08-04 13:57:35 -0400 | [diff] [blame] | 1771 | la.dynamicDeps.Exclude(bazel.NoConfigAxis, "", toRemove) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1772 | la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove) |
| 1773 | la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove) |
Liz Kammer | 91487d4 | 2022-09-13 11:27:11 -0400 | [diff] [blame] | 1774 | la.implementationDynamicDeps.Exclude(bazel.NoConfigAxis, "", toRemove) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1775 | la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove) |
| 1776 | la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove) |
Liz Kammer | 91487d4 | 2022-09-13 11:27:11 -0400 | [diff] [blame] | 1777 | |
| 1778 | la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.ConditionsDefaultConfigKey, toRemove) |
Liz Kammer | 91487d4 | 2022-09-13 11:27:11 -0400 | [diff] [blame] | 1779 | stubsToRemove := make([]bazel.Label, 0, len(la.usedSystemDynamicDepAsDynamicDep)) |
| 1780 | for _, lib := range toRemove.Includes { |
Spandan Das | 2518c02 | 2023-03-17 03:02:32 +0000 | [diff] [blame] | 1781 | stubLabelInApiSurfaces := bazel.Label{ |
| 1782 | Label: apiSurfaceModuleLibCurrentPackage + lib.OriginalModuleName, |
| 1783 | } |
| 1784 | stubsToRemove = append(stubsToRemove, stubLabelInApiSurfaces) |
Liz Kammer | 91487d4 | 2022-09-13 11:27:11 -0400 | [diff] [blame] | 1785 | } |
Spandan Das | 6d4d9da | 2023-04-18 06:20:40 +0000 | [diff] [blame] | 1786 | // system libraries (e.g. libc, libm, libdl) belong the com.android.runtime api domain |
| 1787 | // dedupe the stubs of these libraries from the other api domains (platform, other_apexes...) |
| 1788 | for _, aa := range ctx.Module().(*Module).ApexAvailable() { |
| 1789 | la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, inApexConfigSetting(aa), bazel.MakeLabelList(stubsToRemove)) |
| 1790 | } |
| 1791 | la.implementationDynamicDeps.Exclude(bazel.OsAndInApexAxis, bazel.AndroidPlatform, bazel.MakeLabelList(stubsToRemove)) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1792 | } |
Liz Kammer | b492843 | 2023-06-02 18:43:36 -0400 | [diff] [blame] | 1793 | if la.systemDynamicDeps.IsNil() && len(la.usedSystemDynamicDepAsStaticDep) > 0 { |
| 1794 | toRemove := bazelLabelForStaticDeps(ctx, android.SortedKeys(la.usedSystemDynamicDepAsStaticDep)) |
| 1795 | la.deps.Exclude(bazel.NoConfigAxis, "", toRemove) |
| 1796 | la.deps.Exclude(bazel.OsConfigurationAxis, "android", toRemove) |
| 1797 | la.deps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove) |
| 1798 | la.implementationDeps.Exclude(bazel.NoConfigAxis, "", toRemove) |
| 1799 | la.implementationDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove) |
| 1800 | la.implementationDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove) |
| 1801 | } |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1802 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 1803 | la.deps.ResolveExcludes() |
| 1804 | la.implementationDeps.ResolveExcludes() |
| 1805 | la.dynamicDeps.ResolveExcludes() |
| 1806 | la.implementationDynamicDeps.ResolveExcludes() |
| 1807 | la.wholeArchiveDeps.ResolveExcludes() |
| 1808 | la.systemDynamicDeps.ForceSpecifyEmptyList = true |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 1809 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 1810 | } |
| 1811 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 1812 | // Relativize a list of root-relative paths with respect to the module's |
| 1813 | // directory. |
| 1814 | // |
| 1815 | // include_dirs Soong prop are root-relative (b/183742505), but |
| 1816 | // local_include_dirs, export_include_dirs and export_system_include_dirs are |
| 1817 | // module dir relative. This function makes a list of paths entirely module dir |
| 1818 | // relative. |
| 1819 | // |
| 1820 | // For the `include` attribute, Bazel wants the paths to be relative to the |
| 1821 | // module. |
| 1822 | func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 1823 | var relativePaths []string |
| 1824 | for _, path := range paths { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 1825 | // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path |
| 1826 | relativePath, err := filepath.Rel(ctx.ModuleDir(), path) |
| 1827 | if err != nil { |
| 1828 | panic(err) |
| 1829 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 1830 | relativePaths = append(relativePaths, relativePath) |
| 1831 | } |
| 1832 | return relativePaths |
| 1833 | } |
| 1834 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 1835 | // BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel |
| 1836 | // attributes. |
| 1837 | type BazelIncludes struct { |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 1838 | AbsoluteIncludes bazel.StringListAttribute |
| 1839 | Includes bazel.StringListAttribute |
| 1840 | SystemIncludes bazel.StringListAttribute |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 1841 | } |
| 1842 | |
Liz Kammer | 5454944 | 2022-05-11 13:55:06 -0400 | [diff] [blame] | 1843 | func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module, includes *BazelIncludes) BazelIncludes { |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 1844 | var exported BazelIncludes |
| 1845 | if includes != nil { |
| 1846 | exported = *includes |
| 1847 | } else { |
| 1848 | exported = BazelIncludes{} |
| 1849 | } |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 1850 | |
| 1851 | // cc library Export_include_dirs and Export_system_include_dirs are marked |
| 1852 | // "variant_prepend" in struct tag, set their prepend property to true to make |
| 1853 | // sure bp2build generates correct result. |
| 1854 | exported.Includes.Prepend = true |
| 1855 | exported.SystemIncludes.Prepend = true |
| 1856 | |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1857 | bp2BuildPropParseHelper(ctx, module, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 1858 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
| 1859 | if len(flagExporterProperties.Export_include_dirs) > 0 { |
| 1860 | exported.Includes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.Includes.SelectValue(axis, config), flagExporterProperties.Export_include_dirs...))) |
| 1861 | } |
| 1862 | if len(flagExporterProperties.Export_system_include_dirs) > 0 { |
| 1863 | 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] | 1864 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 1865 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1866 | }) |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 1867 | exported.AbsoluteIncludes.DeduplicateAxesFromBase() |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 1868 | exported.Includes.DeduplicateAxesFromBase() |
| 1869 | exported.SystemIncludes.DeduplicateAxesFromBase() |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 1870 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 1871 | return exported |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 1872 | } |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1873 | |
Trevor Radcliffe | cee4e05 | 2022-09-06 19:31:25 +0000 | [diff] [blame] | 1874 | func BazelLabelNameForStaticModule(baseLabel string) string { |
| 1875 | return baseLabel + "_bp2build_cc_library_static" |
| 1876 | } |
| 1877 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1878 | func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1879 | label := android.BazelModuleLabel(ctx, m) |
Sasha Smundak | 39a301c | 2022-12-29 17:11:49 -0800 | [diff] [blame] | 1880 | if ccModule, ok := m.(*Module); ok && ccModule.typ() == fullLibrary { |
Trevor Radcliffe | cee4e05 | 2022-09-06 19:31:25 +0000 | [diff] [blame] | 1881 | return BazelLabelNameForStaticModule(label) |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1882 | } |
| 1883 | return label |
| 1884 | } |
| 1885 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1886 | func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1887 | // cc_library, at it's root name, propagates the shared library, which depends on the static |
| 1888 | // library. |
| 1889 | return android.BazelModuleLabel(ctx, m) |
| 1890 | } |
| 1891 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1892 | func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1893 | label := bazelLabelForStaticModule(ctx, m) |
| 1894 | if aModule, ok := m.(android.Module); ok { |
| 1895 | if android.IsModulePrebuilt(aModule) { |
| 1896 | label += "_alwayslink" |
| 1897 | } |
| 1898 | } |
| 1899 | return label |
| 1900 | } |
| 1901 | |
Liz Kammer | 5f5dbaa | 2023-07-17 17:44:08 -0400 | [diff] [blame] | 1902 | func xsdConfigCppTarget(xsd android.XsdConfigBp2buildTargets) string { |
| 1903 | return xsd.CppBp2buildTargetName() |
Liz Kammer | 084d6a9 | 2023-06-22 16:23:53 -0400 | [diff] [blame] | 1904 | } |
| 1905 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 1906 | func bazelLabelForWholeDeps(ctx android.Bp2buildMutatorContext, modules []string) bazel.LabelList { |
Cole Faust | 06ea531 | 2023-10-18 17:38:40 -0700 | [diff] [blame] | 1907 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps, true) |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1908 | } |
| 1909 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 1910 | func bazelLabelForWholeDepsExcludes(ctx android.Bp2buildMutatorContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1911 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps) |
| 1912 | } |
| 1913 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 1914 | func bazelLabelForStaticDepsExcludes(ctx android.Bp2buildMutatorContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1915 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule) |
| 1916 | } |
| 1917 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 1918 | func bazelLabelForStaticDeps(ctx android.Bp2buildMutatorContext, modules []string) bazel.LabelList { |
Cole Faust | 06ea531 | 2023-10-18 17:38:40 -0700 | [diff] [blame] | 1919 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule, true) |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1920 | } |
| 1921 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 1922 | func bazelLabelForSharedDeps(ctx android.Bp2buildMutatorContext, modules []string) bazel.LabelList { |
Cole Faust | 06ea531 | 2023-10-18 17:38:40 -0700 | [diff] [blame] | 1923 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule, true) |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1924 | } |
| 1925 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 1926 | func bazelLabelForHeaderDeps(ctx android.Bp2buildMutatorContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1927 | // This is not elegant, but bp2build's shared library targets only propagate |
| 1928 | // their header information as part of the normal C++ provider. |
| 1929 | return bazelLabelForSharedDeps(ctx, modules) |
| 1930 | } |
| 1931 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 1932 | func bazelLabelForHeaderDepsExcludes(ctx android.Bp2buildMutatorContext, modules, excludes []string) bazel.LabelList { |
Zi Wang | 0a8a129 | 2022-08-30 06:27:01 +0000 | [diff] [blame] | 1933 | // This is only used when product_variable header_libs is processed, to follow |
| 1934 | // the pattern of depResolutionFunc |
| 1935 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule) |
| 1936 | } |
| 1937 | |
Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 1938 | func bazelLabelForSharedDepsExcludes(ctx android.Bp2buildMutatorContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 1939 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule) |
| 1940 | } |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1941 | |
| 1942 | type binaryLinkerAttrs struct { |
| 1943 | Linkshared *bool |
Spandan Das | 39ccf93 | 2023-05-26 18:03:39 +0000 | [diff] [blame] | 1944 | Stem bazel.StringAttribute |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a56e970 | 2022-02-23 18:39:59 -0500 | [diff] [blame] | 1945 | Suffix bazel.StringAttribute |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1946 | } |
| 1947 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 1948 | func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs { |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1949 | attrs := binaryLinkerAttrs{} |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1950 | bp2BuildPropParseHelper(ctx, m, &BinaryLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 1951 | linkerProps := props.(*BinaryLinkerProperties) |
| 1952 | staticExecutable := linkerProps.Static_executable |
| 1953 | if axis == bazel.NoConfigAxis { |
| 1954 | if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared { |
| 1955 | attrs.Linkshared = &linkBinaryShared |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1956 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1957 | } else if staticExecutable != nil { |
| 1958 | // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a |
| 1959 | // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling |
| 1960 | 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] | 1961 | } |
Spandan Das | 39ccf93 | 2023-05-26 18:03:39 +0000 | [diff] [blame] | 1962 | if stem := linkerProps.Stem; stem != nil { |
| 1963 | attrs.Stem.SetSelectValue(axis, config, stem) |
| 1964 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | a56e970 | 2022-02-23 18:39:59 -0500 | [diff] [blame] | 1965 | if suffix := linkerProps.Suffix; suffix != nil { |
| 1966 | attrs.Suffix.SetSelectValue(axis, config, suffix) |
| 1967 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 1968 | }) |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 1969 | |
| 1970 | return attrs |
| 1971 | } |
Trevor Radcliffe | db7e026 | 2022-10-28 16:48:18 +0000 | [diff] [blame] | 1972 | |
Trevor Radcliffe | d9b7f17 | 2023-08-09 22:21:38 +0000 | [diff] [blame] | 1973 | type sanitizerValues struct { |
| 1974 | features bazel.StringListAttribute |
| 1975 | copts bazel.StringListAttribute |
| 1976 | additionalCompilerInputs bazel.LabelListAttribute |
| 1977 | } |
| 1978 | |
| 1979 | func bp2buildSanitizerFeatures(ctx android.BazelConversionPathContext, m *Module) sanitizerValues { |
Trevor Radcliffe | db7e026 | 2022-10-28 16:48:18 +0000 | [diff] [blame] | 1980 | sanitizerFeatures := bazel.StringListAttribute{} |
Trevor Radcliffe | d9b7f17 | 2023-08-09 22:21:38 +0000 | [diff] [blame] | 1981 | sanitizerCopts := bazel.StringListAttribute{} |
| 1982 | sanitizerCompilerInputs := bazel.LabelListAttribute{} |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame] | 1983 | memtagFeatures := bazel.StringListAttribute{} |
| 1984 | memtagFeature := "" |
Trevor Radcliffe | db7e026 | 2022-10-28 16:48:18 +0000 | [diff] [blame] | 1985 | bp2BuildPropParseHelper(ctx, m, &SanitizeProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 1986 | var features []string |
| 1987 | if sanitizerProps, ok := props.(*SanitizeProperties); ok { |
| 1988 | if sanitizerProps.Sanitize.Integer_overflow != nil && *sanitizerProps.Sanitize.Integer_overflow { |
| 1989 | features = append(features, "ubsan_integer_overflow") |
| 1990 | } |
| 1991 | for _, sanitizer := range sanitizerProps.Sanitize.Misc_undefined { |
| 1992 | features = append(features, "ubsan_"+sanitizer) |
| 1993 | } |
Trevor Radcliffe | ded095c | 2023-06-12 19:18:28 +0000 | [diff] [blame] | 1994 | blocklist := sanitizerProps.Sanitize.Blocklist |
| 1995 | if blocklist != nil { |
Trevor Radcliffe | d9b7f17 | 2023-08-09 22:21:38 +0000 | [diff] [blame] | 1996 | // TODO: b/294868620 - Change this not to use the special axis when completing the bug |
| 1997 | coptValue := fmt.Sprintf("-fsanitize-ignorelist=$(location %s)", *blocklist) |
| 1998 | sanitizerCopts.SetSelectValue(bazel.SanitizersEnabledAxis, bazel.SanitizersEnabled, []string{coptValue}) |
| 1999 | sanitizerCompilerInputs.SetSelectValue(bazel.SanitizersEnabledAxis, bazel.SanitizersEnabled, bazel.MakeLabelListFromTargetNames([]string{*blocklist})) |
Trevor Radcliffe | ded095c | 2023-06-12 19:18:28 +0000 | [diff] [blame] | 2000 | } |
Trevor Radcliffe | 523c5c6 | 2023-06-16 20:15:45 +0000 | [diff] [blame] | 2001 | if sanitizerProps.Sanitize.Cfi != nil && !proptools.Bool(sanitizerProps.Sanitize.Cfi) { |
Trevor Radcliffe | 85d55c2 | 2023-09-21 17:04:43 +0000 | [diff] [blame] | 2002 | features = append(features, "-android_cfi") |
Trevor Radcliffe | 523c5c6 | 2023-06-16 20:15:45 +0000 | [diff] [blame] | 2003 | } else if proptools.Bool(sanitizerProps.Sanitize.Cfi) { |
Trevor Radcliffe | 85d55c2 | 2023-09-21 17:04:43 +0000 | [diff] [blame] | 2004 | features = append(features, "android_cfi") |
Trevor Radcliffe | 27669c0 | 2023-03-28 20:47:10 +0000 | [diff] [blame] | 2005 | if proptools.Bool(sanitizerProps.Sanitize.Config.Cfi_assembly_support) { |
| 2006 | features = append(features, "android_cfi_assembly_support") |
| 2007 | } |
| 2008 | } |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame] | 2009 | |
| 2010 | if sanitizerProps.Sanitize.Memtag_heap != nil { |
| 2011 | if (axis == bazel.NoConfigAxis && memtagFeature == "") || |
| 2012 | (axis == bazel.OsArchConfigurationAxis && config == bazel.OsArchAndroidArm64) { |
| 2013 | memtagFeature = setMemtagValue(sanitizerProps, &memtagFeatures) |
| 2014 | } |
| 2015 | } |
Trevor Radcliffe | db7e026 | 2022-10-28 16:48:18 +0000 | [diff] [blame] | 2016 | sanitizerFeatures.SetSelectValue(axis, config, features) |
| 2017 | } |
| 2018 | }) |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame] | 2019 | sanitizerFeatures.Append(memtagFeatures) |
| 2020 | |
Trevor Radcliffe | d9b7f17 | 2023-08-09 22:21:38 +0000 | [diff] [blame] | 2021 | return sanitizerValues{ |
| 2022 | features: sanitizerFeatures, |
| 2023 | copts: sanitizerCopts, |
| 2024 | additionalCompilerInputs: sanitizerCompilerInputs, |
| 2025 | } |
Trevor Radcliffe | db7e026 | 2022-10-28 16:48:18 +0000 | [diff] [blame] | 2026 | } |
Trevor Radcliffe | 56b1a2b | 2023-02-06 21:58:30 +0000 | [diff] [blame] | 2027 | |
Yu Liu | 7ece4aa | 2023-08-25 16:56:33 -0700 | [diff] [blame] | 2028 | func setMemtagValue(sanitizerProps *SanitizeProperties, memtagFeatures *bazel.StringListAttribute) string { |
| 2029 | var features []string |
| 2030 | if proptools.Bool(sanitizerProps.Sanitize.Memtag_heap) { |
| 2031 | features = append(features, "memtag_heap") |
| 2032 | } else { |
| 2033 | features = append(features, "-memtag_heap") |
| 2034 | } |
| 2035 | // Logic comes from: https://cs.android.com/android/platform/superproject/main/+/32ea1afbd1148b0b78553f24fa61116c999eb968:build/soong/cc/sanitize.go;l=910 |
| 2036 | if sanitizerProps.Sanitize.Diag.Memtag_heap != nil { |
| 2037 | if proptools.Bool(sanitizerProps.Sanitize.Diag.Memtag_heap) { |
| 2038 | features = append(features, "diag_memtag_heap") |
| 2039 | } else { |
| 2040 | features = append(features, "-diag_memtag_heap") |
| 2041 | } |
| 2042 | } |
| 2043 | memtagFeatures.SetSelectValue(bazel.OsArchConfigurationAxis, bazel.OsArchAndroidArm64, features) |
| 2044 | |
| 2045 | return features[0] |
| 2046 | } |
| 2047 | |
Trevor Radcliffe | 56b1a2b | 2023-02-06 21:58:30 +0000 | [diff] [blame] | 2048 | func bp2buildLtoFeatures(ctx android.BazelConversionPathContext, m *Module) bazel.StringListAttribute { |
| 2049 | lto_feature_name := "android_thin_lto" |
| 2050 | ltoBoolFeatures := bazel.BoolAttribute{} |
| 2051 | bp2BuildPropParseHelper(ctx, m, <OProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 2052 | if ltoProps, ok := props.(*LTOProperties); ok { |
| 2053 | thinProp := ltoProps.Lto.Thin != nil && *ltoProps.Lto.Thin |
| 2054 | thinPropSetToFalse := ltoProps.Lto.Thin != nil && !*ltoProps.Lto.Thin |
| 2055 | neverProp := ltoProps.Lto.Never != nil && *ltoProps.Lto.Never |
| 2056 | if thinProp { |
| 2057 | ltoBoolFeatures.SetSelectValue(axis, config, BoolPtr(true)) |
| 2058 | return |
| 2059 | } |
| 2060 | if neverProp || thinPropSetToFalse { |
| 2061 | if thinProp { |
| 2062 | ctx.ModuleErrorf("lto.thin and lto.never are mutually exclusive but were specified together") |
| 2063 | } else { |
| 2064 | ltoBoolFeatures.SetSelectValue(axis, config, BoolPtr(false)) |
| 2065 | } |
| 2066 | return |
| 2067 | } |
| 2068 | } |
| 2069 | ltoBoolFeatures.SetSelectValue(axis, config, nil) |
| 2070 | }) |
| 2071 | |
| 2072 | props := m.GetArchVariantProperties(ctx, <OProperties{}) |
| 2073 | ltoStringFeatures, err := ltoBoolFeatures.ToStringListAttribute(func(boolPtr *bool, axis bazel.ConfigurationAxis, config string) []string { |
| 2074 | if boolPtr == nil { |
| 2075 | return []string{} |
| 2076 | } |
| 2077 | if !*boolPtr { |
| 2078 | return []string{"-" + lto_feature_name} |
| 2079 | } |
| 2080 | features := []string{lto_feature_name} |
| 2081 | if ltoProps, ok := props[axis][config].(*LTOProperties); ok { |
| 2082 | if ltoProps.Whole_program_vtables != nil && *ltoProps.Whole_program_vtables { |
| 2083 | features = append(features, "android_thin_lto_whole_program_vtables") |
| 2084 | } |
| 2085 | } |
| 2086 | return features |
| 2087 | }) |
| 2088 | if err != nil { |
| 2089 | ctx.ModuleErrorf("Error processing LTO attributes: %s", err) |
| 2090 | } |
| 2091 | return ltoStringFeatures |
| 2092 | } |
Trevor Radcliffe | a8b4416 | 2023-04-14 18:25:24 +0000 | [diff] [blame] | 2093 | |
| 2094 | func convertHiddenVisibilityToFeatureBase(ctx android.BazelConversionPathContext, m *Module) bazel.StringListAttribute { |
| 2095 | visibilityHiddenFeature := bazel.StringListAttribute{} |
| 2096 | bp2BuildPropParseHelper(ctx, m, &BaseCompilerProperties{}, func(axis bazel.ConfigurationAxis, configString string, props interface{}) { |
| 2097 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
| 2098 | convertHiddenVisibilityToFeatureHelper(&visibilityHiddenFeature, axis, configString, baseCompilerProps.Cflags) |
| 2099 | } |
| 2100 | }) |
| 2101 | return visibilityHiddenFeature |
| 2102 | } |
| 2103 | |
| 2104 | func convertHiddenVisibilityToFeatureStaticOrShared(ctx android.BazelConversionPathContext, m *Module, isStatic bool) bazel.StringListAttribute { |
| 2105 | visibilityHiddenFeature := bazel.StringListAttribute{} |
| 2106 | if isStatic { |
| 2107 | bp2BuildPropParseHelper(ctx, m, &StaticProperties{}, func(axis bazel.ConfigurationAxis, configString string, props interface{}) { |
| 2108 | if staticProps, ok := props.(*StaticProperties); ok { |
| 2109 | convertHiddenVisibilityToFeatureHelper(&visibilityHiddenFeature, axis, configString, staticProps.Static.Cflags) |
| 2110 | } |
| 2111 | }) |
| 2112 | } else { |
| 2113 | bp2BuildPropParseHelper(ctx, m, &SharedProperties{}, func(axis bazel.ConfigurationAxis, configString string, props interface{}) { |
| 2114 | if sharedProps, ok := props.(*SharedProperties); ok { |
| 2115 | convertHiddenVisibilityToFeatureHelper(&visibilityHiddenFeature, axis, configString, sharedProps.Shared.Cflags) |
| 2116 | } |
| 2117 | }) |
| 2118 | } |
| 2119 | |
| 2120 | return visibilityHiddenFeature |
| 2121 | } |
| 2122 | |
| 2123 | func convertHiddenVisibilityToFeatureHelper(feature *bazel.StringListAttribute, axis bazel.ConfigurationAxis, configString string, cflags []string) { |
| 2124 | if inList(config.VisibilityHiddenFlag, cflags) { |
| 2125 | feature.SetSelectValue(axis, configString, []string{"visibility_hidden"}) |
| 2126 | } |
| 2127 | } |