From 3eaf054d6a68ea7e915abddb61e82ff94b123fd6 Mon Sep 17 00:00:00 2001 From: Ken Martin Date: Thu, 19 Sep 2002 09:48:39 -0400 Subject: new command --- Source/cmTryRunCommand.cxx | 124 +++++++++++++++++++++++++++++++++++++++++++++ Source/cmTryRunCommand.h | 76 +++++++++++++++++++++++++++ 2 files changed, 200 insertions(+) create mode 100644 Source/cmTryRunCommand.cxx create mode 100644 Source/cmTryRunCommand.h diff --git a/Source/cmTryRunCommand.cxx b/Source/cmTryRunCommand.cxx new file mode 100644 index 0000000..d65015d --- /dev/null +++ b/Source/cmTryRunCommand.cxx @@ -0,0 +1,124 @@ +/*========================================================================= + + Program: Insight Segmentation & Registration Toolkit + Module: $RCSfile$ + Language: C++ + Date: $Date$ + Version: $Revision$ + + Copyright (c) 2002 Insight Consortium. All rights reserved. + See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#include "cmTryRunCommand.h" +#include "cmCacheManager.h" +#include "cmTryCompileCommand.h" + +// cmExecutableCommand +bool cmTryRunCommand::InitialPass(std::vector const& argv) +{ + if(argv.size() < 4) + { + return false; + } + + // build an arg list for TryCompile and extract the runArgs + std::vector tryCompile; + std::string runArgs; + int i; + for (i = 1; i < argv.size(); ++i) + { + if (argv[i] == "ARGS") + { + ++i; + while (i < argv.size() && argv[i] != "COMPILE_DEFINITIONS" && + argv[i] != "CMAKE_FLAGS") + { + runArgs += " "; + runArgs += argv[i]; + ++i; + } + if (i < argv.size()) + { + tryCompile.push_back(argv[i]); + } + } + else + { + tryCompile.push_back(argv[i]); + } + } + + // do the try compile + int res = cmTryCompileCommand::CoreTryCompileCode(m_Makefile, tryCompile, false); + + // now try running the command if it compiled + std::string binaryDirectory = argv[1] + "/CMakeTmp"; + if (!res) + { + int retVal; + std::string output; + std::string command; + command = binaryDirectory; + command += "/cmTryRunExec"; + command += cmSystemTools::GetExecutableExtension(); + std::string fullPath; + if(cmSystemTools::FileExists(command.c_str())) + { + fullPath = cmSystemTools::CollapseFullPath(command.c_str()); + } + else + { + command = binaryDirectory; + command += "/Debug/cmTryRunExec"; + command += cmSystemTools::GetExecutableExtension(); + if(cmSystemTools::FileExists(command.c_str())) + { + fullPath = cmSystemTools::CollapseFullPath(command.c_str()); + } + } + if (runArgs.size() > 1) + { + std::string finalCommand = fullPath; + if(runArgs.size()) + { + finalCommand = cmSystemTools::ConvertToOutputPath(fullPath.c_str()); + finalCommand += runArgs; + } + cmSystemTools::RunCommand(finalCommand.c_str(), output, retVal); + // set the run var + char retChar[1000]; + sprintf(retChar,"%i",retVal); + m_Makefile->AddDefinition(argv[1].c_str(), retChar); + } + else + { + cmSystemTools::Error("Unable to find executable for TRY_RUN"); + } + } + + // if we created a directory etc, then cleanup after ourselves + cmDirectory dir; + dir.Load(binaryDirectory.c_str()); + size_t fileNum; + for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum) + { + if (strcmp(dir.GetFile(fileNum),".") && + strcmp(dir.GetFile(fileNum),"..")) + { + std::string fullPath = binaryDirectory; + fullPath += "/"; + fullPath += dir.GetFile(fileNum); + cmSystemTools::RemoveFile(fullPath.c_str()); + } + } + + return true; +} + + + diff --git a/Source/cmTryRunCommand.h b/Source/cmTryRunCommand.h new file mode 100644 index 0000000..985ccd0 --- /dev/null +++ b/Source/cmTryRunCommand.h @@ -0,0 +1,76 @@ +/*========================================================================= + + Program: Insight Segmentation & Registration Toolkit + Module: $RCSfile$ + Language: C++ + Date: $Date$ + Version: $Revision$ + + Copyright (c) 2002 Insight Consortium. All rights reserved. + See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef cmTryRunCommand_h +#define cmTryRunCommand_h + +#include "cmStandardIncludes.h" +#include "cmCommand.h" + +/** \class cmTryRunCommand + * \brief Specifies where to install some files + * + * cmTryRunCommand is used to test if soucre code can be compiled + */ +class cmTryRunCommand : public cmCommand +{ +public: + /** + * This is a virtual constructor for the command. + */ + virtual cmCommand* Clone() + { + return new cmTryRunCommand; + } + + /** + * This is called when the command is first encountered in + * the CMakeLists.txt file. + */ + virtual bool InitialPass(std::vector const& args); + + /** + * The name of the command as specified in CMakeList.txt. + */ + virtual const char* GetName() { return "TRY_RUN";} + + /** + * Succinct documentation. + */ + virtual const char* GetTerseDocumentation() + { + return "Try compiling and then running some code."; + } + + + /** + * More documentation. + */ + virtual const char* GetFullDocumentation() + { + return + "TRY_RUN(RUN_RESULT_VAR COMPILE_RESULT_VAR bindir srcfile > > ...>)\n" + "Try compiling a srcfile. Return the success or failure in "; + "COMPILE_RESULT_VAR. Then if the compile succeeded, run the "; + "executable and return the result in RUN_RESULT_VAR. "; + } + + cmTypeMacro(cmTryRunCommand, cmCommand); + +}; + + +#endif -- cgit v0.12