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