| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- /*=========================================================================
- Program: CMake - Cross-Platform Makefile Generator
- 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.
- 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 "cmExportFileGenerator.h"
- #include "cmGeneratedFileStream.h"
- #include "cmMakefile.h"
- #include "cmSystemTools.h"
- #include "cmTarget.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;
- // 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);
- 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
- ::SetImportDetailProperties(const char* config, std::string const& suffix,
- cmTarget* target, ImportPropertyMap& properties)
- {
- // 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 soname = target->GetSOName(config);
- std::string prop = "IMPORTED_SONAME";
- prop += suffix;
- properties[prop] = soname;
- }
- }
- // Add the transitive link dependencies for this configuration.
- if(cmTargetLinkInterface const* interface =
- target->GetLinkInterface(config))
- {
- // This target provides a link interface, so use it.
- this->SetImportLinkProperty(suffix, target,
- "IMPORTED_LINK_INTERFACE_LIBRARIES",
- interface->Libraries, properties);
- this->SetImportLinkProperty(suffix, target,
- "IMPORTED_LINK_DEPENDENT_LIBRARIES",
- interface->SharedDeps, properties);
- }
- else if(target->GetType() == cmTarget::STATIC_LIBRARY ||
- target->GetType() == cmTarget::SHARED_LIBRARY)
- {
- // The default link interface for static and shared libraries is
- // their link implementation library list.
- this->SetImportLinkProperties(config, suffix, target, properties);
- }
- }
- //----------------------------------------------------------------------------
- void
- cmExportFileGenerator
- ::SetImportLinkProperties(const char* config, std::string const& suffix,
- cmTarget* target, ImportPropertyMap& properties)
- {
- // Compute which library configuration to link.
- cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
- if(config && cmSystemTools::UpperCase(config) == "DEBUG")
- {
- linkType = cmTarget::DEBUG;
- }
- // Construct the list of libs linked for this configuration.
- std::vector<std::string> actual_libs;
- cmTarget::LinkLibraryVectorType const& libs =
- target->GetOriginalLinkLibraries();
- for(cmTarget::LinkLibraryVectorType::const_iterator li = libs.begin();
- li != libs.end(); ++li)
- {
- // Skip entries that will resolve to the target itself, are empty,
- // or are not meant for this configuration.
- if(li->first == target->GetName() || li->first.empty() ||
- !(li->second == cmTarget::GENERAL || li->second == linkType))
- {
- continue;
- }
- // Store this entry.
- actual_libs.push_back(li->first);
- }
- // Store the entries in the property.
- this->SetImportLinkProperty(suffix, target,
- "IMPORTED_LINK_INTERFACE_LIBRARIES",
- actual_libs, properties);
- }
- //----------------------------------------------------------------------------
- void
- cmExportFileGenerator
- ::SetImportLinkProperty(std::string const& suffix,
- cmTarget* target,
- const char* propName,
- std::vector<std::string> const& libs,
- ImportPropertyMap& properties)
- {
- // Skip the property if there are no libraries.
- if(libs.empty())
- {
- return;
- }
- // Get the makefile in which to lookup target information.
- cmMakefile* mf = target->GetMakefile();
- // 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 = ";";
- // Append this entry.
- if(cmTarget* tgt = mf->FindTargetToUse(li->c_str()))
- {
- // This is a target.
- if(tgt->IsImported())
- {
- // The target is imported (and therefore is not in the
- // export). Append the raw name.
- link_libs += *li;
- }
- else if(this->ExportedTargets.find(tgt) != this->ExportedTargets.end())
- {
- // The target is in the export. Append it with the export
- // namespace.
- link_libs += this->Namespace;
- link_libs += *li;
- }
- else
- {
- // The target is not in the export.
- if(!this->AppendMode)
- {
- // We are not appending, so all exported targets should be
- // known here. This is probably user-error.
- this->ComplainAboutMissingTarget(target, li->c_str());
- }
- // Assume the target will be exported by another command.
- // Append it with the export namespace.
- link_libs += this->Namespace;
- link_libs += *li;
- }
- }
- else
- {
- // Append the raw name.
- link_libs += *li;
- }
- }
- // 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
- ::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";
- }
- 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";
- }
|