If-test within ExternalProject_Add/FetchContent_Declare?

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

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:

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.

1 Like

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.