# Excluding a subdirectory from FetchContent

**URL:** <https://discourse.cmake.org/t/excluding-a-subdirectory-from-fetchcontent/5660>\
**Category:** Code\
**Created:** [May 12, 2022, 9:29am UTC](https://discourse.cmake.org/t/excluding-a-subdirectory-from-fetchcontent/5660 "2022-05-12T09:29:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rturrado](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rturrado/32/3499_2.png) [@rturrado](https://discourse.cmake.org/u/rturrado)\
**Post date:** [May 12, 2022, 9:29am UTC](https://discourse.cmake.org/t/excluding-a-subdirectory-from-fetchcontent/5660/1 "2022-05-12T09:29:47Z")

</div>

I’m using FetchContent to add [PDF-Writer](https://github.com/galkahana/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)?

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [May 12, 2022, 11:11am UTC](https://discourse.cmake.org/t/excluding-a-subdirectory-from-fetchcontent/5660/2 "2022-05-12T11:11:23Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![rturrado](https://discourse.cmake.org/user_avatar/discourse.cmake.org/rturrado/32/3499_2.png) [@rturrado](https://discourse.cmake.org/u/rturrado)\
**Post date:** [May 12, 2022, 4:36pm UTC](https://discourse.cmake.org/t/excluding-a-subdirectory-from-fetchcontent/5660/3 "2022-05-12T16:36:02Z")

</div>

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:

```auto
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!
