From e73c8eaff20b33452db251ce1de1b1162b647178 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 15 Jun 2022 15:07:06 -0400 Subject: cmTry{Compile,Run}Command: Port away from legacy cmCommand Convert the command entry points to free functions. --- Source/cmCommands.cxx | 6 ++-- Source/cmCoreTryCompile.h | 12 +++++-- Source/cmTryCompileCommand.cxx | 27 +++++++------- Source/cmTryCompileCommand.h | 30 ++-------------- Source/cmTryRunCommand.cxx | 82 +++++++++++++++++++++++++++++++----------- Source/cmTryRunCommand.h | 50 ++------------------------ 6 files changed, 90 insertions(+), 117 deletions(-) diff --git a/Source/cmCommands.cxx b/Source/cmCommands.cxx index c6296f9f42..5e616b30d0 100644 --- a/Source/cmCommands.cxx +++ b/Source/cmCommands.cxx @@ -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()); - state->AddBuiltinCommand("try_run", cm::make_unique()); + state->AddBuiltinCommand("try_compile", cmTryCompileCommand); + state->AddBuiltinCommand("try_run", cmTryRunCommand); state->AddBuiltinCommand("target_precompile_headers", cmTargetPrecompileHeadersCommand); diff --git a/Source/cmCoreTryCompile.h b/Source/cmCoreTryCompile.h index 594fd7f9f5..9d43899110 100644 --- a/Source/cmCoreTryCompile.h +++ b/Source/cmCoreTryCompile.h @@ -7,19 +7,24 @@ #include #include -#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; }; diff --git a/Source/cmTryCompileCommand.cxx b/Source/cmTryCompileCommand.cxx index 130c2288a8..7514a23769 100644 --- a/Source/cmTryCompileCommand.cxx +++ b/Source/cmTryCompileCommand.cxx @@ -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 const& argv, - cmExecutionStatus&) +bool cmTryCompileCommand(std::vector 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; diff --git a/Source/cmTryCompileCommand.h b/Source/cmTryCompileCommand.h index d8cc16e59a..6a3430b6b6 100644 --- a/Source/cmTryCompileCommand.h +++ b/Source/cmTryCompileCommand.h @@ -7,33 +7,7 @@ #include #include -#include - -#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 Clone() override - { - return cm::make_unique(); - } - - /** - * This is called when the command is first encountered in - * the CMakeLists.txt file. - */ - bool InitialPass(std::vector const& args, - cmExecutionStatus& status) override; -}; +bool cmTryCompileCommand(std::vector const& args, + cmExecutionStatus& status); diff --git a/Source/cmTryRunCommand.cxx b/Source/cmTryRunCommand.cxx index 4cd0adc78a..98cacdc65a 100644 --- a/Source/cmTryRunCommand.cxx +++ b/Source/cmTryRunCommand.cxx @@ -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 const& argv, - cmExecutionStatus&) +class TryRunCommandImpl : public cmCoreTryCompile { - if (argv.size() < 4) { - return false; - } - - 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; +public: + TryRunCommandImpl(cmMakefile* mf) + : cmCoreTryCompile(mf) + { } + bool TryRunCode(std::vector 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 const& argv) +{ // build an arg list for TryCompile and extract the runArgs, std::vector tryCompile; @@ -240,9 +258,9 @@ bool cmTryRunCommand::InitialPass(std::vector 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 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); +} diff --git a/Source/cmTryRunCommand.h b/Source/cmTryRunCommand.h index ccf678e2ac..38e36380e4 100644 --- a/Source/cmTryRunCommand.h +++ b/Source/cmTryRunCommand.h @@ -7,53 +7,7 @@ #include #include -#include - -#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 Clone() override - { - return cm::make_unique(); - } - - /** - * This is called when the command is first encountered in - * the CMakeLists.txt file. - */ - bool InitialPass(std::vector 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 const& args, + cmExecutionStatus& status); -- cgit v1.2.1