cmMakefileLibraryTargetGenerator.cxx 32 KB

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