cmExportFileGenerator.cxx 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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 "cmExportFileGenerator.h"
  11. #include "cmExportSet.h"
  12. #include "cmGeneratedFileStream.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmInstallExportGenerator.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmSystemTools.h"
  18. #include "cmTarget.h"
  19. #include "cmTargetExport.h"
  20. #include "cmVersion.h"
  21. #include "cmComputeLinkInformation.h"
  22. #include <cmsys/auto_ptr.hxx>
  23. #include <cmsys/FStream.hxx>
  24. #include <assert.h>
  25. //----------------------------------------------------------------------------
  26. cmExportFileGenerator::cmExportFileGenerator()
  27. {
  28. this->AppendMode = false;
  29. this->ExportOld = false;
  30. }
  31. //----------------------------------------------------------------------------
  32. void cmExportFileGenerator::AddConfiguration(const std::string& config)
  33. {
  34. this->Configurations.push_back(config);
  35. }
  36. //----------------------------------------------------------------------------
  37. void cmExportFileGenerator::SetExportFile(const char* mainFile)
  38. {
  39. this->MainImportFile = mainFile;
  40. this->FileDir =
  41. cmSystemTools::GetFilenamePath(this->MainImportFile);
  42. this->FileBase =
  43. cmSystemTools::GetFilenameWithoutLastExtension(this->MainImportFile);
  44. this->FileExt =
  45. cmSystemTools::GetFilenameLastExtension(this->MainImportFile);
  46. }
  47. //----------------------------------------------------------------------------
  48. const char* cmExportFileGenerator::GetMainExportFileName() const
  49. {
  50. return this->MainImportFile.c_str();
  51. }
  52. //----------------------------------------------------------------------------
  53. bool cmExportFileGenerator::GenerateImportFile()
  54. {
  55. // Open the output file to generate it.
  56. cmsys::auto_ptr<cmsys::ofstream> foutPtr;
  57. if(this->AppendMode)
  58. {
  59. // Open for append.
  60. cmsys::auto_ptr<cmsys::ofstream>
  61. ap(new cmsys::ofstream(this->MainImportFile.c_str(), std::ios::app));
  62. foutPtr = ap;
  63. }
  64. else
  65. {
  66. // Generate atomically and with copy-if-different.
  67. cmsys::auto_ptr<cmGeneratedFileStream>
  68. ap(new cmGeneratedFileStream(this->MainImportFile.c_str(), true));
  69. ap->SetCopyIfDifferent(true);
  70. foutPtr = ap;
  71. }
  72. if(!foutPtr.get() || !*foutPtr)
  73. {
  74. std::string se = cmSystemTools::GetLastSystemError();
  75. std::ostringstream e;
  76. e << "cannot write to file \"" << this->MainImportFile
  77. << "\": " << se;
  78. cmSystemTools::Error(e.str().c_str());
  79. return false;
  80. }
  81. std::ostream& os = *foutPtr;
  82. // Protect that file against use with older CMake versions.
  83. os << "# Generated by CMake " << cmVersion::GetCMakeVersion() << "\n\n";
  84. os << "if(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.5)\n"
  85. << " message(FATAL_ERROR \"CMake >= 2.6.0 required\")\n"
  86. << "endif()\n";
  87. // Isolate the file policy level.
  88. // We use 2.6 here instead of the current version because newer
  89. // versions of CMake should be able to export files imported by 2.6
  90. // until the import format changes.
  91. os << "cmake_policy(PUSH)\n"
  92. << "cmake_policy(VERSION 2.6)\n";
  93. // Start with the import file header.
  94. this->GenerateImportHeaderCode(os);
  95. // Create all the imported targets.
  96. bool result = this->GenerateMainFile(os);
  97. // End with the import file footer.
  98. this->GenerateImportFooterCode(os);
  99. os << "cmake_policy(POP)\n";
  100. return result;
  101. }
  102. //----------------------------------------------------------------------------
  103. void cmExportFileGenerator::GenerateImportConfig(std::ostream& os,
  104. const std::string& config,
  105. std::vector<std::string> &missingTargets)
  106. {
  107. // Construct the property configuration suffix.
  108. std::string suffix = "_";
  109. if(!config.empty())
  110. {
  111. suffix += cmSystemTools::UpperCase(config);
  112. }
  113. else
  114. {
  115. suffix += "NOCONFIG";
  116. }
  117. // Generate the per-config target information.
  118. this->GenerateImportTargetsConfig(os, config, suffix, missingTargets);
  119. }
  120. //----------------------------------------------------------------------------
  121. void cmExportFileGenerator::PopulateInterfaceProperty(
  122. const std::string& propName,
  123. cmTarget *target,
  124. ImportPropertyMap &properties)
  125. {
  126. const char *input = target->GetProperty(propName);
  127. if (input)
  128. {
  129. properties[propName] = input;
  130. }
  131. }
  132. //----------------------------------------------------------------------------
  133. void cmExportFileGenerator::PopulateInterfaceProperty(
  134. const std::string& propName,
  135. const std::string& outputName,
  136. cmTarget *target,
  137. cmGeneratorExpression::PreprocessContext preprocessRule,
  138. ImportPropertyMap &properties,
  139. std::vector<std::string> &missingTargets)
  140. {
  141. const char *input = target->GetProperty(propName);
  142. if (input)
  143. {
  144. if (!*input)
  145. {
  146. // Set to empty
  147. properties[outputName] = "";
  148. return;
  149. }
  150. std::string prepro = cmGeneratorExpression::Preprocess(input,
  151. preprocessRule);
  152. if (!prepro.empty())
  153. {
  154. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  155. missingTargets);
  156. properties[outputName] = prepro;
  157. }
  158. }
  159. }
  160. void cmExportFileGenerator::GenerateRequiredCMakeVersion(std::ostream& os,
  161. const char *versionString)
  162. {
  163. os << "if(CMAKE_VERSION VERSION_LESS " << versionString << ")\n"
  164. " message(FATAL_ERROR \"This file relies on consumers using "
  165. "CMake " << versionString << " or greater.\")\n"
  166. "endif()\n\n";
  167. }
  168. //----------------------------------------------------------------------------
  169. bool cmExportFileGenerator::PopulateInterfaceLinkLibrariesProperty(
  170. cmTarget *target,
  171. cmGeneratorExpression::PreprocessContext preprocessRule,
  172. ImportPropertyMap &properties,
  173. std::vector<std::string> &missingTargets)
  174. {
  175. if(!target->IsLinkable())
  176. {
  177. return false;
  178. }
  179. const char *input = target->GetProperty("INTERFACE_LINK_LIBRARIES");
  180. if (input)
  181. {
  182. std::string prepro = cmGeneratorExpression::Preprocess(input,
  183. preprocessRule);
  184. if (!prepro.empty())
  185. {
  186. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  187. missingTargets,
  188. ReplaceFreeTargets);
  189. properties["INTERFACE_LINK_LIBRARIES"] = prepro;
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. //----------------------------------------------------------------------------
  196. static bool isSubDirectory(const char* a, const char* b)
  197. {
  198. return (cmSystemTools::ComparePath(a, b) ||
  199. cmSystemTools::IsSubDirectory(a, b));
  200. }
  201. //----------------------------------------------------------------------------
  202. static bool checkInterfaceDirs(const std::string &prepro,
  203. cmTarget *target, const std::string& prop)
  204. {
  205. const char* installDir =
  206. target->GetMakefile()->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  207. const char* topSourceDir = target->GetMakefile()->GetHomeDirectory();
  208. const char* topBinaryDir = target->GetMakefile()->GetHomeOutputDirectory();
  209. std::vector<std::string> parts;
  210. cmGeneratorExpression::Split(prepro, parts);
  211. const bool inSourceBuild = strcmp(topSourceDir, topBinaryDir) == 0;
  212. bool hadFatalError = false;
  213. for(std::vector<std::string>::iterator li = parts.begin();
  214. li != parts.end(); ++li)
  215. {
  216. size_t genexPos = cmGeneratorExpression::Find(*li);
  217. if (genexPos == 0)
  218. {
  219. continue;
  220. }
  221. cmake::MessageType messageType = cmake::FATAL_ERROR;
  222. std::ostringstream e;
  223. if (genexPos != std::string::npos)
  224. {
  225. if (prop == "INTERFACE_INCLUDE_DIRECTORIES")
  226. {
  227. switch (target->GetPolicyStatusCMP0041())
  228. {
  229. case cmPolicies::WARN:
  230. messageType = cmake::WARNING;
  231. e << target->GetMakefile()->GetPolicies()
  232. ->GetPolicyWarning(cmPolicies::CMP0041) << "\n";
  233. break;
  234. case cmPolicies::OLD:
  235. continue;
  236. case cmPolicies::REQUIRED_IF_USED:
  237. case cmPolicies::REQUIRED_ALWAYS:
  238. case cmPolicies::NEW:
  239. hadFatalError = true;
  240. break; // Issue fatal message.
  241. }
  242. }
  243. else
  244. {
  245. hadFatalError = true;
  246. }
  247. }
  248. if (cmHasLiteralPrefix(li->c_str(), "${_IMPORT_PREFIX}"))
  249. {
  250. continue;
  251. }
  252. if (!cmSystemTools::FileIsFullPath(li->c_str()))
  253. {
  254. e << "Target \"" << target->GetName() << "\" " << prop <<
  255. " property contains relative path:\n"
  256. " \"" << *li << "\"";
  257. target->GetMakefile()->IssueMessage(messageType, e.str());
  258. }
  259. bool inBinary = isSubDirectory(li->c_str(), topBinaryDir);
  260. bool inSource = isSubDirectory(li->c_str(), topSourceDir);
  261. if (isSubDirectory(li->c_str(), installDir))
  262. {
  263. // The include directory is inside the install tree. If the
  264. // install tree is not inside the source tree or build tree then
  265. // fall through to the checks below that the include directory is not
  266. // also inside the source tree or build tree.
  267. bool shouldContinue =
  268. (!inBinary || isSubDirectory(installDir, topBinaryDir)) &&
  269. (!inSource || isSubDirectory(installDir, topSourceDir));
  270. if (prop == "INTERFACE_INCLUDE_DIRECTORIES")
  271. {
  272. if (!shouldContinue)
  273. {
  274. switch(target->GetPolicyStatusCMP0052())
  275. {
  276. case cmPolicies::WARN:
  277. {
  278. std::ostringstream s;
  279. s << target->GetMakefile()->GetPolicies()
  280. ->GetPolicyWarning(cmPolicies::CMP0052) << "\n";
  281. s << "Directory:\n \"" << *li << "\"\nin "
  282. "INTERFACE_INCLUDE_DIRECTORIES of target \""
  283. << target->GetName() << "\" is a subdirectory of the install "
  284. "directory:\n \"" << installDir << "\"\nhowever it is also "
  285. "a subdirectory of the " << (inBinary ? "build" : "source")
  286. << " tree:\n \"" << (inBinary ? topBinaryDir : topSourceDir)
  287. << "\"" << std::endl;
  288. target->GetMakefile()->IssueMessage(cmake::AUTHOR_WARNING,
  289. s.str());
  290. }
  291. case cmPolicies::OLD:
  292. shouldContinue = true;
  293. break;
  294. case cmPolicies::REQUIRED_ALWAYS:
  295. case cmPolicies::REQUIRED_IF_USED:
  296. case cmPolicies::NEW:
  297. break;
  298. }
  299. }
  300. }
  301. if (shouldContinue)
  302. {
  303. continue;
  304. }
  305. }
  306. if (inBinary)
  307. {
  308. e << "Target \"" << target->GetName() << "\" " << prop <<
  309. " property contains path:\n"
  310. " \"" << *li << "\"\nwhich is prefixed in the build directory.";
  311. target->GetMakefile()->IssueMessage(messageType, e.str());
  312. }
  313. if (!inSourceBuild)
  314. {
  315. if (inSource)
  316. {
  317. e << "Target \"" << target->GetName() << "\" " << prop <<
  318. " property contains path:\n"
  319. " \"" << *li << "\"\nwhich is prefixed in the source directory.";
  320. target->GetMakefile()->IssueMessage(messageType, e.str());
  321. }
  322. }
  323. }
  324. return !hadFatalError;
  325. }
  326. //----------------------------------------------------------------------------
  327. static void prefixItems(std::string &exportDirs)
  328. {
  329. std::vector<std::string> entries;
  330. cmGeneratorExpression::Split(exportDirs, entries);
  331. exportDirs = "";
  332. const char *sep = "";
  333. for(std::vector<std::string>::const_iterator ei = entries.begin();
  334. ei != entries.end(); ++ei)
  335. {
  336. exportDirs += sep;
  337. sep = ";";
  338. if (!cmSystemTools::FileIsFullPath(ei->c_str())
  339. && ei->find("${_IMPORT_PREFIX}") == std::string::npos)
  340. {
  341. exportDirs += "${_IMPORT_PREFIX}/";
  342. }
  343. exportDirs += *ei;
  344. }
  345. }
  346. //----------------------------------------------------------------------------
  347. void cmExportFileGenerator::PopulateSourcesInterface(
  348. cmTargetExport *tei,
  349. cmGeneratorExpression::PreprocessContext preprocessRule,
  350. ImportPropertyMap &properties,
  351. std::vector<std::string> &missingTargets)
  352. {
  353. cmTarget *target = tei->Target;
  354. assert(preprocessRule == cmGeneratorExpression::InstallInterface);
  355. const char *propName = "INTERFACE_SOURCES";
  356. const char *input = target->GetProperty(propName);
  357. if (!input)
  358. {
  359. return;
  360. }
  361. if (!*input)
  362. {
  363. properties[propName] = "";
  364. return;
  365. }
  366. std::string prepro = cmGeneratorExpression::Preprocess(input,
  367. preprocessRule,
  368. true);
  369. if (!prepro.empty())
  370. {
  371. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  372. missingTargets);
  373. if (!checkInterfaceDirs(prepro, target, propName))
  374. {
  375. return;
  376. }
  377. properties[propName] = prepro;
  378. }
  379. }
  380. //----------------------------------------------------------------------------
  381. void cmExportFileGenerator::PopulateIncludeDirectoriesInterface(
  382. cmTargetExport *tei,
  383. cmGeneratorExpression::PreprocessContext preprocessRule,
  384. ImportPropertyMap &properties,
  385. std::vector<std::string> &missingTargets)
  386. {
  387. cmTarget *target = tei->Target;
  388. assert(preprocessRule == cmGeneratorExpression::InstallInterface);
  389. const char *propName = "INTERFACE_INCLUDE_DIRECTORIES";
  390. const char *input = target->GetProperty(propName);
  391. cmGeneratorExpression ge;
  392. std::string dirs = cmGeneratorExpression::Preprocess(
  393. tei->InterfaceIncludeDirectories,
  394. preprocessRule,
  395. true);
  396. this->ReplaceInstallPrefix(dirs);
  397. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(dirs);
  398. std::string exportDirs = cge->Evaluate(target->GetMakefile(), "",
  399. false, target);
  400. if (cge->GetHadContextSensitiveCondition())
  401. {
  402. cmMakefile* mf = target->GetMakefile();
  403. std::ostringstream e;
  404. e << "Target \"" << target->GetName() << "\" is installed with "
  405. "INCLUDES DESTINATION set to a context sensitive path. Paths which "
  406. "depend on the configuration, policy values or the link interface are "
  407. "not supported. Consider using target_include_directories instead.";
  408. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  409. return;
  410. }
  411. if (!input && exportDirs.empty())
  412. {
  413. return;
  414. }
  415. if ((input && !*input) && exportDirs.empty())
  416. {
  417. // Set to empty
  418. properties[propName] = "";
  419. return;
  420. }
  421. prefixItems(exportDirs);
  422. std::string includes = (input?input:"");
  423. const char* sep = input ? ";" : "";
  424. includes += sep + exportDirs;
  425. std::string prepro = cmGeneratorExpression::Preprocess(includes,
  426. preprocessRule,
  427. true);
  428. if (!prepro.empty())
  429. {
  430. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  431. missingTargets);
  432. if (!checkInterfaceDirs(prepro, target, propName))
  433. {
  434. return;
  435. }
  436. properties[propName] = prepro;
  437. }
  438. }
  439. //----------------------------------------------------------------------------
  440. void cmExportFileGenerator::PopulateInterfaceProperty(
  441. const std::string& propName,
  442. cmTarget *target,
  443. cmGeneratorExpression::PreprocessContext preprocessRule,
  444. ImportPropertyMap &properties,
  445. std::vector<std::string> &missingTargets)
  446. {
  447. this->PopulateInterfaceProperty(propName, propName, target, preprocessRule,
  448. properties, missingTargets);
  449. }
  450. //----------------------------------------------------------------------------
  451. void getPropertyContents(cmTarget const* tgt, const std::string& prop,
  452. std::set<std::string> &ifaceProperties)
  453. {
  454. const char *p = tgt->GetProperty(prop);
  455. if (!p)
  456. {
  457. return;
  458. }
  459. std::vector<std::string> content;
  460. cmSystemTools::ExpandListArgument(p, content);
  461. ifaceProperties.insert(content.begin(), content.end());
  462. }
  463. //----------------------------------------------------------------------------
  464. void getCompatibleInterfaceProperties(cmTarget *target,
  465. std::set<std::string> &ifaceProperties,
  466. const std::string& config)
  467. {
  468. cmComputeLinkInformation *info = target->GetLinkInformation(config);
  469. if (!info)
  470. {
  471. cmMakefile* mf = target->GetMakefile();
  472. std::ostringstream e;
  473. e << "Exporting the target \"" << target->GetName() << "\" is not "
  474. "allowed since its linker language cannot be determined";
  475. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  476. return;
  477. }
  478. const cmComputeLinkInformation::ItemVector &deps = info->GetItems();
  479. for(cmComputeLinkInformation::ItemVector::const_iterator li =
  480. deps.begin();
  481. li != deps.end(); ++li)
  482. {
  483. if (!li->Target)
  484. {
  485. continue;
  486. }
  487. getPropertyContents(li->Target,
  488. "COMPATIBLE_INTERFACE_BOOL",
  489. ifaceProperties);
  490. getPropertyContents(li->Target,
  491. "COMPATIBLE_INTERFACE_STRING",
  492. ifaceProperties);
  493. getPropertyContents(li->Target,
  494. "COMPATIBLE_INTERFACE_NUMBER_MIN",
  495. ifaceProperties);
  496. getPropertyContents(li->Target,
  497. "COMPATIBLE_INTERFACE_NUMBER_MAX",
  498. ifaceProperties);
  499. }
  500. }
  501. //----------------------------------------------------------------------------
  502. void cmExportFileGenerator::PopulateCompatibleInterfaceProperties(
  503. cmTarget *target,
  504. ImportPropertyMap &properties)
  505. {
  506. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_BOOL",
  507. target, properties);
  508. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_STRING",
  509. target, properties);
  510. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_NUMBER_MIN",
  511. target, properties);
  512. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_NUMBER_MAX",
  513. target, properties);
  514. std::set<std::string> ifaceProperties;
  515. getPropertyContents(target, "COMPATIBLE_INTERFACE_BOOL", ifaceProperties);
  516. getPropertyContents(target, "COMPATIBLE_INTERFACE_STRING", ifaceProperties);
  517. getPropertyContents(target, "COMPATIBLE_INTERFACE_NUMBER_MIN",
  518. ifaceProperties);
  519. getPropertyContents(target, "COMPATIBLE_INTERFACE_NUMBER_MAX",
  520. ifaceProperties);
  521. if (target->GetType() != cmTarget::INTERFACE_LIBRARY)
  522. {
  523. getCompatibleInterfaceProperties(target, ifaceProperties, "");
  524. std::vector<std::string> configNames;
  525. target->GetMakefile()->GetConfigurations(configNames);
  526. for (std::vector<std::string>::const_iterator ci = configNames.begin();
  527. ci != configNames.end(); ++ci)
  528. {
  529. getCompatibleInterfaceProperties(target, ifaceProperties, *ci);
  530. }
  531. }
  532. for (std::set<std::string>::const_iterator it = ifaceProperties.begin();
  533. it != ifaceProperties.end(); ++it)
  534. {
  535. this->PopulateInterfaceProperty("INTERFACE_" + *it,
  536. target, properties);
  537. }
  538. }
  539. //----------------------------------------------------------------------------
  540. void cmExportFileGenerator::GenerateInterfaceProperties(cmTarget const* target,
  541. std::ostream& os,
  542. const ImportPropertyMap &properties)
  543. {
  544. if (!properties.empty())
  545. {
  546. std::string targetName = this->Namespace;
  547. targetName += target->GetExportName();
  548. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  549. for(ImportPropertyMap::const_iterator pi = properties.begin();
  550. pi != properties.end(); ++pi)
  551. {
  552. os << " " << pi->first << " \"" << pi->second << "\"\n";
  553. }
  554. os << ")\n\n";
  555. }
  556. }
  557. //----------------------------------------------------------------------------
  558. bool
  559. cmExportFileGenerator::AddTargetNamespace(std::string &input,
  560. cmTarget* target,
  561. std::vector<std::string> &missingTargets)
  562. {
  563. cmMakefile *mf = target->GetMakefile();
  564. cmTarget *tgt = mf->FindTargetToUse(input);
  565. if (!tgt)
  566. {
  567. return false;
  568. }
  569. if(tgt->IsImported())
  570. {
  571. return true;
  572. }
  573. if(this->ExportedTargets.find(tgt) != this->ExportedTargets.end())
  574. {
  575. input = this->Namespace + tgt->GetExportName();
  576. }
  577. else
  578. {
  579. std::string namespacedTarget;
  580. this->HandleMissingTarget(namespacedTarget, missingTargets,
  581. mf, target, tgt);
  582. if (!namespacedTarget.empty())
  583. {
  584. input = namespacedTarget;
  585. }
  586. }
  587. return true;
  588. }
  589. //----------------------------------------------------------------------------
  590. void
  591. cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
  592. std::string &input,
  593. cmTarget* target,
  594. std::vector<std::string> &missingTargets,
  595. FreeTargetsReplace replace)
  596. {
  597. if (replace == NoReplaceFreeTargets)
  598. {
  599. this->ResolveTargetsInGeneratorExpression(input, target, missingTargets);
  600. return;
  601. }
  602. std::vector<std::string> parts;
  603. cmGeneratorExpression::Split(input, parts);
  604. std::string sep;
  605. input = "";
  606. for(std::vector<std::string>::iterator li = parts.begin();
  607. li != parts.end(); ++li)
  608. {
  609. if (cmGeneratorExpression::Find(*li) == std::string::npos)
  610. {
  611. this->AddTargetNamespace(*li, target, missingTargets);
  612. }
  613. else
  614. {
  615. this->ResolveTargetsInGeneratorExpression(
  616. *li,
  617. target,
  618. missingTargets);
  619. }
  620. input += sep + *li;
  621. sep = ";";
  622. }
  623. }
  624. //----------------------------------------------------------------------------
  625. void
  626. cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
  627. std::string &input,
  628. cmTarget* target,
  629. std::vector<std::string> &missingTargets)
  630. {
  631. std::string::size_type pos = 0;
  632. std::string::size_type lastPos = pos;
  633. cmMakefile *mf = target->GetMakefile();
  634. while((pos = input.find("$<TARGET_PROPERTY:", lastPos)) != input.npos)
  635. {
  636. std::string::size_type nameStartPos = pos +
  637. sizeof("$<TARGET_PROPERTY:") - 1;
  638. std::string::size_type closePos = input.find(">", nameStartPos);
  639. std::string::size_type commaPos = input.find(",", nameStartPos);
  640. std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
  641. if (commaPos == input.npos // Implied 'this' target
  642. || closePos == input.npos // Imcomplete expression.
  643. || closePos < commaPos // Implied 'this' target
  644. || nextOpenPos < commaPos) // Non-literal
  645. {
  646. lastPos = nameStartPos;
  647. continue;
  648. }
  649. std::string targetName = input.substr(nameStartPos,
  650. commaPos - nameStartPos);
  651. if (this->AddTargetNamespace(targetName, target, missingTargets))
  652. {
  653. input.replace(nameStartPos, commaPos - nameStartPos, targetName);
  654. }
  655. lastPos = nameStartPos + targetName.size() + 1;
  656. }
  657. std::string errorString;
  658. pos = 0;
  659. lastPos = pos;
  660. while((pos = input.find("$<TARGET_NAME:", lastPos)) != input.npos)
  661. {
  662. std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1;
  663. std::string::size_type endPos = input.find(">", nameStartPos);
  664. if (endPos == input.npos)
  665. {
  666. errorString = "$<TARGET_NAME:...> expression incomplete";
  667. break;
  668. }
  669. std::string targetName = input.substr(nameStartPos,
  670. endPos - nameStartPos);
  671. if(targetName.find("$<") != input.npos)
  672. {
  673. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  674. "literal.";
  675. break;
  676. }
  677. if (!this->AddTargetNamespace(targetName, target, missingTargets))
  678. {
  679. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  680. "reachable target.";
  681. break;
  682. }
  683. input.replace(pos, endPos - pos + 1, targetName);
  684. lastPos = endPos;
  685. }
  686. this->ReplaceInstallPrefix(input);
  687. if (!errorString.empty())
  688. {
  689. mf->IssueMessage(cmake::FATAL_ERROR, errorString);
  690. }
  691. }
  692. //----------------------------------------------------------------------------
  693. void
  694. cmExportFileGenerator::ReplaceInstallPrefix(std::string &)
  695. {
  696. // Do nothing
  697. }
  698. //----------------------------------------------------------------------------
  699. void
  700. cmExportFileGenerator
  701. ::SetImportLinkInterface(const std::string& config, std::string const& suffix,
  702. cmGeneratorExpression::PreprocessContext preprocessRule,
  703. cmTarget* target, ImportPropertyMap& properties,
  704. std::vector<std::string>& missingTargets)
  705. {
  706. // Add the transitive link dependencies for this configuration.
  707. cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
  708. target);
  709. if (!iface)
  710. {
  711. return;
  712. }
  713. if (iface->ImplementationIsInterface)
  714. {
  715. // Policy CMP0022 must not be NEW.
  716. this->SetImportLinkProperty(suffix, target,
  717. "IMPORTED_LINK_INTERFACE_LIBRARIES",
  718. iface->Libraries, properties, missingTargets);
  719. return;
  720. }
  721. const char *propContent;
  722. if (const char *prop_suffixed = target->GetProperty(
  723. "LINK_INTERFACE_LIBRARIES" + suffix))
  724. {
  725. propContent = prop_suffixed;
  726. }
  727. else if (const char *prop = target->GetProperty(
  728. "LINK_INTERFACE_LIBRARIES"))
  729. {
  730. propContent = prop;
  731. }
  732. else
  733. {
  734. return;
  735. }
  736. const bool newCMP0022Behavior =
  737. target->GetPolicyStatusCMP0022() != cmPolicies::WARN
  738. && target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  739. if(newCMP0022Behavior && !this->ExportOld)
  740. {
  741. cmMakefile *mf = target->GetMakefile();
  742. std::ostringstream e;
  743. e << "Target \"" << target->GetName() << "\" has policy CMP0022 enabled, "
  744. "but also has old-style LINK_INTERFACE_LIBRARIES properties "
  745. "populated, but it was exported without the "
  746. "EXPORT_LINK_INTERFACE_LIBRARIES to export the old-style properties";
  747. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  748. return;
  749. }
  750. if (!*propContent)
  751. {
  752. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = "";
  753. return;
  754. }
  755. std::string prepro = cmGeneratorExpression::Preprocess(propContent,
  756. preprocessRule);
  757. if (!prepro.empty())
  758. {
  759. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  760. missingTargets,
  761. ReplaceFreeTargets);
  762. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = prepro;
  763. }
  764. }
  765. //----------------------------------------------------------------------------
  766. void
  767. cmExportFileGenerator
  768. ::SetImportDetailProperties(const std::string& config,
  769. std::string const& suffix,
  770. cmTarget* target, ImportPropertyMap& properties,
  771. std::vector<std::string>& missingTargets
  772. )
  773. {
  774. // Get the makefile in which to lookup target information.
  775. cmMakefile* mf = target->GetMakefile();
  776. // Add the soname for unix shared libraries.
  777. if(target->GetType() == cmTarget::SHARED_LIBRARY ||
  778. target->GetType() == cmTarget::MODULE_LIBRARY)
  779. {
  780. // Check whether this is a DLL platform.
  781. bool dll_platform =
  782. (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
  783. if(!dll_platform)
  784. {
  785. std::string prop;
  786. std::string value;
  787. if(target->HasSOName(config))
  788. {
  789. if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  790. {
  791. value = this->InstallNameDir(target, config);
  792. }
  793. prop = "IMPORTED_SONAME";
  794. value += target->GetSOName(config);
  795. }
  796. else
  797. {
  798. prop = "IMPORTED_NO_SONAME";
  799. value = "TRUE";
  800. }
  801. prop += suffix;
  802. properties[prop] = value;
  803. }
  804. }
  805. // Add the transitive link dependencies for this configuration.
  806. if(cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
  807. target))
  808. {
  809. this->SetImportLinkProperty(suffix, target,
  810. "IMPORTED_LINK_INTERFACE_LANGUAGES",
  811. iface->Languages, properties, missingTargets);
  812. std::vector<std::string> dummy;
  813. this->SetImportLinkProperty(suffix, target,
  814. "IMPORTED_LINK_DEPENDENT_LIBRARIES",
  815. iface->SharedDeps, properties, dummy);
  816. if(iface->Multiplicity > 0)
  817. {
  818. std::string prop = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  819. prop += suffix;
  820. std::ostringstream m;
  821. m << iface->Multiplicity;
  822. properties[prop] = m.str();
  823. }
  824. }
  825. }
  826. //----------------------------------------------------------------------------
  827. template <typename T>
  828. void
  829. cmExportFileGenerator
  830. ::SetImportLinkProperty(std::string const& suffix,
  831. cmTarget* target,
  832. const std::string& propName,
  833. std::vector<T> const& entries,
  834. ImportPropertyMap& properties,
  835. std::vector<std::string>& missingTargets
  836. )
  837. {
  838. // Skip the property if there are no entries.
  839. if(entries.empty())
  840. {
  841. return;
  842. }
  843. // Construct the property value.
  844. std::string link_entries;
  845. const char* sep = "";
  846. for(typename std::vector<T>::const_iterator li = entries.begin();
  847. li != entries.end(); ++li)
  848. {
  849. // Separate this from the previous entry.
  850. link_entries += sep;
  851. sep = ";";
  852. std::string temp = *li;
  853. this->AddTargetNamespace(temp, target, missingTargets);
  854. link_entries += temp;
  855. }
  856. // Store the property.
  857. std::string prop = propName;
  858. prop += suffix;
  859. properties[prop] = link_entries;
  860. }
  861. //----------------------------------------------------------------------------
  862. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  863. const std::string& config)
  864. {
  865. os << "#----------------------------------------------------------------\n"
  866. << "# Generated CMake target import file";
  867. if(!config.empty())
  868. {
  869. os << " for configuration \"" << config << "\".\n";
  870. }
  871. else
  872. {
  873. os << ".\n";
  874. }
  875. os << "#----------------------------------------------------------------\n"
  876. << "\n";
  877. this->GenerateImportVersionCode(os);
  878. }
  879. //----------------------------------------------------------------------------
  880. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  881. {
  882. os << "# Commands beyond this point should not need to know the version.\n"
  883. << "set(CMAKE_IMPORT_FILE_VERSION)\n";
  884. }
  885. //----------------------------------------------------------------------------
  886. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  887. {
  888. // Store an import file format version. This will let us change the
  889. // format later while still allowing old import files to work.
  890. os << "# Commands may need to know the format version.\n"
  891. << "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
  892. << "\n";
  893. }
  894. //----------------------------------------------------------------------------
  895. void cmExportFileGenerator::GenerateExpectedTargetsCode(std::ostream& os,
  896. const std::string &expectedTargets)
  897. {
  898. os << "# Protect against multiple inclusion, which would fail when already "
  899. "imported targets are added once more.\n"
  900. "set(_targetsDefined)\n"
  901. "set(_targetsNotDefined)\n"
  902. "set(_expectedTargets)\n"
  903. "foreach(_expectedTarget " << expectedTargets << ")\n"
  904. " list(APPEND _expectedTargets ${_expectedTarget})\n"
  905. " if(NOT TARGET ${_expectedTarget})\n"
  906. " list(APPEND _targetsNotDefined ${_expectedTarget})\n"
  907. " endif()\n"
  908. " if(TARGET ${_expectedTarget})\n"
  909. " list(APPEND _targetsDefined ${_expectedTarget})\n"
  910. " endif()\n"
  911. "endforeach()\n"
  912. "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  913. " set(CMAKE_IMPORT_FILE_VERSION)\n"
  914. " cmake_policy(POP)\n"
  915. " return()\n"
  916. "endif()\n"
  917. "if(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  918. " message(FATAL_ERROR \"Some (but not all) targets in this export "
  919. "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
  920. "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
  921. "endif()\n"
  922. "unset(_targetsDefined)\n"
  923. "unset(_targetsNotDefined)\n"
  924. "unset(_expectedTargets)\n"
  925. "\n\n";
  926. }
  927. //----------------------------------------------------------------------------
  928. void
  929. cmExportFileGenerator
  930. ::GenerateImportTargetCode(std::ostream& os, cmTarget const* target)
  931. {
  932. // Construct the imported target name.
  933. std::string targetName = this->Namespace;
  934. targetName += target->GetExportName();
  935. // Create the imported target.
  936. os << "# Create imported target " << targetName << "\n";
  937. switch(target->GetType())
  938. {
  939. case cmTarget::EXECUTABLE:
  940. os << "add_executable(" << targetName << " IMPORTED)\n";
  941. break;
  942. case cmTarget::STATIC_LIBRARY:
  943. os << "add_library(" << targetName << " STATIC IMPORTED)\n";
  944. break;
  945. case cmTarget::SHARED_LIBRARY:
  946. os << "add_library(" << targetName << " SHARED IMPORTED)\n";
  947. break;
  948. case cmTarget::MODULE_LIBRARY:
  949. os << "add_library(" << targetName << " MODULE IMPORTED)\n";
  950. break;
  951. case cmTarget::UNKNOWN_LIBRARY:
  952. os << "add_library(" << targetName << " UNKNOWN IMPORTED)\n";
  953. break;
  954. case cmTarget::INTERFACE_LIBRARY:
  955. os << "add_library(" << targetName << " INTERFACE IMPORTED)\n";
  956. break;
  957. default: // should never happen
  958. break;
  959. }
  960. // Mark the imported executable if it has exports.
  961. if(target->IsExecutableWithExports())
  962. {
  963. os << "set_property(TARGET " << targetName
  964. << " PROPERTY ENABLE_EXPORTS 1)\n";
  965. }
  966. // Mark the imported library if it is a framework.
  967. if(target->IsFrameworkOnApple())
  968. {
  969. os << "set_property(TARGET " << targetName
  970. << " PROPERTY FRAMEWORK 1)\n";
  971. }
  972. // Mark the imported executable if it is an application bundle.
  973. if(target->IsAppBundleOnApple())
  974. {
  975. os << "set_property(TARGET " << targetName
  976. << " PROPERTY MACOSX_BUNDLE 1)\n";
  977. }
  978. if (target->IsCFBundleOnApple())
  979. {
  980. os << "set_property(TARGET " << targetName
  981. << " PROPERTY BUNDLE 1)\n";
  982. }
  983. os << "\n";
  984. }
  985. //----------------------------------------------------------------------------
  986. void
  987. cmExportFileGenerator
  988. ::GenerateImportPropertyCode(std::ostream& os, const std::string& config,
  989. cmTarget const* target,
  990. ImportPropertyMap const& properties)
  991. {
  992. // Construct the imported target name.
  993. std::string targetName = this->Namespace;
  994. targetName += target->GetExportName();
  995. // Set the import properties.
  996. os << "# Import target \"" << targetName << "\" for configuration \""
  997. << config << "\"\n";
  998. os << "set_property(TARGET " << targetName
  999. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  1000. if(!config.empty())
  1001. {
  1002. os << cmSystemTools::UpperCase(config);
  1003. }
  1004. else
  1005. {
  1006. os << "NOCONFIG";
  1007. }
  1008. os << ")\n";
  1009. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  1010. for(ImportPropertyMap::const_iterator pi = properties.begin();
  1011. pi != properties.end(); ++pi)
  1012. {
  1013. os << " " << pi->first << " \"" << pi->second << "\"\n";
  1014. }
  1015. os << " )\n"
  1016. << "\n";
  1017. }
  1018. //----------------------------------------------------------------------------
  1019. void cmExportFileGenerator::GenerateMissingTargetsCheckCode(std::ostream& os,
  1020. const std::vector<std::string>& missingTargets)
  1021. {
  1022. if (missingTargets.empty())
  1023. {
  1024. os << "# This file does not depend on other imported targets which have\n"
  1025. "# been exported from the same project but in a separate "
  1026. "export set.\n\n";
  1027. return;
  1028. }
  1029. os << "# Make sure the targets which have been exported in some other \n"
  1030. "# export set exist.\n"
  1031. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  1032. "foreach(_target ";
  1033. std::set<std::string> emitted;
  1034. for(unsigned int i=0; i<missingTargets.size(); ++i)
  1035. {
  1036. if (emitted.insert(missingTargets[i]).second)
  1037. {
  1038. os << "\"" << missingTargets[i] << "\" ";
  1039. }
  1040. }
  1041. os << ")\n"
  1042. " if(NOT TARGET \"${_target}\" )\n"
  1043. " set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets \""
  1044. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets} ${_target}\")"
  1045. "\n"
  1046. " endif()\n"
  1047. "endforeach()\n"
  1048. "\n"
  1049. "if(DEFINED ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  1050. " if(CMAKE_FIND_PACKAGE_NAME)\n"
  1051. " set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  1052. " set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  1053. "\"The following imported targets are "
  1054. "referenced, but are missing: "
  1055. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  1056. " else()\n"
  1057. " message(FATAL_ERROR \"The following imported targets are "
  1058. "referenced, but are missing: "
  1059. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  1060. " endif()\n"
  1061. "endif()\n"
  1062. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  1063. "\n";
  1064. }
  1065. //----------------------------------------------------------------------------
  1066. void
  1067. cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
  1068. {
  1069. // Add code which verifies at cmake time that the file which is being
  1070. // imported actually exists on disk. This should in theory always be theory
  1071. // case, but still when packages are split into normal and development
  1072. // packages this might get broken (e.g. the Config.cmake could be part of
  1073. // the non-development package, something similar happened to me without
  1074. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  1075. // but the development package was not installed.).
  1076. os << "# Loop over all imported files and verify that they actually exist\n"
  1077. "foreach(target ${_IMPORT_CHECK_TARGETS} )\n"
  1078. " foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
  1079. " if(NOT EXISTS \"${file}\" )\n"
  1080. " message(FATAL_ERROR \"The imported target \\\"${target}\\\""
  1081. " references the file\n"
  1082. " \\\"${file}\\\"\n"
  1083. "but this file does not exist. Possible reasons include:\n"
  1084. "* The file was deleted, renamed, or moved to another location.\n"
  1085. "* An install or uninstall procedure did not complete successfully.\n"
  1086. "* The installation package was faulty and contained\n"
  1087. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  1088. "but not all the files it references.\n"
  1089. "\")\n"
  1090. " endif()\n"
  1091. " endforeach()\n"
  1092. " unset(_IMPORT_CHECK_FILES_FOR_${target})\n"
  1093. "endforeach()\n"
  1094. "unset(_IMPORT_CHECK_TARGETS)\n"
  1095. "\n";
  1096. }
  1097. //----------------------------------------------------------------------------
  1098. void
  1099. cmExportFileGenerator
  1100. ::GenerateImportedFileChecksCode(std::ostream& os, cmTarget* target,
  1101. ImportPropertyMap const& properties,
  1102. const std::set<std::string>& importedLocations)
  1103. {
  1104. // Construct the imported target name.
  1105. std::string targetName = this->Namespace;
  1106. targetName += target->GetExportName();
  1107. os << "list(APPEND _IMPORT_CHECK_TARGETS " << targetName << " )\n"
  1108. "list(APPEND _IMPORT_CHECK_FILES_FOR_" << targetName << " ";
  1109. for(std::set<std::string>::const_iterator li = importedLocations.begin();
  1110. li != importedLocations.end();
  1111. ++li)
  1112. {
  1113. ImportPropertyMap::const_iterator pi = properties.find(*li);
  1114. if (pi != properties.end())
  1115. {
  1116. os << "\"" << pi->second << "\" ";
  1117. }
  1118. }
  1119. os << ")\n\n";
  1120. }