【问题标题】:Can I run an arbitrary CMake function from the command line?我可以从命令行运行任意 CMake 函数吗?
【发布时间】:2023-04-03 19:44:01
【问题描述】:

我想做这样的事情:

$ cmake -E "find_package(Boost)"

看看是成功还是失败? (即,我可以对项目进行一些依赖性检查,并尝试在 cmake 实际运行 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

【问题讨论】:

    标签: cmake


    【解决方案1】:

    -P <source.cmake> 标志,将CMake 置于脚本处理模式。您可以使用它执行任意 CMake 代码

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

    但是从您的 CMakeLists.txt 调用 find_package(Boost) 并没有错,因为如果没有找到 Boost,它不会立即失败,而是为您提供检查这一点并执行其他操作的方法。

    所有依赖关系检查都应该在您的 CMakeLists.txt 内部的某个地方完成,而不是外部脚本。

    【讨论】:

    猜你喜欢
    • 2013-06-01
    • 2010-09-11
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 2020-12-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多