如何在 Windows 提示符中调用 cmake 并将 txt 文件内容作为选项传递?

How can I call cmake in Windows Prompt and passing txt file content as options?

我厌倦了在 .cmd/.bat/.sh/.ps1 文件中手动指定每个 cmake 选项,尤其是我正在使用的项目有数百个选项。 将选项放在文本文件中 会更直接。

假设我有这个 CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)

project(demo)

option(USE_A "use a?" OFF)
option(USE_B "use b?" OFF)
option(USE_C "use c?" OFF)

if (USE_A)
    message(STATUS "USE_A: ON")
else()
    message(STATUS "USE_A: OFF")
endif()

if (USE_B)
    message(STATUS "USE_B: ON")
else()
    message(STATUS "USE_B: OFF")
endif()

if (USE_C)
    message(STATUS "USE_C: ON")
else()
    message(STATUS "USE_C: OFF")
endif()

我想用这样的命令(在 Windows 提示终端中)构建它:

md build
cd build
cmake .. ^
    -G "Visual Studio 16 2019" -A x64 ^
    $(type ..\options.txt)  # this line is not working, how can I modify it?

其中 options.txt 是:

-DUSE_A=ON
-DUSE_B=ON
-DUSE_C=ON

这应该等同于此:

md build
cd build
cmake .. ^
    -G "Visual Studio 16 2019" -A x64 ^
    -DUSE_A=ON ^
    -DUSE_B=ON ^
    -DUSE_C=ON

CMake 用户经常面临的一个问题是与其他人共享设置以获取配置项目的常用方法。预设在 3.19 中添加并在 3.20 中得到改进。这样做可能是为了支持 CI 版本,或者是为了经常使用相同版本的用户。 CMake 支持两个文件,CMakePresets.json 和 CMakeUserPresets.json,允许用户指定常用配置选项并与他人共享。

我已经编写了一个示例来突出您想要的用例。

创建一个名为 CMakePresets.json

的文件
{
    "version": 2,
    "cmakeMinimumRequired": {
      "major": 3,
      "minor": 20,
      "patch": 0
    },
    "configurePresets": [
        {
          "name": "vs2019",
          "displayName": "Vs 2019",
          "description": "Build using VS 2019",
          "generator": "Visual Studio 16 2019",
          "architecture": "x64",
          "binaryDir": "${sourceDir}/build/vs",
          "cacheVariables": {
            "USE_A": "ON",
            "USE_B": "ON",
            "USE_C": "ON"
          }
        }
    ],
    "buildPresets": [
      {
        "name": "vs2019",
        "configurePreset": "vs2019"
      }
    ]
}

这是我使用这个的例子:

$> cmake --list-presets
Available configure presets:

  "vs2019" - Vs 2019

$> cmake --preset vs2019
Preset CMake variables:

  USE_A="ON"
  USE_B="ON"
  USE_C="ON"

-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.21337.
-- The CXX compiler identification is MSVC 19.29.29917.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Preview/VC/Tools/MSVC/14.29.29917/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/git/ItoGraphics/build/vs

$> cmake --build --preset vs2019
Microsoft (R) Build Engine version 16.10.0-preview-21118-01+f0eebf287 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

当然,您不必从命令行构建。您可以只打开 Visual Studio,但如果您愿意的话。您可以从命令行构建,如上所示。

真正要注意的一件事是 CMake 是一个非常严格的 json 解析器。没有评论。没有尾随逗号。而且这个新功能的错误消息还不是很好。但是,一旦开始,这确实是一种非常好的生活质量。

CMakePresets.json 适合团队中的每个人。

CMakeUserPresets.json是给自己的。