Skip to content

Commit f9961b9

Browse files
aminyaalandefreitas
authored andcommitted
feat: support OpenGL dependencies with config
1 parent d0b1d2d commit f9961b9

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

Matplot++Config.cmake.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ if(NOT ${MATPLOT_BUILT_SHARED})
1616
list(APPEND CMAKE_MODULE_PATH ${MATPLOT_CONFIG_INSTALL_DIR})
1717
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
1818
find_dependency(Filesystem COMPONENTS Experimental Final)
19+
# OpenGL backend
20+
if (@MATPLOTPP_BUILD_EXPERIMENTAL_OPENGL_BACKEND@)
21+
find_dependency(glad CONFIG)
22+
find_dependency(glfw3 CONFIG)
23+
endif()
1924
list(POP_BACK CMAKE_MODULE_PATH)
2025
endif()
2126

source/matplot/CMakeLists.txt

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -214,40 +214,49 @@ if (MATPLOTPP_BUILD_EXPERIMENTAL_OPENGL_BACKEND)
214214
# The biggest con of the OpenGL backend is that it cannot open a window
215215
# in another thread. All it can do is get in the middle of the render
216216
# loop and draw the plot.
217-
find_package(OpenGL REQUIRED)
217+
find_package(OpenGL)
218218

219219
# https://github.com/Dav1dde/glad
220-
find_package(GLAD QUIET)
221-
if (NOT GLAD_FOUND AND NOT TARGET glad)
222-
# Use CPM only if not found, to avoid ODR violations
223-
# find_package(GLAD REQUIRE) would suffice if it worked well
224-
FetchContent_Declare(glad GIT_REPOSITORY https://github.com/Dav1dde/glad.git GIT_TAG df8e9e16110b305479a875399cee13daa0ccadd9)
225-
FetchContent_MakeAvailable(glad)
226-
else ()
227-
# FindGLAD does not usually create a target, so we create an interface target
228-
if (NOT TARGET glad)
229-
add_library(glad INTERFACE)
230-
target_include_directories(glad INTERFACE ${GLAD_INCLUDE_PATH})
231-
target_link_libraries(glad INTERFACE ${GLAD_LIBRARIES})
232-
endif ()
233-
endif ()
220+
find_package(glad CONFIG)
221+
if (NOT glad_FOUND)
222+
find_package(GLAD QUIET)
223+
if (NOT GLAD_FOUND AND NOT TARGET glad)
224+
# Use CPM only if not found, to avoid ODR violations
225+
# find_package(GLAD REQUIRE) would suffice if it worked well
226+
FetchContent_Declare(glad GIT_REPOSITORY https://github.com/Dav1dde/glad.git GIT_TAG df8e9e16110b305479a875399cee13daa0ccadd9)
227+
FetchContent_MakeAvailable(glad)
228+
else ()
229+
if(TARGET glad)
230+
add_library(glad::glad ALIAS glad)
231+
endif()
232+
if(NOT TARGET glad::glad)
233+
# FindGLAD does not usually create a target, so we create an interface target
234+
add_library(glad::glad INTERFACE)
235+
target_include_directories(glad::glad INTERFACE ${GLAD_INCLUDE_PATH})
236+
target_link_libraries(glad::glad INTERFACE ${GLAD_LIBRARIES})
237+
endif()
238+
endif()
239+
endif()
234240

235241
# https://github.com/glfw/glfw
236-
find_package(glfw3 QUIET)
237-
if (NOT GLFW3_FOUND AND NOT TARGET glfw)
238-
# Use CPM only if not found, to avoid ODR violations
239-
# find_package(glfw3 REQUIRE) would suffice if it worked well
240-
FetchContent_Declare(glfw3 GIT_REPOSITORY https://github.com/glfw/glfw.git GIT_TAG 3.3.2)
241-
FetchContent_MakeAvailable(glfw3)
242-
endif ()
242+
find_package(glfw3 CONFIG)
243+
if (NOT glfw3_FOUND)
244+
find_package(glfw3 QUIET)
245+
if (NOT GLFW3_FOUND AND NOT TARGET glfw)
246+
# Use CPM only if not found, to avoid ODR violations
247+
# find_package(glfw3 REQUIRE) would suffice if it worked well
248+
FetchContent_Declare(glfw3 GIT_REPOSITORY https://github.com/glfw/glfw.git GIT_TAG 3.3.2)
249+
FetchContent_MakeAvailable(glfw3)
250+
endif ()
251+
endif()
243252

244253
add_library(matplot_opengl
245254
backend/opengl_embed.h
246255
backend/opengl_embed.cpp
247256
backend/opengl.h
248257
backend/opengl.cpp
249258
)
250-
target_link_libraries(matplot_opengl PUBLIC matplot glad glfw ${CMAKE_DL_LIBS})
259+
target_link_libraries(matplot_opengl PUBLIC matplot glad::glad glfw ${CMAKE_DL_LIBS})
251260

252261
list(APPEND TARGETS matplot_opengl)
253262
endif()

0 commit comments

Comments
 (0)