cmMakefileLibraryTargetGenerator.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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->GetPropertyAsBool("FRAMEWORK"))
  464. {
  465. this->CreateFramework(targetName, outpath);
  466. }
  467. #endif
  468. std::string targetFullPath = outpath + targetName;
  469. std::string targetFullPathPDB = outpath + targetNamePDB;
  470. std::string targetFullPathSO = outpath + targetNameSO;
  471. std::string targetFullPathReal = outpath + targetNameReal;
  472. std::string targetFullPathImport = outpathImp + targetNameImport;
  473. // Construct the output path version of the names for use in command
  474. // arguments.
  475. std::string targetOutPathPDB =
  476. this->Convert(targetFullPathPDB.c_str(),cmLocalGenerator::FULL,
  477. cmLocalGenerator::SHELL);
  478. std::string targetOutPath =
  479. this->Convert(targetFullPath.c_str(),cmLocalGenerator::START_OUTPUT,
  480. cmLocalGenerator::SHELL);
  481. std::string targetOutPathSO =
  482. this->Convert(targetFullPathSO.c_str(),cmLocalGenerator::START_OUTPUT,
  483. cmLocalGenerator::SHELL);
  484. std::string targetOutPathReal =
  485. this->Convert(targetFullPathReal.c_str(),cmLocalGenerator::START_OUTPUT,
  486. cmLocalGenerator::SHELL);
  487. std::string targetOutPathImport =
  488. this->Convert(targetFullPathImport.c_str(),cmLocalGenerator::START_OUTPUT,
  489. cmLocalGenerator::SHELL);
  490. // Add the link message.
  491. std::string buildEcho = "Linking ";
  492. buildEcho += linkLanguage;
  493. const char* forbiddenFlagVar = 0;
  494. switch(this->Target->GetType())
  495. {
  496. case cmTarget::STATIC_LIBRARY:
  497. buildEcho += " static library ";
  498. break;
  499. case cmTarget::SHARED_LIBRARY:
  500. forbiddenFlagVar = "_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS";
  501. buildEcho += " shared library ";
  502. break;
  503. case cmTarget::MODULE_LIBRARY:
  504. forbiddenFlagVar = "_CREATE_SHARED_MODULE_FORBIDDEN_FLAGS";
  505. buildEcho += " shared module ";
  506. break;
  507. default:
  508. buildEcho += " library ";
  509. break;
  510. }
  511. buildEcho += targetOutPath.c_str();
  512. this->LocalGenerator->AppendEcho(commands, buildEcho.c_str(),
  513. cmLocalUnixMakefileGenerator3::EchoLink);
  514. // Construct a list of files associated with this library that may
  515. // need to be cleaned.
  516. std::vector<std::string> libCleanFiles;
  517. if(this->Target->GetPropertyAsBool("CLEAN_DIRECT_OUTPUT"))
  518. {
  519. // The user has requested that only the files directly built
  520. // by this target be cleaned instead of all possible names.
  521. libCleanFiles.push_back(this->Convert(targetFullPath.c_str(),
  522. cmLocalGenerator::START_OUTPUT,
  523. cmLocalGenerator::UNCHANGED));
  524. if(targetNameReal != targetName)
  525. {
  526. libCleanFiles.push_back(this->Convert(targetFullPathReal.c_str(),
  527. cmLocalGenerator::START_OUTPUT,
  528. cmLocalGenerator::UNCHANGED));
  529. }
  530. if(targetNameSO != targetName &&
  531. targetNameSO != targetNameReal)
  532. {
  533. libCleanFiles.push_back(this->Convert(targetFullPathSO.c_str(),
  534. cmLocalGenerator::START_OUTPUT,
  535. cmLocalGenerator::UNCHANGED));
  536. }
  537. if(!targetNameImport.empty())
  538. {
  539. libCleanFiles.push_back(this->Convert(targetFullPathImport.c_str(),
  540. cmLocalGenerator::START_OUTPUT,
  541. cmLocalGenerator::UNCHANGED));
  542. }
  543. }
  544. else
  545. {
  546. // This target may switch between static and shared based
  547. // on a user option or the BUILD_SHARED_LIBS switch. Clean
  548. // all possible names.
  549. std::string cleanStaticName;
  550. std::string cleanSharedName;
  551. std::string cleanSharedSOName;
  552. std::string cleanSharedRealName;
  553. std::string cleanImportName;
  554. std::string cleanPDBName;
  555. this->Target->GetLibraryCleanNames(
  556. cleanStaticName,
  557. cleanSharedName,
  558. cleanSharedSOName,
  559. cleanSharedRealName,
  560. cleanImportName,
  561. cleanPDBName,
  562. this->LocalGenerator->ConfigurationName.c_str());
  563. std::string cleanFullStaticName = outpath + cleanStaticName;
  564. std::string cleanFullSharedName = outpath + cleanSharedName;
  565. std::string cleanFullSharedSOName = outpath + cleanSharedSOName;
  566. std::string cleanFullSharedRealName = outpath + cleanSharedRealName;
  567. std::string cleanFullImportName = outpathImp + cleanImportName;
  568. std::string cleanFullPDBName = outpath + cleanPDBName;
  569. libCleanFiles.push_back
  570. (this->Convert(cleanFullStaticName.c_str(),
  571. cmLocalGenerator::START_OUTPUT,
  572. cmLocalGenerator::UNCHANGED));
  573. if(cleanSharedRealName != cleanStaticName)
  574. {
  575. libCleanFiles.push_back(this->Convert(cleanFullSharedRealName.c_str(),
  576. cmLocalGenerator::START_OUTPUT,
  577. cmLocalGenerator::UNCHANGED));
  578. }
  579. if(cleanSharedSOName != cleanStaticName &&
  580. cleanSharedSOName != cleanSharedRealName)
  581. {
  582. libCleanFiles.push_back(this->Convert(cleanFullSharedSOName.c_str(),
  583. cmLocalGenerator::START_OUTPUT,
  584. cmLocalGenerator::UNCHANGED));
  585. }
  586. if(cleanSharedName != cleanStaticName &&
  587. cleanSharedName != cleanSharedSOName &&
  588. cleanSharedName != cleanSharedRealName)
  589. {
  590. libCleanFiles.push_back(this->Convert(cleanFullSharedName.c_str(),
  591. cmLocalGenerator::START_OUTPUT,
  592. cmLocalGenerator::UNCHANGED));
  593. }
  594. if(!cleanImportName.empty())
  595. {
  596. libCleanFiles.push_back(this->Convert(cleanFullImportName.c_str(),
  597. cmLocalGenerator::START_OUTPUT,
  598. cmLocalGenerator::UNCHANGED));
  599. }
  600. // List the PDB for cleaning only when the whole target is
  601. // cleaned. We do not want to delete the .pdb file just before
  602. // linking the target.
  603. this->CleanFiles.push_back
  604. (this->Convert(cleanFullPDBName.c_str(),
  605. cmLocalGenerator::START_OUTPUT,
  606. cmLocalGenerator::UNCHANGED));
  607. }
  608. #ifdef _WIN32
  609. // There may be a manifest file for this target. Add it to the
  610. // clean set just in case.
  611. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  612. {
  613. libCleanFiles.push_back(
  614. this->Convert((targetFullPath+".manifest").c_str(),
  615. cmLocalGenerator::START_OUTPUT,
  616. cmLocalGenerator::UNCHANGED));
  617. }
  618. #endif
  619. // Add a command to remove any existing files for this library.
  620. std::vector<std::string> commands1;
  621. this->LocalGenerator->AppendCleanCommand(commands1, libCleanFiles,
  622. *this->Target, "target");
  623. this->LocalGenerator->CreateCDCommand
  624. (commands1,
  625. this->Makefile->GetStartOutputDirectory(),
  626. this->Makefile->GetHomeOutputDirectory());
  627. commands.insert(commands.end(), commands1.begin(), commands1.end());
  628. commands1.clear();
  629. // Add the pre-build and pre-link rules building but not when relinking.
  630. if(!relink)
  631. {
  632. this->LocalGenerator
  633. ->AppendCustomCommands(commands, this->Target->GetPreBuildCommands());
  634. this->LocalGenerator
  635. ->AppendCustomCommands(commands, this->Target->GetPreLinkCommands());
  636. }
  637. // Open the link script if it will be used.
  638. bool useLinkScript = false;
  639. std::string linkScriptName;
  640. std::auto_ptr<cmGeneratedFileStream> linkScriptStream;
  641. if(this->GlobalGenerator->GetUseLinkScript() &&
  642. (this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  643. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  644. this->Target->GetType() == cmTarget::MODULE_LIBRARY))
  645. {
  646. useLinkScript = true;
  647. linkScriptName = this->TargetBuildDirectoryFull;
  648. if(relink)
  649. {
  650. linkScriptName += "/relink.txt";
  651. }
  652. else
  653. {
  654. linkScriptName += "/link.txt";
  655. }
  656. std::auto_ptr<cmGeneratedFileStream> lss(
  657. new cmGeneratedFileStream(linkScriptName.c_str()));
  658. linkScriptStream = lss;
  659. }
  660. std::vector<std::string> link_script_commands;
  661. // Construct the main link rule.
  662. std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
  663. if(useLinkScript)
  664. {
  665. cmSystemTools::ExpandListArgument(linkRule, link_script_commands);
  666. std::string link_command = "$(CMAKE_COMMAND) -E cmake_link_script ";
  667. link_command += this->Convert(linkScriptName.c_str(),
  668. cmLocalGenerator::START_OUTPUT,
  669. cmLocalGenerator::SHELL);
  670. link_command += " --verbose=$(VERBOSE)";
  671. commands1.push_back(link_command);
  672. }
  673. else
  674. {
  675. cmSystemTools::ExpandListArgument(linkRule, commands1);
  676. }
  677. this->LocalGenerator->CreateCDCommand
  678. (commands1,
  679. this->Makefile->GetStartOutputDirectory(),
  680. this->Makefile->GetHomeOutputDirectory());
  681. commands.insert(commands.end(), commands1.begin(), commands1.end());
  682. // Add a rule to create necessary symlinks for the library.
  683. if(targetOutPath != targetOutPathReal)
  684. {
  685. std::string symlink = "$(CMAKE_COMMAND) -E cmake_symlink_library ";
  686. symlink += targetOutPathReal;
  687. symlink += " ";
  688. symlink += targetOutPathSO;
  689. symlink += " ";
  690. symlink += targetOutPath;
  691. commands1.clear();
  692. commands1.push_back(symlink);
  693. this->LocalGenerator->CreateCDCommand(commands1,
  694. this->Makefile->GetStartOutputDirectory(),
  695. this->Makefile->GetHomeOutputDirectory());
  696. commands.insert(commands.end(), commands1.begin(), commands1.end());
  697. }
  698. // Add the post-build rules when building but not when relinking.
  699. if(!relink)
  700. {
  701. this->LocalGenerator->
  702. AppendCustomCommands(commands, this->Target->GetPostBuildCommands());
  703. }
  704. // Collect up flags to link in needed libraries.
  705. cmOStringStream linklibs;
  706. this->LocalGenerator->OutputLinkLibraries(linklibs, *this->Target, relink);
  707. // Construct object file lists that may be needed to expand the
  708. // rule.
  709. std::string variableName;
  710. std::string variableNameExternal;
  711. this->WriteObjectsVariable(variableName, variableNameExternal);
  712. std::string buildObjs;
  713. if(useLinkScript)
  714. {
  715. this->WriteObjectsString(buildObjs);
  716. }
  717. else
  718. {
  719. buildObjs = "$(";
  720. buildObjs += variableName;
  721. buildObjs += ") $(";
  722. buildObjs += variableNameExternal;
  723. buildObjs += ")";
  724. }
  725. std::string cleanObjs = "$(";
  726. cleanObjs += variableName;
  727. cleanObjs += ")";
  728. cmLocalGenerator::RuleVariables vars;
  729. vars.TargetPDB = targetOutPathPDB.c_str();
  730. // Setup the target version.
  731. std::string targetVersionMajor;
  732. std::string targetVersionMinor;
  733. {
  734. cmOStringStream majorStream;
  735. cmOStringStream minorStream;
  736. int major;
  737. int minor;
  738. this->Target->GetTargetVersion(major, minor);
  739. majorStream << major;
  740. minorStream << minor;
  741. targetVersionMajor = majorStream.str();
  742. targetVersionMinor = minorStream.str();
  743. }
  744. vars.TargetVersionMajor = targetVersionMajor.c_str();
  745. vars.TargetVersionMinor = targetVersionMinor.c_str();
  746. vars.Language = linkLanguage;
  747. vars.Objects = buildObjs.c_str();
  748. std::string objdir = cmake::GetCMakeFilesDirectoryPostSlash();
  749. objdir += this->Target->GetName();
  750. objdir += ".dir";
  751. vars.ObjectDir = objdir.c_str();
  752. vars.Target = targetOutPathReal.c_str();
  753. std::string linkString = linklibs.str();
  754. vars.LinkLibraries = linkString.c_str();
  755. vars.ObjectsQuoted = buildObjs.c_str();
  756. vars.TargetSOName= targetNameSO.c_str();
  757. vars.LinkFlags = linkFlags.c_str();
  758. // Compute the directory portion of the install_name setting.
  759. std::string install_name_dir;
  760. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  761. {
  762. // Get the install_name directory for the build tree.
  763. const char* config = this->LocalGenerator->ConfigurationName.c_str();
  764. install_name_dir = this->Target->GetInstallNameDirForBuildTree(config);
  765. // Set the rule variable replacement value.
  766. if(install_name_dir.empty())
  767. {
  768. vars.TargetInstallNameDir = "";
  769. }
  770. else
  771. {
  772. // Convert to a path for the native build tool.
  773. install_name_dir =
  774. this->LocalGenerator->Convert(install_name_dir.c_str(),
  775. cmLocalGenerator::NONE,
  776. cmLocalGenerator::SHELL, false);
  777. vars.TargetInstallNameDir = install_name_dir.c_str();
  778. }
  779. }
  780. std::string langFlags;
  781. this->LocalGenerator
  782. ->AddLanguageFlags(langFlags, linkLanguage,
  783. this->LocalGenerator->ConfigurationName.c_str());
  784. // remove any language flags that might not work with the
  785. // particular os
  786. if(forbiddenFlagVar)
  787. {
  788. this->RemoveForbiddenFlags(forbiddenFlagVar,
  789. linkLanguage, langFlags);
  790. }
  791. vars.LanguageCompileFlags = langFlags.c_str();
  792. // Expand placeholders in the commands.
  793. this->LocalGenerator->TargetImplib = targetOutPathImport;
  794. if(useLinkScript)
  795. {
  796. for(std::vector<std::string>::iterator i = link_script_commands.begin();
  797. i != link_script_commands.end(); ++i)
  798. {
  799. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  800. }
  801. }
  802. else
  803. {
  804. for(std::vector<std::string>::iterator i = commands.begin();
  805. i != commands.end(); ++i)
  806. {
  807. this->LocalGenerator->ExpandRuleVariables(*i, vars);
  808. }
  809. }
  810. this->LocalGenerator->TargetImplib = "";
  811. // Optionally convert the build rule to use a script to avoid long
  812. // command lines in the make shell.
  813. if(useLinkScript)
  814. {
  815. for(std::vector<std::string>::iterator cmd = link_script_commands.begin();
  816. cmd != link_script_commands.end(); ++cmd)
  817. {
  818. // Do not write out empty commands or commands beginning in the
  819. // shell no-op ":".
  820. if(!cmd->empty() && (*cmd)[0] != ':')
  821. {
  822. (*linkScriptStream) << *cmd << "\n";
  823. }
  824. }
  825. }
  826. // Write the build rule.
  827. this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, 0,
  828. targetFullPathReal.c_str(),
  829. depends, commands, false);
  830. // Some targets have more than one output file. Create rules to
  831. // drive the build if any extra outputs are missing.
  832. std::vector<std::string> extraOutputs;
  833. if(targetNameSO != targetNameReal)
  834. {
  835. this->GenerateExtraOutput(targetFullPathSO.c_str(),
  836. targetFullPathReal.c_str());
  837. }
  838. if(targetName != targetNameSO &&
  839. targetName != targetNameReal)
  840. {
  841. this->GenerateExtraOutput(targetFullPath.c_str(),
  842. targetFullPathReal.c_str());
  843. }
  844. // Write the main driver rule to build everything in this target.
  845. this->WriteTargetDriverRule(targetFullPath.c_str(), relink);
  846. // Clean all the possible library names and symlinks and object files.
  847. this->CleanFiles.insert(this->CleanFiles.end(),
  848. libCleanFiles.begin(),libCleanFiles.end());
  849. this->CleanFiles.insert(this->CleanFiles.end(),
  850. this->Objects.begin(),
  851. this->Objects.end());
  852. }