cmMakefileLibraryTargetGenerator.cxx 32 KB

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