From 350f5365ac1cdf8482eca632a103f45806e3cd89 Mon Sep 17 00:00:00 2001 From: furudbat Date: Sat, 26 Oct 2024 20:57:27 +0200 Subject: [PATCH 1/3] Add CMake support --- CMakeLists.txt | 37 +++++++++++++++++++++++++++++++ cmake/CPM.cmake | 19 ++++++++++++++++ cmake/PreventInSourceBuilds.cmake | 22 ++++++++++++++++++ libs/CMakeLists.txt | 1 + libs/raylib/CMakeLists.txt | 21 ++++++++++++++++++ src/CMakeLists.txt | 27 ++++++++++++++++++++++ 6 files changed, 127 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 cmake/CPM.cmake create mode 100644 cmake/PreventInSourceBuilds.cmake create mode 100644 libs/CMakeLists.txt create mode 100644 libs/raylib/CMakeLists.txt create mode 100644 src/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a2d2b0f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,37 @@ +cmake_minimum_required(VERSION 3.19...3.24) + +# Generate compile_commands.json +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +# Not ideal to use this global variable, but necessary to make sure that tooling and projects use the same version +#set(CMAKE_CXX_STANDARD 11) +set(CMAKE_C_STANDARD 99) + +# strongly encouraged to enable this globally to avoid conflicts between -Wpedantic being enabled and -std=c++20 and -std=gnu++20 for +# example when compiling with PCH enabled +#set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_C_EXTENSIONS ON) + +# Set the project name to your project name, my project isn't very descriptive +project(raylib_game + VERSION 0.1.0 + LANGUAGES C CXX) +include(cmake/PreventInSourceBuilds.cmake) + +# ---- Add dependencies via CPM ---- +include(cmake/CPM.cmake) +add_subdirectory(libs) + +# ########################################################################################################################################## +# Project +# ########################################################################################################################################## + +add_subdirectory(src) + +# If MSVC is being used, and ASAN is enabled, we need to set the debugger environment so that it behaves well with MSVC's debugger, and we +# can run the target from visual studio +if(MSVC) + get_all_installable_targets(all_targets) + message("all_targets=${all_targets}") + set_target_properties(${all_targets} PROPERTIES VS_DEBUGGER_ENVIRONMENT "PATH=$(VC_ExecutablePath_x64);%PATH%") +endif() diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake new file mode 100644 index 0000000..9e7aafc --- /dev/null +++ b/cmake/CPM.cmake @@ -0,0 +1,19 @@ +# https://github.com/cpm-cmake/CPM.cmake +set(CPM_DOWNLOAD_VERSION 0.40.2) + +if(CPM_SOURCE_CACHE) + # Expand relative path. This is important if the provided path contains a tilde (~) + get_filename_component(CPM_SOURCE_CACHE ${CPM_SOURCE_CACHE} ABSOLUTE) + set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +elseif(DEFINED ENV{CPM_SOURCE_CACHE}) + set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +else() + set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +endif() + +if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION})) + message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}") + file(DOWNLOAD https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake ${CPM_DOWNLOAD_LOCATION}) +endif() + +include(${CPM_DOWNLOAD_LOCATION}) diff --git a/cmake/PreventInSourceBuilds.cmake b/cmake/PreventInSourceBuilds.cmake new file mode 100644 index 0000000..be0ee12 --- /dev/null +++ b/cmake/PreventInSourceBuilds.cmake @@ -0,0 +1,22 @@ +# +# This function will prevent in-source builds +function(AssureOutOfSourceBuilds) + if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR) + message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.") + endif() + + # make sure the user doesn't play dirty with symlinks + get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH) + get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH) + + # disallow in-source builds + if("${srcdir}" STREQUAL "${bindir}") + message("######################################################") + message("Warning: in-source builds are disabled") + message("Please create a separate build directory and run cmake from there") + message("######################################################") + message(FATAL_ERROR "Quitting configuration") + endif() +endfunction() + +AssureOutOfSourceBuilds() diff --git a/libs/CMakeLists.txt b/libs/CMakeLists.txt new file mode 100644 index 0000000..c94202f --- /dev/null +++ b/libs/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(raylib) \ No newline at end of file diff --git a/libs/raylib/CMakeLists.txt b/libs/raylib/CMakeLists.txt new file mode 100644 index 0000000..3d53cea --- /dev/null +++ b/libs/raylib/CMakeLists.txt @@ -0,0 +1,21 @@ +# add raylib (3rd-party) +# https://github.com/raysan5/raylib + +cpmaddpackage( + NAME + raylib + GITHUB_REPOSITORY + raysan5/raylib + GIT_TAG + #5.5 + master # use up-to-date branch +) +target_compile_options(raylib PRIVATE $<$:-Wno-error=implicit-function-declaration>) +target_compile_options(raylib PRIVATE $<$:-Wno-unused-result>) +target_compile_options(raylib PRIVATE $<$:-Wno-stringop-overflow>) +target_compile_options(raylib PRIVATE $<$:-Wno-implicit-const-int-float-conversion>) +target_compile_features(raylib PRIVATE c_std_99) + +if("${PLATFORM}" STREQUAL "Desktop") + target_compile_features(glfw PRIVATE c_std_99) +endif() \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..b322c44 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,27 @@ +add_executable(raylib_game) +# @NOTE: add more source files here +target_sources(raylib_game PRIVATE raylib_game.c) + +target_include_directories(raylib_game PRIVATE "$") +target_link_libraries(raylib_game raylib) +if(NOT WIN32) + target_link_libraries(raylib_game m) +endif() + +# Web Configurations +if (${PLATFORM} STREQUAL "Web") + set_target_properties(raylib_game PROPERTIES SUFFIX ".html") # Tell Emscripten to build an example.html file. + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY -s GL_ENABLE_GET_PROC_ADDRESS=1") +endif() + +# Checks if OSX and links appropriate frameworks (only required on MacOS) +if(APPLE) + target_link_libraries(raylib_game "-framework IOKit") + target_link_libraries(raylib_game "-framework Cocoa") + target_link_libraries(raylib_game "-framework OpenGL") +endif() + +# misc +set_target_properties(raylib_game PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") +# set the startup project for the "play" button in MSVC +set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT raylib_game) From 18bb595ac5257d6a1f466205a4759f82aa81bdda Mon Sep 17 00:00:00 2001 From: furudbat Date: Sun, 27 Oct 2024 10:33:45 +0100 Subject: [PATCH 2/3] Update CMakeLists --- src/CMakeLists.txt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b322c44..ef4f637 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,7 +11,16 @@ endif() # Web Configurations if (${PLATFORM} STREQUAL "Web") set_target_properties(raylib_game PROPERTIES SUFFIX ".html") # Tell Emscripten to build an example.html file. - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY -s GL_ENABLE_GET_PROC_ADDRESS=1") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s USE_GLFW=3 -s FORCE_FILESYSTEM=1 -s WASM=1") + + set(web_link_flags) + if(CMAKE_BUILD_TYPE MATCHES Debug OR CMAKE_BUILD_TYPE MATCHES RelWithDebInfo) + set(web_link_flags "${web_link_flags} -s ASSERTIONS=1") + endif() + set(web_link_flags "${web_link_flags} --preload-file ${CMAKE_CURRENT_SOURCE_DIR}/resources@resources --use-preload-plugins") + set(web_link_flags "${web_link_flags} --shell-file ${CMAKE_CURRENT_SOURCE_DIR}/minshell.html") + + set_target_properties(raylib_game PROPERTIES LINK_FLAGS "${web_link_flags}") endif() # Checks if OSX and links appropriate frameworks (only required on MacOS) From 3da1140f851fc33358f37b4518e8eb2e80b49281 Mon Sep 17 00:00:00 2001 From: furudbat Date: Sun, 27 Oct 2024 23:50:18 +0100 Subject: [PATCH 3/3] Update CMakeLists.txt --- CMakeLists.txt | 37 ++++++++++++++++++++++++++++--- cmake/CPM.cmake | 19 ---------------- cmake/PreventInSourceBuilds.cmake | 22 ------------------ libs/CMakeLists.txt | 1 - libs/raylib/CMakeLists.txt | 21 ------------------ 5 files changed, 34 insertions(+), 66 deletions(-) delete mode 100644 cmake/CPM.cmake delete mode 100644 cmake/PreventInSourceBuilds.cmake delete mode 100644 libs/CMakeLists.txt delete mode 100644 libs/raylib/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index a2d2b0f..9eb0293 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,11 +16,42 @@ set(CMAKE_C_EXTENSIONS ON) project(raylib_game VERSION 0.1.0 LANGUAGES C CXX) -include(cmake/PreventInSourceBuilds.cmake) # ---- Add dependencies via CPM ---- -include(cmake/CPM.cmake) -add_subdirectory(libs) +## https://github.com/cpm-cmake/CPM.cmake +set(CPM_DOWNLOAD_VERSION 0.40.2) +if(CPM_SOURCE_CACHE) + # Expand relative path. This is important if the provided path contains a tilde (~) + get_filename_component(CPM_SOURCE_CACHE ${CPM_SOURCE_CACHE} ABSOLUTE) + set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +elseif(DEFINED ENV{CPM_SOURCE_CACHE}) + set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +else() + set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +endif() +if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION})) + message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}") + file(DOWNLOAD https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake ${CPM_DOWNLOAD_LOCATION}) +endif() +include(${CPM_DOWNLOAD_LOCATION}) +## add raylib (3rd-party) https://github.com/raysan5/raylib +cpmaddpackage( + NAME + raylib + GITHUB_REPOSITORY + raysan5/raylib + GIT_TAG + #5.5 + master # use up-to-date branch +) +target_compile_options(raylib PRIVATE $<$:-Wno-error=implicit-function-declaration>) +target_compile_options(raylib PRIVATE $<$:-Wno-unused-result>) +target_compile_options(raylib PRIVATE $<$:-Wno-stringop-overflow>) +target_compile_options(raylib PRIVATE $<$:-Wno-implicit-const-int-float-conversion>) +target_compile_features(raylib PRIVATE c_std_99) +if("${PLATFORM}" STREQUAL "Desktop") + target_compile_features(glfw PRIVATE c_std_99) +endif() # ########################################################################################################################################## # Project diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake deleted file mode 100644 index 9e7aafc..0000000 --- a/cmake/CPM.cmake +++ /dev/null @@ -1,19 +0,0 @@ -# https://github.com/cpm-cmake/CPM.cmake -set(CPM_DOWNLOAD_VERSION 0.40.2) - -if(CPM_SOURCE_CACHE) - # Expand relative path. This is important if the provided path contains a tilde (~) - get_filename_component(CPM_SOURCE_CACHE ${CPM_SOURCE_CACHE} ABSOLUTE) - set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") -elseif(DEFINED ENV{CPM_SOURCE_CACHE}) - set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") -else() - set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake") -endif() - -if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION})) - message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}") - file(DOWNLOAD https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake ${CPM_DOWNLOAD_LOCATION}) -endif() - -include(${CPM_DOWNLOAD_LOCATION}) diff --git a/cmake/PreventInSourceBuilds.cmake b/cmake/PreventInSourceBuilds.cmake deleted file mode 100644 index be0ee12..0000000 --- a/cmake/PreventInSourceBuilds.cmake +++ /dev/null @@ -1,22 +0,0 @@ -# -# This function will prevent in-source builds -function(AssureOutOfSourceBuilds) - if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR) - message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there.") - endif() - - # make sure the user doesn't play dirty with symlinks - get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH) - get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH) - - # disallow in-source builds - if("${srcdir}" STREQUAL "${bindir}") - message("######################################################") - message("Warning: in-source builds are disabled") - message("Please create a separate build directory and run cmake from there") - message("######################################################") - message(FATAL_ERROR "Quitting configuration") - endif() -endfunction() - -AssureOutOfSourceBuilds() diff --git a/libs/CMakeLists.txt b/libs/CMakeLists.txt deleted file mode 100644 index c94202f..0000000 --- a/libs/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_subdirectory(raylib) \ No newline at end of file diff --git a/libs/raylib/CMakeLists.txt b/libs/raylib/CMakeLists.txt deleted file mode 100644 index 3d53cea..0000000 --- a/libs/raylib/CMakeLists.txt +++ /dev/null @@ -1,21 +0,0 @@ -# add raylib (3rd-party) -# https://github.com/raysan5/raylib - -cpmaddpackage( - NAME - raylib - GITHUB_REPOSITORY - raysan5/raylib - GIT_TAG - #5.5 - master # use up-to-date branch -) -target_compile_options(raylib PRIVATE $<$:-Wno-error=implicit-function-declaration>) -target_compile_options(raylib PRIVATE $<$:-Wno-unused-result>) -target_compile_options(raylib PRIVATE $<$:-Wno-stringop-overflow>) -target_compile_options(raylib PRIVATE $<$:-Wno-implicit-const-int-float-conversion>) -target_compile_features(raylib PRIVATE c_std_99) - -if("${PLATFORM}" STREQUAL "Desktop") - target_compile_features(glfw PRIVATE c_std_99) -endif() \ No newline at end of file