From 6b69f9240b6cfd127fa90da0bca61e34857ea018 Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Fri, 11 Oct 2024 09:22:28 -0400 Subject: [PATCH] CMake: use EXCLUDE_FROM_ALL when declaring Fetch for deflate library Commit af9ef0e made a change in the CMake setup to switch from using FetchContent_Populate() to FetchContent_MakeAvailable() to bring in libdeflate (and Imath) as dependencies. Using FetchContent_MakeAvailable() has the side effect that libdeflate is now being included as part of the build, causing libraries and headers to be built and installed. The intent with fetching the deflate source is solely to copy select files into OpenEXRCore, so this change adds the EXCLUDE_FROM_ALL option to the call to FetchContent_Declare() to prevent deflate from being included in the build. One wrinkle though is that prior to CMake 3.28, passing the EXCLUDE_FROM_ALL option to FetchContent_Declare() does *not* have the desired effect of excluding the fetched content from the build when FetchContent_MakeAvailable() is called, so we "manually" exclude it for those CMake versions. --- cmake/OpenEXRSetup.cmake | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/cmake/OpenEXRSetup.cmake b/cmake/OpenEXRSetup.cmake index 3a31710e0..01ad5960e 100644 --- a/cmake/OpenEXRSetup.cmake +++ b/cmake/OpenEXRSetup.cmake @@ -219,10 +219,13 @@ else() message(STATUS "libdeflate was not found, installing from ${OPENEXR_DEFLATE_REPO} (${OPENEXR_DEFLATE_TAG})") endif() include(FetchContent) + # Fetch deflate but exclude it from the "all" target. + # This prevents the library from being built. FetchContent_Declare(Deflate GIT_REPOSITORY "${OPENEXR_DEFLATE_REPO}" GIT_TAG "${OPENEXR_DEFLATE_TAG}" GIT_SHALLOW ON + EXCLUDE_FROM_ALL ) FetchContent_GetProperties(Deflate) @@ -230,6 +233,20 @@ else() FetchContent_MakeAvailable(Deflate) endif() + if(CMAKE_VERSION VERSION_LESS "3.28") + # Prior to CMake 3.28, passing the EXCLUDE_FROM_ALL option to + # FetchContent_Declare() does *not* have the desired effect of + # excluding the fetched content from the build when + # FetchContent_MakeAvailable() is called, so we "manually" + # exclude it for those CMake versions. + if(IS_DIRECTORY "${deflate_SOURCE_DIR}") + set_property(DIRECTORY ${deflate_SOURCE_DIR} PROPERTY EXCLUDE_FROM_ALL YES) + endif() + if(IS_DIRECTORY "${deflate_BINARY_DIR}") + set_property(DIRECTORY ${deflate_BINARY_DIR} PROPERTY EXCLUDE_FROM_ALL YES) + endif() + endif() + # Rather than actually compile something, just embed the sources # into exrcore. This could in theory cause issues when compiling as # a static library into another application which also uses