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) project(townengine LANGUAGES C)
# dependencies # SDL dependencies
find_package(SDL2 REQUIRED GLOBAL) find_package(SDL2 REQUIRED GLOBAL)
find_package(SDL2_image REQUIRED GLOBAL) find_package(SDL2_image REQUIRED GLOBAL)
find_package(SDL2_ttf REQUIRED GLOBAL) find_package(SDL2_ttf REQUIRED GLOBAL)
if (NOT CMAKE_BUILD_TYPE) # 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) set(CMAKE_BUILD_TYPE Debug)
endif () endif()
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(TWN_TARGET townengine CACHE INTERNAL "") set(TWN_TARGET townengine CACHE INTERNAL "")
set(TWN_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR} 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_FEATURE_DYNLIB_GAME "Enable dynamic library loading support" ON)
option(TWN_ARCHIVE_DATA "Enable archival of assets" OFF) 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_SLB OFF)
set(PHYSFS_ARCHIVE_ISO9660 OFF) set(PHYSFS_ARCHIVE_ISO9660 OFF)
set(PHYSFS_ARCHIVE_VDF OFF) set(PHYSFS_ARCHIVE_VDF OFF)
add_subdirectory(third-party/physfs) add_subdirectory(third-party/physfs SYSTEM)
add_subdirectory(third-party/libxm) add_subdirectory(third-party/libxm SYSTEM)
if(UNIX) if(UNIX)
@ -65,11 +69,11 @@ set(TWN_SOURCE_FILES
list(TRANSFORM TWN_SOURCE_FILES PREPEND ${TWN_ROOT_DIR}/) list(TRANSFORM TWN_SOURCE_FILES PREPEND ${TWN_ROOT_DIR}/)
# base engine code, reused for games and tools # base engine code, reused for games and tools
if (TWN_FEATURE_DYNLIB_GAME) if(TWN_FEATURE_DYNLIB_GAME)
add_library(${TWN_TARGET} SHARED ${TWN_SOURCE_FILES}) add_library(${TWN_TARGET} SHARED ${TWN_SOURCE_FILES})
else () else()
add_library(${TWN_TARGET} STATIC ${TWN_SOURCE_FILES}) add_library(${TWN_TARGET} STATIC ${TWN_SOURCE_FILES})
endif () endif()
source_group(TREE ${TWN_ROOT_DIR} FILES ${TWN_SOURCE_FILES}) source_group(TREE ${TWN_ROOT_DIR} FILES ${TWN_SOURCE_FILES})
@ -78,11 +82,12 @@ set_target_properties(${TWN_TARGET} PROPERTIES
C_STANDARD_REQUIRED ON C_STANDARD_REQUIRED ON
C_EXTENSIONS ON) # extensions are required by stb_ds.h C_EXTENSIONS ON) # extensions are required by stb_ds.h
# precompile commonly used not-so-small headers
target_precompile_headers(${TWN_TARGET} PRIVATE target_precompile_headers(${TWN_TARGET} PRIVATE
third-party/glad/include/glad/glad.h third-party/glad/include/glad/glad.h
third-party/stb/stb_ds.h) third-party/stb/stb_ds.h)
# distribution definitions # distribution definitions, set them with -DX=X in cli
set(ORGANIZATION_NAME "wanp" CACHE STRING set(ORGANIZATION_NAME "wanp" CACHE STRING
"App developer/publisher's identifier") "App developer/publisher's identifier")
set(APP_NAME "townengine" CACHE STRING set(APP_NAME "townengine" CACHE STRING
@ -90,75 +95,64 @@ set(APP_NAME "townengine" CACHE STRING
set(PACKAGE_EXTENSION "btw" CACHE STRING set(PACKAGE_EXTENSION "btw" CACHE STRING
"File extension used to look for data archives") "File extension used to look for data archives")
function(give_options target) function(give_options target)
if(MSVC) set(WARNING_FLAGS_CLANG
# close enough i say -Weverything
target_compile_options(${target} PRIVATE -Wno-padded
/W4 -Wno-declaration-after-statement
$<$<CONFIG:Release>:/GL>) -Wno-unsafe-buffer-usage
-Wno-unused-command-line-argument)
target_link_options(${target} PRIVATE set(WARNING_FLAGS
$<$<CONFIG:Release>:/LTCG>) -Wall
-Wextra
-Wpedantic
-Wshadow
-Wdouble-promotion
-Wconversion -Wno-sign-conversion
-Werror=vla
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Gnu>:-Wcast-align=strict>
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Clang>:${WARNING_FLAGS_CLANG}>)
else() set(BUILD_FLAGS
set(WARNING_FLAGS_CLANG # these SHOULDN'T break anything...
-Weverything -fno-math-errno
-Wno-padded -ffp-contract=fast
-Wno-declaration-after-statement -fno-signed-zeros
-Wno-unsafe-buffer-usage -fno-trapping-math
-Wno-unused-command-line-argument) -freciprocal-math)
set(WARNING_FLAGS set(BUILD_FLAGS_RELEASE
-Wall -O3
-Wextra -flto
-Wpedantic -mavx -mavx2
-Wshadow -Wl,--gc-sections
-Wdouble-promotion -fdata-sections
-Wconversion -Wno-sign-conversion -ffunction-sections
-Werror=vla -funroll-loops
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Gnu>:-Wcast-align=strict> -fomit-frame-pointer
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Clang>:${WARNING_FLAGS_CLANG}>) $<$<STREQUAL:${CMAKE_C_COMPILER_ID},Gnu>:-fallow-store-data-races>)
set(BUILD_FLAGS set(BUILD_FLAGS_DEBUG
# these SHOULDN'T break anything... -O0
-fno-math-errno -g3
-ffp-contract=fast -gdwarf
-fno-signed-zeros -fno-omit-frame-pointer
-fno-trapping-math -fstack-protector-all
-freciprocal-math) -fsanitize=undefined
-fsanitize=address)
set(BUILD_FLAGS_RELEASE target_compile_options(${target} PRIVATE
-O3 ${WARNING_FLAGS}
-flto ${BUILD_FLAGS}
-mavx -mavx2 $<$<CONFIG:Release>:${BUILD_FLAGS_RELEASE}>
-Wl,--gc-sections $<$<CONFIG:Debug>:${BUILD_FLAGS_DEBUG}>)
-fdata-sections
-ffunction-sections
-funroll-loops
-fomit-frame-pointer
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Gnu>:-fallow-store-data-races>)
set(BUILD_FLAGS_DEBUG target_link_options(${target} PRIVATE
-O0 ${BUILD_FLAGS}
-g3 $<$<CONFIG:Release>:${BUILD_FLAGS_RELEASE}>
-gdwarf $<$<CONFIG:Debug>:${BUILD_FLAGS_DEBUG}>)
-fno-omit-frame-pointer
-fstack-protector-all
-fsanitize=undefined
-fsanitize=address)
target_compile_options(${target} PRIVATE
${WARNING_FLAGS}
${BUILD_FLAGS}
$<$<CONFIG:Release>:${BUILD_FLAGS_RELEASE}>
$<$<CONFIG:Debug>:${BUILD_FLAGS_DEBUG}>)
target_link_options(${target} PRIVATE
${BUILD_FLAGS}
$<$<CONFIG:Release>:${BUILD_FLAGS_RELEASE}>
$<$<CONFIG:Debug>:${BUILD_FLAGS_DEBUG}>)
endif()
target_compile_definitions(${target} PRIVATE target_compile_definitions(${target} PRIVATE
ORGANIZATION_NAME="${ORGANIZATION_NAME}" ORGANIZATION_NAME="${ORGANIZATION_NAME}"
@ -198,7 +192,7 @@ endfunction()
function(use_townengine target sources output_directory data_dir) function(use_townengine target sources output_directory data_dir)
if (TWN_FEATURE_DYNLIB_GAME) if(TWN_FEATURE_DYNLIB_GAME)
# game shared library, for reloading # game shared library, for reloading
add_library(${target}_game SHARED ${sources}) add_library(${target}_game SHARED ${sources})
give_options(${target}_game) give_options(${target}_game)
@ -216,9 +210,10 @@ function(use_townengine target sources output_directory data_dir)
# put libtownengine.so alongside the binary # put libtownengine.so alongside the binary
set_target_properties(${TWN_TARGET} PROPERTIES set_target_properties(${TWN_TARGET} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${output_directory}) LIBRARY_OUTPUT_DIRECTORY ${output_directory})
else () else()
# build and link all statically
add_executable(${target}_app ${sources}) add_executable(${target}_app ${sources})
endif () endif()
set_target_properties(${target}_app PROPERTIES set_target_properties(${target}_app PROPERTIES
OUTPUT_NAME launcher OUTPUT_NAME launcher
@ -228,7 +223,7 @@ function(use_townengine target sources output_directory data_dir)
find_library(MATH_LIBRARY m) find_library(MATH_LIBRARY m)
if (MATH_LIBRARY) if (MATH_LIBRARY)
target_link_libraries(${target}_app PUBLIC ${MATH_LIBRARY}) target_link_libraries(${target}_app PUBLIC ${MATH_LIBRARY})
endif () endif()
give_options(${target}_app) give_options(${target}_app)
include_deps(${target}_app) include_deps(${target}_app)
@ -237,12 +232,15 @@ function(use_townengine target sources output_directory data_dir)
set_target_properties(${target}_app PROPERTIES set_target_properties(${target}_app PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${output_directory}) RUNTIME_OUTPUT_DIRECTORY ${output_directory})
# copy dlls for baby windows if(WIN32)
add_custom_command(TARGET ${target}_app POST_BUILD # copy dlls for baby windows
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_RUNTIME_DLLS:${target}_app> add_custom_command(TARGET ${target}_app POST_BUILD
$<TARGET_FILE_DIR:${target}_app> COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_RUNTIME_DLLS:${target}_app>
COMMAND_EXPAND_LISTS) $<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 set(TWN_BOOTSTRAP_EXEC_ARGS
"$<IF:$<BOOL:${TWN_ARCHIVE_DATA}>,--data-dir ./data.${PACKAGE_EXTENSION},--data-dir ${data_dir}>") "$<IF:$<BOOL:${TWN_ARCHIVE_DATA}>,--data-dir ./data.${PACKAGE_EXTENSION},--data-dir ${data_dir}>")
@ -256,7 +254,7 @@ function(use_townengine target sources output_directory data_dir)
CONTENT "${TWN_BOOTSTRAP_SH}" CONTENT "${TWN_BOOTSTRAP_SH}"
FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ)
if (WIN32) if(WIN32)
string(JOIN "\n" TWN_BOOTSTRAP_BAT string(JOIN "\n" TWN_BOOTSTRAP_BAT
"pushd \"%~dp0\"" "pushd \"%~dp0\""
"launcher ${TWN_BOOTSTRAP_EXEC_ARGS}" "launcher ${TWN_BOOTSTRAP_EXEC_ARGS}"
@ -265,14 +263,14 @@ function(use_townengine target sources output_directory data_dir)
FILE(GENERATE OUTPUT ${output_directory}/${target}.bat FILE(GENERATE OUTPUT ${output_directory}/${target}.bat
CONTENT "${TWN_BOOTSTRAP_BAT}" CONTENT "${TWN_BOOTSTRAP_BAT}"
FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ) FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ)
endif () endif()
if (TWN_ARCHIVE_DATA) if (TWN_ARCHIVE_DATA)
# zip up assets # 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 ./ COMMAND cd ${data_dir} && ${CMAKE_COMMAND} -E tar "cf" ${output_directory}/data.${PACKAGE_EXTENSION} --format=zip ./
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif () endif()
endfunction() endfunction()
give_options(${TWN_TARGET}) give_options(${TWN_TARGET})
@ -280,12 +278,11 @@ include_deps(${TWN_TARGET})
link_deps(${TWN_TARGET}) link_deps(${TWN_TARGET})
# build the testgame if this cmake list is built directly # build the testgame if this cmake list is built directly
if (${CMAKE_PROJECT_NAME} MATCHES townengine) if(${CMAKE_PROJECT_NAME} MATCHES townengine)
add_subdirectory(apps/testgame) add_subdirectory(apps/testgame)
endif () endif()
# move compie_commands.json into root directory so that it plays nicer with editors # move compie_commands.json into root directory so that it plays nicer with code editors without any configuration
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) add_custom_target(copy-compile-commands ALL
add_custom_command(TARGET ${TWN_TARGET} POST_BUILD ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/compile_commands.json
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_BINARY_DIR}/compile_commands.json ${TWN_ROOT_DIR})
${TWN_ROOT_DIR})