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 ( |
| 30 | cSrcPartition = "c" |
| 31 | asSrcPartition = "as" |
| 32 | cppSrcPartition = "cpp" |
| 33 | ) |
| 34 | |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 35 | // staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties -- |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 36 | // 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] | 37 | type staticOrSharedAttributes struct { |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 38 | Srcs bazel.LabelListAttribute |
| 39 | Srcs_c bazel.LabelListAttribute |
| 40 | Srcs_as bazel.LabelListAttribute |
| 41 | Copts bazel.StringListAttribute |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 42 | |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 43 | Deps bazel.LabelListAttribute |
| 44 | Implementation_deps bazel.LabelListAttribute |
| 45 | Dynamic_deps bazel.LabelListAttribute |
| 46 | Implementation_dynamic_deps bazel.LabelListAttribute |
| 47 | Whole_archive_deps bazel.LabelListAttribute |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 48 | |
| 49 | System_dynamic_deps bazel.LabelListAttribute |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame^] | 52 | func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute { |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 53 | // Check that a module is a filegroup type named <label>. |
| 54 | isFilegroupNamed := func(m android.Module, fullLabel string) bool { |
| 55 | if ctx.OtherModuleType(m) != "filegroup" { |
| 56 | return false |
| 57 | } |
| 58 | labelParts := strings.Split(fullLabel, ":") |
| 59 | if len(labelParts) > 2 { |
| 60 | // There should not be more than one colon in a label. |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 61 | ctx.ModuleErrorf("%s is not a valid Bazel label for a filegroup", fullLabel) |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 62 | } |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 63 | return m.Name() == labelParts[len(labelParts)-1] |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 64 | } |
| 65 | |
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 { |
| 69 | return func(ctx bazel.OtherModuleContext, label string) (string, bool) { |
| 70 | m, exists := ctx.ModuleFromName(label) |
| 71 | if !exists { |
| 72 | return label, false |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 73 | } |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 74 | aModule, _ := m.(android.Module) |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 75 | if !isFilegroupNamed(aModule, label) { |
| 76 | return label, false |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 77 | } |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 78 | return label + suffix, true |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 79 | } |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 82 | // TODO(b/190006308): Handle language detection of sources in a Bazel rule. |
| 83 | partitioned := bazel.PartitionLabelListAttribute(ctx, &srcs, bazel.LabelPartitions{ |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame^] | 84 | cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")}, |
| 85 | asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")}, |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 86 | // C++ is the "catch-all" group, and comprises generated sources because we don't |
| 87 | // know the language of these sources until the genrule is executed. |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame^] | 88 | cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true}, |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 89 | }) |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 90 | |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame^] | 91 | return partitioned |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 94 | // bp2BuildParseLibProps returns the attributes for a variant of a cc_library. |
| 95 | func bp2BuildParseLibProps(ctx android.TopDownMutatorContext, module *Module, isStatic bool) staticOrSharedAttributes { |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 96 | lib, ok := module.compiler.(*libraryDecorator) |
| 97 | if !ok { |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 98 | return staticOrSharedAttributes{} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 99 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 100 | return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic) |
| 101 | } |
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 | // bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library. |
| 104 | func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes { |
| 105 | return bp2BuildParseLibProps(ctx, module, false) |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // bp2buildParseStaticProps returns the attributes for the static variant of a cc_library. |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 109 | func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, 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] | 110 | return bp2BuildParseLibProps(ctx, module, true) |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 111 | } |
| 112 | |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 113 | type depsPartition struct { |
| 114 | export bazel.LabelList |
| 115 | implementation bazel.LabelList |
| 116 | } |
| 117 | |
| 118 | type bazelLabelForDepsFn func(android.TopDownMutatorContext, []string) bazel.LabelList |
| 119 | |
| 120 | func partitionExportedAndImplementationsDeps(ctx android.TopDownMutatorContext, allDeps, exportedDeps []string, fn bazelLabelForDepsFn) depsPartition { |
| 121 | implementation, export := android.FilterList(allDeps, exportedDeps) |
| 122 | |
| 123 | return depsPartition{ |
| 124 | export: fn(ctx, export), |
| 125 | implementation: fn(ctx, implementation), |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | type bazelLabelForDepsExcludesFn func(android.TopDownMutatorContext, []string, []string) bazel.LabelList |
| 130 | |
| 131 | func partitionExportedAndImplementationsDepsExcludes(ctx android.TopDownMutatorContext, allDeps, excludes, exportedDeps []string, fn bazelLabelForDepsExcludesFn) depsPartition { |
| 132 | implementation, export := android.FilterList(allDeps, exportedDeps) |
| 133 | |
| 134 | return depsPartition{ |
| 135 | export: fn(ctx, export, excludes), |
| 136 | implementation: fn(ctx, implementation, excludes), |
| 137 | } |
| 138 | } |
| 139 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 140 | func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes { |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 141 | attrs := staticOrSharedAttributes{} |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 142 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 143 | setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) { |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 144 | attrs.Copts.SetSelectValue(axis, config, props.Cflags) |
| 145 | attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs)) |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 146 | 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] | 147 | |
| 148 | staticDeps := partitionExportedAndImplementationsDeps(ctx, props.Static_libs, props.Export_static_lib_headers, bazelLabelForStaticDeps) |
| 149 | attrs.Deps.SetSelectValue(axis, config, staticDeps.export) |
| 150 | attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation) |
| 151 | |
| 152 | sharedDeps := partitionExportedAndImplementationsDeps(ctx, props.Shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDeps) |
| 153 | attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export) |
| 154 | attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation) |
| 155 | |
| 156 | attrs.Whole_archive_deps.SetSelectValue(axis, config, bazelLabelForWholeDeps(ctx, props.Whole_static_libs)) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 157 | } |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 158 | // system_dynamic_deps distinguishes between nil/empty list behavior: |
| 159 | // nil -> use default values |
| 160 | // empty list -> no values specified |
| 161 | attrs.System_dynamic_deps.ForceSpecifyEmptyList = true |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 162 | |
| 163 | if isStatic { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 164 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) { |
| 165 | for config, props := range configToProps { |
| 166 | if staticOrSharedProps, ok := props.(*StaticProperties); ok { |
| 167 | setAttrs(axis, config, staticOrSharedProps.Static) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | } else { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 172 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) { |
| 173 | for config, props := range configToProps { |
| 174 | if staticOrSharedProps, ok := props.(*SharedProperties); ok { |
| 175 | setAttrs(axis, config, staticOrSharedProps.Shared) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame^] | 181 | partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs) |
| 182 | attrs.Srcs = partitionedSrcs[cppSrcPartition] |
| 183 | attrs.Srcs_c = partitionedSrcs[cSrcPartition] |
| 184 | attrs.Srcs_as = partitionedSrcs[asSrcPartition] |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 185 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 186 | return attrs |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 189 | // Convenience struct to hold all attributes parsed from prebuilt properties. |
| 190 | type prebuiltAttributes struct { |
| 191 | Src bazel.LabelAttribute |
| 192 | } |
| 193 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 194 | // NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 195 | func Bp2BuildParsePrebuiltLibraryProps(ctx android.TopDownMutatorContext, module *Module) prebuiltAttributes { |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 196 | var srcLabelAttribute bazel.LabelAttribute |
| 197 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 198 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltLinkerProperties{}) { |
| 199 | for config, props := range configToProps { |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 200 | if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok { |
| 201 | if len(prebuiltLinkerProperties.Srcs) > 1 { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 202 | ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for %s %s\n", axis, config) |
| 203 | continue |
| 204 | } else if len(prebuiltLinkerProperties.Srcs) == 0 { |
| 205 | continue |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 206 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 207 | src := android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0]) |
| 208 | srcLabelAttribute.SetSelectValue(axis, config, src) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 213 | return prebuiltAttributes{ |
| 214 | Src: srcLabelAttribute, |
| 215 | } |
| 216 | } |
| 217 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 218 | // Convenience struct to hold all attributes parsed from compiler properties. |
| 219 | type compilerAttributes struct { |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 220 | // Options for all languages |
| 221 | copts bazel.StringListAttribute |
| 222 | // Assembly options and sources |
| 223 | asFlags bazel.StringListAttribute |
| 224 | asSrcs bazel.LabelListAttribute |
| 225 | // C options and sources |
| 226 | conlyFlags bazel.StringListAttribute |
| 227 | cSrcs bazel.LabelListAttribute |
| 228 | // C++ options and sources |
| 229 | cppFlags bazel.StringListAttribute |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 230 | srcs bazel.LabelListAttribute |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 231 | |
| 232 | rtti bazel.BoolAttribute |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 233 | |
| 234 | // Not affected by arch variants |
| 235 | stl *string |
| 236 | cppStd *string |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 237 | |
| 238 | localIncludes bazel.StringListAttribute |
| 239 | absoluteIncludes bazel.StringListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 242 | // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 243 | func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes { |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 244 | var srcs bazel.LabelListAttribute |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame^] | 245 | var implementationHdrs bazel.LabelListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 246 | var copts bazel.StringListAttribute |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 247 | var asFlags bazel.StringListAttribute |
| 248 | var conlyFlags bazel.StringListAttribute |
| 249 | var cppFlags bazel.StringListAttribute |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 250 | var rtti bazel.BoolAttribute |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 251 | var localIncludes bazel.StringListAttribute |
| 252 | var absoluteIncludes bazel.StringListAttribute |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 253 | var stl *string = nil |
| 254 | var cppStd *string = nil |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 255 | |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 256 | parseCommandLineFlags := func(soongFlags []string) []string { |
| 257 | var result []string |
| 258 | for _, flag := range soongFlags { |
Colin Cross | 52aa4e1 | 2021-05-25 15:20:39 +0000 | [diff] [blame] | 259 | // Soong's cflags can contain spaces, like `-include header.h`. For |
| 260 | // Bazel's copts, split them up to be compatible with the |
| 261 | // no_copts_tokenization feature. |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 262 | result = append(result, strings.Split(flag, " ")...) |
Colin Cross | 52aa4e1 | 2021-05-25 15:20:39 +0000 | [diff] [blame] | 263 | } |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 264 | return result |
| 265 | } |
| 266 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 267 | // Parse srcs from an arch or OS's props value. |
Liz Kammer | 222bdcf | 2021-10-11 14:15:51 -0400 | [diff] [blame] | 268 | parseSrcs := func(props *BaseCompilerProperties) (bazel.LabelList, bool) { |
| 269 | anySrcs := false |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 270 | // Add srcs-like dependencies such as generated files. |
| 271 | // First create a LabelList containing these dependencies, then merge the values with srcs. |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame^] | 272 | generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources) |
| 273 | if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 { |
Liz Kammer | 222bdcf | 2021-10-11 14:15:51 -0400 | [diff] [blame] | 274 | anySrcs = true |
| 275 | } |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 276 | |
Liz Kammer | 222bdcf | 2021-10-11 14:15:51 -0400 | [diff] [blame] | 277 | allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs) |
| 278 | if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 { |
| 279 | anySrcs = true |
| 280 | } |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame^] | 281 | return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 284 | archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 285 | for axis, configToProps := range archVariantCompilerProps { |
| 286 | for config, props := range configToProps { |
| 287 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
| 288 | // If there's arch specific srcs or exclude_srcs, generate a select entry for it. |
| 289 | // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too. |
Liz Kammer | 222bdcf | 2021-10-11 14:15:51 -0400 | [diff] [blame] | 290 | if srcsList, ok := parseSrcs(baseCompilerProps); ok { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 291 | srcs.SetSelectValue(axis, config, srcsList) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 292 | } |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame^] | 293 | if len(baseCompilerProps.Generated_headers) > 0 { |
| 294 | implementationHdrs.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, baseCompilerProps.Generated_headers)) |
| 295 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 296 | |
Jingwen Chen | 97b8531 | 2021-10-08 10:41:31 +0000 | [diff] [blame] | 297 | if axis == bazel.NoConfigAxis { |
| 298 | // If cpp_std is not specified, don't generate it in the |
| 299 | // BUILD file. For readability purposes, cpp_std and gnu_extensions are |
| 300 | // combined into a single -std=<version> copt, except in the |
| 301 | // default case where cpp_std is nil and gnu_extensions is true or unspecified, |
| 302 | // then the toolchain's default "gnu++17" will be used. |
| 303 | if baseCompilerProps.Cpp_std != nil { |
| 304 | // TODO(b/202491296): Handle C_std. |
| 305 | // These transformations are shared with compiler.go. |
| 306 | cppStdVal := parseCppStd(baseCompilerProps.Cpp_std) |
| 307 | _, cppStdVal = maybeReplaceGnuToC(baseCompilerProps.Gnu_extensions, "", cppStdVal) |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 308 | cppStd = &cppStdVal |
Jingwen Chen | 97b8531 | 2021-10-08 10:41:31 +0000 | [diff] [blame] | 309 | } else if baseCompilerProps.Gnu_extensions != nil && !*baseCompilerProps.Gnu_extensions { |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 310 | cppStdVal := "c++17" |
| 311 | cppStd = &cppStdVal |
Jingwen Chen | 97b8531 | 2021-10-08 10:41:31 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 314 | |
| 315 | var archVariantCopts []string |
Jingwen Chen | 97b8531 | 2021-10-08 10:41:31 +0000 | [diff] [blame] | 316 | archVariantCopts = append(archVariantCopts, parseCommandLineFlags(baseCompilerProps.Cflags)...) |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame] | 317 | archVariantAsflags := parseCommandLineFlags(baseCompilerProps.Asflags) |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 318 | |
| 319 | localIncludeDirs := baseCompilerProps.Local_include_dirs |
| 320 | if axis == bazel.NoConfigAxis && includeBuildDirectory(baseCompilerProps.Include_build_directory) { |
| 321 | localIncludeDirs = append(localIncludeDirs, ".") |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame] | 322 | } |
| 323 | |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 324 | absoluteIncludes.SetSelectValue(axis, config, baseCompilerProps.Include_dirs) |
| 325 | localIncludes.SetSelectValue(axis, config, localIncludeDirs) |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 326 | |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame] | 327 | copts.SetSelectValue(axis, config, archVariantCopts) |
| 328 | asFlags.SetSelectValue(axis, config, archVariantAsflags) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 329 | conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Conlyflags)) |
| 330 | cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Cppflags)) |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 331 | rtti.SetSelectValue(axis, config, baseCompilerProps.Rtti) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 332 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 336 | srcs.ResolveExcludes() |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame^] | 337 | partitionedSrcs := groupSrcsByExtension(ctx, srcs) |
| 338 | |
| 339 | for p, lla := range partitionedSrcs { |
| 340 | // if there are no sources, there is no need for headers |
| 341 | if lla.IsEmpty() { |
| 342 | continue |
| 343 | } |
| 344 | lla.Append(implementationHdrs) |
| 345 | partitionedSrcs[p] = lla |
| 346 | } |
| 347 | |
| 348 | srcs = partitionedSrcs[cppSrcPartition] |
| 349 | cSrcs := partitionedSrcs[cSrcPartition] |
| 350 | asSrcs := partitionedSrcs[asSrcPartition] |
| 351 | |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 352 | absoluteIncludes.DeduplicateAxesFromBase() |
| 353 | localIncludes.DeduplicateAxesFromBase() |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 354 | |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 355 | productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{ |
| 356 | "Cflags": &copts, |
| 357 | "Asflags": &asFlags, |
| 358 | "CppFlags": &cppFlags, |
| 359 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 360 | productVariableProps := android.ProductVariableProperties(ctx) |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 361 | for propName, attr := range productVarPropNameToAttribute { |
| 362 | if props, exists := productVariableProps[propName]; exists { |
| 363 | for _, prop := range props { |
| 364 | flags, ok := prop.Property.([]string) |
| 365 | if !ok { |
| 366 | ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName)) |
| 367 | } |
| 368 | newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 369 | attr.SetSelectValue(bazel.ProductVariableConfigurationAxis(prop.FullConfig), prop.FullConfig, newFlags) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 370 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
Chris Parsons | a967f25 | 2021-09-23 16:34:35 -0400 | [diff] [blame] | 374 | stlPropsByArch := module.GetArchVariantProperties(ctx, &StlProperties{}) |
| 375 | for _, configToProps := range stlPropsByArch { |
| 376 | for _, props := range configToProps { |
| 377 | if stlProps, ok := props.(*StlProperties); ok { |
| 378 | if stlProps.Stl != nil { |
| 379 | if stl == nil { |
| 380 | stl = stlProps.Stl |
| 381 | } else { |
| 382 | if stl != stlProps.Stl { |
| 383 | ctx.ModuleErrorf("Unsupported conversion: module with different stl for different variants: %s and %s", *stl, stlProps.Stl) |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 391 | return compilerAttributes{ |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 392 | copts: copts, |
| 393 | srcs: srcs, |
| 394 | asFlags: asFlags, |
| 395 | asSrcs: asSrcs, |
| 396 | cSrcs: cSrcs, |
| 397 | conlyFlags: conlyFlags, |
| 398 | cppFlags: cppFlags, |
| 399 | rtti: rtti, |
Chris Parsons | a967f25 | 2021-09-23 16:34:35 -0400 | [diff] [blame] | 400 | stl: stl, |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 401 | cppStd: cppStd, |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 402 | localIncludes: localIncludes, |
| 403 | absoluteIncludes: absoluteIncludes, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
| 407 | // Convenience struct to hold all attributes parsed from linker properties. |
| 408 | type linkerAttributes struct { |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 409 | deps bazel.LabelListAttribute |
| 410 | implementationDeps bazel.LabelListAttribute |
| 411 | dynamicDeps bazel.LabelListAttribute |
| 412 | implementationDynamicDeps bazel.LabelListAttribute |
| 413 | wholeArchiveDeps bazel.LabelListAttribute |
| 414 | systemDynamicDeps bazel.LabelListAttribute |
| 415 | |
Jingwen Chen | 6ada589 | 2021-09-17 11:38:09 +0000 | [diff] [blame] | 416 | linkCrt bazel.BoolAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 417 | useLibcrt bazel.BoolAttribute |
| 418 | linkopts bazel.StringListAttribute |
Liz Kammer | d287118 | 2021-10-04 13:54:37 -0400 | [diff] [blame] | 419 | additionalLinkerInputs bazel.LabelListAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 420 | stripKeepSymbols bazel.BoolAttribute |
| 421 | stripKeepSymbolsAndDebugFrame bazel.BoolAttribute |
| 422 | stripKeepSymbolsList bazel.StringListAttribute |
| 423 | stripAll bazel.BoolAttribute |
| 424 | stripNone bazel.BoolAttribute |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 425 | features bazel.StringListAttribute |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 426 | } |
| 427 | |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 428 | // bp2BuildParseLinkerProps parses the linker properties of a module, including |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 429 | // configurable attribute values. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 430 | func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes { |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 431 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 432 | var headerDeps bazel.LabelListAttribute |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 433 | var implementationHeaderDeps bazel.LabelListAttribute |
| 434 | var deps bazel.LabelListAttribute |
| 435 | var implementationDeps bazel.LabelListAttribute |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 436 | var dynamicDeps bazel.LabelListAttribute |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 437 | var implementationDynamicDeps bazel.LabelListAttribute |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 438 | var wholeArchiveDeps bazel.LabelListAttribute |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 439 | systemSharedDeps := bazel.LabelListAttribute{ForceSpecifyEmptyList: true} |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 440 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 441 | var linkopts bazel.StringListAttribute |
Jingwen Chen | 6ada589 | 2021-09-17 11:38:09 +0000 | [diff] [blame] | 442 | var linkCrt bazel.BoolAttribute |
Liz Kammer | d287118 | 2021-10-04 13:54:37 -0400 | [diff] [blame] | 443 | var additionalLinkerInputs bazel.LabelListAttribute |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 444 | var useLibcrt bazel.BoolAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 445 | |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 446 | var stripKeepSymbols bazel.BoolAttribute |
| 447 | var stripKeepSymbolsAndDebugFrame bazel.BoolAttribute |
| 448 | var stripKeepSymbolsList bazel.StringListAttribute |
| 449 | var stripAll bazel.BoolAttribute |
| 450 | var stripNone bazel.BoolAttribute |
| 451 | |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 452 | var features bazel.StringListAttribute |
| 453 | |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 454 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &StripProperties{}) { |
| 455 | for config, props := range configToProps { |
| 456 | if stripProperties, ok := props.(*StripProperties); ok { |
| 457 | stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols) |
| 458 | stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list) |
| 459 | stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame) |
| 460 | stripAll.SetSelectValue(axis, config, stripProperties.Strip.All) |
| 461 | stripNone.SetSelectValue(axis, config, stripProperties.Strip.None) |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
Jingwen Chen | 6ada589 | 2021-09-17 11:38:09 +0000 | [diff] [blame] | 466 | // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module |
| 467 | var disallowedArchVariantCrt bool |
| 468 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 469 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) { |
| 470 | for config, props := range configToProps { |
| 471 | if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok { |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 472 | var axisFeatures []string |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 473 | |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 474 | // Excludes to parallel Soong: |
| 475 | // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0 |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 476 | staticLibs := android.FirstUniqueStrings(baseLinkerProps.Static_libs) |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 477 | staticDeps := partitionExportedAndImplementationsDepsExcludes(ctx, staticLibs, baseLinkerProps.Exclude_static_libs, baseLinkerProps.Export_static_lib_headers, bazelLabelForStaticDepsExcludes) |
| 478 | deps.SetSelectValue(axis, config, staticDeps.export) |
| 479 | implementationDeps.SetSelectValue(axis, config, staticDeps.implementation) |
| 480 | |
| 481 | wholeStaticLibs := android.FirstUniqueStrings(baseLinkerProps.Whole_static_libs) |
| 482 | wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, baseLinkerProps.Exclude_static_libs)) |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 483 | |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 484 | systemSharedLibs := baseLinkerProps.System_shared_libs |
| 485 | // systemSharedLibs distinguishes between nil/empty list behavior: |
| 486 | // nil -> use default values |
| 487 | // empty list -> no values specified |
| 488 | if len(systemSharedLibs) > 0 { |
| 489 | systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs) |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 490 | } |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 491 | systemSharedDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs)) |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 492 | |
| 493 | sharedLibs := android.FirstUniqueStrings(baseLinkerProps.Shared_libs) |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 494 | sharedDeps := partitionExportedAndImplementationsDepsExcludes(ctx, sharedLibs, baseLinkerProps.Exclude_shared_libs, baseLinkerProps.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes) |
| 495 | dynamicDeps.SetSelectValue(axis, config, sharedDeps.export) |
| 496 | implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 497 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 498 | headerLibs := android.FirstUniqueStrings(baseLinkerProps.Header_libs) |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 499 | hDeps := partitionExportedAndImplementationsDeps(ctx, headerLibs, baseLinkerProps.Export_header_lib_headers, bazelLabelForHeaderDeps) |
| 500 | |
| 501 | headerDeps.SetSelectValue(axis, config, hDeps.export) |
| 502 | implementationHeaderDeps.SetSelectValue(axis, config, hDeps.implementation) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 503 | |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 504 | if !BoolDefault(baseLinkerProps.Pack_relocations, packRelocationsDefault) { |
| 505 | axisFeatures = append(axisFeatures, "disable_pack_relocations") |
| 506 | } |
| 507 | |
| 508 | if Bool(baseLinkerProps.Allow_undefined_symbols) { |
| 509 | axisFeatures = append(axisFeatures, "-no_undefined_symbols") |
| 510 | } |
| 511 | |
Liz Kammer | d287118 | 2021-10-04 13:54:37 -0400 | [diff] [blame] | 512 | var linkerFlags []string |
| 513 | if len(baseLinkerProps.Ldflags) > 0 { |
| 514 | linkerFlags = append(linkerFlags, baseLinkerProps.Ldflags...) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 515 | } |
Liz Kammer | d287118 | 2021-10-04 13:54:37 -0400 | [diff] [blame] | 516 | if baseLinkerProps.Version_script != nil { |
| 517 | label := android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script) |
| 518 | additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}}) |
| 519 | linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label)) |
| 520 | } |
| 521 | linkopts.SetSelectValue(axis, config, linkerFlags) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 522 | useLibcrt.SetSelectValue(axis, config, baseLinkerProps.libCrt()) |
Jingwen Chen | 6ada589 | 2021-09-17 11:38:09 +0000 | [diff] [blame] | 523 | |
| 524 | // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it. |
| 525 | if baseLinkerProps.crt() != nil { |
| 526 | if axis == bazel.NoConfigAxis { |
| 527 | linkCrt.SetSelectValue(axis, config, baseLinkerProps.crt()) |
| 528 | } else if axis == bazel.ArchConfigurationAxis { |
| 529 | disallowedArchVariantCrt = true |
| 530 | } |
| 531 | } |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 532 | |
| 533 | if axisFeatures != nil { |
| 534 | features.SetSelectValue(axis, config, axisFeatures) |
| 535 | } |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 536 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
Jingwen Chen | 6ada589 | 2021-09-17 11:38:09 +0000 | [diff] [blame] | 540 | if disallowedArchVariantCrt { |
| 541 | ctx.ModuleErrorf("nocrt is not supported for arch variants") |
| 542 | } |
| 543 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 544 | type productVarDep struct { |
| 545 | // the name of the corresponding excludes field, if one exists |
| 546 | excludesField string |
| 547 | // reference to the bazel attribute that should be set for the given product variable config |
| 548 | attribute *bazel.LabelListAttribute |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 549 | |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 550 | depResolutionFunc func(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | productVarToDepFields := map[string]productVarDep{ |
| 554 | // product variables do not support exclude_shared_libs |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 555 | "Shared_libs": productVarDep{attribute: &implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes}, |
| 556 | "Static_libs": productVarDep{"Exclude_static_libs", &implementationDeps, bazelLabelForStaticDepsExcludes}, |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 557 | "Whole_static_libs": productVarDep{"Exclude_static_libs", &wholeArchiveDeps, bazelLabelForWholeDepsExcludes}, |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | productVariableProps := android.ProductVariableProperties(ctx) |
| 561 | for name, dep := range productVarToDepFields { |
| 562 | props, exists := productVariableProps[name] |
| 563 | excludeProps, excludesExists := productVariableProps[dep.excludesField] |
| 564 | // if neither an include or excludes property exists, then skip it |
| 565 | if !exists && !excludesExists { |
| 566 | continue |
| 567 | } |
| 568 | // collect all the configurations that an include or exclude property exists for. |
| 569 | // we want to iterate all configurations rather than either the include or exclude because for a |
| 570 | // particular configuration we may have only and include or only an exclude to handle |
| 571 | configs := make(map[string]bool, len(props)+len(excludeProps)) |
| 572 | for config := range props { |
| 573 | configs[config] = true |
| 574 | } |
| 575 | for config := range excludeProps { |
| 576 | configs[config] = true |
| 577 | } |
| 578 | |
| 579 | for config := range configs { |
| 580 | prop, includesExists := props[config] |
| 581 | excludesProp, excludesExists := excludeProps[config] |
| 582 | var includes, excludes []string |
| 583 | var ok bool |
| 584 | // if there was no includes/excludes property, casting fails and that's expected |
| 585 | if includes, ok = prop.Property.([]string); includesExists && !ok { |
| 586 | ctx.ModuleErrorf("Could not convert product variable %s property", name) |
| 587 | } |
| 588 | if excludes, ok = excludesProp.Property.([]string); excludesExists && !ok { |
| 589 | ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField) |
| 590 | } |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 591 | |
| 592 | dep.attribute.SetSelectValue(bazel.ProductVariableConfigurationAxis(config), config, dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes)) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 596 | headerDeps.Append(deps) |
| 597 | implementationHeaderDeps.Append(implementationDeps) |
| 598 | |
| 599 | headerDeps.ResolveExcludes() |
| 600 | implementationHeaderDeps.ResolveExcludes() |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 601 | dynamicDeps.ResolveExcludes() |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 602 | implementationDynamicDeps.ResolveExcludes() |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 603 | wholeArchiveDeps.ResolveExcludes() |
| 604 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 605 | return linkerAttributes{ |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 606 | deps: headerDeps, |
| 607 | implementationDeps: implementationHeaderDeps, |
| 608 | dynamicDeps: dynamicDeps, |
| 609 | implementationDynamicDeps: implementationDynamicDeps, |
| 610 | wholeArchiveDeps: wholeArchiveDeps, |
| 611 | systemDynamicDeps: systemSharedDeps, |
| 612 | |
Liz Kammer | d287118 | 2021-10-04 13:54:37 -0400 | [diff] [blame] | 613 | linkCrt: linkCrt, |
| 614 | linkopts: linkopts, |
| 615 | useLibcrt: useLibcrt, |
| 616 | additionalLinkerInputs: additionalLinkerInputs, |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 617 | |
| 618 | // Strip properties |
| 619 | stripKeepSymbols: stripKeepSymbols, |
| 620 | stripKeepSymbolsAndDebugFrame: stripKeepSymbolsAndDebugFrame, |
| 621 | stripKeepSymbolsList: stripKeepSymbolsList, |
| 622 | stripAll: stripAll, |
| 623 | stripNone: stripNone, |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 624 | |
| 625 | features: features, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 626 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 627 | } |
| 628 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 629 | // Relativize a list of root-relative paths with respect to the module's |
| 630 | // directory. |
| 631 | // |
| 632 | // include_dirs Soong prop are root-relative (b/183742505), but |
| 633 | // local_include_dirs, export_include_dirs and export_system_include_dirs are |
| 634 | // module dir relative. This function makes a list of paths entirely module dir |
| 635 | // relative. |
| 636 | // |
| 637 | // For the `include` attribute, Bazel wants the paths to be relative to the |
| 638 | // module. |
| 639 | func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 640 | var relativePaths []string |
| 641 | for _, path := range paths { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 642 | // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path |
| 643 | relativePath, err := filepath.Rel(ctx.ModuleDir(), path) |
| 644 | if err != nil { |
| 645 | panic(err) |
| 646 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 647 | relativePaths = append(relativePaths, relativePath) |
| 648 | } |
| 649 | return relativePaths |
| 650 | } |
| 651 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 652 | // BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel |
| 653 | // attributes. |
| 654 | type BazelIncludes struct { |
| 655 | Includes bazel.StringListAttribute |
| 656 | SystemIncludes bazel.StringListAttribute |
| 657 | } |
| 658 | |
| 659 | func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) BazelIncludes { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 660 | libraryDecorator := module.linker.(*libraryDecorator) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 661 | return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator) |
| 662 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 663 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 664 | // Bp2buildParseExportedIncludesForPrebuiltLibrary returns a BazelIncludes with Bazel-ified values |
| 665 | // to export includes from the underlying module's properties. |
| 666 | func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.TopDownMutatorContext, module *Module) BazelIncludes { |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 667 | prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker) |
| 668 | libraryDecorator := prebuiltLibraryLinker.libraryDecorator |
| 669 | return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator) |
| 670 | } |
| 671 | |
| 672 | // bp2BuildParseExportedIncludes creates a string list attribute contains the |
| 673 | // exported included directories of a module. |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 674 | func bp2BuildParseExportedIncludesHelper(ctx android.TopDownMutatorContext, module *Module, libraryDecorator *libraryDecorator) BazelIncludes { |
| 675 | exported := BazelIncludes{} |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 676 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) { |
| 677 | for config, props := range configToProps { |
| 678 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 679 | if len(flagExporterProperties.Export_include_dirs) > 0 { |
| 680 | exported.Includes.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs) |
| 681 | } |
| 682 | if len(flagExporterProperties.Export_system_include_dirs) > 0 { |
| 683 | exported.SystemIncludes.SetSelectValue(axis, config, flagExporterProperties.Export_system_include_dirs) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 684 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 685 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 686 | } |
| 687 | } |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 688 | exported.Includes.DeduplicateAxesFromBase() |
| 689 | exported.SystemIncludes.DeduplicateAxesFromBase() |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 690 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 691 | return exported |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 692 | } |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 693 | |
| 694 | func bazelLabelForStaticModule(ctx android.TopDownMutatorContext, m blueprint.Module) string { |
| 695 | label := android.BazelModuleLabel(ctx, m) |
| 696 | if aModule, ok := m.(android.Module); ok { |
| 697 | if ctx.OtherModuleType(aModule) == "cc_library" && !android.GenerateCcLibraryStaticOnly(m.Name()) { |
| 698 | label += "_bp2build_cc_library_static" |
| 699 | } |
| 700 | } |
| 701 | return label |
| 702 | } |
| 703 | |
| 704 | func bazelLabelForSharedModule(ctx android.TopDownMutatorContext, m blueprint.Module) string { |
| 705 | // cc_library, at it's root name, propagates the shared library, which depends on the static |
| 706 | // library. |
| 707 | return android.BazelModuleLabel(ctx, m) |
| 708 | } |
| 709 | |
| 710 | func bazelLabelForStaticWholeModuleDeps(ctx android.TopDownMutatorContext, m blueprint.Module) string { |
| 711 | label := bazelLabelForStaticModule(ctx, m) |
| 712 | if aModule, ok := m.(android.Module); ok { |
| 713 | if android.IsModulePrebuilt(aModule) { |
| 714 | label += "_alwayslink" |
| 715 | } |
| 716 | } |
| 717 | return label |
| 718 | } |
| 719 | |
| 720 | func bazelLabelForWholeDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList { |
| 721 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps) |
| 722 | } |
| 723 | |
| 724 | func bazelLabelForWholeDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList { |
| 725 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps) |
| 726 | } |
| 727 | |
| 728 | func bazelLabelForStaticDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList { |
| 729 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule) |
| 730 | } |
| 731 | |
| 732 | func bazelLabelForStaticDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList { |
| 733 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule) |
| 734 | } |
| 735 | |
| 736 | func bazelLabelForSharedDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList { |
| 737 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule) |
| 738 | } |
| 739 | |
| 740 | func bazelLabelForHeaderDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList { |
| 741 | // This is not elegant, but bp2build's shared library targets only propagate |
| 742 | // their header information as part of the normal C++ provider. |
| 743 | return bazelLabelForSharedDeps(ctx, modules) |
| 744 | } |
| 745 | |
| 746 | func bazelLabelForSharedDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList { |
| 747 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule) |
| 748 | } |