CreatureLib/CMakeLists.txt

146 lines
5.4 KiB
CMake

cmake_minimum_required(VERSION 3.16)
include(CheckIPOSupported)
include(CPM.cmake)
project(CreatureLib)
# 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)
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(CREATURELIB_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)
set(LEVEL_SIZE "8" CACHE STRING "Number of bits to store the level as. Currently reserved, and can only be 8")
execute_process(COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/include)
CPMAddPackage(
NAME Arbutils
GIT_REPOSITORY https://git.p-epsilon.com/Deukhoofd/Arbutils.git
GIT_TAG master
OPTIONS
"SHARED=${SHARED}"
"WINDOWS=${WINDOWS}"
"STATICC=${STATICC}"
)
execute_process(COMMAND ln -sf ${Arbutils_SOURCE_DIR}/src ${CMAKE_CURRENT_BINARY_DIR}/include/Arbutils)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(-fconcepts)
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 ()
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
# 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 -flto)
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)
# 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 (LEVEL_SIZE STREQUAL "8")
add_definitions(-DLEVEL_U8)
message(STATUS "Using level size of 8")
elseif (LEVEL_SIZE STREQUAL "16")
add_definitions(-DLEVEL_U16)
message(STATUS "Using level size of 16")
elseif (LEVEL_SIZE STREQUAL "32")
add_definitions(-DLEVEL_U32)
message(STATUS "Using level size of 32")
elseif (LEVEL_SIZE STREQUAL "64")
add_definitions(-DLEVEL_U64)
message(STATUS "Using level size of 64")
else ()
message(FATAL_ERROR, "Invalid level size was given.")
endif ()
file(GLOB_RECURSE LIBRARY_SRC_FILES
"src/*.cpp" "src/*.hpp" "CInterface/*.cpp" "CInterface/*.hpp")
add_library(CreatureLib ${LIBTYPE} ${LIBRARY_SRC_FILES})
# If interprocedural optimization is available, apply it
check_ipo_supported(RESULT IPO_SUPPORTED)
if (IPO_SUPPORTED)
message(STATUS "IPO / LTO enabled")
set_property(TARGET CreatureLib PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif ()
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)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,-allow-multiple-definition")
if (SHARED)
set_target_properties(CreatureLib PROPERTIES SUFFIX ".dll")
endif (SHARED)
endif (WINDOWS)
# Set up links to all relevant libraries.
SET(_LIBRARYLINKS Arbutils)
if (NOT WINDOWS)
set(_LIBRARYLINKS ${_LIBRARYLINKS} -ldw)
endif ()
# If we need to link the C libraries statically, do so.
if (STATICC)
message(STATUS "Linking C statically.")
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")
SET(_LIBRARYLINKS ${_LIBRARYLINKS} -static-libgcc -static-libstdc++)
endif ()
# And link the libraries together
target_link_libraries(CreatureLib PUBLIC ${_LIBRARYLINKS})
if (CREATURELIB_TESTS)
CPMAddPackage(
NAME doctest
GITHUB_REPOSITORY doctest/doctest
GIT_TAG v2.4.8
DOWNLOAD_ONLY YES
)
# Create Test executable
file(GLOB_RECURSE TEST_FILES "tests/*.cpp" "tests/*.hpp")
add_executable(CreatureLibTests ${TEST_FILES})
target_link_libraries(CreatureLibTests PUBLIC CreatureLib Arbutils)
target_include_directories(CreatureLibTests PUBLIC ${doctest_SOURCE_DIR}/doctest)
# Add a definition for the test library
target_compile_definitions(CreatureLibTests PRIVATE TESTS_BUILD)
if (SANITIZER_TESTS AND NOT WINDOWS)
target_compile_options(CreatureLib PRIVATE -fsanitize=address,undefined,leak,integer,nullability -fno-sanitize-recover=all)
target_compile_options(CreatureLibTests PRIVATE -fsanitize=address,undefined,leak,integer,nullability -fno-sanitize-recover=all)
target_link_options(CreatureLibTests PRIVATE -fsanitize=address,undefined,leak,integer,nullability -fno-sanitize-recover=all)
endif ()
endif ()