cmMakefileLibraryTargetGenerator.cxx 29 KB

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