mirror of
https://github.com/Kitware/CMake.git
synced 2026-02-13 10:39:07 -06:00
cmTry{Compile,Run}Command: Port away from legacy cmCommand
Convert the command entry points to free functions.
This commit is contained in:
@@ -20,7 +20,6 @@
|
|||||||
#include "cmCMakeMinimumRequired.h"
|
#include "cmCMakeMinimumRequired.h"
|
||||||
#include "cmCMakePathCommand.h"
|
#include "cmCMakePathCommand.h"
|
||||||
#include "cmCMakePolicyCommand.h"
|
#include "cmCMakePolicyCommand.h"
|
||||||
#include "cmCommand.h"
|
|
||||||
#include "cmConfigureFileCommand.h"
|
#include "cmConfigureFileCommand.h"
|
||||||
#include "cmContinueCommand.h"
|
#include "cmContinueCommand.h"
|
||||||
#include "cmCreateTestSourceList.h"
|
#include "cmCreateTestSourceList.h"
|
||||||
@@ -264,9 +263,8 @@ void GetProjectCommands(cmState* state)
|
|||||||
cmTargetLinkLibrariesCommand);
|
cmTargetLinkLibrariesCommand);
|
||||||
state->AddBuiltinCommand("target_link_options", cmTargetLinkOptionsCommand);
|
state->AddBuiltinCommand("target_link_options", cmTargetLinkOptionsCommand);
|
||||||
state->AddBuiltinCommand("target_sources", cmTargetSourcesCommand);
|
state->AddBuiltinCommand("target_sources", cmTargetSourcesCommand);
|
||||||
state->AddBuiltinCommand("try_compile",
|
state->AddBuiltinCommand("try_compile", cmTryCompileCommand);
|
||||||
cm::make_unique<cmTryCompileCommand>());
|
state->AddBuiltinCommand("try_run", cmTryRunCommand);
|
||||||
state->AddBuiltinCommand("try_run", cm::make_unique<cmTryRunCommand>());
|
|
||||||
state->AddBuiltinCommand("target_precompile_headers",
|
state->AddBuiltinCommand("target_precompile_headers",
|
||||||
cmTargetPrecompileHeadersCommand);
|
cmTargetPrecompileHeadersCommand);
|
||||||
|
|
||||||
|
|||||||
@@ -7,19 +7,24 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "cmCommand.h"
|
|
||||||
#include "cmStateTypes.h"
|
#include "cmStateTypes.h"
|
||||||
|
|
||||||
|
class cmMakefile;
|
||||||
|
|
||||||
/** \class cmCoreTryCompile
|
/** \class cmCoreTryCompile
|
||||||
* \brief Base class for cmTryCompileCommand and cmTryRunCommand
|
* \brief Base class for cmTryCompileCommand and cmTryRunCommand
|
||||||
*
|
*
|
||||||
* cmCoreTryCompile implements the functionality to build a program.
|
* cmCoreTryCompile implements the functionality to build a program.
|
||||||
* It is the base class for cmTryCompileCommand and cmTryRunCommand.
|
* It is the base class for cmTryCompileCommand and cmTryRunCommand.
|
||||||
*/
|
*/
|
||||||
class cmCoreTryCompile : public cmCommand
|
class cmCoreTryCompile
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
protected:
|
cmCoreTryCompile(cmMakefile* mf)
|
||||||
|
: Makefile(mf)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the core code for try compile. It is here so that other
|
* This is the core code for try compile. It is here so that other
|
||||||
* commands, such as TryRun can access the same logic without
|
* commands, such as TryRun can access the same logic without
|
||||||
@@ -46,4 +51,5 @@ protected:
|
|||||||
std::string OutputFile;
|
std::string OutputFile;
|
||||||
std::string FindErrorMessage;
|
std::string FindErrorMessage;
|
||||||
bool SrcFileSignature = false;
|
bool SrcFileSignature = false;
|
||||||
|
cmMakefile* Makefile;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,34 +2,35 @@
|
|||||||
file Copyright.txt or https://cmake.org/licensing for details. */
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
||||||
#include "cmTryCompileCommand.h"
|
#include "cmTryCompileCommand.h"
|
||||||
|
|
||||||
|
#include "cmCoreTryCompile.h"
|
||||||
|
#include "cmExecutionStatus.h"
|
||||||
#include "cmMakefile.h"
|
#include "cmMakefile.h"
|
||||||
#include "cmMessageType.h"
|
#include "cmMessageType.h"
|
||||||
#include "cmake.h"
|
#include "cmake.h"
|
||||||
|
|
||||||
class cmExecutionStatus;
|
bool cmTryCompileCommand(std::vector<std::string> const& args,
|
||||||
|
cmExecutionStatus& status)
|
||||||
// cmTryCompileCommand
|
|
||||||
bool cmTryCompileCommand::InitialPass(std::vector<std::string> const& argv,
|
|
||||||
cmExecutionStatus&)
|
|
||||||
{
|
{
|
||||||
if (argv.size() < 3) {
|
if (args.size() < 3) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->Makefile->GetCMakeInstance()->GetWorkingMode() ==
|
cmMakefile& mf = status.GetMakefile();
|
||||||
cmake::FIND_PACKAGE_MODE) {
|
|
||||||
this->Makefile->IssueMessage(
|
if (mf.GetCMakeInstance()->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) {
|
||||||
|
mf.IssueMessage(
|
||||||
MessageType::FATAL_ERROR,
|
MessageType::FATAL_ERROR,
|
||||||
"The try_compile() command is not supported in --find-package mode.");
|
"The try_compile() command is not supported in --find-package mode.");
|
||||||
return false;
|
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 They specified clean then we clean up what we can
|
||||||
if (this->SrcFileSignature) {
|
if (tc.SrcFileSignature) {
|
||||||
if (!this->Makefile->GetCMakeInstance()->GetDebugTryCompile()) {
|
if (!mf.GetCMakeInstance()->GetDebugTryCompile()) {
|
||||||
this->CleanupFiles(this->BinaryDirectory);
|
tc.CleanupFiles(tc.BinaryDirectory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -7,33 +7,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <cm/memory>
|
|
||||||
|
|
||||||
#include "cmCommand.h"
|
|
||||||
#include "cmCoreTryCompile.h"
|
|
||||||
|
|
||||||
class cmExecutionStatus;
|
class cmExecutionStatus;
|
||||||
|
|
||||||
/** \class cmTryCompileCommand
|
bool cmTryCompileCommand(std::vector<std::string> const& args,
|
||||||
* \brief Specifies where to install some files
|
cmExecutionStatus& status);
|
||||||
*
|
|
||||||
* 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;
|
|
||||||
};
|
|
||||||
|
|||||||
@@ -6,7 +6,9 @@
|
|||||||
|
|
||||||
#include "cmsys/FStream.hxx"
|
#include "cmsys/FStream.hxx"
|
||||||
|
|
||||||
|
#include "cmCoreTryCompile.h"
|
||||||
#include "cmDuration.h"
|
#include "cmDuration.h"
|
||||||
|
#include "cmExecutionStatus.h"
|
||||||
#include "cmMakefile.h"
|
#include "cmMakefile.h"
|
||||||
#include "cmMessageType.h"
|
#include "cmMessageType.h"
|
||||||
#include "cmRange.h"
|
#include "cmRange.h"
|
||||||
@@ -17,24 +19,40 @@
|
|||||||
#include "cmValue.h"
|
#include "cmValue.h"
|
||||||
#include "cmake.h"
|
#include "cmake.h"
|
||||||
|
|
||||||
class cmExecutionStatus;
|
namespace {
|
||||||
|
|
||||||
// cmTryRunCommand
|
class TryRunCommandImpl : public cmCoreTryCompile
|
||||||
bool cmTryRunCommand::InitialPass(std::vector<std::string> const& argv,
|
|
||||||
cmExecutionStatus&)
|
|
||||||
{
|
{
|
||||||
if (argv.size() < 4) {
|
public:
|
||||||
return false;
|
TryRunCommandImpl(cmMakefile* mf)
|
||||||
|
: cmCoreTryCompile(mf)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->Makefile->GetCMakeInstance()->GetWorkingMode() ==
|
bool TryRunCode(std::vector<std::string> const& args);
|
||||||
cmake::FIND_PACKAGE_MODE) {
|
|
||||||
this->Makefile->IssueMessage(
|
|
||||||
MessageType::FATAL_ERROR,
|
|
||||||
"The try_run() command is not supported in --find-package mode.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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,
|
// build an arg list for TryCompile and extract the runArgs,
|
||||||
std::vector<std::string> tryCompile;
|
std::vector<std::string> tryCompile;
|
||||||
|
|
||||||
@@ -240,9 +258,9 @@ bool cmTryRunCommand::InitialPass(std::vector<std::string> const& argv,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cmTryRunCommand::RunExecutable(const std::string& runArgs,
|
void TryRunCommandImpl::RunExecutable(const std::string& runArgs,
|
||||||
std::string* out, std::string* stdOut,
|
std::string* out, std::string* stdOut,
|
||||||
std::string* stdErr)
|
std::string* stdErr)
|
||||||
{
|
{
|
||||||
int retVal = -1;
|
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
|
executable, two cache variables are created which will hold the results
|
||||||
the executable would have produced.
|
the executable would have produced.
|
||||||
*/
|
*/
|
||||||
void cmTryRunCommand::DoNotRunExecutable(const std::string& runArgs,
|
void TryRunCommandImpl::DoNotRunExecutable(const std::string& runArgs,
|
||||||
const std::string& srcFile,
|
const std::string& srcFile,
|
||||||
std::string* out, std::string* stdOut,
|
std::string* out,
|
||||||
std::string* stdErr)
|
std::string* stdOut,
|
||||||
|
std::string* stdErr)
|
||||||
{
|
{
|
||||||
// copy the executable out of the CMakeFiles/ directory, so it is not
|
// 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
|
// 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);
|
(*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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,53 +7,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <cm/memory>
|
|
||||||
|
|
||||||
#include "cmCommand.h"
|
|
||||||
#include "cmCoreTryCompile.h"
|
|
||||||
|
|
||||||
class cmExecutionStatus;
|
class cmExecutionStatus;
|
||||||
|
|
||||||
/** \class cmTryRunCommand
|
bool cmTryRunCommand(std::vector<std::string> const& args,
|
||||||
* \brief Specifies where to install some files
|
cmExecutionStatus& status);
|
||||||
*
|
|
||||||
* 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;
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user