update cmake to work with linux, plus some cleanup

This commit is contained in:
veclav talica 2024-08-27 13:05:10 +03:00
parent cee8d5f50f
commit 1bb33d3f34

View File

@ -2,18 +2,22 @@ cmake_minimum_required(VERSION 3.21)
project(townengine LANGUAGES C)
# dependencies
# SDL dependencies
find_package(SDL2 REQUIRED GLOBAL)
find_package(SDL2_image REQUIRED GLOBAL)
find_package(SDL2_ttf REQUIRED GLOBAL)
# CMake actually has no default configuration and it's toolchain file dependent, set debug if not specified
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(TWN_TARGET townengine CACHE INTERNAL "")
set(TWN_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")
# feature configuration, set them with -DFEATURE=ON/OFF in cli
option(TWN_FEATURE_DYNLIB_GAME "Enable dynamic library loading support" ON)
option(TWN_ARCHIVE_DATA "Enable archival of assets" OFF)
@ -31,8 +35,8 @@ set(PHYSFS_ARCHIVE_QPAK OFF)
set(PHYSFS_ARCHIVE_SLB OFF)
set(PHYSFS_ARCHIVE_ISO9660 OFF)
set(PHYSFS_ARCHIVE_VDF OFF)
add_subdirectory(third-party/physfs)
add_subdirectory(third-party/libxm)
add_subdirectory(third-party/physfs SYSTEM)
add_subdirectory(third-party/libxm SYSTEM)
if(UNIX)
@ -78,11 +82,12 @@ set_target_properties(${TWN_TARGET} PROPERTIES
C_STANDARD_REQUIRED ON
C_EXTENSIONS ON) # extensions are required by stb_ds.h
# precompile commonly used not-so-small headers
target_precompile_headers(${TWN_TARGET} PRIVATE
third-party/glad/include/glad/glad.h
third-party/stb/stb_ds.h)
# distribution definitions
# distribution definitions, set them with -DX=X in cli
set(ORGANIZATION_NAME "wanp" CACHE STRING
"App developer/publisher's identifier")
set(APP_NAME "townengine" CACHE STRING
@ -90,17 +95,8 @@ set(APP_NAME "townengine" CACHE STRING
set(PACKAGE_EXTENSION "btw" CACHE STRING
"File extension used to look for data archives")
function(give_options target)
if(MSVC)
# close enough i say
target_compile_options(${target} PRIVATE
/W4
$<$<CONFIG:Release>:/GL>)
target_link_options(${target} PRIVATE
$<$<CONFIG:Release>:/LTCG>)
else()
set(WARNING_FLAGS_CLANG
-Weverything
-Wno-padded
@ -158,8 +154,6 @@ function(give_options target)
$<$<CONFIG:Release>:${BUILD_FLAGS_RELEASE}>
$<$<CONFIG:Debug>:${BUILD_FLAGS_DEBUG}>)
endif()
target_compile_definitions(${target} PRIVATE
ORGANIZATION_NAME="${ORGANIZATION_NAME}"
APP_NAME="${APP_NAME}"
@ -217,6 +211,7 @@ function(use_townengine target sources output_directory data_dir)
set_target_properties(${TWN_TARGET} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${output_directory})
else()
# build and link all statically
add_executable(${target}_app ${sources})
endif()
@ -237,12 +232,15 @@ function(use_townengine target sources output_directory data_dir)
set_target_properties(${target}_app PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${output_directory})
if(WIN32)
# copy dlls for baby windows
add_custom_command(TARGET ${target}_app POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_RUNTIME_DLLS:${target}_app>
$<TARGET_FILE_DIR:${target}_app>
COMMAND_EXPAND_LISTS)
endif()
# bootstrapping scripts are used to setup the expected state for an application
set(TWN_BOOTSTRAP_EXEC_ARGS
"$<IF:$<BOOL:${TWN_ARCHIVE_DATA}>,--data-dir ./data.${PACKAGE_EXTENSION},--data-dir ${data_dir}>")
@ -269,7 +267,7 @@ function(use_townengine target sources output_directory data_dir)
if (TWN_ARCHIVE_DATA)
# zip up assets
add_custom_command(TARGET ${target}_app POST_BUILD
add_custom_target(archive-data ALL
COMMAND cd ${data_dir} && ${CMAKE_COMMAND} -E tar "cf" ${output_directory}/data.${PACKAGE_EXTENSION} --format=zip ./
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif()
@ -284,8 +282,7 @@ if (${CMAKE_PROJECT_NAME} MATCHES townengine)
add_subdirectory(apps/testgame)
endif()
# move compie_commands.json into root directory so that it plays nicer with editors
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_custom_command(TARGET ${TWN_TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/compile_commands.json
# move compie_commands.json into root directory so that it plays nicer with code editors without any configuration
add_custom_target(copy-compile-commands ALL
${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/compile_commands.json
${TWN_ROOT_DIR})