From 3baa78d5ca30de0184fb7fe97d8528876108bd1a Mon Sep 17 00:00:00 2001 From: pradeep Date: Wed, 21 Apr 2021 17:33:19 +0530 Subject: [PATCH 1/2] Use glad, glm system packages automatically if available In the case they aren't, then FetchContent from CMake is used to retrieve the source code for glm and glad during CMake configure time. Tested the cmake logic in the following scenarios: - glad and glm are available via package manager like vcpkg - One of the two, glad and glm, is only available via package manager - None of the two, glad and glm are not installed on the system This change also added glm, glad to vcpkg manifest file --- CMakeLists.txt | 48 +++++++++++++++++------ CMakeModules/ForgeConfigureDepsVars.cmake | 7 ++++ CMakeModules/ForgeInternalUtils.cmake | 20 +++++++++- src/backend/CMakeLists.txt | 2 +- vcpkg/vcpkg.json | 4 +- 5 files changed, 64 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b8b6b07..62fe1c0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,8 @@ find_package(FontConfig QUIET) find_package(FreeImage QUIET) find_package(Freetype REQUIRED) find_package(Sphinx QUIET) +find_package(glad CONFIG QUIET) +find_package(glm CONFIG QUIET) if(UNIX) dependency_check(FontConfig_FOUND @@ -67,20 +69,38 @@ if(Boost_FOUND AND NOT TARGET Boost::boost) PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${Boost_INCLUDE_DIRS}) endif(Boost_FOUND AND NOT TARGET Boost::boost) -FetchContent_Declare( - ${glad_prefix} - GIT_REPOSITORY https://github.com/arrayfire/glad.git - GIT_TAG obj_lib -) -FetchContent_Populate(${glad_prefix}) -FetchContent_Declare( - ${glm_prefix} - GIT_REPOSITORY https://github.com/g-truc/glm.git - GIT_TAG 0.9.9.8 -) -FetchContent_Populate(${glm_prefix}) +if(NOT TARGET glad::glad) # find_package(glad) failed + FetchContent_Declare( + ${glad_prefix} + GIT_REPOSITORY https://github.com/arrayfire/glad.git + GIT_TAG obj_lib + ) + fg_check_and_populate(${glad_prefix}) + add_subdirectory(${${glad_prefix}_SOURCE_DIR} ${${glad_prefix}_BINARY_DIR}) + + add_library(forge_glad STATIC $) + target_link_libraries(forge_glad PUBLIC ${CMAKE_DL_LIBS}) + target_include_directories(forge_glad + PUBLIC + $> + ) +else() + add_library(forge_glad ALIAS glad::glad) +endif() + +if(NOT TARGET glm::glm AND NOT TARGET glm) # find_package(glm) failed + FetchContent_Declare( + ${glm_prefix} + GIT_REPOSITORY https://github.com/g-truc/glm.git + GIT_TAG 0.9.9.8 + ) + fg_check_and_populate(${glm_prefix}) + add_library(glm INTERFACE IMPORTED) + set_target_properties(glm PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "${${glm_prefix}_SOURCE_DIR}" + ) +endif() -add_subdirectory(${${glad_prefix}_SOURCE_DIR} ${${glad_prefix}_BINARY_DIR}) add_subdirectory(src/backend/common) add_subdirectory(src/backend/glsl_shaders) add_subdirectory(src/api/c) @@ -175,6 +195,8 @@ mark_as_advanced( Z_VCPKG_PWSH_PATH Z_VCPKG_CL _VCPKG_INSTALLED_DIR + glm_DIR + glad_DIR ) include(ForgeCPackConfig) diff --git a/CMakeModules/ForgeConfigureDepsVars.cmake b/CMakeModules/ForgeConfigureDepsVars.cmake index 5e6ae171..4b64ebde 100644 --- a/CMakeModules/ForgeConfigureDepsVars.cmake +++ b/CMakeModules/ForgeConfigureDepsVars.cmake @@ -43,3 +43,10 @@ if(FG_BUILD_OFFLINE) set_fetchcontent_src_dir(assets_prefix "glad") set_fetchcontent_src_dir(testdata_prefix "glm") endif() + +macro(fg_check_and_populate prefix) + FetchContent_GetProperties(${prefix}) + if(NOT ${prefix}_POPULATED) + FetchContent_Populate(${prefix}) + endif() +endmacro() diff --git a/CMakeModules/ForgeInternalUtils.cmake b/CMakeModules/ForgeInternalUtils.cmake index 50897e4b..595e1f06 100644 --- a/CMakeModules/ForgeInternalUtils.cmake +++ b/CMakeModules/ForgeInternalUtils.cmake @@ -91,10 +91,26 @@ function(fg_set_target_compilation_props target) target_include_directories(${target} SYSTEM PRIVATE - ${${glm_prefix}_SOURCE_DIR} $ - $ + $ ) + # As imported targets can't be aliased until CMake 3.11, below is a temporary + # work around until we move to a CMake version >= 3.11 + if(TARGET glm::glm) + target_include_directories(${target} + SYSTEM PRIVATE + $ + ) + elseif(TARGET glm) + target_include_directories(${target} + SYSTEM PRIVATE + $ + ) + else() + # Ideally it wouldn't reach here, just in case of corner case + # I couldn't think of or someother cmake bug + message(FATAL_ERROR "Please install glm dependency") + endif() target_include_directories(${target} PUBLIC $ diff --git a/src/backend/CMakeLists.txt b/src/backend/CMakeLists.txt index c6393895..b976f8b6 100644 --- a/src/backend/CMakeLists.txt +++ b/src/backend/CMakeLists.txt @@ -34,7 +34,6 @@ add_dependencies(${BkndTargetName} ${glsl_shader_targets}) target_sources(${BkndTargetName} PRIVATE - $ $ $ $ @@ -61,6 +60,7 @@ endif() target_link_libraries(${BkndTargetName} PRIVATE Boost::boost + forge_glad ${FREETYPE_LIBRARIES} ${CMAKE_DL_LIBS} ) diff --git a/vcpkg/vcpkg.json b/vcpkg/vcpkg.json index 664b771d..e9322b6e 100644 --- a/vcpkg/vcpkg.json +++ b/vcpkg/vcpkg.json @@ -1,6 +1,6 @@ { "name": "forge", - "version-string": "1.1.0", + "version": "1.1.0", "supports": "x64", "dependencies": [ "boost-functional", @@ -10,7 +10,9 @@ "name": "fontconfig", "platform": "!windows" }, + "glad", "glfw3", + "glm", "sdl2" ] } From d6df6bfe981843a5283cf90809b6d8fd400cbba5 Mon Sep 17 00:00:00 2001 From: pradeep Date: Thu, 22 Apr 2021 17:49:56 +0530 Subject: [PATCH 2/2] Use correct LINK keyword for static freeimage linking --- CMakeLists.txt | 2 -- src/backend/CMakeLists.txt | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 62fe1c0f..6c89c9ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,8 +61,6 @@ fg_deprecate(WITH_FREEIMAGE FG_WITH_FREEIMAGE) fg_deprecate(USE_STATIC_FREEIMAGE FG_USE_STATIC_FREEIMAGE) fg_deprecate(WITH_TOOLKIT FG_USE_WINDOW_TOOLKIT) -message(STATUS "FreeImage Support turned ON") - if(Boost_FOUND AND NOT TARGET Boost::boost) add_library(Boost::boost INTERFACE IMPORTED) set_property(TARGET Boost::boost diff --git a/src/backend/CMakeLists.txt b/src/backend/CMakeLists.txt index b976f8b6..ed63af26 100644 --- a/src/backend/CMakeLists.txt +++ b/src/backend/CMakeLists.txt @@ -50,7 +50,7 @@ target_include_directories(${BkndTargetName} if(FG_WITH_FREEIMAGE) target_compile_definitions(${BkndTargetName} PRIVATE USE_FREEIMAGE) if(FG_USE_STATIC_FREEIMAGE) - target_link_libraries(${BkndTargetName} PRIVATE FreeImage::FreeImage_STATIC) + target_link_libraries(${BkndTargetName} PUBLIC FreeImage::FreeImage_STATIC) else() target_link_libraries(${BkndTargetName} PRIVATE FreeImage::FreeImage) endif()