cmake: Infer command set from role

This commit is contained in:
Brad King
2025-10-20 17:10:12 -04:00
parent bfaaec6179
commit e290d4f2a3
19 changed files with 34 additions and 48 deletions
+1 -1
View File
@@ -760,7 +760,7 @@ int cmCPackGenerator::InstallCMakeProject(
"- Install component: " << component << std::endl); "- Install component: " << component << std::endl);
} }
cmake cm(cmake::CommandSet::Script, cmState::Role::CPack); cmake cm(cmState::Role::CPack);
cm.GetCurrentSnapshot().SetDefaultDefinitions(); cm.GetCurrentSnapshot().SetDefaultDefinitions();
cm.AddCMakePaths(); cm.AddCMakePaths();
cm.SetProgressCallback([this](std::string const& msg, float prog) { cm.SetProgressCallback([this](std::string const& msg, float prog) {
+1 -1
View File
@@ -225,7 +225,7 @@ int main(int argc, char const* const* argv)
} }, } },
}; };
cmake cminst(cmake::CommandSet::Script, cmState::Role::CPack); cmake cminst(cmState::Role::CPack);
cminst.SetProgressCallback(cpackProgressCallback); cminst.SetProgressCallback(cpackProgressCallback);
cminst.GetCurrentSnapshot().SetDefaultDefinitions(); cminst.GetCurrentSnapshot().SetDefaultDefinitions();
cmGlobalGenerator cmgg(&cminst); cmGlobalGenerator cmgg(&cminst);
+1 -1
View File
@@ -177,7 +177,7 @@ int cmCTestBuildAndTest::Run()
return 1; return 1;
} }
cmake cm(cmake::CommandSet::Project, cmState::Role::Project); cmake cm(cmState::Role::Project);
cmCTestBuildAndTestCaptureRAII captureRAII(cm); cmCTestBuildAndTestCaptureRAII captureRAII(cm);
static_cast<void>(captureRAII); static_cast<void>(captureRAII);
+1 -1
View File
@@ -405,7 +405,7 @@ int cmCTestLaunch::Main(int argc, char const* const argv[], Op operation)
void cmCTestLaunch::LoadConfig() void cmCTestLaunch::LoadConfig()
{ {
cmake cm(cmake::CommandSet::Script, cmState::Role::CTest); cmake cm(cmState::Role::CTest);
cm.GetCurrentSnapshot().SetDefaultDefinitions(); cm.GetCurrentSnapshot().SetDefaultDefinitions();
cmGlobalGenerator gg(&cm); cmGlobalGenerator gg(&cm);
cmMakefile mf(&gg, cm.GetCurrentSnapshot()); cmMakefile mf(&gg, cm.GetCurrentSnapshot());
+1 -2
View File
@@ -172,8 +172,7 @@ int cmCTestScriptHandler::ExecuteScript(std::string const& total_script_arg)
void cmCTestScriptHandler::CreateCMake() void cmCTestScriptHandler::CreateCMake()
{ {
// create a cmake instance to read the configuration script // create a cmake instance to read the configuration script
this->CMake = this->CMake = cm::make_unique<cmake>(cmState::Role::CTest);
cm::make_unique<cmake>(cmake::CommandSet::Script, cmState::Role::CTest);
this->CMake->GetCurrentSnapshot().SetDefaultDefinitions(); this->CMake->GetCurrentSnapshot().SetDefaultDefinitions();
this->CMake->AddCMakePaths(); this->CMake->AddCMakePaths();
this->CMake->SetWorkingMode(cmake::SCRIPT_MODE, this->CMake->SetWorkingMode(cmake::SCRIPT_MODE,
+1 -1
View File
@@ -1754,7 +1754,7 @@ bool cmCTestTestHandler::GetListOfTests()
} }
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
"Constructing a list of tests" << std::endl, this->Quiet); "Constructing a list of tests" << std::endl, this->Quiet);
cmake cm(cmake::CommandSet::Script, cmState::Role::CTest); cmake cm(cmState::Role::CTest);
cm.GetCurrentSnapshot().SetDefaultDefinitions(); cm.GetCurrentSnapshot().SetDefaultDefinitions();
cmGlobalGenerator gg(&cm); cmGlobalGenerator gg(&cm);
cmMakefile mf(&gg, cm.GetCurrentSnapshot()); cmMakefile mf(&gg, cm.GetCurrentSnapshot());
+1 -1
View File
@@ -75,7 +75,7 @@ int main(int argc, char const* const* argv)
cmDocumentation doc; cmDocumentation doc;
doc.addCMakeStandardDocSections(); doc.addCMakeStandardDocSections();
if (doc.CheckOptions(argc, argv)) { if (doc.CheckOptions(argc, argv)) {
cmake hcm(cmake::CommandSet::None, cmState::Role::Help); cmake hcm(cmState::Role::Help);
hcm.AddCMakePaths(); hcm.AddCMakePaths();
auto generators = hcm.GetGeneratorsDocumentation(); auto generators = hcm.GetGeneratorsDocumentation();
doc.SetName("ccmake"); doc.SetName("ccmake");
+1 -2
View File
@@ -39,8 +39,7 @@ cmCursesMainForm::cmCursesMainForm(std::vector<std::string> args,
"Welcome to ccmake, curses based user interface for CMake."); "Welcome to ccmake, curses based user interface for CMake.");
this->HelpMessage.emplace_back(); this->HelpMessage.emplace_back();
this->HelpMessage.emplace_back(s_ConstHelpMessage); this->HelpMessage.emplace_back(s_ConstHelpMessage);
this->CMakeInstance = this->CMakeInstance = cm::make_unique<cmake>(cmState::Role::Project);
cm::make_unique<cmake>(cmake::CommandSet::Project, cmState::Role::Project);
this->CMakeInstance->SetCMakeEditCommand( this->CMakeInstance->SetCMakeEditCommand(
cmSystemTools::GetCMakeCursesCommand()); cmSystemTools::GetCMakeCursesCommand());
+1 -1
View File
@@ -82,7 +82,7 @@ int main(int argc, char** argv)
doc.addCMakeStandardDocSections(); doc.addCMakeStandardDocSections();
if (argc2 > 1 && doc.CheckOptions(argc2, argv2)) { if (argc2 > 1 && doc.CheckOptions(argc2, argv2)) {
// Construct and print requested documentation. // Construct and print requested documentation.
cmake hcm(cmake::CommandSet::None, cmState::Role::Help); cmake hcm(cmState::Role::Help);
hcm.AddCMakePaths(); hcm.AddCMakePaths();
auto generators = hcm.GetGeneratorsDocumentation(); auto generators = hcm.GetGeneratorsDocumentation();
+1 -2
View File
@@ -46,8 +46,7 @@ QCMake::QCMake(QObject* p)
cmSystemTools::SetStderrCallback( cmSystemTools::SetStderrCallback(
[this](std::string const& msg) { this->stderrCallback(msg); }); [this](std::string const& msg) { this->stderrCallback(msg); });
this->CMakeInstance = this->CMakeInstance = cm::make_unique<cmake>(cmState::Role::Project);
cm::make_unique<cmake>(cmake::CommandSet::Project, cmState::Role::Project);
this->CMakeInstance->SetCMakeEditCommand( this->CMakeInstance->SetCMakeEditCommand(
cmSystemTools::GetCMakeGUICommand()); cmSystemTools::GetCMakeGUICommand());
this->CMakeInstance->SetProgressCallback( this->CMakeInstance->SetProgressCallback(
+1 -1
View File
@@ -2656,7 +2656,7 @@ int cmCTest::ExecuteTests(std::vector<std::string> const& args)
cmCTestTestHandler handler(this); cmCTestTestHandler handler(this);
{ {
cmake cm(cmake::CommandSet::Script, cmState::Role::CTest); cmake cm(cmState::Role::CTest);
cm.GetCurrentSnapshot().SetDefaultDefinitions(); cm.GetCurrentSnapshot().SetDefaultDefinitions();
cmGlobalGenerator gg(&cm); cmGlobalGenerator gg(&cm);
cmMakefile mf(&gg, cm.GetCurrentSnapshot()); cmMakefile mf(&gg, cm.GetCurrentSnapshot());
+1 -1
View File
@@ -3049,7 +3049,7 @@ int cmcmd_cmake_ninja_dyndep(std::vector<std::string>::const_iterator argBeg,
auto export_info = cmDyndepCollation::ParseExportInfo(tdi); auto export_info = cmDyndepCollation::ParseExportInfo(tdi);
cmake cm(cmake::CommandSet::None, cmState::Role::Internal); cmake cm(cmState::Role::Internal);
cm.SetHomeDirectory(dir_top_src); cm.SetHomeDirectory(dir_top_src);
cm.SetHomeOutputDirectory(dir_top_bld); cm.SetHomeOutputDirectory(dir_top_bld);
auto ggd = cm.CreateGlobalGenerator("Ninja"); auto ggd = cm.CreateGlobalGenerator("Ninja");
+1 -1
View File
@@ -195,7 +195,7 @@ void cmGraphVizWriter::ReadSettings(
std::string const& settingsFileName, std::string const& settingsFileName,
std::string const& fallbackSettingsFileName) std::string const& fallbackSettingsFileName)
{ {
cmake cm(cmake::CommandSet::Script, cmState::Role::Script); cmake cm(cmState::Role::Script);
cm.GetCurrentSnapshot().SetDefaultDefinitions(); cm.GetCurrentSnapshot().SetDefaultDefinitions();
cmGlobalGenerator ggi(&cm); cmGlobalGenerator ggi(&cm);
cmMakefile mf(&ggi, cm.GetCurrentSnapshot()); cmMakefile mf(&ggi, cm.GetCurrentSnapshot());
+1 -2
View File
@@ -3251,8 +3251,7 @@ int cmMakefile::TryCompile(std::string const& srcdir,
// make sure the same generator is used // make sure the same generator is used
// use this program as the cmake to be run, it should not // use this program as the cmake to be run, it should not
// be run that way but the cmake object requires a valid path // be run that way but the cmake object requires a valid path
cmake cm(cmake::CommandSet::Project, cmState::Role::Project, cmake cm(cmState::Role::Project, cmState::TryCompile::Yes);
cmState::TryCompile::Yes);
auto gg = cm.CreateGlobalGenerator(this->GetGlobalGenerator()->GetName()); auto gg = cm.CreateGlobalGenerator(this->GetGlobalGenerator()->GetName());
if (!gg) { if (!gg) {
this->IssueMessage(MessageType::INTERNAL_ERROR, this->IssueMessage(MessageType::INTERNAL_ERROR,
+6 -5
View File
@@ -297,8 +297,7 @@ cmDocumentationEntry cmake::CMAKE_STANDARD_OPTIONS_TABLE[19] = {
"not errors." } "not errors." }
}; };
cmake::cmake(CommandSet commandSet, cmState::Role role, cmake::cmake(cmState::Role role, cmState::TryCompile isTryCompile)
cmState::TryCompile isTryCompile)
: CMakeWorkingDirectory(cmSystemTools::GetLogicalWorkingDirectory()) : CMakeWorkingDirectory(cmSystemTools::GetLogicalWorkingDirectory())
, FileTimeCache(cm::make_unique<cmFileTimeCache>()) , FileTimeCache(cm::make_unique<cmFileTimeCache>())
#ifndef CMAKE_BOOTSTRAP #ifndef CMAKE_BOOTSTRAP
@@ -322,10 +321,12 @@ cmake::cmake(CommandSet commandSet, cmState::Role role,
this->AddDefaultGenerators(); this->AddDefaultGenerators();
this->AddDefaultExtraGenerators(); this->AddDefaultExtraGenerators();
if (commandSet == CommandSet::Script || commandSet == CommandSet::Project) { if (role == cmState::Role::Project || role == cmState::Role::FindPackage ||
role == cmState::Role::Script || role == cmState::Role::CTest ||
role == cmState::Role::CPack) {
this->AddScriptingCommands(); this->AddScriptingCommands();
} }
if (commandSet == CommandSet::Project) { if (role == cmState::Role::Project || role == cmState::Role::FindPackage) {
this->AddProjectCommands(); this->AddProjectCommands();
} }
@@ -3477,7 +3478,7 @@ int cmake::CheckBuildSystem()
// Read the rerun check file and use it to decide whether to do the // Read the rerun check file and use it to decide whether to do the
// global generate. // global generate.
// Actually, all we need is the `set` command. // Actually, all we need is the `set` command.
cmake cm(CommandSet::Script, cmState::Role::Script); cmake cm(cmState::Role::Script);
cm.GetCurrentSnapshot().SetDefaultDefinitions(); cm.GetCurrentSnapshot().SetDefaultDefinitions();
cmGlobalGenerator gg(&cm); cmGlobalGenerator gg(&cm);
cmMakefile mf(&gg, cm.GetCurrentSnapshot()); cmMakefile mf(&gg, cm.GetCurrentSnapshot());
+1 -8
View File
@@ -86,13 +86,6 @@ struct cmGlobCacheEntry;
class cmake class cmake
{ {
public: public:
enum class CommandSet
{
None, // no commands
Script, // script commands
Project, // all commands
};
enum DiagLevel enum DiagLevel
{ {
DIAG_IGNORE, DIAG_IGNORE,
@@ -171,7 +164,7 @@ public:
static int const DEFAULT_BUILD_PARALLEL_LEVEL = 0; static int const DEFAULT_BUILD_PARALLEL_LEVEL = 0;
/// Default constructor /// Default constructor
cmake(CommandSet commandSet, cmState::Role role, cmake(cmState::Role role,
cmState::TryCompile isTryCompile = cmState::TryCompile::No); cmState::TryCompile isTryCompile = cmState::TryCompile::No);
/// Destructor /// Destructor
~cmake(); ~cmake();
+7 -10
View File
@@ -221,7 +221,7 @@ int do_cmake(int ac, char const* const* av)
doc.addCMakeStandardDocSections(); doc.addCMakeStandardDocSections();
if (doc.CheckOptions(ac, av, "--")) { if (doc.CheckOptions(ac, av, "--")) {
// Construct and print requested documentation. // Construct and print requested documentation.
cmake hcm(cmake::CommandSet::None, cmState::Role::Help); cmake hcm(cmState::Role::Help);
hcm.AddCMakePaths(); hcm.AddCMakePaths();
// the command line args are processed here so that you can do // the command line args are processed here so that you can do
@@ -359,13 +359,10 @@ int do_cmake(int ac, char const* const* av)
} }
if (sysinfo) { if (sysinfo) {
cmake cm(cmake::CommandSet::Project, cmState::Role::Project); cmake cm(cmState::Role::Project);
int ret = cm.GetSystemInformation(parsedArgs); int ret = cm.GetSystemInformation(parsedArgs);
return ret; return ret;
} }
cmake::CommandSet const commandSet = workingMode == cmake::SCRIPT_MODE
? cmake::CommandSet::Script
: cmake::CommandSet::Project;
cmState::Role role = cmState::Role::Internal; cmState::Role role = cmState::Role::Internal;
switch (workingMode) { switch (workingMode) {
case cmake::NORMAL_MODE: case cmake::NORMAL_MODE:
@@ -382,7 +379,7 @@ int do_cmake(int ac, char const* const* av)
auto const failurePolicy = workingMode == cmake::NORMAL_MODE auto const failurePolicy = workingMode == cmake::NORMAL_MODE
? cmake::CommandFailureAction::EXIT_CODE ? cmake::CommandFailureAction::EXIT_CODE
: cmake::CommandFailureAction::FATAL_ERROR; : cmake::CommandFailureAction::FATAL_ERROR;
cmake cm(commandSet, role); cmake cm(role);
cmSystemTools::SetMessageCallback( cmSystemTools::SetMessageCallback(
[&cm](std::string const& msg, cmMessageMetadata const& md) { [&cm](std::string const& msg, cmMessageMetadata const& md) {
cmakemainMessageCallback(msg, md, &cm); cmakemainMessageCallback(msg, md, &cm);
@@ -680,7 +677,7 @@ int do_build(int ac, char const* const* av)
return 1; return 1;
} }
cmake cm(cmake::CommandSet::None, cmState::Role::Internal); cmake cm(cmState::Role::Internal);
cmSystemTools::SetMessageCallback( cmSystemTools::SetMessageCallback(
[&cm](std::string const& msg, cmMessageMetadata const& md) { [&cm](std::string const& msg, cmMessageMetadata const& md) {
cmakemainMessageCallback(msg, md, &cm); cmakemainMessageCallback(msg, md, &cm);
@@ -962,7 +959,7 @@ int do_install(int ac, char const* const* av)
} else { } else {
for (auto const& script : handler.GetScripts()) { for (auto const& script : handler.GetScripts()) {
std::vector<std::string> cmd = script.command; std::vector<std::string> cmd = script.command;
cmake cm(cmake::CommandSet::Script, cmState::Role::Script); cmake cm(cmState::Role::Script);
cmSystemTools::SetMessageCallback( cmSystemTools::SetMessageCallback(
[&cm](std::string const& msg, cmMessageMetadata const& md) { [&cm](std::string const& msg, cmMessageMetadata const& md) {
cmakemainMessageCallback(msg, md, &cm); cmakemainMessageCallback(msg, md, &cm);
@@ -1065,7 +1062,7 @@ int do_workflow(int ac, char const* const* av)
return 1; return 1;
} }
cmake cm(cmake::CommandSet::None, cmState::Role::Internal); cmake cm(cmState::Role::Internal);
cmSystemTools::SetMessageCallback( cmSystemTools::SetMessageCallback(
[&cm](std::string const& msg, cmMessageMetadata const& md) { [&cm](std::string const& msg, cmMessageMetadata const& md) {
cmakemainMessageCallback(msg, md, &cm); cmakemainMessageCallback(msg, md, &cm);
@@ -1109,7 +1106,7 @@ int do_open(int ac, char const* const* av)
return 1; return 1;
} }
cmake cm(cmake::CommandSet::None, cmState::Role::Internal); cmake cm(cmState::Role::Internal);
cmSystemTools::SetMessageCallback( cmSystemTools::SetMessageCallback(
[&cm](std::string const& msg, cmMessageMetadata const& md) { [&cm](std::string const& msg, cmMessageMetadata const& md) {
cmakemainMessageCallback(msg, md, &cm); cmakemainMessageCallback(msg, md, &cm);
+3 -3
View File
@@ -1150,7 +1150,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string> const& args,
std::cerr << "-E capabilities accepts no additional arguments\n"; std::cerr << "-E capabilities accepts no additional arguments\n";
return 1; return 1;
} }
cmake cm(cmake::CommandSet::None, cmState::Role::Internal); cmake cm(cmState::Role::Internal);
std::cout << cm.ReportCapabilities(); std::cout << cm.ReportCapabilities();
return 0; return 0;
} }
@@ -1398,7 +1398,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string> const& args,
// Create a cmake object instance to process dependencies. // Create a cmake object instance to process dependencies.
// All we need is the `set` command. // All we need is the `set` command.
cmake cm(cmake::CommandSet::Script, cmState::Role::Script); cmake cm(cmState::Role::Script);
std::string gen; std::string gen;
std::string homeDir; std::string homeDir;
std::string startDir; std::string startDir;
@@ -1752,7 +1752,7 @@ int cmcmd::ExecuteCMakeCommand(std::vector<std::string> const& args,
} }
// Create a cmake object instance to process dependencies. // Create a cmake object instance to process dependencies.
// All we need is the `set` command. // All we need is the `set` command.
cmake cm(cmake::CommandSet::Script, cmState::Role::Script); cmake cm(cmState::Role::Script);
std::string homeDir; std::string homeDir;
std::string startDir; std::string startDir;
std::string homeOutDir; std::string homeOutDir;
@@ -54,8 +54,7 @@ static Dummies CreateDummies(
std::string currentBinaryDirectory = "c:/CurrentBinaryDirectory") std::string currentBinaryDirectory = "c:/CurrentBinaryDirectory")
{ {
Dummies dummies; Dummies dummies;
dummies.CMake = std::make_shared<cmake>(cmake::CommandSet::Project, dummies.CMake = std::make_shared<cmake>(cmState::Role::Project);
cmState::Role::Project);
cmState* state = dummies.CMake->GetState(); cmState* state = dummies.CMake->GetState();
dummies.GlobalGenerator = dummies.GlobalGenerator =
std::make_shared<cmGlobalGenerator>(dummies.CMake.get()); std::make_shared<cmGlobalGenerator>(dummies.CMake.get());
@@ -501,7 +500,7 @@ static bool testCreateFromFileSet()
auto variablesManager = auto variablesManager =
std::make_shared<cmDebugger::cmDebuggerVariablesManager>(); std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
cmake cm(cmake::CommandSet::None, cmState::Role::Internal); cmake cm(cmState::Role::Internal);
cmFileSet fileSet(cm, "Foo", "HEADERS", cmFileSetVisibility::Public); cmFileSet fileSet(cm, "Foo", "HEADERS", cmFileSetVisibility::Public);
BT<std::string> directory; BT<std::string> directory;
directory.Value = "c:/"; directory.Value = "c:/";
@@ -545,7 +544,7 @@ static bool testCreateFromFileSets()
auto variablesManager = auto variablesManager =
std::make_shared<cmDebugger::cmDebuggerVariablesManager>(); std::make_shared<cmDebugger::cmDebuggerVariablesManager>();
cmake cm(cmake::CommandSet::None, cmState::Role::Internal); cmake cm(cmState::Role::Internal);
cmFileSet fileSet(cm, "Foo", "HEADERS", cmFileSetVisibility::Public); cmFileSet fileSet(cm, "Foo", "HEADERS", cmFileSetVisibility::Public);
BT<std::string> directory; BT<std::string> directory;
directory.Value = "c:/"; directory.Value = "c:/";