通过 CMake 运行 bash 命令 [英] Running a bash command via CMake
问题描述
我正在尝试让 CMake 运行三个 bash 命令或一个 bash 脚本.但是,我似乎无法让它工作.
bash 命令是:
cd ${CMAKE_SOURCE_DIR}/dependencies/library制作cd ${CMAKE_BINARY_DIR}
本质上,如果该目录不存在,我希望 CMake 在该目录中构建该库.
这是我尝试过的 CMake 代码:
if(NOT "${CMAKE_SOURCE_DIR}/dependencies/library/lib.o")执行进程(命令 cd ${CMAKE_SOURCE_DIR}/dependencies/library)执行进程(命令生成)执行进程(命令 cd ${CMAKE_BINARY_DIR})endif(NOT "${CMAKE_SOURCE_DIR}/dependencies/library/lib.o")
但是,它没有构建任何东西.我究竟做错了什么?
另外,当我在这里问这个问题时:是否应该包含第三个命令来移动到二进制文件夹?
谢谢!
execute_process()
在配置期间执行.但是您希望它在构建时运行,因此 add_custom_command()
和 add_custom_target()
就是你要找的.p>
在这种特殊情况下,您想生成一个输出文件,因此您应该使用 add_custom_command()
(两者本质上是相同的,但 command
会产生一个或多个输出文件,而 target
没有.
为此的 cmake 代码段应如下所示:
add_custom_command(输出 ${CMAKE_SOURCE_DIR}/dependencies/library/lib.oWORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/dependencies/library命令使)
然后,您必须将输出文件作为依赖项添加到另一个目标中,并且一切都应该(希望)按预期工作.
您还可以将 DEPENDS
语句添加到 add_custom_command()
调用中,以在某些输入源发生更改的情况下重建目标文件.
I'm trying to have CMake either run three bash commands or a bash script. However, I can't seem to get it to work.
The bash commands are:
cd ${CMAKE_SOURCE_DIR}/dependencies/library
make
cd ${CMAKE_BINARY_DIR}
Essentially, I would like CMake to build the library in that directory if it does not already exist.
Here's the CMake code I tried:
if(NOT "${CMAKE_SOURCE_DIR}/dependencies/library/lib.o")
execute_process(COMMAND cd ${CMAKE_SOURCE_DIR}/dependencies/library)
execute_process(COMMAND make)
execute_process(COMMAND cd ${CMAKE_BINARY_DIR})
endif(NOT "${CMAKE_SOURCE_DIR}/dependencies/library/lib.o")
However, it's not building anything. What am I doing wrong?
Also, while I'm here asking this: should the third command, to move to the binary folder, be included?
Thanks!
execute_process()
is executed during configure time. But you want this to run at build time, thus add_custom_command()
and add_custom_target()
is what you're looking for.
In this special case you want to generate an output file, so you should go for add_custom_command()
(both are essentially the same, but command
produces one or multiple output files, while target
does not.
The cmake snippet for this should look something like the following:
add_custom_command(
OUTPUT ${CMAKE_SOURCE_DIR}/dependencies/library/lib.o
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/dependencies/library
COMMAND make
)
You then have to add the output file in another target as dependency, and everything should (hopefully) work as expected.
You can also add DEPENDS
statements to the add_custom_command()
call to rebuild the object file in case some input sources have changed.
这篇关于通过 CMake 运行 bash 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!