Random bug when building makefile project by using ExternalProject_Add()

Hello,

I use ExternalProject_Add() to build Open MPI project which uses make file project. When I build the project from command line there is no issue. When I use ExternalProject_Add(), it fails randomly. Sometime it build, sometime it does not.

System Info

macOS 15.4.1 (24E263)
cmake version 3.31.3
AppleClang 17.0.0.17000013
automake 1.17

This is how I build the project from the command line by using make without any issues:

wget https://download.open-mpi.org/release/open-mpi/v5.0/openmpi-5.0.7.tar.gz
tar xf openmpi-5.0.7.tar.gz
cd openmpi-5.0.7
mkdir build
cd build
../configure --prefix=$PWD/installed --enable-static --disable-shared 
make -j 8
make install

The default instruction above builds and installs the project as expected without any issues. I’ve tried many times.

The random issues happen when I use ExternalProject_Add() to build the make project.

Here is the CMake file I use to build the project.

include(ExternalProject)
include(ProcessorCount)

function(add_external_git_project_with_make)
    set(options)
    set(oneValueArgs NAME URL URL_HASH URL_MD5 GIT_REPOSITORY GIT_TAG GIT_SHALLOW EXTERNALS_BIN_DIR)
    set(multiValueArgs CONFIG_ARGS)
    cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    message(STATUS "Configuring External Project: ${ARG_NAME}")
    set(lib_dir "${ARG_EXTERNALS_BIN_DIR}/${ARG_NAME}")

    # By default, GIT_SHALLOW is ON.
    if ("${ARG_GIT_SHALLOW}" STREQUAL "")
        set(ARG_GIT_SHALLOW ON)
    endif()

    # Get processor count.
    ProcessorCount(NUM_PROCESSORS)
    if(NUM_PROCESSORS EQUAL 0)
        set(NUM_PROCESSORS 1)
    endif()

    # Find make command.
    find_program(MAKE_EXECUTABLE NAMES make gmake nmake)
    if(NOT MAKE_EXECUTABLE)
        message(FATAL_ERROR "Could not find make executable.")
    endif()

    ExternalProject_Add(
            ${ARG_NAME}
            URL             ${ARG_URL}
            URL_HASH        ${ARG_URL_HASH}
            URL_MD5         ${ARG_URL_MD5}
            GIT_REPOSITORY  ${ARG_GIT_REPOSITORY}
            GIT_TAG         ${ARG_GIT_TAG}
            GIT_SHALLOW     ${ARG_GIT_SHALLOW}
            PREFIX          "${lib_dir}/prefix"
            SOURCE_DIR      "${lib_dir}/src"
            STAMP_DIR       "${lib_dir}/stamp"
            BINARY_DIR      "${lib_dir}/build"
            INSTALL_DIR     "${lib_dir}/install"
            DOWNLOAD_DIR    "${lib_dir}/download"
            LOG_DIR         "${lib_dir}/log"
            LOG_DOWNLOAD ON
            LOG_CONFIGURE ON
            LOG_BUILD ON
            LOG_INSTALL ON
            LOG_UPDATE ON
            LOG_PATCH ON
            LOG_TEST ON
            LOG_MERGED_STDOUTERR ON
            LOG_OUTPUT_ON_FAILURE ON
            GIT_SUBMODULES_RECURSE ON
            GIT_PROGRESS OFF
            BUILD_ALWAYS OFF
            CONFIGURE_COMMAND ${lib_dir}/src/configure --prefix=${lib_dir}/install ${ARG_CONFIG_ARGS}
            BUILD_COMMAND     ${MAKE_EXECUTABLE} -j ${NUM_PROCESSORS}
            INSTALL_COMMAND   ${MAKE_EXECUTABLE} install
            UPDATE_COMMAND    ""
    )

    # Make include and lib folders available to prevent linker warnings.
    file(MAKE_DIRECTORY "${lib_dir}/install/include" "${lib_dir}/install/lib")

    include_directories(${lib_dir}/install/include)
    link_directories(${lib_dir}/install/lib)
endfunction()


# Build Open MPI
add_external_git_project_with_make(
        NAME                openmpi_cpp
        URL                 https://download.open-mpi.org/release/open-mpi/v5.0/openmpi-5.0.7.tar.gz
        CONFIG_ARGS         --enable-static
                            --disable-shared
        EXTERNALS_BIN_DIR   ${EXTERNALS_BINARY_DIR}
)

The random build issue I get:

[ 84%] Performing build step for 'openmpi_cpp'
CMake Error at /Users/bliss/projects/ompi/build-rel/Externals/openmpi_cpp/stamp/openmpi_cpp-build-release.cmake:37 (message):
  Command failed: 2

   '/usr/bin/make' '-j' '8'

  See also

    /Users/bliss/projects/ompi/build-rel/Externals/openmpi_cpp/log/openmpi_cpp-build.log


-- Log output is:
make[3]: warning: -jN forced in submake: disabling jobserver mode.
 cd /Users/bliss/projects/ompi/build-rel/Externals/openmpi_cpp/src && /bin/sh /Users/bliss/projects/ompi/build-rel/Externals/openmpi_cpp/src/config/missing automake-1.16 --foreign
/Users/bliss/projects/ompi/build-rel/Externals/openmpi_cpp/src/config/missing: line 81: automake-1.16: command not found
WARNING: 'automake-1.16' is missing on your system.
         You should only need it if you modified 'Makefile.am' or
         'configure.ac' or m4 files included by 'configure.ac'.
         The 'automake' program is part of the GNU Automake package:
         <https://www.gnu.org/software/automake>
         It also requires GNU Autoconf, GNU m4 and Perl in order to run:
         <https://www.gnu.org/software/autoconf>
         <https://www.gnu.org/software/m4/>
         <https://www.perl.org/>
make[3]: *** [/Users/bliss/projects/ompi/build-rel/Externals/openmpi_cpp/src/Makefile.in] Error 1
make[3]: *** Waiting for unfinished jobs....
aclocal.m4:17: warning: this file was generated for autoconf 2.71.
You have another version of autoconf.  It may work, but is not guaranteed to.
If you have problems, you may need to regenerate the build system entirely.
To do so, use the procedure documented by the package, typically 'autoreconf'.
sh: config/opal_get_version.sh: No such file or directory
sh: config/opal_get_version.sh: No such file or directory
sh: config/opal_get_version.sh: No such file or directory
sh: config/opal_get_version.sh: No such file or directory
sh: config/opal_get_version.sh: No such file or directory
sh: config/opal_get_version.sh: No such file or directory
sh: config/opal_get_version.sh: No such file or directory
sh: config/opal_get_version.sh: No such file or directory
sh: config/opal_get_version.sh: No such file or directory
sh: config/opal_get_version.sh: No such file or directory
sh: config/opal_get_version.sh: No such file or directory
sh: config/opal_get_version.sh: No such file or directory

CMake Error at /Users/bliss/projects/ompi/build-rel/Externals/openmpi_cpp/stamp/openmpi_cpp-build-release.cmake:47 (message):
  Stopping after outputting logs.


make[2]: *** [Externals/openmpi_cpp/stamp/openmpi_cpp-build] Error 1
make[1]: *** [CMakeFiles/openmpi_cpp.dir/all] Error 2
make: *** [all] Error 2

I feel that this is a bug. I would love to get a solution. Happy to provide more information.

Thank you for your help in advance.

Aaron

I found a solution from the similar issue explained below:

I use touch command for the following files as a pre-configuration step, and it looks like the issue has been fixed. I’m still not sure if this is a CMake issue or not since we only update timestamp of the files before the configuration step.

Could this be handled by CMake?

Here is the code part I updated:

# ...

            CONFIGURE_COMMAND COMMAND ${CMAKE_COMMAND} -E touch ${lib_dir}/src/aclocal.m4
                                                                ${lib_dir}/src/configure
                                                                ${lib_dir}/src/configure.ac
                                                                ${lib_dir}/src/Makefile.am
                                                                ${lib_dir}/src/Makefile.in
                              COMMAND ${lib_dir}/src/configure --prefix=${lib_dir}/install ${ARG_CONFIG_ARGS}
            BUILD_COMMAND     ${MAKE_EXECUTABLE}

# ...

Thanks,
Aaron