Commit 68e20f67 authored by Brad King's avatar Brad King Committed by Kitware Robot

Merge topic 'cmake_role-global-property'

4568d046 Properties: Add CMAKE_ROLE global property
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !2805
parents bd3685b6 4568d046
Pipeline #127906 passed with stage
in 0 seconds
......@@ -23,6 +23,7 @@ Properties of Global Scope
/prop_gbl/AUTORCC_SOURCE_GROUP
/prop_gbl/CMAKE_C_KNOWN_FEATURES
/prop_gbl/CMAKE_CXX_KNOWN_FEATURES
/prop_gbl/CMAKE_ROLE
/prop_gbl/DEBUG_CONFIGURATIONS
/prop_gbl/DISABLED_FEATURES
/prop_gbl/ENABLED_FEATURES
......
CMAKE_ROLE
----------
Tells what mode the current running script is in. Could be one of several
values:
``PROJECT``
Running in project mode (processing a ``CMakeLists.txt`` file).
``SCRIPT``
Running in ``-P`` script mode.
``FIND_PACKAGE``
Running in ``--find-package`` mode.
``CTEST``
Running in CTest script mode.
``CPACK``
Running in CPack.
cmake_role-global-property
--------------------------
* A new global property, :prop_gbl:`CMAKE_ROLE`, was added to allow scripts to
determine whether they're running in project mode, script mode, find-package
mode, CTest, or CPack.
......@@ -18,6 +18,7 @@
#include "cmGeneratedFileStream.h"
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmState.h"
#include "cmStateSnapshot.h"
#include "cmVersion.h"
#include "cmWorkingDirectory.h"
......@@ -690,7 +691,7 @@ int cmCPackGenerator::InstallCMakeProject(
"- Install component: " << component << std::endl);
}
cmake cm(cmake::RoleScript);
cmake cm(cmake::RoleScript, cmState::CPack);
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
cm.GetCurrentSnapshot().SetDefaultDefinitions();
......
......@@ -24,6 +24,7 @@
#include "cmDocumentationFormatter.h"
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmState.h"
#include "cmStateSnapshot.h"
#include "cmSystemTools.h"
#include "cmake.h"
......@@ -208,7 +209,7 @@ int main(int argc, char const* const* argv)
cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
"Read CPack config file: " << cpackConfigFile << std::endl);
cmake cminst(cmake::RoleScript);
cmake cminst(cmake::RoleScript, cmState::CPack);
cminst.SetHomeDirectory("");
cminst.SetHomeOutputDirectory("");
cminst.SetProgressCallback(cpackProgressCallback, nullptr);
......
......@@ -6,6 +6,7 @@
#include "cmCTestTestHandler.h"
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmState.h"
#include "cmSystemTools.h"
#include "cmWorkingDirectory.h"
#include "cmake.h"
......@@ -163,7 +164,7 @@ int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
return 1;
}
cmake cm(cmake::RoleProject);
cmake cm(cmake::RoleProject, cmState::CTest);
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
std::string cmakeOutString;
......
......@@ -15,6 +15,7 @@
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmProcessOutput.h"
#include "cmState.h"
#include "cmStateSnapshot.h"
#include "cmSystemTools.h"
#include "cmXMLWriter.h"
......@@ -610,7 +611,7 @@ int cmCTestLaunch::Main(int argc, const char* const argv[])
void cmCTestLaunch::LoadConfig()
{
cmake cm(cmake::RoleScript);
cmake cm(cmake::RoleScript, cmState::CTest);
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
cm.GetCurrentSnapshot().SetDefaultDefinitions();
......
......@@ -280,7 +280,7 @@ void cmCTestScriptHandler::CreateCMake()
delete this->GlobalGenerator;
delete this->Makefile;
}
this->CMake = new cmake(cmake::RoleScript);
this->CMake = new cmake(cmake::RoleScript, cmState::CTest);
this->CMake->SetHomeDirectory("");
this->CMake->SetHomeOutputDirectory("");
this->CMake->GetCurrentSnapshot().SetDefaultDefinitions();
......
......@@ -1676,7 +1676,7 @@ void cmCTestTestHandler::GetListOfTests()
}
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
"Constructing a list of tests" << std::endl, this->Quiet);
cmake cm(cmake::RoleScript);
cmake cm(cmake::RoleScript, cmState::CTest);
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
cm.GetCurrentSnapshot().SetDefaultDefinitions();
......
......@@ -6,6 +6,7 @@
#include "cmCursesStandardIncludes.h"
#include "cmDocumentation.h"
#include "cmDocumentationEntry.h"
#include "cmState.h"
#include "cmSystemTools.h"
#include "cmake.h"
......@@ -83,7 +84,7 @@ int main(int argc, char const* const* argv)
cmDocumentation doc;
doc.addCMakeStandardDocSections();
if (doc.CheckOptions(argc, argv)) {
cmake hcm(cmake::RoleInternal);
cmake hcm(cmake::RoleInternal, cmState::Unknown);
hcm.SetHomeDirectory("");
hcm.SetHomeOutputDirectory("");
hcm.AddCMakePaths();
......
......@@ -41,7 +41,7 @@ cmCursesMainForm::cmCursesMainForm(std::vector<std::string> const& args,
"Welcome to ccmake, curses based user interface for CMake.");
this->HelpMessage.push_back("");
this->HelpMessage.push_back(s_ConstHelpMessage);
this->CMakeInstance = new cmake(cmake::RoleProject);
this->CMakeInstance = new cmake(cmake::RoleProject, cmState::Project);
this->CMakeInstance->SetCMakeEditCommand(
cmSystemTools::GetCMakeCursesCommand());
......
......@@ -64,7 +64,7 @@ int main(int argc, char** argv)
doc.addCMakeStandardDocSections();
if (argc2 > 1 && doc.CheckOptions(argc2, argv2)) {
// Construct and print requested documentation.
cmake hcm(cmake::RoleInternal);
cmake hcm(cmake::RoleInternal, cmState::Unknown);
hcm.SetHomeDirectory("");
hcm.SetHomeOutputDirectory("");
hcm.AddCMakePaths();
......
......@@ -27,7 +27,7 @@ QCMake::QCMake(QObject* p)
cmSystemTools::SetStdoutCallback(QCMake::stdoutCallback, this);
cmSystemTools::SetStderrCallback(QCMake::stderrCallback, this);
this->CMakeInstance = new cmake(cmake::RoleProject);
this->CMakeInstance = new cmake(cmake::RoleProject, cmState::Project);
this->CMakeInstance->SetCMakeEditCommand(
cmSystemTools::GetCMakeGUICommand());
this->CMakeInstance->SetProgressCallback(QCMake::progressCallback, this);
......
......@@ -429,7 +429,7 @@ int cmCTest::Initialize(const char* binary_dir, cmCTestStartCommand* command)
}
}
cmake cm(cmake::RoleScript);
cmake cm(cmake::RoleScript, cmState::CTest);
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
cm.GetCurrentSnapshot().SetDefaultDefinitions();
......
......@@ -1939,7 +1939,7 @@ int cmcmd_cmake_ninja_dyndep(std::vector<std::string>::const_iterator argBeg,
}
}
cmake cm(cmake::RoleInternal);
cmake cm(cmake::RoleInternal, cmState::Unknown);
cm.SetHomeDirectory(dir_top_src);
cm.SetHomeOutputDirectory(dir_top_bld);
std::unique_ptr<cmGlobalNinjaGenerator> ggd(
......
......@@ -13,6 +13,7 @@
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
#include "cmState.h"
#include "cmStateSnapshot.h"
#include "cmSystemTools.h"
#include "cmTarget.h"
......@@ -172,7 +173,7 @@ cmGraphVizWriter::cmGraphVizWriter(const cmGlobalGenerator* globalGenerator)
void cmGraphVizWriter::ReadSettings(const char* settingsFileName,
const char* fallbackSettingsFileName)
{
cmake cm(cmake::RoleScript);
cmake cm(cmake::RoleScript, cmState::Unknown);
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
cm.GetCurrentSnapshot().SetDefaultDefinitions();
......
......@@ -3374,7 +3374,7 @@ int cmMakefile::TryCompile(const std::string& srcdir,
// make sure the same generator is used
// use this program as the cmake to be run, it should not
// be run that way but the cmake object requires a vailid path
cmake cm(cmake::RoleProject);
cmake cm(cmake::RoleProject, cmState::Project);
cm.SetIsInTryCompile(true);
cmGlobalGenerator* gg =
cm.CreateGlobalGenerator(this->GetGlobalGenerator()->GetName());
......
......@@ -8,6 +8,7 @@
#include "cmAlgorithms.h"
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmState.h"
#include "cmStateDirectory.h"
#include "cmStateSnapshot.h"
#include "cmSystemTools.h"
......@@ -686,7 +687,7 @@ bool cmQtAutoGenerator::Run(std::string const& infoFile,
bool success = false;
{
cmake cm(cmake::RoleScript);
cmake cm(cmake::RoleScript, cmState::Unknown);
cm.SetHomeOutputDirectory(InfoDir());
cm.SetHomeDirectory(InfoDir());
cm.GetCurrentSnapshot().SetDefaultDefinitions();
......
......@@ -130,7 +130,8 @@ bool cmServerProtocol::Activate(cmServer* server,
{
assert(server);
this->m_Server = server;
this->m_CMakeInstance = cm::make_unique<cmake>(cmake::RoleProject);
this->m_CMakeInstance =
cm::make_unique<cmake>(cmake::RoleProject, cmState::Project);
const bool result = this->DoActivate(request, errorMessage);
if (!result) {
this->m_CMakeInstance = nullptr;
......
......@@ -539,6 +539,9 @@ const char* cmState::GetGlobalProperty(const std::string& prop)
std::string langs;
langs = cmJoin(this->EnabledLanguages, ";");
this->SetGlobalProperty("ENABLED_LANGUAGES", langs.c_str());
} else if (prop == "CMAKE_ROLE") {
std::string mode = this->GetModeString();
this->SetGlobalProperty("CMAKE_ROLE", mode.c_str());
}
#define STRING_LIST_ELEMENT(F) ";" #F
if (prop == "CMAKE_C_KNOWN_FEATURES") {
......@@ -643,6 +646,40 @@ unsigned int cmState::GetCacheMinorVersion() const
return this->CacheManager->GetCacheMinorVersion();
}
cmState::Mode cmState::GetMode() const
{
return this->CurrentMode;
}
std::string cmState::GetModeString() const
{
return ModeToString(this->CurrentMode);
}
void cmState::SetMode(cmState::Mode mode)
{
this->CurrentMode = mode;
}
std::string cmState::ModeToString(cmState::Mode mode)
{
switch (mode) {
case Project:
return "PROJECT";
case Script:
return "SCRIPT";
case FindPackage:
return "FIND_PACKAGE";
case CTest:
return "CTEST";
case CPack:
return "CPACK";
case Unknown:
return "UNKNOWN";
}
return "UNKNOWN";
}
std::string const& cmState::GetBinaryDirectory() const
{
return this->BinaryDirectory;
......
......@@ -35,6 +35,16 @@ public:
cmState();
~cmState();
enum Mode
{
Unknown,
Project,
Script,
FindPackage,
CTest,
CPack,
};
static const char* GetTargetTypeName(cmStateEnums::TargetType targetType);
cmStateSnapshot CreateBaseSnapshot();
......@@ -166,6 +176,12 @@ public:
unsigned int GetCacheMajorVersion() const;
unsigned int GetCacheMinorVersion() const;
Mode GetMode() const;
std::string GetModeString() const;
void SetMode(Mode mode);
static std::string ModeToString(Mode mode);
private:
friend class cmake;
void AddCacheEntry(const std::string& key, const char* value,
......@@ -210,6 +226,7 @@ private:
bool MinGWMake = false;
bool NMake = false;
bool MSYSShell = false;
Mode CurrentMode = Unknown;
};
#endif
......@@ -126,7 +126,7 @@ void cmWarnUnusedCliWarning(const std::string& variable, int /*unused*/,
cm->MarkCliAsUsed(variable);
}
cmake::cmake(Role role)
cmake::cmake(Role role, cmState::Mode mode)
{
this->Trace = false;
this->TraceExpand = false;
......@@ -140,6 +140,7 @@ cmake::cmake(Role role)
this->FileComparison = new cmFileTimeComparison;
this->State = new cmState;
this->State->SetMode(mode);
this->CurrentSnapshot = this->State->CreateBaseSnapshot();
this->Messenger = new cmMessenger;
......@@ -2023,7 +2024,8 @@ int cmake::CheckBuildSystem()
// Read the rerun check file and use it to decide whether to do the
// global generate.
cmake cm(RoleScript); // Actually, all we need is the `set` command.
// Actually, all we need is the `set` command.
cmake cm(RoleScript, cmState::Unknown);
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
cm.GetCurrentSnapshot().SetDefaultDefinitions();
......
......@@ -15,6 +15,7 @@
#include "cmInstalledFile.h"
#include "cmListFileCache.h"
#include "cmMessageType.h"
#include "cmState.h"
#include "cmStateSnapshot.h"
#include "cmStateTypes.h"
......@@ -29,7 +30,6 @@ class cmGlobalGenerator;
class cmGlobalGeneratorFactory;
class cmMakefile;
class cmMessenger;
class cmState;
class cmVariableWatch;
struct cmDocumentationEntry;
......@@ -113,7 +113,7 @@ public:
static const int DEFAULT_BUILD_PARALLEL_LEVEL = 0;
/// Default constructor
cmake(Role role);
cmake(Role role, cmState::Mode mode);
/// Destructor
~cmake();
......
......@@ -217,7 +217,7 @@ int do_cmake(int ac, char const* const* av)
doc.addCMakeStandardDocSections();
if (doc.CheckOptions(ac, av)) {
// Construct and print requested documentation.
cmake hcm(cmake::RoleInternal);
cmake hcm(cmake::RoleInternal, cmState::Unknown);
hcm.SetHomeDirectory("");
hcm.SetHomeOutputDirectory("");
hcm.AddCMakePaths();
......@@ -299,7 +299,7 @@ int do_cmake(int ac, char const* const* av)
}
}
if (sysinfo) {
cmake cm(cmake::RoleProject);
cmake cm(cmake::RoleProject, cmState::Project);
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
int ret = cm.GetSystemInformation(args);
......@@ -307,7 +307,19 @@ int do_cmake(int ac, char const* const* av)
}
cmake::Role const role =
workingMode == cmake::SCRIPT_MODE ? cmake::RoleScript : cmake::RoleProject;
cmake cm(role);
cmState::Mode mode = cmState::Unknown;
switch (workingMode) {
case cmake::NORMAL_MODE:
mode = cmState::Project;
break;
case cmake::SCRIPT_MODE:
mode = cmState::Script;
break;
case cmake::FIND_PACKAGE_MODE:
mode = cmState::FindPackage;
break;
}
cmake cm(role, mode);
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
cmSystemTools::SetMessageCallback(cmakemainMessageCallback, &cm);
......@@ -463,7 +475,7 @@ static int do_build(int ac, char const* const* av)
return 1;
}
cmake cm(cmake::RoleInternal);
cmake cm(cmake::RoleInternal, cmState::Unknown);
cmSystemTools::SetMessageCallback(cmakemainMessageCallback, &cm);
cm.SetProgressCallback(cmakemainProgressCallback, &cm);
return cm.Build(jobs, dir, target, config, nativeOptions, clean);
......@@ -501,7 +513,7 @@ static int do_open(int ac, char const* const* av)
return 1;
}
cmake cm(cmake::RoleInternal);
cmake cm(cmake::RoleInternal, cmState::Unknown);
cmSystemTools::SetMessageCallback(cmakemainMessageCallback, &cm);
cm.SetProgressCallback(cmakemainProgressCallback, &cm);
return cm.Open(dir, false) ? 0 : 1;
......
......@@ -9,6 +9,7 @@
#include "cmMakefile.h"
#include "cmQtAutoGeneratorMocUic.h"
#include "cmQtAutoGeneratorRcc.h"
#include "cmState.h"
#include "cmStateDirectory.h"
#include "cmStateSnapshot.h"
#include "cmSystemTools.h"
......@@ -721,7 +722,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
std::cerr << "-E capabilities accepts no additional arguments\n";
return 1;
}
cmake cm(cmake::RoleInternal);
cmake cm(cmake::RoleInternal, cmState::Unknown);
#if defined(CMAKE_BUILD_WITH_CMAKE)
std::cout << cm.ReportCapabilities(true);
#else
......@@ -898,7 +899,8 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string>& args)
const bool verbose = isCMakeVerbose();
// Create a cmake object instance to process dependencies.
cmake cm(cmake::RoleScript); // All we need is the `set` command.
// All we need is the `set` command.
cmake cm(cmake::RoleScript, cmState::Unknown);
std::string gen;
std::string homeDir;
std::string startDir;
......
......@@ -155,6 +155,7 @@ add_RunCMake_test(BuildDepends)
if(UNIX AND "${CMAKE_GENERATOR}" MATCHES "Unix Makefiles|Ninja")
add_RunCMake_test(Byproducts)
endif()
add_RunCMake_test(CMakeRoleGlobalProperty)
if(UNIX AND "${CMAKE_GENERATOR}" MATCHES "Unix Makefiles|Ninja")
add_RunCMake_test(CompilerChange)
endif()
......
cmake_minimum_required(VERSION 3.12)
project(${RunCMake_TEST} NONE)
include(${RunCMake_TEST}.cmake)
cmake_minimum_required(VERSION 3.1)
project(CTestStart@CASE_NAME@ NONE)
include(CTest)
add_test(NAME RunCMakeVersion COMMAND "${CMAKE_COMMAND}" --version)
cmake_minimum_required(VERSION 3.12)
get_property(role GLOBAL PROPERTY CMAKE_ROLE)
if(NOT role STREQUAL "FIND_PACKAGE")
message(SEND_ERROR "CMAKE_ROLE property is \"${role}\", should be \"FIND_PACKAGE\"")
endif()
set(DummyPackage_FOUND 1)
get_property(role GLOBAL PROPERTY CMAKE_ROLE)
if(NOT role STREQUAL "PROJECT")
message(SEND_ERROR "CMAKE_ROLE property is \"${role}\", should be \"PROJECT\"")
endif()
add_subdirectory(sub)
include(RunCMake)
include(RunCTest)
run_cmake(Project)
run_cmake_command(Script "${CMAKE_COMMAND}" -P "${CMAKE_CURRENT_LIST_DIR}/Script.cmake")
run_cmake_command(FindPackage "${CMAKE_COMMAND}" --find-package -DNAME=DummyPackage -DCOMPILER_ID=GNU -DLANGUAGE=CXX -DMODE=EXIST "-DCMAKE_MODULE_PATH=${CMAKE_CURRENT_LIST_DIR}")
run_ctest(CTest)
cmake_minimum_required(VERSION 3.12)
get_property(role GLOBAL PROPERTY CMAKE_ROLE)
if(NOT role STREQUAL "SCRIPT")
message(SEND_ERROR "CMAKE_ROLE property is \"${role}\", should be \"SCRIPT\"")
endif()
get_property(role GLOBAL PROPERTY CMAKE_ROLE)
if(NOT role STREQUAL "PROJECT")
message(SEND_ERROR "CMAKE_ROLE property is \"${role}\", should be \"PROJECT\"")
endif()
cmake_minimum_required(VERSION 3.12)
set(CTEST_RUN_CURRENT_SCRIPT 0)
get_property(role GLOBAL PROPERTY CMAKE_ROLE)
if(NOT role STREQUAL "CTEST")
message(SEND_ERROR "CMAKE_ROLE property is \"${role}\", should be \"CTEST\"")
endif()
message("This script could run an external packaging tool")
get_property(role GLOBAL PROPERTY CMAKE_ROLE)
if(NOT role STREQUAL "CPACK")
message(SEND_ERROR "CMAKE_ROLE property is \"${role}\", should be \"CPACK\"")
endif()
function(expect_variable VAR)
if(NOT ${VAR})
message(FATAL_ERROR "${VAR} is unexpectedly not set")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment