我能否从命令行运行任意的CMake函数?

4
我希望能做类似以下这样的事情:
$ cmake -E "find_package(Boost)"

是为了查看是否成功或失败?(即,我可以在项目中进行一些依赖项检查,并在CMakeLists.txt实际运行之前尝试安装它们所有。)

我知道CMake已经有了-E标志,但当我运行它时,只会得到一个可用命令的小列表:

Usage: cmake -E [command] [arguments ...]
Available commands:
  chdir dir cmd [args]...   - run command in a given directory
  compare_files file1 file2 - check if file1 is same as file2
  copy file destination     - copy file to destination (either file or directory)
  copy_directory source destination   - copy directory 'source' content to directory 'destination'
  copy_if_different in-file out-file  - copy file if input has changed
  echo [string]...          - displays arguments as text
  echo_append [string]...   - displays arguments as text but no new line
  environment               - display the current environment
  make_directory dir        - create a directory
  md5sum file1 [...]        - compute md5sum of files
  remove [-f] file1 file2 ... - remove the file(s), use -f to force it
  remove_directory dir      - remove a directory and its contents
  rename oldname newname    - rename a file or directory (on one volume)
  tar [cxt][vfz][cvfj] file.tar [file/dir1 file/dir2 ...]
                            - create or extract a tar or zip archive
  time command [args] ...   - run command and return elapsed time
  touch file                - touch a file.
  touch_nocreate file       - touch a file but do not create it.
Available on UNIX only:
  create_symlink old new    - create a symbolic link new -> old
1个回答

5

有一个-P <source.cmake> 参数,可以让CMake进入脚本处理模式。您可以使用它来执行任意的CMake代码

execute_process(COMMAND ${CMAKE_PROGRAM} -P yourscript.cmake)

但是从您的 CMakeLists.txt 中调用 find_package(Boost) 并没有什么问题,因为如果没有找到 Boost,它不会立即失败,而是提供了一种检查此情况并执行其他操作的方法。

所有依赖项检查都应在您的 CMakeLists.txt 中完成,而不是外部脚本中完成。


你介意看一下 https://stackoverflow.com/questions/66481629/how-to-call-a-cmake-method-from-shell-script 吗? - TheWaterProgrammer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接