cmExportInstallFileGenerator.cxx 15 KB

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