Skip to content

Commit f759e0e

Browse files
committed
Remove std::filesystem fallbacks from early versions of C++17
1 parent 6c99aef commit f759e0e

File tree

5 files changed

+60
-127
lines changed

5 files changed

+60
-127
lines changed

cmake/test_filesystem.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This is a dummy program that just needs to compile and link to tell us if
2+
// the C++17 std::filesystem API requires any additional libraries.
3+
4+
#include <filesystem>
5+
6+
int main()
7+
{
8+
try
9+
{
10+
throw std::filesystem::filesystem_error("instantiate one to make sure it links",
11+
std::make_error_code(std::errc::function_not_supported));
12+
}
13+
catch (const std::filesystem::filesystem_error& error)
14+
{
15+
return -1;
16+
}
17+
18+
return !std::filesystem::temp_directory_path().is_absolute();
19+
}

cmake/test_filesystem.cpp.in

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/CMakeLists.txt

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,12 @@ endif()
245245

246246
# Common schemagen and clientgen filesystem and Boost dependencies
247247
if(GRAPHQL_BUILD_SCHEMAGEN OR GRAPHQL_BUILD_CLIENTGEN)
248-
set(BOOST_COMPONENTS program_options)
249-
set(BOOST_LIBRARIES Boost::program_options)
250-
251248
# Try compiling a test program with std::filesystem or one of its alternatives.
252-
function(check_filesystem_impl FILESYSTEM_HEADER FILESYSTEM_NAMESPACE OPTIONAL_LIBS OUT_RESULT)
253-
set(TEST_FILE "test_${OUT_RESULT}.cpp")
254-
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/test_filesystem.cpp.in ${TEST_FILE} @ONLY)
255-
249+
function(check_filesystem_impl OPTIONAL_LIBS)
256250
try_compile(TEST_RESULT
257251
${CMAKE_CURRENT_BINARY_DIR}
258-
${CMAKE_CURRENT_BINARY_DIR}/${TEST_FILE}
259-
CXX_STANDARD 17)
252+
${CMAKE_CURRENT_SOURCE_DIR}/../cmake/test_filesystem.cpp
253+
CXX_STANDARD 20)
260254

261255
if(NOT TEST_RESULT)
262256
# Retry with each of the optional libraries.
@@ -265,7 +259,7 @@ if(GRAPHQL_BUILD_SCHEMAGEN OR GRAPHQL_BUILD_CLIENTGEN)
265259
${CMAKE_CURRENT_BINARY_DIR}
266260
${CMAKE_CURRENT_BINARY_DIR}/${TEST_FILE}
267261
LINK_LIBRARIES ${OPTIONAL_LIB}
268-
CXX_STANDARD 17)
262+
CXX_STANDARD 20)
269263

270264
if(TEST_RESULT)
271265
# Looks like the optional library was required, go ahead and add it to the link options.
@@ -279,49 +273,19 @@ if(GRAPHQL_BUILD_SCHEMAGEN OR GRAPHQL_BUILD_CLIENTGEN)
279273
endif()
280274
endforeach(OPTIONAL_LIB)
281275
endif()
282-
283-
set(${OUT_RESULT} ${TEST_RESULT} PARENT_SCOPE)
284276
endfunction(check_filesystem_impl)
285277

286-
# Try compiling a minimal program with each header/namespace, in order of preference:
287-
# C++17: #include <filesystem> // std::filesystem
288-
# Experimental C++17: #include <experimental/filesystem> // std::experimental::filesystem
289-
# Boost.Filesystem: #include <boost/filesystem.hpp> // boost::filesystem
290-
check_filesystem_impl("filesystem" "std::filesystem" "stdc++fs;c++fs" STD_FILESYTEM)
291-
if(STD_FILESYTEM)
292-
if(GRAPHQL_BUILD_SCHEMAGEN)
293-
target_compile_definitions(schemagen PRIVATE USE_STD_FILESYSTEM)
294-
endif()
295-
if(GRAPHQL_BUILD_CLIENTGEN)
296-
target_compile_definitions(clientgen PRIVATE USE_STD_FILESYSTEM)
297-
endif()
298-
else()
299-
check_filesystem_impl("experimental/filesystem" "std::experimental::filesystem" "stdc++fs;c++fs" STD_EXPERIMENTAL_FILESYTEM)
300-
if(STD_EXPERIMENTAL_FILESYTEM)
301-
if(GRAPHQL_BUILD_SCHEMAGEN)
302-
target_compile_definitions(schemagen PRIVATE USE_STD_EXPERIMENTAL_FILESYSTEM)
303-
endif()
304-
if(GRAPHQL_BUILD_CLIENTGEN)
305-
target_compile_definitions(clientgen PRIVATE USE_STD_EXPERIMENTAL_FILESYSTEM)
306-
endif()
307-
else()
308-
set(BOOST_COMPONENTS ${BOOST_COMPONENTS} filesystem)
309-
set(BOOST_LIBRARIES ${BOOST_LIBRARIES} Boost::filesystem)
310-
if(GRAPHQL_BUILD_SCHEMAGEN)
311-
target_compile_definitions(schemagen PRIVATE USE_BOOST_FILESYSTEM)
312-
endif()
313-
if(GRAPHQL_BUILD_CLIENTGEN)
314-
target_compile_definitions(clientgen PRIVATE USE_BOOST_FILESYSTEM)
315-
endif()
316-
endif()
317-
endif()
278+
# Try compiling a minimal program without any extra libraries, then with each optional library until it succeeded:
279+
# stdc++fs
280+
# c++fs
281+
check_filesystem_impl("stdc++fs;c++fs" STD_FILESYTEM)
318282

319-
find_package(Boost REQUIRED COMPONENTS ${BOOST_COMPONENTS})
283+
find_package(Boost REQUIRED COMPONENTS program_options)
320284
if(GRAPHQL_BUILD_SCHEMAGEN)
321-
target_link_libraries(schemagen PRIVATE ${BOOST_LIBRARIES})
285+
target_link_libraries(schemagen PRIVATE Boost::program_options)
322286
endif()
323287
if(GRAPHQL_BUILD_CLIENTGEN)
324-
target_link_libraries(clientgen PRIVATE ${BOOST_LIBRARIES})
288+
target_link_libraries(clientgen PRIVATE Boost::program_options)
325289
endif()
326290
endif()
327291

src/ClientGenerator.cpp

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,8 @@
2020
#pragma warning(pop)
2121
#endif // _MSC_VER
2222

23-
// clang-format off
24-
#ifdef USE_STD_FILESYSTEM
25-
#include <filesystem>
26-
namespace fs = std::filesystem;
27-
#else
28-
#ifdef USE_STD_EXPERIMENTAL_FILESYSTEM
29-
#include <experimental/filesystem>
30-
namespace fs = std::experimental::filesystem;
31-
#else
32-
#ifdef USE_BOOST_FILESYSTEM
33-
#include <boost/filesystem.hpp>
34-
namespace fs = boost::filesystem;
35-
#else
36-
#error "No std::filesystem implementation defined"
37-
#endif
38-
#endif
39-
#endif
40-
// clang-format on
41-
4223
#include <cctype>
24+
#include <filesystem>
4325
#include <fstream>
4426
#include <iostream>
4527
#include <regex>
@@ -66,7 +48,7 @@ std::string Generator::getHeaderDir() const noexcept
6648
{
6749
if (!_options.paths.headerPath.empty())
6850
{
69-
return fs::path { _options.paths.headerPath }.string();
51+
return std::filesystem::path { _options.paths.headerPath }.string();
7052
}
7153
else
7254
{
@@ -78,7 +60,7 @@ std::string Generator::getSourceDir() const noexcept
7860
{
7961
if (!_options.paths.sourcePath.empty())
8062
{
81-
return fs::path(_options.paths.sourcePath).string();
63+
return std::filesystem::path(_options.paths.sourcePath).string();
8264
}
8365
else
8466
{
@@ -88,7 +70,7 @@ std::string Generator::getSourceDir() const noexcept
8870

8971
std::string Generator::getHeaderPath() const noexcept
9072
{
91-
fs::path fullPath { _headerDir };
73+
std::filesystem::path fullPath { _headerDir };
9274

9375
fullPath /= (std::string { _schemaLoader.getFilenamePrefix() } + "Client.h");
9476

@@ -97,7 +79,7 @@ std::string Generator::getHeaderPath() const noexcept
9779

9880
std::string Generator::getSourcePath() const noexcept
9981
{
100-
fs::path fullPath { _sourceDir };
82+
std::filesystem::path fullPath { _sourceDir };
10183

10284
fullPath /= (std::string { _schemaLoader.getFilenamePrefix() } + "Client.cpp");
10385

@@ -188,7 +170,8 @@ std::vector<std::string> Generator::Build() const noexcept
188170
bool Generator::outputHeader() const noexcept
189171
{
190172
std::ofstream headerFile(_headerPath, std::ios_base::trunc);
191-
IncludeGuardScope includeGuard { headerFile, fs::path(_headerPath).filename().string() };
173+
IncludeGuardScope includeGuard { headerFile,
174+
std::filesystem::path(_headerPath).filename().string() };
192175

193176
headerFile << R"cpp(#include "graphqlservice/GraphQLClient.h"
194177
#include "graphqlservice/GraphQLParse.h"
@@ -223,7 +206,8 @@ static_assert(graphql::internal::MinorVersion == )cpp"
223206
{
224207
pendingSeparator.reset();
225208

226-
headerFile << R"cpp(enum class )cpp" << _schemaLoader.getCppType(enumType->name()) << R"cpp(
209+
headerFile << R"cpp(enum class )cpp" << _schemaLoader.getCppType(enumType->name())
210+
<< R"cpp(
227211
{
228212
)cpp";
229213
for (const auto& enumValue : enumType->enumValues())

src/SchemaGenerator.cpp

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,8 @@
1616
#pragma warning(pop)
1717
#endif // _MSC_VER
1818

19-
// clang-format off
20-
#ifdef USE_STD_FILESYSTEM
21-
#include <filesystem>
22-
namespace fs = std::filesystem;
23-
#else
24-
#ifdef USE_STD_EXPERIMENTAL_FILESYSTEM
25-
#include <experimental/filesystem>
26-
namespace fs = std::experimental::filesystem;
27-
#else
28-
#ifdef USE_BOOST_FILESYSTEM
29-
#include <boost/filesystem.hpp>
30-
namespace fs = boost::filesystem;
31-
#else
32-
#error "No std::filesystem implementation defined"
33-
#endif
34-
#endif
35-
#endif
36-
// clang-format on
37-
3819
#include <cctype>
20+
#include <filesystem>
3921
#include <fstream>
4022
#include <iostream>
4123
#include <regex>
@@ -61,7 +43,7 @@ std::string Generator::getHeaderDir() const noexcept
6143
{
6244
if (!_options.paths.headerPath.empty())
6345
{
64-
return fs::path { _options.paths.headerPath }.string();
46+
return std::filesystem::path { _options.paths.headerPath }.string();
6547
}
6648
else
6749
{
@@ -73,7 +55,7 @@ std::string Generator::getSourceDir() const noexcept
7355
{
7456
if (!_options.paths.sourcePath.empty())
7557
{
76-
return fs::path(_options.paths.sourcePath).string();
58+
return std::filesystem::path(_options.paths.sourcePath).string();
7759
}
7860
else
7961
{
@@ -83,15 +65,15 @@ std::string Generator::getSourceDir() const noexcept
8365

8466
std::string Generator::getHeaderPath() const noexcept
8567
{
86-
fs::path fullPath { _headerDir };
68+
std::filesystem::path fullPath { _headerDir };
8769

8870
fullPath /= (std::string { _loader.getFilenamePrefix() } + "Schema.h");
8971
return fullPath.string();
9072
}
9173

9274
std::string Generator::getSourcePath() const noexcept
9375
{
94-
fs::path fullPath { _sourceDir };
76+
std::filesystem::path fullPath { _sourceDir };
9577

9678
fullPath /= (std::string { _loader.getFilenamePrefix() } + "Schema.cpp");
9779
return fullPath.string();
@@ -124,7 +106,8 @@ std::vector<std::string> Generator::Build() const noexcept
124106
bool Generator::outputHeader() const noexcept
125107
{
126108
std::ofstream headerFile(_headerPath, std::ios_base::trunc);
127-
IncludeGuardScope includeGuard { headerFile, fs::path(_headerPath).filename().string() };
109+
IncludeGuardScope includeGuard { headerFile,
110+
std::filesystem::path(_headerPath).filename().string() };
128111

129112
headerFile << R"cpp(#include "graphqlservice/internal/Schema.h"
130113
@@ -977,7 +960,8 @@ void Generator::outputObjectDeclaration(
977960

978961
for (auto unionName : objectType.unions)
979962
{
980-
headerFile << R"cpp( friend )cpp" << _loader.getSafeCppName(unionName) << R"cpp(;
963+
headerFile << R"cpp( friend )cpp" << _loader.getSafeCppName(unionName)
964+
<< R"cpp(;
981965
)cpp";
982966
}
983967

@@ -1758,7 +1742,8 @@ Operations::Operations()cpp";
17581742
)cpp";
17591743
}
17601744
sourceFile << R"cpp(}, )cpp"
1761-
<< (directive.isRepeatable ? R"cpp(true)cpp" : R"cpp(false)cpp") << R"cpp());
1745+
<< (directive.isRepeatable ? R"cpp(true)cpp" : R"cpp(false)cpp")
1746+
<< R"cpp());
17621747
)cpp";
17631748
}
17641749
}
@@ -2655,8 +2640,8 @@ std::string Generator::getIntrospectionType(
26552640

26562641
std::vector<std::string> Generator::outputSeparateFiles() const noexcept
26572642
{
2658-
const fs::path headerDir(_headerDir);
2659-
const fs::path sourceDir(_sourceDir);
2643+
const std::filesystem::path headerDir(_headerDir);
2644+
const std::filesystem::path sourceDir(_sourceDir);
26602645
std::vector<std::string> files;
26612646
std::string_view queryType;
26622647

@@ -2676,7 +2661,8 @@ std::vector<std::string> Generator::outputSeparateFiles() const noexcept
26762661
std::ofstream headerFile(headerPath, std::ios_base::trunc);
26772662
IncludeGuardScope includeGuard { headerFile, headerFilename };
26782663

2679-
headerFile << R"cpp(#include ")cpp" << fs::path(_headerPath).filename().string() << R"cpp("
2664+
headerFile << R"cpp(#include ")cpp"
2665+
<< std::filesystem::path(_headerPath).filename().string() << R"cpp("
26802666
26812667
)cpp";
26822668

@@ -2766,7 +2752,8 @@ using namespace std::literals;
27662752
std::ofstream headerFile(headerPath, std::ios_base::trunc);
27672753
IncludeGuardScope includeGuard { headerFile, headerFilename };
27682754

2769-
headerFile << R"cpp(#include ")cpp" << fs::path(_headerPath).filename().string() << R"cpp("
2755+
headerFile << R"cpp(#include ")cpp"
2756+
<< std::filesystem::path(_headerPath).filename().string() << R"cpp("
27702757
27712758
)cpp";
27722759

@@ -2856,7 +2843,8 @@ using namespace std::literals;
28562843
std::ofstream headerFile(headerPath, std::ios_base::trunc);
28572844
IncludeGuardScope includeGuard { headerFile, headerFilename };
28582845

2859-
headerFile << R"cpp(#include ")cpp" << fs::path(_headerPath).filename().string() << R"cpp("
2846+
headerFile << R"cpp(#include ")cpp"
2847+
<< std::filesystem::path(_headerPath).filename().string() << R"cpp("
28602848
28612849
)cpp";
28622850

@@ -3053,8 +3041,8 @@ int main(int argc, char** argv)
30533041
po::value(&headerDir),
30543042
"Target path for the <prefix>Schema.h header file")("stubs",
30553043
po::bool_switch(&stubs),
3056-
"Unimplemented fields throw runtime exceptions instead of compiler errors")(
3057-
"no-introspection",
3044+
"Unimplemented fields throw runtime exceptions instead of compiler errors")("no-"
3045+
"introspection",
30583046
po::bool_switch(&noIntrospection),
30593047
"Do not generate support for Introspection");
30603048
positional.add("schema", 1).add("prefix", 1).add("namespace", 1);

0 commit comments

Comments
 (0)