cmExportInstallFileGenerator.cxx 14 KB

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