Arbutils/CMakeLists.txt

148 lines
5.2 KiB
CMake

cmake_minimum_required(VERSION 3.16)
project(Arbutils)
include(CPM.cmake)
# Enable all warnings, and make them error when occurring.
add_compile_options(-Wall -Wextra -Werror -pedantic-errors)
# We like new stuff, so set the c++ standard to c++20.
set(CMAKE_CXX_STANDARD 20)
if (NOT WINDOWS)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
else ()
set(CMAKE_POSITION_INDEPENDENT_CODE OFF)
endif ()
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)
option(ARBUTILS_TESTS "Whether the test executable should be build as well." OFF)
option(SANITIZER_TESTS "Whether the test executable should be built using address sanitizer." ON)
option(STATICC "Whether gcc and stdc++ should be linked statically to the library." OFF)
option(PRETTYTRACES "Whether full stacktraces should be included. Note that this adds a dependency to libdw." ON)
option(SIGNAL_HANDLING "whether to include signal handling." OFF)
# Set whether we want a static or shared library.
set(LIBTYPE STATIC)
if (SHARED)
set(LIBTYPE SHARED)
endif (SHARED)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if (EXISTS /usr/bin/mold)
message(STATUS "Mold found, using as linker")
add_link_options(-fuse-ld=mold)
else()
add_link_options(-fuse-ld=lld)
endif()
# Only warn for unknown sanitizers. This error is not major enough to error on.
add_compile_options(-Wno-error=unknown-sanitizers -Wno-nullability-extension -Wno-gnu-zero-variadic-macro-arguments)
# Ignore pedantic nullability extension warning, as we only use this for clang specifically
add_compile_options(-Wno-nullability-extension)
# As far as I can tell this is an invalid pedantic warning since C++ 20. Empty variadic macro arguments is completely legal.
add_compile_options(-Wno-gnu-zero-variadic-macro-arguments)
endif ()
if (CMAKE_BUILD_TYPE MATCHES Release)
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
endif ()
# Grab all cpp and hpp files in our source directories.
file(GLOB_RECURSE SRC_FILES "src/*.cpp" "src/*.hpp" "CInterface/*.cpp" "CInterface/*.hpp")
add_library(Arbutils ${LIBTYPE} ${SRC_FILES})
CPMAddPackage(
NAME backward
GITHUB_REPOSITORY bombela/backward-cpp
VERSION 1.6
DOWNLOAD_ONLY YES
)
CPMAddPackage(
NAME pcg-cpp
GITHUB_REPOSITORY imneme/pcg-cpp
GIT_TAG master
DOWNLOAD_ONLY YES
)
target_include_directories(Arbutils PUBLIC ${backward_SOURCE_DIR})
target_include_directories(Arbutils PUBLIC ${pcg-cpp_SOURCE_DIR}/include)
# If we are building for Windows we need to set some specific variables.
if (WINDOWS)
MESSAGE(WARNING, "Using Windows Build.")
# 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)
if (SHARED)
set_target_properties(Arbutils PROPERTIES SUFFIX ".dll")
endif ()
endif (WINDOWS)
if (CMAKE_BUILD_TYPE MATCHES "Debug")
ADD_DEFINITIONS(-DDEBUG)
endif ()
set(LINKS)
if (PRETTYTRACES)
if (NOT WINDOWS)
set(LINKS ${LINKS} -ldw)
else ()
set(LINKS ${LINKS} -ldbghelp -lpsapi)
endif ()
ADD_DEFINITIONS(-DPRETTYTRACES=1)
endif ()
if (SIGNAL_HANDLING)
ADD_DEFINITIONS(-DSIGNAL_HANDLING=1)
endif ()
# If we want to link the C and C++ library statically, link those as well.
if (STATICC)
message("Linking dependencies statically.")
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")
set(LINKS -static-libgcc -static-libstdc++ ${LINKS})
endif (STATICC)
if (WINDOWS)
set(LINKS ${LINKS} -Wl,-Bstatic -lm -lstdc++ -lpthread -Wl,-Bdynamic)
endif ()
target_link_libraries(Arbutils ${LINKS})
if (ARBUTILS_TESTS)
CPMAddPackage(
NAME doctest
GITHUB_REPOSITORY doctest/doctest
GIT_TAG v2.4.8
DOWNLOAD_ONLY YES
)
# If we want a tests executable, grab all tests source files
file(GLOB_RECURSE TEST_FILES "tests/*.cpp" "tests/*.hpp")
# And create an executable from it. Also include doctest.hpp.
add_executable(ArbutilsTests ${TEST_FILES})
target_include_directories(ArbutilsTests PUBLIC ${doctest_SOURCE_DIR}/doctest)
# 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)
if (SANITIZER_TESTS AND NOT WINDOWS)
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 )
endif()
endif ()
# If we aren't building for Windows, also include an option for installing on unix.
if (NOT WINDOWS)
install(TARGETS Arbutils
CONFIGURATIONS Release
RUNTIME DESTINATION lib)
endif ()