CMake/Source/cmTryRunCommand.cxx

135 lines
3.7 KiB
C++
Raw Normal View History

2002-09-19 17:48:39 +04:00
/*=========================================================================
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<std::string> const& argv)
{
if(argv.size() < 4)
{
return false;
}
if ( m_Makefile->GetLocal() )
{
return true;
}
2002-09-19 17:48:39 +04:00
// build an arg list for TryCompile and extract the runArgs
std::vector<std::string> tryCompile;
std::string runArgs;
2002-09-20 16:09:03 +04:00
unsigned int i;
2002-09-19 17:48:39 +04:00
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
2002-09-20 00:07:23 +04:00
std::string binaryDirectory = argv[2] + "/CMakeTmp";
2002-09-19 17:48:39 +04:00
if (!res)
{
int retVal;
std::string output;
std::string command;
command = binaryDirectory;
2002-09-20 00:07:23 +04:00
command += "/cmTryCompileExec";
2002-09-19 17:48:39 +04:00
command += cmSystemTools::GetExecutableExtension();
std::string fullPath;
if(cmSystemTools::FileExists(command.c_str()))
{
fullPath = cmSystemTools::CollapseFullPath(command.c_str());
}
else
{
command = binaryDirectory;
2002-09-20 00:07:23 +04:00
command += "/Debug/cmTryCompileExec";
2002-09-19 17:48:39 +04:00
command += cmSystemTools::GetExecutableExtension();
if(cmSystemTools::FileExists(command.c_str()))
{
fullPath = cmSystemTools::CollapseFullPath(command.c_str());
}
2002-09-20 00:07:23 +04:00
else
{
cmSystemTools::Error("Unable to find executable for TRY_RUN",
command.c_str());
}
2002-09-19 17:48:39 +04:00
}
2002-09-20 00:07:23 +04:00
if (fullPath.size() > 1)
2002-09-19 17:48:39 +04:00
{
std::string finalCommand = fullPath;
if(runArgs.size())
{
finalCommand = cmSystemTools::ConvertToOutputPath(fullPath.c_str());
finalCommand += runArgs;
}
2002-09-19 19:06:22 +04:00
cmSystemTools::RunCommand(finalCommand.c_str(), output, retVal, 0, false);
2002-09-19 17:48:39 +04:00
// set the run var
char retChar[1000];
sprintf(retChar,"%i",retVal);
m_Makefile->AddCacheDefinition(argv[0].c_str(), retChar,
"Result of TRY_RUN", cmCacheManager::INTERNAL);
2002-09-19 17:48:39 +04:00
}
}
// 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());
}
}
2002-09-20 00:07:23 +04:00
std::string cacheFile = binaryDirectory;
cacheFile += "/CMakeLists.txt";
cmListFileCache::GetInstance()->FlushCache(cacheFile.c_str());
2002-09-19 17:48:39 +04:00
return true;
}