Search code examples
cmakebuildbuild-system

cmake pre-build command to copy files from external repository


In my project, I have an external dependency (a Github repository) from which I want to copy certain files to the source and include directories of my project. I only need a subset of the source and header files in the external repository, so I just want to be able to copy the updated versions of the files I need into the relevant directories in my project. I believe ExternalProject_Add should be the right command to download the files from an external project but I can't figure out how to copy them to my source and include directories before each build. Is there an option of ExternalProject_Add to achieve this, or should a custom build rule be added using add_custom_command which copies the files after the external project is downloaded?


Solution

  • ou can check the CMakeLists.txt of the external repository I want to link to here. I need to compile and install the library and don't want to compile the executables

    You can add the external code either via git submodule if your code is inside a submodule, or by using FetchContent as expalined in the documentation:

    include(FetchContent)
    FetchContent_Declare(
           screamrepo
           GIT_REPOSITORY "https://github.com/EricssonResearch/scream"
    )
    FetchContent_GetProperties(screamrepo)
    if(NOT screamrepo_POPULATED)
      FetchContent_Populate(screamrepo)
    endif()
    # use ${screamrepo_SOURCE_DIR} for source dir
    

    If the external code is well structured and exposes the library you want to have, then:

    add_subdirectory(the/source/path EXCLUDE_FROM_ALL)
    

    and use the library. If not, like in cases you do not like third party cmake configuration or it comes with unsupported configuration, write your own target add_library and configuration and include paths that use the other repository files from source dir.