From a367fdb27ac31e49a194708a77b82fec6f0c3586 Mon Sep 17 00:00:00 2001 From: Hristo Stamenov Date: Tue, 26 Jan 2021 15:10:48 +0200 Subject: [PATCH] Some more restructuring. Removed unnecessary comments that were self described by the code. Added some more explanations around certain parts of CMake and especially around compiler flags. --- CMakeLists.txt | 6 +- cmake/LibraryConfigurations.cmake | 6 + examples/CMakeLists.txt | 208 ++++++++++++++++-------------- src/CMakeLists.txt | 23 ++-- 4 files changed, 136 insertions(+), 107 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c5d2113c..5fe826dff 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,9 +2,11 @@ cmake_minimum_required(VERSION 3.0) project(raylib) # Directory for easier includes +# Anywhere you see include(...) you can check /cmake for that file set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) -# RAYLIB_IS_MAIN determines whether the project is being used from root, or as a dependency. +# RAYLIB_IS_MAIN determines whether the project is being used from root +# or if it is added as a dependency (through add_subdirectory for example). if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") set(RAYLIB_IS_MAIN TRUE) else() @@ -17,7 +19,7 @@ include(CompilerFlags) # Registers build options that are exposed to cmake include(CMakeOptions.txt) -# Checks a few environment and compiler configurations +# Enforces a few environment and compiler configurations include(BuildOptions) # Main sources directory (the second parameter sets the output directory name to raylib) diff --git a/cmake/LibraryConfigurations.cmake b/cmake/LibraryConfigurations.cmake index 5b09b9e73..1b1961b9b 100644 --- a/cmake/LibraryConfigurations.cmake +++ b/cmake/LibraryConfigurations.cmake @@ -96,4 +96,10 @@ endif () if (NOT GRAPHICS) set(GRAPHICS "GRAPHICS_API_OPENGL_33") +endif () + +set(LIBS_PRIVATE ${LIBS_PRIVATE} ${OPENAL_LIBRARY}) + +if (${PLATFORM} MATCHES "Desktop") + set(LIBS_PRIVATE ${LIBS_PRIVATE} glfw) endif () \ No newline at end of file diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index e3ef3a0fc..ad391edd4 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,122 +1,138 @@ # Setup the project and settings project(examples) -# Get the sources together -set(example_dirs audio core models others shaders shapes text textures) +# Directories that contain examples +set(example_dirs + audio + core + models + others + shaders + shapes + text + textures + ) + +# Next few lines will check for existence of symbols or header files +# They are needed for the physac example and threads examples set(CMAKE_REQUIRED_DEFINITIONS -D_POSIX_C_SOURCE=199309L) - include(CheckSymbolExists) - check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC) - check_symbol_exists(QueryPerformanceCounter windows.h HAVE_QPC) +include(CheckSymbolExists) +check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC) +check_symbol_exists(QueryPerformanceCounter windows.h HAVE_QPC) set(CMAKE_REQUIRED_DEFINITIONS) -if(HAVE_QPC OR HAVE_CLOCK_MONOTONIC) - set(example_dirs ${example_dirs} physac) -endif() -set(example_sources) -set(example_resources) -foreach(example_dir ${example_dirs}) - # Get the .c files - file(GLOB sources ${example_dir}/*.c) - list(APPEND example_sources ${sources}) - - # Any any resources - file(GLOB resources ${example_dir}/resources/*) - list(APPEND example_resources ${resources}) -endforeach() - -if (APPLE AND NOT CMAKE_SYSTEM STRLESS "Darwin-18.0.0") - add_definitions(-DGL_SILENCE_DEPRECATION) - MESSAGE(AUTHOR_WARNING "OpenGL is deprecated starting with macOS 10.14 (Mojave)!") -endif() +if (HAVE_QPC OR HAVE_CLOCK_MONOTONIC) + set(example_dirs ${example_dirs} physac) +endif () include(CheckIncludeFile) CHECK_INCLUDE_FILE("stdatomic.h" HAVE_STDATOMIC_H) set(CMAKE_THREAD_PREFER_PTHREAD TRUE) find_package(Threads) if (CMAKE_USE_PTHREADS_INIT AND HAVE_STDATOMIC_H) - add_if_flag_compiles("-std=c11" CMAKE_C_FLAGS) - if(THREADS_HAVE_PTHREAD_ARG) - add_if_flag_compiles("-pthread" CMAKE_C_FLAGS) - endif() - if(CMAKE_THREAD_LIBS_INIT) - link_libraries("${CMAKE_THREAD_LIBS_INIT}") - endif() -else() - # Items requiring pthreads - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/core/core_loading_thread.c) -endif() + add_if_flag_compiles("-std=c11" CMAKE_C_FLAGS) + if (THREADS_HAVE_PTHREAD_ARG) + add_if_flag_compiles("-pthread" CMAKE_C_FLAGS) + endif () + if (CMAKE_THREAD_LIBS_INIT) + link_libraries("${CMAKE_THREAD_LIBS_INIT}") + endif () +endif () +if (APPLE AND NOT CMAKE_SYSTEM STRLESS "Darwin-18.0.0") + add_definitions(-DGL_SILENCE_DEPRECATION) + MESSAGE(AUTHOR_WARNING "OpenGL is deprecated starting with macOS 10.14 (Mojave)!") +endif () -if(${PLATFORM} MATCHES "Android") - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/others/rlgl_standalone.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/others/standard_lighting.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/core/core_3d_picking.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/core/core_vr_simulator.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/core/core_3d_camera_free.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/core/core_3d_camera_first_person.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/core/core_world_screen.c) +# Collect all source files and resource files +# into a CMake variable +set(example_sources) +set(example_resources) +foreach (example_dir ${example_dirs}) + # Get the .c files + file(GLOB sources ${example_dir}/*.c) + list(APPEND example_sources ${sources}) + + # Any any resources + file(GLOB resources ${example_dir}/resources/*) + list(APPEND example_resources ${resources}) +endforeach () - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_mesh_picking.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_material_pbr.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_cubicmap.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_skybox.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_mesh_picking.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_mesh_generation.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_heightmap.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_billboard.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_rlgl_solar_system.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_rlgl_full_solar_system.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_solar_system.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_obj_viewer.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_animation.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_first_person_maze.c) +if(NOT CMAKE_USE_PTHREADS_INIT OR NOT HAVE_STDATOMIC_H) + # Items requiring pthreads + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/core/core_loading_thread.c) +endif () - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_custom_uniform.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_model_shader.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_postprocessing.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_raymarching.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_palette_switch.c) - list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_basic_lighting.c) +if (${PLATFORM} MATCHES "Android") + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/others/rlgl_standalone.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/others/standard_lighting.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/core/core_3d_picking.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/core/core_vr_simulator.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/core/core_3d_camera_free.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/core/core_3d_camera_first_person.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/core/core_world_screen.c) + + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_mesh_picking.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_material_pbr.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_cubicmap.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_skybox.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_mesh_picking.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_mesh_generation.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_heightmap.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_billboard.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_rlgl_solar_system.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_rlgl_full_solar_system.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_solar_system.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_obj_viewer.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_animation.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_first_person_maze.c) + + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_custom_uniform.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_model_shader.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_postprocessing.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_raymarching.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_palette_switch.c) + list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_basic_lighting.c) -elseif(${PLATFORM} MATCHES "Web") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Os -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") - # Since WASM is used, ALLOW_MEMORY_GROWTH has no extra overheads - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s ALLOW_MEMORY_GROWTH=1 --no-heap-copy") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --shell-file ${CMAKE_SOURCE_DIR}/src/shell.html") - set(CMAKE_EXECUTABLE_SUFFIX ".html") - - # Remove the -rdynamic flag because otherwise emscripten - # does not generate HTML+JS+WASM files, only a non-working - # and fat HTML - string(REPLACE "-rdynamic" "" CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS}") -endif() +elseif (${PLATFORM} MATCHES "Web") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Os -s USE_GLFW=3 -s ASSERTIONS=1 -s WASM=1 -s ASYNCIFY") + # Since WASM is used, ALLOW_MEMORY_GROWTH has no extra overheads + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s ALLOW_MEMORY_GROWTH=1 --no-heap-copy") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --shell-file ${CMAKE_SOURCE_DIR}/src/shell.html") + set(CMAKE_EXECUTABLE_SUFFIX ".html") + + # Remove the -rdynamic flag because otherwise emscripten + # does not generate HTML+JS+WASM files, only a non-working + # and fat HTML + string(REPLACE "-rdynamic" "" CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS}") +endif () include_directories(BEFORE SYSTEM others/external/include) if (NOT TARGET raylib) - find_package(raylib 2.0 REQUIRED) -endif() + find_package(raylib 2.0 REQUIRED) +endif () # Do each example -foreach(example_source ${example_sources}) - # Create the basename for the example - get_filename_component(example_name ${example_source} NAME) - string(REPLACE ".c" "" example_name ${example_name}) - - # Setup the example - add_executable(${example_name} ${example_source}) - - target_link_libraries(${example_name} raylib) - - string(REGEX MATCH ".*/.*/" resources_dir ${example_source}) - string(APPEND resources_dir "resources") - - if(${PLATFORM} MATCHES "Web" AND EXISTS ${resources_dir}) - # The local resources path needs to be mapped to /resources virtual path - string(APPEND resources_dir "@resources") - set_target_properties(${example_name} PROPERTIES LINK_FLAGS "--preload-file ${resources_dir}") - endif() -endforeach() +foreach (example_source ${example_sources}) + # Create the basename for the example + get_filename_component(example_name ${example_source} NAME) + string(REPLACE ".c" "" example_name ${example_name}) + + # Setup the example + add_executable(${example_name} ${example_source}) + + target_link_libraries(${example_name} raylib) + + string(REGEX MATCH ".*/.*/" resources_dir ${example_source}) + string(APPEND resources_dir "resources") + + if (${PLATFORM} MATCHES "Web" AND EXISTS ${resources_dir}) + # The local resources path needs to be mapped to /resources virtual path + string(APPEND resources_dir "@resources") + set_target_properties(${example_name} PROPERTIES LINK_FLAGS "--preload-file ${resources_dir}") + endif () +endforeach () # Copy all of the resource files to the destination file(COPY ${example_resources} DESTINATION "resources/") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bcef058bb..d1d80652b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,6 +10,8 @@ include(JoinPaths) if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) if(RAYLIB_IS_MAIN) set(default_build_type Debug) + else() + message(WARNING "Default build type is not set (CMAKE_BUILD_TYPE)") endif() message(STATUS "Setting build type to '${default_build_type}' as none was specified.") @@ -18,7 +20,7 @@ if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo") endif() -# Get the sources together +# Used as public API to be included into other projects set(raylib_public_headers raylib.h rlgl.h @@ -27,6 +29,7 @@ set(raylib_public_headers raudio.h ) +# Sources to be compiled set(raylib_sources core.c models.c @@ -36,7 +39,7 @@ set(raylib_sources utils.c ) -# cmake/GlfwImport.cmake handles the details around the inclusion of glfw +# /cmake/GlfwImport.cmake handles the details around the inclusion of glfw include(GlfwImport) @@ -47,10 +50,11 @@ else () MESSAGE(STATUS "Audio Backend: None (-DUSE_AUDIO=OFF)") endif () +# Sets additional platform options and link libraries for each platform +# also selects the proper graphics API and version for that platform +# Produces a variable LIBS_PRIVATE that will be used later include(LibraryConfigurations) -set(LIBS_PRIVATE ${LIBS_PRIVATE} ${OPENAL_LIBRARY}) - add_library(raylib ${raylib_sources} ${raylib_public_headers}) if (NOT BUILD_SHARED_LIBS) @@ -66,7 +70,6 @@ else() endif () endif() -# Setting target properties set_target_properties(raylib PROPERTIES PUBLIC_HEADER "${raylib_public_headers}" VERSION ${PROJECT_VERSION} @@ -77,12 +80,11 @@ if (WITH_PIC OR BUILD_SHARED_LIBS) set_property(TARGET raylib PROPERTY POSITION_INDEPENDENT_CODE ON) endif () -# Linking libraries target_link_libraries(raylib "${LIBS_PRIVATE}") -if (${PLATFORM} MATCHES "Desktop") - target_link_libraries(raylib glfw) -endif () +# Sets some compile time definitions for the pre-processor +# If CUSTOMIZE_BUILD option is on you will not use config.h by default +# and you will be able to select more build options include(CompileDefinitions) # Registering include directories @@ -99,6 +101,8 @@ target_include_directories(raylib # Copy the header files to the build directory for convenience file(COPY ${raylib_public_headers} DESTINATION "include") +# Includes information on how the library will be installed on the system +# when cmake --install is run include(InstallConfigurations) # Print the flags for the user @@ -112,6 +116,7 @@ message(STATUS "Compiling with the flags:") message(STATUS " PLATFORM=" ${PLATFORM_CPP}) message(STATUS " GRAPHICS=" ${GRAPHICS}) +# Options if you want to create an installer using CPack include(PackConfigurations) enable_testing()