1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmExportInstallFileGenerator.h"
14 #include "cmExportSet.h"
15 #include "cmExportSetMap.h"
16 #include "cmGeneratedFileStream.h"
17 #include "cmGlobalGenerator.h"
18 #include "cmLocalGenerator.h"
19 #include "cmInstallExportGenerator.h"
20 #include "cmInstallTargetGenerator.h"
21 #include "cmTargetExport.h"
23 //----------------------------------------------------------------------------
24 cmExportInstallFileGenerator
25 ::cmExportInstallFileGenerator(cmInstallExportGenerator* iegen):
30 //----------------------------------------------------------------------------
31 std::string cmExportInstallFileGenerator::GetConfigImportFileGlob()
33 std::string glob = this->FileBase;
35 glob += this->FileExt;
39 //----------------------------------------------------------------------------
40 bool cmExportInstallFileGenerator::GenerateMainFile(std::ostream& os)
42 std::vector<cmTarget*> allTargets;
44 std::string expectedTargets;
46 for(std::vector<cmTargetExport*>::const_iterator
47 tei = this->IEGen->GetExportSet()->GetTargetExports()->begin();
48 tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei)
50 expectedTargets += sep + this->Namespace + (*tei)->Target->GetName();
52 cmTargetExport const* te = *tei;
53 if(this->ExportedTargets.insert(te->Target).second)
55 allTargets.push_back(te->Target);
60 e << "install(EXPORT \""
61 << this->IEGen->GetExportSet()->GetName()
62 << "\" ...) " << "includes target \"" << te->Target->GetName()
63 << "\" more than once in the export set.";
64 cmSystemTools::Error(e.str().c_str());
69 this->GenerateExpectedTargetsCode(os, expectedTargets);
72 // Add code to compute the installation prefix relative to the
73 // import file location.
74 const char* installDest = this->IEGen->GetDestination();
75 if(!cmSystemTools::FileIsFullPath(installDest))
77 std::string installPrefix =
78 this->IEGen->GetMakefile()->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
79 std::string absDest = installPrefix + "/" + installDest;
80 std::string absDestS = absDest + "/";
81 os << "# Compute the installation prefix relative to this file.\n"
82 << "get_filename_component(_IMPORT_PREFIX"
83 << " \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n";
84 if(strncmp(absDestS.c_str(), "/lib/", 5) == 0 ||
85 strncmp(absDestS.c_str(), "/lib64/", 7) == 0 ||
86 strncmp(absDestS.c_str(), "/usr/lib/", 9) == 0 ||
87 strncmp(absDestS.c_str(), "/usr/lib64/", 11) == 0)
89 // Handle "/usr move" symlinks created by some Linux distros.
91 "# Use original install prefix when loaded through a\n"
92 "# cross-prefix symbolic link such as /lib -> /usr/lib.\n"
93 "get_filename_component(_realCurr \"${_IMPORT_PREFIX}\" REALPATH)\n"
94 "get_filename_component(_realOrig \"" << absDest << "\" REALPATH)\n"
95 "if(_realCurr STREQUAL _realOrig)\n"
96 " set(_IMPORT_PREFIX \"" << absDest << "\")\n"
101 std::string dest = installDest;
105 "get_filename_component(_IMPORT_PREFIX \"${_IMPORT_PREFIX}\" PATH)\n";
106 dest = cmSystemTools::GetFilenamePath(dest);
110 // Import location properties may reference this variable.
111 this->ImportPrefix = "${_IMPORT_PREFIX}/";
114 std::vector<std::string> missingTargets;
116 // Create all the imported targets.
117 for(std::vector<cmTarget*>::const_iterator
118 tei = allTargets.begin();
119 tei != allTargets.end(); ++tei)
122 this->GenerateImportTargetCode(os, te);
124 ImportPropertyMap properties;
126 this->PopulateIncludeDirectoriesInterface(te,
127 cmGeneratorExpression::InstallInterface,
128 properties, missingTargets);
129 this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS",
131 cmGeneratorExpression::InstallInterface,
132 properties, missingTargets);
133 this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE",
135 this->PopulateCompatibleInterfaceProperties(te, properties);
137 this->GenerateInterfaceProperties(te, os, properties);
141 // Now load per-configuration properties for them.
142 os << "# Load information for each installed configuration.\n"
143 << "get_filename_component(_DIR \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n"
144 << "file(GLOB CONFIG_FILES \"${_DIR}/"
145 << this->GetConfigImportFileGlob() << "\")\n"
146 << "foreach(f ${CONFIG_FILES})\n"
147 << " include(${f})\n"
151 // Cleanup the import prefix variable.
152 if(!this->ImportPrefix.empty())
154 os << "# Cleanup temporary variables.\n"
155 << "set(_IMPORT_PREFIX)\n"
158 this->GenerateImportedFileCheckLoop(os);
160 // Generate an import file for each configuration.
162 for(std::vector<std::string>::const_iterator
163 ci = this->Configurations.begin();
164 ci != this->Configurations.end(); ++ci)
166 if(!this->GenerateImportFileConfig(ci->c_str(), missingTargets))
172 this->GenerateMissingTargetsCheckCode(os, missingTargets);
177 //----------------------------------------------------------------------------
179 cmExportInstallFileGenerator::ReplaceInstallPrefix(std::string &input)
181 std::string::size_type pos = 0;
182 std::string::size_type lastPos = pos;
184 while((pos = input.find("$<INSTALL_PREFIX>", lastPos)) != input.npos)
186 std::string::size_type endPos = pos + sizeof("$<INSTALL_PREFIX>") - 1;
187 input.replace(pos, endPos - pos, "${_IMPORT_PREFIX}");
192 //----------------------------------------------------------------------------
194 cmExportInstallFileGenerator::GenerateImportFileConfig(const char* config,
195 std::vector<std::string> &missingTargets)
197 // Skip configurations not enabled for this export.
198 if(!this->IEGen->InstallsForConfig(config))
203 // Construct the name of the file to generate.
204 std::string fileName = this->FileDir;
206 fileName += this->FileBase;
208 if(config && *config)
210 fileName += cmSystemTools::LowerCase(config);
214 fileName += "noconfig";
216 fileName += this->FileExt;
218 // Open the output file to generate it.
219 cmGeneratedFileStream exportFileStream(fileName.c_str(), true);
220 if(!exportFileStream)
222 std::string se = cmSystemTools::GetLastSystemError();
224 e << "cannot write to file \"" << fileName.c_str()
226 cmSystemTools::Error(e.str().c_str());
229 std::ostream& os = exportFileStream;
231 // Start with the import file header.
232 this->GenerateImportHeaderCode(os, config);
234 // Generate the per-config target information.
235 this->GenerateImportConfig(os, config, missingTargets);
237 // End with the import file footer.
238 this->GenerateImportFooterCode(os);
240 // Record this per-config import file.
241 this->ConfigImportFiles[config] = fileName;
246 //----------------------------------------------------------------------------
248 cmExportInstallFileGenerator
249 ::GenerateImportTargetsConfig(std::ostream& os,
250 const char* config, std::string const& suffix,
251 std::vector<std::string> &missingTargets)
253 // Add each target in the set to the export.
254 for(std::vector<cmTargetExport*>::const_iterator
255 tei = this->IEGen->GetExportSet()->GetTargetExports()->begin();
256 tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei)
258 // Collect import properties for this target.
259 cmTargetExport const* te = *tei;
260 ImportPropertyMap properties;
261 std::set<std::string> importedLocations;
262 this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
263 properties, importedLocations);
264 this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
265 properties, importedLocations);
266 this->SetImportLocationProperty(config, suffix,
267 te->RuntimeGenerator, properties,
269 this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
270 properties, importedLocations);
271 this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
272 properties, importedLocations);
274 // If any file location was set for the target add it to the
276 if(!properties.empty())
278 // Get the rest of the target details.
279 this->SetImportDetailProperties(config, suffix,
280 te->Target, properties, missingTargets);
282 this->SetImportLinkInterface(config, suffix,
283 cmGeneratorExpression::InstallInterface,
284 te->Target, properties, missingTargets);
286 // TOOD: PUBLIC_HEADER_LOCATION
287 // This should wait until the build feature propagation stuff
288 // is done. Then this can be a propagated include directory.
289 // this->GenerateImportProperty(config, te->HeaderGenerator,
292 // Generate code in the export file.
293 this->GenerateImportPropertyCode(os, config, te->Target, properties);
294 this->GenerateImportedFileChecksCode(os, te->Target, properties,
300 //----------------------------------------------------------------------------
302 cmExportInstallFileGenerator
303 ::SetImportLocationProperty(const char* config, std::string const& suffix,
304 cmInstallTargetGenerator* itgen,
305 ImportPropertyMap& properties,
306 std::set<std::string>& importedLocations
309 // Skip rules that do not match this configuration.
310 if(!(itgen && itgen->InstallsForConfig(config)))
315 // Get the target to be installed.
316 cmTarget* target = itgen->GetTarget();
318 // Construct the installed location of the target.
319 std::string dest = itgen->GetDestination();
321 if(!cmSystemTools::FileIsFullPath(dest.c_str()))
323 // The target is installed relative to the installation prefix.
324 if(this->ImportPrefix.empty())
326 this->ComplainAboutImportPrefix(itgen);
328 value = this->ImportPrefix;
333 if(itgen->IsImportLibrary())
335 // Construct the property name.
336 std::string prop = "IMPORTED_IMPLIB";
339 // Append the installed file name.
340 value += itgen->GetInstallFilename(target, config,
341 cmInstallTargetGenerator::NameImplib);
343 // Store the property.
344 properties[prop] = value;
345 importedLocations.insert(prop);
349 // Construct the property name.
350 std::string prop = "IMPORTED_LOCATION";
353 // Append the installed file name.
354 if(target->IsFrameworkOnApple())
356 value += itgen->GetInstallFilename(target, config);
357 value += ".framework/";
358 value += itgen->GetInstallFilename(target, config);
360 else if(target->IsCFBundleOnApple())
362 const char *ext = target->GetProperty("BUNDLE_EXTENSION");
368 value += itgen->GetInstallFilename(target, config);
372 value += itgen->GetInstallFilename(target, config);
374 else if(target->IsAppBundleOnApple())
376 value += itgen->GetInstallFilename(target, config);
377 value += ".app/Contents/MacOS/";
378 value += itgen->GetInstallFilename(target, config);
382 value += itgen->GetInstallFilename(target, config,
383 cmInstallTargetGenerator::NameReal);
386 // Store the property.
387 properties[prop] = value;
388 importedLocations.insert(prop);
392 //----------------------------------------------------------------------------
394 cmExportInstallFileGenerator::HandleMissingTarget(
395 std::string& link_libs, std::vector<std::string>& missingTargets,
396 cmMakefile* mf, cmTarget* depender, cmTarget* dependee)
398 std::string name = dependee->GetName();
399 std::vector<std::string> namespaces = this->FindNamespaces(mf, name);
400 int targetOccurrences = (int)namespaces.size();
401 if (targetOccurrences == 1)
403 std::string missingTarget = namespaces[0];
404 missingTarget += name;
405 link_libs += missingTarget;
406 missingTargets.push_back(missingTarget);
410 // We are not appending, so all exported targets should be
411 // known here. This is probably user-error.
412 this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
416 //----------------------------------------------------------------------------
417 std::vector<std::string>
418 cmExportInstallFileGenerator
419 ::FindNamespaces(cmMakefile* mf, const std::string& name)
421 std::vector<std::string> namespaces;
422 cmGlobalGenerator* gg = mf->GetLocalGenerator()->GetGlobalGenerator();
423 const cmExportSetMap& exportSets = gg->GetExportSets();
425 for(cmExportSetMap::const_iterator expIt = exportSets.begin();
426 expIt != exportSets.end();
429 const cmExportSet* exportSet = expIt->second;
430 std::vector<cmTargetExport*> const* targets =
431 exportSet->GetTargetExports();
433 bool containsTarget = false;
434 for(unsigned int i=0; i<targets->size(); i++)
436 if (name == (*targets)[i]->Target->GetName())
438 containsTarget = true;
445 std::vector<cmInstallExportGenerator const*> const* installs =
446 exportSet->GetInstallations();
447 for(unsigned int i=0; i<installs->size(); i++)
449 namespaces.push_back((*installs)[i]->GetNamespace());
458 //----------------------------------------------------------------------------
460 cmExportInstallFileGenerator
461 ::ComplainAboutImportPrefix(cmInstallTargetGenerator* itgen)
463 const char* installDest = this->IEGen->GetDestination();
465 e << "install(EXPORT \""
466 << this->IEGen->GetExportSet()->GetName()
467 << "\") given absolute "
468 << "DESTINATION \"" << installDest << "\" but the export "
469 << "references an installation of target \""
470 << itgen->GetTarget()->GetName() << "\" which has relative "
471 << "DESTINATION \"" << itgen->GetDestination() << "\".";
472 cmSystemTools::Error(e.str().c_str());
475 //----------------------------------------------------------------------------
477 cmExportInstallFileGenerator
478 ::ComplainAboutMissingTarget(cmTarget* depender,
483 e << "install(EXPORT \""
484 << this->IEGen->GetExportSet()->GetName()
486 << "includes target \"" << depender->GetName()
487 << "\" which requires target \"" << dependee->GetName() << "\" ";
488 if (occurrences == 0)
490 e << "that is not in the export set.";
494 e << "that is not in this export set, but " << occurrences
495 << " times in others.";
497 cmSystemTools::Error(e.str().c_str());