Skip to content

Commit 0e74974

Browse files
committed
Add the USE_CPP23 option to CMake
1 parent 5eb211b commit 0e74974

File tree

4 files changed

+90
-9
lines changed

4 files changed

+90
-9
lines changed

CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ endif()
858858
################################################################################
859859
macro(AddApplicationInternal Target Executable)
860860
add_executable(${Target} ${Sources})
861-
target_link_libraries(${Target} ${A_Target}-objects)
861+
target_link_libraries(${Target} ${A_Target}-objects ${CPP23SupportLibrary})
862862

863863
if (DEPS_DIR)
864864
add_dependencies(${Target} runtime_deps)
@@ -883,15 +883,16 @@ endmacro()
883883

884884
function(AddApplication)
885885
set(oneValueArgs Target ExecutableName)
886-
set(multiValueArgs ApplicationMain Definitions Flags Files Libs Tests)
886+
set(multiValueArgs ApplicationMain Definitions Flags CompileFeatures Files Libs Tests)
887887
cmake_parse_arguments(A "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
888888

889889
# Reuse object files between the real application and the test one
890890
add_library(${A_Target}-objects OBJECT EXCLUDE_FROM_ALL ${A_Files} ${PCH_FILE})
891-
target_link_libraries(${A_Target}-objects engine-lib ${A_Libs} ${LIBS_BASE})
891+
target_link_libraries(${A_Target}-objects engine-lib ${A_Libs} ${LIBS_BASE} ${CPP23SupportLibrary})
892892
set_property(TARGET ${A_Target}-objects APPEND PROPERTY COMPILE_OPTIONS ${A_Flags})
893893
set_property(TARGET ${A_Target}-objects APPEND PROPERTY INCLUDE_DIRECTORIES ${ENGINE_DIR} ${MOUNT_DIR} ${LIB_DIR})
894894
set_property(TARGET ${A_Target}-objects APPEND PROPERTY COMPILE_DEFINITIONS ${A_Definitions})
895+
895896
set_target_properties(${A_Target}-objects PROPERTIES FOLDER "engine/objects")
896897

897898
set(Sources WIN32 ${A_ApplicationMain})
@@ -905,7 +906,7 @@ function(AddApplication)
905906
# -Wl,--whole-archive -l<library path> -Wl,--no-whole-archive
906907
set(Sources ${A_ApplicationMain} ${A_Tests})
907908
AddApplicationInternal(test-${A_Target} test-${A_Target})
908-
target_link_libraries(test-${A_Target} GTest::gmock)
909+
target_link_libraries(test-${A_Target} ${CPP23SupportLibrary} GTest::gmock)
909910
endif()
910911

911912
ADD_PRECOMPILED_HEADER(${A_Target}-objects)
@@ -915,7 +916,7 @@ daemon_write_buildinfo("Engine")
915916

916917
if (NOT NACL)
917918
add_library(engine-lib EXCLUDE_FROM_ALL ${PCH_FILE} ${BUILDINFOLIST} ${COMMONLIST} ${ENGINELIST})
918-
target_link_libraries(engine-lib ${LIBS_BASE} ${LIBS_ENGINE_BASE})
919+
target_link_libraries(engine-lib ${LIBS_BASE} ${LIBS_ENGINE_BASE} ${CPP23SupportLibrary})
919920
set_property(TARGET engine-lib APPEND PROPERTY COMPILE_DEFINITIONS BUILD_ENGINE)
920921
set_property(TARGET engine-lib APPEND PROPERTY INCLUDE_DIRECTORIES ${ENGINE_DIR} ${MOUNT_DIR} ${LIB_DIR})
921922
set_property(TARGET engine-lib APPEND PROPERTY COMPILE_OPTIONS ${WARNINGS})

cmake/DaemonFlags.cmake

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,70 @@ endif()
5353
option(USE_RECOMMENDED_CXX_STANDARD "Use recommended C++ standard" ON)
5454
mark_as_advanced(USE_RECOMMENDED_CXX_STANDARD)
5555

56+
option(USE_CPP23 "Use C++23 standard where possible" OFF)
57+
58+
# Required for <stacktrace> on Clang/GCC
59+
if(USE_CPP23)
60+
if (DAEMON_CXX_COMPILER_Clang_COMPATIBILITY OR DAEMON_CXX_COMPILER_GCC_COMPATIBILITY)
61+
if ((DAEMON_CXX_COMPILER_Clang_VERSION VERSION_GREATER_EQUAL 19.1.0) OR (DAEMON_CXX_COMPILER_GCC_VERSION VERSION_GREATER_EQUAL 13.3))
62+
set(CPP23SupportLibraryTryExp TRUE)
63+
endif()
64+
65+
if ((DAEMON_CXX_COMPILER_Clang_VERSION VERSION_GREATER_EQUAL 17.0.1) OR (DAEMON_CXX_COMPILER_GCC_VERSION VERSION_GREATER_EQUAL 12.1))
66+
set(CPP23SupportLibraryTryBacktrace TRUE)
67+
endif()
68+
69+
if (CPP23SupportLibraryTryExp)
70+
set(CPP23SupportLibrary "-lstdc++exp")
71+
set(CPP23SupportLibraryCompatibleCompiler TRUE)
72+
73+
find_library(HAVE_CPP23SupportLibrary "libstdc++exp")
74+
endif()
75+
76+
if (CPP23SupportLibraryTryBacktrace AND (HAVE_CPP23SupportLibrary-NOTFOUND OR NOT CPP23SupportLibraryTryExp))
77+
if (CPP23SupportLibraryCompatibleCompiler)
78+
set(CPP23SupportLibraryOldLibrary TRUE)
79+
endif()
80+
81+
set(CPP23SupportLibrary "-lstdc++_libbacktrace")
82+
set(CPP23SupportLibraryCompatibleCompiler TRUE)
83+
84+
find_library(HAVE_CPP23SupportLibrary "libstdc++_libbacktrace")
85+
endif()
86+
87+
if (HAVE_CPP23SupportLibrary-NOTFOUND)
88+
if (NOT CPP23SupportLibraryCompatibleCompiler)
89+
message(WARNING "Not using <stacktrace>: the compiler is too old (requires clang >= 17.0.1 or GCC >= 12.1)")
90+
else()
91+
message(WARNING "Not using <stacktrace>: libstdc++exp or libstdc++_backtrace is required, but wasn't found in system paths")
92+
endif()
93+
94+
set(CPP23SupportLibrary "")
95+
elseif (CXX_FLAGS MATCHES ".*\\-stdlib\\=libc\\+\\+*")
96+
message(WARNING "Not using <stacktrace>: only -stdlib=libstdc++ is supported")
97+
98+
set(CPP23SupportLibrary "")
99+
else()
100+
add_definitions(-DDAEMON_CPP23_SUPPORT_LIBRARY_ENABLED=1)
101+
# FIXME: Doesn't work?
102+
add_compile_options("-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/src=.")
103+
104+
if (CPP23SupportLibraryOldLibrary)
105+
message(STATUS "Using <stacktrace>: found ${CPP23SupportLibrary} (recommended to use libc++exp on this compiler version instead, but it wasn't found)")
106+
else()
107+
message(STATUS "Using <stacktrace>: found ${CPP23SupportLibrary}")
108+
endif()
109+
endif()
110+
elseif (MSVC)
111+
# FIXME: Doesn't work in sgame/cgame?
112+
string(REPLACE "/" "\\" backslashed_dir ${CMAKE_CURRENT_SOURCE_DIR}/src)
113+
add_compile_options("/d1trimfile:${backslashed_dir}")
114+
115+
string(REPLACE "/" "\\" backslashed_dir ${CMAKE_CURRENT_SOURCE_DIR}/daemon/src)
116+
add_compile_options("/d1trimfile:${backslashed_dir}")
117+
endif()
118+
endif()
119+
56120
# Set flag without checking, optional argument specifies build type
57121
macro(set_c_flag FLAG)
58122
if (${ARGC} GREATER 1)
@@ -255,7 +319,19 @@ else()
255319
endif()
256320
endif()
257321

258-
if (USE_RECOMMENDED_CXX_STANDARD)
322+
if (USE_CPP23)
323+
if (MSVC)
324+
add_compile_options("/std:c++23preview")
325+
else()
326+
try_cxx_flag(GNUXX23 "-std=gnu++23")
327+
328+
if (NOT FLAG_GNUXX23)
329+
message(WARNING "Requested C++23 is not supported, falling back to C++14")
330+
endif()
331+
endif()
332+
endif()
333+
334+
if (NOT USE_CPP23 AND (NOT FLAG_GNUXX23 OR USE_RECOMMENDED_CXX_STANDARD))
259335
# PNaCl only defines isascii if __STRICT_ANSI__ is not defined,
260336
# always prefer GNU dialect.
261337
try_cxx_flag(GNUXX14 "-std=gnu++14")

cmake/DaemonGame.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ function(buildGameModule module_slug)
135135
set_target_properties(${module_target} PROPERTIES
136136
OUTPUT_NAME "${GAMEMODULE_NAME}"
137137
SUFFIX "${PLATFORM_EXE_SUFFIX}")
138-
endif()
139138

140-
target_link_libraries(${module_target} ${GAMEMODULE_LIBS} ${LIBS_BASE})
139+
target_link_libraries(${module_target} ${GAMEMODULE_LIBS} ${LIBS_BASE})
140+
else()
141+
target_link_libraries(${module_target} ${GAMEMODULE_LIBS} ${LIBS_BASE} ${CPP23SupportLibrary})
142+
endif()
141143

142144
ADD_PRECOMPILED_HEADER(${module_target})
143145
endfunction()

src/common/CPPStandard.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8383
#define CPP_SOURCE_LOCATION
8484
#endif
8585

86-
#if __cpp_lib_stacktrace >= 202011L
86+
/* Clang/GCC support <stacktrace> with -lstdc++exp/-lstdlibc++_libbacktrace, but they don't define the feature macro,
87+
so we set a custom macro in build system */
88+
#if __cpp_lib_stacktrace >= 202011L || defined(DAEMON_CPP23_SUPPORT_LIBRARY_ENABLED)
8789
#define CPP_STACKTRACE
8890
#endif
8991

0 commit comments

Comments
 (0)