I'm tired of manually specifying each cmake options in .cmd/.bat/.sh/.ps1 files, especially the project that I'm using is with hundred of options. Putting options in a text file would be more straight forward.
Let's say I have this 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()
I would like to build it with commands (in Windows prompt terminal) like this:
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?
where options.txt
is:
-DUSE_A=ON
-DUSE_B=ON
-DUSE_C=ON
which should be equivelant to this:
md build
cd build
cmake .. ^
-G "Visual Studio 16 2019" -A x64 ^
-DUSE_A=ON ^
-DUSE_B=ON ^
-DUSE_C=ON
One problem that CMake users often face is sharing settings with other people for common ways to configure a project. Presets were added in 3.19 and improved in 3.20. This may be done to support CI builds, or for users who frequently use the same build. CMake supports two files, CMakePresets.json and CMakeUserPresets.json, that allow users to specify common configure options and share them with others.
I've written an example to highlight your desired use case.
Create a file named 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"
}
]
}
Here is an example of me using this:
$> 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.
Of course you don't have to build from the command line. You can just open Visual Studio but if you wanted to. You could build from the command line as shown above.
One thing to really note is that CMake is a very strict json parser. No comments. No trailing commas. And the error messages aren't great yet on this new feature. However, it's a really great quality of life once you get it going.
CMakePresets.json is for everyone on your team.
CMakeUserPresets.json is for yourself.