CMake/Source/cmTryRunCommand.cxx

210 lines
6.2 KiB
C++
Raw Normal View History

2002-09-19 17:48:39 +04:00
/*=========================================================================
Program: CMake - Cross-Platform Makefile Generator
2002-09-19 17:48:39 +04:00
Module: $RCSfile$
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
2002-09-19 17:48:39 +04:00
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"
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 21:20:44 +04:00
// cmTryRunCommand
2002-09-19 17:48:39 +04:00
bool cmTryRunCommand::InitialPass(std::vector<std::string> const& argv)
{
if(argv.size() < 4)
{
return false;
}
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 21:20:44 +04:00
if (this->Makefile->IsOn("CMAKE_CROSSCOMPILING"))
{
this->SetError("doesn't work when crosscompiling.");
cmSystemTools::SetFatalErrorOccured();
return false;
}
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 outputVariable;
2002-09-19 17:48:39 +04:00
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
2002-09-19 17:48:39 +04:00
{
tryCompile.push_back(argv[i]);
if (argv[i] == "OUTPUT_VARIABLE")
{
if ( argv.size() <= (i+1) )
{
cmSystemTools::Error(
2006-03-10 21:54:57 +03:00
"OUTPUT_VARIABLE specified but there is no variable");
return false;
}
outputVariable = argv[i+1];
}
2002-09-19 17:48:39 +04:00
}
}
// do the try compile
2006-03-15 19:02:08 +03:00
int res = cmTryCompileCommand::CoreTryCompileCode(this->Makefile,
tryCompile, false);
2002-09-19 17:48:39 +04:00
// now try running the command if it compiled
std::string binaryDirectory = argv[2];
binaryDirectory += cmake::GetCMakeFilesDirectory();
binaryDirectory += "/CMakeTmp";
2002-09-19 17:48:39 +04:00
if (!res)
{
2005-02-10 07:00:16 +03:00
int retVal = -1;
2002-09-19 17:48:39 +04:00
std::string output;
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 21:20:44 +04:00
std::string executableSuffix=this->Makefile->GetDefinition("CMAKE_EXECUTABLE_SUFFIX");
std::string command1 = binaryDirectory;
std::vector<std::string> attemptedPaths;
command1 += "/cmTryCompileExec";
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 21:20:44 +04:00
command1 += executableSuffix;
2002-09-19 17:48:39 +04:00
std::string fullPath;
if(cmSystemTools::FileExists(command1.c_str()))
2002-09-19 17:48:39 +04:00
{
fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
2002-09-19 17:48:39 +04:00
}
attemptedPaths.push_back(command1);
command1 = binaryDirectory;
// try CMAKE_TRY_COMPILE_CONFIGURATION if it is set
if (fullPath.empty())
2002-09-19 17:48:39 +04:00
{
const char* config =
this->Makefile->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION");
// if a config was specified try that first
if (config && config[0])
2002-09-19 17:48:39 +04:00
{
command1 += "/";
command1 += config;
command1 += "/cmTryCompileExec";
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 21:20:44 +04:00
command1 += executableSuffix;
if(cmSystemTools::FileExists(command1.c_str()))
{
fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
}
attemptedPaths.push_back(command1);
2002-09-19 17:48:39 +04:00
}
}
// try Debug if still not found
if (fullPath.empty())
{
command1 = binaryDirectory;
command1 += "/Debug/cmTryCompileExec";
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 21:20:44 +04:00
command1 += executableSuffix;
if(cmSystemTools::FileExists(command1.c_str()))
{
fullPath = cmSystemTools::CollapseFullPath(command1.c_str());
}
attemptedPaths.push_back(command1);
}
// try Deployment if still not found
if (fullPath.empty())
{
command1 = binaryDirectory;
command1 += "/Development/cmTryCompileExec";
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 21:20:44 +04:00
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)
2002-09-20 00:07:23 +04:00
{
emsg << attemptedPaths[i];
if (i < attemptedPaths.size() - 1)
{
emsg << "\" and \"";
}
else
{
emsg << "\".";
}
2002-09-20 00:07:23 +04:00
}
cmSystemTools::Error(emsg.str().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;
finalCommand = cmSystemTools::ConvertToRunCommandPath(fullPath.c_str());
2002-09-19 17:48:39 +04:00
if(runArgs.size())
{
finalCommand += runArgs;
}
2003-08-04 06:33:35 +04:00
int timeout = 0;
2005-02-10 07:00:16 +03:00
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
2005-02-10 07:00:16 +03:00
const char* compileOutput
2006-03-15 19:02:08 +03:00
= this->Makefile->GetDefinition(outputVariable.c_str());
if(compileOutput)
{
output = std::string(compileOutput) + output;
}
2006-03-15 19:02:08 +03:00
this->Makefile->AddDefinition(outputVariable.c_str(), output.c_str());
}
2002-09-19 17:48:39 +04:00
// set the run var
char retChar[1000];
2005-02-10 07:00:16 +03:00
if(worked)
{
sprintf(retChar,"%i",retVal);
}
else
{
strcpy(retChar, "FAILED_TO_RUN");
}
2006-03-15 19:02:08 +03:00
this->Makefile->AddCacheDefinition(argv[0].c_str(), retChar,
2005-02-10 07:00:16 +03:00
"Result of TRY_RUN",
cmCacheManager::INTERNAL);
2002-09-19 17:48:39 +04:00
}
}
// if we created a directory etc, then cleanup after ourselves
2002-09-20 00:07:23 +04:00
std::string cacheFile = binaryDirectory;
cacheFile += "/CMakeLists.txt";
2006-03-15 19:02:08 +03:00
if(!this->Makefile->GetCMakeInstance()->GetDebugTryCompile())
2004-08-05 01:21:19 +04:00
{
cmTryCompileCommand::CleanupFiles(binaryDirectory.c_str());
}
2002-09-19 17:48:39 +04:00
return true;
}