Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use glad, glm system packages automatically if available #226

Merged
merged 2 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -59,28 +61,44 @@ 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
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_OBJECTS:glad_obj_lib>)
target_link_libraries(forge_glad PUBLIC ${CMAKE_DL_LIBS})
target_include_directories(forge_glad
PUBLIC
$<BUILD_INTERFACE:$<TARGET_PROPERTY:glad_obj_lib,INTERFACE_INCLUDE_DIRECTORIES>>
)
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)
Expand Down Expand Up @@ -175,6 +193,8 @@ mark_as_advanced(
Z_VCPKG_PWSH_PATH
Z_VCPKG_CL
_VCPKG_INSTALLED_DIR
glm_DIR
glad_DIR
)

include(ForgeCPackConfig)
7 changes: 7 additions & 0 deletions CMakeModules/ForgeConfigureDepsVars.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
20 changes: 18 additions & 2 deletions CMakeModules/ForgeInternalUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,26 @@ function(fg_set_target_compilation_props target)

target_include_directories(${target}
SYSTEM PRIVATE
${${glm_prefix}_SOURCE_DIR}
$<TARGET_PROPERTY:Boost::boost,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:glad_obj_lib,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:forge_glad,INTERFACE_INCLUDE_DIRECTORIES>
)
# 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
$<TARGET_PROPERTY:glm::glm,INTERFACE_INCLUDE_DIRECTORIES>
)
elseif(TARGET glm)
target_include_directories(${target}
SYSTEM PRIVATE
$<TARGET_PROPERTY:glm,INTERFACE_INCLUDE_DIRECTORIES>
)
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
$<INSTALL_INTERFACE:${FG_INSTALL_INC_DIR}>
Expand Down
4 changes: 2 additions & 2 deletions src/backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ add_dependencies(${BkndTargetName} ${glsl_shader_targets})

target_sources(${BkndTargetName}
PRIVATE
$<TARGET_OBJECTS:glad_obj_lib>
$<TARGET_OBJECTS:forge_wtk>
$<TARGET_OBJECTS:forge_common_obj_lib>
$<TARGET_OBJECTS:forge_c_api_obj_lib>
Expand All @@ -51,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()
Expand All @@ -61,6 +60,7 @@ endif()
target_link_libraries(${BkndTargetName}
PRIVATE
Boost::boost
forge_glad
${FREETYPE_LIBRARIES}
${CMAKE_DL_LIBS}
)
Expand Down
4 changes: 3 additions & 1 deletion vcpkg/vcpkg.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "forge",
"version-string": "1.1.0",
"version": "1.1.0",
"supports": "x64",
"dependencies": [
"boost-functional",
Expand All @@ -10,7 +10,9 @@
"name": "fontconfig",
"platform": "!windows"
},
"glad",
"glfw3",
"glm",
"sdl2"
]
}