Cmake if add_custom_target 中的条件(目录存在)在某些构建中失败?

问题描述 投票:0回答:1

我已经看到了CMake:如何在add_custom_command(...)中使用if条件 - 但我根本无法让以下内容工作:在add_custom_target中,检查目录是否存在,如果不存在,则输出消息- 我尝试将其实现为

bash
单行代码。

奇怪的是,我无法在 PC 上重现此错误 - 只能在带有定制 cmake-3.28.1 的 Raspberry Pi 上重现:

$ uname -a
Linux raspberry 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l GNU/Linux
$ cat /etc/issue
Raspbian GNU/Linux 9 \n \l

这是一个例子

CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
project(ProjectName)

SET(OPT_DEBS "/opt/debs")
add_custom_target(check_opt_debs
  ALL
  COMMENT "Checking for existence of ${OPT_DEBS} directory"
  COMMAND /bin/bash -c "[ ! -d '${OPT_DEBS}' ] && echo 'Directory ${OPT_DEBS} DOES NOT exist; please run \\`sudo mkdir /opt/debs\\` to create it, before running \\`make package\\`/\\`cpack\\` !!'"
)

然后我就这样跑:

$ /home/pi/src/cmake-3.28.1/bin/cmake .
-- The C compiler identification is GNU 6.3.0
-- The CXX compiler identification is GNU 6.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (2.1s)
-- Generating done (0.0s)
-- Build files have been written to: /tmp

...当我运行

make
时,我得到:

/bin/sh: 1:  echo Directory\ /opt/debs\ DOES\ NOT\ exist;\ please\ run\ \`sudo\ mkdir\ /opt/debs\`\ to\ create\ it,\ before\ running\ \`make\ package\`/\`cpack\`\ !!: not found
CMakeFiles/check_opt_debs.dir/build.make:70: recipe for target 'CMakeFiles/check_opt_debs' failed
make[2]: *** [CMakeFiles/check_opt_debs] Error 127
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/check_opt_debs.dir/all' failed
make[1]: *** [CMakeFiles/check_opt_debs.dir/all] Error 2
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2

我以为我已经正确地转义了所有内容(事实上,当在计算机上的 cmake 上运行时,此代码片段不会失败) - 为什么它会在这里失败?

linux cmake
1个回答
0
投票

必须用两个反斜杠

&
转义该死的&符号
\\
,以及用一个反斜杠
;
转义分号
\
- 然后它就可以工作了(不要问我为什么)。

更正

CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
project(ProjectName)

SET(OPT_DEBS "/opt/debs")
add_custom_target(check_opt_debs
  ALL
  COMMENT "Checking for existence of ${OPT_DEBS} directory"
  COMMAND /bin/bash -c "[ ! -d '${OPT_DEBS}' ] \\&\\& echo 'Directory ${OPT_DEBS} DOES NOT exist\; please run \\`sudo mkdir /opt/debs\\` to create it, before running \\`make package\\`/\\`cpack\\` !!'" # works
)

这是它工作时的结果:

$ make
[100%] Checking for existence of /opt/debs directory
Directory /opt/debs DOES NOT exist; please run `sudo mkdir /opt/debs` to create it, before running `make package`/`cpack` !!
[100%] Built target check_opt_debs
© www.soinside.com 2019 - 2024. All rights reserved.