FetchContent with authentication

I am trying to get FetchContent to download a pre-compiled library from a gitlab repository that requires authentication.
On command line it looks like this:
curl --header ‘PRIVATE-TOKEN: afw7f9sd87f’ https://repo.url/api/v4/projects/mylib

I tried the following, but neither an error message, nor the file downloaded.

FetchContent_Declare(MyLib 
   DOWNLOAD_COMMAND /usr/bin/curl --header 'PRIVATE-TOKEN: afw7f9sd87f' https://repo.url/api/v4/projects/bla-bla
)
FetchContent_MakeAvailable(MyLib)
  • Is it possible to use a custom command to download?
  • How to halt configuration if download fails?
  • is there a better way to get third party, closed source pre-compiled binaries into a build?

Answering myself:

Is it possible to use a custom command to download?

The custom download command works if it is a single string argument to a bash call - at least on Linux.

FetchContent_Declare(MyLib
    DOWNLOAD_COMMAND /usr/bin/bash -c "/usr/bin/curl --header 'PRIVATE-TOKEN ... "
)

How to halt configuration if download fails?

I guess this can/has to be implemented manually.

Since CMake 3.7 there is HTTP_HEADER parameter you could use to set the header without resorting to overwriting DOWNLOAD_COMMAND.

Perfect, thank you. It is documented in ExternalProject
https://cmake.org/cmake/help/latest/module/ExternalProject.html?highlight=http_header

The HTTP_HEADER keyword mentioned by @fenrir is what I’d go with to, but to shed some light on your attempt using curl, CMake doesn’t interpret single quotes like shells do. If you had surrounded the PRIVATE-TOKEN... part with double quotes instead of single quotes, I think it would have worked (it looks like you also have an error where you specified curl twice at the start of the command line, but I assume that was just a typo for the example here).

Hi Craig,
Thanks for your comment.

part with double quotes instead of single quotes, I think it would have worked

Actually I was experimenting with double/singe/no quotes (for half a day) and I did manage to download the remote archive once. But couldn’t reproduce it.

Thanks anyhow.