Skip to content

Commit

Permalink
New Update
Browse files Browse the repository at this point in the history
- CMake update to 3.23
- Added new compiler flags.
- Added new Sanitizers.
- Added some comments for cmake configs.
  • Loading branch information
TheCompez committed Jan 26, 2024
1 parent fb6e242 commit 0a3c1a3
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 85 deletions.
18 changes: 14 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
# -------------------- PROJECT TEMPLATE INFO ---------------------------------------------
# Title : Project Template
# Version : 1.1.325
# Version : 1.1.430
# Author : Kambiz Asadzadeh
# License : MIT
# Url : https://github.com/genyleap/Project-Template
# Organization : The Genyleap
# Organization : Genyleap
# ----------------------------------------------------------------------------------------

cmake_minimum_required(VERSION 3.27)
cmake_minimum_required(VERSION 3.28)

if(${CMAKE_VERSION} VERSION_LESS 3.28)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.28)
endif()

cmake_policy(SET CMP0048 NEW)
cmake_policy(SET CMP0152 NEW)
cmake_policy(SET CMP0155 NEW)

# Add include path for cmake modules
# ------ PROJECT EXTRA CMAKE ------
Expand Down Expand Up @@ -42,8 +52,8 @@ if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
OUTPUT_VARIABLE PROJECT_VERSION_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE
)

endif()

if(NOT PROJECT_VERSION_TAG)
set(PROJECT_VERSION_TAG 00000000)
endif()
Expand Down
65 changes: 31 additions & 34 deletions cmake/color-message.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
cmake_minimum_required(VERSION 3.23)
if(${CMAKE_VERSION} VERSION_LESS 3.23)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.23)
endif()

# Define color codes for terminal output
if(NOT WIN32)
string(ASCII 27 Esc)
set(ColourReset "${Esc}[m")
Expand All @@ -26,33 +20,36 @@ if(NOT WIN32)
set(BoldWhite "${Esc}[1;37m")
endif()

# Function to print colored messages to the terminal
function(ColorMessage)
list(GET ARGV 0 MessageType)
if(MessageType STREQUAL FATAL_ERROR OR MessageType STREQUAL SEND_ERROR)
list(REMOVE_AT ARGV 0)
message(${MessageType} "${BoldRed}>> ${ARGV}${ColourReset}")
elseif(MessageType STREQUAL WARNING)
list(REMOVE_AT ARGV 0)
message(${MessageType} "${BoldYellow}>> ${ARGV}${ColourReset}")
elseif(MessageType STREQUAL AUTHOR_WARNING)
list(REMOVE_AT ARGV 0)
message(${MessageType} "${BoldCyan}>> ${ARGV}${ColourReset}")
elseif(MessageType STREQUAL STATUS)
list(REMOVE_AT ARGV 0)
message(${MessageType} "${Green}>> ${ARGV}${ColourReset}")
elseif(MessageType STREQUAL INFO)
list(REMOVE_AT ARGV 0)
message("-- ${BoldBlue}>> ${ARGV}${ColourReset}")
elseif(MessageType STREQUAL NOTICE)
list(REMOVE_AT ARGV 0)
message("-- ${Blue}>> ${ARGV}${ColourReset}")
elseif(MessageType STREQUAL IMPORTANT)
list(REMOVE_AT ARGV 0)
message("-- ${Yellow}>> ${ARGV}${ColourReset}")
elseif(MessageType STREQUAL MORE_IMPORTANT)
list(REMOVE_AT ARGV 0)
message("-- ${BoldYellow}>> ${ARGV}${ColourReset}")
else()
message(">> ${ARGV}")
endif()
# Determine the message type and apply the appropriate color
if(MessageType STREQUAL FATAL_ERROR OR MessageType STREQUAL SEND_ERROR)
list(REMOVE_AT ARGV 0)
message(${MessageType} "${BoldRed}>> ${ARGV}${ColourReset}")
elseif(MessageType STREQUAL WARNING)
list(REMOVE_AT ARGV 0)
message(${MessageType} "${BoldYellow}>> ${ARGV}${ColourReset}")
elseif(MessageType STREQUAL AUTHOR_WARNING)
list(REMOVE_AT ARGV 0)
message(${MessageType} "${BoldCyan}>> ${ARGV}${ColourReset}")
elseif(MessageType STREQUAL STATUS)
list(REMOVE_AT ARGV 0)
message(${MessageType} "${Green}>> ${ARGV}${ColourReset}")
elseif(MessageType STREQUAL INFO)
list(REMOVE_AT ARGV 0)
message("-- ${BoldBlue}>> ${ARGV}${ColourReset}")
elseif(MessageType STREQUAL NOTICE)
list(REMOVE_AT ARGV 0)
message("-- ${Blue}>> ${ARGV}${ColourReset}")
elseif(MessageType STREQUAL IMPORTANT)
list(REMOVE_AT ARGV 0)
message("-- ${Yellow}>> ${ARGV}${ColourReset}")
elseif(MessageType STREQUAL MORE_IMPORTANT)
list(REMOVE_AT ARGV 0)
message("-- ${BoldYellow}>> ${ARGV}${ColourReset}")
else()
# Default message without color
message(">> ${ARGV}")
endif()
endfunction()
124 changes: 98 additions & 26 deletions cmake/compiler-options.cmake
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
cmake_minimum_required(VERSION 3.23)
if(${CMAKE_VERSION} VERSION_LESS 3.23)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.23)
endif()

include(CheckCXXCompilerFlag)

CHECK_CXX_COMPILER_FLAG("-std=c++26" COMPILER_SUPPORTS_CXX26)
CHECK_CXX_COMPILER_FLAG("-std=c++23" COMPILER_SUPPORTS_CXX23)
CHECK_CXX_COMPILER_FLAG("-std=c++20" COMPILER_SUPPORTS_CXX20)
CHECK_CXX_COMPILER_FLAG("-std=c++17" COMPILER_SUPPORTS_CXX17)
CHECK_CXX_COMPILER_FLAG("-std=c++14" COMPILER_SUPPORTS_CXX14)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)

if(COMPILER_SUPPORTS_CXX23)
if(COMPILER_SUPPORTS_CXX26)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++26")
elseif(COMPILER_SUPPORTS_CXX23)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++23")
elseif(COMPILER_SUPPORTS_CXX23)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20")
Expand Down Expand Up @@ -45,9 +41,9 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_STANDARD_LIBRARIES "-lstdc++")
endif()


# Multi-threaded DLL runtime library (/MD or /MDd)
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
#Todo...
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MD")
endif()

if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
Expand Down Expand Up @@ -85,7 +81,7 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(CMAKE_CXX_FLAGS_RELEASE "/Ot")
endif()
else()
message("-- CMake run for non-msvc")
message("-- CMake run for GNU")
if(OPTIMIZATION_LEVEL EQUAL "0")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O0")
Expand Down Expand Up @@ -116,72 +112,86 @@ endif()
# --- Build Options --- #
#########################

# Use Feature Test library
option(USE_FEATURE_TEST "Use Feature Test library" OFF)
if (USE_FEATURE_TEST)
add_definitions(-DUSE_FEATURE_TEST)
endif()

option(USE_SSL_METHOD "Use Https/SSL system" OFF)
# Use Https/SSL system
option(USE_SSL_METHOD "Use Https/SSL system" OFF)
if (USE_SSL_METHOD)
add_definitions(-DUSE_SSL_METHOD)
endif()

option(ENABLE_TESTING "Use test mode" FALSE)
# Use test mode
option(ENABLE_TESTING "Use test mode" OFF)
if (ENABLE_TESTING)
add_definitions(-DENABLE_TESTING)
endif()

option(ENABLE_CLANG_TIDY "Enabling the test of clang-tidy" FALSE)
# Enable the test of clang-tidy
option(ENABLE_CLANG_TIDY "Enabling the test of clang-tidy" OFF)
if (ENABLE_CLANG_TIDY)
add_definitions(-DENABLE_CLANG_TIDY)
endif()

option(ENABLE_CPPCHECK "Enabling the test of cppcheck" FALSE)
# Enable the test of cppcheck
option(ENABLE_CPPCHECK "Enabling the test of cppcheck" OFF)
if (ENABLE_CPPCHECK)
add_definitions(-DENABLE_CPPCHECK)
endif()

option(SIMPLE_BUILD "Build the project as minimally as possible" FALSE)
# Build the project as minimally as possible
option(SIMPLE_BUILD "Build the project as minimally as possible" OFF)
if (SIMPLE_BUILD)
add_definitions(-DSIMPLE_BUILD)
endif()

option(ENABLE_ASAN "Enable address sanitizer" FALSE)
# Enable address sanitizer
option(ENABLE_ASAN "Enable address sanitizer" OFF)
if (ENABLE_ASAN)
add_definitions(-DENABLE_ASAN)
endif()

option(BUILD_DOC "Build the project's documentation" OFF)
# Build the project's documentation
option(BUILD_DOC "Build the project's documentation" OFF)
if (BUILD_DOC)
add_definitions(-DBUILD_DOC)
endif()

# Compile Qt Quick (QML) files
option(USE_QT_QUICK_COMPILER "Compile Qt Quick (QML) files." OFF)
if (USE_QT_QUICK_COMPILER)
add_definitions(-DUSE_QT_QUICK_COMPILER)
endif()

option(ENABLE_DEVELOPER_MODE "Developer Mode" ON)
# Developer Mode
option(ENABLE_DEVELOPER_MODE "Developer Mode" ON)
if (ENABLE_DEVELOPER_MODE)
add_definitions(-DENABLE_DEVELOPER_MODE)
endif()

option(ENABLE_TODO_MODE "Todo Mode (Not activated feature!)" ON)
# Todo Mode (Not activated feature!)
option(ENABLE_TODO_MODE "Todo Mode (Not activated feature!)" ON)
if (ENABLE_TODO_MODE)
add_definitions(-DENABLE_TODO_MODE)
endif()

option(ENABLE_EXPERIMENTAL_MODE "Experimental Mode" ON)
# Experimental Mode
option(ENABLE_EXPERIMENTAL_MODE "Experimental Mode" ON)
if (ENABLE_EXPERIMENTAL_MODE)
add_definitions(-DENABLE_EXPERIMENTAL_MODE)
endif()

option(FORCE_COLORED_OUTPUT "Always produce ANSI-colored output (GNU/Clang only)." TRUE)
# Always produce ANSI-colored output (GNU/Clang only).
option(FORCE_COLORED_OUTPUT "Always produce ANSI-colored output (GNU/Clang only)." TRUE)
if (FORCE_COLORED_OUTPUT)
add_definitions(-DFORCE_COLORED_OUTPUT)
endif()

option(ENABLE_SAFE_ONLY "Enable safe codes only!" FALSE)
# Enable safe codes only!
option(ENABLE_SAFE_ONLY "Enable safe codes only!" OFF)
if (ENABLE_SAFE_ONLY)
add_definitions(-DENABLE_SAFE_ONLY)
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
Expand All @@ -191,7 +201,8 @@ if (ENABLE_SAFE_ONLY)
endif()
endif()

option(ENABLE_WARN_MODE "Enable full warnings for compiler!" FALSE)
# Enable full warnings for the compiler!
option(ENABLE_WARN_MODE "Enable full warnings for compiler!" OFF)
if (ENABLE_WARN_MODE)
add_definitions(-DENABLE_WARN_MODE)
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
Expand All @@ -201,38 +212,99 @@ if (ENABLE_WARN_MODE)
endif()
endif()

# Enable developer (debug) mode
option(BUILD_DEBUG_MODE "Enable developer (debug) mode" ON)
if (BUILD_DEBUG_MODE)
add_definitions(-DBUILD_DEBUG_MODE)
endif()

option(DEBUG_LOGGING "Enabling the build of debug logging" FALSE)
# Enabling the build of debug logging
option(DEBUG_LOGGING "Enabling the build of debug logging" OFF)
if (DEBUG_LOGGING)
add_definitions(-DDEBUG_LOGGING)
endif()

# Build Static Version
option(ENABLE_STATIC_LIB_BUILD "Build Static Version" OFF)
if (ENABLE_STATIC_LIB_BUILD)
add_definitions(-DENABLE_STATIC_LIB_BUILD)
endif()

# Build Shared Version
option(ENABLE_SHARED_LIB_BUILD "Build Shared Version" OFF)
if (ENABLE_SHARED_LIB_BUILD)
add_definitions(-DENABLE_SHARED_LIB_BUILD)
endif()

# Build Executable Version
option(ENABLE_BINARY_BUILD "Build Executable Version" ON)
if (ENABLE_BINARY_BUILD)
add_definitions(-DENABLE_BINARY_BUILD)
endif()

# Header Only Version
option(ENABLE_HEADER_ONLY_BUILD "Header Only Version" OFF)
if (ENABLE_HEADER_ONLY_BUILD)
add_definitions(-DENABLE_HEADER_ONLY_BUILD)
endif()

option(FORCE_LATEST_STANDARD_FEATURE "Forcing to enable updated programming language." FALSE)
# Forcing to enable updated programming language.
option(FORCE_LATEST_STANDARD_FEATURE "Forcing to enable updated programming language." OFF)
if (FORCE_LATEST_STANDARD_FEATURE)
add_definitions(-DFORCE_LATEST_STANDARD_FEATURE)
endif()

# Sanitizers Options
option(ENABLE_SANITIZERS "Enable Sanitizers" ON)
option(ENABLE_ADDRESS_SANITIZER "Enable AddressSanitizer (ASan)" OFF)
option(ENABLE_LEAK_SANITIZER "Enable LeakSanitizer (LSan)" OFF)
option(ENABLE_MEMORY_SANITIZER "Enable MemorySanitizer" OFF)
option(ENABLE_THREAD_SANITIZER "Enable ThreadSanitizer" OFF)
option(ENABLE_UNDEFINED_SANITIZER "Enable UndefinedBehaviorSanitizer" OFF)

if (ENABLE_SANITIZERS)
add_definitions(-DENABLE_SANITIZERS)
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,leak,undefined")
if (ENABLE_MEMORY_SANITIZER AND NOT APPLE)
add_definitions(-DENABLE_MEMORY_SANITIZER)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory")
endif()
if (ENABLE_THREAD_SANITIZER)
add_definitions(-DENABLE_THREAD_SANITIZER)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
endif()
if (ENABLE_UNDEFINED_SANITIZER)
add_definitions(-DENABLE_UNDEFINED_SANITIZER)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
endif()
if (APPLE)
message(STATUS "Sanitizers are based on Clang on macOS.")
endif()
elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND NOT APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,leak,undefined")
elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND APPLE)
message(WARNING "Sanitizer is not supported with GCC on macOS. Ignoring the options.")
else()
message(WARNING "Unsupported compiler. Sanitizers are disabled.")
endif()
else()
# Check and enable individual sanitizers
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
foreach(SANITIZER IN ITEMS ADDRESS LEAK MEMORY THREAD UNDEFINED)
if ("ENABLE_${SANITIZER}_SANITIZER")
add_definitions(-DENABLE_${SANITIZER}_SANITIZER)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=${SANITIZER}")
endif()
endforeach()
elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND NOT APPLE)
foreach(SANITIZER IN ITEMS ADDRESS LEAK MEMORY THREAD UNDEFINED)
if ("ENABLE_${SANITIZER}_SANITIZER")
add_definitions(-DENABLE_${SANITIZER}_SANITIZER)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=${SANITIZER}")
endif()
endforeach()
else()
message(WARNING "Unsupported compiler. Sanitizers are disabled.")
endif()
endif()
7 changes: 0 additions & 7 deletions cmake/cross-compile.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
cmake_minimum_required(VERSION 3.23)
if(${CMAKE_VERSION} VERSION_LESS 3.23)
cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
cmake_policy(VERSION 3.23)
endif()

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/platforms-toolchain/")

Expand Down
Loading

0 comments on commit 0a3c1a3

Please sign in to comment.