townengine/CMakeLists.txt

323 lines
12 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_USE_AMALGAM "Enable use of twn_amalgam.c as a single compilation unit" ON)
# 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>
-fvisibility=hidden)
2024-08-21 13:55:34 +00:00
set(PHYSFS_BUILD_SHARED FALSE CACHE INTERNAL "")
set(PHYSFS_DISABLE_INSTALL TRUE CACHE INTERNAL "")
set(PHYSFS_TARGETNAME_UNINSTALL "physfs_uninstall" CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_GRP OFF CACHE BOOL "")
set(PHYSFS_ARCHIVE_WAD OFF CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_HOG OFF CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_MVL OFF CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_QPAK OFF CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_SLB OFF CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_ISO9660 OFF CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_VDF OFF CACHE INTERNAL "")
set(PHYSFS_ARCHIVE_7Z OFF CACHE INTERNAL "")
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
set(TWN_NONOPT_SOURCE_FILES
src/twn_stb.c
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
2024-10-06 21:26:48 +00:00
2024-10-07 14:53:09 +00:00
src/rendering/twn_draw.c src/rendering/twn_draw_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
src/rendering/twn_fog.c)
set(TWN_SOURCE_FILES
$<IF:$<BOOL:${TWN_USE_AMALGAM}>,src/twn_amalgam.c src/twn_stb.c,${TWN_NONOPT_SOURCE_FILES}>
# 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}/)
source_group(TREE ${TWN_ROOT_DIR} FILES ${TWN_NONOPT_SOURCE_FILES})
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
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>
${SDL2_INCLUDE_DIR}/SDL.h
third-party/physfs/src/physfs.h)
2024-08-20 14:25:01 +00:00
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=auto
-mavx -mavx2
-Wl,--gc-sections
-fdata-sections
-ffunction-sections
-funroll-loops
-fomit-frame-pointer
-s)
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}>
2024-10-13 18:32:31 +00:00
$<$<CONFIG:Debug>:${BUILD_FLAGS_DEBUG}>)
2024-09-27 14:49:56 +00:00
target_link_options(${target} PUBLIC
${BUILD_FLAGS}
# -Wl,--no-undefined # TODO: use later for implementing no-libc
$<$<CONFIG:Release>:${BUILD_FLAGS_RELEASE}>
$<$<CONFIG:Debug>:${BUILD_FLAGS_DEBUG}>
-Bsymbolic-functions
-Wl,--hash-style=gnu)
target_compile_definitions(${target} PRIVATE
2024-10-13 18:32:31 +00:00
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:TWN_FEATURE_DYNLIB_GAME>
_GNU_SOURCE)
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
2024-10-13 18:32:31 +00:00
-Wno-unused-command-line-argument
-Wno-covered-switch-default)
2024-08-27 10:24:15 +00:00
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)
2024-10-07 14:54:46 +00:00
target_include_directories(${target} PUBLIC ${SDL2_INCLUDE_DIRS})
endfunction()
2024-10-07 15:55:25 +00:00
function(use_townengine target sources output_directory)
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
2024-10-07 21:50:25 +00:00
add_executable(${target} ${TWN_ROOT_DIR}/src/twn_main.c)
2024-10-13 18:32:31 +00:00
target_compile_options(${target} PRIVATE $<$<BOOL:${LINUX}>:-Wl,-rpath,$ORIGIN/>)
# 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
2024-10-07 21:50:25 +00:00
add_executable(${target} ${sources})
endif()
2024-08-20 14:50:50 +00:00
2024-10-07 21:50:25 +00:00
set_target_properties(${target} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${output_directory}
RUNTIME_OUTPUT_DIRECTORY ${output_directory})
if(EMSCRIPTEN)
2024-10-07 21:50:25 +00:00
set_target_properties(${target} PROPERTIES SUFFIX .html)
endif()
2024-10-07 21:50:25 +00:00
target_compile_options(${target} 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)
2024-10-07 21:50:25 +00:00
target_link_libraries(${target} PUBLIC ${MATH_LIBRARY})
endif()
2024-10-07 21:50:25 +00:00
give_options(${target})
include_deps(${target})
link_deps(${target})
2024-07-08 00:44:20 +00:00
2024-10-07 21:50:25 +00:00
target_link_libraries(${target} PUBLIC ${TWN_TARGET})
if(WIN32)
# copy dlls for baby windows
2024-10-07 21:50:25 +00:00
add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_RUNTIME_DLLS:${target}>
$<TARGET_FILE_DIR:${target}>
COMMAND_EXPAND_LISTS)
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})