Olumide
Olumide

Reputation: 5839

Building cmake project with dozens of package dependencies in different subdirectories

I am trying to build an example project that requires dozens of packages contained in different subdirectories, for example

/home/Olumide/src/project/Release/modules/module1/CMakeFiles/Export/lib/cmake/module1    
/home/Olumide/src/project/Release/modules/module2/CMakeFiles/Export/lib/cmake/module2
/home/Olumide/src/project/Release/modules/module3/CMakeFiles/Export/lib/cmake/module3
...

Given that the application that I'm trying to build is in the location /home/Olumide/src/project/applications/tutorial, is it possible to build the application without explicitly specifying the paths to all each package that the project requires, or modifying the CMakeLists.txt file, for example by specify a common root path to be searched for all the packages, for example:

cmake -DCMAKE_MODULE_PATH=/home/Olumide/src/project/Release/modules .

Unfortunately this does not work and cmake complains that it cannot find package configuration files with the names Modul1eConfig.cmake or module1-config.cmake etc.

Upvotes: 0

Views: 57

Answers (1)

fabian
fabian

Reputation: 82521

If all of the modules adhere tho this exact pattern you should be able to provide the "root path" via cache variable and pass a the info where to look for the libs to find_package via PATHS parameter:

# maybe a more reasonable default here?
set(MYPROJECT_MODULE_PATH "/home/Olumide/src/project/Release/modules" CACHE PATH "the path to the modules we're looking for")

set(MYPROJECT_MODULE_NAMES
    module1
    module2
    ...
)

foreach(_MODULE IN LISTS MYPROJECT_MODULE_NAMES)
    find_package(${_MODULE} REQUIRED CONFIG
        PATHS "${MYPROJECT_MODULE_PATH}/${_MODULE}/CMakeFiles/Export"
    )
endforeach()

In general if these packages logically belong to the same software package, you should provide a way of finding those modules by passing them as components for find_package but since this seems to be code not under your own control you may not have that luxury. (E.g. boost provides this functionality: find_package(boost COMPONENTS test mpl ...).) However it may be worth some investigation if there is a configuration script somewhere that's supposed to automatically include the modules, if listed as components, e.g. somewhere in Release or Release/modules.

Upvotes: 1

Related Questions