Skip to content

Commit 28d79ee

Browse files
committed
Swift: make compilation with different STL versions possible
Previous to this patch the code contained a workaround for the standard defect https://cplusplus.github.io/LWG/issue3657 where `std::filesystem::path` did not have a `std::hash` implementation. This patch allows compiling against versions of the STL that contain the fix to the above issue. This is done by running the compiler against code defining `std::hash<std::filesystem::path>`: if compilation succeeds, it means the fix is not there and we need to use the workaround, contained in `PathHash.h.workaround`. Otherwise, the fix is there and we use `PathHash.h.fixed` instead, which only includes the standard headers included by `PathHash.h.workaround`, so that one is a drop-in replacement of the other.
1 parent 16cd148 commit 28d79ee

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

swift/extractor/infra/file/BUILD.bazel

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,26 @@ load("//swift:rules.bzl", "swift_cc_library")
33
swift_cc_library(
44
name = "file",
55
srcs = glob(["*.cpp"]),
6-
hdrs = glob(["*.h"]),
6+
hdrs = glob(["*.h"]) + [":path_hash_workaround"],
77
visibility = ["//swift:__subpackages__"],
88
deps = ["@picosha2"],
99
)
10+
11+
genrule(
12+
name = "path_hash_workaround",
13+
srcs = [
14+
"PathHash.h.workaround",
15+
"PathHash.h.fixed",
16+
],
17+
outs = ["PathHash.h"],
18+
# see if https://cplusplus.github.io/LWG/issue3657 is fixed with the current compiler or not
19+
# if fixed, PathHash.h.workaround will not compile
20+
cmd = "\n".join([
21+
"if $(CC) -c -x c++ -std=c++17 $(rootpath PathHash.h.workaround) -o /dev/null &> /dev/null; then",
22+
" cp $(rootpath PathHash.h.workaround) $@",
23+
"else",
24+
" cp $(rootpath PathHash.h.fixed) $@",
25+
"fi",
26+
]),
27+
toolchains = ["@bazel_tools//tools/cpp:current_cc_host_toolchain"],
28+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
3+
#include <filesystem>
4+
#include <functional>

swift/extractor/infra/file/PathHash.h renamed to swift/extractor/infra/file/PathHash.h.workaround

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// but it works, and this is recognized as a defect of the standard.
99
// Using a non-standard Hasher type would be a huge pain, as the automatic hash implementation of
1010
// std::variant would not kick in (we use std::filesystem::path in a variant used as a map key).
11-
// We can reconsider when the fix to the above issue hits clang, which might require a version check
12-
// here
1311
namespace std {
1412
template <>
1513
struct std::hash<std::filesystem::path> {

0 commit comments

Comments
 (0)