cmMakefileLibraryTargetGenerator.cxx 32 KB

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