cmMakefileLibraryTargetGenerator.cxx 30 KB

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