Add include files for Github ExternalProject library

I’m running out of ideas how to fix this. I have a CMakeList used by the catkin_tools (ROS) build system. Furthermore, I’m using catkin_simple, which defines some macros. However, the problem I have is more related to the ExternalProjects_Add function, which I use to get my files from Github to add them as a library.

Having local files in a source folder with an include folder, everything went as expected. When I started downloading them directly from Github, I had the problem that the headers can not be found, because they are not in the source include folder as before. I added cs_export(INCLUDE_DIRS ${SOURCE_DIR}/AdsLib) which now tries to find the folder before the folders are even cloned.

This is my file which does not work:

cmake_minimum_required(VERSION 2.8.3)
project(ads_catkin)

find_package(catkin_simple REQUIRED)
catkin_simple()

include(ExternalProject)

ExternalProject_Add(ads
    PREFIX ${CATKIN_DEVEL_PREFIX}/ads
    GIT_REPOSITORY https://github.com/Beckhoff/ADS.git
    CONFIGURE_COMMAND ""
    UPDATE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND ""
    BUILD_BYPRODUCTS 
        <SOURCE_DIR>/AdsLib/AdsDef.cpp
        <SOURCE_DIR>/AdsLib/AdsLib.cpp
        ...
        <SOURCE_DIR>/AdsLib/AdsDef.h
        <SOURCE_DIR>/AdsLib/AdsLib.h
        ...
)

ExternalProject_Get_Property(ads SOURCE_DIR)


include_directories(
    ${SOURCE_DIR}/AdsLib
)

cs_add_library(AdsLib
    ${SOURCE_DIR}/AdsLib/AdsDef.cpp
    ${SOURCE_DIR}/AdsLib/AdsLib.cpp
    ...
)
add_dependencies(AdsLib ads)

cs_install()

install(DIRECTORY ${SOURCE_DIR}/AdsLib/
        DESTINATION ${CATKIN_GLOBAL_INCLUDE_DESTINATION}
        FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"  
)


cs_export(
    INCLUDE_DIRS ${SOURCE_DIR}/AdsLib
)

As a comparison, here is a file that works perfectly fine with local files:

cmake_minimum_required(VERSION 2.8.3)

project(ads_twincat)

find_package(catkin_simple REQUIRED)
catkin_simple()

add_definitions(-std=c++11 -Wall -Wextra)

cs_add_library(${PROJECT_NAME}
    src/AdsDef.cpp
    src/AdsLib.cpp
    ...
)

cs_install()
cs_export()

ExternalProject wouldn’t be the tool I would use to solve this problem, since ExternalProject does all the operations such as git clone at build time.

Instead what you should use is FetchContent as it is designed to fetch the repository at configure time, and therefore everything exists for subsequent calls such as export

The problem with FetchContent is that it’s not available for Cmake 3.10 and I can’t install this on the target devices.

You could try to backport the FetchContent to CMake 3.10, or use the predecessor to it that @craig.scott wrote: https://github.com/Crascit/DownloadProject

It’s more that this catkin wrapper should run out of the box without any further installations. It’s intended for large ROS projects which require the ADS library, but I do not want the user to install anything additional.

Yes, place a copy of FetchContent, or DownloadProject inside the project which you use.

Alright, works like a charm with just leaving out the add_subdirectory building step.

One remaining question: All the .cpp and .hpp files are in the same source folder and, therefore, included by include <AdsLib.h> etc. From these files a library is built. Does this now mean that I also need to put them on top level in the include folder, or can I create a sub-folder include/AdsLib?

You can create a subdirectory, but you will likely need to make sure you have a target_include_directories or include_directories statement for that new subdirectory.

1 Like