Is it possible to set URL option for FetchContent_Declare() using a CMake variable?

I would like to define a CMake (v3.30.2) function that uses FetchContent_Declare() and FetchContent_MakeAvailable() to download some artifact from my GitLab instance.

The URL is

https://gitlab.example.com/api/v4/projects/<project-id>/jobs/<job-id>/artifacts

where

  • <project-id> is the project’s ID
  • <job-id> is the job ID from some pipeline that is part of the project

Since it’s a private repository I need to pass an access token. In addition, in order to make it more flexible, I would like to set the project-id and job-id using my function’s arguments.

Below is the current state of my function

include(FetchContent)

function(download_file_gitlab name token proj_id job_id)

    set(URL_STR "https://gitlab.example.com/api/v4/projects/${proj_id}/jobs/${job_id}/artifacts")
    message("URL set to ${URL_STR}")

    FetchContent_Declare(download_${name}
        QUIET
        URL "${URL_STR}"
        HTTP_HEADER: "PRIVATE-TOKEN: ${token}"
        # BINARY_DIR: "${CMAKE_BINARY_DIR}/${name}"
    )

    if(NOT download_${name}_POPULATED)
        FetchContent_MakeAvailable(download_${name})
    endif()
endfunction(download_file_gitlab)

which I call like this

download_file_gitlab(
        release
        sometokenthatworks
        68522
        7013628
    )

The rest of the CMakeLists.txt can be anything so the content is not important. I am currently literally using an almost empty one with the cmake_minimum_required() and project() followed by the FetchContent part and function call.

cmake -Bbuild -G "Visual Studio 16 2019"

-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19043.
-- The C compiler identification is MSVC 19.29.30152.0
-- The CXX compiler identification is MSVC 19.29.30152.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
URL set to https://gitlab.example.com/api/v4/projects/68522/jobs/7013628/artifacts
CMake Warning (dev) at C:/Program Files/CMake/share/cmake-3.30/Modules/FetchContent.cmake:1373 (message):
  The DOWNLOAD_EXTRACT_TIMESTAMP option was not given and policy CMP0135 is
  not set.  The policy's OLD behavior will be used.  When using a URL
  download, the timestamps of extracted files should preferably be that of
  the time of extraction, otherwise code that depends on the extracted
  contents might not be rebuilt if the URL changes.  The OLD behavior
  preserves the timestamps from the archive instead, but this is usually not
  what you want.  Update your project to the NEW behavior or specify the
  DOWNLOAD_EXTRACT_TIMESTAMP option with a value of true to avoid this
  robustness issue.
Call Stack (most recent call first):
  CMakeLists.txt:26 (FetchContent_Declare)
  CMakeLists.txt:56 (download_file_gitlab)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19043.
CMake Error at C:/Program Files/CMake/share/cmake-3.30/Modules/ExternalProject/shared_internal_commands.cmake:1127 (message):
  At least one entry of URL is a path (invalid in a list)
Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.30/Modules/ExternalProject.cmake:3035 (_ep_add_download_command)
  CMakeLists.txt:22 (ExternalProject_Add)

-- Configuring incomplete, errors occurred!

CMake Error at C:/Program Files/CMake/share/cmake-3.30/Modules/FetchContent.cmake:1906 (message):
  CMake step for download_imgui-release failed: 1
Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.30/Modules/FetchContent.cmake:1609 (__FetchContent_populateSubbuild)
  C:/Program Files/CMake/share/cmake-3.30/Modules/FetchContent.cmake:2145:EVAL:2 (__FetchContent_doPopulation)
  C:/Program Files/CMake/share/cmake-3.30/Modules/FetchContent.cmake:2145 (cmake_language)
  C:/Program Files/CMake/share/cmake-3.30/Modules/FetchContent.cmake:2384 (__FetchContent_Populate)
  CMakeLists.txt:33 (FetchContent_MakeAvailable)
  CMakeLists.txt:56 (download_file_gitlab)


-- Configuring incomplete, errors occurred!

Note that the artifact does exist and I am able to download it, e.g. using cURL inside the Git Bash:

curl -X GET -H "PRIVATE-TOKEN: sometokenthatworks" https://gitlab.example.com/api/v4/projects/68522/jobs/7013628/artifacts > artifacts.zip

The resulting ZIP archive can be viewed and extracted.

I am not even sure if I can pass variables to FetchContent_Declare() and was unable to find information on that in the CMake documentation (v3.30.2).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật