0

Please consider below CMakeLists.txt.

I am adding an external project if I am unable to find FOO; mylib1 and mylib2 depends on FOO.

This works if I am doing make, it sets up FOO if not found and proceeds towards mylib1 and mylib2.

However if I run make -j4 it does not quite work, as more workers are available it rushes to do mylibX while foo is being setup - end result is mylibX builds fails. Which brings me to my question:

In cmake, what is the idiomatic way to conditionally include a ExternalProject_Add() call?


cmake_minimum_required(VERSION 3.10)
project(myAweSomeLibs VERSION 1.0)

find_library(FOO
        NAMES foo
)

if (FOO)
    message("Found FOO")
else()
    message("Foo not found. Adding external project")
    include(ExternalProject)
    ExternalProject_Add(foobar
      GIT_REPOSITORY    git@github.com:FooCo/FooBar.git
      GIT_TAG           origin/release/1.2.3
    )
endif()

add_subdirectory(mylib1)
add_subdirectory(mylib2)



struggling_learner
  • 1,214
  • 15
  • 29
  • 1
    `more than one -j option` "-j" option? Most probably you improperly set up dependencies and this is unrelated to how you setup your `ExternalProject_Add`. – KamilCuk May 27 '21 at 19:32
  • OK.. I am also trying to get the dependency `FOO` installed if not found. Could you pls show me the idiomatic way to accomplish this? – struggling_learner May 27 '21 at 19:37
  • Idiomatic way for use ExternalProject is described in [that answer](https://stackoverflow.com/a/29324527/3440745). With that way even `make -j` would work correctly. – Tsyvarev May 27 '21 at 19:45
  • 1
    And idiomatic way for use results of `find_library` see there: https://stackoverflow.com/a/10550334/3440745. As you can see, **both** your branches could create an IMPORTED target, which can be used by your other libraries in the **same way**. – Tsyvarev May 27 '21 at 19:49

0 Answers0