# If-test within ExternalProject\_Add/FetchContent\_Declare?

**URL:** <https://discourse.cmake.org/t/if-test-within-externalproject-add-fetchcontent-declare/1556>\
**Category:** Code\
**Created:** [July 14, 2020, 3:57pm UTC](https://discourse.cmake.org/t/if-test-within-externalproject-add-fetchcontent-declare/1556 "2020-07-14T15:57:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)\
**Post date:** [July 14, 2020, 3:57pm UTC](https://discourse.cmake.org/t/if-test-within-externalproject-add-fetchcontent-declare/1556/1 "2020-07-14T15:57:10Z")

</div>

A query for the experts. Namely, I have a patch I’d like to apply to either a `FetchContent_Declare` or `ExternalProject_Add` command. And reading the doc I see there is a `PATCH_COMMAND` that can be used. Nice. (Note I’m not sure which one I’ll have to add it too because [I’m using some weirdness](https://discourse.cmake.org/t/can-you-combine-fetchcontent-and-externalproject/228/2) that @craig.scott helped me out with.)

But the challenge is that this patch can _only_ be applied if the Fortran compiler is NAG Fortran. And my Google-Fu just cannot find examples where there is an if-test _inside_ a `FetchContent_Declare` or `ExternalProject_Add`. Is there a way to do so? (I’ve tried some simple things that didn’t seem to work, but at the same time I was changing other stuff that was bad so I’m trying to untangle that as well.)

---

<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:** [July 14, 2020, 10:41pm UTC](https://discourse.cmake.org/t/if-test-within-externalproject-add-fetchcontent-declare/1556/2 "2020-07-14T22:41:44Z")

</div>

You shouldn’t need to put the logic inside the call to `FetchContent_Declare()`. You should be able to test that condition before the call and only include the patch command if needed. Something like the following pseudo-code should get you close:

```cmake
if(some-condition-you-want-to-test)
    set(patchCommand PATCH_COMMAND whatever-you-want-to-do)
else()
    unset(patchCommand)
endif()
FetchContent_Declare(someDep
    URL ...
    URL_HASH ...
    ${patchCommand}
)

```

Beware that PATCH\_COMMAND is only really intended/suitable for use with URL. It doesn’t play nice with other download methods such as GIT\_REPOSITORY.

---

<div class="post-metadata">

**Author:** ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)\
**Post date:** [July 16, 2020, 2:40pm UTC](https://discourse.cmake.org/t/if-test-within-externalproject-add-fetchcontent-declare/1556/3 "2020-07-16T14:40:10Z")

</div>

Huh. Well I feel dumb. I was trying all sorts of fancy stuff (I definitely learned about generator expressions in toy projects). I guess I didn’t realize you could pass a “blank” command to Fetch. And I didn’t even know about `unset()`.

And, yes, these are almost all URLs. Since my libraries mainly build with `configure`, using release tarballs is nearly always better than Git repos where you have to do `autoreconf` manually.

I’ll try this out soon, @craig.scott. Thanks.
