ENH: move output file search to cmTryCompileCommand.cxx, so it can be used
there too... many public static functions with lots of arguments... :-/ Alex
This commit is contained in:
parent
79756b0e67
commit
00e6d62fd1
|
@ -22,7 +22,8 @@
|
|||
#include <cmsys/Directory.hxx>
|
||||
|
||||
int cmTryCompileCommand::CoreTryCompileCode(
|
||||
cmMakefile *mf, std::vector<std::string> const& argv, bool clean)
|
||||
cmMakefile *mf, std::vector<std::string> const& argv, bool clean,
|
||||
const char* cmakeCommand, std::string& outputFile)
|
||||
{
|
||||
// which signature were we called with ?
|
||||
bool srcFileSignature = false;
|
||||
|
@ -257,11 +258,18 @@ int cmTryCompileCommand::CoreTryCompileCode(
|
|||
}
|
||||
|
||||
// if They specified clean then we clean up what we can
|
||||
if (srcFileSignature && clean)
|
||||
{
|
||||
if(!mf->GetCMakeInstance()->GetDebugTryCompile())
|
||||
{
|
||||
cmTryCompileCommand::CleanupFiles(binaryDirectory);
|
||||
if (srcFileSignature)
|
||||
{
|
||||
std::string errorMessage;
|
||||
outputFile = cmTryCompileCommand::GetOutputFile(mf, binaryDirectory,
|
||||
targetName, cmakeCommand,
|
||||
errorMessage);
|
||||
if (clean)
|
||||
{
|
||||
if(!mf->GetCMakeInstance()->GetDebugTryCompile())
|
||||
{
|
||||
cmTryCompileCommand::CleanupFiles(binaryDirectory);
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
|
@ -275,7 +283,9 @@ bool cmTryCompileCommand::InitialPass(std::vector<std::string> const& argv)
|
|||
return false;
|
||||
}
|
||||
|
||||
cmTryCompileCommand::CoreTryCompileCode(this->Makefile,argv,true);
|
||||
std::string dummy;
|
||||
cmTryCompileCommand::CoreTryCompileCode(this->Makefile,argv, true,
|
||||
this->GetName(), dummy);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -330,3 +340,62 @@ void cmTryCompileCommand::CleanupFiles(const char* binDir)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char* cmTryCompileCommand::GetOutputFile(cmMakefile* mf,
|
||||
const char* binaryDirectory,
|
||||
const char* targetName,
|
||||
const char* cmakeCommand,
|
||||
std::string& errorMessage)
|
||||
{
|
||||
errorMessage = "";
|
||||
std::string outputFile = "/";
|
||||
outputFile += targetName;
|
||||
outputFile += mf->GetSafeDefinition("CMAKE_EXECUTABLE_SUFFIX");
|
||||
|
||||
// a list of directories where to search for the compilation result
|
||||
// at first directly in the binary dir
|
||||
std::vector<std::string> searchDirs;
|
||||
searchDirs.push_back("");
|
||||
|
||||
const char* config = mf->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
|
||||
// if a config was specified try that first
|
||||
if (config && config[0])
|
||||
{
|
||||
std::string tmp = "/";
|
||||
tmp += config;
|
||||
searchDirs.push_back(tmp);
|
||||
}
|
||||
searchDirs.push_back("/Debug");
|
||||
searchDirs.push_back("/Development");
|
||||
|
||||
for(std::vector<std::string>::const_iterator it = searchDirs.begin();
|
||||
it != searchDirs.end();
|
||||
++it)
|
||||
{
|
||||
std::string command = binaryDirectory;
|
||||
command += *it;
|
||||
command += outputFile;
|
||||
if(cmSystemTools::FileExists(command.c_str()))
|
||||
{
|
||||
outputFile = cmSystemTools::CollapseFullPath(command.c_str());
|
||||
return outputFile.c_str();
|
||||
}
|
||||
}
|
||||
|
||||
cmOStringStream emsg;
|
||||
emsg << "Unable to find executable for " << cmakeCommand << ": tried \"";
|
||||
for (unsigned int i = 0; i < searchDirs.size(); ++i)
|
||||
{
|
||||
emsg << binaryDirectory << searchDirs[i] << outputFile;
|
||||
if (i < searchDirs.size() - 1)
|
||||
{
|
||||
emsg << "\" and \"";
|
||||
}
|
||||
else
|
||||
{
|
||||
emsg << "\".";
|
||||
}
|
||||
}
|
||||
errorMessage = emsg.str();
|
||||
return "";
|
||||
}
|
||||
|
|
|
@ -59,8 +59,11 @@ public:
|
|||
* commands, such as TryRun can access the same logic without
|
||||
* duplication.
|
||||
*/
|
||||
static int CoreTryCompileCode(
|
||||
cmMakefile *mf, std::vector<std::string> const& argv, bool clean);
|
||||
static int CoreTryCompileCode(cmMakefile *mf,
|
||||
std::vector<std::string> const& argv,
|
||||
bool clean,
|
||||
const char* cmakeCommand,
|
||||
std::string& outputFile);
|
||||
|
||||
/**
|
||||
* This deletes all the files created by TRY_COMPILE or TRY_RUN
|
||||
|
@ -68,7 +71,15 @@ public:
|
|||
* dependencies of makefiles.
|
||||
*/
|
||||
static void CleanupFiles(const char* binDir);
|
||||
|
||||
|
||||
/**
|
||||
* This tries to find the (executable) file created by TRY_COMPILE or
|
||||
* TRY_RUN. If nothing is found an empty string will be returned.
|
||||
*/
|
||||
static const char* GetOutputFile(cmMakefile* mf, const char* binaryDirectory,
|
||||
const char* targetName, const char* cmakeCommand,
|
||||
std::string& errorMessage);
|
||||
|
||||
/**
|
||||
* More documentation. */
|
||||
virtual const char* GetFullDocumentation()
|
||||
|
|
|
@ -71,140 +71,62 @@ bool cmTryRunCommand::InitialPass(std::vector<std::string> const& argv)
|
|||
}
|
||||
}
|
||||
// do the try compile
|
||||
int res = cmTryCompileCommand::CoreTryCompileCode(this->Makefile,
|
||||
tryCompile, false);
|
||||
|
||||
std::string fullPath;
|
||||
int res = cmTryCompileCommand::CoreTryCompileCode(this->Makefile, tryCompile,
|
||||
false, this->GetName(), fullPath);
|
||||
|
||||
// now try running the command if it compiled
|
||||
std::string binaryDirectory = argv[2];
|
||||
binaryDirectory += cmake::GetCMakeFilesDirectory();
|
||||
binaryDirectory += "/CMakeTmp";
|
||||
if (!res)
|
||||
if (!res==0)
|
||||
{
|
||||
if (fullPath.size() > 0)
|
||||
{
|
||||
int retVal = -1;
|
||||
std::string output;
|
||||
std::string executableSuffix=this->Makefile->GetDefinition(
|
||||
"CMAKE_EXECUTABLE_SUFFIX");
|
||||
std::string command1 = binaryDirectory;
|
||||
std::vector<std::string> attemptedPaths;
|
||||
command1 += "/cmTryCompileExec";
|
||||
command1 += executableSuffix;
|
||||
std::string fullPath;
|
||||
if(cmSystemTools::FileExists(command1.c_str()))
|
||||
std::string finalCommand = fullPath;
|
||||
finalCommand = cmSystemTools::ConvertToRunCommandPath(fullPath.c_str());
|
||||
if(runArgs.size())
|
||||
{
|
||||
fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
|
||||
finalCommand += runArgs;
|
||||
}
|
||||
attemptedPaths.push_back(command1);
|
||||
command1 = binaryDirectory;
|
||||
// try CMAKE_TRY_COMPILE_CONFIGURATION if it is set
|
||||
if (fullPath.empty())
|
||||
int timeout = 0;
|
||||
bool worked = cmSystemTools::RunSingleCommand(finalCommand.c_str(),
|
||||
&output, &retVal,
|
||||
0, false, timeout);
|
||||
if(outputVariable.size())
|
||||
{
|
||||
const char* config =
|
||||
this->Makefile->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
|
||||
// if a config was specified try that first
|
||||
if (config && config[0])
|
||||
// if the TryCompileCore saved output in this outputVariable then
|
||||
// prepend that output to this output
|
||||
const char* compileOutput
|
||||
= this->Makefile->GetDefinition(outputVariable.c_str());
|
||||
if(compileOutput)
|
||||
{
|
||||
command1 += "/";
|
||||
command1 += config;
|
||||
command1 += "/cmTryCompileExec";
|
||||
command1 += executableSuffix;
|
||||
if(cmSystemTools::FileExists(command1.c_str()))
|
||||
{
|
||||
fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
|
||||
}
|
||||
attemptedPaths.push_back(command1);
|
||||
output = std::string(compileOutput) + output;
|
||||
}
|
||||
this->Makefile->AddDefinition(outputVariable.c_str(), output.c_str());
|
||||
}
|
||||
// try Debug if still not found
|
||||
if (fullPath.empty())
|
||||
// set the run var
|
||||
char retChar[1000];
|
||||
if(worked)
|
||||
{
|
||||
command1 = binaryDirectory;
|
||||
command1 += "/Debug/cmTryCompileExec";
|
||||
command1 += executableSuffix;
|
||||
if(cmSystemTools::FileExists(command1.c_str()))
|
||||
{
|
||||
fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
|
||||
}
|
||||
attemptedPaths.push_back(command1);
|
||||
sprintf(retChar,"%i",retVal);
|
||||
}
|
||||
// try Deployment if still not found
|
||||
if (fullPath.empty())
|
||||
else
|
||||
{
|
||||
command1 = binaryDirectory;
|
||||
command1 += "/Development/cmTryCompileExec";
|
||||
command1 += executableSuffix;
|
||||
if(cmSystemTools::FileExists(command1.c_str()))
|
||||
{
|
||||
fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
|
||||
}
|
||||
attemptedPaths.push_back(command1);
|
||||
}
|
||||
if (fullPath.empty())
|
||||
{
|
||||
cmOStringStream emsg;
|
||||
emsg << "Unable to find executable for TRY_RUN: tried \"";
|
||||
for (i = 0; i < attemptedPaths.size(); ++i)
|
||||
{
|
||||
emsg << attemptedPaths[i];
|
||||
if (i < attemptedPaths.size() - 1)
|
||||
{
|
||||
emsg << "\" and \"";
|
||||
}
|
||||
else
|
||||
{
|
||||
emsg << "\".";
|
||||
}
|
||||
}
|
||||
cmSystemTools::Error(emsg.str().c_str());
|
||||
}
|
||||
if (fullPath.size() > 1)
|
||||
{
|
||||
std::string finalCommand = fullPath;
|
||||
finalCommand = cmSystemTools::ConvertToRunCommandPath(fullPath.c_str());
|
||||
if(runArgs.size())
|
||||
{
|
||||
finalCommand += runArgs;
|
||||
}
|
||||
int timeout = 0;
|
||||
bool worked = cmSystemTools::RunSingleCommand(finalCommand.c_str(),
|
||||
&output, &retVal,
|
||||
0, false, timeout);
|
||||
if(outputVariable.size())
|
||||
{
|
||||
// if the TryCompileCore saved output in this outputVariable then
|
||||
// prepend that output to this output
|
||||
const char* compileOutput
|
||||
= this->Makefile->GetDefinition(outputVariable.c_str());
|
||||
if(compileOutput)
|
||||
{
|
||||
output = std::string(compileOutput) + output;
|
||||
}
|
||||
this->Makefile->AddDefinition(outputVariable.c_str(), output.c_str());
|
||||
}
|
||||
// set the run var
|
||||
char retChar[1000];
|
||||
if(worked)
|
||||
{
|
||||
sprintf(retChar,"%i",retVal);
|
||||
}
|
||||
else
|
||||
{
|
||||
strcpy(retChar, "FAILED_TO_RUN");
|
||||
}
|
||||
this->Makefile->AddCacheDefinition(argv[0].c_str(), retChar,
|
||||
"Result of TRY_RUN",
|
||||
cmCacheManager::INTERNAL);
|
||||
strcpy(retChar, "FAILED_TO_RUN");
|
||||
}
|
||||
this->Makefile->AddCacheDefinition(argv[0].c_str(), retChar,
|
||||
"Result of TRY_RUN",
|
||||
cmCacheManager::INTERNAL);
|
||||
}
|
||||
|
||||
// if we created a directory etc, then cleanup after ourselves
|
||||
std::string cacheFile = binaryDirectory;
|
||||
cacheFile += "/CMakeLists.txt";
|
||||
}
|
||||
|
||||
// if we created a directory etc, then cleanup after ourselves
|
||||
if(!this->Makefile->GetCMakeInstance()->GetDebugTryCompile())
|
||||
{
|
||||
std::string binaryDirectory = argv[2];
|
||||
binaryDirectory += cmake::GetCMakeFilesDirectory();
|
||||
binaryDirectory += "/CMakeTmp";
|
||||
cmTryCompileCommand::CleanupFiles(binaryDirectory.c_str());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue