1
0
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

212 lines
5.2 KiB

cmake_minimum_required(VERSION 3.16)
###########################################################
# COMPILER CONFIGURATION
###########################################################
set(CMAKE_C_COMPILER "clang" CACHE STRING "C compiler" FORCE)
set(CMAKE_CXX_COMPILER "clang++" CACHE STRING "C++ compiler" FORCE)
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2")
set(DEPENDENCY_WARNINGS "-w")
set(PROJECT_WARNINGS
-Werror
-W -Wall -Wextra -Wshadow
-Wno-unused-result -Wno-unused-function -Wno-implicit-fallthrough)
set(PROJECT_PROPERTIES
C_STANDARD 99
C_STANDARD_REQUIRED 99
C_EXTENSIONS OFF)
if(UNIX)
set(PROJECT_PROPERTIES ${PROJECT_PROPERTIES} LINK_FLAGS "-Wl,-rpath,./")
endif()
###########################################################
# PROJECT
###########################################################
set(PROJECT obolos)
project(${PROJECT})
set(BIN ${PROJECT})
set(GAME game)
###########################################################
# DIRECTORIES
###########################################################
set(3RD_PARTY_DIR ${CMAKE_SOURCE_DIR}/code/3rd_party)
set(SRC_DIR ${CMAKE_SOURCE_DIR}/code/src)
set(ASSET_DIR ${CMAKE_SOURCE_DIR}/assets/processed)
set(SHADER_DIR ${CMAKE_SOURCE_DIR}/code/shaders)
set(SCRIPTS_DIR ${CMAKE_SOURCE_DIR}/tools/scripts)
set(PACKAGE_DIR ${CMAKE_BINARY_DIR}/package)
###########################################################
# 3RD PARTY LIBRARIES
###########################################################
# glad
add_subdirectory(${3RD_PARTY_DIR}/glad EXCLUDE_FROM_ALL)
# SDL
set(SDL_SHARED ON CACHE BOOL "" FORCE)
set(SDL_STATIC OFF CACHE BOOL "" FORCE)
add_subdirectory(${3RD_PARTY_DIR}/sdl-2.0.20 EXCLUDE_FROM_ALL)
set_target_properties(SDL2 PROPERTIES COMPILE_FLAGS ${DEPENDENCY_WARNINGS})
set_target_properties(SDL2main PROPERTIES COMPILE_FLAGS ${DEPENDENCY_WARNINGS})
###########################################################
# ASSETS
###########################################################
# Copy processed assets and shaders to build directory
add_custom_target(
invalidate_assets ALL
COMMAND ${CMAKE_COMMAND} -E touch
${ASSET_DIR}
COMMAND ${CMAKE_COMMAND} -E touch
${SHADER_DIR})
add_custom_command(
TARGET invalidate_assets POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${ASSET_DIR}/ ${CMAKE_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E copy_directory ${SHADER_DIR} ${CMAKE_BINARY_DIR}/shaders)
###########################################################
# TARGETS
###########################################################
add_executable(${BIN})
add_library(${GAME} SHARED)
###########################################################
# BUILD CONFIGURATIONS
###########################################################
set(BIN_SRCS ${SRC_DIR}/main.c)
set(GAME_SRCS ${SRC_DIR}/all.c)
set(BIN_LIBS glad SDL2)
set(GAME_LIBS glad SDL2)
if(WIN32)
set(BIN_LIBS
${BIN_LIBS}
mingw32
SDL2main)
endif()
if (CMAKE_BUILD_TYPE STREQUAL "Release")
add_compile_definitions(BUILD_RELEASE)
set(GAME_SRCS ${GAME_SRCS} ${SRC_DIR}/engine/glsl.inc)
add_custom_command(
PRE_BUILD
OUTPUT ${SRC_DIR}/engine/glsl.inc
COMMAND
${SCRIPTS_DIR}/header_generator.sh
${SRC_DIR}/engine/glsl.inc # output
${SHADER_DIR}/geo.glsl # input
${SHADER_DIR}/screen.glsl # input
${SHADER_DIR}/text.glsl # input
DEPENDS
${SHADER_DIR}/geo.glsl
${SHADER_DIR}/screen.glsl
${SHADER_DIR}/text.glsl)
add_custom_command(
TARGET ${GAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E rm ${SRC_DIR}/engine/glsl.inc)
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_definitions(BUILD_DEBUG)
# Required for a valid language server compilation database to be generated with unity builds
if (CMAKE_EXPORT_COMPILE_COMMANDS)
file(GLOB GAME_SRCS
${SRC_DIR}/all.c
${SRC_DIR}/engine/*.c
${SRC_DIR}/game/*.c)
target_compile_options(
${GAME}
PRIVATE
-include ${SRC_DIR}/all.c)
endif()
else()
message(FATAL_ERROR "No build configuration selected")
endif()
###########################################################
# GAME LIBRARY
###########################################################
set_target_properties(${GAME} PROPERTIES ${PROJECT_PROPERTIES})
target_sources(
${GAME}
PRIVATE
${GAME_SRCS})
target_link_libraries(
${GAME}
PRIVATE
${GAME_LIBS})
target_compile_options(
${GAME}
PRIVATE
${PROJECT_WARNINGS})
###########################################################
# EXECUTABLE
###########################################################
set_target_properties(${BIN} PROPERTIES ${PROJECT_PROPERTIES})
target_sources(
${BIN}
PRIVATE
${BIN_SRCS})
target_link_libraries(
${BIN}
PRIVATE
${BIN_LIBS})
target_compile_options(
${BIN}
PRIVATE
${PROJECT_WARNINGS})
add_custom_target(
run
COMMAND ${BIN}
DEPENDS ${BIN} ${GAME} ${ENGINE}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
###########################################################
# INSTALL
###########################################################
install(
TARGETS ${BIN} ${GAME} ${ENGINE} ${BIN_LIBS} ${GAME_LIBS} ${ENGINE_LIBS}
CONFIGURATIONS Release
RUNTIME DESTINATION ${PACKAGE_DIR}
LIBRARY DESTINATION ${PACKAGE_DIR})
install(
DIRECTORY ${ASSET_DIR}/
CONFIGURATIONS Release
DESTINATION ${PACKAGE_DIR})