JohnTheCompilator
JohnTheCompilator

Reputation: 43

CMAKE : Build only one directory of an downloaded external project

CMAKE : I only want to build part of a downloadable external project

The external project I want to use has the following structure :

- ExternalProject
    - Subproject A  <- this i care for
    - Subproject B

It's all in the same archive that is available for download. The problem is that Subproject B has more dependencies that I don't want in my project, also it's not relevant for what I'm doing. The subprojects are buildable on their own, so for now I just took Subproject A out of the archive and put it in my main project which is working fine, but I'd like to not deploy the external project with my project but allow the user to download the external project on his own when running cmake. Sadly there is no archive for each of the subprojects on their own so all I can download is the full external project.

What I want is to tell CMAKE to download and unpack the whole archive ExternalProject but then only add Subproject A to my project. I read all the documentation on ExternalProject_add but it mostly allows for detailed configuration of the build parameters of the project. Maybe I'm just looking for the wrong keyword and my question is really simple to answer - or it is just not possible.

If someone could point me towards the right approach here I'd be very thankful.

Actual project : The mentioned archive has two subfolders "octomap" and "octovis". octomap is the one I want to build while octovis will create a lot of errors if the dependencies are not met.

ExternalProject_Add(octomap-1.6.5
    URL https://github.com/OctoMap/octomap/archive/v1.6.5.tar.gz
    URL_MD5 de09b1189a03ac8cbe4f813951e86605
     SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR}/octomap/
    CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_DIR}"
)

Upvotes: 2

Views: 4835

Answers (1)

user2288008
user2288008

Reputation:

You can define all commands explicitly. For instance you have a directory structure:

FooBar/
        - CMakeLists.txt
        - Foo/
        -      - CMakeLists.txt # You wanna build
        - Bar/
        -      - CMakeLists.txt # You wanna ignore

Example with hardcoded paths:

set(ext_dir "${CMAKE_BINARY_DIR}/third-party-activities/ExternalProject/")
set(foobar_dir "${ext_dir}/FooBar")

ExternalProject_Add(
    FooBar
    URL "your-url-here"
    SOURCE_DIR "${foobar_dir}/Source"
    CONFIGURE_COMMAND
        "${CMAKE_COMMAND}"
        "-H${foobar_dir}/Source/Foo"
        "-B${foobar_dir}/Builds"
        "-DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_DIR}"
    BUILD_COMMAND
        "${CMAKE_COMMAND}" --build "${foobar_dir}/Builds"
    INSTALL_COMMAND
        "${CMAKE_COMMAND}" --build "${foobar_dir}/Builds" --target install
)

Update

Note that it's much easier to patch parent project like this:

# FooBar/CMakeLists.txt
option(BUILD_BAR_SUBPROJECT "Build targets from subproject Bar" ON)
...
add_subdirectory(Foo)
if(BUILD_BAR_SUBPROJECT)
  add_subdirectory(Bar)
endif()

... and now you don't need to hack ExternalProject_Add so much:

ExternalProject_Add(
    FooBar
    URL "your-url-here"
    CMAKE_ARGS -DBUILD_BAR_SUBPROJECT=OFF "-DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_DIR}"
)

Upvotes: 4

Related Questions