Merge topic 'try_compile-COPY_FILE_ERROR'

c28715b try_compile: Add COPY_FILE_ERROR option to capture failure
This commit is contained in:
Brad King 2013-07-15 09:33:32 -04:00 committed by CMake Topic Stage
commit f6024ef420
10 changed files with 78 additions and 5 deletions

View File

@ -33,18 +33,20 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv)
std::vector<std::string> compileDefs; std::vector<std::string> compileDefs;
std::string outputVariable; std::string outputVariable;
std::string copyFile; std::string copyFile;
std::string copyFileError;
std::vector<cmTarget*> targets; std::vector<cmTarget*> targets;
std::string libsToLink = " "; std::string libsToLink = " ";
bool useOldLinkLibs = true; bool useOldLinkLibs = true;
char targetNameBuf[64]; char targetNameBuf[64];
bool didOutputVariable = false; bool didOutputVariable = false;
bool didCopyFile = false; bool didCopyFile = false;
bool didCopyFileError = false;
bool useSources = argv[2] == "SOURCES"; bool useSources = argv[2] == "SOURCES";
std::vector<std::string> sources; std::vector<std::string> sources;
enum Doing { DoingNone, DoingCMakeFlags, DoingCompileDefinitions, enum Doing { DoingNone, DoingCMakeFlags, DoingCompileDefinitions,
DoingLinkLibraries, DoingOutputVariable, DoingCopyFile, DoingLinkLibraries, DoingOutputVariable, DoingCopyFile,
DoingSources }; DoingCopyFileError, DoingSources };
Doing doing = useSources? DoingSources : DoingNone; Doing doing = useSources? DoingSources : DoingNone;
for(size_t i=3; i < argv.size(); ++i) for(size_t i=3; i < argv.size(); ++i)
{ {
@ -75,6 +77,11 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv)
doing = DoingCopyFile; doing = DoingCopyFile;
didCopyFile = true; didCopyFile = true;
} }
else if(argv[i] == "COPY_FILE_ERROR")
{
doing = DoingCopyFileError;
didCopyFileError = true;
}
else if(doing == DoingCMakeFlags) else if(doing == DoingCMakeFlags)
{ {
cmakeFlags.push_back(argv[i]); cmakeFlags.push_back(argv[i]);
@ -122,6 +129,11 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv)
copyFile = argv[i].c_str(); copyFile = argv[i].c_str();
doing = DoingNone; doing = DoingNone;
} }
else if(doing == DoingCopyFileError)
{
copyFileError = argv[i].c_str();
doing = DoingNone;
}
else if(doing == DoingSources) else if(doing == DoingSources)
{ {
sources.push_back(argv[i]); sources.push_back(argv[i]);
@ -150,6 +162,20 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv)
return -1; return -1;
} }
if(didCopyFileError && copyFileError.empty())
{
this->Makefile->IssueMessage(cmake::FATAL_ERROR,
"COPY_FILE_ERROR must be followed by a variable name");
return -1;
}
if(didCopyFileError && !didCopyFile)
{
this->Makefile->IssueMessage(cmake::FATAL_ERROR,
"COPY_FILE_ERROR may be used only with COPY_FILE");
return -1;
}
if(didOutputVariable && outputVariable.empty()) if(didOutputVariable && outputVariable.empty())
{ {
this->Makefile->IssueMessage(cmake::FATAL_ERROR, this->Makefile->IssueMessage(cmake::FATAL_ERROR,
@ -444,6 +470,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv)
if (this->SrcFileSignature) if (this->SrcFileSignature)
{ {
std::string copyFileErrorMessage;
this->FindOutputFile(targetName); this->FindOutputFile(targetName);
if ((res==0) && (copyFile.size())) if ((res==0) && (copyFile.size()))
@ -461,9 +488,22 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv)
{ {
emsg << this->FindErrorMessage.c_str(); emsg << this->FindErrorMessage.c_str();
} }
if(copyFileError.empty())
{
this->Makefile->IssueMessage(cmake::FATAL_ERROR, emsg.str()); this->Makefile->IssueMessage(cmake::FATAL_ERROR, emsg.str());
return -1; return -1;
} }
else
{
copyFileErrorMessage = emsg.str();
}
}
}
if(!copyFileError.empty())
{
this->Makefile->AddDefinition(copyFileError.c_str(),
copyFileErrorMessage.c_str());
} }
} }
return res; return res;

View File

@ -69,14 +69,14 @@ public:
" [COMPILE_DEFINITIONS flags...]\n" " [COMPILE_DEFINITIONS flags...]\n"
" [LINK_LIBRARIES libs...]\n" " [LINK_LIBRARIES libs...]\n"
" [OUTPUT_VARIABLE <var>]\n" " [OUTPUT_VARIABLE <var>]\n"
" [COPY_FILE <fileName>])\n" " [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]])\n"
"Try building an executable from one or more source files. " "Try building an executable from one or more source files. "
"In this form the user need only supply one or more source files " "In this form the user need only supply one or more source files "
"that include a definition for 'main'. " "that include a definition for 'main'. "
"CMake will create a CMakeLists.txt file to build the source(s) " "CMake will create a CMakeLists.txt file to build the source(s) "
"as an executable. " "as an executable. "
"Specify COPY_FILE to get a copy of the linked executable at the " "Specify COPY_FILE to get a copy of the linked executable at the "
"given fileName." "given fileName and optionally COPY_FILE_ERROR to capture any error."
"\n" "\n"
"In this version all files in bindir/CMakeFiles/CMakeTmp " "In this version all files in bindir/CMakeFiles/CMakeTmp "
"will be cleaned automatically. For debugging, --debug-trycompile can " "will be cleaned automatically. For debugging, --debug-trycompile can "

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,4 @@
CMake Error at CopyFileErrorNoCopyFile.cmake:1 \(try_compile\):
COPY_FILE_ERROR may be used only with COPY_FILE
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1,2 @@
try_compile(RESULT ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/src.c
COPY_FILE_ERROR _copied)

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,4 @@
CMake Error at NoCopyFileError.cmake:1 \(try_compile\):
COPY_FILE_ERROR must be followed by a variable name
Call Stack \(most recent call first\):
CMakeLists.txt:3 \(include\)

View File

@ -0,0 +1,2 @@
try_compile(RESULT ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/src.c
COPY_FILE ${CMAKE_CURRENT_BINARY_DIR}/copied.bin COPY_FILE_ERROR)

View File

@ -1,10 +1,12 @@
include(RunCMake) include(RunCMake)
run_cmake(CopyFileErrorNoCopyFile)
run_cmake(NoArgs) run_cmake(NoArgs)
run_cmake(OneArg) run_cmake(OneArg)
run_cmake(TwoArgs) run_cmake(TwoArgs)
run_cmake(NoCopyFile) run_cmake(NoCopyFile)
run_cmake(NoCopyFile2) run_cmake(NoCopyFile2)
run_cmake(NoCopyFileError)
run_cmake(NoOutputVariable) run_cmake(NoOutputVariable)
run_cmake(NoOutputVariable2) run_cmake(NoOutputVariable2)
run_cmake(NoSources) run_cmake(NoSources)

View File

@ -44,6 +44,23 @@ else()
file(REMOVE "${TryCompile_BINARY_DIR}/CopyOfPass") file(REMOVE "${TryCompile_BINARY_DIR}/CopyOfPass")
endif() endif()
# try to compile a file that should compile
# also check that COPY_FILE_ERROR works
file(WRITE ${TryCompile_BINARY_DIR}/invalid "")
try_compile(SHOULD_PASS
${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp
${TryCompile_SOURCE_DIR}/pass.c
OUTPUT_VARIABLE TRY_OUT
COPY_FILE ${TryCompile_BINARY_DIR}/invalid/path
COPY_FILE_ERROR _captured
)
if(NOT SHOULD_PASS)
message(SEND_ERROR "should pass failed ${TRY_OUT}")
endif()
if(NOT _captured MATCHES "Cannot copy output executable.*/invalid/path")
message(SEND_ERROR "COPY_FILE_ERROR did not capture expected message")
endif()
# try to compile a file that should not compile # try to compile a file that should not compile
try_compile(SHOULD_FAIL try_compile(SHOULD_FAIL
${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp ${TryCompile_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp