cmMakefileLibraryTargetGenerator.cxx 32 KB

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