ExternalProject_Add may fail if CMAKE_MAKE_PROGRAM for CMAKE_GENERATOR is not on PATH
When using ExternalProject_Add on a project that has CMAKE_MAKE_PROGRAM set but no binary for the CMAKE_GENERATOR exists on the PATH, the generated build script won't be able to compile the external project.
This is specially noticeable when using CLion, given it bundles CMake, Ninja, and other tools that may not be installed on the computer when building a project. The issue has been tracked by JetBrains and triaged as not actionable (https://youtrack.jetbrains.com/issue/CPP-26828), since build scripts should to pass the value of CMAKE_MAKE_PROGRAM to every new instance of CMake forked from the original process invoked by the IDE.
I noticed the problem when working on the Raspberry PI Pico Examples repository (http://github.com/raspberrypi/pico-examples). If you open the repository in CLion on a computer that doesn't have Ninja installed (default generator set by the IDE), the build will fail when it hits the code generated by ExternalProject_Add. It spawns a new CMake process with CMAKE_MAKE_PROGRAM unset, falling to build the project since there is no binary for Ninja available on the PATH.
====================[ Build | blink | Debug ]===================================
"/Users/pedro/Library/Application Support/JetBrains/Toolbox/apps/CLion/ch-0/221.5921.27/CLion.app/Contents/bin/cmake/mac/bin/cmake" --build /Users/pedro/repos/pico-examples/cmake-build-debug --target blink -j 3
[8/59] Performing configure step for 'ELF2UF2Build'
FAILED: blink/elf2uf2/src/ELF2UF2Build-stamp/ELF2UF2Build-configure /Users/pedro/repos/pico-examples/cmake-build-debug/blink/elf2uf2/src/ELF2UF2Build-stamp/ELF2UF2Build-configure
cd /Users/pedro/repos/pico-examples/cmake-build-debug/elf2uf2 && "/Users/pedro/Library/Application Support/JetBrains/Toolbox/apps/CLion/ch-0/221.5921.27/CLion.app/Contents/bin/cmake/mac/bin/cmake" -GNinja /Users/pedro/repos/pico-examples/cmake-build-debug/_deps/pico_sdk-src/tools/elf2uf2 && "/Users/pedro/Library/Application Support/JetBrains/Toolbox/apps/CLion/ch-0/221.5921.27/CLion.app/Contents/bin/cmake/mac/bin/cmake" -E touch /Users/pedro/repos/pico-examples/cmake-build-debug/blink/elf2uf2/src/ELF2UF2Build-stamp/ELF2UF2Build-configure
CMake Error: CMake was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
See also "/Users/pedro/repos/pico-examples/cmake-build-debug/elf2uf2/CMakeFiles/CMakeOutput.log".
[9/59] Generating bs2_default_padded_checksummed.S
ninja: build stopped: subcommand failed.
After some investigation, I was able to fix the problem by adding the following code to the _ep_extract_configure_command
function of the Modules/ExternalProject.cmake
file.
if(CMAKE_MAKE_PROGRAM)
list(APPEND cmd "-DCMAKE_MAKE_PROGRAM:FILEPATH=${CMAKE_MAKE_PROGRAM}")
endif()