diff options
author | Stephen Kelly <steveire@gmail.com> | 2013-02-09 09:34:37 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2013-02-22 13:35:39 (GMT) |
commit | 236133e79e2d047810eeba90915e38d9861e2a22 (patch) | |
tree | 25265f06106cbc0403c53f6705e14baac002687c /Source | |
parent | 1c0597c25bf69a9c73e3d4f6ab68d16b5e56a271 (diff) | |
download | CMake-236133e79e2d047810eeba90915e38d9861e2a22.zip CMake-236133e79e2d047810eeba90915e38d9861e2a22.tar.gz CMake-236133e79e2d047810eeba90915e38d9861e2a22.tar.bz2 |
Handle targets in the LINK_LIBRARIES of try_compile.
Imported targets are re-exported so that they can be used by the
try_compile generated code with target_link_libraries.
This makes the use of the cmake_expand_imported_targets macro
obsolete. The macro is not able to expand the generator expressions
which may appear in the IMPORTED_LINK_INTERFACE_LIBRARIES content.
Instead it just sees them as 'not a target'.
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmCoreTryCompile.cxx | 108 | ||||
-rw-r--r-- | Source/cmTryCompileCommand.h | 8 |
2 files changed, 106 insertions, 10 deletions
diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index 1ae7035..387f6ed 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -13,8 +13,11 @@ #include "cmake.h" #include "cmCacheManager.h" #include "cmGlobalGenerator.h" +#include "cmExportTryCompileFileGenerator.h" #include <cmsys/Directory.hxx> +#include <assert.h> + int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv) { this->BinaryDirectory = argv[1].c_str(); @@ -39,7 +42,8 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv) // is not used, so it matches regular command line parsing which has // the program name as arg 0 for (; i < argv.size() && argv[i] != "COMPILE_DEFINITIONS" && - argv[i] != "OUTPUT_VARIABLE"; + argv[i] != "OUTPUT_VARIABLE" && + argv[i] != "LINK_LIBRARIES"; ++i) { extraArgs++; @@ -75,7 +79,8 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv) { extraArgs++; for (i = i + 1; i < argv.size() && argv[i] != "CMAKE_FLAGS" && - argv[i] != "OUTPUT_VARIABLE"; + argv[i] != "OUTPUT_VARIABLE" && + argv[i] != "LINK_LIBRARIES"; ++i) { extraArgs++; @@ -85,6 +90,61 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv) } } + std::vector<cmTarget*> targets; + std::string libsToLink = " "; + bool useOldLinkLibs = true; + for (i = 3; i < argv.size(); ++i) + { + if (argv[i] == "LINK_LIBRARIES") + { + if ( argv.size() <= (i+1) ) + { + this->Makefile->IssueMessage(cmake::FATAL_ERROR, + "LINK_LIBRARIES specified but there is no content"); + return -1; + } + extraArgs++; + ++i; + useOldLinkLibs = false; + for ( ; i < argv.size() && argv[i] != "CMAKE_FLAGS" + && argv[i] != "COMPILE_DEFINITIONS" && argv[i] != "OUTPUT_VARIABLE"; + ++i) + { + extraArgs++; + libsToLink += argv[i] + " "; + cmTarget *tgt = this->Makefile->FindTargetToUse(argv[i].c_str()); + if (!tgt) + { + continue; + } + switch(tgt->GetType()) + { + case cmTarget::SHARED_LIBRARY: + case cmTarget::STATIC_LIBRARY: + case cmTarget::UNKNOWN_LIBRARY: + break; + case cmTarget::EXECUTABLE: + if (tgt->IsExecutableWithExports()) + { + break; + } + default: + this->Makefile->IssueMessage(cmake::FATAL_ERROR, + "Only libraries may be used as try_compile IMPORTED " + "LINK_LIBRARIES. Got " + std::string(tgt->GetName()) + " of " + "type " + tgt->GetTargetTypeName(tgt->GetType()) + "."); + return -1; + } + if (!tgt->IsImported()) + { + continue; + } + targets.push_back(tgt); + } + break; + } + } + // look for COPY_FILE std::string copyFile; for (i = 3; i < argv.size(); ++i) @@ -247,6 +307,32 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv) fprintf(fout, ")\n"); } + /* Use a random file name to avoid rapid creation and deletion + of the same executable name (some filesystems fail on that). */ + sprintf(targetNameBuf, "cmTryCompileExec%u", + cmSystemTools::RandomSeed()); + targetName = targetNameBuf; + + if (!targets.empty()) + { + std::string fname = "/" + std::string(targetName) + "Targets.cmake"; + cmExportTryCompileFileGenerator tcfg; + tcfg.SetExportFile((this->BinaryDirectory + fname).c_str()); + tcfg.SetExports(targets); + tcfg.SetConfig(this->Makefile->GetDefinition( + "CMAKE_TRY_COMPILE_CONFIGURATION")); + + if(!tcfg.GenerateImportFile()) + { + this->Makefile->IssueMessage(cmake::FATAL_ERROR, + "could not write export file."); + return -1; + } + fprintf(fout, + "\ninclude(\"${CMAKE_CURRENT_LIST_DIR}/%s\")\n\n", + fname.c_str()); + } + /* for the TRY_COMPILEs we want to be able to specify the architecture. So the user can set CMAKE_OSX_ARCHITECTURE to i386;ppc and then set CMAKE_TRY_COMPILE_OSX_ARCHITECTURE first to i386 and then to ppc to @@ -286,18 +372,22 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv) fprintf(fout, "SET(CMAKE_POSITION_INDEPENDENT_CODE \"ON\")\n"); } - /* Use a random file name to avoid rapid creation and deletion - of the same executable name (some filesystems fail on that). */ - sprintf(targetNameBuf, "cmTryCompileExec%u", - cmSystemTools::RandomSeed()); - targetName = targetNameBuf; - /* Put the executable at a known location (for COPY_FILE). */ fprintf(fout, "SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY \"%s\")\n", this->BinaryDirectory.c_str()); /* Create the actual executable. */ fprintf(fout, "ADD_EXECUTABLE(%s \"%s\")\n", targetName, source.c_str()); - fprintf(fout, "TARGET_LINK_LIBRARIES(%s ${LINK_LIBRARIES})\n",targetName); + if (useOldLinkLibs) + { + fprintf(fout, + "TARGET_LINK_LIBRARIES(%s ${LINK_LIBRARIES})\n",targetName); + } + else + { + fprintf(fout, "TARGET_LINK_LIBRARIES(%s %s)\n", + targetName, + libsToLink.c_str()); + } fclose(fout); projectName = "CMAKE_TRY_COMPILE"; // if the source is not in CMakeTmp diff --git a/Source/cmTryCompileCommand.h b/Source/cmTryCompileCommand.h index 68ec666..6caa130 100644 --- a/Source/cmTryCompileCommand.h +++ b/Source/cmTryCompileCommand.h @@ -67,6 +67,7 @@ public: " try_compile(RESULT_VAR <bindir> <srcfile>\n" " [CMAKE_FLAGS flags...]\n" " [COMPILE_DEFINITIONS flags...]\n" + " [LINK_LIBRARIES libs...]\n" " [OUTPUT_VARIABLE <var>]\n" " [COPY_FILE <fileName>])\n" "Try building a source file into an executable. " @@ -90,7 +91,12 @@ public: "Some extra flags that can be included are, " "INCLUDE_DIRECTORIES, LINK_DIRECTORIES, and LINK_LIBRARIES. " "COMPILE_DEFINITIONS are -Ddefinition that will be passed to the " - "compile line. " + "compile line.\n" + "The srcfile signature also accepts a LINK_LIBRARIES argument which " + "may contain a list of libraries or IMPORTED targets which will be " + "linked to in the generated project. If LINK_LIBRARIES is specified " + "as a parameter to try_compile, then any LINK_LIBRARIES passed as " + "CMAKE_FLAGS will be ignored.\n" "try_compile creates a CMakeList.txt " "file on the fly that looks like this:\n" " add_definitions( <expanded COMPILE_DEFINITIONS from calling " |