Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ environment:
matrix:
# see 96d5c1f3ed77b09c64ce7c3c7cbd37c70456b3db
# for NMake template
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
generator: Visual Studio 16 2019
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2022
generator: Visual Studio 17 2022
platform: x64
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
generator: Visual Studio 16 2019
platform: win32

build:
parallel: true
Expand All @@ -51,14 +48,15 @@ build_script:
:: /Wv pins warnings to a specific compiler version so that new ones
:: don't make the build error after Appveyor updates the compiler.

set CFLAGS=/Wv:19.29.30037
set CFLAGS=/Wv:19.34

set CXXFLAGS=/Wv:19.29.30037
set CXXFLAGS=/Wv:19.34

cmake
-Wdev -Wdeprecated
-G"%generator%" -A"%platform%"
-DUSE_PRECOMPILED_HEADER=0 -DUSE_WERROR=1 -DBE_VERBOSE=1
-DUSE_CPP23=1
-DBUILD_DUMMY_APP=1 -DBUILD_TESTS=1
-S. -Bbuild

Expand Down
12 changes: 7 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ try_flag(WARNINGS "-W${WARNMODE}old-style-cast")
try_flag(WARNINGS "-Woverloaded-virtual")
try_flag(WARNINGS "-Wstrict-null-sentinel")
try_flag(WARNINGS "-W${WARNMODE}sign-compare")
try_flag(WARNINGS "-Wno-nonnull")

# MSVC /wd = warning disable
try_flag(WARNINGS "/wd4127") # conditional expression is constant
Expand Down Expand Up @@ -858,7 +859,7 @@ endif()
################################################################################
macro(AddApplicationInternal Target Executable)
add_executable(${Target} ${Sources})
target_link_libraries(${Target} ${A_Target}-objects)
target_link_libraries(${Target} ${A_Target}-objects ${CPP23SupportLibrary})

if (DEPS_DIR)
add_dependencies(${Target} runtime_deps)
Expand All @@ -883,15 +884,16 @@ endmacro()

function(AddApplication)
set(oneValueArgs Target ExecutableName)
set(multiValueArgs ApplicationMain Definitions Flags Files Libs Tests)
set(multiValueArgs ApplicationMain Definitions Flags CompileFeatures Files Libs Tests)
cmake_parse_arguments(A "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

# Reuse object files between the real application and the test one
add_library(${A_Target}-objects OBJECT EXCLUDE_FROM_ALL ${A_Files} ${PCH_FILE})
target_link_libraries(${A_Target}-objects engine-lib ${A_Libs} ${LIBS_BASE})
target_link_libraries(${A_Target}-objects engine-lib ${A_Libs} ${LIBS_BASE} ${CPP23SupportLibrary})
set_property(TARGET ${A_Target}-objects APPEND PROPERTY COMPILE_OPTIONS ${A_Flags})
set_property(TARGET ${A_Target}-objects APPEND PROPERTY INCLUDE_DIRECTORIES ${ENGINE_DIR} ${MOUNT_DIR} ${LIB_DIR})
set_property(TARGET ${A_Target}-objects APPEND PROPERTY COMPILE_DEFINITIONS ${A_Definitions})

set_target_properties(${A_Target}-objects PROPERTIES FOLDER "engine/objects")

set(Sources WIN32 ${A_ApplicationMain})
Expand All @@ -905,7 +907,7 @@ function(AddApplication)
# -Wl,--whole-archive -l<library path> -Wl,--no-whole-archive
set(Sources ${A_ApplicationMain} ${A_Tests})
AddApplicationInternal(test-${A_Target} test-${A_Target})
target_link_libraries(test-${A_Target} GTest::gmock)
target_link_libraries(test-${A_Target} ${CPP23SupportLibrary} GTest::gmock)
endif()

ADD_PRECOMPILED_HEADER(${A_Target}-objects)
Expand All @@ -915,7 +917,7 @@ daemon_write_buildinfo("Engine")

if (NOT NACL)
add_library(engine-lib EXCLUDE_FROM_ALL ${PCH_FILE} ${BUILDINFOLIST} ${COMMONLIST} ${ENGINELIST})
target_link_libraries(engine-lib ${LIBS_BASE} ${LIBS_ENGINE_BASE})
target_link_libraries(engine-lib ${LIBS_BASE} ${LIBS_ENGINE_BASE} ${CPP23SupportLibrary})
set_property(TARGET engine-lib APPEND PROPERTY COMPILE_DEFINITIONS BUILD_ENGINE)
set_property(TARGET engine-lib APPEND PROPERTY INCLUDE_DIRECTORIES ${ENGINE_DIR} ${MOUNT_DIR} ${LIB_DIR})
set_property(TARGET engine-lib APPEND PROPERTY COMPILE_OPTIONS ${WARNINGS})
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:

- job: Linux
pool:
vmImage: 'ubuntu-22.04'
vmImage: 'ubuntu-24.04'
strategy:
matrix:
GCC:
Expand Down
99 changes: 92 additions & 7 deletions cmake/DaemonFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,61 @@ endif()
option(USE_RECOMMENDED_CXX_STANDARD "Use recommended C++ standard" ON)
mark_as_advanced(USE_RECOMMENDED_CXX_STANDARD)

option(USE_CPP23 "Use C++23 standard where possible" OFF)

# Required for <stacktrace> on Clang/GCC
if(USE_CPP23)
if (DAEMON_CXX_COMPILER_Clang_COMPATIBILITY OR DAEMON_CXX_COMPILER_GCC_COMPATIBILITY)
if ((DAEMON_CXX_COMPILER_Clang_VERSION VERSION_GREATER_EQUAL 19.1.0) OR (DAEMON_CXX_COMPILER_GCC_VERSION VERSION_GREATER_EQUAL 13.3))
set(CPP23SupportLibraryTryExp TRUE)
endif()

if ((DAEMON_CXX_COMPILER_Clang_VERSION VERSION_GREATER_EQUAL 17.0.1) OR (DAEMON_CXX_COMPILER_GCC_VERSION VERSION_GREATER_EQUAL 12.1))
set(CPP23SupportLibraryTryBacktrace TRUE)
endif()

if (CPP23SupportLibraryTryExp)
set(CPP23SupportLibrary "-lstdc++exp")
set(CPP23SupportLibraryCompatibleCompiler TRUE)

find_library(HAVE_CPP23SupportLibrary "libstdc++exp")
endif()

if (CPP23SupportLibraryTryBacktrace AND (HAVE_CPP23SupportLibrary-NOTFOUND OR NOT CPP23SupportLibraryTryExp))
if (CPP23SupportLibraryCompatibleCompiler)
set(CPP23SupportLibraryOldLibrary TRUE)
endif()

set(CPP23SupportLibrary "-lstdc++_libbacktrace")
set(CPP23SupportLibraryCompatibleCompiler TRUE)

find_library(HAVE_CPP23SupportLibrary "libstdc++_libbacktrace")
endif()

if (HAVE_CPP23SupportLibrary-NOTFOUND)
if (NOT CPP23SupportLibraryCompatibleCompiler)
message(WARNING "Not using <stacktrace>: the compiler is too old (requires clang >= 17.0.1 or GCC >= 12.1)")
else()
message(WARNING "Not using <stacktrace>: libstdc++exp or libstdc++_backtrace is required, but wasn't found in system paths")
endif()

set(CPP23SupportLibrary "")
elseif (CXX_FLAGS MATCHES ".*\\-stdlib\\=libc\\+\\+*")
message(WARNING "Not using <stacktrace>: only -stdlib=libstdc++ is supported")

set(CPP23SupportLibrary "")
else()
add_definitions(-DDAEMON_CPP23_SUPPORT_LIBRARY_ENABLED=1)

if (CPP23SupportLibraryOldLibrary)
message(STATUS "Using <stacktrace>: found ${CPP23SupportLibrary} (recommended to use libc++exp on this compiler version instead, but it wasn't found)")
else()
message(STATUS "Using <stacktrace>: found ${CPP23SupportLibrary}")
endif()
endif()
endif()
endif()

# Set flag without checking, optional argument specifies build type
macro(set_c_flag FLAG)
if (${ARGC} GREATER 1)
Expand Down Expand Up @@ -162,6 +217,24 @@ macro(try_exe_linker_flag PROP FLAG)
endif()
endmacro()

# Stripping of absolute paths for __FILE__ / source_location
# Also do without src/ to get libs/
set(FILENAME_STRIP_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/src" "${CMAKE_CURRENT_SOURCE_DIR}")
if (NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL DAEMON_DIR)
set(FILENAME_STRIP_DIRS ${FILENAME_STRIP_DIRS} "${DAEMON_DIR}/src" "${DAEMON_DIR}")
endif()
foreach(strip_dir ${FILENAME_STRIP_DIRS})
if (MSVC)
string(REPLACE "/" "\\" backslashed_dir ${strip_dir})
# set_c_cxx_flag can't be used because macros barf if the input contains backslashes
# https://gitlab.kitware.com/cmake/cmake/-/issues/19281
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /d1trimfile:${backslashed_dir}\\")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /d1trimfile:${backslashed_dir}\\")
else()
try_c_cxx_flag(PREFIX_MAP "-ffile-prefix-map=${strip_dir}/=")
endif()
endforeach()

if (BE_VERBOSE)
set(WARNMODE "no-error=")
else()
Expand All @@ -179,6 +252,11 @@ endif()
if (MSVC)
set_c_cxx_flag("/MP")

# There is no flag for standards before C++17
if (USE_CPP23 AND USE_RECOMMENDED_CXX_STANDARD)
set_cxx_flag("/std:c++23preview")
endif()

if (USE_FAST_MATH)
set_c_cxx_flag("/fp:fast")
else()
Expand Down Expand Up @@ -256,13 +334,20 @@ else()
endif()

if (USE_RECOMMENDED_CXX_STANDARD)
# PNaCl only defines isascii if __STRICT_ANSI__ is not defined,
# always prefer GNU dialect.
try_cxx_flag(GNUXX14 "-std=gnu++14")
if (NOT FLAG_GNUXX14)
try_cxx_flag(GNUXX1Y "-std=gnu++1y")
if (NOT FLAG_GNUXX1Y)
message(FATAL_ERROR "GNU++14 is not supported by the compiler")
if (USE_CPP23)
try_cxx_flag(GNUXX23 "-std=gnu++23")
if (NOT FLAG_GNUXX23)
message(FATAL_ERROR "GNU++23 is not supported by the compiler")
endif()
else()
# PNaCl only defines isascii if __STRICT_ANSI__ is not defined,
# always prefer GNU dialect.
try_cxx_flag(GNUXX14 "-std=gnu++14")
if (NOT FLAG_GNUXX14)
try_cxx_flag(GNUXX1Y "-std=gnu++1y")
if (NOT FLAG_GNUXX1Y)
message(FATAL_ERROR "GNU++14 is not supported by the compiler")
endif()
endif()
endif()
endif()
Expand Down
6 changes: 4 additions & 2 deletions cmake/DaemonGame.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ function(buildGameModule module_slug)
set_target_properties(${module_target} PROPERTIES
OUTPUT_NAME "${GAMEMODULE_NAME}"
SUFFIX "${PLATFORM_EXE_SUFFIX}")
endif()

target_link_libraries(${module_target} ${GAMEMODULE_LIBS} ${LIBS_BASE})
target_link_libraries(${module_target} ${GAMEMODULE_LIBS} ${LIBS_BASE})
else()
target_link_libraries(${module_target} ${GAMEMODULE_LIBS} ${LIBS_BASE} ${CPP23SupportLibrary})
endif()

ADD_PRECOMPILED_HEADER(${module_target})
endfunction()
Expand Down
1 change: 1 addition & 0 deletions src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set(COMMONLIST
${COMMON_DIR}/Optional.h
${COMMON_DIR}/Platform.h
${COMMON_DIR}/Serialize.h
${COMMON_DIR}/StackTrace.h
${COMMON_DIR}/String.cpp
${COMMON_DIR}/String.h
${COMMON_DIR}/System.cpp
Expand Down
4 changes: 3 additions & 1 deletion src/common/CPPStandard.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define CPP_SOURCE_LOCATION
#endif

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

Expand Down
83 changes: 83 additions & 0 deletions src/common/StackTrace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
===========================================================================

Daemon BSD Source Code
Copyright (c) 2025 Daemon Developers
All rights reserved.

This file is part of the Daemon BSD Source Code (Daemon Source Code).

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Daemon developers nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

===========================================================================
*/
// StackTrace.h

#ifndef STACKTRACE_H
#define STACKTRACE_H

#include "CPPStandard.h"
#include "String.h"

#include "Log.h"

#if defined( CPP_STACKTRACE )

#include <stacktrace>

inline std::string FormatStackTrace( const std::stacktrace& stackTrace,
const bool skipCurrent = false, const bool compact = false ) {
std::string out;
bool skipped = !skipCurrent;
bool addLineEnd = false;

for ( const std::stacktrace_entry& entry : stackTrace ) {
if ( !skipped ) {
skipped = true;
continue;
}

if( compact ) {
out += Str::Format( addLineEnd ? "\n%s:%u" : "%s:%u", entry.source_file(), entry.source_line());
} else {
out += Str::Format( addLineEnd ? "\n%s:%u: %s" : "%s:%u: %s", entry.source_file(), entry.source_line(), entry.description());
}
addLineEnd = true;
}

return out;
}

inline void PrintStackTrace( const std::stacktrace& stackTrace = std::stacktrace::current() ) {
Log::Warn( "\n\n====================\nStackTrace:\n%s\n====================\n\n", FormatStackTrace( stackTrace ) );
}

#else

inline void PrintStackTrace() {
Log::Warn( "StackTrace unavailable: CPP23 required" );
}

#endif

#endif // STACKTRACE_H
7 changes: 6 additions & 1 deletion src/common/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "qcommon/sys.h"
#endif

#include "StackTrace.h"

namespace Sys {

// https://devblogs.microsoft.com/oldnewthing/20120105-00/?p=8683
Expand Down Expand Up @@ -234,10 +236,13 @@ void Drop(Str::StringRef message)
if (now - lastError < std::chrono::milliseconds(100)) {
if (++errorCount > 3)
Sys::Error(message);
} else
} else {
errorCount = 0;
}
lastError = now;

PrintStackTrace();

throw DropErr(true, message);
}

Expand Down
6 changes: 5 additions & 1 deletion src/engine/framework/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "System.h"
#include "CrashDump.h"
#include "CvarSystem.h"
#include "common/StackTrace.h"
#include <common/FileSystem.h>
#ifdef _WIN32
#include <windows.h>
Expand Down Expand Up @@ -552,10 +553,13 @@ void Error(Str::StringRef message)
{
// Crash immediately in case of a recursive error
static std::atomic_flag errorEntered;
if (errorEntered.test_and_set())
if (errorEntered.test_and_set()) {
_exit(-1);
}

Log::Warn(message);
PrintStackTrace();

Shutdown(true, message);

OSExit(1);
Expand Down
Loading