CMake forwarding parallel builds to make when using ExternalProject_Add on a Autotools-generated project

38 views Asked by At

I'm trying to have CMake to call make in parallel mode when using ExternalProject_Add, but the external project's makefiles aren't being generated by CMake, but with Autotools. I don't want to hard-code the number of jobs, but have it being controlled by CMake.

I'm wrapping the external project with ExternalProject_Add like so:


find_program(MAKE_COMMAND NAMES make REQUIRED)

ExternalProject_Add(qemu
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}
    BINARY_DIR ${PROJECT_BINARY_DIR}
    CONFIGURE_COMMAND
    ${CMAKE_CURRENT_SOURCE_DIR}/qemu/configure --prefix="${CMAKE_INSTALL_PREFIX}/qemu"
    BUILD_COMMAND ${MAKE_COMMAND} -j16
    INSTALL_COMMAND ${MAKE_COMMAND} install
)

I'm calling CMake like so:

cmake -B~/builds -S~/my_project -DCMAKE_INSTALL_PREFIX=~/installs
cmake --build ~/builds -j`nproc`

This works really well, the project gets configured, built and installed.

What I noticed though, is that if I change the build command to just BUILD_COMMAND ${MAKE_COMMAND}, building the external project takes much longer. For me that means that make isn't using parallel builds. I also don't want to just hard-code a number of parallel builds in the build command because there are other parts being built that are CMake-native.

The CMAKE_BUILD_PARALLEL_LEVEL variable determines the maximum number of parallel jobs CMake will use overall when -j is omitted, and I'd the like the exact opposite: to figure out what is the value of num_jobs when CMake is called like cmake --build -jnum_jobs.

This answer hard-codes the number of jobs and uses CMake as the generator. I've also checked this CMake forum post to no avail.

Any input is welcome! Cheers!

1

There are 1 answers

0
Leonardo On

Turns out @fdk1342 's answer is the right way to do it. Even though the external project uses Autotools as the generator and not CMake, CMake can build it:

ExternalProject_Add(qemu
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}
    BINARY_DIR ${PROJECT_BINARY_DIR}
    CONFIGURE_COMMAND
    ${CMAKE_CURRENT_SOURCE_DIR}/qemu/configure --prefix="${CMAKE_INSTALL_PREFIX}/qemu"
    BUILD_COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR} -j
    INSTALL_COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR} --target install
)