townengine/CMakeLists.txt

366 lines
14 KiB
CMake
Raw Normal View History

2024-07-08 00:44:20 +00:00
cmake_minimum_required(VERSION 3.21)
2024-07-29 10:06:23 +00:00
project(townengine LANGUAGES C)
2024-07-08 00:44:20 +00:00
# SDL dependencies
# for whatever reason Emscripten has SDL2 config, but not actual SDL2 port by default
if(NOT EMSCRIPTEN)
find_package(SDL2 REQUIRED GLOBAL)
endif()
2024-07-08 00:44:20 +00:00
# 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()
2024-09-27 14:49:56 +00:00
if(NOT TWN_SANITIZE AND CMAKE_BUILD_TYPE MATCHES Debug)
2024-09-26 18:44:31 +00:00
set(TWN_SANITIZE ON)
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)
# todo: figure out how to compile for dynamic linking instead
if(EMSCRIPTEN)
if(TWN_FEATURE_DYNLIB_GAME)
message(WARNING "TWN_FEATURE_DYNLIB_GAME is set, but not supported - it is turned off")
set(TWN_FEATURE_DYNLIB_GAME OFF CACHE INTERNAL "")
endif()
endif()
if(HAIKU)
if(TWN_SANITIZE)
message(WARNING "TWN_SANITIZE is set, but not supported - it is turned off")
set(TWN_SANITIZE OFF CACHE INTERNAL "")
endif()
endif()
# add -fPIC globally so that it's linked well
add_compile_options($<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:-fPIC>)
2024-08-21 13:55:34 +00:00
2024-07-08 00:44:20 +00:00
set(PHYSFS_BUILD_SHARED FALSE)
set(PHYSFS_DISABLE_INSTALL TRUE)
set(PHYSFS_TARGETNAME_UNINSTALL "physfs_uninstall")
set(PHYSFS_ARCHIVE_GRP OFF)
set(PHYSFS_ARCHIVE_WAD OFF)
set(PHYSFS_ARCHIVE_HOG OFF)
set(PHYSFS_ARCHIVE_MVL OFF)
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 ${CMAKE_CURRENT_BINARY_DIR}/third-party/physfs SYSTEM)
add_subdirectory(third-party/libxm ${CMAKE_CURRENT_BINARY_DIR}/third-party/libxm SYSTEM)
2024-07-08 00:44:20 +00:00
if(LINUX)
set(SYSTEM_SOURCE_FILES
src/system/linux/twn_elf.c
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:src/game_object/twn_linux_game_object.c>)
elseif(WIN32)
set(SYSTEM_SOURCE_FILES
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:src/game_object/twn_win32_game_object.c>)
2024-09-18 17:36:00 +00:00
else()
set(SYSTEM_SOURCE_FILES)
endif()
if(EMSCRIPTEN)
set(TWN_RENDERING_API WEBGL1)
else()
set(TWN_RENDERING_API OPENGL_15)
endif()
if(TWN_RENDERING_API MATCHES OPENGL_15)
set(SYSTEM_SOURCE_FILES ${SYSTEM_SOURCE_FILES}
src/rendering/twn_gl_any_rendering.c
src/rendering/twn_gl_15_rendering.c
src/rendering/twn_gl_15_gpu_texture.c)
endif()
2024-08-27 10:24:15 +00:00
set(TWN_THIRD_PARTY_SOURCE_FILES
2024-07-08 00:44:20 +00:00
third-party/physfs/extras/physfsrwops.c
2024-07-08 06:46:12 +00:00
third-party/stb/stb_vorbis.c
third-party/tomlc99/toml.c
$<$<NOT:$<BOOL:${EMSCRIPTEN}>>:third-party/glad/src/glad.c>)
2024-07-08 06:46:12 +00:00
2024-08-27 10:24:15 +00:00
set(TWN_SOURCE_FILES
src/twn_loop.c
src/twn_main.c
src/twn_context.c include/twn_context.h
src/twn_audio.c include/twn_audio.h
src/twn_util.c include/twn_util.h
src/twn_input.c include/twn_input.h
src/twn_camera.c include/twn_camera.h
src/twn_textures.c src/twn_textures_c.h
src/rendering/twn_rendering.c src/rendering/twn_rendering_c.h
src/rendering/twn_sprites.c
src/rendering/twn_text.c
src/rendering/twn_triangles.c
src/rendering/twn_circles.c
2024-09-26 18:02:56 +00:00
src/rendering/twn_skybox.c
2024-10-01 13:10:36 +00:00
src/rendering/twn_fog.c
# for dynamic load based solution main is compiled in a separate target
$<$<NOT:$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>>:src/twn_main.c
src/game_object/twn_static_game_object.c>
2024-08-21 13:55:34 +00:00
${SYSTEM_SOURCE_FILES})
list(TRANSFORM TWN_SOURCE_FILES PREPEND ${TWN_ROOT_DIR}/)
2024-07-08 00:44:20 +00:00
2024-08-27 10:24:15 +00:00
add_library(twn_third_parties STATIC ${TWN_THIRD_PARTY_SOURCE_FILES})
# base engine code, reused for games and tools
if(TWN_FEATURE_DYNLIB_GAME)
2024-08-27 10:24:15 +00:00
add_library(${TWN_TARGET} SHARED ${TWN_SOURCE_FILES} ${twn_third_parties})
else()
2024-08-27 10:24:15 +00:00
add_library(${TWN_TARGET} STATIC ${TWN_SOURCE_FILES} ${twn_third_parties})
endif()
2024-07-08 00:44:20 +00:00
source_group(TREE ${TWN_ROOT_DIR} FILES ${TWN_SOURCE_FILES})
set_target_properties(${TWN_TARGET} PROPERTIES
C_STANDARD 11
C_STANDARD_REQUIRED ON
C_EXTENSIONS ON) # extensions are required by stb_ds.h
2024-07-08 00:44:20 +00:00
# precompile commonly used not-so-small headers
target_precompile_headers(${TWN_TARGET} PRIVATE
$<$<NOT:$<BOOL:${EMSCRIPTEN}>>:third-party/glad/include/glad/glad.h>
2024-08-21 13:55:34 +00:00
third-party/stb/stb_ds.h)
2024-08-20 14:25:01 +00:00
# distribution definitions, set them with -DX=X in cli
2024-07-08 00:44:20 +00:00
set(PACKAGE_EXTENSION "btw" CACHE STRING
"File extension used to look for data archives")
2024-08-27 10:24:15 +00:00
function(give_options_without_warnings target)
set(BUILD_FLAGS
# these SHOULDN'T break anything...
-fno-math-errno
-ffp-contract=fast
-fno-signed-zeros
-fno-trapping-math
-freciprocal-math
$<$<BOOL:${EMSCRIPTEN}>:-sUSE_SDL=2>)
set(BUILD_FLAGS_RELEASE
-O3
-flto
-mavx -mavx2
-Wl,--gc-sections
-fdata-sections
-ffunction-sections
-funroll-loops
-fomit-frame-pointer)
set(BUILD_FLAGS_DEBUG
-O0
-g3
-gdwarf
-fno-omit-frame-pointer
2024-09-27 14:49:56 +00:00
$<$<BOOL:${TWN_SANITIZE}>:-fstack-protector-all -fsanitize=undefined -fsanitize=address>
$<$<BOOL:${EMSCRIPTEN}>:-gsource-map>)
2024-09-27 14:49:56 +00:00
target_compile_options(${target} PUBLIC
${BUILD_FLAGS}
$<$<CONFIG:Release>:${BUILD_FLAGS_RELEASE}>
$<$<CONFIG:Debug>:${BUILD_FLAGS_DEBUG}>)
2024-09-27 14:49:56 +00:00
target_link_options(${target} PUBLIC
${BUILD_FLAGS}
$<$<CONFIG:Release>:${BUILD_FLAGS_RELEASE}>
$<$<CONFIG:Debug>:${BUILD_FLAGS_DEBUG}>)
2024-09-27 14:49:56 +00:00
target_compile_definitions(${target} PUBLIC
ORGANIZATION_NAME="${ORGANIZATION_NAME}"
APP_NAME="${APP_NAME}"
2024-08-21 13:55:34 +00:00
PACKAGE_EXTENSION="${PACKAGE_EXTENSION}"
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:TWN_FEATURE_DYNLIB_GAME>)
endfunction()
2024-08-27 10:24:15 +00:00
function(give_options target)
give_options_without_warnings(${target})
set(WARNING_FLAGS_CLANG
-Weverything
-Wno-padded
-Wno-declaration-after-statement
-Wno-unsafe-buffer-usage
-Wno-unused-command-line-argument)
set(WARNING_FLAGS
-Wall
-Wextra
-Wpedantic
-Wshadow
-Wdouble-promotion
-Wconversion -Wno-sign-conversion
-Werror=vla
2024-08-27 10:42:40 +00:00
-Wno-missing-field-initializers
2024-08-27 11:45:26 +00:00
-Wunused-result
2024-08-27 10:24:15 +00:00
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Gnu>:-Wcast-align=strict>)
target_compile_options(${target} PRIVATE
${WARNING_FLAGS}
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Clang>:${WARNING_FLAGS_CLANG}>)
endfunction()
2024-08-21 13:55:34 +00:00
function(include_deps target)
# header-only libraries should be marked as "system includes"
# to suppress compiler warnings in their code (it's not my problem after all)
2024-07-29 22:20:30 +00:00
set(THIRD_PARTY_INCLUDES
third-party/physfs/src
third-party/physfs/extras
third-party/libxm/include
third-party/stb
third-party/x-watcher
third-party/tomlc99
$<$<NOT:$<BOOL:${EMSCRIPTEN}>>:third-party/glad/include>)
2024-08-21 13:55:34 +00:00
list(TRANSFORM THIRD_PARTY_INCLUDES PREPEND ${TWN_ROOT_DIR}/)
2024-07-29 22:20:30 +00:00
target_include_directories(${target} SYSTEM PRIVATE ${THIRD_PARTY_INCLUDES})
2024-07-08 00:44:20 +00:00
# allow access to headers from any point in source tree
target_include_directories(${target} PRIVATE ${TWN_ROOT_DIR} ${TWN_ROOT_DIR}/include)
2024-08-21 13:55:34 +00:00
endfunction()
2024-08-21 13:55:34 +00:00
function(link_deps target)
target_link_libraries(${target} PUBLIC
$<$<NOT:$<BOOL:${EMSCRIPTEN}>>:SDL2::SDL2>
physfs-static
2024-08-21 13:55:34 +00:00
xms)
target_include_directories(${target} PUBLIC ${SDL2_INCLUDE_DIRS})
endfunction()
function(use_townengine target sources output_directory data_dir)
if(TWN_FEATURE_DYNLIB_GAME)
# game shared library, for reloading
add_library(${target}_game SHARED ${sources})
give_options(${target}_game)
include_deps(${target}_game)
target_link_libraries(${target}_game PUBLIC $<$<NOT:$<BOOL:${EMSCRIPTEN}>>:SDL2::SDL2> ${TWN_TARGET})
set_target_properties(${target}_game PROPERTIES
OUTPUT_NAME game
LIBRARY_OUTPUT_DIRECTORY ${output_directory}
RUNTIME_OUTPUT_DIRECTORY ${output_directory})
# launcher binary, loads game and engine shared library
add_executable(${target}_app ${TWN_ROOT_DIR}/src/twn_main.c)
# todo: copy instead?
# put libtownengine.so alongside the binary
set_target_properties(${TWN_TARGET} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${output_directory})
else()
# build and link all statically
add_executable(${target}_app ${sources})
endif()
2024-08-20 14:50:50 +00:00
set_target_properties(${target}_app PROPERTIES
OUTPUT_NAME launcher
LIBRARY_OUTPUT_DIRECTORY ${output_directory}
RUNTIME_OUTPUT_DIRECTORY ${output_directory})
if(EMSCRIPTEN)
set_target_properties(${target}_app PROPERTIES
SUFFIX .html)
endif()
target_compile_options(${target}_app PRIVATE
$<$<BOOL:${EMSCRIPTEN}>:--shell-file ${TWN_ROOT_DIR}/shell_minimal.html>)
# system libraries
find_library(MATH_LIBRARY m)
2024-08-21 13:55:34 +00:00
if (MATH_LIBRARY)
target_link_libraries(${target}_app PUBLIC ${MATH_LIBRARY})
endif()
give_options(${target}_app)
include_deps(${target}_app)
link_deps(${target}_app)
2024-07-08 00:44:20 +00:00
target_link_libraries(${target}_app PUBLIC ${TWN_TARGET})
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}>")
2024-09-26 18:02:56 +00:00
# todo: generate by python script instead
# todo: handle the case where no numerical trace exists
string(JOIN "\n" TWN_BOOTSTRAP_SH
"#!/bin/env sh"
"cd \"$(dirname \"$0\")\""
"export LD_LIBRARY_PATH=\"\$LD_LIBRARY_PATH:./\""
"if [ \"\$1\" = \"gdb\" ]; then"
2024-09-21 18:14:25 +00:00
" unset DEBUGINFOD_URLS"
" gdb -ex run --args ./launcher ${TWN_BOOTSTRAP_EXEC_ARGS}"
"elif [ \"\$1\" = \"apitrace-take\" ]; then"
" export ASAN_OPTIONS=verify_asan_link_order=0"
" export LD_PRELOAD=\"/usr/lib/libubsan.so.1 /usr/lib/apitrace/wrappers/glxtrace.so \$LD_PRELOAD\""
" ./launcher ${TWN_BOOTSTRAP_EXEC_ARGS}"
"elif [ \"\$1\" = \"apitrace-use\" ]; then"
" traces=( launcher.*.trace )"
" trace=\${traces[-1]}"
" qapitrace \${trace}"
2024-09-21 18:14:25 +00:00
"else"
" ./launcher ${TWN_BOOTSTRAP_EXEC_ARGS}"
"fi"
"")
FILE(GENERATE OUTPUT ${output_directory}/${target}
CONTENT "${TWN_BOOTSTRAP_SH}"
FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ)
if(WIN32)
string(JOIN "\n" TWN_BOOTSTRAP_BAT
"pushd \"%~dp0\""
"launcher ${TWN_BOOTSTRAP_EXEC_ARGS}"
2024-08-21 18:40:32 +00:00
"")
FILE(GENERATE OUTPUT ${output_directory}/${target}.bat
CONTENT "${TWN_BOOTSTRAP_BAT}"
FILE_PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ)
endif()
if (TWN_ARCHIVE_DATA)
# zip up assets
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()
endfunction()
2024-07-08 00:44:20 +00:00
2024-08-27 10:24:15 +00:00
give_options_without_warnings(twn_third_parties)
include_deps(twn_third_parties)
link_deps(twn_third_parties)
give_options(${TWN_TARGET})
include_deps(${TWN_TARGET})
link_deps(${TWN_TARGET})
2024-08-27 10:24:15 +00:00
target_link_libraries(${TWN_TARGET} PUBLIC twn_third_parties)
target_include_directories(${TWN_TARGET} PRIVATE ${TWN_ROOT_DIR}/src)
2024-07-08 00:44:20 +00:00
# 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})