PkmnLib/CMakeLists.txt

240 lines
9.0 KiB
CMake
Raw Normal View History

cmake_minimum_required(VERSION 3.16)
2020-12-31 12:56:36 +00:00
include(CheckIPOSupported)
include(CPM.cmake)
2019-12-29 14:29:52 +00:00
project(pkmnLib)
2020-07-18 10:42:54 +00:00
# We like new stuff, so set the c++ standard to c++20.
2020-07-04 13:50:30 +00:00
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
2019-12-29 14:29:52 +00:00
2020-07-18 10:42:54 +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." ON)
option(PKMNLIB_TESTS "Whether the test executable should be build as well." OFF)
2022-03-22 17:35:00 +00:00
option(SANITIZER_TESTS "Whether the test executable should be built using address sanitizer." ON)
2020-07-18 10:42:54 +00:00
option(STATICC "Whether gcc and stdc++ should be linked statically to the library." OFF)
option(ANGELSCRIPT_DEBUGGER "Include the angelscript debug server in the build." OFF)
2020-07-18 10:42:54 +00:00
set(SCRIPT_PROVIDER "angelscript" CACHE STRING "Which script provider to use.")
2020-09-19 10:22:52 +00:00
set(LEVEL_SIZE "8" CACHE STRING "Number of bits to store the level as. Can be 8")
2020-08-16 09:12:04 +00:00
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(-fconcepts)
endif ()
2020-09-29 16:28:54 +00:00
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_link_options(-fuse-ld=lld)
endif ()
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)
CPMAddPackage(
NAME CreatureLib
GIT_REPOSITORY https://git.p-epsilon.com/Deukhoofd/CreatureLib.git
GIT_TAG master
OPTIONS
"SHARED=${SHARED}"
"WINDOWS=${WINDOWS}"
"STATICC=${STATICC}"
)
execute_process(COMMAND ln -sf ${CreatureLib_SOURCE_DIR}/src ${CMAKE_CURRENT_BINARY_DIR}/include/CreatureLib)
if (SCRIPT_PROVIDER STREQUAL "angelscript")
CPMAddPackage(
NAME Angelscript
GIT_REPOSITORY https://git.p-epsilon.com/Deukhoofd/Angelscript.git
GIT_TAG master
DOWNLOAD_ONLY YES
)
if (Angelscript_ADDED)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . -B ${Angelscript_BINARY_DIR}
-DBUILD_SHARED_LIBS=${SHARED} -DMSVC=${WINDOWS} -DLINK_STD_STATICALLY=${STATICC} -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON
RESULT_VARIABLE result
WORKING_DIRECTORY ${Angelscript_SOURCE_DIR}/angelscript/projects/cmake)
execute_process(COMMAND ${CMAKE_COMMAND} --build ${Angelscript_BINARY_DIR}
RESULT_VARIABLE result
WORKING_DIRECTORY ${Angelscript_SOURCE_DIR}/angelscript/projects/cmake)
2022-03-23 18:16:27 +00:00
endif ()
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)
include_directories(${Angelscript_SOURCE_DIR}/angelscript/include)
2022-02-11 11:17:27 +00:00
include_directories(${Angelscript_SOURCE_DIR}/add_on)
2022-03-23 18:16:27 +00:00
endif ()
2019-12-29 14:29:52 +00:00
if (WINDOWS)
2020-01-01 18:59:47 +00:00
SET(CMAKE_SYSTEM_NAME Windows)
2019-12-29 14:29:52 +00:00
ADD_DEFINITIONS(-D WINDOWS=1)
2020-07-18 10:42:54 +00:00
endif (WINDOWS)
2019-12-29 14:29:52 +00:00
2020-07-18 11:44:52 +00:00
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# The angelscript libraries break these warnings extensively. Turn them off.
add_compile_options(-Wno-error=cast-function-type -Wno-cast-function-type)
add_compile_options(-Wno-error=type-limits -Wno-type-limits)
2020-07-18 11:44:52 +00:00
endif ()
message(STATUS "Using:
\t C ${CMAKE_C_COMPILER}
\t C++ ${CMAKE_CXX_COMPILER}
\t CXX ABI ${CMAKE_CXX_COMPILER_ABI}
\t C++ Version ${CMAKE_CXX_STANDARD}")
2020-09-29 16:28:54 +00:00
2020-08-22 10:24:52 +00:00
if (LEVEL_SIZE STREQUAL "8")
add_definitions(-DLEVEL_U8)
2020-09-29 16:28:54 +00:00
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")
2020-08-22 10:24:52 +00:00
else ()
message(FATAL_ERROR, "Invalid level size was given.")
endif ()
if (CMAKE_BUILD_TYPE MATCHES Release AND NOT WINDOWS)
2020-08-16 16:03:26 +00:00
# Include debug symbols in all linux builds
message("Including debug symbols")
add_compile_options(-g -gline-tables-only)
2020-08-16 16:03:26 +00:00
endif ()
2020-07-18 10:42:54 +00:00
# Set whether we want a static or shared library.
set(LIBTYPE STATIC)
if (SHARED)
set(LIBTYPE SHARED)
endif (SHARED)
2019-12-29 14:29:52 +00:00
2020-01-12 17:20:59 +00:00
SET(FILE_SOURCE
"src/Battling/*.cpp"
"src/Battling/*.hpp"
"src/Library/*.cpp"
"src/Library/*.hpp"
"CInterface/Core.*"
"CInterface/Library/*.cpp"
"CInterface/Library/*.hpp"
"CInterface/Battling/*.cpp"
"CInterface/Battling/*.hpp"
2020-01-12 17:20:59 +00:00
)
if (SCRIPT_PROVIDER STREQUAL "angelscript")
SET(FILE_SOURCE ${FILE_SOURCE}
2020-02-02 11:23:50 +00:00
"src/ScriptResolving/AngelScript/*.cpp"
"src/ScriptResolving/AngelScript/*.hpp"
2022-02-11 11:17:27 +00:00
"${Angelscript_SOURCE_DIR}/add_on/scriptbuilder/scriptbuilder.cpp"
"${Angelscript_SOURCE_DIR}/add_on/scripthelper/scripthelper.cpp"
"${Angelscript_SOURCE_DIR}/add_on/scripthandle/scripthandle.cpp"
"${Angelscript_SOURCE_DIR}/add_on/scriptstdstring/scriptstdstring.cpp"
"${Angelscript_SOURCE_DIR}/add_on/scriptdictionary/scriptdictionary.cpp"
"${Angelscript_SOURCE_DIR}/add_on/scriptarray/scriptarray.cpp"
2022-02-11 11:22:58 +00:00
"${Angelscript_SOURCE_DIR}/add_on/scriptmath/scriptmath.cpp"
"CInterface/AngelScript/*.cpp"
"CInterface/AngelScript/*.hpp"
)
if (ANGELSCRIPT_DEBUGGER)
2022-02-11 11:17:27 +00:00
CPMAddPackage(
2022-03-23 18:16:27 +00:00
NAME AngelscriptDebugger
GIT_REPOSITORY https://git.p-epsilon.com/Deukhoofd/AngelscriptDebuggerServer.git
GIT_TAG master
2022-02-11 11:17:27 +00:00
)
execute_process(COMMAND ln -sf ${AngelscriptDebugger_SOURCE_DIR}/src ${CMAKE_CURRENT_BINARY_DIR}/include/AngelscriptDebuggerServer)
set(_LINKS ${_LINKS} AngelscriptDebugger)
set(_TESTLINKS ${_TESTLINKS} AngelscriptDebugger)
2022-03-23 18:16:27 +00:00
endif ()
2020-02-06 15:25:55 +00:00
ADD_DEFINITIONS(-D AS_USE_ACCESSORS=1)
endif ()
2020-01-12 17:20:59 +00:00
2020-07-18 10:42:54 +00:00
file(GLOB_RECURSE CORE_SRC_FILES ${FILE_SOURCE})
add_library(pkmnLib ${LIBTYPE} ${CORE_SRC_FILES})
# Enable all warnings, and make them error when occurring.
target_compile_options(pkmnLib PRIVATE -Wall -Wextra -Werror)
target_link_directories(pkmnLib PUBLIC ${Angelscript_BINARY_DIR})
2020-01-12 17:20:59 +00:00
2020-12-31 12:56:36 +00:00
# If interprocedural optimization is available, apply it
check_ipo_supported(RESULT IPO_SUPPORTED)
if (IPO_SUPPORTED)
message(STATUS "IPO / LTO enabled")
set_property(TARGET pkmnLib PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
endif ()
2022-02-11 11:17:27 +00:00
SET(_LINKS ${_LINKS} CreatureLib Arbutils)
SET(_TESTLINKS ${_TESTLINKS} pkmnLib CreatureLib Arbutils)
2020-01-12 17:20:59 +00:00
if (SCRIPT_PROVIDER STREQUAL "angelscript")
2020-07-18 10:42:54 +00:00
message(STATUS "Using Angelscript as script provider.")
ADD_DEFINITIONS(-D ANGELSCRIPT=1)
2020-08-16 16:03:26 +00:00
SET(_LINKS ${_LINKS} angelscript)
SET(_TESTLINKS ${_TESTLINKS} angelscript)
if (ANGELSCRIPT_DEBUGGER)
ADD_DEFINITIONS(-D ANGELSCRIPT_DEBUGGER=1)
2022-03-23 18:16:27 +00:00
endif ()
2020-07-18 10:42:54 +00:00
endif ()
2020-01-12 17:20:59 +00:00
2020-07-31 15:38:28 +00:00
# If we are building for Windows we need to set some specific variables.
if (WINDOWS)
2020-07-31 15:38:28 +00:00
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")
2020-12-07 17:01:10 +00:00
if (SHARED)
set_target_properties(pkmnLib PROPERTIES SUFFIX ".dll")
2021-10-22 13:11:52 +00:00
endif (SHARED)
2022-02-12 13:32:15 +00:00
set(_LINKS ${_LINKS} -Wl,-Bstatic -lws2_32 -lstdc++ -lpthread -Wl,-Bdynamic)
set(_TESTLINKS ${_TESTLINKS} -Wl,-Bstatic -lws2_32 -lstdc++ -lpthread -Wl,-Bdynamic)
endif (WINDOWS)
if (STATICC)
2021-10-22 13:11:52 +00:00
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")
message(STATUS "Linking C library statically")
2022-02-12 11:55:58 +00:00
set(_LINKS ${_LINKS} -static-libstdc++)
SET(_TESTLINKS ${_TESTLINKS})
2021-10-22 13:11:52 +00:00
endif ()
2022-03-05 10:03:51 +00:00
target_link_libraries(pkmnLib PUBLIC ${_LINKS})
2019-12-31 12:58:43 +00:00
if (PKMNLIB_TESTS)
2022-02-11 11:17:27 +00:00
CPMAddPackage(
NAME doctest
GITHUB_REPOSITORY doctest/doctest
2022-03-11 11:13:15 +00:00
GIT_TAG v2.4.8
2022-02-11 11:17:27 +00:00
DOWNLOAD_ONLY YES
)
2019-12-31 12:58:43 +00:00
# Create Test executable
file(GLOB_RECURSE TEST_FILES "tests/*.cpp" "tests/*.hpp")
2022-02-11 11:17:27 +00:00
add_executable(pkmnLibTests ${TEST_FILES})
# Enable all warnings, and make them error when occurring.
target_compile_options(pkmnLibTests PRIVATE -Wall -Wextra -Werror)
2020-01-12 17:20:59 +00:00
message(STATUS "${_TESTLINKS}")
target_link_libraries(pkmnLibTests PUBLIC ${_TESTLINKS})
2022-02-11 11:17:27 +00:00
target_include_directories(pkmnLibTests PUBLIC ${doctest_SOURCE_DIR}/doctest)
2019-12-31 12:58:43 +00:00
# Add a definition for the test library
target_compile_definitions(pkmnLibTests PRIVATE TESTS_BUILD)
2022-03-22 17:35:00 +00:00
if (SANITIZER_TESTS AND NOT WINDOWS)
target_compile_options(pkmnLib PRIVATE -fsanitize=address,undefined,leak,integer,nullability -fno-sanitize-recover=all -fno-sanitize=unsigned-integer-overflow)
2022-03-23 18:16:27 +00:00
target_compile_options(pkmnLibTests PRIVATE -fsanitize=address,undefined,leak,integer,nullability -fno-sanitize-recover=all)
target_link_options(pkmnLibTests PRIVATE -fsanitize=address,undefined,leak,integer,nullability -fno-sanitize-recover=all)
endif ()
2019-12-31 12:58:43 +00:00
endif ()
if (ANGELSCRIPT_DEBUGGER)
include_directories(extern/AngelscriptDebuggerServer/extern/asio-1.18.2/include)
2022-03-23 18:16:27 +00:00
endif ()