Excluding a subdirectory from FetchContent

I’m using FetchContent to add PDF-Writer to my project.

This PDF-Writer project contains a series of subfolders for different libraries (Jpeg, Zlib…) and for the main library (PDFWriter). However, it also adds a test executable (PDFWriterTestPlayground).

If I just do FetchContent_Declare(PDF-Writer…) and FetchContent_MakeAvailable(PDF-Writer…), I’ll have the extra unwanted PDFWriterTestPlayground executable added to my project.

What’s the way to go in this case?

  • FetchContent_Declare(PDF-Writer…) to download the whole repo (I’ve tried downloading each subdir but without success), then
  • FetchContent_Populate(PDF-Writer), and
  • add_subdirectory(LibJpeg), add_subdirectory(LibZlib), …, add_subdirectory(PDFWriter)?

The PDF-Writer project doesn’t look like it gives you any direct control over whether to pull in that subdirectory or not. You’ve got at least a few ways of tackling this situation.

  • OPTION A: First option is always to see if the upstream project will accept a pull request to make adding that subdirectory depend on an option(). You could then turn that option off before calling FetchContent_MakeAvailable().
  • OPTION B: If that isn’t possible, you could create a fork of the PDF-Writer repo, make that change on your own fork in a branch and point your FetchContent_Declare() call at your fork instead of the official repo.
  • OPTION C: If you don’t want to use a fork, you could use the URL download method in your FetchContent_Declare() call, then add a PATCH_COMMAND that applies the change you want (basically remove the ADD_SUBDIRECTORY(PDFWriterTestPlayground) at the end of the top level CMakeLists.txt file).
1 Like

Hi Craig,

I comment on the options you wrote about:
A) It’s not possible in my case since the creator of the project stopped supporting it in 2019.
B) I thought about this one, but (I don’t know exactly for what reason) I found a better (¿more elegant?) solution to just try and use the existing project.
C) Beautiful. This is indeed somehow what I was looking for. I (think I) had got it working with the code below, but the PATCH_COMMAND seems much a better option:

FetchContent_Declare(PDF-Writer
    GIT_REPOSITORY https://github.com/galkahana/PDF-Writer
    GIT_TAG "52300156971d4f02e698b17ddb2815f8b2414599"
)
FetchContent_GetProperties(PDF-Writer)
if(NOT PDF-Writer_POPULATED)
    FetchContent_Populate(PDF-Writer)
    add_subdirectory(${PDFHUMMUS_SOURCE_DIR}/ZLib ${PDFHUMMUS_BINARY_DIR}/ZLib)
    add_subdirectory(${PDFHUMMUS_SOURCE_DIR}/FreeType ${PDFHUMMUS_BINARY_DIR}/FreeType)
    add_subdirectory(${PDFHUMMUS_SOURCE_DIR}/LibAesgm ${PDFHUMMUS_BINARY_DIR}/LibAesgm)
    add_subdirectory(${PDFHUMMUS_SOURCE_DIR}/LibJpeg ${PDFHUMMUS_BINARY_DIR}/LibJpeg)
    add_subdirectory(${PDFHUMMUS_SOURCE_DIR}/LibPng ${PDFHUMMUS_BINARY_DIR}/LibPng)
    add_subdirectory(${PDFHUMMUS_SOURCE_DIR}/LibTiff ${PDFHUMMUS_BINARY_DIR}/LibTiff)
    add_subdirectory(${PDFHUMMUS_SOURCE_DIR}/PDFWriter ${PDFHUMMUS_BINARY_DIR}/PDFWriter)
endif()
include_directories(${PDFHUMMUS_SOURCE_DIR})

Many thanks for your response!