Skip to content

Commit

Permalink
CMake: use EXCLUDE_FROM_ALL when declaring Fetch for deflate library
Browse files Browse the repository at this point in the history
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. Ideally we could "manually"
set the EXCLUDE_FROM_ALL property on the deflate SOURCE_DIR and
BINARY_DIR, but a bug that was only fixed as of CMake 3.20.3 prevents
that from properly excluding the directories:
    https://gitlab.kitware.com/cmake/cmake/-/issues/22234

To support the full range of CMake versions without overly complicating
things with cumbersome workarounds, we continue to use Populate for CMake
versions before 3.30, and switch to MakeAvailable for CMake 3.30 and later.

Signed-off-by: Matt Johnson <[email protected]>
  • Loading branch information
mattyjams committed Oct 16, 2024
1 parent fb5e118 commit 0b6a06d
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion cmake/OpenEXRSetup.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,37 @@ 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)
if(NOT deflate_POPULATED)
FetchContent_MakeAvailable(Deflate)
if(CMAKE_VERSION VERSION_LESS "3.30")
# CMake 3.30 deprecated this single argument version of
# FetchContent_Populate():
# https://cmake.org/cmake/help/latest/policy/CMP0169.html
# 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.
# Ideally we could "manually" set the EXCLUDE_FROM_ALL property on the
# deflate SOURCE_DIR and BINARY_DIR, but a bug that was only fixed as of
# CMake 3.20.3 prevents that from properly excluding the directories:
# https://gitlab.kitware.com/cmake/cmake/-/issues/22234
# To support the full range of CMake versions without overly
# complicating the logic here with workarounds, we continue to use
# Populate for CMake versions before 3.30, and switch to MakeAvailable
# for CMake 3.30 and later.
FetchContent_Populate(Deflate)
else()
FetchContent_MakeAvailable(Deflate)
endif()
endif()

# Rather than actually compile something, just embed the sources
Expand Down

0 comments on commit 0b6a06d

Please sign in to comment.