Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Howto include selected sources from third party project in CMake

Tags:

cmake

I've got a CMake based project that depends on external sources that I need to download and extract. I decided to use the ExternalProject_Add to do part of this. The question is how to make an add_library rule depend on some of the extracted files.

Here's what I've got so far:

# This line is needed for the ExternalProject command to work.
# It references the the Module in the cmake distro
include(ExternalProject)

# Download and extract the FreeRTOS core sources
set(FREERTOS_DIR    "${CMAKE_CURRENT_SOURCE_DIR}/FreeRTOS")
set(FREERTOS_URL    "http://downloads.sourceforge.net/project/freertos/FreeRTOS/V6.0.2/FreeRTOSV6.0.2.zip")
set(FREERTOS_CORE_DIR ${FREERTOS_DIR}/Source)

set(FREERTOS_SOURCES "${FREERTOS_CORE_DIR}/croutine.c"
    ${FREERTOS_CORE_DIR}/list.c 
    ${FREERTOS_CORE_DIR}/queue.c 
    ${FREERTOS_CORE_DIR}/tasks.c
    ${FREERTOS_CORE_DIR}/portable/MemMang/heap_3.c)

include_directories(${FREERTOS_CORE_DIR}/include)

add_library(freertos STATIC ${FREERTOS_SOURCES})

ExternalProject_Add(freertos_download
    DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}
    SOURCE_DIR ${FREERTOS_DIR}
    URL ${FREERTOS_URL}
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND "")

NOTE I've disabled the configure, build and install steps in the external project .

As expected because, I haven't specified the dependencies, I get the following error

-- Configuring done
CMake Error at third-party/CMakeLists.txt:20 (add_library):
  Cannot find source file:

    /home/dushara/prj/sw/djetk-demo/djetk/third-party/FreeRTOS/Source/croutine.c

  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
  .hxx .in .txx

I tried using

add_dependencies(freertos_download freertos)

But that wouldn't work.

Ideally I'd like all the items in FREERTOS_SOURCES depend on the extract phase of the external project. Any ideas on how I can achieve this?

like image 757
Dushara Avatar asked Jan 25 '26 04:01

Dushara


1 Answers

CMake will try to see if source files exists for a library during the configure step, but as you experienced the download step of the external project isn't done during configuration.

You can tell CMake not to worry about non-existing files, by setting the GENERATED property on them:

set_source_files_properties(${FREERTOS_SOURCES} PROPERTIES GENERATED TRUE)

You should still add the external project as a dependency to your library to ensure that it is downloaded & extracted first, but in your example the command was incorrect; it should be:

add_dependencies(freertos freertos_download)

so the target that depends on something is the first parameter, and its dependencies listed afterwards.

like image 76
Akos Bannerth Avatar answered Jan 27 '26 21:01

Akos Bannerth