Skip to content

Commit 0bd09ec

Browse files
committed
fix build error on macOS 10.14 w/ Xcode 10
<filesystem> is not available in macOS 10.14's SDK. This change causes the "tests" app to use chdir(), if <filesystem> cannot be found.
1 parent 0b1eae2 commit 0bd09ec

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

P0267_RefImpl/Tests/main.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11

2+
#include <cerrno>
3+
#include <cstring>
24
#include <exception>
3-
#include <filesystem>
45
#include <iostream>
56
#include <system_error>
67

8+
#if __has_include(<filesystem>)
9+
#include <filesystem>
10+
#elif __has_include(<unistd.h>)
11+
#include <unistd.h>
12+
#endif
13+
714
#define CATCH_CONFIG_RUNNER
815
#include "catch.hpp"
916

@@ -19,6 +26,7 @@ int main(int argc, char* argv[])
1926
// For now, set the app's CWD (Current Working Directory) to where the
2027
// app's executable is, presuming this info is available.
2128
if (argc >= 1 && argv[0] != nullptr) {
29+
#if __has_include(<filesystem>)
2230
try {
2331
using namespace std::filesystem;
2432
current_path(path(argv[0]).parent_path());
@@ -29,6 +37,20 @@ int main(int argc, char* argv[])
2937
"\"\n";
3038
return 1; // Return a non-zero integer to indicate failure
3139
}
40+
#elif __has_include(<unistd.h>)
41+
std::string p = argv[0];
42+
const auto last_path_sep = p.find_last_of('/');
43+
if (last_path_sep != std::string::npos && last_path_sep > 0) {
44+
p = p.substr(0, last_path_sep);
45+
if (chdir(p.c_str()) != 0) {
46+
std::cerr <<
47+
"ERROR: Unable to set CWD via chdir(). strerror(errno)=\"" <<
48+
strerror(errno) <<
49+
"\"\n";
50+
return 1; // Return a non-zero integer to indicate failure
51+
}
52+
}
53+
#endif
3254
}
3355

3456
return Catch::Session().run(argc, argv);

0 commit comments

Comments
 (0)