townengine/CMakeLists.txt

217 lines
7.8 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
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
2024-07-08 00:56:00 +00:00
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
2024-07-08 00:44:20 +00:00
# dependencies
2024-07-29 22:20:30 +00:00
find_package(SDL2 REQUIRED GLOBAL)
find_package(SDL2_image REQUIRED GLOBAL)
find_package(SDL2_ttf REQUIRED GLOBAL)
2024-07-08 00:44:20 +00:00
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif ()
2024-07-29 22:20:30 +00:00
set(TOWNENGINE_TARGET townengine CACHE INTERNAL "")
set(TOWNENGINE_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "")
2024-07-08 00:44:20 +00:00
set(PHYSFS_BUILD_SHARED FALSE)
set(PHYSFS_DISABLE_INSTALL TRUE)
set(PHYSFS_TARGETNAME_UNINSTALL "physfs_uninstall")
add_subdirectory(third-party/physfs)
2024-07-08 13:58:23 +00:00
add_subdirectory(third-party/libxm)
2024-07-08 00:44:20 +00:00
if(UNIX)
set(SYSTEM_SOURCE_FILES
src/system/linux/elf.c
)
else()
set(SYSTEM_SOURCE_FILES)
endif()
set(TOWNENGINE_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/glad/src/glad.c
2024-07-08 06:46:12 +00:00
2024-07-08 00:44:20 +00:00
src/main.c
src/config.h
src/context.c src/context.h
src/audio.c src/audio.h
2024-07-08 00:44:20 +00:00
src/util.c src/util.h
src/rendering.c src/rendering.h
src/input.c src/input.h
src/text.c src/text.h
src/camera.c src/camera.h
src/textures/textures.c
${SYSTEM_SOURCE_FILES}
)
2024-07-29 22:20:30 +00:00
list(TRANSFORM TOWNENGINE_SOURCE_FILES PREPEND ${TOWNENGINE_DIR}/)
2024-07-08 00:44:20 +00:00
# base engine code, reused for games and tools
2024-07-29 22:20:30 +00:00
add_library(${TOWNENGINE_TARGET} ${TOWNENGINE_SOURCE_FILES})
source_group(TREE ${TOWNENGINE_DIR} FILES ${TOWNENGINE_SOURCE_FILES})
2024-07-08 00:44:20 +00:00
2024-07-29 22:20:30 +00:00
set_target_properties(${TOWNENGINE_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
# distribution definitions
set(ORGANIZATION_NAME "wanp" CACHE STRING
"App developer/publisher's identifier")
2024-07-29 10:06:23 +00:00
set(APP_NAME "townengine" CACHE STRING
2024-07-08 00:44:20 +00:00
"App identifier")
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
-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
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Gnu>:-Wcast-align=strict>
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Clang>:${WARNING_FLAGS_CLANG}>)
set(BUILD_FLAGS
# these SHOULDN'T break anything...
-fno-math-errno
-ffp-contract=fast
-fno-signed-zeros
-fno-trapping-math
-freciprocal-math)
set(BUILD_FLAGS_RELEASE
-O3
-flto
-mavx -mavx2
-Wl,--gc-sections
-fdata-sections
-ffunction-sections
-funroll-loops
-fomit-frame-pointer
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},Gnu>:-fallow-store-data-races>)
set(BUILD_FLAGS_DEBUG
-O0
-g3
-gdwarf
-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
ORGANIZATION_NAME="${ORGANIZATION_NAME}"
APP_NAME="${APP_NAME}"
PACKAGE_EXTENSION="${PACKAGE_EXTENSION}")
endfunction()
function(link_and_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/glad/include
third-party/stb
)
2024-07-29 22:20:30 +00:00
list(TRANSFORM THIRD_PARTY_INCLUDES PREPEND ${TOWNENGINE_DIR}/)
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
2024-07-29 22:20:30 +00:00
target_include_directories(${target} PRIVATE ${TOWNENGINE_DIR})
target_link_libraries(${target} PUBLIC
SDL2::SDL2
SDL2::SDL2main
SDL2_image::SDL2_image
SDL2_ttf::SDL2_ttf
physfs-static
xms
)
endfunction()
function(use_townengine target)
# system libraries
find_library(MATH_LIBRARY m)
if(MATH_LIBRARY)
target_link_libraries(${target} PUBLIC ${MATH_LIBRARY})
endif()
give_options(${target})
link_and_include_deps(${target})
# third-party libraries
target_link_libraries(${target} PUBLIC
2024-07-29 22:20:30 +00:00
${TOWNENGINE_TARGET}
SDL2::SDL2
SDL2::SDL2main
SDL2_image::SDL2_image
SDL2_ttf::SDL2_ttf
physfs-static
xms
)
2024-07-08 00:44:20 +00:00
# copy dlls for baby windows
add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy -t $<TARGET_FILE_DIR:${target}>
$<TARGET_RUNTIME_DLLS:${target}>
COMMAND_EXPAND_LISTS
)
endfunction()
2024-07-08 00:44:20 +00:00
2024-07-29 22:20:30 +00:00
give_options(${TOWNENGINE_TARGET})
link_and_include_deps(${TOWNENGINE_TARGET})
2024-07-08 00:44:20 +00:00
2024-07-29 22:20:30 +00:00
add_subdirectory(apps/testgame)
2024-07-08 00:44:20 +00:00
# zip up assets
# currently, you must run cmake from the project root dir for this to work correctly
#file(ARCHIVE_CREATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/data.${PACKAGE_EXTENSION}
# PATHS ${PROJECT_SOURCE_DIR}/assets
# FORMAT zip
#)