2020-09-25 09:06:55 +00:00
|
|
|
cmake_minimum_required(VERSION 3.16)
|
2020-02-26 11:57:18 +00:00
|
|
|
project(Arbutils)
|
|
|
|
|
2022-02-05 12:37:47 +00:00
|
|
|
include(CPM.cmake)
|
|
|
|
|
2020-07-17 10:15:32 +00:00
|
|
|
# Enable all warnings, and make them error when occurring.
|
|
|
|
add_compile_options(-Wall -Wextra -Werror)
|
|
|
|
# We like new stuff, so set the c++ standard to c++20.
|
2020-06-26 13:56:00 +00:00
|
|
|
set(CMAKE_CXX_STANDARD 20)
|
2021-08-22 09:35:08 +00:00
|
|
|
|
|
|
|
if (NOT WINDOWS)
|
|
|
|
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
2022-02-26 13:55:28 +00:00
|
|
|
else ()
|
2021-08-22 09:35:08 +00:00
|
|
|
set(CMAKE_POSITION_INDEPENDENT_CODE OFF)
|
2022-02-26 13:55:28 +00:00
|
|
|
endif ()
|
2020-02-26 11:57:18 +00:00
|
|
|
|
2020-07-17 10:15:32 +00:00
|
|
|
option(WINDOWS "Whether the build target is Windows or not." OFF)
|
|
|
|
option(SHARED "Whether we should build a shared library, instead of a static one." OFF)
|
2022-02-05 14:45:11 +00:00
|
|
|
option(ARBUTILS_TESTS "Whether the test executable should be build as well." OFF)
|
2022-03-22 11:36:52 +00:00
|
|
|
option(SANITIZER_TESTS "Whether the test executable should be built using address sanitizer." ON)
|
2020-07-17 10:15:32 +00:00
|
|
|
option(STATICC "Whether gcc and stdc++ should be linked statically to the library." OFF)
|
2020-08-17 18:08:23 +00:00
|
|
|
option(PRETTYTRACES "Whether full stacktraces should be included. Note that this adds a dependency to libdw." ON)
|
2020-10-18 15:18:27 +00:00
|
|
|
option(SIGNAL_HANDLING "whether to include signal handling." OFF)
|
2020-05-02 13:30:25 +00:00
|
|
|
|
2020-07-17 10:15:32 +00:00
|
|
|
# Set whether we want a static or shared library.
|
2020-05-02 13:30:25 +00:00
|
|
|
set(LIBTYPE STATIC)
|
2020-05-02 10:31:04 +00:00
|
|
|
if (SHARED)
|
2020-07-17 10:15:32 +00:00
|
|
|
set(LIBTYPE SHARED)
|
|
|
|
endif (SHARED)
|
2020-02-26 11:57:18 +00:00
|
|
|
|
2020-09-21 10:37:34 +00:00
|
|
|
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
|
|
|
|
add_link_options(-fuse-ld=lld)
|
2022-03-23 09:58:32 +00:00
|
|
|
# Only warn for unknown sanitizers. This error is not major enough to error on.
|
|
|
|
add_compile_options(-Wno-error=unknown-sanitizers)
|
2020-09-21 10:37:34 +00:00
|
|
|
endif ()
|
2020-09-12 14:50:36 +00:00
|
|
|
|
2022-02-26 13:55:28 +00:00
|
|
|
if (CMAKE_BUILD_TYPE MATCHES Release)
|
2022-03-11 10:32:44 +00:00
|
|
|
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
|
|
|
|
# Include debug symbols in all linux builds
|
|
|
|
message("Including debug symbols")
|
|
|
|
add_compile_options(-g -gline-tables-only)
|
|
|
|
endif ()
|
|
|
|
# FIXME: Add debug symbols for GCC
|
2020-12-31 12:48:52 +00:00
|
|
|
endif ()
|
2020-08-16 15:24:37 +00:00
|
|
|
|
2020-07-17 10:15:32 +00:00
|
|
|
# Grab all cpp and hpp files in our source directories.
|
2020-10-02 16:44:29 +00:00
|
|
|
file(GLOB_RECURSE SRC_FILES "src/*.cpp" "src/*.hpp" "CInterface/*.cpp" "CInterface/*.hpp")
|
2020-07-17 10:15:32 +00:00
|
|
|
add_library(Arbutils ${LIBTYPE} ${SRC_FILES})
|
2020-07-12 13:06:20 +00:00
|
|
|
|
2022-02-05 12:37:47 +00:00
|
|
|
|
|
|
|
CPMAddPackage(
|
|
|
|
NAME backward
|
|
|
|
GITHUB_REPOSITORY bombela/backward-cpp
|
|
|
|
VERSION 1.6
|
2022-02-05 12:48:13 +00:00
|
|
|
DOWNLOAD_ONLY YES
|
2022-02-05 12:37:47 +00:00
|
|
|
)
|
|
|
|
CPMAddPackage(
|
2022-02-26 13:55:28 +00:00
|
|
|
NAME pcg-cpp
|
|
|
|
GITHUB_REPOSITORY imneme/pcg-cpp
|
2022-02-05 12:37:47 +00:00
|
|
|
GIT_TAG master
|
2022-02-05 12:48:13 +00:00
|
|
|
DOWNLOAD_ONLY YES
|
2022-02-05 12:37:47 +00:00
|
|
|
)
|
|
|
|
target_include_directories(Arbutils PUBLIC ${backward_SOURCE_DIR})
|
|
|
|
target_include_directories(Arbutils PUBLIC ${pcg-cpp_SOURCE_DIR}/include)
|
|
|
|
|
2020-07-17 10:15:32 +00:00
|
|
|
# If we are building for Windows we need to set some specific variables.
|
2020-07-10 12:12:22 +00:00
|
|
|
if (WINDOWS)
|
|
|
|
MESSAGE(WARNING, "Using Windows Build.")
|
2020-07-17 10:15:32 +00:00
|
|
|
# Add a definition for the compiler, so we can use it in C++ as well.
|
|
|
|
ADD_DEFINITIONS(-D WINDOWS=1)
|
|
|
|
# -m64: Build a 64 bit library
|
|
|
|
add_compile_options(-m64)
|
2021-04-30 12:36:31 +00:00
|
|
|
if (SHARED)
|
|
|
|
set_target_properties(Arbutils PROPERTIES SUFFIX ".dll")
|
2022-02-26 13:55:28 +00:00
|
|
|
endif ()
|
2020-07-10 12:12:22 +00:00
|
|
|
endif (WINDOWS)
|
|
|
|
|
2020-12-23 11:04:55 +00:00
|
|
|
if (CMAKE_BUILD_TYPE MATCHES "Debug")
|
|
|
|
ADD_DEFINITIONS(-DDEBUG)
|
2022-02-26 13:55:28 +00:00
|
|
|
endif ()
|
2020-12-23 11:04:55 +00:00
|
|
|
|
2020-08-15 12:58:44 +00:00
|
|
|
set(LINKS)
|
2022-02-26 13:55:28 +00:00
|
|
|
if (PRETTYTRACES)
|
|
|
|
if (NOT WINDOWS)
|
|
|
|
set(LINKS ${LINKS} -ldw)
|
|
|
|
else ()
|
|
|
|
set(LINKS ${LINKS} -ldbghelp -lpsapi)
|
|
|
|
endif ()
|
2020-08-17 18:08:23 +00:00
|
|
|
ADD_DEFINITIONS(-DPRETTYTRACES=1)
|
2022-02-26 13:55:28 +00:00
|
|
|
endif ()
|
2020-10-18 15:18:27 +00:00
|
|
|
if (SIGNAL_HANDLING)
|
|
|
|
ADD_DEFINITIONS(-DSIGNAL_HANDLING=1)
|
2022-02-26 13:55:28 +00:00
|
|
|
endif ()
|
2020-10-18 15:18:27 +00:00
|
|
|
|
2020-07-17 10:15:32 +00:00
|
|
|
# If we want to link the C and C++ library statically, link those as well.
|
2020-07-10 12:12:22 +00:00
|
|
|
if (STATICC)
|
2021-03-26 12:37:01 +00:00
|
|
|
message("Linking dependencies statically.")
|
2022-02-26 13:55:28 +00:00
|
|
|
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")
|
2022-02-12 11:41:51 +00:00
|
|
|
set(LINKS -static-libgcc -static-libstdc++ ${LINKS})
|
2022-02-26 13:55:28 +00:00
|
|
|
endif (STATICC)
|
2022-02-12 11:41:51 +00:00
|
|
|
|
|
|
|
if (WINDOWS)
|
|
|
|
set(LINKS ${LINKS} -Wl,-Bstatic -lm -lstdc++ -lpthread -Wl,-Bdynamic)
|
2022-02-26 13:55:28 +00:00
|
|
|
endif ()
|
2022-02-12 11:41:51 +00:00
|
|
|
|
2020-08-15 12:58:44 +00:00
|
|
|
target_link_libraries(Arbutils ${LINKS})
|
2020-02-26 11:57:18 +00:00
|
|
|
|
2022-02-05 14:45:11 +00:00
|
|
|
if (ARBUTILS_TESTS)
|
2022-02-05 12:51:50 +00:00
|
|
|
CPMAddPackage(
|
|
|
|
NAME doctest
|
|
|
|
GITHUB_REPOSITORY doctest/doctest
|
2022-03-11 10:06:44 +00:00
|
|
|
GIT_TAG v2.4.8
|
2022-02-05 12:51:50 +00:00
|
|
|
DOWNLOAD_ONLY YES
|
|
|
|
)
|
|
|
|
|
2020-08-20 10:54:37 +00:00
|
|
|
# If we want a tests executable, grab all tests source files
|
|
|
|
file(GLOB_RECURSE TEST_FILES "tests/*.cpp" "tests/*.hpp")
|
2020-09-25 09:50:53 +00:00
|
|
|
# And create an executable from it. Also include doctest.hpp.
|
2022-02-05 12:37:47 +00:00
|
|
|
add_executable(ArbutilsTests ${TEST_FILES})
|
2022-02-05 12:51:50 +00:00
|
|
|
target_include_directories(ArbutilsTests PUBLIC ${doctest_SOURCE_DIR}/doctest)
|
2020-08-20 10:54:37 +00:00
|
|
|
# And finally link the library to the executable.
|
|
|
|
target_link_libraries(ArbutilsTests Arbutils ${LINKS})
|
|
|
|
# Add a compilation definition to the code that we are building a test build.
|
|
|
|
target_compile_definitions(ArbutilsTests PRIVATE TESTS_BUILD)
|
2022-03-22 11:28:42 +00:00
|
|
|
|
2022-03-22 11:42:56 +00:00
|
|
|
if (SANITIZER_TESTS AND NOT WINDOWS)
|
2022-03-23 09:53:02 +00:00
|
|
|
message(STATUS "Sanitizers included")
|
|
|
|
target_compile_options(ArbutilsTests PRIVATE -fsanitize=address,undefined,leak,integer,nullability -fno-sanitize-recover=all )
|
|
|
|
target_link_options(ArbutilsTests PRIVATE -fsanitize=address,undefined,leak,integer,nullability -fno-sanitize-recover=all )
|
2022-03-22 11:36:52 +00:00
|
|
|
endif()
|
2020-08-20 10:54:37 +00:00
|
|
|
endif ()
|
|
|
|
|
|
|
|
|
2020-07-17 10:15:32 +00:00
|
|
|
# If we aren't building for Windows, also include an option for installing on unix.
|
2020-03-11 10:50:56 +00:00
|
|
|
if (NOT WINDOWS)
|
|
|
|
install(TARGETS Arbutils
|
|
|
|
CONFIGURATIONS Release
|
|
|
|
RUNTIME DESTINATION lib)
|
2022-02-26 13:55:28 +00:00
|
|
|
endif ()
|