1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 02:08:27 +08:00

Refactor: Add cmSystemTools::GetSystemName()

And use it for CMAKE_HOST_SYSTEM_NAME and CMAKE_SYSTEM_NAME.
This commit is contained in:
Kyle Edwards
2021-03-11 12:02:13 -05:00
parent 336c57edd0
commit ad19da011d
5 changed files with 88 additions and 66 deletions

View File

@@ -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.

View File

@@ -158,37 +158,14 @@ endif()
include(Platform/${CMAKE_SYSTEM_NAME}-Determine OPTIONAL) include(Platform/${CMAKE_SYSTEM_NAME}-Determine OPTIONAL)
macro(ADJUST_CMAKE_SYSTEM_VARIABLES _PREFIX) set(CMAKE_SYSTEM ${CMAKE_SYSTEM_NAME})
if(NOT ${_PREFIX}_NAME) if(CMAKE_SYSTEM_VERSION)
set(${_PREFIX}_NAME "UnknownOS") string(APPEND CMAKE_SYSTEM -${CMAKE_SYSTEM_VERSION})
endif() endif()
set(CMAKE_HOST_SYSTEM ${CMAKE_HOST_SYSTEM_NAME})
# fix for BSD/OS , remove the / if(CMAKE_HOST_SYSTEM_VERSION)
if(${_PREFIX}_NAME MATCHES BSD.OS) string(APPEND CMAKE_HOST_SYSTEM -${CMAKE_HOST_SYSTEM_VERSION})
set(${_PREFIX}_NAME BSDOS) endif()
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)
# this file is also executed from cpack, then we don't need to generate these files # 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 # in this case there is no CMAKE_BINARY_DIR

View File

@@ -16,16 +16,9 @@
#include "cmState.h" #include "cmState.h"
#include "cmStateDirectory.h" #include "cmStateDirectory.h"
#include "cmStatePrivate.h" #include "cmStatePrivate.h"
#include "cmSystemTools.h"
#include "cmVersion.h" #include "cmVersion.h"
#if !defined(_WIN32)
# include <sys/utsname.h>
#endif
#if defined(__CYGWIN__)
# include "cmSystemTools.h"
#endif
cmStateSnapshot::cmStateSnapshot(cmState* state) cmStateSnapshot::cmStateSnapshot(cmState* state)
: State(state) : State(state)
{ {
@@ -292,34 +285,26 @@ void InitializeContentFromParent(T& parentContent, T& thisContent,
void cmStateSnapshot::SetDefaultDefinitions() void cmStateSnapshot::SetDefaultDefinitions()
{ {
/* Up to CMake 2.4 here only WIN32, UNIX and APPLE were set. /* 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 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 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 -> additional set of variables for the host system is required ->
CMAKE_HOST_WIN32, CMAKE_HOST_UNIX, CMAKE_HOST_APPLE. CMAKE_HOST_WIN32, CMAKE_HOST_UNIX, CMAKE_HOST_APPLE.
WIN32, UNIX and APPLE are now set in the platform files in WIN32, UNIX and APPLE are now set in the platform files in
Modules/Platforms/. Modules/Platforms/.
To keep cmake scripts (-P) and custom language and compiler modules To keep cmake scripts (-P) and custom language and compiler modules
working, these variables are still also set here in this place, but they working, these variables are still also set here in this place, but they
will be reset in CMakeSystemSpecificInformation.cmake before the platform will be reset in CMakeSystemSpecificInformation.cmake before the platform
files are executed. */ files are executed. */
#if defined(_WIN32) cm::string_view hostSystemName = cmSystemTools::GetSystemName();
this->SetDefinition("WIN32", "1"); this->SetDefinition("CMAKE_HOST_SYSTEM_NAME", hostSystemName);
this->SetDefinition("CMAKE_HOST_WIN32", "1"); if (hostSystemName == "Windows") {
this->SetDefinition("CMAKE_HOST_SYSTEM_NAME", "Windows"); this->SetDefinition("WIN32", "1");
#else this->SetDefinition("CMAKE_HOST_WIN32", "1");
this->SetDefinition("UNIX", "1"); } else {
this->SetDefinition("CMAKE_HOST_UNIX", "1"); 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);
} }
# endif
#endif
#if defined(__CYGWIN__) #if defined(__CYGWIN__)
std::string legacy; std::string legacy;
if (cmSystemTools::GetEnv("CMAKE_LEGACY_CYGWIN_WIN32", legacy) && if (cmSystemTools::GetEnv("CMAKE_LEGACY_CYGWIN_WIN32", legacy) &&

View File

@@ -103,6 +103,10 @@
# include <malloc.h> /* for malloc/free on QNX */ # include <malloc.h> /* for malloc/free on QNX */
#endif #endif
#if !defined(_WIN32) && !defined(__ANDROID__)
# include <sys/utsname.h>
#endif
namespace { namespace {
cmSystemTools::InterruptCallback s_InterruptCallback; cmSystemTools::InterruptCallback s_InterruptCallback;
@@ -3207,3 +3211,46 @@ bool cmSystemTools::CreateLink(const std::string& origName,
return true; 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
}

View File

@@ -498,6 +498,9 @@ public:
const std::string& newName, const std::string& newName,
std::string* errorMessage = nullptr); std::string* errorMessage = nullptr);
/** Get the system name. */
static cm::string_view GetSystemName();
private: private:
static bool s_ForceUnixPaths; static bool s_ForceUnixPaths;
static bool s_RunCommandHideConsole; static bool s_RunCommandHideConsole;