<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Feb 23, 2019 at 4:14 PM Timothy Wrona <<a href="mailto:tjwrona1992@gmail.com" target="_blank">tjwrona1992@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">I am working on a CMake project that depends on a couple other projects I have previously written. I would like to include those other projects using "ExternalProject_Add", but I am having some issues.<div><br></div><div>The basic layout of the project is this:</div><div><br></div><div><font face="monospace, monospace">cmake_minimum_required(VERSION 3.14.0)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">project(ProjectWithDependencies)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">include(ExternalProject)</font></div><div><font face="monospace, monospace">ExternalProject_Add(Dependency1</font></div><div><font face="monospace, monospace">    GIT_REPOSITORY <URL to git repo></font></div><div><font face="monospace, monospace">)</font></div><div><font face="monospace, monospace">ExternalProject_Add(Dependency2</font></div><div><font face="monospace, monospace">    GIT_REPOSITORY <URL to git repo></font></div><div><font face="monospace, monospace">)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">find_package(Dependency1 Required)</font></div><div><font face="monospace, monospace">find_package(Dependency2 Required)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"># Use targets</font></div><div><br></div><div>I was under the assumption that "ExternalProject_Add" would automatically build and install the dependencies before getting to the "find_package" calls (they are CMake projects so the default build and install commands should be fine) but this doesn't seem to be how it works.</div><div><br></div><div>When I get to "find_package" it fails because the dependency hasn't been installed yet.</div><div><br></div><div>How can I ensure that the dependencies are fully compiled and installed before attempting to find them? (Note: FetchContent doesn't work here because the two projects "Dependency1" and "Dependency2" have targets with the same name which causes FetchContent to fail.)</div><div><br></div><div>Any help is appreciated.</div></div></blockquote></div><div><br></div><div>find_package() requires that the dependencies have been built already, as you've noted. When CMake runs and processes your example CMakeLists.txt file, it sets up the build rules for Dependency1 and Dependency2, but they won't actually be configured, built and installed until you build the main project. Since your find_package() calls will be processed during the configure stage of the main project (i.e. before the build stage), it's too early.</div><div><br></div><div>To address this, you need to make your main project a true superbuild where it basically contains nothing more than a set of ExternalProject_Add() calls. Rather than putting the find_package() calls and use of targets directly in the main build, you can add it as another sub-build. Note that this will mean that none of the targets you define in the mainBuild will be visible as targets in your top level superbuild project. I'd also advise you to override the default install location of your dependencies. Otherwise they will typically try to install to a system-wide location, which is not usually a good thing. Putting it together, the top level superbuild project would look something like this:</div><div><br></div><div><font face="monospace, monospace">cmake_minimum_required(VERSION 3.14.0)</font></div><div><span style="font-family:monospace,monospace">project(</span><span style="font-family:monospace,monospace">ProjectWithDependencies)</span><br></div><div><font face="monospace, monospace"><br></font></div><div><span style="font-family:monospace,monospace">set(installDir ${CMAKE_CURRENT_BINARY_DIR}/install)</span><br></div><div><span style="font-family:monospace,monospace"><br></span></div><font face="monospace, monospace"></font><div><font face="monospace, monospace">include(ExternalProject)</font></div><div><span style="font-family:monospace,monospace">ExternalProject_Add(Dependency1</span><br></div></div><div><font face="monospace, monospace">    GIT_REPOSITORY <URL to git repo></font></div><div dir="ltr"><font face="monospace, monospace">    INSTALL_DIR    ${installDir}<br>    CMAKE_ARGS     -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR><br>)</font><div><div dir="ltr"><font face="monospace, monospace">ExternalProject_Add(Dependency2</font></div><div><font face="monospace, monospace">    GIT_REPOSITORY <URL to git repo></font></div><div dir="ltr"><font face="monospace, monospace">    INSTALL_DIR    ${installDir}<br>    CMAKE_ARGS     -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR><br>)</font></div></div><div><div><font face="monospace, monospace">ExternalProject_Add(mainBuild</font></div><div><font face="monospace, monospace">    SOURCE_DIR  <path_to_some_subdir_in_this_repo></font></div><span style="font-family:monospace,monospace">    INSTALL_DIR ${installDir}</span><br style="font-family:monospace,monospace"><div><font face="monospace, monospace">    DEPENDS     Dependency1 Dependency2</font></div><span style="font-family:monospace,monospace">    CMAKE_ARGS  -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR></span><br style="font-family:monospace,monospace"><span style="font-family:monospace,monospace">                -DCMAKE_PREFIX_PATH:PATH=<INSTALL_DIR></span><br style="font-family:monospace,monospace"><div><font face="monospace, monospace">)</font></div></div><div><br></div><div>Or you could make the superbuild a completely separate repo and then use GIT_REPOSITORY rather than SOURCE_DIR for the "mainBuild" part.</div><div><br></div>-- <br><div dir="ltr" class="gmail-m_-3616315565010079985gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div dir="ltr">Craig Scott<br><div>Melbourne, Australia</div><div><a href="https://crascit.com" target="_blank">https://crascit.com</a><br></div><div><br></div><div>Get the hand-book for every CMake user: <a href="https://crascit.com/professional-cmake/" target="_blank">Professional CMake: A Practical Guide</a><br></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div>