Skip to content
Open
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
120 changes: 120 additions & 0 deletions CMake/set_compiler_flags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# =============================================================================
# A CMake library script for the `set_compiler_flags' function
# =============================================================================

# ============================================================================
#
# 2025-09-21 Ljubomir Kurij <ljubomir_kurij@proton.me>
#
# * Created.
#
# ============================================================================

# -----------------------------------------------------------------------------
# Function to set compiler flags based on compiler and build type
# -----------------------------------------------------------------------------
function (set_compiler_flags)
# Check which compiler is being used and set flags accordingly
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
message (STATUS "Using Clang compiler ...")
# -------------------------------------------------------------------------
# Clang-specific flags
# -------------------------------------------------------------------------
set (DEBUG_FLAGS
# "-g3 -O0 -fno-omit-frame-pointer -Wall -Wextra -Wpedantic -Werror -lstdc++"
"-g3 -O0 -fno-omit-frame-pointer -Wall -Wextra -Wpedantic -lstdc++"
)
set (RELEASE_FLAGS
"-O3 -DNDEBUG -march=native -fvectorize -flto -lstdc++"
)
set (MINSIZEREL_FLAGS
"-Os -DNDEBUG -ffunction-sections -fdata-sections -flto -lstdc++"
)
set (RELWITHDEBINFO_FLAGS
"-O2 -g -DNDEBUG -march=native -fvectorize -lstdc++"
)
set (MINSIZEREL_LINKER_FLAGS
"-Wl,--gc-sections -Wl,--strip-all"
)

elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
message (STATUS "Using GNU compiler ...")
# -------------------------------------------------------------------------
# GNU-specific flags
# -------------------------------------------------------------------------
set (DEBUG_FLAGS
# "-g3 -O0 -fno-omit-frame-pointer -Wall -Wextra -Wpedantic -Werror"
"-g3 -O0 -fno-omit-frame-pointer -Wall -Wextra -Wpedantic -lstdc++"
)
set (RELEASE_FLAGS
"-O3 -DNDEBUG -march=native -ftree-vectorize -flto -lstdc++"
)
set (MINSIZEREL_FLAGS
"-Os -DNDEBUG -ffunction-sections -fdata-sections -flto -lstdc++"
)
set (RELWITHDEBINFO_FLAGS
"-O2 -g -DNDEBUG -march=native -ftree-vectorize -lstdc++"
)
set (MINSIZEREL_LINKER_FLAGS
"-Wl,--gc-sections -Wl,--strip-all"
)

elseif (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
message (STATUS "Using MSVC compiler ...")
# -------------------------------------------------------------------------
# MSVC-specific flags
# -------------------------------------------------------------------------
set (DEBUG_FLAGS
# "/Zi /Od /Ob0 /MDd /W4 /WX /permissive- /RTC1"
"/Zi /Od /Ob0 /MDd /W4 /permissive- /RTC1"
)
set (RELEASE_FLAGS
"/O2 /MD /DNDEBUG /GL /fp:fast"
)
set (MINSIZEREL_FLAGS
"/O1 /MD /DNDEBUG /GL"
)
set (RELWITHDEBINFO_FLAGS
"/O2 /Zi /MD /DNDEBUG"
)
set (MINSIZEREL_LINKER_FLAGS
"/OPT:REF /OPT:ICF /LTCG"
)

elseif(CMAKE_CXX_COMPILER_ID MATCHES "Intel"
OR CMAKE_CXX_COMPILER_ID MATCHES "IntelLLVM"
)
message (STATUS "Using Intel compiler ...")
# -------------------------------------------------------------------------
# Intel-specific flags
# -------------------------------------------------------------------------
set(DEBUG_FLAGS
# "-g3 -O0 -Wall -Wextra -Werror -traceback -lstdc++"
"-g3 -O0 -Wall -Wextra -traceback -lstdc++"
)
set(RELEASE_FLAGS
"-O3 -DNDEBUG -xHost -ipo -lstdc++"
)
set(MINSIZEREL_FLAGS
"-Os -DNDEBUG -ipo -lstdc++"
)
set(RELWITHDEBINFO_FLAGS
"-O2 -g -DNDEBUG -xHost -ipo -lstdc++")
set(MINSIZEREL_LINKER_FLAGS
"-Wl,--gc-sections -Wl,--strip-all"
)
endif()

# Apply the flags to the appropriate CMake variables with PARENT_SCOPE
# to make them available in the parent scope
set(CMAKE_CXX_FLAGS_DEBUG "${DEBUG_FLAGS}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS_RELEASE "${RELEASE_FLAGS}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS_MINSIZEREL "${MINSIZEREL_FLAGS}" PARENT_SCOPE)
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${RELWITHDEBINFO_FLAGS}" PARENT_SCOPE)

if(DEFINED MINSIZEREL_LINKER_FLAGS)
set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${MINSIZEREL_LINKER_FLAGS}" PARENT_SCOPE)
endif()
endfunction()

# End of `set_compiler_flags.cmake'
120 changes: 107 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,43 +1,137 @@
cmake_minimum_required(VERSION 3.8)
# =============================================================================
# A CMake build script for the `C++ Playground' project
# =============================================================================

# -----------------------------------------------------------------------------
# Define the minimum required version of CMake
# -----------------------------------------------------------------------------

cmake_minimum_required(VERSION 3.8...3.29)


# -----------------------------------------------------------------------------
# Enable Hot Reload for MSVC compilers if supported.
# -----------------------------------------------------------------------------

if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
set(
CMAKE_MSVC_DEBUG_INFORMATION_FORMAT
"$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>"
)
endif()


# -----------------------------------------------------------------------------
# Define the project name and version
# -----------------------------------------------------------------------------

project(clipp
VERSION 1.2.3
LANGUAGES CXX)
DESCRIPTION "Command line interfaces for modern C++"
LANGUAGES CXX
)


# -----------------------------------------------------------------------------
# Define customizable user options
# -----------------------------------------------------------------------------

# option (BUILD_SHARED_LIBS "Build using shared libraries" ON)
option (BUILD_TESTS "Build with tests" OFF)


# -----------------------------------------------------------------------------
# Configure build options
# -----------------------------------------------------------------------------

include(GNUInstallDirs)
set(CMAKE_VERBOSE_MAKEFILE ON)

# Show which project we are building
message(STATUS
"Configuring build for `"
${PROJECT_NAME}
"' v"
${PROJECT_VERSION}
" ..."
)

# Set the build type to MinSizeRel if not specified
if (NOT DEFINED CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE MinSizeRel)
endif ()
message(STATUS "Build type set to: `" ${CMAKE_BUILD_TYPE} "' ...")

# If build type is Debug set for debugging code to compile
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
message(STATUS "Use debugging code set to: `ON' ...")
set(USE_DEBUG ON)
else ()
message(STATUS "Use debugging code set to: `OFF' ...")
set(USE_DEBUG OFF)
endif ()

# Set custom compiler options
message (STATUS "Setting custom compiler options ...")
include ("${CMAKE_SOURCE_DIR}/CMake/set_compiler_flags.cmake")
set_compiler_flags()
message (STATUS "Custom compiler options set to: `"
${CMAKE_CXX_FLAGS} " "
${CMAKE_CXX_FLAGS_DEBUG}
"' ..."
)

message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
add_library(${PROJECT_NAME} INTERFACE)
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_11)
target_include_directories(${PROJECT_NAME} INTERFACE
target_include_directories(
${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
)

set(CONFIG_VERSION_FILE ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake)
set(
CONFIG_VERSION_FILE
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake
)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
${CONFIG_VERSION_FILE} COMPATIBILITY AnyNewerVersion
)
)

install(DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FILES_MATCHING PATTERN "*.h"
)
)
install(TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}-config
)
)
install(EXPORT ${PROJECT_NAME}-config
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}
NAMESPACE ${PROJECT_NAME}::
)
)
install(FILES ${CONFIG_VERSION_FILE}
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}
)
)

option(BUILD_TESTING "Do not build tests by default" OFF)
include(CTest)
if(BUILD_TESTING AND ${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})

# -----------------------------------------------------------------------------
# Check if we are building with the unit tests
# -----------------------------------------------------------------------------

# Check if the tests are enabled
if (NOT DEFINED BUILD_TESTS)
set (BUILD_TESTS OFF) # Default to OFF
endif ()
message(STATUS "Build tests set to: `" ${BUILD_TESTS} "' ...")

# Add the test files directory if the tests are enabled
if(BUILD_TESTS AND ${CMAKE_SOURCE_DIR} STREQUAL ${PROJECT_SOURCE_DIR})
include(CTest)
add_subdirectory(test)
endif()


# End of `CMakeLists.txt'
2 changes: 0 additions & 2 deletions include/clipp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1406,9 +1406,7 @@ class action_provider
//---------------------------------------------------------------
/** @brief executes all argument actions */
void execute_actions(const arg_string& arg) const {
int i = 0;
for(const auto& a : argActions_) {
++i;
a(arg.c_str());
}
}
Expand Down
50 changes: 25 additions & 25 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@ endif()
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")

message(STATUS "CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
add_compile_options(-stdlib=libc++)
else()
add_compile_options(-Wlogical-op)
add_compile_options(-Wnoexcept)
add_compile_options(-Wstrict-null-sentinel)
add_compile_options(-Wuseless-cast)
endif()
add_compile_options(-Wall -Wextra -Wpedantic)
add_compile_options(-Wcast-align -Wcast-qual)
add_compile_options(-Wctor-dtor-privacy)
add_compile_options(-Wconversion -Wno-sign-conversion)
add_compile_options(-Wdisabled-optimization)
add_compile_options(-Wdouble-promotion)
add_compile_options(-Wformat=2)
add_compile_options(-Winit-self)
add_compile_options(-Wmissing-include-dirs)
add_compile_options(-Wold-style-cast)
add_compile_options(-Woverloaded-virtual)
add_compile_options(-Wredundant-decls)
add_compile_options(-Wshadow)
add_compile_options(-Wstrict-aliasing=1)
add_compile_options(-Wstrict-overflow=5)
add_compile_options(-Wswitch-default)
add_compile_options(-Wundef)
# if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
# add_compile_options(-stdlib=libc++)
# else()
# add_compile_options(-Wlogical-op)
# add_compile_options(-Wnoexcept)
# add_compile_options(-Wstrict-null-sentinel)
# add_compile_options(-Wuseless-cast)
# endif()
# add_compile_options(-Wall -Wextra -Wpedantic)
# add_compile_options(-Wcast-align -Wcast-qual)
# add_compile_options(-Wctor-dtor-privacy)
# add_compile_options(-Wconversion -Wno-sign-conversion)
# add_compile_options(-Wdisabled-optimization)
# add_compile_options(-Wdouble-promotion)
# add_compile_options(-Wformat=2)
# add_compile_options(-Winit-self)
# add_compile_options(-Wmissing-include-dirs)
# add_compile_options(-Wold-style-cast)
# add_compile_options(-Woverloaded-virtual)
# add_compile_options(-Wredundant-decls)
# add_compile_options(-Wshadow)
# add_compile_options(-Wstrict-aliasing=1)
# add_compile_options(-Wstrict-overflow=5)
# add_compile_options(-Wswitch-default)
# add_compile_options(-Wundef)

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} source_files)
foreach(src IN LISTS source_files)
Expand Down