Skip to content

Instantly share code, notes, and snippets.

@xenobrain
Created August 30, 2023 20:10
Show Gist options
  • Save xenobrain/25d42096b4be781907bdcaca3d08de58 to your computer and use it in GitHub Desktop.
Save xenobrain/25d42096b4be781907bdcaca3d08de58 to your computer and use it in GitHub Desktop.
CMake Emscripten
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
# Install CPM ##########################################################################################################
set(CPM_VERSION 0.38.1)
set(CPM_SOURCE_CACHE ${CMAKE_SOURCE_DIR}/libraries)
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
file(DOWNLOAD https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_VERSION}/CPM.cmake
${CPM_DOWNLOAD_LOCATION})
endif()
include(${CPM_DOWNLOAD_LOCATION})
# Install Emscripten Toolchain #########################################################################################
if(${PLATFORM} STREQUAL HTML)
message("Activating Emscripten Toolchain")
CPMAddPackage(NAME emsdk GITHUB_REPOSITORY emscripten-core/emsdk GIT_TAG 3.1.43 DOWNLOAD_ONLY True)
if(NOT (EXISTS ${emsdk_SOURCE_DIR}/upstream))
execute_process(COMMAND ${emsdk_SOURCE_DIR}/emsdk install latest)
execute_process(COMMAND ${emsdk_SOURCE_DIR}/emsdk activate latest)
endif()
set(CMAKE_TOOLCHAIN_FILE
${emsdk_SOURCE_DIR}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake CACHE STRING "")
endif()
# Project Configuration ################################################################################################
project(lowrezjam VERSION 0.0.1 LANGUAGES CXX)
set(CLANG_WARNINGS
-Wall
-Wextra
-Wshadow
-Wunused
-Wpedantic
-Wformat=2
-Wcast-align
-Wconversion
-Wold-style-cast
-Wsign-conversion
-Wnull-dereference
-Wdouble-promotion
-Wnon-virtual-dtor
-Woverloaded-virtual)
set(CLANG_LINK_OPTIONS
-nostdlib
-nostartfiles
-nodefaultlibs)
set(PROJECT_INCLUDE_DIRECTORIES source)
set(PROJECT_COMPILE_DEFINITIONS PLATFORM_${PLATFORM} RENDERER_${RENDER_API})
set(PROJECT_COMPILE_FEATURES cxx_std_20)
set(PROJECT_COMPILE_OPTIONS ${CLANG_WARNINGS})
set(PROJECT_LINK_DIRECTORIES)
set(PROJECT_LINK_LIBRARIES)
set(PROJECT_LINK_OPTIONS ${CLANG_LINK_OPTIONS})
macro(engine_common_configuration library)
target_include_directories(${library} PRIVATE ${PROJECT_INCLUDE_DIRECTORIES})
target_compile_definitions(${library} PRIVATE ${PROJECT_COMPILE_DEFINITIONS})
target_compile_features( ${library} PRIVATE ${PROJECT_COMPILE_FEATURES})
target_compile_options(${library} PRIVATE ${PROJECT_COMPILE_OPTIONS})
target_link_directories(${library} PRIVATE ${PROJECT_LINK_DIRECTORIES})
target_link_libraries(${library} PRIVATE ${PROJECT_LINK_LIBRARIES})
target_link_options(${library} PRIVATE ${PROJECT_LINK_OPTIONS})
endmacro()
# Build Foundation Library #############################################################################################
file(GLOB foundation_HEADERS source/engine/foundation/*.h)
file(GLOB foundation_SOURCES source/engine/foundation/*.cpp)
add_library(foundation ${foundation_HEADERS} ${foundation_SOURCES})
if(${PLATFORM} STREQUAL HTML)
target_sources(foundation PRIVATE source/engine/foundation/platform/platform_system_html.cpp)
elseif(${PLATFORM} STREQUAL LINUX)
target_sources(foundation PRIVATE source/engine/foundation/platform/platform_system_linux.cpp)
elseif(${PLATFORM} STREQUAL MACOS)
target_link_libraries(foundation PRIVATE "-framework Cocoa")
target_sources(foundation PRIVATE source/engine/foundation/platform/platform_system_macos.cpp)
elseif(${PLATFORM} STREQUAL WINDOWS)
target_sources(foundation PRIVATE source/engine/foundation/platform/platform_system_windows.cpp)
endif()
engine_common_configuration(foundation)
# Build Engine Plugins #################################################################################################
# Build Renderer Plugin
file(GLOB renderer_HEADERS source/engine/plugins/renderer/*.h)
file(GLOB renderer_SOURCES source/engine/plugins/renderer/*.cpp)
add_library(renderer ${renderer_HEADERS} ${renderer_SOURCES})
if(RENDER_API STREQUAL OPENGL)
file(GLOB renderer_opengl_HEADERS source/engine/plugins/renderer/opengl/*.h)
file(GLOB renderer_opengl_SOURCES source/engine/plugins/renderer/opengl/*.cpp)
target_sources(renderer PRIVATE ${renderer_opengl_HEADERS} ${renderer_opengl_SOURCES})
elseif(RENDER_API STREQUAL VULKAN)
CPMAddPackage("gh:KhronosGroup/Vulkan-Headers@1.3.257")
target_include_directories(renderer PRIVATE ${Vulkan-Headers_SOURCE_DIR}/include)
file(GLOB renderer_vulkan_HEADERS source/engine/plugins/renderer/vulkan/*.h)
file(GLOB renderer_vulkan_SOURCES source/engine/plugins/renderer/vulkan/*.cpp)
target_sources(renderer PRIVATE ${renderer_vulkan_HEADERS} ${renderer_vulkan_SOURCES})
endif()
engine_common_configuration(renderer)
# Build Engine Executable ##############################################################################################
file(GLOB application_HEADERS source/engine/application/*.h)
file(GLOB application_SOURCES source/engine/application/*.cpp)
add_executable(application ${application_HEADERS} ${application_SOURCES})
target_link_libraries(application PRIVATE foundation renderer game)
if(${PLATFORM} STREQUAL MACOS)
target_link_options(application PRIVATE -e _entry)
target_link_libraries(application PRIVATE System)
elseif(${PLATFORM} STREQUAL HTML)
set_target_properties(application PROPERTIES SUFFIX ".html")
target_link_libraries(application PRIVATE)
endif()
engine_common_configuration(application)
# Build Game Library ###################################################################################################
file(GLOB_RECURSE game_HEADERS source/game/*.h)
file(GLOB_RECURSE game_SOURCES source/game/*.cpp)
add_library(game ${game_HEADERS} ${game_SOURCES})
target_link_libraries(game PRIVATE foundation renderer)
engine_common_configuration(game)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment