cmTry{Compile,Run}Command: Port away from legacy cmCommand

Convert the command entry points to free functions.
stage/master/nightly/2022/07/27
Brad King 2022-06-15 15:07:06 -04:00
parent 7ba3a3290f
commit e73c8eaff2
6 changed files with 89 additions and 116 deletions

View File

@ -20,7 +20,6 @@
#include "cmCMakeMinimumRequired.h"
#include "cmCMakePathCommand.h"
#include "cmCMakePolicyCommand.h"
#include "cmCommand.h"
#include "cmConfigureFileCommand.h"
#include "cmContinueCommand.h"
#include "cmCreateTestSourceList.h"
@ -264,9 +263,8 @@ void GetProjectCommands(cmState* state)
cmTargetLinkLibrariesCommand);
state->AddBuiltinCommand("target_link_options", cmTargetLinkOptionsCommand);
state->AddBuiltinCommand("target_sources", cmTargetSourcesCommand);
state->AddBuiltinCommand("try_compile",
cm::make_unique<cmTryCompileCommand>());
state->AddBuiltinCommand("try_run", cm::make_unique<cmTryRunCommand>());
state->AddBuiltinCommand("try_compile", cmTryCompileCommand);
state->AddBuiltinCommand("try_run", cmTryRunCommand);
state->AddBuiltinCommand("target_precompile_headers",
cmTargetPrecompileHeadersCommand);

View File

@ -7,19 +7,24 @@
#include <string>
#include <vector>
#include "cmCommand.h"
#include "cmStateTypes.h"
class cmMakefile;
/** \class cmCoreTryCompile
* \brief Base class for cmTryCompileCommand and cmTryRunCommand
*
* cmCoreTryCompile implements the functionality to build a program.
* It is the base class for cmTryCompileCommand and cmTryRunCommand.
*/
class cmCoreTryCompile : public cmCommand
class cmCoreTryCompile
{
public:
protected:
cmCoreTryCompile(cmMakefile* mf)
: Makefile(mf)
{
}
/**
* This is the core code for try compile. It is here so that other
* commands, such as TryRun can access the same logic without
@ -46,4 +51,5 @@ protected:
std::string OutputFile;
std::string FindErrorMessage;
bool SrcFileSignature = false;
cmMakefile* Makefile;
};

View File

@ -2,34 +2,35 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmTryCompileCommand.h"
#include "cmCoreTryCompile.h"
#include "cmExecutionStatus.h"
#include "cmMakefile.h"
#include "cmMessageType.h"
#include "cmake.h"
class cmExecutionStatus;
// cmTryCompileCommand
bool cmTryCompileCommand::InitialPass(std::vector<std::string> const& argv,
cmExecutionStatus&)
bool cmTryCompileCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
if (argv.size() < 3) {
if (args.size() < 3) {
return false;
}
if (this->Makefile->GetCMakeInstance()->GetWorkingMode() ==
cmake::FIND_PACKAGE_MODE) {
this->Makefile->IssueMessage(
cmMakefile& mf = status.GetMakefile();
if (mf.GetCMakeInstance()->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) {
mf.IssueMessage(
MessageType::FATAL_ERROR,
"The try_compile() command is not supported in --find-package mode.");
return false;
}
this->TryCompileCode(argv, false);
cmCoreTryCompile tc(&mf);
tc.TryCompileCode(args, false);
// if They specified clean then we clean up what we can
if (this->SrcFileSignature) {
if (!this->Makefile->GetCMakeInstance()->GetDebugTryCompile()) {
this->CleanupFiles(this->BinaryDirectory);
if (tc.SrcFileSignature) {
if (!mf.GetCMakeInstance()->GetDebugTryCompile()) {
tc.CleanupFiles(tc.BinaryDirectory);
}
}
return true;

View File

@ -7,33 +7,7 @@
#include <string>
#include <vector>
#include <cm/memory>
#include "cmCommand.h"
#include "cmCoreTryCompile.h"
class cmExecutionStatus;
/** \class cmTryCompileCommand
* \brief Specifies where to install some files
*
* cmTryCompileCommand is used to test if source code can be compiled
*/
class cmTryCompileCommand : public cmCoreTryCompile
{
public:
/**
* This is a virtual constructor for the command.
*/
std::unique_ptr<cmCommand> Clone() override
{
return cm::make_unique<cmTryCompileCommand>();
}
/**
* This is called when the command is first encountered in
* the CMakeLists.txt file.
*/
bool InitialPass(std::vector<std::string> const& args,
cmExecutionStatus& status) override;
};
bool cmTryCompileCommand(std::vector<std::string> const& args,
cmExecutionStatus& status);

View File

@ -6,7 +6,9 @@
#include "cmsys/FStream.hxx"
#include "cmCoreTryCompile.h"
#include "cmDuration.h"
#include "cmExecutionStatus.h"
#include "cmMakefile.h"
#include "cmMessageType.h"
#include "cmRange.h"
@ -17,24 +19,40 @@
#include "cmValue.h"
#include "cmake.h"
class cmExecutionStatus;
namespace {
// cmTryRunCommand
bool cmTryRunCommand::InitialPass(std::vector<std::string> const& argv,
cmExecutionStatus&)
class TryRunCommandImpl : public cmCoreTryCompile
{
if (argv.size() < 4) {
return false;
public:
TryRunCommandImpl(cmMakefile* mf)
: cmCoreTryCompile(mf)
{
}
if (this->Makefile->GetCMakeInstance()->GetWorkingMode() ==
cmake::FIND_PACKAGE_MODE) {
this->Makefile->IssueMessage(
MessageType::FATAL_ERROR,
"The try_run() command is not supported in --find-package mode.");
return false;
}
bool TryRunCode(std::vector<std::string> const& args);
void RunExecutable(const std::string& runArgs,
std::string* runOutputContents,
std::string* runOutputStdOutContents,
std::string* runOutputStdErrContents);
void DoNotRunExecutable(const std::string& runArgs,
const std::string& srcFile,
std::string* runOutputContents,
std::string* runOutputStdOutContents,
std::string* runOutputStdErrContents);
std::string CompileResultVariable;
std::string RunResultVariable;
std::string OutputVariable;
std::string RunOutputVariable;
std::string RunOutputStdOutVariable;
std::string RunOutputStdErrVariable;
std::string CompileOutputVariable;
std::string WorkingDirectory;
};
bool TryRunCommandImpl::TryRunCode(std::vector<std::string> const& argv)
{
// build an arg list for TryCompile and extract the runArgs,
std::vector<std::string> tryCompile;
@ -240,9 +258,9 @@ bool cmTryRunCommand::InitialPass(std::vector<std::string> const& argv,
return true;
}
void cmTryRunCommand::RunExecutable(const std::string& runArgs,
std::string* out, std::string* stdOut,
std::string* stdErr)
void TryRunCommandImpl::RunExecutable(const std::string& runArgs,
std::string* out, std::string* stdOut,
std::string* stdErr)
{
int retVal = -1;
@ -288,10 +306,11 @@ void cmTryRunCommand::RunExecutable(const std::string& runArgs,
executable, two cache variables are created which will hold the results
the executable would have produced.
*/
void cmTryRunCommand::DoNotRunExecutable(const std::string& runArgs,
const std::string& srcFile,
std::string* out, std::string* stdOut,
std::string* stdErr)
void TryRunCommandImpl::DoNotRunExecutable(const std::string& runArgs,
const std::string& srcFile,
std::string* out,
std::string* stdOut,
std::string* stdErr)
{
// copy the executable out of the CMakeFiles/ directory, so it is not
// removed at the end of try_run() and the user can run it manually
@ -521,3 +540,24 @@ void cmTryRunCommand::DoNotRunExecutable(const std::string& runArgs,
(*out) = *this->Makefile->GetDefinition(internalRunOutputName);
}
}
}
bool cmTryRunCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
if (args.size() < 4) {
return false;
}
cmMakefile& mf = status.GetMakefile();
if (mf.GetCMakeInstance()->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) {
mf.IssueMessage(
MessageType::FATAL_ERROR,
"The try_run() command is not supported in --find-package mode.");
return false;
}
TryRunCommandImpl tr(&mf);
return tr.TryRunCode(args);
}

View File

@ -7,53 +7,7 @@
#include <string>
#include <vector>
#include <cm/memory>
#include "cmCommand.h"
#include "cmCoreTryCompile.h"
class cmExecutionStatus;
/** \class cmTryRunCommand
* \brief Specifies where to install some files
*
* cmTryRunCommand is used to test if source code can be compiled
*/
class cmTryRunCommand : public cmCoreTryCompile
{
public:
/**
* This is a virtual constructor for the command.
*/
std::unique_ptr<cmCommand> Clone() override
{
return cm::make_unique<cmTryRunCommand>();
}
/**
* This is called when the command is first encountered in
* the CMakeLists.txt file.
*/
bool InitialPass(std::vector<std::string> const& args,
cmExecutionStatus& status) override;
private:
void RunExecutable(const std::string& runArgs,
std::string* runOutputContents,
std::string* runOutputStdOutContents,
std::string* runOutputStdErrContents);
void DoNotRunExecutable(const std::string& runArgs,
const std::string& srcFile,
std::string* runOutputContents,
std::string* runOutputStdOutContents,
std::string* runOutputStdErrContents);
std::string CompileResultVariable;
std::string RunResultVariable;
std::string OutputVariable;
std::string RunOutputVariable;
std::string RunOutputStdOutVariable;
std::string RunOutputStdErrVariable;
std::string CompileOutputVariable;
std::string WorkingDirectory;
};
bool cmTryRunCommand(std::vector<std::string> const& args,
cmExecutionStatus& status);