spdlog/cmake/utils.cmake

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

74 lines
3.2 KiB
CMake
Raw Permalink Normal View History

2020-05-02 16:38:14 +08:00
# Get spdlog version from include/spdlog/version.h and put it in SPDLOG_VERSION
function(spdlog_extract_version)
2020-05-02 16:42:08 +08:00
file(READ "${CMAKE_CURRENT_LIST_DIR}/include/spdlog/version.h" file_contents)
string(REGEX MATCH "SPDLOG_VER_MAJOR ([0-9]+)" _ "${file_contents}")
2021-07-19 05:50:51 +08:00
if(NOT CMAKE_MATCH_COUNT EQUAL 1)
2020-05-02 16:42:08 +08:00
message(FATAL_ERROR "Could not extract major version number from spdlog/version.h")
2021-07-19 05:50:51 +08:00
endif()
2020-05-02 16:42:08 +08:00
set(ver_major ${CMAKE_MATCH_1})
2020-05-02 16:38:14 +08:00
2020-05-02 16:42:08 +08:00
string(REGEX MATCH "SPDLOG_VER_MINOR ([0-9]+)" _ "${file_contents}")
2021-07-19 05:50:51 +08:00
if(NOT CMAKE_MATCH_COUNT EQUAL 1)
2020-05-02 16:42:08 +08:00
message(FATAL_ERROR "Could not extract minor version number from spdlog/version.h")
2021-07-19 05:50:51 +08:00
endif()
2020-05-02 16:38:14 +08:00
2020-05-02 16:42:08 +08:00
set(ver_minor ${CMAKE_MATCH_1})
string(REGEX MATCH "SPDLOG_VER_PATCH ([0-9]+)" _ "${file_contents}")
2021-07-19 05:50:51 +08:00
if(NOT CMAKE_MATCH_COUNT EQUAL 1)
2020-05-02 16:42:08 +08:00
message(FATAL_ERROR "Could not extract patch version number from spdlog/version.h")
2021-07-19 05:50:51 +08:00
endif()
2020-05-02 16:42:08 +08:00
set(ver_patch ${CMAKE_MATCH_1})
2020-05-02 16:38:14 +08:00
2020-05-02 16:42:08 +08:00
set(SPDLOG_VERSION_MAJOR ${ver_major} PARENT_SCOPE)
set(SPDLOG_VERSION_MINOR ${ver_minor} PARENT_SCOPE)
set(SPDLOG_VERSION_PATCH ${ver_patch} PARENT_SCOPE)
set(SPDLOG_VERSION "${ver_major}.${ver_minor}.${ver_patch}" PARENT_SCOPE)
2020-05-02 16:38:14 +08:00
endfunction()
# Turn on warnings on the given target
function(spdlog_enable_warnings target_name)
2021-07-19 05:50:51 +08:00
if(SPDLOG_BUILD_WARNINGS)
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
2020-05-02 16:42:08 +08:00
list(APPEND MSVC_OPTIONS "/W3")
2021-07-19 05:50:51 +08:00
if(MSVC_VERSION GREATER 1900) # Allow non fatal security warnings for msvc 2015
2020-05-02 16:42:08 +08:00
list(APPEND MSVC_OPTIONS "/WX")
2021-07-19 05:50:51 +08:00
endif()
endif()
2020-05-02 16:38:14 +08:00
2020-05-02 16:42:08 +08:00
target_compile_options(
2021-07-19 05:50:51 +08:00
${target_name}
PRIVATE $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall
-Wextra
-Wconversion
-pedantic
-Werror
-Wfatal-errors>
$<$<CXX_COMPILER_ID:MSVC>:${MSVC_OPTIONS}>)
endif()
2020-05-02 16:38:14 +08:00
endfunction()
# Enable address sanitizer (gcc/clang only)
function(spdlog_enable_addr_sanitizer target_name)
2021-07-19 05:50:51 +08:00
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
2020-05-02 16:42:08 +08:00
message(FATAL_ERROR "Sanitizer supported only for gcc/clang")
2021-07-19 05:50:51 +08:00
endif()
2020-05-02 16:42:08 +08:00
message(STATUS "Address sanitizer enabled")
target_compile_options(${target_name} PRIVATE -fsanitize=address,undefined)
target_compile_options(${target_name} PRIVATE -fno-sanitize=signed-integer-overflow)
target_compile_options(${target_name} PRIVATE -fno-sanitize-recover=all)
target_compile_options(${target_name} PRIVATE -fno-omit-frame-pointer)
target_link_libraries(${target_name} PRIVATE -fsanitize=address,undefined)
2020-05-02 16:38:14 +08:00
endfunction()
# Enable thread sanitizer (gcc/clang only)
function(spdlog_enable_thread_sanitizer target_name)
if(NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
message(FATAL_ERROR "Sanitizer supported only for gcc/clang")
endif()
message(STATUS "Thread sanitizer enabled")
target_compile_options(${target_name} PRIVATE -fsanitize=thread)
target_compile_options(${target_name} PRIVATE -fno-omit-frame-pointer)
target_link_libraries(${target_name} PRIVATE -fsanitize=thread)
endfunction()