Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active September 4, 2023 17:56
Show Gist options
  • Save scivision/2ac78749008520fc5a5f03d727a79886 to your computer and use it in GitHub Desktop.
Save scivision/2ac78749008520fc5a5f03d727a79886 to your computer and use it in GitHub Desktop.
CMake FetchContent specific source files from other project with current project
cmake_minimum_required(VERSION 3.14...3.27)
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
message(FATAL_ERROR "please use out-of-source build
cmake -Bbuild")
endif()
project(otherSource LANGUAGES C)
set(NRF_SDK_NAME nRF5_SDK_17.1.0_ddde560)
if(NOT DEFINED FETCHCONTENT_QUIET)
set(FETCHCONTENT_QUIET FALSE)
endif()
set(CMAKE_TLS_VERIFY true)
# placeholders
set(BOARD_TARGET NRF52805_XXAA)
include(FetchContent)
# https://www.nordicsemi.com/Products/Development-software/nRF5-SDK/Download
set(NRF_SDK_DOWNLOAD_URL
https://www.nordicsemi.com/-/media/Software-and-other-downloads/SDKs/nRF5/Binaries/${NRF_SDK_NAME}.zip
)
# setting hash avoid redownload of big archive on each CMake configure,
# and provides a way to check if the archive is corrupted
set(NRF_SDK_DOWNLOAD_SHA256 5bfe38e744c39fd7f30e10077ba12df306ef91f368894795d6a3e7a62dc68061)
FetchContent_Declare(nrf5_sdk
URL ${NRF_SDK_DOWNLOAD_URL}
URL_HASH SHA256=${NRF_SDK_DOWNLOAD_SHA256}
TLS_VERIFY ${CMAKE_TLS_VERIFY}
INACTIVITY_TIMEOUT 60
)
FetchContent_Populate(nrf5_sdk)
# auto-ignore build dir
file(GENERATE OUTPUT .gitignore CONTENT "*")
# main program
add_library(system_nrf54 ${nrf5_sdk_SOURCE_DIR}/modules/nrfx/mdk/system_nrf52.c)
target_include_directories(system_nrf54 PRIVATE ${nrf5_sdk_SOURCE_DIR}/components/toolchain/cmsis/include)
target_compile_definitions(system_nrf54 PRIVATE ${BOARD_TARGET})
@craigscott-crascit
Copy link

Calling FetchContent_Populate() directly from projects will soon be deprecated, and eventually removed. See this forum post where I raised this recently. The timing of when this will occur isn't clear yet, but I'm aiming for either CMake 3.28 or 3.29 as the most likely timeframe.

@scivision
Copy link
Author

Okay I just saw that on discourse as well. What would I do then to download someone else's Git project but then use my own CMake script to build all or part of it. For example, I use a few big long-time projects but their CMakeLists is like CMake 2.6 era and they don't seem to care about fixing it. So I use my own CMake script to build their project via FetchContent.

@craigscott-crascit
Copy link

See the following from the FetchContent docs:

New in version 3.18: The SOURCE_SUBDIR option can be given in the declared details to look somewhere below the top directory instead (i.e. the same way that SOURCE_SUBDIR is used by the ExternalProject_Add() command). The path provided with SOURCE_SUBDIR must be relative and will be treated as relative to the top directory. It can also point to a directory that does not contain a CMakeLists.txt file or even to a directory that doesn't exist. This can be used to avoid adding a project that contains a CMakeLists.txt file in its top directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment