libfort/CMakeLists.txt

271 lines
8.9 KiB
CMake
Raw Normal View History

cmake_minimum_required(VERSION 3.0)
2019-04-15 20:38:07 +02:00
2019-07-26 21:27:32 +02:00
project(libfort VERSION 0.1.6)
string(REGEX REPLACE "([0-9]+)\\.([0-9]+)\\.([0-9]+)"
2019-05-26 09:22:12 +02:00
"\\1.\\2" libfort_SOVERSION
${libfort_VERSION})
2018-03-31 12:33:37 +02:00
include(${CMAKE_ROOT}/Modules/ExternalProject.cmake)
2018-01-01 09:26:34 +01:00
2019-04-26 22:41:28 +02:00
# ------------------------------------------------------------------------------
# Build options
# ------------------------------------------------------------------------------
2018-03-31 12:33:37 +02:00
option(FORT_ENABLE_ASTYLE "Enable astyle" OFF)
option(FORT_ENABLE_WCHAR "Enable wchar support" ON)
2019-04-21 09:29:02 +02:00
set(FORT_BUILD_TYPE "common" CACHE STRING "Build type")
set_property(CACHE FORT_BUILD_TYPE PROPERTY STRINGS
common asan ubsan coveralls)
2018-03-18 14:44:49 +01:00
2018-04-01 12:40:56 +02:00
# Determine compiler (pos. values Clang, GNU, Intel, MSVC, AppleClang...
# (https://cmake.org/cmake/help/v3.0/variable/CMAKE_LANG_COMPILER_ID.html)
2018-03-18 17:02:53 +01:00
if(FORT_CXX_BUILD)
set(FORT_COMPILER ${CMAKE_CXX_COMPILER_ID})
2019-04-12 20:25:23 +02:00
else()
2018-03-18 17:02:53 +01:00
set(FORT_COMPILER ${CMAKE_C_COMPILER_ID})
2019-04-12 20:25:23 +02:00
endif()
2018-03-18 17:02:53 +01:00
2018-03-18 14:44:49 +01:00
2018-01-01 09:26:34 +01:00
set(CMAKE_VERBOSE_MAKEFILE ON)
2019-05-19 18:09:11 +02:00
message(STATUS "libfort build options: "
" wchar support = ${FORT_ENABLE_WCHAR}; "
" build type = ${FORT_BUILD_TYPE}")
# for fort.hpp
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
2018-03-31 12:33:37 +02:00
# ------------------------------------------------------------------------------
# Warnings
# ------------------------------------------------------------------------------
2018-04-16 20:39:29 +02:00
if("${FORT_COMPILER}" STREQUAL "MSVC")
2018-03-18 14:56:14 +01:00
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W4")
2018-04-01 15:20:15 +02:00
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W4")
2019-04-12 20:25:23 +02:00
else()
2018-11-17 08:44:35 +01:00
set(ADDITIONAL_WARNINGS "\
2019-04-12 20:30:13 +02:00
-Wall \
-Wextra \
2018-11-17 08:44:35 +01:00
-Wdouble-promotion \
-Wshadow \
-Wformat=2 \
-Wno-variadic-macros \
-Wcast-align \
-Wstrict-aliasing=2 \
-Wstrict-overflow=5 \
-Wfloat-equal \
-Wwrite-strings \
2018-11-17 08:44:35 +01:00
")
if("${FORT_COMPILER}" STREQUAL "GNU")
2019-04-12 20:25:23 +02:00
set(ADDITIONAL_WARNINGS "${ADDITIONAL_WARNINGS} \
2019-04-12 20:30:13 +02:00
-Wtrampolines \
2019-04-12 20:25:23 +02:00
-Wlogical-op")
endif()
2018-11-17 08:44:35 +01:00
2019-04-12 20:25:23 +02:00
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${ADDITIONAL_WARNINGS} -Wpedantic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ADDITIONAL_WARNINGS}")
endif()
2019-04-21 11:31:45 +02:00
# ------------------------------------------------------------------------------
# Sanitizers
# ------------------------------------------------------------------------------
if("${FORT_COMPILER}" STREQUAL "GNU" OR "${FORT_COMPILER}" STREQUAL "Clang")
# asan case
if(FORT_BUILD_TYPE STREQUAL "asan")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
if("${FORT_COMPILER}" STREQUAL "GNU")
2019-04-26 22:20:43 +02:00
set(FORT_LINK_LIBRARIES asan)
2019-04-21 11:31:45 +02:00
endif()
endif()
#ubsan case
if(FORT_BUILD_TYPE STREQUAL "ubsan")
# -fno-sanitize-recover option is used to force faild
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
if("${FORT_COMPILER}" STREQUAL "GNU")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize-recover")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize-recover")
2019-04-26 22:20:43 +02:00
set(FORT_LINK_LIBRARIES ubsan)
2019-04-26 22:11:09 +02:00
2019-04-21 11:31:45 +02:00
elseif("${FORT_COMPILER}" STREQUAL "Clang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize-recover=all")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize-recover=all")
endif()
endif()
#coveralls case
if(FORT_BUILD_TYPE STREQUAL "coveralls")
set(COVERAGE_FLAGS "-g -fprofile-arcs -ftest-coverage")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_FLAGS}")
#target_link_libraries(${PROJECT_NAME}_example_cpp gcov)
endif()
endif()
# Hack for some gcc versions
2019-05-24 19:25:13 +02:00
if("${FORT_COMPILER}" STREQUAL "GNU"
AND NOT "${FORT_BUILD_TYPE}" STREQUAL "common")
2019-04-21 11:31:45 +02:00
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fuse-ld=gold")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fuse-ld=gold")
endif()
2019-04-27 15:00:20 +02:00
# ------------------------------------------------------------------------------
# Disable NDEBUG for all builds
# ------------------------------------------------------------------------------
foreach(flags_var_to_scrub
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS_MINSIZEREL
)
2019-04-27 15:21:24 +02:00
string(REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
2019-04-27 15:00:20 +02:00
"${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
endforeach()
2019-04-26 22:11:09 +02:00
# ------------------------------------------------------------------------------
# Add WChar support for the build
# ------------------------------------------------------------------------------
if(FORT_ENABLE_WCHAR)
add_definitions(-DFT_CONGIG_HAVE_WCHAR)
endif()
set(FORT_HAVE_WCHAR "${FORT_ENABLE_WCHAR}" CACHE STRING "fort option")
# ------------------------------------------------------------------------------
# Subdirectories
# ------------------------------------------------------------------------------
add_subdirectory(lib)
add_subdirectory(src)
add_subdirectory(tests)
# ------------------------------------------------------------------------------
# Sources and executables
# ------------------------------------------------------------------------------
file(GLOB_RECURSE FortHeaders
"lib/*.h"
"lib/*.hpp"
"tests/*.h"
"tests/*.hpp"
"src/*.h")
add_custom_target(headers SOURCES ${FortHeaders})
add_executable(${PROJECT_NAME}_example
2019-04-26 22:20:43 +02:00
example/main.c)
2019-04-26 22:11:09 +02:00
target_link_libraries(${PROJECT_NAME}_example
fort)
add_executable(${PROJECT_NAME}_example_cpp
example/main.cpp)
target_link_libraries(${PROJECT_NAME}_example_cpp
fort)
2019-04-26 22:20:43 +02:00
if(DEFINED FORT_LINK_LIBRARIES)
target_link_libraries(${PROJECT_NAME}_example_cpp "${FORT_LINK_LIBRARIES}")
target_link_libraries(${PROJECT_NAME}_example "${FORT_LINK_LIBRARIES}")
2019-04-26 22:11:09 +02:00
endif()
2018-09-01 18:42:57 +02:00
# ------------------------------------------------------------------------------
# Set preprocessor macros for all test builds
# ------------------------------------------------------------------------------
2019-04-26 22:11:09 +02:00
2018-09-01 18:42:57 +02:00
target_compile_definitions(${PROJECT_NAME}_test_dev PRIVATE FT_TEST_BUILD=1)
target_compile_definitions(${PROJECT_NAME}_test_cpp PRIVATE FT_TEST_BUILD=1)
target_compile_definitions(${PROJECT_NAME}_test PRIVATE FT_TEST_BUILD=1)
2018-03-18 13:32:02 +01:00
2018-03-31 12:33:37 +02:00
# ------------------------------------------------------------------------------
# Astyle
# ------------------------------------------------------------------------------
if(FORT_ENABLE_ASTYLE)
list(APPEND ASTYLE_CMAKE_ARGS
"-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}"
)
2019-04-12 20:25:23 +02:00
externalproject_add(
2018-03-31 12:33:37 +02:00
astyle
GIT_REPOSITORY https://github.com/Bareflank/astyle.git
GIT_TAG v1.2
GIT_SHALLOW 1
CMAKE_ARGS ${ASTYLE_CMAKE_ARGS}
PREFIX ${CMAKE_BINARY_DIR}/external/astyle/prefix
TMP_DIR ${CMAKE_BINARY_DIR}/external/astyle/tmp
STAMP_DIR ${CMAKE_BINARY_DIR}/external/astyle/stamp
DOWNLOAD_DIR ${CMAKE_BINARY_DIR}/external/astyle/download
SOURCE_DIR ${CMAKE_BINARY_DIR}/external/astyle/src
BINARY_DIR ${CMAKE_BINARY_DIR}/external/astyle/build
)
list(APPEND ASTYLE_ARGS
--style=kr
--lineend=linux
--suffix=none
--pad-oper
--unpad-paren
--align-pointer=name
--align-reference=name
--indent-switches
--keep-one-line-statements
--keep-one-line-blocks
--pad-header
--convert-tabs
--min-conditional-indent=0
--indent=spaces=4
2018-05-06 15:50:08 +02:00
${CMAKE_SOURCE_DIR}/lib/*.h
${CMAKE_SOURCE_DIR}/lib/*.c
${CMAKE_SOURCE_DIR}/src/*.h
2018-03-31 12:33:37 +02:00
${CMAKE_SOURCE_DIR}/src/*.c
${CMAKE_SOURCE_DIR}/tests/*.c
2018-05-06 15:50:08 +02:00
${CMAKE_SOURCE_DIR}/tests/*.h
2018-11-24 21:14:26 +01:00
${CMAKE_SOURCE_DIR}/tests/*.hpp
2018-05-06 15:50:08 +02:00
${CMAKE_SOURCE_DIR}/tests/bb_tests/*.c
${CMAKE_SOURCE_DIR}/tests/wb_tests/*.c
${CMAKE_SOURCE_DIR}/tests/bb_tests_cpp/*.cpp
2018-03-31 12:33:37 +02:00
)
if(NOT WIN32 STREQUAL "1")
add_custom_target(
format
COMMAND ${CMAKE_BINARY_DIR}/bin/astyle ${ASTYLE_ARGS}
COMMENT "running astyle"
)
else()
add_custom_target(
format
COMMAND ${CMAKE_BINARY_DIR}/bin/astyle.exe ${ASTYLE_ARGS}
COMMENT "running astyle"
)
endif()
endif()
2019-04-21 09:29:02 +02:00
# ------------------------------------------------------------------------------
# Tests
# ------------------------------------------------------------------------------
enable_testing()
add_test(NAME ${PROJECT_NAME}_example COMMAND ${PROJECT_NAME}_example)
add_test(NAME ${PROJECT_NAME}_example_cpp COMMAND ${PROJECT_NAME}_example_cpp)
add_test(NAME ${PROJECT_NAME}_test COMMAND ${PROJECT_NAME}_test)
add_test(NAME ${PROJECT_NAME}_test_cpp COMMAND ${PROJECT_NAME}_test_cpp)
add_test(NAME ${PROJECT_NAME}_test_dev COMMAND ${PROJECT_NAME}_test_dev)