Merge topic 'cmake-presets-host-system-name'

0d497e159b CMakePresets.json: Add ${hostSystemName} macro
79d03ab505 Help: Fix version numbers in CMakePresets.json documentation
69527a1979 Refactor: Pass CMakePresets.json version to ExpandMacros() functions
ad19da011d Refactor: Add cmSystemTools::GetSystemName()

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !5902
This commit is contained in:
Brad King
2021-03-16 14:51:25 +00:00
committed by Kitware Robot
15 changed files with 219 additions and 110 deletions
+12 -5
View File
@@ -39,7 +39,7 @@ The root object recognizes the following fields:
``version``
A required integer representing the version of the JSON schema.
The supported versions are ``1`` and ``2``.
The supported versions are ``1``, ``2``, and ``3``.
``cmakeMinimumRequired``
@@ -70,17 +70,17 @@ The root object recognizes the following fields:
``configurePresets``
An optional array of `Configure Preset`_ objects.
This is allowed in preset files specifying version 1 or above.
This is allowed in preset files specifying version ``1`` or above.
``buildPresets``
An optional array of `Build Preset`_ objects.
This is allowed in preset files specifying version 2 or above.
This is allowed in preset files specifying version ``2`` or above.
``testPresets``
An optional array of `Test Preset`_ objects.
This is allowed in preset files specifying version 2 or above.
This is allowed in preset files specifying version ``2`` or above.
Configure Preset
^^^^^^^^^^^^^^^^
@@ -187,7 +187,8 @@ that may contain the following fields:
An optional string representing the path to the installation directory.
This field supports `macro expansion`_. If a relative path is specified,
it is calculated relative to the source directory.
it is calculated relative to the source directory. This is allowed in
preset files specifying version ``3`` or above.
``cmakeExecutable``
@@ -829,6 +830,12 @@ Recognized macros include:
test presets, this will evaluate to the generator specified by
``configurePreset``.
``${hostSystemName}``
The name of the host operating system. Contains the same value as
:variable:`CMAKE_HOST_SYSTEM_NAME`. This is allowed in preset files
specifying version ``3`` or above.
``${dollar}``
A literal dollar sign (``$``).
@@ -0,0 +1,5 @@
cmake-presets-host-system-name
------------------------------
* :manual:`cmake-presets(7)` gained support for a new ``${hostSystemName}``
macro.
@@ -0,0 +1,10 @@
cmake-system-name-version
-------------------------
* :variable:`CMAKE_HOST_SYSTEM_NAME`'s undocumented version-stripping behavior
has been moved earlier, before :command:`project` or
:command:`enable_language` is called.
* :variable:`CMAKE_SYSTEM_NAME`'s undocumented version-stripping behavior has
been removed entirely. If it is set by a ``-D`` flag or by a
:manual:`toolchain file <cmake-toolchains(7)>`, it is left unaltered, even if
it still contains a version number.
+8 -31
View File
@@ -158,37 +158,14 @@ endif()
include(Platform/${CMAKE_SYSTEM_NAME}-Determine OPTIONAL)
macro(ADJUST_CMAKE_SYSTEM_VARIABLES _PREFIX)
if(NOT ${_PREFIX}_NAME)
set(${_PREFIX}_NAME "UnknownOS")
endif()
# fix for BSD/OS , remove the /
if(${_PREFIX}_NAME MATCHES BSD.OS)
set(${_PREFIX}_NAME BSDOS)
endif()
# fix for GNU/kFreeBSD, remove the GNU/
if(${_PREFIX}_NAME MATCHES kFreeBSD)
set(${_PREFIX}_NAME kFreeBSD)
endif()
# fix for CYGWIN which has windows version in it
if(${_PREFIX}_NAME MATCHES CYGWIN)
set(${_PREFIX}_NAME CYGWIN)
endif()
# set CMAKE_SYSTEM to the CMAKE_SYSTEM_NAME
set(${_PREFIX} ${${_PREFIX}_NAME})
# if there is a CMAKE_SYSTEM_VERSION then add a -${CMAKE_SYSTEM_VERSION}
if(${_PREFIX}_VERSION)
set(${_PREFIX} ${${_PREFIX}}-${${_PREFIX}_VERSION})
endif()
endmacro()
ADJUST_CMAKE_SYSTEM_VARIABLES(CMAKE_SYSTEM)
ADJUST_CMAKE_SYSTEM_VARIABLES(CMAKE_HOST_SYSTEM)
set(CMAKE_SYSTEM ${CMAKE_SYSTEM_NAME})
if(CMAKE_SYSTEM_VERSION)
string(APPEND CMAKE_SYSTEM -${CMAKE_SYSTEM_VERSION})
endif()
set(CMAKE_HOST_SYSTEM ${CMAKE_HOST_SYSTEM_NAME})
if(CMAKE_HOST_SYSTEM_VERSION)
string(APPEND CMAKE_HOST_SYSTEM -${CMAKE_HOST_SYSTEM_VERSION})
endif()
# this file is also executed from cpack, then we don't need to generate these files
# in this case there is no CMAKE_BINARY_DIR
+69 -39
View File
@@ -9,6 +9,7 @@
#include <iterator>
#include <utility>
#include <cm/string_view>
#include <cmext/string_view>
#include <cm3p/json/reader.h>
@@ -28,9 +29,9 @@
return _result; \
}
#define CHECK_EXPAND(out, field, expanders) \
#define CHECK_EXPAND(out, field, expanders, version) \
{ \
switch (ExpandMacros(field, expanders)) { \
switch (ExpandMacros(field, expanders, version)) { \
case ExpandMacroResult::Error: \
return false; \
case ExpandMacroResult::Ignore: \
@@ -849,16 +850,19 @@ enum class ExpandMacroResult
};
using MacroExpander = std::function<ExpandMacroResult(
const std::string&, const std::string&, std::string&)>;
const std::string&, const std::string&, std::string&, int version)>;
ExpandMacroResult VisitEnv(std::string& value, CycleStatus& status,
const std::vector<MacroExpander>& macroExpanders);
const std::vector<MacroExpander>& macroExpanders,
int version);
ExpandMacroResult ExpandMacros(
std::string& out, const std::vector<MacroExpander>& macroExpanders);
ExpandMacroResult ExpandMacro(
std::string& out, const std::string& macroNamespace,
const std::string& macroName,
const std::vector<MacroExpander>& macroExpanders);
std::string& out, const std::vector<MacroExpander>& macroExpanders,
int version);
ExpandMacroResult ExpandMacro(std::string& out,
const std::string& macroNamespace,
const std::string& macroName,
const std::vector<MacroExpander>& macroExpanders,
int version);
bool ExpandMacros(const cmCMakePresetsFile& file,
const ConfigurePreset& preset,
@@ -866,7 +870,7 @@ bool ExpandMacros(const cmCMakePresetsFile& file,
const std::vector<MacroExpander>& macroExpanders)
{
std::string binaryDir = preset.BinaryDir;
CHECK_EXPAND(out, binaryDir, macroExpanders)
CHECK_EXPAND(out, binaryDir, macroExpanders, file.GetVersion(preset))
if (!cmSystemTools::FileIsFullPath(binaryDir)) {
binaryDir = cmStrCat(file.SourceDir, '/', binaryDir);
@@ -876,7 +880,7 @@ bool ExpandMacros(const cmCMakePresetsFile& file,
if (!preset.InstallDir.empty()) {
std::string installDir = preset.InstallDir;
CHECK_EXPAND(out, installDir, macroExpanders)
CHECK_EXPAND(out, installDir, macroExpanders, file.GetVersion(preset))
if (!cmSystemTools::FileIsFullPath(installDir)) {
installDir = cmStrCat(file.SourceDir, '/', installDir);
@@ -887,67 +891,76 @@ bool ExpandMacros(const cmCMakePresetsFile& file,
for (auto& variable : out->CacheVariables) {
if (variable.second) {
CHECK_EXPAND(out, variable.second->Value, macroExpanders)
CHECK_EXPAND(out, variable.second->Value, macroExpanders,
file.GetVersion(preset))
}
}
return true;
}
bool ExpandMacros(const cmCMakePresetsFile&, const BuildPreset&,
bool ExpandMacros(const cmCMakePresetsFile& file, const BuildPreset& preset,
cm::optional<BuildPreset>& out,
const std::vector<MacroExpander>& macroExpanders)
{
for (auto& target : out->Targets) {
CHECK_EXPAND(out, target, macroExpanders)
CHECK_EXPAND(out, target, macroExpanders, file.GetVersion(preset))
}
for (auto& nativeToolOption : out->NativeToolOptions) {
CHECK_EXPAND(out, nativeToolOption, macroExpanders)
CHECK_EXPAND(out, nativeToolOption, macroExpanders,
file.GetVersion(preset))
}
return true;
}
bool ExpandMacros(const cmCMakePresetsFile&, const TestPreset&,
bool ExpandMacros(const cmCMakePresetsFile& file, const TestPreset& preset,
cm::optional<TestPreset>& out,
const std::vector<MacroExpander>& macroExpanders)
{
for (auto& overwrite : out->OverwriteConfigurationFile) {
CHECK_EXPAND(out, overwrite, macroExpanders);
CHECK_EXPAND(out, overwrite, macroExpanders, file.GetVersion(preset));
}
if (out->Output) {
CHECK_EXPAND(out, out->Output->OutputLogFile, macroExpanders)
CHECK_EXPAND(out, out->Output->OutputLogFile, macroExpanders,
file.GetVersion(preset))
}
if (out->Filter) {
if (out->Filter->Include) {
CHECK_EXPAND(out, out->Filter->Include->Name, macroExpanders)
CHECK_EXPAND(out, out->Filter->Include->Label, macroExpanders)
CHECK_EXPAND(out, out->Filter->Include->Name, macroExpanders,
file.GetVersion(preset))
CHECK_EXPAND(out, out->Filter->Include->Label, macroExpanders,
file.GetVersion(preset))
if (out->Filter->Include->Index) {
CHECK_EXPAND(out, out->Filter->Include->Index->IndexFile,
macroExpanders);
macroExpanders, file.GetVersion(preset));
}
}
if (out->Filter->Exclude) {
CHECK_EXPAND(out, out->Filter->Exclude->Name, macroExpanders)
CHECK_EXPAND(out, out->Filter->Exclude->Label, macroExpanders)
CHECK_EXPAND(out, out->Filter->Exclude->Name, macroExpanders,
file.GetVersion(preset))
CHECK_EXPAND(out, out->Filter->Exclude->Label, macroExpanders,
file.GetVersion(preset))
if (out->Filter->Exclude->Fixtures) {
CHECK_EXPAND(out, out->Filter->Exclude->Fixtures->Any, macroExpanders)
CHECK_EXPAND(out, out->Filter->Exclude->Fixtures->Any, macroExpanders,
file.GetVersion(preset))
CHECK_EXPAND(out, out->Filter->Exclude->Fixtures->Setup,
macroExpanders)
macroExpanders, file.GetVersion(preset))
CHECK_EXPAND(out, out->Filter->Exclude->Fixtures->Cleanup,
macroExpanders)
macroExpanders, file.GetVersion(preset))
}
}
}
if (out->Execution) {
CHECK_EXPAND(out, out->Execution->ResourceSpecFile, macroExpanders)
CHECK_EXPAND(out, out->Execution->ResourceSpecFile, macroExpanders,
file.GetVersion(preset))
}
return true;
@@ -968,8 +981,8 @@ bool ExpandMacros(const cmCMakePresetsFile& file, const T& preset,
MacroExpander defaultMacroExpander =
[&file, &preset](const std::string& macroNamespace,
const std::string& macroName,
std::string& macroOut) -> ExpandMacroResult {
const std::string& macroName, std::string& macroOut,
int version) -> ExpandMacroResult {
if (macroNamespace.empty()) {
if (macroName == "sourceDir") {
macroOut += file.SourceDir;
@@ -998,6 +1011,13 @@ bool ExpandMacros(const cmCMakePresetsFile& file, const T& preset,
macroOut += '$';
return ExpandMacroResult::Ok;
}
if (macroName == "hostSystemName") {
if (version < 3) {
return ExpandMacroResult::Error;
}
macroOut += cmSystemTools::GetSystemName();
return ExpandMacroResult::Ok;
}
}
return ExpandMacroResult::Ignore;
@@ -1006,11 +1026,12 @@ bool ExpandMacros(const cmCMakePresetsFile& file, const T& preset,
MacroExpander environmentMacroExpander =
[&macroExpanders, &out, &envCycles](
const std::string& macroNamespace, const std::string& macroName,
std::string& result) -> ExpandMacroResult {
std::string& result, int version) -> ExpandMacroResult {
if (macroNamespace == "env" && !macroName.empty() && out) {
auto v = out->Environment.find(macroName);
if (v != out->Environment.end() && v->second) {
auto e = VisitEnv(*v->second, envCycles[macroName], macroExpanders);
auto e =
VisitEnv(*v->second, envCycles[macroName], macroExpanders, version);
if (e != ExpandMacroResult::Ok) {
return e;
}
@@ -1038,7 +1059,8 @@ bool ExpandMacros(const cmCMakePresetsFile& file, const T& preset,
for (auto& v : out->Environment) {
if (v.second) {
switch (VisitEnv(*v.second, envCycles[v.first], macroExpanders)) {
switch (VisitEnv(*v.second, envCycles[v.first], macroExpanders,
file.GetVersion(preset))) {
case ExpandMacroResult::Error:
return false;
case ExpandMacroResult::Ignore:
@@ -1054,7 +1076,8 @@ bool ExpandMacros(const cmCMakePresetsFile& file, const T& preset,
}
ExpandMacroResult VisitEnv(std::string& value, CycleStatus& status,
const std::vector<MacroExpander>& macroExpanders)
const std::vector<MacroExpander>& macroExpanders,
int version)
{
if (status == CycleStatus::Verified) {
return ExpandMacroResult::Ok;
@@ -1064,7 +1087,7 @@ ExpandMacroResult VisitEnv(std::string& value, CycleStatus& status,
}
status = CycleStatus::InProgress;
auto e = ExpandMacros(value, macroExpanders);
auto e = ExpandMacros(value, macroExpanders, version);
if (e != ExpandMacroResult::Ok) {
return e;
}
@@ -1073,7 +1096,8 @@ ExpandMacroResult VisitEnv(std::string& value, CycleStatus& status,
}
ExpandMacroResult ExpandMacros(
std::string& out, const std::vector<MacroExpander>& macroExpanders)
std::string& out, const std::vector<MacroExpander>& macroExpanders,
int version)
{
std::string result;
std::string macroNamespace;
@@ -1120,8 +1144,8 @@ ExpandMacroResult ExpandMacros(
case State::MacroName:
if (c == '}') {
auto e =
ExpandMacro(result, macroNamespace, macroName, macroExpanders);
auto e = ExpandMacro(result, macroNamespace, macroName,
macroExpanders, version);
if (e != ExpandMacroResult::Ok) {
return e;
}
@@ -1153,10 +1177,11 @@ ExpandMacroResult ExpandMacros(
ExpandMacroResult ExpandMacro(std::string& out,
const std::string& macroNamespace,
const std::string& macroName,
const std::vector<MacroExpander>& macroExpanders)
const std::vector<MacroExpander>& macroExpanders,
int version)
{
for (auto const& macroExpander : macroExpanders) {
auto result = macroExpander(macroNamespace, macroName, out);
auto result = macroExpander(macroNamespace, macroName, out, version);
if (result != ExpandMacroResult::Ignore) {
return result;
}
@@ -1549,6 +1574,11 @@ cmCMakePresetsFile::ReadFileResult cmCMakePresetsFile::ReadJSONFile(
if (v < MIN_VERSION || v > MAX_VERSION) {
return ReadFileResult::UNRECOGNIZED_VERSION;
}
if (user) {
this->UserVersion = v;
} else {
this->Version = v;
}
// Support for build and test presets added in version 2.
if (v < 2 &&
+7
View File
@@ -294,6 +294,13 @@ public:
std::vector<std::string> TestPresetOrder;
std::string SourceDir;
int Version;
int UserVersion;
int GetVersion(const Preset& preset) const
{
return preset.User ? this->UserVersion : this->Version;
}
static std::string GetFilename(const std::string& sourceDir);
static std::string GetUserFilename(const std::string& sourceDir);
+20 -35
View File
@@ -16,16 +16,9 @@
#include "cmState.h"
#include "cmStateDirectory.h"
#include "cmStatePrivate.h"
#include "cmSystemTools.h"
#include "cmVersion.h"
#if !defined(_WIN32)
# include <sys/utsname.h>
#endif
#if defined(__CYGWIN__)
# include "cmSystemTools.h"
#endif
cmStateSnapshot::cmStateSnapshot(cmState* state)
: State(state)
{
@@ -292,34 +285,26 @@ void InitializeContentFromParent(T& parentContent, T& thisContent,
void cmStateSnapshot::SetDefaultDefinitions()
{
/* Up to CMake 2.4 here only WIN32, UNIX and APPLE were set.
With CMake must separate between target and host platform. In most cases
the tests for WIN32, UNIX and APPLE will be for the target system, so an
additional set of variables for the host system is required ->
CMAKE_HOST_WIN32, CMAKE_HOST_UNIX, CMAKE_HOST_APPLE.
WIN32, UNIX and APPLE are now set in the platform files in
Modules/Platforms/.
To keep cmake scripts (-P) and custom language and compiler modules
working, these variables are still also set here in this place, but they
will be reset in CMakeSystemSpecificInformation.cmake before the platform
files are executed. */
#if defined(_WIN32)
this->SetDefinition("WIN32", "1");
this->SetDefinition("CMAKE_HOST_WIN32", "1");
this->SetDefinition("CMAKE_HOST_SYSTEM_NAME", "Windows");
#else
this->SetDefinition("UNIX", "1");
this->SetDefinition("CMAKE_HOST_UNIX", "1");
# if defined(__ANDROID__)
this->SetDefinition("CMAKE_HOST_SYSTEM_NAME", "Android");
# else
struct utsname uts_name;
if (uname(&uts_name) >= 0) {
this->SetDefinition("CMAKE_HOST_SYSTEM_NAME", uts_name.sysname);
/* Up to CMake 2.4 here only WIN32, UNIX and APPLE were set.
With CMake must separate between target and host platform. In most cases
the tests for WIN32, UNIX and APPLE will be for the target system, so an
additional set of variables for the host system is required ->
CMAKE_HOST_WIN32, CMAKE_HOST_UNIX, CMAKE_HOST_APPLE.
WIN32, UNIX and APPLE are now set in the platform files in
Modules/Platforms/.
To keep cmake scripts (-P) and custom language and compiler modules
working, these variables are still also set here in this place, but they
will be reset in CMakeSystemSpecificInformation.cmake before the platform
files are executed. */
cm::string_view hostSystemName = cmSystemTools::GetSystemName();
this->SetDefinition("CMAKE_HOST_SYSTEM_NAME", hostSystemName);
if (hostSystemName == "Windows") {
this->SetDefinition("WIN32", "1");
this->SetDefinition("CMAKE_HOST_WIN32", "1");
} else {
this->SetDefinition("UNIX", "1");
this->SetDefinition("CMAKE_HOST_UNIX", "1");
}
# endif
#endif
#if defined(__CYGWIN__)
std::string legacy;
if (cmSystemTools::GetEnv("CMAKE_LEGACY_CYGWIN_WIN32", legacy) &&
+47
View File
@@ -103,6 +103,10 @@
# include <malloc.h> /* for malloc/free on QNX */
#endif
#if !defined(_WIN32) && !defined(__ANDROID__)
# include <sys/utsname.h>
#endif
namespace {
cmSystemTools::InterruptCallback s_InterruptCallback;
@@ -3207,3 +3211,46 @@ bool cmSystemTools::CreateLink(const std::string& origName,
return true;
}
cm::string_view cmSystemTools::GetSystemName()
{
#if defined(_WIN32)
return "Windows";
#elif defined(__ANDROID__)
return "Android";
#else
static struct utsname uts_name;
static bool initialized = false;
static cm::string_view systemName;
if (initialized) {
return systemName;
}
if (uname(&uts_name) >= 0) {
initialized = true;
systemName = uts_name.sysname;
if (cmIsOff(systemName)) {
systemName = "UnknownOS";
}
// fix for BSD/OS, remove the /
static const cmsys::RegularExpression bsdOsRegex("BSD.OS");
cmsys::RegularExpressionMatch match;
if (bsdOsRegex.find(uts_name.sysname, match)) {
systemName = "BSDOS";
}
// fix for GNU/kFreeBSD, remove the GNU/
if (systemName.find("kFreeBSD") != cm::string_view::npos) {
systemName = "kFreeBSD";
}
// fix for CYGWIN which has windows version in it
if (systemName.find("CYGWIN") != cm::string_view::npos) {
systemName = "CYGWIN";
}
return systemName;
}
return "";
#endif
}
+3
View File
@@ -498,6 +498,9 @@ public:
const std::string& newName,
std::string* errorMessage = nullptr);
/** Get the system name. */
static cm::string_view GetSystemName();
private:
static bool s_ForceUnixPaths;
static bool s_RunCommandHideConsole;
@@ -0,0 +1,3 @@
include(${CMAKE_CURRENT_LIST_DIR}/TestVariable.cmake)
test_variable(TEST_HOST_SYSTEM_NAME "" "${CMAKE_HOST_SYSTEM_NAME}")
@@ -0,0 +1,13 @@
{
"version": 3,
"configurePresets": [
{
"name": "HostSystemName",
"generator": "@RunCMake_GENERATOR@",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"TEST_HOST_SYSTEM_NAME": "${hostSystemName}"
}
}
]
}
@@ -0,0 +1 @@
1
@@ -0,0 +1,2 @@
^CMake Error: Could not read presets from [^
]*/Tests/RunCMake/CMakePresets/HostSystemNameFuture: Invalid macro expansion$
@@ -0,0 +1,13 @@
{
"version": 2,
"configurePresets": [
{
"name": "HostSystemNameFuture",
"generator": "@RunCMake_GENERATOR@",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"TEST_HOST_SYSTEM_NAME": "${hostSystemName}"
}
}
]
}
@@ -261,6 +261,12 @@ set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/Debug.json.in")
run_cmake_presets(NoDebug)
run_cmake_presets(Debug)
# Test ${hostSystemName} macro
set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/HostSystemName.json.in")
run_cmake_presets(HostSystemName)
set(CMakePresets_FILE "${RunCMake_SOURCE_DIR}/HostSystemNameFuture.json.in")
run_cmake_presets(HostSystemNameFuture)
# Test the example from the documentation
file(READ "${RunCMake_SOURCE_DIR}/../../../Help/manual/presets/example.json" _example)
string(REPLACE "\"generator\": \"Ninja\"" "\"generator\": \"@RunCMake_GENERATOR@\"" _example "${_example}")