instrumentation: Make hooks compatible with presets

This commit is contained in:
Martin Duffy 2025-02-14 15:43:13 -05:00 committed by Brad King
parent 1fb8f4cad7
commit a2232db802
6 changed files with 67 additions and 50 deletions

View File

@ -2638,32 +2638,23 @@ int cmCTest::Run(std::vector<std::string> const& args)
}
#endif
std::function<int()> doTest = [this, &cmakeAndTest, &runScripts,
&processSteps]() -> int {
// now what should cmake do? if --build-and-test was specified then
// we run the build and test handler and return
if (cmakeAndTest) {
return this->RunCMakeAndTest();
}
// now what should cmake do? if --build-and-test was specified then
// we run the build and test handler and return
if (cmakeAndTest) {
return this->RunCMakeAndTest();
}
// -S, -SP, and/or -SP was specified
if (!runScripts.empty()) {
return this->RunScripts(runScripts);
}
// -S, -SP, and/or -SP was specified
if (!runScripts.empty()) {
return this->RunScripts(runScripts);
}
// -D, -T, and/or -M was specified
if (processSteps) {
return this->ProcessSteps();
}
// -D, -T, and/or -M was specified
if (processSteps) {
return this->ProcessSteps();
}
return this->ExecuteTests();
};
cmInstrumentation instrumentation(
cmSystemTools::GetCurrentWorkingDirectory());
int ret = instrumentation.InstrumentCommand("ctest", args,
[doTest]() { return doTest(); });
instrumentation.CollectTimingData(cmInstrumentationQuery::Hook::PostTest);
return ret;
return this->ExecuteTests(args);
}
int cmCTest::RunScripts(
@ -2687,7 +2678,7 @@ int cmCTest::RunScripts(
return res;
}
int cmCTest::ExecuteTests()
int cmCTest::ExecuteTests(std::vector<std::string> const& args)
{
this->Impl->ExtraVerbose = this->Impl->Verbose;
this->Impl->Verbose = true;
@ -2732,7 +2723,14 @@ int cmCTest::ExecuteTests()
}
handler.SetVerbose(this->Impl->Verbose);
if (handler.ProcessHandler() < 0) {
cmInstrumentation instrumentation(this->GetBinaryDir());
std::function<int()> processHandler = [&handler]() {
return handler.ProcessHandler();
};
int ret = instrumentation.InstrumentCommand("ctest", args, processHandler);
instrumentation.CollectTimingData(cmInstrumentationQuery::Hook::PostTest);
if (ret < 0) {
cmCTestLog(this, ERROR_MESSAGE, "Errors while running CTest\n");
if (!this->Impl->OutputTestOutputOnTestFailure) {
std::string const lastTestLog =

View File

@ -480,7 +480,7 @@ private:
int RunCMakeAndTest();
int RunScripts(std::vector<std::pair<std::string, bool>> const& scripts);
int ExecuteTests();
int ExecuteTests(std::vector<std::string> const& args);
struct Private;
std::unique_ptr<Private> Impl;

View File

@ -3720,7 +3720,8 @@ std::vector<std::string> cmake::GetDebugConfigs()
int cmake::Build(int jobs, std::string dir, std::vector<std::string> targets,
std::string config, std::vector<std::string> nativeOptions,
cmBuildOptions& buildOptions, bool verbose,
std::string const& presetName, bool listPresets)
std::string const& presetName, bool listPresets,
std::vector<std::string> const& args)
{
this->SetHomeDirectory("");
this->SetHomeOutputDirectory("");
@ -3958,16 +3959,39 @@ int cmake::Build(int jobs, std::string dir, std::vector<std::string> targets,
return 1;
}
#if !defined(CMAKE_BOOTSTRAP)
cmInstrumentation instrumentation(dir);
if (!instrumentation.errorMsg.empty()) {
cmSystemTools::Error(instrumentation.errorMsg);
return 1;
}
instrumentation.CollectTimingData(
cmInstrumentationQuery::Hook::PreCMakeBuild);
#endif
this->GlobalGenerator->PrintBuildCommandAdvice(std::cerr, jobs);
std::stringstream ostr;
// `cmGlobalGenerator::Build` logs metadata about what directory and commands
// are being executed to the `output` parameter. If CMake is verbose, print
// this out.
std::ostream& verbose_ostr = verbose ? std::cout : ostr;
int buildresult = this->GlobalGenerator->Build(
jobs, "", dir, projName, targets, verbose_ostr, "", config, buildOptions,
verbose, cmDuration::zero(), cmSystemTools::OUTPUT_PASSTHROUGH,
nativeOptions);
std::function<int()> doBuild = [this, jobs, dir, projName, targets,
&verbose_ostr, config, buildOptions, verbose,
nativeOptions]() {
return this->GlobalGenerator->Build(
jobs, "", dir, projName, targets, verbose_ostr, "", config, buildOptions,
verbose, cmDuration::zero(), cmSystemTools::OUTPUT_PASSTHROUGH,
nativeOptions);
};
#if !defined(CMAKE_BOOTSTRAP)
int buildresult =
instrumentation.InstrumentCommand("cmakeBuild", args, doBuild);
instrumentation.CollectTimingData(
cmInstrumentationQuery::Hook::PostCMakeBuild);
#else
int buildresult = doBuild();
#endif
return buildresult;
}

View File

@ -640,7 +640,8 @@ public:
int Build(int jobs, std::string dir, std::vector<std::string> targets,
std::string config, std::vector<std::string> nativeOptions,
cmBuildOptions& buildOptions, bool verbose,
std::string const& presetName, bool listPresets);
std::string const& presetName, bool listPresets,
std::vector<std::string> const& args);
//! run the --open option
bool Open(std::string const& dir, bool dryRun);

View File

@ -705,27 +705,12 @@ int do_build(int ac, char const* const* av)
cmakemainProgressCallback(msg, prog, &cm);
});
cmInstrumentation instrumentation(dir);
if (!instrumentation.errorMsg.empty()) {
cmSystemTools::Error(instrumentation.errorMsg);
return 1;
}
cmBuildOptions buildOptions(cleanFirst, false, resolveMode);
std::function<int()> doBuild = [&cm, &jobs, &dir, &targets, &config,
&nativeOptions, &buildOptions, &verbose,
&presetName, &listPresets]() {
return cm.Build(jobs, dir, std::move(targets), std::move(config),
std::move(nativeOptions), buildOptions, verbose,
presetName, listPresets);
};
instrumentation.CollectTimingData(
cmInstrumentationQuery::Hook::PreCMakeBuild);
std::vector<std::string> cmd;
cm::append(cmd, av, av + ac);
int ret = instrumentation.InstrumentCommand("cmakeBuild", cmd, doBuild);
instrumentation.CollectTimingData(
cmInstrumentationQuery::Hook::PostCMakeBuild);
return ret;
return cm.Build(jobs, dir, std::move(targets), std::move(config),
std::move(nativeOptions), buildOptions, verbose, presetName,
listPresets, cmd);
#endif
}

View File

@ -107,6 +107,15 @@ foreach(snippet IN LISTS snippets)
snippet_error(${snippet} "Unexpected config: ${config}")
endif()
endif()
# Verify command args were passed
if (filename MATCHES "^cmakeBuild|^ctest")
string(JSON command GET "${contents}" command)
if (NOT command MATCHES "-.* Debug")
snippet_error(${snippet} "Command value missing passed arguments")
endif()
endif()
endforeach()
# Verify that listed snippets match expected roles