ExternalProject fails on Windows with strange errors:

I need to build and install an old openssl lib both in debug and release on windows.

Unfortunately, the does not work out of the box.

with this setup

ExternalProject_Add(
    openssl
    DOWNLOAD_EXTRACT_TIMESTAMP TRUE
    PREFIX ${CMAKE_BINARY_DIR}
    #--Download step--------------
    URL ${OPENSSL_URL}
    URL_HASH SHA256=${SHA256}
    #--Update/Patch step----------
    #--Configure step-------------
    USES_TERMINAL_CONFIGURE TRUE
    # see build/src/openssl/Configure
    # first:
    #     build/src/openssl-stamp/openssl-configure-Debug.cmake
    #     build/src/openssl-stamp/openssl-configure-err.log
    #     build/src/openssl-stamp/openssl-configure-out.log
    # second:
    #     build/src/openssl-stamp/openssl-build-Debug.cmake
    #     build/src/openssl-stamp/openssl-build-err.log
    #     build/src/openssl-stamp/openssl-build-out.log
    CONFIGURE_COMMAND
        cd <SOURCE_DIR> && ${PERL_PROGRAM} Configure ${CONFIG_DEBUG_PREFIX}${OS_CONFIG_SETUP} no-asm no-hw no-krb5
        --prefix=${CMAKE_INSTALL_PREFIX} ${GENERATE_MAKE_FILES}
    #--Build step-----------------
    USES_TERMINAL_BUILD TRUE
    BUILD_COMMAND
        cd <SOURCE_DIR> && ${MAKE_PROGRAM}
        # UNIX: ${MAKE_PROGRAM} -C <SOURCE_DIR>
    #--Install step---------------
    USES_TERMINAL_INSTALL TRUE
    INSTALL_COMMAND
        cd <SOURCE_DIR> && ${MAKE_PROGRAM} ${INSTALL_SW}
        # UNIX: ${MAKE_PROGRAM} -C <SOURCE_DIR> ${INSTALL_SW}
    #--Logging -------------------
    LOG_DOWNLOAD OFF
    LOG_CONFIGURE ${OPENSSL_WRITE_LOG}
    LOG_BUILD ${OPENSSL_WRITE_LOG}
    LOG_INSTALL OFF
)

I see this output

-- extracting...
     src='C:/Users/KLEIN_CL/Workspace/Qt/openssl-cmake/build/src/openssl-1.0.2u.tar.gz'
     dst='C:/Users/KLEIN_CL/Workspace/Qt/openssl-cmake/build/src/openssl'
-- extracting... [tar xfz]
-- extracting... [analysis]
-- extracting... [rename]
-- extracting... [clean up]
-- extracting... done
[12/20] Performing configure step for 'openssl'CMake Error at C:/Users/KLEIN_CL/Workspace/Qt/openssl-cmake/build/src/openssl-stamp/openssl-configure-Debug.cmake:49 (message):
  Command failed: no such file or directory

   'cd' 'C:/Users/KLEIN_CL/Workspace/Qt/openssl-cmake/build/src/openssl' '&&' 'C:/Strawberry/perl/bin/perl.exe' 'Configure' 'debug-VC-WIN32' 'no-asm' 'no-hw' 'no-krb5' '--prefix=/tmp/install' '&&' 'ms\do_nt.bat'

  See also

    C:/Users/KLEIN_CL/Workspace/Qt/openssl-cmake/build/src/openssl-stamp/openssl-configure-*.log



FAILED: src/openssl-stamp/openssl-configure C:/Users/KLEIN_CL/Workspace/Qt/openssl-cmake/build/src/openssl-stamp/openssl-configure
C:\Windows\system32\cmd.exe /C "cd /D C:\Users\KLEIN_CL\Workspace\Qt\openssl-cmake\build\src\openssl-build && C:\Python\3.12\Lib\site-packages\cmake\data\bin\cmake.exe -P C:/Users/KLEIN_CL/Workspace/Qt/openssl-cmake/build/src/openssl-stamp/openssl-configure-Debug.cmake && C:\Python\3.12\Lib\site-packages\cmake\data\bin\cmake.exe -E touch C:/Users/KLEIN_CL/Workspace/Qt/openssl-cmake/build/src/openssl-stamp/openssl-configure"
ninja: build stopped: subcommand failed.

C:\Users\KLEIN_CL\Workspace\Qt\openssl-cmake>

The project files may be found here:

Usage and envrionment:

C:\Users\KLEIN_CL\Workspace\Qt\openssl-cmake>cmake -B build -S . -G Ninja -D CMAKE_BUILD_TYPE=Debug -D CMAKE_INSTALL_PREFIX=/tmp/install --fresh
-- The C compiler identification is MSVC 19.0.24247.2
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- FIND_LIBRARY_USE_LIB64_PATHS:
-- CMAKE_LIBRARY_ARCHITECTURE:
-- CMAKE_LINK_LIBRARY_SUFFIX: .lib
-- OpenSSL will be installed to:
-- Include directory: /tmp/install/include
-- Library directory: /tmp/install/lib
-- Configuring done (5.0s)
-- Generating done (0.2s)
-- Build files have been written to: C:/Users/KLEIN_CL/Workspace/Qt/openssl-cmake/build

C:\Users\KLEIN_CL\Workspace\Qt\openssl-cmake>cmake --version
cmake version 3.31.2

CMake suite maintained and supported by Kitware (kitware.com/cmake).

C:\Users\KLEIN_CL\Workspace\Qt\openssl-cmake>

I found my problem:

The used execute_process() does’nt allow on windows multible commands like:

CONFIGURE_COMMAND
        cd <SOURCE_DIR> && ${PERL_PROGRAM} Configure ...

And for the build and install command, I have to use the BUILD_IN_SOURCE option to prevent:

cd <SOURCE_DIR> && ${MAKE_PROGRAM} ${INSTALL_SW}