How to properly invoke MSYS2 Bash commands using CMake execute_process ()?

Description of the problem

I had a problem setting up the CMake external_process () command, which executes the MSYS2 bash command. When I am in the MSYS2 shell, if I run the command $ bash -v ./bootstrap.sh, the command works correctly. But if I run the CMake script in the MSYS2 shell using $ cmake -P Run_bash_command.cmake, the command skips part of the path through the process. The important part of the information that I found in the CMake Documentation makes me think that I am not calling bash correctly or there is no environment variable:

CMake executes the child process directly using the operating system APIs. All arguments are passed VERBATIM to the child process. An intermediate shell is not used, therefore shell operators such as> are treated as ordinary arguments.

I would like to be able to make this command using CMake, if possible, as this problem is part of a much larger project for the CMake supermachine. If there is another approach to solving the problem, I am open to suggestions, so far I can include it in the automation of the super paper project. Any help would be appreciated.

Contents of Run_bash_command.cmake:

SET( ENV{MSYSTEM} MINGW64 )
SET( DIR_CONTAINING_BOOTSTRAP_SH C:/bash_test )
SET( BASH_COMMAND_TO_RUN bash -v ./bootstrap.sh )

EXECUTE_PROCESS( COMMAND ${BASH_COMMAND_TO_RUN}
          WORKING_DIRECTORY ${DIR_CONTAINING_BOOTSTRAP_SH} RESULT_VARIABLE command_result )

IF( NOT "${command_result}" STREQUAL "0" )
    MESSAGE( FATAL_ERROR "Error: command_result='${command_result}'" )
ENDIF()

Environment setup

  • I followed the instructions to configure MSYS2 64bit and added the toolchain mingw-w64 as well as cmake using the commandpacman -S base-devel git mingw-w64-x86_64-cmake mingw-w64-x86_64-toolchain
  • MinGW-w64 Win64 Shell, MSYS2
  • bootstrap.sh libusb github c:/bash_test

$ bash -v ./bootstrap.sh :

$ bash -v ./bootstrap.sh
#!/bin/sh

if ! test -d m4 ; then
    mkdir m4
fi
autoreconf -ivf || exit 1
autoreconf: Entering directory `.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal --force -I m4
autoreconf: configure.ac: tracing
autoreconf: running: libtoolize --copy --force
libtoolize: putting auxiliary files in '.'.
libtoolize: copying file './ltmain.sh''
...<clipped output due to length>...
configure.ac:29: installing './install-sh'
configure.ac:29: installing './missing'
examples/Makefile.am: installing './depcomp'
autoreconf: Leaving directory `.'

$ cmake -P Run_bash_command.cmake :

$ cmake -P Run_bash_command.cmake
#!/bin/sh

if ! test -d m4 ; then
    mkdir m4
fi
autoreconf -ivf || exit 1
autoreconf: Entering directory `.'
autoreconf: configure.ac: not using Gettext
autoreconf: running: aclocal --force -I m4
aclocal-1.15: error: aclocal: file '/msys64/usr/share/aclocal/xsize.m4' does not exist
autoreconf: aclocal failed with exit status: 1
CMake Error at Run_bash_command.cmake:10 (MESSAGE):
  Error: command_result='1'

, :

  • bash -l -c, , , bootstrap.sh
  • , bash PATH
  • MSYS2
  • sh bash
  • autoreconf -ivf ,
  • Unix Windows
+4
1

, .

Run_bash_command.cmake:

SET( DIR_CONTAINING_BOOTSTRAP_SH /C/bash_test )
SET( BASH_COMMAND_TO_RUN bash -l -c "cd ${DIR_CONTAINING_BOOTSTRAP_SH} && sh ./bootstrap.sh" )

EXECUTE_PROCESS( COMMAND ${BASH_COMMAND_TO_RUN}
                WORKING_DIRECTORY ${DIR_CONTAINING_BOOTSTRAP_SH} RESULT_VARIABLE command_result )

IF( NOT "${command_result}" STREQUAL "0" )
    MESSAGE( FATAL_ERROR "Error: command_result='${command_result}'" )
ENDIF()

:

  • bash -l , , change cd <path>, , bash.
  • -c bash . : script, && , "", , .
+1

All Articles