xref: /aosp_15_r20/external/bazelbuild-rules_rust/bindgen/3rdparty/patches/llvm-project.cxx17.patch (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1diff --git a/utils/bazel/configure.bzl b/utils/bazel/configure.bzl
2index 4c5ab8bd0972..44de2da1a136 100644
3--- a/utils/bazel/configure.bzl
4+++ b/utils/bazel/configure.bzl
5@@ -70,6 +70,11 @@ def _overlay_directories(repository_ctx):
6         ))
7
8 def _llvm_configure_impl(repository_ctx):
9+    # Force this repository rule to update if any of the overlay
10+    # sources change.
11+    for target in repository_ctx.attr._srcs:
12+        repository_ctx.path(target)
13+
14     _overlay_directories(repository_ctx)
15
16     # Create a starlark file with the requested LLVM targets.
17@@ -86,6 +91,16 @@ llvm_configure = repository_rule(
18     configure = True,
19     attrs = {
20         "targets": attr.string_list(default = DEFAULT_TARGETS),
21+        "_srcs": attr.label_list(default = [
22+            Label("//utils/bazel/llvm-project-overlay/clang:BUILD.bazel"),
23+            Label("//utils/bazel/llvm-project-overlay/libunwind:BUILD.bazel"),
24+            Label("//utils/bazel/llvm-project-overlay/llvm:binary_alias.bzl"),
25+            Label("//utils/bazel/llvm-project-overlay/llvm:BUILD.bazel"),
26+            Label("//utils/bazel/llvm-project-overlay/llvm:cc_plugin_library.bzl"),
27+            Label("//utils/bazel/llvm-project-overlay/llvm:config.bzl"),
28+            Label("//utils/bazel/llvm-project-overlay/llvm:tblgen.bzl"),
29+            Label("//utils/bazel/llvm-project-overlay/llvm:template_rule.bzl"),
30+        ]),
31     },
32 )
33
34diff --git a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
35index a2b1e0f33073..0c1abe607b58 100644
36--- a/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
37+++ b/utils/bazel/llvm-project-overlay/clang/BUILD.bazel
38@@ -5,6 +5,7 @@
39 load("//llvm:tblgen.bzl", "gentbl")
40 load("//llvm:binary_alias.bzl", "binary_alias")
41 load("//llvm:cc_plugin_library.bzl", "cc_plugin_library")
42+load("//llvm:config.bzl", "llvm_stdcxx_copts")
43
44 package(
45     default_visibility = ["//visibility:public"],
46@@ -28,7 +29,7 @@ cc_binary(
47     ]),
48     copts = [
49         "$(STACK_FRAME_UNLIMITED)",
50-    ],
51+    ] + llvm_stdcxx_copts,
52     stamp = 0,
53     deps = [
54         "//llvm:Support",
55@@ -391,8 +392,8 @@ cc_library(
56 cc_library(
57     name = "basic",
58     srcs = [
59-        "include/clang/Basic/Version.inc",
60         "include/VCSVersion.inc",
61+        "include/clang/Basic/Version.inc",
62     ] + glob([
63         "lib/Basic/*.cpp",
64         "lib/Basic/*.c",
65@@ -406,7 +407,7 @@ cc_library(
66     copts = [
67         "-DHAVE_VCS_VERSION_INC",
68         "$(STACK_FRAME_UNLIMITED)",
69-    ],
70+    ] + llvm_stdcxx_copts,
71     includes = ["include"],
72     textual_hdrs = [
73         "include/clang/Basic/arm_fp16.inc",
74@@ -471,6 +472,7 @@ cc_library(
75     hdrs = glob([
76         "include/clang/Lex/*.h",
77     ]),
78+    copts = llvm_stdcxx_copts,
79     includes = ["include"],
80     deps = [
81         ":basic",
82@@ -711,7 +713,7 @@ cc_library(
83         # headers such as `CXXABI.h`.
84         "-I$(GENDIR)/external/llvm-project/clang/lib/AST",
85         "-I$(GENDIR)/external/llvm-project/clang/lib/AST/Interp",
86-    ],
87+    ] + llvm_stdcxx_copts,
88     textual_hdrs = [
89         "include/clang/AST/AttrImpl.inc",
90         "include/clang/AST/AttrNodeTraverse.inc",
91@@ -763,6 +765,7 @@ cc_library(
92         "include/clang/Index/*.h",
93         "include/clang-c/*.h",
94     ]),
95+    copts = llvm_stdcxx_copts,
96     includes = ["include"],
97     deps = [
98         ":ast",
99@@ -786,6 +789,7 @@ cc_library(
100     hdrs = glob([
101         "include/clang/Analysis/**/*.h",
102     ]),
103+    copts = llvm_stdcxx_copts,
104     includes = ["include"],
105     textual_hdrs = glob([
106         "include/clang/Analysis/**/*.def",
107@@ -844,7 +848,7 @@ cc_library(
108         "include/clang/Sema/*.h",
109         "include/clang-c/*.h",
110     ]),
111-    copts = ["$(STACK_FRAME_UNLIMITED)"],
112+    copts = ["$(STACK_FRAME_UNLIMITED)"] + llvm_stdcxx_copts,
113     includes = ["include"],
114     textual_hdrs = [
115         "include/clang/Sema/AttrParsedAttrImpl.inc",
116@@ -911,6 +915,7 @@ cc_library(
117         "include/clang/Parse/AttrParserStringSwitches.inc",
118         "include/clang/Parse/AttrSubMatchRulesParserStringSwitches.inc",
119     ] + glob(["include/clang/Parse/*.h"]),
120+    copts = llvm_stdcxx_copts,
121     includes = ["include"],
122     deps = [
123         ":ast",
124@@ -932,6 +937,7 @@ cc_library(
125         "lib/ASTMatchers/*.h",
126     ]),
127     hdrs = glob(["include/clang/ASTMatchers/*.h"]),
128+    copts = llvm_stdcxx_copts,
129     includes = ["include"],
130     deps = [
131         ":ast",
132@@ -948,7 +954,7 @@ cc_library(
133         "lib/ASTMatchers/Dynamic/*.h",
134     ]),
135     hdrs = glob(["include/clang/ASTMatchers/Dynamic/*.h"]),
136-    copts = ["$(STACK_FRAME_UNLIMITED)"],
137+    copts = ["$(STACK_FRAME_UNLIMITED)"] + llvm_stdcxx_copts,
138     includes = ["include"],
139     deps = [
140         ":ast",
141@@ -966,6 +972,7 @@ cc_library(
142         "lib/Rewrite/*.h",
143     ]),
144     hdrs = glob(["include/clang/Rewrite/Core/*.h"]),
145+    copts = llvm_stdcxx_copts,
146     includes = ["include"],
147     deps = [
148         ":ast",
149@@ -982,6 +989,7 @@ cc_library(
150         "lib/Testing/*.cpp",
151     ]),
152     hdrs = glob(["include/clang/Testing/*.h"]),
153+    copts = llvm_stdcxx_copts,
154     includes = ["include"],
155     deps = [
156         ":basic",
157@@ -996,6 +1004,7 @@ cc_library(
158         "lib/Tooling/Core/*.h",
159     ]),
160     hdrs = glob(["include/clang/Tooling/Core/*.h"]),
161+    copts = llvm_stdcxx_copts,
162     includes = ["include"],
163     deps = [
164         ":ast",
165@@ -1018,6 +1027,7 @@ cc_library(
166     hdrs = glob([
167         "include/clang/Tooling/*.h",
168     ]),
169+    copts = llvm_stdcxx_copts,
170     includes = ["include"],
171     deps = [
172         ":ast",
173@@ -1042,6 +1052,7 @@ cc_library(
174     hdrs = glob([
175         "include/clang/Tooling/Inclusions/**/*.h",
176     ]),
177+    copts = llvm_stdcxx_copts,
178     deps = [
179         ":basic",
180         ":lex",
181@@ -1061,6 +1072,7 @@ cc_library(
182         "include/clang/Tooling/Refactoring/**/*.h",
183         "include/clang/Tooling/Refactoring/**/*.def",
184     ]),
185+    copts = llvm_stdcxx_copts,
186     deps = [
187         ":ast",
188         ":ast_matchers",
189@@ -1094,6 +1106,7 @@ cc_library(
190     name = "tooling_syntax",
191     srcs = glob(["lib/Tooling/Syntax/**/*.cpp"]),
192     hdrs = glob(["include/clang/Tooling/Syntax/**/*.h"]),
193+    copts = llvm_stdcxx_copts,
194     deps = [
195         ":ast",
196         ":basic",
197@@ -1108,6 +1121,7 @@ cc_library(
198     name = "tooling_dependency_scanning",
199     srcs = glob(["lib/Tooling/DependencyScanning/**/*.cpp"]),
200     hdrs = glob(["include/clang/Tooling/DependencyScanning/**/*.h"]),
201+    copts = llvm_stdcxx_copts,
202     deps = [
203         ":basic",
204         ":codegen",
205@@ -1123,6 +1137,7 @@ cc_library(
206     name = "transformer",
207     srcs = glob(["lib/Tooling/Transformer/**/*.cpp"]),
208     hdrs = glob(["include/clang/Tooling/Transformer/**/*.h"]),
209+    copts = llvm_stdcxx_copts,
210     deps = [
211         ":ast",
212         ":ast_matchers",
213@@ -1138,6 +1153,7 @@ cc_library(
214     name = "ast-diff",
215     srcs = glob(["lib/Tooling/ASTDiff/*.cpp"]),
216     hdrs = glob(["include/clang/Tooling/ASTDiff/*.h"]),
217+    copts = llvm_stdcxx_copts,
218     deps = [
219         ":ast",
220         ":basic",
221@@ -1150,6 +1166,7 @@ cc_library(
222     name = "crosstu",
223     srcs = glob(["lib/CrossTU/*.cpp"]),
224     hdrs = glob(["include/clang/CrossTU/*.h"]),
225+    copts = llvm_stdcxx_copts,
226     deps = [
227         ":ast",
228         ":basic",
229@@ -1174,6 +1191,7 @@ cc_library(
230     ] + glob([
231         "include/clang/Format/*.h",
232     ]),
233+    copts = llvm_stdcxx_copts,
234     includes = ["include"],
235     deps = [
236         ":basic",
237@@ -1188,6 +1206,7 @@ cc_library(
238     name = "edit",
239     srcs = glob(["lib/Edit/*.cpp"]),
240     hdrs = glob(["include/clang/Edit/*.h"]),
241+    copts = llvm_stdcxx_copts,
242     includes = ["include"],
243     deps = [
244         ":ast",
245@@ -1222,6 +1241,7 @@ cc_library(
246     hdrs = glob([
247         "include/clang/StaticAnalyzer/Core/**/*.h",
248     ]),
249+    copts = llvm_stdcxx_copts,
250     includes = ["include"],
251     textual_hdrs = glob([
252         "include/clang/StaticAnalyzer/Core/**/*.def",
253@@ -1268,7 +1288,7 @@ cc_library(
254     ] + glob([
255         "include/clang/StaticAnalyzer/Checkers/**/*.h",
256     ]),
257-    copts = ["$(STACK_FRAME_UNLIMITED)"],
258+    copts = ["$(STACK_FRAME_UNLIMITED)"] + llvm_stdcxx_copts,
259     includes = ["include"],
260     deps = [
261         ":analysis",
262@@ -1316,7 +1336,7 @@ cc_library(
263     hdrs = glob([
264         "include/clang/Driver/*.h",
265     ]),
266-    copts = ["$(STACK_FRAME_UNLIMITED)"],
267+    copts = ["$(STACK_FRAME_UNLIMITED)"] + llvm_stdcxx_copts,
268     includes = [
269         "include",
270         # TODO: This is likely a layering issue, but files in Arch are currently
271@@ -1479,7 +1499,7 @@ cc_library(
272     hdrs = glob([
273         "include/clang/Frontend/*.h",
274     ]),
275-    copts = ["$(STACK_FRAME_UNLIMITED)"],
276+    copts = ["$(STACK_FRAME_UNLIMITED)"] + llvm_stdcxx_copts,
277     data = [":builtin_headers_gen"],
278     includes = ["include"],
279     textual_hdrs = glob([
280@@ -1519,6 +1539,7 @@ cc_library(
281         "lib/Frontend/Rewrite/*.h",
282     ]),
283     hdrs = glob(["include/clang/Rewrite/Frontend/*.h"]),
284+    copts = llvm_stdcxx_copts,
285     includes = ["include"],
286     deps = [
287         ":ast",
288@@ -1541,6 +1562,7 @@ cc_library(
289         "lib/Interpreter/*.h",
290     ]),
291     hdrs = glob(["include/clang/Interpreter/*.h"]),
292+    copts = llvm_stdcxx_copts,
293     includes = ["include"],
294     deps = [
295         ":analysis",
296@@ -1573,7 +1595,7 @@ cc_library(
297         "lib/CodeGen/*.h",
298     ]),
299     hdrs = glob(["include/clang/CodeGen/*.h"]),
300-    copts = ["$(STACK_FRAME_UNLIMITED)"],
301+    copts = ["$(STACK_FRAME_UNLIMITED)"] + llvm_stdcxx_copts,
302     includes = ["include"],
303     deps = [
304         ":analysis",
305@@ -1624,6 +1646,7 @@ cc_library(
306         "lib/StaticAnalyzer/Frontend/**/*.h",
307     ]),
308     hdrs = glob(["include/clang/StaticAnalyzer/Frontend/**/*.h"]),
309+    copts = llvm_stdcxx_copts,
310     includes = ["include"],
311     deps = [
312         ":analysis",
313@@ -1678,6 +1701,7 @@ cc_library(
314     hdrs = glob([
315         "include/clang/Serialization/*.h",
316     ]),
317+    copts = llvm_stdcxx_copts,
318     includes = ["include"],
319     textual_hdrs = glob([
320         "include/clang/Serialization/*.def",
321@@ -1705,6 +1729,7 @@ cc_library(
322         "lib/FrontendTool/*.h",
323     ]),
324     hdrs = glob(["include/clang/FrontendTool/*.h"]),
325+    copts = llvm_stdcxx_copts,
326     includes = ["include"],
327     deps = [
328         ":arc_migrate",
329@@ -1726,6 +1751,7 @@ cc_library(
330         "lib/ARCMigrate/*.h",
331     ]),
332     hdrs = glob(["include/clang/ARCMigrate/*.h"]),
333+    copts = llvm_stdcxx_copts,
334     includes = ["include"],
335     deps = [
336         ":analysis",
337@@ -1752,6 +1778,7 @@ cc_library(
338         "tools/libclang/*.h",
339     ]),
340     hdrs = glob(["include/clang-c/*.h"]),
341+    copts = llvm_stdcxx_copts,
342     defines = ["CINDEX_NO_EXPORTS"],
343     deps = [
344         ":arc_migrate",
345@@ -1783,7 +1810,7 @@ cc_plugin_library(
346     copts = select({
347         "@bazel_tools//src/conditions:windows": ["-D_CINDEX_LIB_"],
348         "//conditions:default": [],
349-    }),
350+    }) + llvm_stdcxx_copts,
351     strip_include_prefix = "include",
352     deps = [
353         ":arc_migrate",
354@@ -1912,7 +1939,7 @@ cc_library(
355         # Disable stack frame size checks in the driver because
356         # clang::ensureStackAddressSpace allocates a large array on the stack.
357         "$(STACK_FRAME_UNLIMITED)",
358-    ],
359+    ] + llvm_stdcxx_copts,
360     deps = [
361         ":analysis",
362         ":ast",
363@@ -1947,6 +1974,7 @@ cc_library(
364 cc_binary(
365     name = "clang",
366     srcs = [],
367+    copts = llvm_stdcxx_copts,
368     stamp = 0,
369     deps = [
370         ":clang-driver",
371diff --git a/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel b/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
372index 85d79a29b571..716d7f5d9645 100644
373--- a/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
374+++ b/utils/bazel/llvm-project-overlay/llvm/BUILD.bazel
375@@ -4,7 +4,7 @@
376
377 load(":template_rule.bzl", "template_rule")
378 load(":tblgen.bzl", "gentbl")
379-load(":config.bzl", "llvm_config_defines")
380+load(":config.bzl", "llvm_config_defines", "llvm_stdcxx_copts")
381 load(":targets.bzl", "llvm_targets")
382 load(":enum_targets_gen.bzl", "enum_targets_gen")
383 load(":binary_alias.bzl", "binary_alias")
384@@ -21,10 +21,12 @@ exports_files(["LICENSE.TXT"])
385 # toolchain or the `.bazelrc` file. This is just a workaround until we have a
386 # widely available feature to enable unlimited stack frame instead of using
387 # this `Make` variable.
388-llvm_copts = [
389+llvm_c_only_opts = [
390     "$(STACK_FRAME_UNLIMITED)",
391 ]
392
393+llvm_copts = llvm_c_only_opts + llvm_stdcxx_copts
394+
395 enum_targets_gen(
396     name = "targets_def_gen",
397     src = "include/llvm/Config/Targets.def.in",
398@@ -167,10 +169,69 @@ genrule(
399           "echo -e '#undef HANDLE_EXTENSION' >> $@\n",
400 )
401
402+# TODO: This separation is required to separate out C++ and C opts
403+# https://github.com/bazelbuild/bazel/issues/16551
404 cc_library(
405-    name = "Support",
406+    name = "Support-c",
407     srcs = glob([
408         "lib/Support/*.c",
409+    ]),
410+    hdrs = glob([
411+        "include/llvm/Support/**/*.h",
412+        "include/llvm/ADT/*.h",
413+    ]) + [
414+        "include/llvm-c/Core.h",
415+        "include/llvm-c/DataTypes.h",
416+        "include/llvm-c/Deprecated.h",
417+        "include/llvm-c/DisassemblerTypes.h",
418+        "include/llvm-c/Error.h",
419+        "include/llvm-c/ErrorHandling.h",
420+        "include/llvm-c/ExternC.h",
421+        "include/llvm-c/Support.h",
422+        "include/llvm-c/Types.h",
423+        "include/llvm/ExecutionEngine/JITSymbol.h",
424+        "include/llvm/Support/Extension.def",
425+        "include/llvm/Support/VCSRevision.h",
426+    ],
427+    copts = llvm_c_only_opts,
428+    includes = ["include"],
429+    linkopts = select({
430+        "@bazel_tools//src/conditions:windows": [],
431+        "@bazel_tools//src/conditions:freebsd": [
432+            "-pthread",
433+            "-lexecinfo",
434+            "-ldl",
435+            "-lm",
436+        ],
437+        "//conditions:default": [
438+            "-pthread",
439+            "-ldl",
440+            "-lm",
441+        ],
442+    }),
443+    textual_hdrs = glob([
444+        "include/llvm/Support/*.def",
445+        "lib/Support/*.h",
446+        "lib/Support/*.inc",
447+    ]),
448+    deps = [
449+        ":config",
450+        ":Demangle",
451+        # We unconditionally depend on the custom LLVM terminfo wrapper. This
452+        # will be an empty library unless terminfo is enabled, in which case it
453+        # will both provide the necessary dependencies and configuration
454+        # defines.
455+        "@llvm_terminfo//:terminfo",
456+        # We unconditionally depend on the custom LLVM zlib wrapper. This will
457+        # be an empty library unless zlib is enabled, in which case it will
458+        # both provide the necessary dependencies and configuration defines.
459+        "@llvm_zlib//:zlib",
460+    ],
461+)
462+
463+cc_library(
464+    name = "Support",
465+    srcs = glob([
466         "lib/Support/*.cpp",
467         "lib/Support/*.h",
468         "lib/Support/*.inc",
469@@ -223,6 +284,7 @@ cc_library(
470         "include/llvm/Support/*.def",
471     ]),
472     deps = [
473+        ":Support-c",
474         ":config",
475         ":Demangle",
476         # We unconditionally depend on the custom LLVM terminfo wrapper. This
477diff --git a/utils/bazel/llvm-project-overlay/llvm/cc_plugin_library.bzl b/utils/bazel/llvm-project-overlay/llvm/cc_plugin_library.bzl
478index 2ebd39c630dc..e45bd8a1ce92 100644
479--- a/utils/bazel/llvm-project-overlay/llvm/cc_plugin_library.bzl
480+++ b/utils/bazel/llvm-project-overlay/llvm/cc_plugin_library.bzl
481@@ -16,6 +16,7 @@ configure generic aspects of all generated rules such as `testonly`. Lastly,
482 """
483
484 load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_import", "cc_library")
485+load(":config.bzl", "llvm_stdcxx_copts")
486
487 def cc_plugin_library(name, srcs, hdrs, include_prefix = None, strip_include_prefix = None, alwayslink = False, features = [], tags = [], testonly = False, **kwargs):
488     # Neither the name of the plugin binary nor tags on whether it is built are
489@@ -29,6 +30,7 @@ def cc_plugin_library(name, srcs, hdrs, include_prefix = None, strip_include_pre
490     dylib_name = name + ".dylib"
491     interface_output_name = name + "_interface_output"
492     import_name = name + "_import"
493+    copts = kwargs.pop("copts", [])
494     for impl_name in [dll_name, dylib_name, so_name]:
495         cc_binary(
496             name = impl_name,
497@@ -38,6 +40,7 @@ def cc_plugin_library(name, srcs, hdrs, include_prefix = None, strip_include_pre
498             features = features,
499             tags = ["manual"] + tags,
500             testonly = testonly,
501+            copts = copts + llvm_stdcxx_copts,
502             **kwargs
503         )
504     native.filegroup(
505diff --git a/utils/bazel/llvm-project-overlay/llvm/config.bzl b/utils/bazel/llvm-project-overlay/llvm/config.bzl
506index 2046b2645362..736844bfc2d0 100644
507--- a/utils/bazel/llvm-project-overlay/llvm/config.bzl
508+++ b/utils/bazel/llvm-project-overlay/llvm/config.bzl
509@@ -97,3 +97,8 @@ llvm_config_defines = os_defines + select({
510     "__STDC_CONSTANT_MACROS",
511     "__STDC_FORMAT_MACROS",
512 ]
513+
514+llvm_stdcxx_copts = select({
515+    "@platforms//os:windows": ["/std:c++17"],
516+    "//conditions:default": ["-std=c++17"],
517+})
518diff --git a/utils/bazel/llvm-project-overlay/llvm/tblgen.bzl b/utils/bazel/llvm-project-overlay/llvm/tblgen.bzl
519index d43390918e39..e0482a6fd4fa 100644
520--- a/utils/bazel/llvm-project-overlay/llvm/tblgen.bzl
521+++ b/utils/bazel/llvm-project-overlay/llvm/tblgen.bzl
522@@ -12,6 +12,8 @@ TODO(chandlerc): Currently this expresses include-based dependencies as
523 correctly understood by the build system.
524 """
525
526+load(":config.bzl", "llvm_stdcxx_copts")
527+
528 def gentbl(
529         name,
530         tblgen,
531@@ -77,5 +79,6 @@ def gentbl(
532             # distinction between these two.
533             hdrs = [f for (_, f) in tbl_outs],
534             features = ["-parse_headers", "-header_modules"],
535+            copts = llvm_stdcxx_copts,
536             **kwargs
537         )
538