如何使用CMake execute_process()正确调用MSYS2 Bash命令?
问题描述
我在设置执行MSYS2 bash命令的CMake external_process()命令时遇到问题.当我在MSYS2 shell中运行命令$bash -v ./bootstrap.sh时,该命令正常工作.但是如果我使用$cmake -P Run_bash_command.cmake在MSYS2 shell中运行CMake脚本,那么命令会在整个过程中出错.我在CMake Documentation中找到的一条重要信息让我觉得我没有正确调用bash或者缺少环境变量:
如果可能的话,我希望能够使用CMake执行此命令,因为此问题是更大的CMake superbuild项目的一部分.如果有另一种解决问题的方法,只要我可以将其包含在superbuild项目的自动化中,我就可以接受建议.任何帮助将不胜感激. 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() 环境设置 >我按照指示到setup MSYS2 64位并使用命令pacman -S base-devel git mingw-w64-x86_64-cmake mingw-w64-x86_64-toolchain添加了mingw-w64工具链以及cmake 产量 $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但这导致shell默认为主目录,然后它无法找到文件bootstrap.sh 解决方法
我能够使用下面的代码解决问题.
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导致shell默认为主目录,为了解决这个问题,我添加了一个更改目录cd< path>命令让我们回到我们要发出bash命令的目录.>使用-c会导致bash从字符串中读取命令.由于我们要发出两个命令,一个用于更改目录,另一个用于运行shell脚本,我们需要使用&&将命令链接在一起以及使用“引号”以确保整个命令作为字符串正确读取. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |