cmMakefileLibraryTargetGenerator.cxx 29 KB

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