cmExportInstallFileGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmExportInstallFileGenerator.h"
  11. #include "cmExportSet.h"
  12. #include "cmExportSetMap.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmInstallExportGenerator.h"
  17. #include "cmInstallTargetGenerator.h"
  18. #include "cmTargetExport.h"
  19. //----------------------------------------------------------------------------
  20. cmExportInstallFileGenerator
  21. ::cmExportInstallFileGenerator(cmInstallExportGenerator* iegen):
  22. IEGen(iegen)
  23. {
  24. }
  25. //----------------------------------------------------------------------------
  26. std::string cmExportInstallFileGenerator::GetConfigImportFileGlob()
  27. {
  28. std::string glob = this->FileBase;
  29. glob += "-*";
  30. glob += this->FileExt;
  31. return glob;
  32. }
  33. //----------------------------------------------------------------------------
  34. bool cmExportInstallFileGenerator::GenerateMainFile(std::ostream& os)
  35. {
  36. std::vector<cmTarget*> allTargets;
  37. {
  38. std::string expectedTargets;
  39. std::string sep;
  40. for(std::vector<cmTargetExport*>::const_iterator
  41. tei = this->IEGen->GetExportSet()->GetTargetExports()->begin();
  42. tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei)
  43. {
  44. expectedTargets += sep + this->Namespace + (*tei)->Target->GetName();
  45. sep = " ";
  46. cmTargetExport const* te = *tei;
  47. if(this->ExportedTargets.insert(te->Target).second)
  48. {
  49. allTargets.push_back(te->Target);
  50. }
  51. else
  52. {
  53. cmOStringStream e;
  54. e << "INSTALL(EXPORT \""
  55. << this->IEGen->GetExportSet()->GetName()
  56. << "\" ...) " << "includes target \"" << te->Target->GetName()
  57. << "\" more than once in the export set.";
  58. cmSystemTools::Error(e.str().c_str());
  59. return false;
  60. }
  61. }
  62. this->GenerateExpectedTargetsCode(os, expectedTargets);
  63. }
  64. std::vector<std::string> missingTargets;
  65. // Create all the imported targets.
  66. for(std::vector<cmTarget*>::const_iterator
  67. tei = allTargets.begin();
  68. tei != allTargets.end(); ++tei)
  69. {
  70. cmTarget* te = *tei;
  71. this->GenerateImportTargetCode(os, te);
  72. ImportPropertyMap properties;
  73. this->PopulateInterfaceProperty("INTERFACE_INCLUDE_DIRECTORIES",
  74. te,
  75. cmGeneratorExpression::InstallInterface,
  76. properties, missingTargets);
  77. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS",
  78. te,
  79. cmGeneratorExpression::InstallInterface,
  80. properties, missingTargets);
  81. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE",
  82. te, properties);
  83. this->PopulateCompatibleInterfaceProperties(te, properties);
  84. this->GenerateInterfaceProperties(te, os, properties);
  85. }
  86. // Now load per-configuration properties for them.
  87. os << "# Load information for each installed configuration.\n"
  88. << "GET_FILENAME_COMPONENT(_DIR \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n"
  89. << "FILE(GLOB CONFIG_FILES \"${_DIR}/"
  90. << this->GetConfigImportFileGlob() << "\")\n"
  91. << "FOREACH(f ${CONFIG_FILES})\n"
  92. << " INCLUDE(${f})\n"
  93. << "ENDFOREACH(f)\n"
  94. << "\n";
  95. this->GenerateImportedFileCheckLoop(os);
  96. // Generate an import file for each configuration.
  97. bool result = true;
  98. for(std::vector<std::string>::const_iterator
  99. ci = this->Configurations.begin();
  100. ci != this->Configurations.end(); ++ci)
  101. {
  102. if(!this->GenerateImportFileConfig(ci->c_str(), missingTargets))
  103. {
  104. result = false;
  105. }
  106. }
  107. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  108. return result;
  109. }
  110. //----------------------------------------------------------------------------
  111. bool
  112. cmExportInstallFileGenerator::GenerateImportFileConfig(const char* config,
  113. std::vector<std::string> &missingTargets)
  114. {
  115. // Skip configurations not enabled for this export.
  116. if(!this->IEGen->InstallsForConfig(config))
  117. {
  118. return true;
  119. }
  120. // Construct the name of the file to generate.
  121. std::string fileName = this->FileDir;
  122. fileName += "/";
  123. fileName += this->FileBase;
  124. fileName += "-";
  125. if(config && *config)
  126. {
  127. fileName += cmSystemTools::LowerCase(config);
  128. }
  129. else
  130. {
  131. fileName += "noconfig";
  132. }
  133. fileName += this->FileExt;
  134. // Open the output file to generate it.
  135. cmGeneratedFileStream exportFileStream(fileName.c_str(), true);
  136. if(!exportFileStream)
  137. {
  138. std::string se = cmSystemTools::GetLastSystemError();
  139. cmOStringStream e;
  140. e << "cannot write to file \"" << fileName.c_str()
  141. << "\": " << se;
  142. cmSystemTools::Error(e.str().c_str());
  143. return false;
  144. }
  145. std::ostream& os = exportFileStream;
  146. // Start with the import file header.
  147. this->GenerateImportHeaderCode(os, config);
  148. // Generate the per-config target information.
  149. this->GenerateImportConfig(os, config, missingTargets);
  150. // End with the import file footer.
  151. this->GenerateImportFooterCode(os);
  152. // Record this per-config import file.
  153. this->ConfigImportFiles[config] = fileName;
  154. return true;
  155. }
  156. //----------------------------------------------------------------------------
  157. void
  158. cmExportInstallFileGenerator
  159. ::GenerateImportTargetsConfig(std::ostream& os,
  160. const char* config, std::string const& suffix,
  161. std::vector<std::string> &missingTargets)
  162. {
  163. // Add code to compute the installation prefix relative to the
  164. // import file location.
  165. const char* installDest = this->IEGen->GetDestination();
  166. if(!cmSystemTools::FileIsFullPath(installDest))
  167. {
  168. std::string dest = installDest;
  169. os << "# Compute the installation prefix relative to this file.\n"
  170. << "GET_FILENAME_COMPONENT(_IMPORT_PREFIX "
  171. << "\"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n";
  172. while(!dest.empty())
  173. {
  174. os <<
  175. "GET_FILENAME_COMPONENT(_IMPORT_PREFIX \"${_IMPORT_PREFIX}\" PATH)\n";
  176. dest = cmSystemTools::GetFilenamePath(dest);
  177. }
  178. os << "\n";
  179. // Import location properties may reference this variable.
  180. this->ImportPrefix = "${_IMPORT_PREFIX}/";
  181. }
  182. // Add each target in the set to the export.
  183. for(std::vector<cmTargetExport*>::const_iterator
  184. tei = this->IEGen->GetExportSet()->GetTargetExports()->begin();
  185. tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei)
  186. {
  187. // Collect import properties for this target.
  188. cmTargetExport const* te = *tei;
  189. ImportPropertyMap properties;
  190. std::set<std::string> importedLocations;
  191. this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
  192. properties, importedLocations);
  193. this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
  194. properties, importedLocations);
  195. this->SetImportLocationProperty(config, suffix,
  196. te->RuntimeGenerator, properties,
  197. importedLocations);
  198. this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
  199. properties, importedLocations);
  200. this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
  201. properties, importedLocations);
  202. // If any file location was set for the target add it to the
  203. // import file.
  204. if(!properties.empty())
  205. {
  206. // Get the rest of the target details.
  207. this->SetImportDetailProperties(config, suffix,
  208. te->Target, properties, missingTargets);
  209. this->SetImportLinkInterface(config, suffix,
  210. cmGeneratorExpression::InstallInterface,
  211. te->Target, properties, missingTargets);
  212. // TOOD: PUBLIC_HEADER_LOCATION
  213. // This should wait until the build feature propagation stuff
  214. // is done. Then this can be a propagated include directory.
  215. // this->GenerateImportProperty(config, te->HeaderGenerator,
  216. // properties);
  217. // Generate code in the export file.
  218. this->GenerateImportPropertyCode(os, config, te->Target, properties);
  219. this->GenerateImportedFileChecksCode(os, te->Target, properties,
  220. importedLocations);
  221. }
  222. }
  223. // Cleanup the import prefix variable.
  224. if(!this->ImportPrefix.empty())
  225. {
  226. os << "# Cleanup temporary variables.\n"
  227. << "SET(_IMPORT_PREFIX)\n"
  228. << "\n";
  229. }
  230. }
  231. //----------------------------------------------------------------------------
  232. void
  233. cmExportInstallFileGenerator
  234. ::SetImportLocationProperty(const char* config, std::string const& suffix,
  235. cmInstallTargetGenerator* itgen,
  236. ImportPropertyMap& properties,
  237. std::set<std::string>& importedLocations
  238. )
  239. {
  240. // Skip rules that do not match this configuration.
  241. if(!(itgen && itgen->InstallsForConfig(config)))
  242. {
  243. return;
  244. }
  245. // Get the target to be installed.
  246. cmTarget* target = itgen->GetTarget();
  247. // Construct the installed location of the target.
  248. std::string dest = itgen->GetDestination();
  249. std::string value;
  250. if(!cmSystemTools::FileIsFullPath(dest.c_str()))
  251. {
  252. // The target is installed relative to the installation prefix.
  253. if(this->ImportPrefix.empty())
  254. {
  255. this->ComplainAboutImportPrefix(itgen);
  256. }
  257. value = this->ImportPrefix;
  258. }
  259. value += dest;
  260. value += "/";
  261. if(itgen->IsImportLibrary())
  262. {
  263. // Construct the property name.
  264. std::string prop = "IMPORTED_IMPLIB";
  265. prop += suffix;
  266. // Append the installed file name.
  267. value += itgen->GetInstallFilename(target, config,
  268. cmInstallTargetGenerator::NameImplib);
  269. // Store the property.
  270. properties[prop] = value;
  271. importedLocations.insert(prop);
  272. }
  273. else
  274. {
  275. // Construct the property name.
  276. std::string prop = "IMPORTED_LOCATION";
  277. prop += suffix;
  278. // Append the installed file name.
  279. if(target->IsFrameworkOnApple())
  280. {
  281. value += itgen->GetInstallFilename(target, config);
  282. value += ".framework/";
  283. value += itgen->GetInstallFilename(target, config);
  284. }
  285. else if(target->IsCFBundleOnApple())
  286. {
  287. const char *ext = target->GetProperty("BUNDLE_EXTENSION");
  288. if (!ext)
  289. {
  290. ext = "bundle";
  291. }
  292. value += itgen->GetInstallFilename(target, config);
  293. value += ".";
  294. value += ext;
  295. value += "/";
  296. value += itgen->GetInstallFilename(target, config);
  297. }
  298. else if(target->IsAppBundleOnApple())
  299. {
  300. value += itgen->GetInstallFilename(target, config);
  301. value += ".app/Contents/MacOS/";
  302. value += itgen->GetInstallFilename(target, config);
  303. }
  304. else
  305. {
  306. value += itgen->GetInstallFilename(target, config,
  307. cmInstallTargetGenerator::NameReal);
  308. }
  309. // Store the property.
  310. properties[prop] = value;
  311. importedLocations.insert(prop);
  312. }
  313. }
  314. //----------------------------------------------------------------------------
  315. void
  316. cmExportInstallFileGenerator::HandleMissingTarget(
  317. std::string& link_libs, std::vector<std::string>& missingTargets,
  318. cmMakefile* mf, cmTarget* depender, cmTarget* dependee)
  319. {
  320. std::string name = dependee->GetName();
  321. std::vector<std::string> namespaces = this->FindNamespaces(mf, name);
  322. int targetOccurrences = (int)namespaces.size();
  323. if (targetOccurrences == 1)
  324. {
  325. std::string missingTarget = namespaces[0];
  326. missingTarget += name;
  327. link_libs += missingTarget;
  328. missingTargets.push_back(missingTarget);
  329. }
  330. else
  331. {
  332. // We are not appending, so all exported targets should be
  333. // known here. This is probably user-error.
  334. this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
  335. }
  336. }
  337. //----------------------------------------------------------------------------
  338. std::vector<std::string>
  339. cmExportInstallFileGenerator
  340. ::FindNamespaces(cmMakefile* mf, const std::string& name)
  341. {
  342. std::vector<std::string> namespaces;
  343. cmGlobalGenerator* gg = mf->GetLocalGenerator()->GetGlobalGenerator();
  344. const cmExportSetMap& exportSets = gg->GetExportSets();
  345. for(cmExportSetMap::const_iterator expIt = exportSets.begin();
  346. expIt != exportSets.end();
  347. ++expIt)
  348. {
  349. const cmExportSet* exportSet = expIt->second;
  350. std::vector<cmTargetExport*> const* targets =
  351. exportSet->GetTargetExports();
  352. bool containsTarget = false;
  353. for(unsigned int i=0; i<targets->size(); i++)
  354. {
  355. if (name == (*targets)[i]->Target->GetName())
  356. {
  357. containsTarget = true;
  358. break;
  359. }
  360. }
  361. if (containsTarget)
  362. {
  363. std::vector<cmInstallExportGenerator const*> const* installs =
  364. exportSet->GetInstallations();
  365. for(unsigned int i=0; i<installs->size(); i++)
  366. {
  367. namespaces.push_back((*installs)[i]->GetNamespace());
  368. }
  369. }
  370. }
  371. return namespaces;
  372. }
  373. //----------------------------------------------------------------------------
  374. void
  375. cmExportInstallFileGenerator
  376. ::ComplainAboutImportPrefix(cmInstallTargetGenerator* itgen)
  377. {
  378. const char* installDest = this->IEGen->GetDestination();
  379. cmOStringStream e;
  380. e << "INSTALL(EXPORT \""
  381. << this->IEGen->GetExportSet()->GetName()
  382. << "\") given absolute "
  383. << "DESTINATION \"" << installDest << "\" but the export "
  384. << "references an installation of target \""
  385. << itgen->GetTarget()->GetName() << "\" which has relative "
  386. << "DESTINATION \"" << itgen->GetDestination() << "\".";
  387. cmSystemTools::Error(e.str().c_str());
  388. }
  389. //----------------------------------------------------------------------------
  390. void
  391. cmExportInstallFileGenerator
  392. ::ComplainAboutMissingTarget(cmTarget* depender,
  393. cmTarget* dependee,
  394. int occurrences)
  395. {
  396. cmOStringStream e;
  397. e << "INSTALL(EXPORT \""
  398. << this->IEGen->GetExportSet()->GetName()
  399. << "\" ...) "
  400. << "includes target \"" << depender->GetName()
  401. << "\" which requires target \"" << dependee->GetName() << "\" ";
  402. if (occurrences == 0)
  403. {
  404. e << "that is not in the export set.";
  405. }
  406. else
  407. {
  408. e << "that is not in this export set, but " << occurrences
  409. << " times in others.";
  410. }
  411. cmSystemTools::Error(e.str().c_str());
  412. }