CMake FetchContent ignore build system

CMake FetchContent and ExternalProject bring remote projects into the top-level project to avoid making a monorepo or vendoring code into the top-level project. With FetchContent, the source code is retrieved at CMake configure time, allowing one to ignore the subproject build system and / or use only specific source files.

An example of this is using nRF5 SDK, which is a large project, but one may only wish to use a single source file and header as in this example:

view raw README.md hosted with ❤ by GitHub
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})
view raw CMakeLists.txt hosted with ❤ by GitHub