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