2009-09-28 15:43:28 +00:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2002-09-19 13:48:39 +00:00
|
|
|
|
2009-09-28 15:43:28 +00:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2002-09-19 13:48:39 +00:00
|
|
|
|
2009-09-28 15:43:28 +00:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2002-09-19 13:48:39 +00:00
|
|
|
#include "cmTryRunCommand.h"
|
|
|
|
#include "cmCacheManager.h"
|
|
|
|
#include "cmTryCompileCommand.h"
|
|
|
|
|
ENH: merge CMake-CrossCompileBasic to HEAD
-add a RESULT_VARIABLE to INCLUDE()
-add CMAKE_TOOLCHAIN_FILE for specifiying your (potentially crosscompiling) toolchain
-have TRY_RUN() complain if you try to use it in crosscompiling mode (which were compiled but cannot run on this system)
-use CMAKE_EXECUTABLE_SUFFIX in TRY_RUN(), probably TRY_RUN won't be able to
run the executables if they have a different suffix because they are
probably crosscompiled, but nevertheless it should be able to find them
-make several cmake variables presettable by the user: CMAKE_C/CXX_COMPILER, CMAKE_C/CXX_OUTPUT_EXTENSION, CMAKE_SYSTEM_NAME, CMAKE_SYSTEM_INFO_FILE
-support prefix for GNU toolchains (arm-elf-gcc, arm-elf-ar, arm-elf-strip etc.)
-move ranlib on OSX from the file command to a command in executed in cmake_install.cmake
-add support for stripping during install in cmake_install.cmake
-split out cl.cmake from Windows-cl.cmake, first (very incomplete) step to support MS crosscompiling tools
-remove stdio.h from the simple C program which checks if the compiler works, since this may not exist for some embedded platforms
-create a new CMakeFindBinUtils.cmake which collects the search fro ar, ranlib, strip, ld, link, install_name_tool and other tools like these
-add support for CMAKE_FIND_ROOT_PATH for all FIND_XXX commands, which is a
list of directories which will be prepended to all search directories, right
now as a cmake variable, turning it into a global cmake property may need
some more work
-remove cmTestTestHandler::TryExecutable(), it's unused
-split cmFileCommand::HandleInstall() into slightly smaller functions
Alex
2007-05-17 17:20:44 +00:00
|
|
|
// cmTryRunCommand
|
2008-01-23 15:28:26 +00:00
|
|
|
bool cmTryRunCommand
|
|
|
|
::InitialPass(std::vector<std::string> const& argv, cmExecutionStatus &)
|
2002-09-19 13:48:39 +00:00
|
|
|
{
|
|
|
|
if(argv.size() < 4)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-01-23 15:28:26 +00:00
|
|
|
// build an arg list for TryCompile and extract the runArgs,
|
2002-09-19 13:48:39 +00:00
|
|
|
std::vector<std::string> tryCompile;
|
2007-06-01 15:16:29 +00:00
|
|
|
|
|
|
|
this->CompileResultVariable = "";
|
|
|
|
this->RunResultVariable = "";
|
|
|
|
this->OutputVariable = "";
|
|
|
|
this->RunOutputVariable = "";
|
|
|
|
this->CompileOutputVariable = "";
|
|
|
|
|
2002-09-19 13:48:39 +00:00
|
|
|
std::string runArgs;
|
2002-09-20 12:09:03 +00:00
|
|
|
unsigned int i;
|
2002-09-19 13:48:39 +00: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]);
|
|
|
|
}
|
2004-09-22 18:42:05 +00:00
|
|
|
}
|
|
|
|
else
|
2002-09-19 13:48:39 +00:00
|
|
|
{
|
2004-09-22 18:42:05 +00:00
|
|
|
if (argv[i] == "OUTPUT_VARIABLE")
|
2007-06-01 15:16:29 +00:00
|
|
|
{
|
2004-09-22 18:42:05 +00:00
|
|
|
if ( argv.size() <= (i+1) )
|
|
|
|
{
|
|
|
|
cmSystemTools::Error(
|
2006-03-10 18:54:57 +00:00
|
|
|
"OUTPUT_VARIABLE specified but there is no variable");
|
2004-09-22 18:42:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-06-01 15:16:29 +00:00
|
|
|
i++;
|
|
|
|
this->OutputVariable = argv[i];
|
|
|
|
}
|
|
|
|
else if (argv[i] == "RUN_OUTPUT_VARIABLE")
|
|
|
|
{
|
|
|
|
if (argv.size() <= (i + 1))
|
|
|
|
{
|
|
|
|
cmSystemTools::Error(
|
|
|
|
"RUN_OUTPUT_VARIABLE specified but there is no variable");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
this->RunOutputVariable = argv[i];
|
|
|
|
}
|
|
|
|
else if (argv[i] == "COMPILE_OUTPUT_VARIABLE")
|
|
|
|
{
|
|
|
|
if (argv.size() <= (i + 1))
|
|
|
|
{
|
|
|
|
cmSystemTools::Error(
|
|
|
|
"COMPILE_OUTPUT_VARIABLE specified but there is no variable");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
this->CompileOutputVariable = argv[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tryCompile.push_back(argv[i]);
|
2004-09-22 18:42:05 +00:00
|
|
|
}
|
2002-09-19 13:48:39 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-01 15:16:29 +00:00
|
|
|
|
|
|
|
// although they could be used together, don't allow it, because
|
|
|
|
// using OUTPUT_VARIABLE makes crosscompiling harder
|
|
|
|
if (this->OutputVariable.size()
|
|
|
|
&& ((this->RunOutputVariable.size())
|
|
|
|
|| (this->CompileOutputVariable.size())))
|
|
|
|
{
|
|
|
|
cmSystemTools::Error(
|
|
|
|
"You cannot use OUTPUT_VARIABLE together with COMPILE_OUTPUT_VARIABLE "
|
|
|
|
"or RUN_OUTPUT_VARIABLE. Please use only COMPILE_OUTPUT_VARIABLE and/or "
|
|
|
|
"RUN_OUTPUT_VARIABLE.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool captureRunOutput = false;
|
|
|
|
if (this->OutputVariable.size())
|
|
|
|
{
|
|
|
|
captureRunOutput = true;
|
|
|
|
tryCompile.push_back("OUTPUT_VARIABLE");
|
|
|
|
tryCompile.push_back(this->OutputVariable);
|
|
|
|
}
|
|
|
|
if (this->CompileOutputVariable.size())
|
|
|
|
{
|
|
|
|
tryCompile.push_back("OUTPUT_VARIABLE");
|
|
|
|
tryCompile.push_back(this->CompileOutputVariable);
|
|
|
|
}
|
|
|
|
if (this->RunOutputVariable.size())
|
|
|
|
{
|
|
|
|
captureRunOutput = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->RunResultVariable = argv[0];
|
|
|
|
this->CompileResultVariable = argv[1];
|
|
|
|
|
2002-09-19 13:48:39 +00:00
|
|
|
// do the try compile
|
2007-05-24 15:27:51 +00:00
|
|
|
int res = this->TryCompileCode(tryCompile);
|
2007-05-24 12:56:14 +00:00
|
|
|
|
2002-09-19 13:48:39 +00:00
|
|
|
// now try running the command if it compiled
|
2007-05-24 15:27:51 +00:00
|
|
|
if (!res)
|
2002-09-19 13:48:39 +00:00
|
|
|
{
|
2007-05-24 15:27:51 +00:00
|
|
|
if (this->OutputFile.size() == 0)
|
2002-09-19 13:48:39 +00:00
|
|
|
{
|
2007-05-24 15:27:51 +00:00
|
|
|
cmSystemTools::Error(this->FindErrorMessage.c_str());
|
2007-05-03 19:25:41 +00:00
|
|
|
}
|
2007-05-24 15:27:51 +00:00
|
|
|
else
|
2007-05-03 19:25:41 +00:00
|
|
|
{
|
2007-06-01 15:16:29 +00:00
|
|
|
// "run" it and capture the output
|
|
|
|
std::string runOutputContents;
|
|
|
|
if (this->Makefile->IsOn("CMAKE_CROSSCOMPILING"))
|
|
|
|
{
|
|
|
|
this->DoNotRunExecutable(runArgs,
|
|
|
|
argv[3],
|
|
|
|
captureRunOutput ? &runOutputContents : 0);
|
|
|
|
}
|
|
|
|
else
|
2007-05-03 19:25:41 +00:00
|
|
|
{
|
2007-06-01 15:16:29 +00:00
|
|
|
this->RunExecutable(runArgs, &runOutputContents);
|
2007-05-03 19:25:41 +00:00
|
|
|
}
|
2007-06-01 15:16:29 +00:00
|
|
|
|
|
|
|
// now put the output into the variables
|
|
|
|
if(this->RunOutputVariable.size())
|
|
|
|
{
|
|
|
|
this->Makefile->AddDefinition(this->RunOutputVariable.c_str(),
|
|
|
|
runOutputContents.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this->OutputVariable.size())
|
2007-05-24 15:27:51 +00:00
|
|
|
{
|
|
|
|
// if the TryCompileCore saved output in this outputVariable then
|
|
|
|
// prepend that output to this output
|
|
|
|
const char* compileOutput
|
2007-06-01 15:16:29 +00:00
|
|
|
= this->Makefile->GetDefinition(this->OutputVariable.c_str());
|
|
|
|
if (compileOutput)
|
2007-05-24 15:27:51 +00:00
|
|
|
{
|
2007-06-01 15:16:29 +00:00
|
|
|
runOutputContents = std::string(compileOutput) + runOutputContents;
|
2007-05-24 15:27:51 +00:00
|
|
|
}
|
2007-06-01 15:16:29 +00:00
|
|
|
this->Makefile->AddDefinition(this->OutputVariable.c_str(),
|
|
|
|
runOutputContents.c_str());
|
2007-05-24 15:27:51 +00:00
|
|
|
}
|
2002-09-19 13:48:39 +00:00
|
|
|
}
|
2007-06-01 15:16:29 +00:00
|
|
|
}
|
|
|
|
|
2007-05-24 12:56:14 +00:00
|
|
|
// if we created a directory etc, then cleanup after ourselves
|
2006-03-15 16:02:08 +00:00
|
|
|
if(!this->Makefile->GetCMakeInstance()->GetDebugTryCompile())
|
2004-08-04 21:21:19 +00:00
|
|
|
{
|
2007-05-24 15:27:51 +00:00
|
|
|
this->CleanupFiles(this->BinaryDirectory.c_str());
|
2004-08-04 21:21:19 +00:00
|
|
|
}
|
2002-09-19 13:48:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-06-01 15:16:29 +00:00
|
|
|
|
|
|
|
void cmTryRunCommand::RunExecutable(const std::string& runArgs,
|
|
|
|
std::string* out)
|
|
|
|
{
|
|
|
|
int retVal = -1;
|
|
|
|
std::string finalCommand = cmSystemTools::ConvertToRunCommandPath(
|
|
|
|
this->OutputFile.c_str());
|
|
|
|
if (runArgs.size())
|
|
|
|
{
|
|
|
|
finalCommand += runArgs;
|
|
|
|
}
|
|
|
|
int timeout = 0;
|
|
|
|
bool worked = cmSystemTools::RunSingleCommand(finalCommand.c_str(),
|
|
|
|
out, &retVal,
|
|
|
|
0, false, timeout);
|
|
|
|
// set the run var
|
|
|
|
char retChar[1000];
|
|
|
|
if (worked)
|
|
|
|
{
|
|
|
|
sprintf(retChar, "%i", retVal);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy(retChar, "FAILED_TO_RUN");
|
|
|
|
}
|
|
|
|
this->Makefile->AddCacheDefinition(this->RunResultVariable.c_str(), retChar,
|
|
|
|
"Result of TRY_RUN",
|
|
|
|
cmCacheManager::INTERNAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is only used when cross compiling. Instead of running the
|
|
|
|
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
|
|
|
|
)
|
|
|
|
{
|
2007-07-31 18:52:01 +00:00
|
|
|
// 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
|
|
|
|
// on the target platform.
|
|
|
|
std::string copyDest = this->Makefile->GetHomeOutputDirectory();
|
2007-09-17 14:53:20 +00:00
|
|
|
copyDest += cmake::GetCMakeFilesDirectory();
|
2007-07-31 18:52:01 +00:00
|
|
|
copyDest += "/";
|
|
|
|
copyDest += cmSystemTools::GetFilenameWithoutExtension(
|
|
|
|
this->OutputFile.c_str());
|
|
|
|
copyDest += "-";
|
|
|
|
copyDest += this->RunResultVariable;
|
|
|
|
copyDest += cmSystemTools::GetFilenameExtension(this->OutputFile.c_str());
|
|
|
|
cmSystemTools::CopyFileAlways(this->OutputFile.c_str(), copyDest.c_str());
|
|
|
|
|
|
|
|
std::string resultFileName = this->Makefile->GetHomeOutputDirectory();
|
|
|
|
resultFileName += "/TryRunResults.cmake";
|
|
|
|
|
|
|
|
std::string detailsString = "For details see ";
|
|
|
|
detailsString += resultFileName;
|
2007-06-01 15:16:29 +00:00
|
|
|
|
2007-08-01 15:59:51 +00:00
|
|
|
std::string internalRunOutputName=this->RunResultVariable+"__TRYRUN_OUTPUT";
|
2007-06-01 15:16:29 +00:00
|
|
|
bool error = false;
|
|
|
|
|
|
|
|
if (this->Makefile->GetDefinition(this->RunResultVariable.c_str()) == 0)
|
|
|
|
{
|
|
|
|
// if the variables doesn't exist, create it with a helpful error text
|
|
|
|
// and mark it as advanced
|
|
|
|
std::string comment;
|
2007-07-31 18:52:01 +00:00
|
|
|
comment += "Run result of TRY_RUN(), indicates whether the executable "
|
|
|
|
"would have been able to run on its target platform.\n";
|
|
|
|
comment += detailsString;
|
2007-06-01 15:16:29 +00:00
|
|
|
this->Makefile->AddCacheDefinition(this->RunResultVariable.c_str(),
|
|
|
|
"PLEASE_FILL_OUT-FAILED_TO_RUN",
|
|
|
|
comment.c_str(),
|
|
|
|
cmCacheManager::STRING);
|
|
|
|
|
|
|
|
cmCacheManager::CacheIterator it = this->Makefile->GetCacheManager()->
|
|
|
|
GetCacheIterator(this->RunResultVariable.c_str());
|
|
|
|
if ( !it.IsAtEnd() )
|
|
|
|
{
|
|
|
|
it.SetProperty("ADVANCED", "1");
|
|
|
|
}
|
|
|
|
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
|
2007-07-23 14:47:23 +00:00
|
|
|
// is the output from the executable used ?
|
2007-06-01 15:16:29 +00:00
|
|
|
if (out!=0)
|
|
|
|
{
|
|
|
|
if (this->Makefile->GetDefinition(internalRunOutputName.c_str()) == 0)
|
|
|
|
{
|
|
|
|
// if the variables doesn't exist, create it with a helpful error text
|
|
|
|
// and mark it as advanced
|
|
|
|
std::string comment;
|
2007-07-31 18:52:01 +00:00
|
|
|
comment+="Output of TRY_RUN(), contains the text, which the executable "
|
|
|
|
"would have printed on stdout and stderr on its target platform.\n";
|
|
|
|
comment += detailsString;
|
2007-06-01 15:16:29 +00:00
|
|
|
|
|
|
|
this->Makefile->AddCacheDefinition(internalRunOutputName.c_str(),
|
|
|
|
"PLEASE_FILL_OUT-NOTFOUND",
|
|
|
|
comment.c_str(),
|
|
|
|
cmCacheManager::STRING);
|
|
|
|
cmCacheManager::CacheIterator it = this->Makefile->GetCacheManager()->
|
|
|
|
GetCacheIterator(internalRunOutputName.c_str());
|
|
|
|
if ( !it.IsAtEnd() )
|
|
|
|
{
|
|
|
|
it.SetProperty("ADVANCED", "1");
|
|
|
|
}
|
|
|
|
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
2007-07-23 17:13:29 +00:00
|
|
|
static bool firstTryRun = true;
|
2007-07-31 18:52:01 +00:00
|
|
|
std::ofstream file(resultFileName.c_str(),
|
2007-07-26 12:40:51 +00:00
|
|
|
firstTryRun ? std::ios::out : std::ios::app);
|
2007-07-23 14:47:23 +00:00
|
|
|
if ( file )
|
|
|
|
{
|
2007-07-23 17:13:29 +00:00
|
|
|
if (firstTryRun)
|
|
|
|
{
|
|
|
|
file << "# This file was generated by CMake because it detected "
|
|
|
|
"TRY_RUN() commands\n"
|
|
|
|
"# in crosscompiling mode. It will be overwritten by the next "
|
|
|
|
"CMake run.\n"
|
|
|
|
"# Copy it to a safe location, set the variables to "
|
|
|
|
"appropriate values\n"
|
|
|
|
"# and use it then to preset the CMake cache (using -C).\n\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string comment ="\n";
|
|
|
|
comment += this->RunResultVariable;
|
2007-08-01 15:59:51 +00:00
|
|
|
comment += "\n indicates whether the executable would have been able "
|
2007-08-01 21:10:22 +00:00
|
|
|
"to run on its\n"
|
|
|
|
" target platform. If so, set ";
|
2007-07-31 18:52:01 +00:00
|
|
|
comment += this->RunResultVariable;
|
|
|
|
comment += " to\n"
|
2007-08-01 15:59:51 +00:00
|
|
|
" the exit code (in many cases 0 for success), otherwise "
|
2007-07-31 18:52:01 +00:00
|
|
|
"enter \"FAILED_TO_RUN\".\n";
|
2007-07-23 17:13:29 +00:00
|
|
|
if (out!=0)
|
|
|
|
{
|
|
|
|
comment += internalRunOutputName;
|
2007-08-01 15:59:51 +00:00
|
|
|
comment += "\n contains the text the executable "
|
2007-07-31 18:52:01 +00:00
|
|
|
"would have printed on stdout and stderr.\n"
|
2007-08-01 15:59:51 +00:00
|
|
|
" If the executable would not have been able to run, set ";
|
2007-07-31 18:52:01 +00:00
|
|
|
comment += internalRunOutputName;
|
|
|
|
comment += " empty.\n"
|
2007-08-01 15:59:51 +00:00
|
|
|
" Otherwise check if the output is evaluated by the "
|
2007-07-31 18:52:01 +00:00
|
|
|
"calling CMake code. If so,\n"
|
2007-08-01 15:59:51 +00:00
|
|
|
" check what the source file would have printed when "
|
|
|
|
"called with the given arguments.\n";
|
2007-07-23 17:13:29 +00:00
|
|
|
}
|
2007-07-31 18:52:01 +00:00
|
|
|
comment += "The ";
|
|
|
|
comment += this->CompileResultVariable;
|
|
|
|
comment += " variable holds the build result for this TRY_RUN().\n\n"
|
|
|
|
"Source file : ";
|
|
|
|
comment += srcFile + "\n";
|
|
|
|
comment += "Executable : ";
|
|
|
|
comment += copyDest + "\n";
|
|
|
|
comment += "Run arguments : ";
|
|
|
|
comment += runArgs;
|
|
|
|
comment += "\n";
|
|
|
|
comment += " Called from: " + this->Makefile->GetListFileStack();
|
2007-07-23 17:13:29 +00:00
|
|
|
cmsys::SystemTools::ReplaceString(comment, "\n", "\n# ");
|
|
|
|
file << comment << "\n\n";
|
|
|
|
|
|
|
|
file << "SET( " << this->RunResultVariable << " \n \""
|
2007-07-23 14:47:23 +00:00
|
|
|
<< this->Makefile->GetDefinition(this->RunResultVariable.c_str())
|
2007-07-30 18:46:57 +00:00
|
|
|
<< "\"\n CACHE STRING \"Result from TRY_RUN\" FORCE)\n\n";
|
2007-07-23 17:13:29 +00:00
|
|
|
|
2007-07-23 14:47:23 +00:00
|
|
|
if (out!=0)
|
|
|
|
{
|
2007-07-23 17:13:29 +00:00
|
|
|
file << "SET( " << internalRunOutputName << " \n \""
|
|
|
|
<< this->Makefile->GetDefinition(internalRunOutputName.c_str())
|
2007-07-30 18:46:57 +00:00
|
|
|
<< "\"\n CACHE STRING \"Output from TRY_RUN\" FORCE)\n\n";
|
2007-07-23 14:47:23 +00:00
|
|
|
}
|
|
|
|
file.close();
|
|
|
|
}
|
2007-07-23 17:13:29 +00:00
|
|
|
firstTryRun = false;
|
2007-07-23 14:47:23 +00:00
|
|
|
|
2007-06-01 15:16:29 +00:00
|
|
|
std::string errorMessage = "TRY_RUN() invoked in cross-compiling mode, "
|
|
|
|
"please set the following cache variables "
|
2010-07-02 14:58:00 +00:00
|
|
|
"appropriately:\n";
|
2007-06-01 15:16:29 +00:00
|
|
|
errorMessage += " " + this->RunResultVariable + " (advanced)\n";
|
|
|
|
if (out!=0)
|
|
|
|
{
|
|
|
|
errorMessage += " " + internalRunOutputName + " (advanced)\n";
|
|
|
|
}
|
2007-07-31 18:52:01 +00:00
|
|
|
errorMessage += detailsString;
|
2007-06-01 15:16:29 +00:00
|
|
|
cmSystemTools::Error(errorMessage.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out!=0)
|
|
|
|
{
|
|
|
|
(*out) = this->Makefile->GetDefinition(internalRunOutputName.c_str());
|
|
|
|
}
|
|
|
|
}
|