From 3ae6ecae8a4ccdafea02da1bc29d1220c26389cb Mon Sep 17 00:00:00 2001 From: Riyyi Date: Thu, 29 Aug 2024 22:15:13 +0200 Subject: [PATCH] Meta: Enable ASan and UBSan, disable warnings in vendor directory (address sanitizer, undefined behavior sanitizer) --- CMakeLists.txt | 32 ++++++++++++++++++++++---------- example/CMakeLists.txt | 1 + src/CMakeLists.txt | 1 + vendor/CMakeLists.txt | 1 + 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fec9518..539a2ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ endif() # Options option(BUILD_SHARED_LIBS "Build shared libraries" OFF) option(INFERNO_BUILD_EXAMPLES "Build the Inferno example programs" ${INFERNO_STANDALONE}) +option(INFERNO_BUILD_WARNINGS "Build with warnings enabled" ${INFERNO_STANDALONE}) # ------------------------------------------ @@ -25,10 +26,15 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) # Compiler flags used for all build types -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic") -# -Wall = All warnings about contructions that are easily avoidable -# -Wextra = Extra warning flags not covered by -Wall -# -Wpedantic = Warnings for compiler extensions not part of the standard +set(COMPILE_FLAGS_DEPS -w) +if(INFERNO_BUILD_WARNINGS) + set(COMPILE_FLAGS_PROJECT -Wall -Wextra -Wpedantic) + # -Wall = All warnings about contructions that are easily avoidable + # -Wextra = Extra warning flags not covered by -Wall + # -Wpedantic = Warnings for compiler extensions not part of the standard +else() + set(COMPILE_FLAGS_PROJECT ${COMPILE_FLAGS_DEPS}) +endif() # Set default build type if not specified set(DEFAULT_BUILD_TYPE Release) @@ -42,14 +48,20 @@ endif() # Set build type specific compiler flags message("--- ${CMAKE_BUILD_TYPE} ---") if(${CMAKE_BUILD_TYPE} STREQUAL Debug) - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Og -g -pg") - # -Og = Optimizations that do not interfere with debugging - # -g = Produce debugging information in OS's native format - # -pg = Generate profile information for analysis with gprof + # Optimizations that do not interfere with debugging + string(APPEND CMAKE_CXX_FLAGS_DEBUG " -Og") + # Produce debugging information in OS's native format + string(APPEND CMAKE_CXX_FLAGS_DEBUG " -g") + # Generate profile information for analysis with gprof # $ gprof gmon.out > profile-data.txt + string(APPEND CMAKE_CXX_FLAGS_DEBUG " -pg") + # Enable ASan (Address Sanitizer) and UBSan (Undefined Behavior Sanitizer) + string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fsanitize=address,undefined") + # Do not omit frame pointer, which helps with debugging. + string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fno-omit-frame-pointer") elseif(${CMAKE_BUILD_TYPE} STREQUAL Release) - set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3") - # -O3 = Optimizations that increase compilation time and performance + # Optimizations that increase compilation time and performance + string(APPEND CMAKE_CXX_FLAGS_RELEASE " -O3") endif() set(CMAKE_EXPORT_COMPILE_COMMANDS ON) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 8c47476..8deede6 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -15,6 +15,7 @@ add_executable(${GAME} ${GAME_SOURCES}) target_include_directories(${GAME} PRIVATE "src") target_link_libraries(${GAME} ${ENGINE}) +target_compile_options(${GAME} PRIVATE ${COMPILE_FLAGS_PROJECT}) target_precompile_headers(${GAME} REUSE_FROM ${ENGINE}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9dd8652..0fc258c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,6 +10,7 @@ target_include_directories(${ENGINE} PUBLIC "../vendor/sol2/include" "../vendor/stb") target_link_libraries(${ENGINE} ${ENGINE}-dependencies) +target_compile_options(${ENGINE} PRIVATE ${COMPILE_FLAGS_PROJECT}) # ------------------------------------------ diff --git a/vendor/CMakeLists.txt b/vendor/CMakeLists.txt index 5a9d270..3e10465 100644 --- a/vendor/CMakeLists.txt +++ b/vendor/CMakeLists.txt @@ -33,3 +33,4 @@ target_include_directories(${ENGINE}-dependencies PUBLIC "glad/include" "lua") target_link_libraries(${ENGINE}-dependencies glfw ruc ruc-test assimp) +target_compile_options(${ENGINE}-dependencies PRIVATE ${COMPILE_FLAGS_DEPS})