cmMakefileLibraryTargetGenerator.cxx 32 KB

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