Skip to content

Project Flags

1
2
3
4
5
if (${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_SOURCE_DIR})
    set(MASTER_PROJECT ON)
else ()
    set(MASTER_PROJECT OFF)
endif ()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(DEBUG_MODE ON)
    set(NOT_DEBUG_MODE OFF)
    set(RELEASE_MODE OFF)
    set(NOT_RELEASE_MODE ON)
else ()
    set(DEBUG_MODE OFF)
    set(NOT_DEBUG_MODE ON)
    set(RELEASE_MODE ON)
    set(NOT_RELEASE_MODE OFF)
endif ()

1
2
3
4
5
6
7
set(CLANG OFF)
set(GCC OFF)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
    set(CLANG ON)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    set(GCC ON)
endif ()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# This will set the flags only if the user didn't explicitly choose them with -DCMAKE_CXX_FLAGS
if (NOT CMAKE_CXX_FLAGS)
    if (CMAKE_BUILD_TYPE STREQUAL "Debug")
        if (MSVC)
            list(APPEND CMAKE_CXX_FLAGS "/O0")
        else ()
            list(APPEND CMAKE_CXX_FLAGS "-O0")
        endif ()
    else ()
        if (MSVC)
            list(APPEND CMAKE_CXX_FLAGS "/O2")
        else ()
            list(APPEND CMAKE_CXX_FLAGS "-O2")
        endif ()
    endif ()
    message("Setting CXX flags to default for ${CMAKE_BUILD_TYPE} mode (${CMAKE_CXX_FLAGS})")
else ()
    message("CXX flags are already set to ${CMAKE_CXX_FLAGS}")
endif ()

Share Snippets