Skip to content

Commit

Permalink
Merge pull request #337 from jatinchowdhury18/cpp-version-macros-msvc
Browse files Browse the repository at this point in the history
Fix C++ version checking on MSVC
  • Loading branch information
abique committed Jul 13, 2023
2 parents d2cf8b1 + e661d1c commit 9d53955
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,17 @@ jobs:

- name: Run CMake+Ninja+CTest to generate/build/test.
uses: lukka/run-cmake@v10
id: runcmake
id: runcmake-ninja
with:
configurePreset: 'ninja'
buildPreset: 'ninja-release'
testPreset: 'ninja-release'

- name: Run CMake+MSVC+CTest to generate/build/test.
uses: lukka/run-cmake@v10
if: startsWith(matrix.os, 'win')
id: runcmake-msvc
with:
configurePreset: 'msvc'
buildPreset: 'msvc-release'
testPreset: 'msvc-release'
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ if (${CLAP_BUILD_TESTS})
add_test(NAME test-clap-compile-${SUFFIX} COMMAND clap-compile-${SUFFIX})
add_dependencies(clap-tests clap-compile-${SUFFIX})

if (${EXT} STREQUAL "cc")
target_compile_definitions(clap-compile-${SUFFIX} PRIVATE CLAP_COMPILE_TEST_CXX_VERSION=${STDCPP})
endif()

if (${CMAKE_C_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_C_COMPILER_ID} STREQUAL "Clang")
target_compile_options(clap-compile-${SUFFIX} PRIVATE -Wall -Wextra -pedantic)
endif()
Expand Down
30 changes: 30 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@
"value": true
}
}
},
{
"name": "msvc",
"displayName": "MSVC",
"description": "Configure and generate MSVC project files for all configurations",
"binaryDir": "${sourceDir}/builds/${presetName}",
"generator": "Visual Studio 17 2022",
"cacheVariables": {
"CMAKE_EXPORT_COMPILE_COMMANDS": {
"type": "boolean",
"value": true
},
"CLAP_BUILD_TESTS": {
"type": "boolean",
"value": true
}
}
}
],
"buildPresets": [
Expand All @@ -32,13 +49,26 @@
"description": "Build ninja Release configuration",
"configuration": "RelWithDebInfo",
"targets": ["clap-tests"]
},
{
"name": "msvc-release",
"configurePreset": "msvc",
"displayName": "Build msvc-release",
"description": "Build msvc Release configuration",
"configuration": "RelWithDebInfo",
"targets": ["clap-tests"]
}
],
"testPresets": [
{
"name": "ninja-release",
"configurePreset": "ninja",
"configuration": "RelWithDebInfo"
},
{
"name": "msvc-release",
"configurePreset": "msvc",
"configuration": "RelWithDebInfo"
}
]
}
12 changes: 9 additions & 3 deletions include/clap/private/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,26 @@
# endif
#endif

#if defined(__cplusplus) && __cplusplus >= 201103L
#if defined(_MSVC_LANG)
# define CLAP_CPLUSPLUS _MSVC_LANG
#elif defined(__cplusplus)
# define CLAP_CPLUSPLUS __cplusplus
#endif

#if defined(CLAP_CPLUSPLUS) && CLAP_CPLUSPLUS >= 201103L
# define CLAP_HAS_CXX11
# define CLAP_CONSTEXPR constexpr
#else
# define CLAP_CONSTEXPR
#endif

#if defined(__cplusplus) && __cplusplus >= 201703L
#if defined(CLAP_CPLUSPLUS) && CLAP_CPLUSPLUS >= 201703L
# define CLAP_HAS_CXX17
# define CLAP_NODISCARD [[nodiscard]]
#else
# define CLAP_NODISCARD
#endif

#if defined(__cplusplus) && __cplusplus >= 202002L
#if defined(CLAP_CPLUSPLUS) && CLAP_CPLUSPLUS >= 202002L
# define CLAP_HAS_CXX20
#endif
12 changes: 12 additions & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@
#error CLAP_VERSION_LT is inconsistent (REVISION)
#endif

#if (CLAP_COMPILE_TEST_CXX_VERSION >= 11) && ! defined(CLAP_HAS_CXX11)
#error CLAP_HAS_CXX11 is not defined correctly
#endif

#if (CLAP_COMPILE_TEST_CXX_VERSION >= 17) && ! defined(CLAP_HAS_CXX17)
#error CLAP_HAS_CXX17 is not defined correctly
#endif

#if (CLAP_COMPILE_TEST_CXX_VERSION >= 20) && ! defined(CLAP_HAS_CXX20)
#error CLAP_HAS_CXX20 is not defined correctly
#endif

static const CLAP_CONSTEXPR clap_version m = CLAP_VERSION;

int main(int, char **) {
Expand Down

0 comments on commit 9d53955

Please sign in to comment.