123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692 |
- /*============================================================================
- CMake - Cross Platform Makefile Generator
- Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
- Distributed under the OSI-approved BSD License (the "License");
- see accompanying file Copyright.txt for details.
- 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.
- ============================================================================*/
- #include "cmExportFileGenerator.h"
- #include "cmExportSet.h"
- #include "cmGeneratedFileStream.h"
- #include "cmGlobalGenerator.h"
- #include "cmInstallExportGenerator.h"
- #include "cmLocalGenerator.h"
- #include "cmMakefile.h"
- #include "cmSystemTools.h"
- #include "cmTarget.h"
- #include "cmTargetExport.h"
- #include "cmVersion.h"
- #include <cmsys/auto_ptr.hxx>
- //----------------------------------------------------------------------------
- cmExportFileGenerator::cmExportFileGenerator()
- {
- this->AppendMode = false;
- }
- //----------------------------------------------------------------------------
- void cmExportFileGenerator::AddConfiguration(const char* config)
- {
- this->Configurations.push_back(config);
- }
- //----------------------------------------------------------------------------
- void cmExportFileGenerator::SetExportFile(const char* mainFile)
- {
- this->MainImportFile = mainFile;
- this->FileDir =
- cmSystemTools::GetFilenamePath(this->MainImportFile);
- this->FileBase =
- cmSystemTools::GetFilenameWithoutLastExtension(this->MainImportFile);
- this->FileExt =
- cmSystemTools::GetFilenameLastExtension(this->MainImportFile);
- }
- //----------------------------------------------------------------------------
- bool cmExportFileGenerator::GenerateImportFile()
- {
- // Open the output file to generate it.
- cmsys::auto_ptr<std::ofstream> foutPtr;
- if(this->AppendMode)
- {
- // Open for append.
- cmsys::auto_ptr<std::ofstream>
- ap(new std::ofstream(this->MainImportFile.c_str(), std::ios::app));
- foutPtr = ap;
- }
- else
- {
- // Generate atomically and with copy-if-different.
- cmsys::auto_ptr<cmGeneratedFileStream>
- ap(new cmGeneratedFileStream(this->MainImportFile.c_str(), true));
- ap->SetCopyIfDifferent(true);
- foutPtr = ap;
- }
- if(!foutPtr.get() || !*foutPtr)
- {
- std::string se = cmSystemTools::GetLastSystemError();
- cmOStringStream e;
- e << "cannot write to file \"" << this->MainImportFile
- << "\": " << se;
- cmSystemTools::Error(e.str().c_str());
- return false;
- }
- std::ostream& os = *foutPtr;
- // Protect that file against use with older CMake versions.
- os << "# Generated by CMake " << cmVersion::GetCMakeVersion() << "\n\n";
- os << "IF(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.5)\n"
- << " MESSAGE(FATAL_ERROR \"CMake >= 2.6.0 required\")\n"
- << "ENDIF(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.5)\n";
- // Isolate the file policy level.
- // We use 2.6 here instead of the current version because newer
- // versions of CMake should be able to export files imported by 2.6
- // until the import format changes.
- os << "CMAKE_POLICY(PUSH)\n"
- << "CMAKE_POLICY(VERSION 2.6)\n";
- // Start with the import file header.
- this->GenerateImportHeaderCode(os);
- // Create all the imported targets.
- bool result = this->GenerateMainFile(os);
- // End with the import file footer.
- this->GenerateImportFooterCode(os);
- os << "CMAKE_POLICY(POP)\n";
- return result;
- }
- //----------------------------------------------------------------------------
- void cmExportFileGenerator::GenerateImportConfig(std::ostream& os,
- const char* config)
- {
- // Construct the property configuration suffix.
- std::string suffix = "_";
- if(config && *config)
- {
- suffix += cmSystemTools::UpperCase(config);
- }
- else
- {
- suffix += "NOCONFIG";
- }
- // Generate the per-config target information.
- this->GenerateImportTargetsConfig(os, config, suffix);
- }
- //----------------------------------------------------------------------------
- void cmExportFileGenerator::PopulateInterfaceProperty(const char *propName,
- const char *outputName,
- cmTarget *target,
- cmGeneratorExpression::PreprocessContext preprocessRule,
- ImportPropertyMap &properties,
- std::vector<std::string> &missingTargets)
- {
- const char *input = target->GetProperty(propName);
- if (input)
- {
- if (!*input)
- {
- // Set to empty
- properties[outputName] = "";
- return;
- }
- std::string prepro = cmGeneratorExpression::Preprocess(input,
- preprocessRule);
- if (!prepro.empty())
- {
- this->ResolveTargetsInGeneratorExpressions(prepro, target,
- missingTargets);
- properties[outputName] = prepro;
- }
- }
- }
- //----------------------------------------------------------------------------
- void cmExportFileGenerator::PopulateInterfaceProperty(const char *propName,
- cmTarget *target,
- cmGeneratorExpression::PreprocessContext preprocessRule,
- ImportPropertyMap &properties,
- std::vector<std::string> &missingTargets)
- {
- this->PopulateInterfaceProperty(propName, propName, target, preprocessRule,
- properties, missingTargets);
- }
- //----------------------------------------------------------------------------
- void cmExportFileGenerator::GenerateInterfaceProperties(cmTarget *target,
- std::ostream& os,
- const ImportPropertyMap &properties)
- {
- if (!properties.empty())
- {
- std::string targetName = this->Namespace;
- targetName += target->GetName();
- os << "SET_TARGET_PROPERTIES(" << targetName << " PROPERTIES\n";
- for(ImportPropertyMap::const_iterator pi = properties.begin();
- pi != properties.end(); ++pi)
- {
- os << " " << pi->first << " \"" << pi->second << "\"\n";
- }
- os << ")\n\n";
- }
- }
- //----------------------------------------------------------------------------
- bool
- cmExportFileGenerator::AddTargetNamespace(std::string &input,
- cmTarget* target,
- std::vector<std::string> &missingTargets)
- {
- cmMakefile *mf = target->GetMakefile();
- cmTarget *tgt = mf->FindTargetToUse(input.c_str());
- if (!tgt)
- {
- return false;
- }
- if(tgt->IsImported())
- {
- return true;
- }
- if(this->ExportedTargets.find(tgt) != this->ExportedTargets.end())
- {
- input = this->Namespace + input;
- }
- else
- {
- std::string namespacedTarget;
- this->HandleMissingTarget(namespacedTarget, missingTargets,
- mf, target, tgt);
- if (!namespacedTarget.empty())
- {
- input = namespacedTarget;
- }
- }
- return true;
- }
- //----------------------------------------------------------------------------
- static bool isGeneratorExpression(const std::string &lib)
- {
- const std::string::size_type openpos = lib.find("$<");
- return (openpos != std::string::npos)
- && (lib.find(">", openpos) != std::string::npos);
- }
- //----------------------------------------------------------------------------
- void
- cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
- std::string &input,
- cmTarget* target,
- std::vector<std::string> &missingTargets,
- FreeTargetsReplace replace)
- {
- if (replace == NoReplaceFreeTargets)
- {
- this->ResolveTargetsInGeneratorExpression(input, target, missingTargets);
- return;
- }
- std::vector<std::string> parts;
- cmGeneratorExpression::Split(input, parts);
- std::string sep;
- input = "";
- for(std::vector<std::string>::iterator li = parts.begin();
- li != parts.end(); ++li)
- {
- if (!isGeneratorExpression(*li))
- {
- this->AddTargetNamespace(*li, target, missingTargets);
- }
- else
- {
- this->ResolveTargetsInGeneratorExpression(
- *li,
- target,
- missingTargets);
- }
- input += sep + *li;
- sep = ";";
- }
- }
- //----------------------------------------------------------------------------
- void
- cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
- std::string &input,
- cmTarget* target,
- std::vector<std::string> &missingTargets)
- {
- std::string::size_type pos = 0;
- std::string::size_type lastPos = pos;
- cmMakefile *mf = target->GetMakefile();
- std::string errorString;
- while((pos = input.find("$<TARGET_PROPERTY:", lastPos)) != input.npos)
- {
- std::string::size_type nameStartPos = pos +
- sizeof("$<TARGET_PROPERTY:") - 1;
- std::string::size_type closePos = input.find(">", nameStartPos);
- std::string::size_type commaPos = input.find(",", nameStartPos);
- std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
- if (commaPos == input.npos // Implied 'this' target
- || closePos == input.npos // Imcomplete expression.
- || closePos < commaPos // Implied 'this' target
- || nextOpenPos < commaPos) // Non-literal
- {
- lastPos = nameStartPos;
- continue;
- }
- std::string targetName = input.substr(nameStartPos,
- commaPos - nameStartPos);
- if (!this->AddTargetNamespace(targetName, target, missingTargets))
- {
- errorString = "$<TARGET_PROPERTY:" + targetName + ",prop> requires "
- "its first parameter to be a reachable target.";
- break;
- }
- input.replace(nameStartPos, commaPos - nameStartPos, targetName);
- lastPos = pos + targetName.size();
- }
- if (!errorString.empty())
- {
- mf->IssueMessage(cmake::FATAL_ERROR, errorString);
- return;
- }
- pos = 0;
- lastPos = pos;
- while((pos = input.find("$<TARGET_NAME:", lastPos)) != input.npos)
- {
- std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1;
- std::string::size_type endPos = input.find(">", nameStartPos);
- if (endPos == input.npos)
- {
- errorString = "$<TARGET_NAME:...> expression incomplete";
- break;
- }
- std::string targetName = input.substr(nameStartPos,
- endPos - nameStartPos);
- if(targetName.find("$<") != input.npos)
- {
- errorString = "$<TARGET_NAME:...> requires its parameter to be a "
- "literal.";
- break;
- }
- if (!this->AddTargetNamespace(targetName, target, missingTargets))
- {
- errorString = "$<TARGET_NAME:...> requires its parameter to be a "
- "reachable target.";
- break;
- }
- input.replace(pos, endPos - pos + 1, targetName);
- lastPos = endPos;
- }
- if (!errorString.empty())
- {
- mf->IssueMessage(cmake::FATAL_ERROR, errorString);
- }
- }
- //----------------------------------------------------------------------------
- void
- cmExportFileGenerator
- ::SetImportDetailProperties(const char* config, std::string const& suffix,
- cmTarget* target, ImportPropertyMap& properties,
- std::vector<std::string>& missingTargets
- )
- {
- // Get the makefile in which to lookup target information.
- cmMakefile* mf = target->GetMakefile();
- // Add the soname for unix shared libraries.
- if(target->GetType() == cmTarget::SHARED_LIBRARY ||
- target->GetType() == cmTarget::MODULE_LIBRARY)
- {
- // Check whether this is a DLL platform.
- bool dll_platform =
- (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
- if(!dll_platform)
- {
- std::string prop;
- std::string value;
- if(target->HasSOName(config))
- {
- prop = "IMPORTED_SONAME";
- value = target->GetSOName(config);
- }
- else
- {
- prop = "IMPORTED_NO_SONAME";
- value = "TRUE";
- }
- prop += suffix;
- properties[prop] = value;
- }
- }
- // Add the transitive link dependencies for this configuration.
- if(cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
- target))
- {
- this->SetImportLinkProperty(suffix, target,
- "IMPORTED_LINK_INTERFACE_LANGUAGES",
- iface->Languages, properties, missingTargets);
- this->SetImportLinkProperty(suffix, target,
- "IMPORTED_LINK_INTERFACE_LIBRARIES",
- iface->Libraries, properties, missingTargets);
- this->SetImportLinkProperty(suffix, target,
- "IMPORTED_LINK_DEPENDENT_LIBRARIES",
- iface->SharedDeps, properties, missingTargets);
- if(iface->Multiplicity > 0)
- {
- std::string prop = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
- prop += suffix;
- cmOStringStream m;
- m << iface->Multiplicity;
- properties[prop] = m.str();
- }
- }
- }
- //----------------------------------------------------------------------------
- void
- cmExportFileGenerator
- ::SetImportLinkProperty(std::string const& suffix,
- cmTarget* target,
- const char* propName,
- std::vector<std::string> const& libs,
- ImportPropertyMap& properties,
- std::vector<std::string>& missingTargets
- )
- {
- // Skip the property if there are no libraries.
- if(libs.empty())
- {
- return;
- }
- // Construct the property value.
- std::string link_libs;
- const char* sep = "";
- for(std::vector<std::string>::const_iterator li = libs.begin();
- li != libs.end(); ++li)
- {
- // Separate this from the previous entry.
- link_libs += sep;
- sep = ";";
- std::string temp = *li;
- this->AddTargetNamespace(temp, target, missingTargets);
- link_libs += temp;
- }
- // Store the property.
- std::string prop = propName;
- prop += suffix;
- properties[prop] = link_libs;
- }
- //----------------------------------------------------------------------------
- void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
- const char* config)
- {
- os << "#----------------------------------------------------------------\n"
- << "# Generated CMake target import file";
- if(config)
- {
- os << " for configuration \"" << config << "\".\n";
- }
- else
- {
- os << ".\n";
- }
- os << "#----------------------------------------------------------------\n"
- << "\n";
- this->GenerateImportVersionCode(os);
- }
- //----------------------------------------------------------------------------
- void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
- {
- os << "# Commands beyond this point should not need to know the version.\n"
- << "SET(CMAKE_IMPORT_FILE_VERSION)\n";
- }
- //----------------------------------------------------------------------------
- void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
- {
- // Store an import file format version. This will let us change the
- // format later while still allowing old import files to work.
- os << "# Commands may need to know the format version.\n"
- << "SET(CMAKE_IMPORT_FILE_VERSION 1)\n"
- << "\n";
- }
- //----------------------------------------------------------------------------
- void cmExportFileGenerator::GenerateExpectedTargetsCode(std::ostream& os,
- const std::string &expectedTargets)
- {
- os << "SET(_targetsDefined)\n"
- "SET(_targetsNotDefined)\n"
- "SET(_expectedTargets)\n"
- "FOREACH(_expectedTarget " << expectedTargets << ")\n"
- " LIST(APPEND _expectedTargets ${_expectedTarget})\n"
- " IF(NOT TARGET ${_expectedTarget})\n"
- " LIST(APPEND _targetsNotDefined ${_expectedTarget})\n"
- " ENDIF(NOT TARGET ${_expectedTarget})\n"
- " IF(TARGET ${_expectedTarget})\n"
- " LIST(APPEND _targetsDefined ${_expectedTarget})\n"
- " ENDIF(TARGET ${_expectedTarget})\n"
- "ENDFOREACH(_expectedTarget)\n"
- "IF(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
- " SET(CMAKE_IMPORT_FILE_VERSION)\n"
- " CMAKE_POLICY(POP)\n"
- " RETURN()\n"
- "ENDIF(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
- "IF(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
- " MESSAGE(FATAL_ERROR \"Some (but not all) targets in this export "
- "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
- "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
- "ENDIF(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
- "UNSET(_targetsDefined)\n"
- "UNSET(_targetsNotDefined)\n"
- "UNSET(_expectedTargets)\n"
- "\n\n";
- }
- //----------------------------------------------------------------------------
- void
- cmExportFileGenerator
- ::GenerateImportTargetCode(std::ostream& os, cmTarget* target)
- {
- // Construct the imported target name.
- std::string targetName = this->Namespace;
- targetName += target->GetName();
- // Create the imported target.
- os << "# Create imported target " << targetName << "\n";
- switch(target->GetType())
- {
- case cmTarget::EXECUTABLE:
- os << "ADD_EXECUTABLE(" << targetName << " IMPORTED)\n";
- break;
- case cmTarget::STATIC_LIBRARY:
- os << "ADD_LIBRARY(" << targetName << " STATIC IMPORTED)\n";
- break;
- case cmTarget::SHARED_LIBRARY:
- os << "ADD_LIBRARY(" << targetName << " SHARED IMPORTED)\n";
- break;
- case cmTarget::MODULE_LIBRARY:
- os << "ADD_LIBRARY(" << targetName << " MODULE IMPORTED)\n";
- break;
- default: // should never happen
- break;
- }
- // Mark the imported executable if it has exports.
- if(target->IsExecutableWithExports())
- {
- os << "SET_PROPERTY(TARGET " << targetName
- << " PROPERTY ENABLE_EXPORTS 1)\n";
- }
- // Mark the imported library if it is a framework.
- if(target->IsFrameworkOnApple())
- {
- os << "SET_PROPERTY(TARGET " << targetName
- << " PROPERTY FRAMEWORK 1)\n";
- }
- // Mark the imported executable if it is an application bundle.
- if(target->IsAppBundleOnApple())
- {
- os << "SET_PROPERTY(TARGET " << targetName
- << " PROPERTY MACOSX_BUNDLE 1)\n";
- }
- if (target->IsCFBundleOnApple())
- {
- os << "SET_PROPERTY(TARGET " << targetName
- << " PROPERTY BUNDLE 1)\n";
- }
- os << "\n";
- }
- //----------------------------------------------------------------------------
- void
- cmExportFileGenerator
- ::GenerateImportPropertyCode(std::ostream& os, const char* config,
- cmTarget* target,
- ImportPropertyMap const& properties)
- {
- // Construct the imported target name.
- std::string targetName = this->Namespace;
- targetName += target->GetName();
- // Set the import properties.
- os << "# Import target \"" << targetName << "\" for configuration \""
- << config << "\"\n";
- os << "SET_PROPERTY(TARGET " << targetName
- << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
- if(config && *config)
- {
- os << cmSystemTools::UpperCase(config);
- }
- else
- {
- os << "NOCONFIG";
- }
- os << ")\n";
- os << "SET_TARGET_PROPERTIES(" << targetName << " PROPERTIES\n";
- for(ImportPropertyMap::const_iterator pi = properties.begin();
- pi != properties.end(); ++pi)
- {
- os << " " << pi->first << " \"" << pi->second << "\"\n";
- }
- os << " )\n"
- << "\n";
- }
- //----------------------------------------------------------------------------
- void cmExportFileGenerator::GenerateMissingTargetsCheckCode(std::ostream& os,
- const std::vector<std::string>& missingTargets)
- {
- os << "# Make sure the targets which have been exported in some other \n"
- "# export set exist.\n";
- for(unsigned int i=0; i<missingTargets.size(); ++i)
- {
- os << "IF(NOT TARGET \"" << missingTargets[i] << "\" )\n"
- << " IF(CMAKE_FIND_PACKAGE_NAME)\n"
- << " SET( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
- << " SET( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
- << "\"Required imported target \\\"" << missingTargets[i]
- << "\\\" not found ! \")\n"
- << " ELSE()\n"
- << " MESSAGE(FATAL_ERROR \"Required imported target \\\""
- << missingTargets[i] << "\\\" not found ! \")\n"
- << " ENDIF()\n"
- << "ENDIF()\n";
- }
- os << "\n";
- }
- //----------------------------------------------------------------------------
- void
- cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
- {
- // Add code which verifies at cmake time that the file which is being
- // imported actually exists on disk. This should in theory always be theory
- // case, but still when packages are split into normal and development
- // packages this might get broken (e.g. the Config.cmake could be part of
- // the non-development package, something similar happened to me without
- // on SUSE with a mysql pkg-config file, which claimed everything is fine,
- // but the development package was not installed.).
- os << "# Loop over all imported files and verify that they actually exist\n"
- "FOREACH(target ${_IMPORT_CHECK_TARGETS} )\n"
- " FOREACH(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
- " IF(NOT EXISTS \"${file}\" )\n"
- " MESSAGE(FATAL_ERROR \"The imported target \\\"${target}\\\""
- " references the file\n"
- " \\\"${file}\\\"\n"
- "but this file does not exist. Possible reasons include:\n"
- "* The file was deleted, renamed, or moved to another location.\n"
- "* An install or uninstall procedure did not complete successfully.\n"
- "* The installation package was faulty and contained\n"
- " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
- "but not all the files it references.\n"
- "\")\n"
- " ENDIF()\n"
- " ENDFOREACH()\n"
- " UNSET(_IMPORT_CHECK_FILES_FOR_${target})\n"
- "ENDFOREACH()\n"
- "UNSET(_IMPORT_CHECK_TARGETS)\n"
- "\n";
- }
- //----------------------------------------------------------------------------
- void
- cmExportFileGenerator
- ::GenerateImportedFileChecksCode(std::ostream& os, cmTarget* target,
- ImportPropertyMap const& properties,
- const std::set<std::string>& importedLocations)
- {
- // Construct the imported target name.
- std::string targetName = this->Namespace;
- targetName += target->GetName();
- os << "LIST(APPEND _IMPORT_CHECK_TARGETS " << targetName << " )\n"
- "LIST(APPEND _IMPORT_CHECK_FILES_FOR_" << targetName << " ";
- for(std::set<std::string>::const_iterator li = importedLocations.begin();
- li != importedLocations.end();
- ++li)
- {
- ImportPropertyMap::const_iterator pi = properties.find(*li);
- if (pi != properties.end())
- {
- os << "\"" << pi->second << "\" ";
- }
- }
- os << ")\n\n";
- }
|