cmMakefileLibraryTargetGenerator.cxx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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 "cmMakefileLibraryTargetGenerator.h"
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmGlobalUnixMakefileGenerator3.h"
  13. #include "cmLocalUnixMakefileGenerator3.h"
  14. #include "cmMakefile.h"
  15. #include "cmSourceFile.h"
  16. #include "cmTarget.h"
  17. #include "cmake.h"
  18. #include <memory> // auto_ptr
  19. //----------------------------------------------------------------------------
  20. cmMakefileLibraryTargetGenerator
  21. ::cmMakefileLibraryTargetGenerator(cmTarget* target):
  22. cmMakefileTargetGenerator(target)
  23. {
  24. if(this->Target->IsCFBundleOnApple())
  25. {
  26. target->SetProperty("PREFIX", "");
  27. target->SetProperty("SUFFIX", "");
  28. }
  29. this->CustomCommandDriver = OnDepends;
  30. this->Target->GetLibraryNames(
  31. this->TargetNameOut, this->TargetNameSO, this->TargetNameReal,
  32. this->TargetNameImport, this->TargetNamePDB, this->ConfigName);
  33. if(this->Target->IsFrameworkOnApple())
  34. {
  35. this->FrameworkVersion = this->Target->GetFrameworkVersion();
  36. this->MacContentDirectory = this->Target->GetDirectory(this->ConfigName);
  37. this->MacContentDirectory += "/";
  38. this->MacContentDirectory += this->TargetNameOut;
  39. this->MacContentDirectory += ".framework/Versions/";
  40. this->MacContentDirectory += this->FrameworkVersion;
  41. this->MacContentDirectory += "/";
  42. }
  43. else if(this->Target->IsCFBundleOnApple())
  44. {
  45. this->MacContentDirectory = this->Target->GetDirectory(this->ConfigName);
  46. this->MacContentDirectory += "/";
  47. this->MacContentDirectory += this->TargetNameOut;
  48. this->MacContentDirectory += ".";
  49. const char *ext = this->Target->GetProperty("BUNDLE_EXTENSION");
  50. if (!ext)
  51. {
  52. ext = "bundle";
  53. }
  54. this->MacContentDirectory += ext;
  55. this->MacContentDirectory += "/Contents/";
  56. }
  57. }
  58. //----------------------------------------------------------------------------
  59. void cmMakefileLibraryTargetGenerator::WriteRuleFiles()
  60. {
  61. // create the build.make file and directory, put in the common blocks
  62. this->CreateRuleFile();
  63. // write rules used to help build object files
  64. this->WriteCommonCodeRules();
  65. // write the per-target per-language flags
  66. this->WriteTargetLanguageFlags();
  67. // write in rules for object files and custom commands
  68. this->WriteTargetBuildRules();
  69. // write the link rules
  70. // Write the rule for this target type.
  71. switch(this->Target->GetType())
  72. {
  73. case cmTarget::STATIC_LIBRARY:
  74. this->WriteStaticLibraryRules();
  75. break;
  76. case cmTarget::SHARED_LIBRARY:
  77. this->WriteSharedLibraryRules(false);
  78. if(this->Target->NeedRelinkBeforeInstall(this->ConfigName))
  79. {
  80. // Write rules to link an installable version of the target.
  81. this->WriteSharedLibraryRules(true);
  82. }
  83. break;
  84. case cmTarget::MODULE_LIBRARY:
  85. this->WriteModuleLibraryRules(false);
  86. if(this->Target->NeedRelinkBeforeInstall(this->ConfigName))
  87. {
  88. // Write rules to link an installable version of the target.
  89. this->WriteModuleLibraryRules(true);
  90. }
  91. break;
  92. case cmTarget::OBJECT_LIBRARY:
  93. this->WriteObjectLibraryRules();
  94. break;
  95. default:
  96. // If language is not known, this is an error.
  97. cmSystemTools::Error("Unknown Library Type");
  98. break;
  99. }
  100. // Write the requires target.
  101. this->WriteTargetRequiresRules();
  102. // Write clean target
  103. this->WriteTargetCleanRules();
  104. // Write the dependency generation rule. This must be done last so
  105. // that multiple output pair information is available.
  106. this->WriteTargetDependRules();
  107. // close the streams
  108. this->CloseFileStreams();
  109. }
  110. //----------------------------------------------------------------------------
  111. void cmMakefileLibraryTargetGenerator::WriteObjectLibraryRules()
  112. {
  113. std::vector<std::string> commands;
  114. std::vector<std::string> depends;
  115. // Add post-build rules.
  116. this->LocalGenerator->
  117. AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
  118. this->Target);
  119. // Depend on the object files.
  120. this->AppendObjectDepends(depends);
  121. // Write the rule.
  122. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  123. this->Target->GetName(),
  124. depends, commands, true);
  125. // Write the main driver rule to build everything in this target.
  126. this->WriteTargetDriverRule(this->Target->GetName(), false);
  127. }
  128. //----------------------------------------------------------------------------
  129. void cmMakefileLibraryTargetGenerator::WriteStaticLibraryRules()
  130. {
  131. const char* linkLanguage =
  132. this->Target->GetLinkerLanguage(this->ConfigName);
  133. std::string linkRuleVar = "CMAKE_";
  134. if (linkLanguage)
  135. {
  136. linkRuleVar += linkLanguage;
  137. }
  138. linkRuleVar += "_CREATE_STATIC_LIBRARY";
  139. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION") &&
  140. this->Makefile->GetDefinition((linkRuleVar+"_IPO").c_str()))
  141. {
  142. linkRuleVar += "_IPO";
  143. }
  144. std::string extraFlags;
  145. this->LocalGenerator->AppendFlags
  146. (extraFlags,this->Target->GetProperty("STATIC_LIBRARY_FLAGS"));
  147. std::string staticLibraryFlagsConfig = "STATIC_LIBRARY_FLAGS_";
  148. staticLibraryFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  149. this->LocalGenerator->AppendFlags
  150. (extraFlags, this->Target->GetProperty(staticLibraryFlagsConfig.c_str()));
  151. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), false);
  152. }
  153. //----------------------------------------------------------------------------
  154. void cmMakefileLibraryTargetGenerator::WriteSharedLibraryRules(bool relink)
  155. {
  156. if(this->Target->IsFrameworkOnApple())
  157. {
  158. this->WriteFrameworkRules(relink);
  159. return;
  160. }
  161. const char* linkLanguage =
  162. this->Target->GetLinkerLanguage(this->ConfigName);
  163. std::string linkRuleVar = "CMAKE_";
  164. if (linkLanguage)
  165. {
  166. linkRuleVar += linkLanguage;
  167. }
  168. linkRuleVar += "_CREATE_SHARED_LIBRARY";
  169. std::string extraFlags;
  170. this->LocalGenerator->AppendFlags
  171. (extraFlags, this->Target->GetProperty("LINK_FLAGS"));
  172. std::string linkFlagsConfig = "LINK_FLAGS_";
  173. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  174. this->LocalGenerator->AppendFlags
  175. (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  176. this->LocalGenerator->AddConfigVariableFlags
  177. (extraFlags, "CMAKE_SHARED_LINKER_FLAGS", this->ConfigName);
  178. this->AddModuleDefinitionFlag(extraFlags);
  179. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
  180. }
  181. //----------------------------------------------------------------------------
  182. void cmMakefileLibraryTargetGenerator::WriteModuleLibraryRules(bool relink)
  183. {
  184. const char* linkLanguage =
  185. this->Target->GetLinkerLanguage(this->ConfigName);
  186. std::string linkRuleVar = "CMAKE_";
  187. if (linkLanguage)
  188. {
  189. linkRuleVar += linkLanguage;
  190. }
  191. linkRuleVar += "_CREATE_SHARED_MODULE";
  192. std::string extraFlags;
  193. this->LocalGenerator->AppendFlags(extraFlags,
  194. this->Target->GetProperty("LINK_FLAGS"));
  195. std::string linkFlagsConfig = "LINK_FLAGS_";
  196. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  197. this->LocalGenerator->AppendFlags
  198. (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  199. this->LocalGenerator->AddConfigVariableFlags
  200. (extraFlags, "CMAKE_MODULE_LINKER_FLAGS", this->ConfigName);
  201. this->AddModuleDefinitionFlag(extraFlags);
  202. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
  203. }
  204. //----------------------------------------------------------------------------
  205. void cmMakefileLibraryTargetGenerator::WriteFrameworkRules(bool relink)
  206. {
  207. const char* linkLanguage =
  208. this->Target->GetLinkerLanguage(this->ConfigName);
  209. std::string linkRuleVar = "CMAKE_";
  210. if (linkLanguage)
  211. {
  212. linkRuleVar += linkLanguage;
  213. }
  214. linkRuleVar += "_CREATE_MACOSX_FRAMEWORK";
  215. std::string extraFlags;
  216. this->LocalGenerator->AppendFlags(extraFlags,
  217. this->Target->GetProperty("LINK_FLAGS"));
  218. std::string linkFlagsConfig = "LINK_FLAGS_";
  219. linkFlagsConfig += cmSystemTools::UpperCase(this->ConfigName);
  220. this->LocalGenerator->AppendFlags
  221. (extraFlags, this->Target->GetProperty(linkFlagsConfig.c_str()));
  222. this->LocalGenerator->AddConfigVariableFlags
  223. (extraFlags, "CMAKE_MACOSX_FRAMEWORK_LINKER_FLAGS", this->ConfigName);
  224. this->WriteLibraryRules(linkRuleVar.c_str(), extraFlags.c_str(), relink);
  225. }
  226. //----------------------------------------------------------------------------
  227. void
  228. cmMakefileLibraryTargetGenerator
  229. ::CreateFramework(std::string const& targetName)
  230. {
  231. // Configure the Info.plist file into the Resources directory.
  232. this->MacContentFolders.insert("Resources");
  233. std::string plist = this->MacContentDirectory + "Resources/Info.plist";
  234. this->LocalGenerator->GenerateFrameworkInfoPList(this->Target,
  235. targetName.c_str(),
  236. plist.c_str());
  237. // TODO: Use the cmMakefileTargetGenerator::ExtraFiles vector to
  238. // drive rules to create these files at build time.
  239. std::string oldName;
  240. std::string newName;
  241. // Compute the location of the top-level foo.framework directory.
  242. std::string top = this->Target->GetDirectory(this->ConfigName);
  243. top += "/";
  244. top += this->TargetNameOut;
  245. top += ".framework/";
  246. // Make foo.framework/Versions
  247. std::string versions = top;
  248. versions += "Versions";
  249. cmSystemTools::MakeDirectory(versions.c_str());
  250. // Make foo.framework/Versions/version
  251. std::string version = versions;
  252. version += "/";
  253. version += this->FrameworkVersion;
  254. cmSystemTools::MakeDirectory(version.c_str());
  255. // Current -> version
  256. oldName = this->FrameworkVersion;
  257. newName = versions;
  258. newName += "/Current";
  259. cmSystemTools::RemoveFile(newName.c_str());
  260. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  261. this->Makefile->AddCMakeOutputFile(newName.c_str());
  262. // foo -> Versions/Current/foo
  263. oldName = "Versions/Current/";
  264. oldName += this->TargetNameOut;
  265. newName = top;
  266. newName += this->TargetNameOut;
  267. cmSystemTools::RemoveFile(newName.c_str());
  268. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  269. this->Makefile->AddCMakeOutputFile(newName.c_str());
  270. // Resources -> Versions/Current/Resources
  271. if(this->MacContentFolders.find("Resources") !=
  272. this->MacContentFolders.end())
  273. {
  274. oldName = "Versions/Current/Resources";
  275. newName = top;
  276. newName += "Resources";
  277. cmSystemTools::RemoveFile(newName.c_str());
  278. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  279. this->Makefile->AddCMakeOutputFile(newName.c_str());
  280. }
  281. // Headers -> Versions/Current/Headers
  282. if(this->MacContentFolders.find("Headers") !=
  283. this->MacContentFolders.end())
  284. {
  285. oldName = "Versions/Current/Headers";
  286. newName = top;
  287. newName += "Headers";
  288. cmSystemTools::RemoveFile(newName.c_str());
  289. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  290. this->Makefile->AddCMakeOutputFile(newName.c_str());
  291. }
  292. // PrivateHeaders -> Versions/Current/PrivateHeaders
  293. if(this->MacContentFolders.find("PrivateHeaders") !=
  294. this->MacContentFolders.end())
  295. {
  296. oldName = "Versions/Current/PrivateHeaders";
  297. newName = top;
  298. newName += "PrivateHeaders";
  299. cmSystemTools::RemoveFile(newName.c_str());
  300. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  301. this->Makefile->AddCMakeOutputFile(newName.c_str());
  302. }
  303. }
  304. //----------------------------------------------------------------------------
  305. void
  306. cmMakefileLibraryTargetGenerator::CreateCFBundle(std::string& targetName,
  307. std::string& outpath)
  308. {
  309. // Compute bundle directory names.
  310. outpath = this->MacContentDirectory;
  311. outpath += "MacOS";
  312. cmSystemTools::MakeDirectory(outpath.c_str());
  313. this->Makefile->AddCMakeOutputFile(outpath.c_str());
  314. outpath += "/";
  315. // Configure the Info.plist file. Note that it needs the executable name
  316. // to be set.
  317. std::string plist = this->MacContentDirectory + "Info.plist";
  318. this->LocalGenerator->GenerateAppleInfoPList(this->Target,
  319. targetName.c_str(),
  320. plist.c_str());
  321. this->Makefile->AddCMakeOutputFile(plist.c_str());
  322. }
  323. //----------------------------------------------------------------------------
  324. void cmMakefileLibraryTargetGenerator::WriteLibraryRules
  325. (const char* linkRuleVar, const char* extraFlags, bool relink)
  326. {
  327. // TODO: Merge the methods that call this method to avoid
  328. // code duplication.
  329. std::vector<std::string> commands;
  330. // Build list of dependencies.
  331. std::vector<std::string> depends;
  332. this->AppendLinkDepends(depends);
  333. // Get the language to use for linking this library.
  334. const char* linkLanguage =
  335. this->Target->GetLinkerLanguage(this->ConfigName);
  336. // Make sure we have a link language.
  337. if(!linkLanguage)
  338. {
  339. cmSystemTools::Error("Cannot determine link language for target \"",
  340. this->Target->GetName(), "\".");
  341. return;
  342. }
  343. // Create set of linking flags.
  344. std::string linkFlags;
  345. this->LocalGenerator->AppendFlags(linkFlags, extraFlags);
  346. // Add OSX version flags, if any.
  347. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  348. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  349. {
  350. this->AppendOSXVerFlag(linkFlags, linkLanguage, "COMPATIBILITY", true);
  351. this->AppendOSXVerFlag(linkFlags, linkLanguage, "CURRENT", false);
  352. }
  353. // Construct the name of the library.
  354. std::string targetName;
  355. std::string targetNameSO;
  356. std::string targetNameReal;
  357. std::string targetNameImport;
  358. std::string targetNamePDB;
  359. this->Target->GetLibraryNames(
  360. targetName, targetNameSO, targetNameReal, targetNameImport, targetNamePDB,
  361. this->ConfigName);
  362. // Construct the full path version of the names.
  363. std::string outpath;
  364. std::string outpathImp;
  365. if(this->Target->IsFrameworkOnApple())
  366. {
  367. outpath = this->MacContentDirectory;
  368. this->CreateFramework(targetName);
  369. }
  370. else if(this->Target->IsCFBundleOnApple())
  371. {
  372. outpath = this->Target->GetDirectory(this->ConfigName);
  373. outpath += "/";
  374. this->CreateCFBundle(targetName, outpath);
  375. }
  376. else if(relink)
  377. {
  378. outpath = this->Makefile->GetStartOutputDirectory();
  379. outpath += cmake::GetCMakeFilesDirectory();
  380. outpath += "/CMakeRelink.dir";
  381. cmSystemTools::MakeDirectory(outpath.c_str());
  382. outpath += "/";
  383. if(!targetNameImport.empty())
  384. {
  385. outpathImp = outpath;
  386. }
  387. }
  388. else
  389. {
  390. outpath = this->Target->GetDirectory(this->ConfigName);
  391. cmSystemTools::MakeDirectory(outpath.c_str());
  392. outpath += "/";
  393. if(!targetNameImport.empty())
  394. {
  395. outpathImp = this->Target->GetDirectory(this->ConfigName, true);
  396. cmSystemTools::MakeDirectory(outpathImp.c_str());
  397. outpathImp += "/";
  398. }
  399. }
  400. std::string targetFullPath = outpath + targetName;
  401. std::string targetFullPathPDB = outpath + targetNamePDB;
  402. std::string targetFullPathSO = outpath + targetNameSO;
  403. std::string targetFullPathReal = outpath + targetNameReal;
  404. std::string targetFullPathImport = outpathImp + targetNameImport;
  405. // Construct the output path version of the names for use in command
  406. // arguments.
  407. std::string targetOutPathPDB =
  408. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::NONE,
  409. cmLocalGenerator::SHELL);
  410. std::string targetOutPath =
  411. this->Convert(targetFullPath.c_str(),cmLocalGenerator::START_OUTPUT,
  412. cmLocalGenerator::SHELL);
  413. std::string targetOutPathSO =
  414. this->Convert(targetFullPathSO.c_str(),cmLocalGenerator::START_OUTPUT,
  415. cmLocalGenerator::SHELL);
  416. std::string targetOutPathReal =
  417. this->Convert(targetFullPathReal.c_str(),cmLocalGenerator::START_OUTPUT,
  418. cmLocalGenerator::SHELL);
  419. std::string targetOutPathImport =
  420. this->Convert(targetFullPathImport.c_str(),cmLocalGenerator::START_OUTPUT,
  421. cmLocalGenerator::SHELL);
  422. if(!this->NoRuleMessages)
  423. {
  424. // Add the link message.
  425. std::string buildEcho = "Linking ";
  426. buildEcho += linkLanguage;
  427. switch(this->Target->GetType())
  428. {
  429. case cmTarget::STATIC_LIBRARY:
  430. buildEcho += " static library ";
  431. break;
  432. case cmTarget::SHARED_LIBRARY:
  433. buildEcho += " shared library ";
  434. break;
  435. case cmTarget::MODULE_LIBRARY:
  436. if (this->Target->IsCFBundleOnApple())
  437. buildEcho += " CFBundle";
  438. buildEcho += " shared module ";
  439. break;
  440. default:
  441. buildEcho += " library ";
  442. break;
  443. }
  444. buildEcho += targetOutPath.c_str();
  445. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  446. cmLocalUnixMakefileGenerator3::EchoLink);
  447. }
  448. const char* forbiddenFlagVar = 0;
  449. switch(this->Target->GetType())
  450. {
  451. case cmTarget::SHARED_LIBRARY:
  452. forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
  453. break;
  454. case cmTarget::MODULE_LIBRARY:
  455. forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
  456. break;
  457. default: break;
  458. }
  459. // Clean files associated with this library.
  460. std::vector<std::string> libCleanFiles;
  461. libCleanFiles.push_back(this->Convert(targetFullPath.c_str(),
  462. cmLocalGenerator::START_OUTPUT,
  463. cmLocalGenerator::UNCHANGED));
  464. if(targetNameReal != targetName)
  465. {
  466. libCleanFiles.push_back(this->Convert(targetFullPathReal.c_str(),
  467. cmLocalGenerator::START_OUTPUT,
  468. cmLocalGenerator::UNCHANGED));
  469. }
  470. if(targetNameSO != targetName &&
  471. targetNameSO != targetNameReal)
  472. {
  473. libCleanFiles.push_back(this->Convert(targetFullPathSO.c_str(),
  474. cmLocalGenerator::START_OUTPUT,
  475. cmLocalGenerator::UNCHANGED));
  476. }
  477. if(!targetNameImport.empty())
  478. {
  479. libCleanFiles.push_back(this->Convert(targetFullPathImport.c_str(),
  480. cmLocalGenerator::START_OUTPUT,
  481. cmLocalGenerator::UNCHANGED));
  482. std::string implib;
  483. if(this->Target->GetImplibGNUtoMS(targetFullPathImport, implib))
  484. {
  485. libCleanFiles.push_back(this->Convert(implib.c_str(),
  486. cmLocalGenerator::START_OUTPUT,
  487. cmLocalGenerator::UNCHANGED));
  488. }
  489. }
  490. // List the PDB for cleaning only when the whole target is
  491. // cleaned. We do not want to delete the .pdb file just before
  492. // linking the target.
  493. this->CleanFiles.push_back
  494. (this->Convert(targetFullPathPDB.c_str(),
  495. cmLocalGenerator::START_OUTPUT,
  496. cmLocalGenerator::UNCHANGED));
  497. #ifdef _WIN32
  498. // There may be a manifest file for this target. Add it to the
  499. // clean set just in case.
  500. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  501. {
  502. libCleanFiles.push_back(
  503. this->Convert((targetFullPath+".manifest").c_str(),
  504. cmLocalGenerator::START_OUTPUT,
  505. cmLocalGenerator::UNCHANGED));
  506. }
  507. #endif
  508. std::vector<std::string> commands1;
  509. // Add a command to remove any existing files for this library.
  510. // for static libs only
  511. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  512. {
  513. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  514. *this->Target, "target");
  515. this->LocalGenerator->CreateCDCommand
  516. (commands1,
  517. this->Makefile->GetStartOutputDirectory(),
  518. cmLocalGenerator::HOME_OUTPUT);
  519. commands.insert(commands.end(), commands1.begin(), commands1.end());
  520. commands1.clear();
  521. }
  522. // Add the pre-build and pre-link rules building but not when relinking.
  523. if(!relink)
  524. {
  525. this->LocalGenerator
  526. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands(),
  527. this->Target);
  528. this->LocalGenerator
  529. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands(),
  530. this->Target);
  531. }
  532. // Determine whether a link script will be used.
  533. bool useLinkScript = this->GlobalGenerator->GetUseLinkScript();
  534. // Select whether to use a response file for objects.
  535. bool useResponseFile = false;
  536. {
  537. std::string responseVar = "CMAKE_";
  538. responseVar += linkLanguage;
  539. responseVar += "_USE_RESPONSE_FILE_FOR_OBJECTS";
  540. if(this->Makefile->IsOn(responseVar.c_str()))
  541. {
  542. useResponseFile = true;
  543. }
  544. }
  545. // For static libraries there might be archiving rules.
  546. bool haveStaticLibraryRule = false;
  547. std::vector<std::string> archiveCreateCommands;
  548. std::vector<std::string> archiveAppendCommands;
  549. std::vector<std::string> archiveFinishCommands;
  550. std::string::size_type archiveCommandLimit = std::string::npos;
  551. if(this->Target->GetType() == cmTarget::STATIC_LIBRARY)
  552. {
  553. haveStaticLibraryRule =
  554. this->Makefile->GetDefinition(linkRuleVar)? true:false;
  555. std::string arCreateVar = "CMAKE_";
  556. arCreateVar += linkLanguage;
  557. arCreateVar += "_ARCHIVE_CREATE";
  558. if(const char* rule = this->Makefile->GetDefinition(arCreateVar.c_str()))
  559. {
  560. cmSystemTools::ExpandListArgument(rule, archiveCreateCommands);
  561. }
  562. std::string arAppendVar = "CMAKE_";
  563. arAppendVar += linkLanguage;
  564. arAppendVar += "_ARCHIVE_APPEND";
  565. if(const char* rule = this->Makefile->GetDefinition(arAppendVar.c_str()))
  566. {
  567. cmSystemTools::ExpandListArgument(rule, archiveAppendCommands);
  568. }
  569. std::string arFinishVar = "CMAKE_";
  570. arFinishVar += linkLanguage;
  571. arFinishVar += "_ARCHIVE_FINISH";
  572. if(const char* rule = this->Makefile->GetDefinition(arFinishVar.c_str()))
  573. {
  574. cmSystemTools::ExpandListArgument(rule, archiveFinishCommands);
  575. }
  576. }
  577. // Decide whether to use archiving rules.
  578. bool useArchiveRules =
  579. !haveStaticLibraryRule &&
  580. !archiveCreateCommands.empty() && !archiveAppendCommands.empty();
  581. if(useArchiveRules)
  582. {
  583. // Archiving rules are always run with a link script.
  584. useLinkScript = true;
  585. // Archiving rules never use a response file.
  586. useResponseFile = false;
  587. // Limit the length of individual object lists to less than the
  588. // 32K command line length limit on Windows. We could make this a
  589. // platform file variable but this should work everywhere.
  590. archiveCommandLimit = 30000;
  591. }
  592. // Expand the rule variables.
  593. std::vector<std::string> real_link_commands;
  594. {
  595. // Set path conversion for link script shells.
  596. this->LocalGenerator->SetLinkScriptShell(useLinkScript);
  597. // Collect up flags to link in needed libraries.
  598. cmOStringStream linklibs;
  599. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  600. {
  601. this->LocalGenerator
  602. ->OutputLinkLibraries(linklibs, *this->Target, relink);
  603. }
  604. // Construct object file lists that may be needed to expand the
  605. // rule.
  606. std::string buildObjs;
  607. this->CreateObjectLists(useLinkScript, useArchiveRules, useResponseFile,
  608. buildObjs, depends);
  609. cmLocalGenerator::RuleVariables vars;
  610. vars.TargetPDB = targetOutPathPDB.c_str();
  611. // Setup the target version.
  612. std::string targetVersionMajor;
  613. std::string targetVersionMinor;
  614. {
  615. cmOStringStream majorStream;
  616. cmOStringStream minorStream;
  617. int major;
  618. int minor;
  619. this->Target->GetTargetVersion(major, minor);
  620. majorStream << major;
  621. minorStream << minor;
  622. targetVersionMajor = majorStream.str();
  623. targetVersionMinor = minorStream.str();
  624. }
  625. vars.TargetVersionMajor = targetVersionMajor.c_str();
  626. vars.TargetVersionMinor = targetVersionMinor.c_str();
  627. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  628. vars.CMTarget = this->Target;
  629. vars.Language = linkLanguage;
  630. vars.Objects = buildObjs.c_str();
  631. std::string objdir = cmake::GetCMakeFilesDirectoryPostSlash();
  632. objdir += this->Target->GetName();
  633. objdir += ".dir";
  634. objdir = this->Convert(objdir.c_str(),
  635. cmLocalGenerator::START_OUTPUT,
  636. cmLocalGenerator::SHELL);
  637. vars.ObjectDir = objdir.c_str();
  638. vars.Target = targetOutPathReal.c_str();
  639. std::string linkString = linklibs.str();
  640. vars.LinkLibraries = linkString.c_str();
  641. vars.ObjectsQuoted = buildObjs.c_str();
  642. vars.TargetSOName= targetNameSO.c_str();
  643. vars.LinkFlags = linkFlags.c_str();
  644. // Compute the directory portion of the install_name setting.
  645. std::string install_name_dir;
  646. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  647. {
  648. // Get the install_name directory for the build tree.
  649. install_name_dir =
  650. this->Target->GetInstallNameDirForBuildTree(this->ConfigName);
  651. // Set the rule variable replacement value.
  652. if(install_name_dir.empty())
  653. {
  654. vars.TargetInstallNameDir = "";
  655. }
  656. else
  657. {
  658. // Convert to a path for the native build tool.
  659. install_name_dir =
  660. this->LocalGenerator->Convert(install_name_dir.c_str(),
  661. cmLocalGenerator::NONE,
  662. cmLocalGenerator::SHELL, false);
  663. vars.TargetInstallNameDir = install_name_dir.c_str();
  664. }
  665. }
  666. // Add language feature flags.
  667. std::string langFlags;
  668. this->AddFeatureFlags(langFlags, linkLanguage);
  669. this->LocalGenerator->AddArchitectureFlags(langFlags, this->Target,
  670. linkLanguage, this->ConfigName);
  671. // remove any language flags that might not work with the
  672. // particular os
  673. if(forbiddenFlagVar)
  674. {
  675. this->RemoveForbiddenFlags(forbiddenFlagVar,
  676. linkLanguage, langFlags);
  677. }
  678. vars.LanguageCompileFlags = langFlags.c_str();
  679. // Construct the main link rule and expand placeholders.
  680. this->LocalGenerator->TargetImplib = targetOutPathImport;
  681. if(useArchiveRules)
  682. {
  683. // Construct the individual object list strings.
  684. std::vector<std::string> object_strings;
  685. this->WriteObjectsStrings(object_strings, archiveCommandLimit);
  686. // Create the archive with the first set of objects.
  687. std::vector<std::string>::iterator osi = object_strings.begin();
  688. {
  689. vars.Objects = osi->c_str();
  690. for(std::vector<std::string>::const_iterator
  691. i = archiveCreateCommands.begin();
  692. i != archiveCreateCommands.end(); ++i)
  693. {
  694. std::string cmd = *i;
  695. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  696. real_link_commands.push_back(cmd);
  697. }
  698. }
  699. // Append to the archive with the other object sets.
  700. for(++osi; osi != object_strings.end(); ++osi)
  701. {
  702. vars.Objects = osi->c_str();
  703. for(std::vector<std::string>::const_iterator
  704. i = archiveAppendCommands.begin();
  705. i != archiveAppendCommands.end(); ++i)
  706. {
  707. std::string cmd = *i;
  708. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  709. real_link_commands.push_back(cmd);
  710. }
  711. }
  712. // Finish the archive.
  713. vars.Objects = "";
  714. for(std::vector<std::string>::const_iterator
  715. i = archiveFinishCommands.begin();
  716. i != archiveFinishCommands.end(); ++i)
  717. {
  718. std::string cmd = *i;
  719. this->LocalGenerator->ExpandRuleVariables(cmd, vars);
  720. real_link_commands.push_back(cmd);
  721. }
  722. }
  723. else
  724. {
  725. // Get the set of commands.
  726. std::string linkRule = this->GetLinkRule(linkRuleVar);
  727. cmSystemTools::ExpandListArgument(linkRule, real_link_commands);
  728. // Expand placeholders.
  729. for(std::vector<std::string>::iterator i = real_link_commands.begin();
  730. i != real_link_commands.end(); ++i)
  731. {
  732. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  733. }
  734. }
  735. this->LocalGenerator->TargetImplib = "";
  736. // Restore path conversion to normal shells.
  737. this->LocalGenerator->SetLinkScriptShell(false);
  738. }
  739. // Optionally convert the build rule to use a script to avoid long
  740. // command lines in the make shell.
  741. if(useLinkScript)
  742. {
  743. // Use a link script.
  744. const char* name = (relink? "relink.txt" : "link.txt");
  745. this->CreateLinkScript(name, real_link_commands, commands1, depends);
  746. }
  747. else
  748. {
  749. // No link script. Just use the link rule directly.
  750. commands1 = real_link_commands;
  751. }
  752. this->LocalGenerator->CreateCDCommand
  753. (commands1,
  754. this->Makefile->GetStartOutputDirectory(),
  755. cmLocalGenerator::HOME_OUTPUT);
  756. commands.insert(commands.end(), commands1.begin(), commands1.end());
  757. commands1.clear();
  758. // Add a rule to create necessary symlinks for the library.
  759. if(targetOutPath != targetOutPathReal)
  760. {
  761. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  762. symlink += targetOutPathReal;
  763. symlink += " ";
  764. symlink += targetOutPathSO;
  765. symlink += " ";
  766. symlink += targetOutPath;
  767. commands1.push_back(symlink);
  768. this->LocalGenerator->CreateCDCommand(commands1,
  769. this->Makefile->GetStartOutputDirectory(),
  770. cmLocalGenerator::HOME_OUTPUT);
  771. commands.insert(commands.end(), commands1.begin(), commands1.end());
  772. commands1.clear();
  773. }
  774. // Add the post-build rules when building but not when relinking.
  775. if(!relink)
  776. {
  777. this->LocalGenerator->
  778. AppendCustomCommands(commands, this->Target->GetPostBuildCommands(),
  779. this->Target);
  780. }
  781. // Write the build rule.
  782. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  783. targetFullPathReal.c_str(),
  784. depends, commands, false);
  785. // Some targets have more than one output file. Create rules to
  786. // drive the build if any extra outputs are missing.
  787. std::vector<std::string> extraOutputs;
  788. if(targetNameSO != targetNameReal)
  789. {
  790. this->GenerateExtraOutput(targetFullPathSO.c_str(),
  791. targetFullPathReal.c_str());
  792. }
  793. if(targetName != targetNameSO &&
  794. targetName != targetNameReal)
  795. {
  796. this->GenerateExtraOutput(targetFullPath.c_str(),
  797. targetFullPathReal.c_str());
  798. }
  799. // Write the main driver rule to build everything in this target.
  800. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  801. // Clean all the possible library names and symlinks.
  802. this->CleanFiles.insert(this->CleanFiles.end(),
  803. libCleanFiles.begin(),libCleanFiles.end());
  804. }
  805. //----------------------------------------------------------------------------
  806. void
  807. cmMakefileLibraryTargetGenerator
  808. ::AppendOSXVerFlag(std::string& flags, const char* lang,
  809. const char* name, bool so)
  810. {
  811. // Lookup the flag to specify the version.
  812. std::string fvar = "CMAKE_";
  813. fvar += lang;
  814. fvar += "_OSX_";
  815. fvar += name;
  816. fvar += "_VERSION_FLAG";
  817. const char* flag = this->Makefile->GetDefinition(fvar.c_str());
  818. // Skip if no such flag.
  819. if(!flag)
  820. {
  821. return;
  822. }
  823. // Lookup the target version information.
  824. int major;
  825. int minor;
  826. int patch;
  827. this->Target->GetTargetVersion(so, major, minor, patch);
  828. if(major > 0 || minor > 0 || patch > 0)
  829. {
  830. // Append the flag since a non-zero version is specified.
  831. cmOStringStream vflag;
  832. vflag << flag << major << "." << minor << "." << patch;
  833. this->LocalGenerator->AppendFlags(flags, vflag.str().c_str());
  834. }
  835. }