cmNinjaNormalTargetGenerator.cxx 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmNinjaNormalTargetGenerator.h"
  4. #include <algorithm>
  5. #include <assert.h>
  6. #include <iterator>
  7. #include <limits>
  8. #include <map>
  9. #include <set>
  10. #include <sstream>
  11. #include <stddef.h>
  12. #include "cmAlgorithms.h"
  13. #include "cmCustomCommand.h"
  14. #include "cmCustomCommandGenerator.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmGeneratorTarget.h"
  17. #include "cmGlobalNinjaGenerator.h"
  18. #include "cmLinkLineComputer.h"
  19. #include "cmLinkLineDeviceComputer.h"
  20. #include "cmLocalGenerator.h"
  21. #include "cmLocalNinjaGenerator.h"
  22. #include "cmMakefile.h"
  23. #include "cmNinjaTypes.h"
  24. #include "cmOSXBundleGenerator.h"
  25. #include "cmOutputConverter.h"
  26. #include "cmRulePlaceholderExpander.h"
  27. #include "cmSourceFile.h"
  28. #include "cmState.h"
  29. #include "cmStateDirectory.h"
  30. #include "cmStateSnapshot.h"
  31. #include "cmStateTypes.h"
  32. #include "cmSystemTools.h"
  33. #include "cm_auto_ptr.hxx"
  34. #include "cmake.h"
  35. #ifndef _WIN32
  36. #include <unistd.h>
  37. #endif
  38. cmNinjaNormalTargetGenerator::cmNinjaNormalTargetGenerator(
  39. cmGeneratorTarget* target)
  40. : cmNinjaTargetGenerator(target)
  41. , TargetNameOut()
  42. , TargetNameSO()
  43. , TargetNameReal()
  44. , TargetNameImport()
  45. , TargetNamePDB()
  46. , TargetLinkLanguage("")
  47. , DeviceLinkObject()
  48. {
  49. this->TargetLinkLanguage = target->GetLinkerLanguage(this->GetConfigName());
  50. if (target->GetType() == cmStateEnums::EXECUTABLE) {
  51. this->GetGeneratorTarget()->GetExecutableNames(
  52. this->TargetNameOut, this->TargetNameReal, this->TargetNameImport,
  53. this->TargetNamePDB, GetLocalGenerator()->GetConfigName());
  54. } else {
  55. this->GetGeneratorTarget()->GetLibraryNames(
  56. this->TargetNameOut, this->TargetNameSO, this->TargetNameReal,
  57. this->TargetNameImport, this->TargetNamePDB,
  58. GetLocalGenerator()->GetConfigName());
  59. }
  60. if (target->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  61. // on Windows the output dir is already needed at compile time
  62. // ensure the directory exists (OutDir test)
  63. EnsureDirectoryExists(target->GetDirectory(this->GetConfigName()));
  64. }
  65. this->OSXBundleGenerator =
  66. new cmOSXBundleGenerator(target, this->GetConfigName());
  67. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  68. }
  69. cmNinjaNormalTargetGenerator::~cmNinjaNormalTargetGenerator()
  70. {
  71. delete this->OSXBundleGenerator;
  72. }
  73. void cmNinjaNormalTargetGenerator::Generate()
  74. {
  75. if (this->TargetLinkLanguage.empty()) {
  76. cmSystemTools::Error("CMake can not determine linker language for "
  77. "target: ",
  78. this->GetGeneratorTarget()->GetName().c_str());
  79. return;
  80. }
  81. // Write the rules for each language.
  82. this->WriteLanguagesRules();
  83. // Write the build statements
  84. this->WriteObjectBuildStatements();
  85. if (this->GetGeneratorTarget()->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  86. this->WriteObjectLibStatement();
  87. } else {
  88. // If this target has cuda language link inputs, and we need to do
  89. // device linking
  90. this->WriteDeviceLinkStatement();
  91. this->WriteLinkStatement();
  92. }
  93. }
  94. void cmNinjaNormalTargetGenerator::WriteLanguagesRules()
  95. {
  96. #ifdef NINJA_GEN_VERBOSE_FILES
  97. cmGlobalNinjaGenerator::WriteDivider(this->GetRulesFileStream());
  98. this->GetRulesFileStream()
  99. << "# Rules for each languages for "
  100. << cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType())
  101. << " target " << this->GetTargetName() << "\n\n";
  102. #endif
  103. // Write rules for languages compiled in this target.
  104. std::set<std::string> languages;
  105. std::vector<cmSourceFile const*> sourceFiles;
  106. this->GetGeneratorTarget()->GetObjectSources(
  107. sourceFiles, this->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  108. for (std::vector<cmSourceFile const*>::const_iterator i =
  109. sourceFiles.begin();
  110. i != sourceFiles.end(); ++i) {
  111. const std::string& lang = (*i)->GetLanguage();
  112. if (!lang.empty()) {
  113. languages.insert(lang);
  114. }
  115. }
  116. for (std::set<std::string>::const_iterator l = languages.begin();
  117. l != languages.end(); ++l) {
  118. this->WriteLanguageRules(*l);
  119. }
  120. }
  121. const char* cmNinjaNormalTargetGenerator::GetVisibleTypeName() const
  122. {
  123. switch (this->GetGeneratorTarget()->GetType()) {
  124. case cmStateEnums::STATIC_LIBRARY:
  125. return "static library";
  126. case cmStateEnums::SHARED_LIBRARY:
  127. return "shared library";
  128. case cmStateEnums::MODULE_LIBRARY:
  129. if (this->GetGeneratorTarget()->IsCFBundleOnApple()) {
  130. return "CFBundle shared module";
  131. } else {
  132. return "shared module";
  133. }
  134. case cmStateEnums::EXECUTABLE:
  135. return "executable";
  136. default:
  137. return CM_NULLPTR;
  138. }
  139. }
  140. std::string cmNinjaNormalTargetGenerator::LanguageLinkerRule() const
  141. {
  142. return this->TargetLinkLanguage + "_" +
  143. cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType()) +
  144. "_LINKER__" + cmGlobalNinjaGenerator::EncodeRuleName(
  145. this->GetGeneratorTarget()->GetName());
  146. }
  147. std::string cmNinjaNormalTargetGenerator::LanguageLinkerDeviceRule() const
  148. {
  149. return this->TargetLinkLanguage + "_" +
  150. cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType()) +
  151. "_DEVICE_LINKER__" + cmGlobalNinjaGenerator::EncodeRuleName(
  152. this->GetGeneratorTarget()->GetName());
  153. }
  154. struct cmNinjaRemoveNoOpCommands
  155. {
  156. bool operator()(std::string const& cmd)
  157. {
  158. return cmd.empty() || cmd[0] == ':';
  159. }
  160. };
  161. void cmNinjaNormalTargetGenerator::WriteDeviceLinkRule(bool useResponseFile)
  162. {
  163. cmStateEnums::TargetType targetType = this->GetGeneratorTarget()->GetType();
  164. std::string ruleName = this->LanguageLinkerDeviceRule();
  165. // Select whether to use a response file for objects.
  166. std::string rspfile;
  167. std::string rspcontent;
  168. if (!this->GetGlobalGenerator()->HasRule(ruleName)) {
  169. cmRulePlaceholderExpander::RuleVariables vars;
  170. vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
  171. vars.CMTargetType =
  172. cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType());
  173. vars.Language = "CUDA";
  174. std::string responseFlag;
  175. if (!useResponseFile) {
  176. vars.Objects = "$in";
  177. vars.LinkLibraries = "$LINK_LIBRARIES";
  178. } else {
  179. std::string cmakeVarLang = "CMAKE_";
  180. cmakeVarLang += this->TargetLinkLanguage;
  181. // build response file name
  182. std::string cmakeLinkVar = cmakeVarLang + "_RESPONSE_FILE_LINK_FLAG";
  183. const char* flag = GetMakefile()->GetDefinition(cmakeLinkVar);
  184. if (flag) {
  185. responseFlag = flag;
  186. } else {
  187. responseFlag = "@";
  188. }
  189. rspfile = "$RSP_FILE";
  190. responseFlag += rspfile;
  191. // build response file content
  192. if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
  193. rspcontent = "$in";
  194. } else {
  195. rspcontent = "$in_newline";
  196. }
  197. rspcontent += " $LINK_LIBRARIES";
  198. vars.Objects = responseFlag.c_str();
  199. vars.LinkLibraries = "";
  200. }
  201. vars.ObjectDir = "$OBJECT_DIR";
  202. vars.Target = "$TARGET_FILE";
  203. vars.SONameFlag = "$SONAME_FLAG";
  204. vars.TargetSOName = "$SONAME";
  205. vars.TargetPDB = "$TARGET_PDB";
  206. vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
  207. vars.Flags = "$FLAGS";
  208. vars.LinkFlags = "$LINK_FLAGS";
  209. vars.Manifests = "$MANIFESTS";
  210. std::string langFlags;
  211. if (targetType != cmStateEnums::EXECUTABLE) {
  212. langFlags += "$LANGUAGE_COMPILE_FLAGS $ARCH_FLAGS";
  213. vars.LanguageCompileFlags = langFlags.c_str();
  214. }
  215. std::string launcher;
  216. const char* val = this->GetLocalGenerator()->GetRuleLauncher(
  217. this->GetGeneratorTarget(), "RULE_LAUNCH_LINK");
  218. if (val && *val) {
  219. launcher = val;
  220. launcher += " ";
  221. }
  222. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  223. this->GetLocalGenerator()->CreateRulePlaceholderExpander());
  224. // Rule for linking library/executable.
  225. std::vector<std::string> linkCmds = this->ComputeDeviceLinkCmd();
  226. for (std::vector<std::string>::iterator i = linkCmds.begin();
  227. i != linkCmds.end(); ++i) {
  228. *i = launcher + *i;
  229. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
  230. *i, vars);
  231. }
  232. {
  233. // If there is no ranlib the command will be ":". Skip it.
  234. std::vector<std::string>::iterator newEnd = std::remove_if(
  235. linkCmds.begin(), linkCmds.end(), cmNinjaRemoveNoOpCommands());
  236. linkCmds.erase(newEnd, linkCmds.end());
  237. }
  238. std::string linkCmd =
  239. this->GetLocalGenerator()->BuildCommandLine(linkCmds);
  240. // Write the linker rule with response file if needed.
  241. std::ostringstream comment;
  242. comment << "Rule for linking " << this->TargetLinkLanguage << " "
  243. << this->GetVisibleTypeName() << ".";
  244. std::ostringstream description;
  245. description << "Linking " << this->TargetLinkLanguage << " "
  246. << this->GetVisibleTypeName() << " $TARGET_FILE";
  247. this->GetGlobalGenerator()->AddRule(ruleName, linkCmd, description.str(),
  248. comment.str(),
  249. /*depfile*/ "",
  250. /*deptype*/ "", rspfile, rspcontent,
  251. /*restat*/ "$RESTAT",
  252. /*generator*/ false);
  253. }
  254. }
  255. void cmNinjaNormalTargetGenerator::WriteLinkRule(bool useResponseFile)
  256. {
  257. cmStateEnums::TargetType targetType = this->GetGeneratorTarget()->GetType();
  258. std::string ruleName = this->LanguageLinkerRule();
  259. // Select whether to use a response file for objects.
  260. std::string rspfile;
  261. std::string rspcontent;
  262. if (!this->GetGlobalGenerator()->HasRule(ruleName)) {
  263. cmRulePlaceholderExpander::RuleVariables vars;
  264. vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
  265. vars.CMTargetType =
  266. cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType());
  267. vars.Language = this->TargetLinkLanguage.c_str();
  268. std::string responseFlag;
  269. if (!useResponseFile) {
  270. vars.Objects = "$in";
  271. vars.LinkLibraries = "$LINK_PATH $LINK_LIBRARIES";
  272. } else {
  273. std::string cmakeVarLang = "CMAKE_";
  274. cmakeVarLang += this->TargetLinkLanguage;
  275. // build response file name
  276. std::string cmakeLinkVar = cmakeVarLang + "_RESPONSE_FILE_LINK_FLAG";
  277. const char* flag = GetMakefile()->GetDefinition(cmakeLinkVar);
  278. if (flag) {
  279. responseFlag = flag;
  280. } else {
  281. responseFlag = "@";
  282. }
  283. rspfile = "$RSP_FILE";
  284. responseFlag += rspfile;
  285. // build response file content
  286. if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
  287. rspcontent = "$in";
  288. } else {
  289. rspcontent = "$in_newline";
  290. }
  291. rspcontent += " $LINK_PATH $LINK_LIBRARIES";
  292. vars.Objects = responseFlag.c_str();
  293. vars.LinkLibraries = "";
  294. }
  295. vars.ObjectDir = "$OBJECT_DIR";
  296. vars.Target = "$TARGET_FILE";
  297. vars.SONameFlag = "$SONAME_FLAG";
  298. vars.TargetSOName = "$SONAME";
  299. vars.TargetInstallNameDir = "$INSTALLNAME_DIR";
  300. vars.TargetPDB = "$TARGET_PDB";
  301. // Setup the target version.
  302. std::string targetVersionMajor;
  303. std::string targetVersionMinor;
  304. {
  305. std::ostringstream majorStream;
  306. std::ostringstream minorStream;
  307. int major;
  308. int minor;
  309. this->GetGeneratorTarget()->GetTargetVersion(major, minor);
  310. majorStream << major;
  311. minorStream << minor;
  312. targetVersionMajor = majorStream.str();
  313. targetVersionMinor = minorStream.str();
  314. }
  315. vars.TargetVersionMajor = targetVersionMajor.c_str();
  316. vars.TargetVersionMinor = targetVersionMinor.c_str();
  317. vars.Flags = "$FLAGS";
  318. vars.LinkFlags = "$LINK_FLAGS";
  319. vars.Manifests = "$MANIFESTS";
  320. std::string langFlags;
  321. if (targetType != cmStateEnums::EXECUTABLE) {
  322. langFlags += "$LANGUAGE_COMPILE_FLAGS $ARCH_FLAGS";
  323. vars.LanguageCompileFlags = langFlags.c_str();
  324. }
  325. std::string launcher;
  326. const char* val = this->GetLocalGenerator()->GetRuleLauncher(
  327. this->GetGeneratorTarget(), "RULE_LAUNCH_LINK");
  328. if (val && *val) {
  329. launcher = val;
  330. launcher += " ";
  331. }
  332. CM_AUTO_PTR<cmRulePlaceholderExpander> rulePlaceholderExpander(
  333. this->GetLocalGenerator()->CreateRulePlaceholderExpander());
  334. // Rule for linking library/executable.
  335. std::vector<std::string> linkCmds = this->ComputeLinkCmd();
  336. for (std::vector<std::string>::iterator i = linkCmds.begin();
  337. i != linkCmds.end(); ++i) {
  338. *i = launcher + *i;
  339. rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
  340. *i, vars);
  341. }
  342. {
  343. // If there is no ranlib the command will be ":". Skip it.
  344. std::vector<std::string>::iterator newEnd = std::remove_if(
  345. linkCmds.begin(), linkCmds.end(), cmNinjaRemoveNoOpCommands());
  346. linkCmds.erase(newEnd, linkCmds.end());
  347. }
  348. linkCmds.insert(linkCmds.begin(), "$PRE_LINK");
  349. linkCmds.push_back("$POST_BUILD");
  350. std::string linkCmd =
  351. this->GetLocalGenerator()->BuildCommandLine(linkCmds);
  352. // Write the linker rule with response file if needed.
  353. std::ostringstream comment;
  354. comment << "Rule for linking " << this->TargetLinkLanguage << " "
  355. << this->GetVisibleTypeName() << ".";
  356. std::ostringstream description;
  357. description << "Linking " << this->TargetLinkLanguage << " "
  358. << this->GetVisibleTypeName() << " $TARGET_FILE";
  359. this->GetGlobalGenerator()->AddRule(ruleName, linkCmd, description.str(),
  360. comment.str(),
  361. /*depfile*/ "",
  362. /*deptype*/ "", rspfile, rspcontent,
  363. /*restat*/ "$RESTAT",
  364. /*generator*/ false);
  365. }
  366. if (this->TargetNameOut != this->TargetNameReal &&
  367. !this->GetGeneratorTarget()->IsFrameworkOnApple()) {
  368. std::string cmakeCommand =
  369. this->GetLocalGenerator()->ConvertToOutputFormat(
  370. cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
  371. if (targetType == cmStateEnums::EXECUTABLE) {
  372. this->GetGlobalGenerator()->AddRule(
  373. "CMAKE_SYMLINK_EXECUTABLE",
  374. cmakeCommand + " -E cmake_symlink_executable"
  375. " $in $out && $POST_BUILD",
  376. "Creating executable symlink $out", "Rule for creating "
  377. "executable symlink.",
  378. /*depfile*/ "",
  379. /*deptype*/ "",
  380. /*rspfile*/ "",
  381. /*rspcontent*/ "",
  382. /*restat*/ "",
  383. /*generator*/ false);
  384. } else {
  385. this->GetGlobalGenerator()->AddRule(
  386. "CMAKE_SYMLINK_LIBRARY",
  387. cmakeCommand + " -E cmake_symlink_library"
  388. " $in $SONAME $out && $POST_BUILD",
  389. "Creating library symlink $out", "Rule for creating "
  390. "library symlink.",
  391. /*depfile*/ "",
  392. /*deptype*/ "",
  393. /*rspfile*/ "",
  394. /*rspcontent*/ "",
  395. /*restat*/ "",
  396. /*generator*/ false);
  397. }
  398. }
  399. }
  400. std::vector<std::string> cmNinjaNormalTargetGenerator::ComputeDeviceLinkCmd()
  401. {
  402. std::vector<std::string> linkCmds;
  403. // this target requires separable cuda compilation
  404. // now build the correct command depending on if the target is
  405. // an executable or a dynamic library.
  406. std::string linkCmd;
  407. switch (this->GetGeneratorTarget()->GetType()) {
  408. case cmStateEnums::SHARED_LIBRARY:
  409. case cmStateEnums::MODULE_LIBRARY: {
  410. const std::string cudaLinkCmd(
  411. this->GetMakefile()->GetDefinition("CMAKE_CUDA_DEVICE_LINK_LIBRARY"));
  412. cmSystemTools::ExpandListArgument(cudaLinkCmd, linkCmds);
  413. } break;
  414. case cmStateEnums::EXECUTABLE: {
  415. const std::string cudaLinkCmd(this->GetMakefile()->GetDefinition(
  416. "CMAKE_CUDA_DEVICE_LINK_EXECUTABLE"));
  417. cmSystemTools::ExpandListArgument(cudaLinkCmd, linkCmds);
  418. } break;
  419. default:
  420. break;
  421. }
  422. return linkCmds;
  423. }
  424. std::vector<std::string> cmNinjaNormalTargetGenerator::ComputeLinkCmd()
  425. {
  426. std::vector<std::string> linkCmds;
  427. cmMakefile* mf = this->GetMakefile();
  428. {
  429. // If we have a rule variable prefer it. In the case of static libraries
  430. // this occurs when things like IPO is enabled, and we need to use the
  431. // CMAKE_<lang>_CREATE_STATIC_LIBRARY_IPO define instead.
  432. std::string linkCmdVar = this->GetGeneratorTarget()->GetCreateRuleVariable(
  433. this->TargetLinkLanguage, this->GetConfigName());
  434. const char* linkCmd = mf->GetDefinition(linkCmdVar);
  435. if (linkCmd) {
  436. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  437. if (this->GetGeneratorTarget()->GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
  438. std::string cmakeCommand =
  439. this->GetLocalGenerator()->ConvertToOutputFormat(
  440. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  441. cmakeCommand += " -E __run_iwyu --lwyu=";
  442. cmGeneratorTarget& gt = *this->GetGeneratorTarget();
  443. const std::string cfgName = this->GetConfigName();
  444. std::string targetOutput = ConvertToNinjaPath(gt.GetFullPath(cfgName));
  445. std::string targetOutputReal =
  446. this->ConvertToNinjaPath(gt.GetFullPath(cfgName,
  447. /*implib=*/false,
  448. /*realname=*/true));
  449. cmakeCommand += targetOutputReal;
  450. cmakeCommand += " || true";
  451. linkCmds.push_back(cmakeCommand);
  452. }
  453. return linkCmds;
  454. }
  455. }
  456. switch (this->GetGeneratorTarget()->GetType()) {
  457. case cmStateEnums::STATIC_LIBRARY: {
  458. // We have archive link commands set. First, delete the existing archive.
  459. {
  460. std::string cmakeCommand =
  461. this->GetLocalGenerator()->ConvertToOutputFormat(
  462. cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
  463. linkCmds.push_back(cmakeCommand + " -E remove $TARGET_FILE");
  464. }
  465. // TODO: Use ARCHIVE_APPEND for archives over a certain size.
  466. {
  467. std::string linkCmdVar = "CMAKE_";
  468. linkCmdVar += this->TargetLinkLanguage;
  469. linkCmdVar += "_ARCHIVE_CREATE";
  470. const char* linkCmd = mf->GetRequiredDefinition(linkCmdVar);
  471. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  472. }
  473. {
  474. std::string linkCmdVar = "CMAKE_";
  475. linkCmdVar += this->TargetLinkLanguage;
  476. linkCmdVar += "_ARCHIVE_FINISH";
  477. const char* linkCmd = mf->GetRequiredDefinition(linkCmdVar);
  478. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  479. }
  480. return linkCmds;
  481. }
  482. case cmStateEnums::SHARED_LIBRARY:
  483. case cmStateEnums::MODULE_LIBRARY:
  484. case cmStateEnums::EXECUTABLE:
  485. break;
  486. default:
  487. assert(false && "Unexpected target type");
  488. }
  489. return std::vector<std::string>();
  490. }
  491. static int calculateCommandLineLengthLimit(int linkRuleLength)
  492. {
  493. static int const limits[] = {
  494. #ifdef _WIN32
  495. 8000,
  496. #endif
  497. #if defined(_SC_ARG_MAX)
  498. // for instance ARG_MAX is 2096152 on Ubuntu or 262144 on Mac
  499. ((int)sysconf(_SC_ARG_MAX)) - 1000,
  500. #endif
  501. #if defined(__linux)
  502. // #define MAX_ARG_STRLEN (PAGE_SIZE * 32) in Linux's binfmts.h
  503. ((int)sysconf(_SC_PAGESIZE) * 32) - 1000,
  504. #endif
  505. std::numeric_limits<int>::max()
  506. };
  507. size_t const arrSz = cmArraySize(limits);
  508. int const sz = *std::min_element(limits, limits + arrSz);
  509. if (sz == std::numeric_limits<int>::max()) {
  510. return 0;
  511. }
  512. return sz - linkRuleLength;
  513. }
  514. void cmNinjaNormalTargetGenerator::WriteDeviceLinkStatement()
  515. {
  516. cmGeneratorTarget& genTarget = *this->GetGeneratorTarget();
  517. // determine if we need to do any device linking for this target
  518. const std::string cuda_lang("CUDA");
  519. cmGeneratorTarget::LinkClosure const* closure =
  520. genTarget.GetLinkClosure(this->GetConfigName());
  521. const bool hasCUDA =
  522. (std::find(closure->Languages.begin(), closure->Languages.end(),
  523. cuda_lang) != closure->Languages.end());
  524. bool shouldHaveDeviceLinking = false;
  525. switch (genTarget.GetType()) {
  526. case cmStateEnums::SHARED_LIBRARY:
  527. case cmStateEnums::MODULE_LIBRARY:
  528. case cmStateEnums::EXECUTABLE:
  529. shouldHaveDeviceLinking = true;
  530. break;
  531. default:
  532. break;
  533. }
  534. if (!shouldHaveDeviceLinking || !hasCUDA) {
  535. return;
  536. }
  537. // Now we can do device linking
  538. // First and very important step is to make sure while inside this
  539. // step our link language is set to CUDA
  540. std::string cudaLinkLanguage = "CUDA";
  541. std::string const objExt =
  542. this->Makefile->GetSafeDefinition("CMAKE_CUDA_OUTPUT_EXTENSION");
  543. std::string const cfgName = this->GetConfigName();
  544. std::string const targetOutputReal = ConvertToNinjaPath(
  545. genTarget.ObjectDirectory + "cmake_device_link" + objExt);
  546. std::string const targetOutputImplib =
  547. ConvertToNinjaPath(genTarget.GetFullPath(cfgName,
  548. /*implib=*/true));
  549. this->DeviceLinkObject = targetOutputReal;
  550. // Write comments.
  551. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  552. const cmStateEnums::TargetType targetType = genTarget.GetType();
  553. this->GetBuildFileStream() << "# Device Link build statements for "
  554. << cmState::GetTargetTypeName(targetType)
  555. << " target " << this->GetTargetName() << "\n\n";
  556. // Compute the comment.
  557. std::ostringstream comment;
  558. comment << "Link the " << this->GetVisibleTypeName() << " "
  559. << targetOutputReal;
  560. cmNinjaDeps emptyDeps;
  561. cmNinjaVars vars;
  562. // Compute outputs.
  563. cmNinjaDeps outputs;
  564. outputs.push_back(targetOutputReal);
  565. // Compute specific libraries to link with.
  566. cmNinjaDeps explicitDeps = this->GetObjects();
  567. cmNinjaDeps implicitDeps = this->ComputeLinkDeps();
  568. std::string frameworkPath;
  569. std::string linkPath;
  570. std::string createRule = genTarget.GetCreateRuleVariable(
  571. this->TargetLinkLanguage, this->GetConfigName());
  572. const bool useWatcomQuote =
  573. this->GetMakefile()->IsOn(createRule + "_USE_WATCOM_QUOTE");
  574. cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator();
  575. vars["TARGET_FILE"] =
  576. localGen.ConvertToOutputFormat(targetOutputReal, cmOutputConverter::SHELL);
  577. CM_AUTO_PTR<cmLinkLineComputer> linkLineComputer(
  578. new cmNinjaLinkLineDeviceComputer(
  579. this->GetLocalGenerator(),
  580. this->GetLocalGenerator()->GetStateSnapshot().GetDirectory(),
  581. this->GetGlobalGenerator()));
  582. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  583. localGen.GetTargetFlags(
  584. linkLineComputer.get(), this->GetConfigName(), vars["LINK_LIBRARIES"],
  585. vars["FLAGS"], vars["LINK_FLAGS"], frameworkPath, linkPath, &genTarget);
  586. this->addPoolNinjaVariable("JOB_POOL_LINK", &genTarget, vars);
  587. vars["LINK_FLAGS"] =
  588. cmGlobalNinjaGenerator::EncodeLiteral(vars["LINK_FLAGS"]);
  589. vars["MANIFESTS"] = this->GetManifests();
  590. vars["LINK_PATH"] = frameworkPath + linkPath;
  591. // Compute architecture specific link flags. Yes, these go into a different
  592. // variable for executables, probably due to a mistake made when duplicating
  593. // code between the Makefile executable and library generators.
  594. if (targetType == cmStateEnums::EXECUTABLE) {
  595. std::string t = vars["FLAGS"];
  596. localGen.AddArchitectureFlags(t, &genTarget, cudaLinkLanguage, cfgName);
  597. vars["FLAGS"] = t;
  598. } else {
  599. std::string t = vars["ARCH_FLAGS"];
  600. localGen.AddArchitectureFlags(t, &genTarget, cudaLinkLanguage, cfgName);
  601. vars["ARCH_FLAGS"] = t;
  602. t = "";
  603. localGen.AddLanguageFlags(t, cudaLinkLanguage, cfgName);
  604. vars["LANGUAGE_COMPILE_FLAGS"] = t;
  605. }
  606. if (this->GetGeneratorTarget()->HasSOName(cfgName)) {
  607. vars["SONAME_FLAG"] =
  608. this->GetMakefile()->GetSONameFlag(this->TargetLinkLanguage);
  609. vars["SONAME"] = this->TargetNameSO;
  610. if (targetType == cmStateEnums::SHARED_LIBRARY) {
  611. std::string install_dir =
  612. this->GetGeneratorTarget()->GetInstallNameDirForBuildTree(cfgName);
  613. if (!install_dir.empty()) {
  614. vars["INSTALLNAME_DIR"] = localGen.ConvertToOutputFormat(
  615. install_dir, cmOutputConverter::SHELL);
  616. }
  617. }
  618. }
  619. cmNinjaDeps byproducts;
  620. if (!this->TargetNameImport.empty()) {
  621. const std::string impLibPath = localGen.ConvertToOutputFormat(
  622. targetOutputImplib, cmOutputConverter::SHELL);
  623. vars["TARGET_IMPLIB"] = impLibPath;
  624. EnsureParentDirectoryExists(impLibPath);
  625. if (genTarget.HasImportLibrary()) {
  626. byproducts.push_back(targetOutputImplib);
  627. }
  628. }
  629. const std::string objPath = GetGeneratorTarget()->GetSupportDirectory();
  630. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  631. this->ConvertToNinjaPath(objPath), cmOutputConverter::SHELL);
  632. EnsureDirectoryExists(objPath);
  633. this->SetMsvcTargetPdbVariable(vars);
  634. if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
  635. // ar.exe can't handle backslashes in rsp files (implicitly used by gcc)
  636. std::string& linkLibraries = vars["LINK_LIBRARIES"];
  637. std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/');
  638. std::string& link_path = vars["LINK_PATH"];
  639. std::replace(link_path.begin(), link_path.end(), '\\', '/');
  640. }
  641. const std::vector<cmCustomCommand>* cmdLists[3] = {
  642. &genTarget.GetPreBuildCommands(), &genTarget.GetPreLinkCommands(),
  643. &genTarget.GetPostBuildCommands()
  644. };
  645. std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
  646. vars["PRE_LINK"] = localGen.BuildCommandLine(preLinkCmdLines);
  647. vars["POST_BUILD"] = localGen.BuildCommandLine(postBuildCmdLines);
  648. std::vector<std::string>* cmdLineLists[3] = { &preLinkCmdLines,
  649. &preLinkCmdLines,
  650. &postBuildCmdLines };
  651. for (unsigned i = 0; i != 3; ++i) {
  652. for (std::vector<cmCustomCommand>::const_iterator ci =
  653. cmdLists[i]->begin();
  654. ci != cmdLists[i]->end(); ++ci) {
  655. cmCustomCommandGenerator ccg(*ci, cfgName, this->GetLocalGenerator());
  656. localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
  657. std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
  658. std::transform(ccByproducts.begin(), ccByproducts.end(),
  659. std::back_inserter(byproducts), MapToNinjaPath());
  660. }
  661. }
  662. cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
  663. // Device linking currently doesn't support response files so
  664. // do not check if the user has explicitly forced a response file.
  665. int const commandLineLengthLimit = calculateCommandLineLengthLimit(
  666. globalGen.GetRuleCmdLength(this->LanguageLinkerDeviceRule()));
  667. const std::string rspfile =
  668. std::string(cmake::GetCMakeFilesDirectoryPostSlash()) +
  669. genTarget.GetName() + ".rsp";
  670. // Gather order-only dependencies.
  671. cmNinjaDeps orderOnlyDeps;
  672. this->GetLocalGenerator()->AppendTargetDepends(this->GetGeneratorTarget(),
  673. orderOnlyDeps);
  674. // Write the build statement for this target.
  675. bool usedResponseFile = false;
  676. globalGen.WriteBuild(this->GetBuildFileStream(), comment.str(),
  677. this->LanguageLinkerDeviceRule(), outputs,
  678. /*implicitOuts=*/cmNinjaDeps(), explicitDeps,
  679. implicitDeps, orderOnlyDeps, vars, rspfile,
  680. commandLineLengthLimit, &usedResponseFile);
  681. this->WriteDeviceLinkRule(usedResponseFile);
  682. }
  683. void cmNinjaNormalTargetGenerator::WriteLinkStatement()
  684. {
  685. cmGeneratorTarget& gt = *this->GetGeneratorTarget();
  686. const std::string cfgName = this->GetConfigName();
  687. std::string targetOutput = ConvertToNinjaPath(gt.GetFullPath(cfgName));
  688. std::string targetOutputReal =
  689. ConvertToNinjaPath(gt.GetFullPath(cfgName,
  690. /*implib=*/false,
  691. /*realname=*/true));
  692. std::string targetOutputImplib =
  693. ConvertToNinjaPath(gt.GetFullPath(cfgName,
  694. /*implib=*/true));
  695. if (gt.IsAppBundleOnApple()) {
  696. // Create the app bundle
  697. std::string outpath = gt.GetDirectory(cfgName);
  698. this->OSXBundleGenerator->CreateAppBundle(this->TargetNameOut, outpath);
  699. // Calculate the output path
  700. targetOutput = outpath;
  701. targetOutput += "/";
  702. targetOutput += this->TargetNameOut;
  703. targetOutput = this->ConvertToNinjaPath(targetOutput);
  704. targetOutputReal = outpath;
  705. targetOutputReal += "/";
  706. targetOutputReal += this->TargetNameReal;
  707. targetOutputReal = this->ConvertToNinjaPath(targetOutputReal);
  708. } else if (gt.IsFrameworkOnApple()) {
  709. // Create the library framework.
  710. this->OSXBundleGenerator->CreateFramework(this->TargetNameOut,
  711. gt.GetDirectory(cfgName));
  712. } else if (gt.IsCFBundleOnApple()) {
  713. // Create the core foundation bundle.
  714. this->OSXBundleGenerator->CreateCFBundle(this->TargetNameOut,
  715. gt.GetDirectory(cfgName));
  716. }
  717. // Write comments.
  718. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  719. const cmStateEnums::TargetType targetType = gt.GetType();
  720. this->GetBuildFileStream() << "# Link build statements for "
  721. << cmState::GetTargetTypeName(targetType)
  722. << " target " << this->GetTargetName() << "\n\n";
  723. cmNinjaDeps emptyDeps;
  724. cmNinjaVars vars;
  725. // Compute the comment.
  726. std::ostringstream comment;
  727. comment << "Link the " << this->GetVisibleTypeName() << " "
  728. << targetOutputReal;
  729. // Compute outputs.
  730. cmNinjaDeps outputs;
  731. outputs.push_back(targetOutputReal);
  732. // Compute specific libraries to link with.
  733. cmNinjaDeps explicitDeps = this->GetObjects();
  734. cmNinjaDeps implicitDeps = this->ComputeLinkDeps();
  735. if (!this->DeviceLinkObject.empty()) {
  736. explicitDeps.push_back(this->DeviceLinkObject);
  737. }
  738. cmMakefile* mf = this->GetMakefile();
  739. std::string frameworkPath;
  740. std::string linkPath;
  741. cmGeneratorTarget& genTarget = *this->GetGeneratorTarget();
  742. std::string createRule = genTarget.GetCreateRuleVariable(
  743. this->TargetLinkLanguage, this->GetConfigName());
  744. bool useWatcomQuote = mf->IsOn(createRule + "_USE_WATCOM_QUOTE");
  745. cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator();
  746. vars["TARGET_FILE"] =
  747. localGen.ConvertToOutputFormat(targetOutputReal, cmOutputConverter::SHELL);
  748. CM_AUTO_PTR<cmLinkLineComputer> linkLineComputer(
  749. this->GetGlobalGenerator()->CreateLinkLineComputer(
  750. this->GetLocalGenerator(),
  751. this->GetLocalGenerator()->GetStateSnapshot().GetDirectory()));
  752. linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
  753. localGen.GetTargetFlags(
  754. linkLineComputer.get(), this->GetConfigName(), vars["LINK_LIBRARIES"],
  755. vars["FLAGS"], vars["LINK_FLAGS"], frameworkPath, linkPath, &genTarget);
  756. if (this->GetMakefile()->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS") &&
  757. (gt.GetType() == cmStateEnums::SHARED_LIBRARY ||
  758. gt.IsExecutableWithExports())) {
  759. if (gt.GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS")) {
  760. std::string name_of_def_file = gt.GetSupportDirectory();
  761. name_of_def_file += "/" + gt.GetName();
  762. name_of_def_file += ".def ";
  763. vars["LINK_FLAGS"] += " /DEF:";
  764. vars["LINK_FLAGS"] += this->GetLocalGenerator()->ConvertToOutputFormat(
  765. name_of_def_file, cmOutputConverter::SHELL);
  766. }
  767. }
  768. // Add OS X version flags, if any.
  769. if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
  770. this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
  771. this->AppendOSXVerFlag(vars["LINK_FLAGS"], this->TargetLinkLanguage,
  772. "COMPATIBILITY", true);
  773. this->AppendOSXVerFlag(vars["LINK_FLAGS"], this->TargetLinkLanguage,
  774. "CURRENT", false);
  775. }
  776. this->addPoolNinjaVariable("JOB_POOL_LINK", &gt, vars);
  777. this->AddModuleDefinitionFlag(linkLineComputer.get(), vars["LINK_FLAGS"]);
  778. vars["LINK_FLAGS"] =
  779. cmGlobalNinjaGenerator::EncodeLiteral(vars["LINK_FLAGS"]);
  780. vars["MANIFESTS"] = this->GetManifests();
  781. vars["LINK_PATH"] = frameworkPath + linkPath;
  782. std::string lwyuFlags;
  783. if (genTarget.GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
  784. lwyuFlags = " -Wl,--no-as-needed";
  785. }
  786. // Compute architecture specific link flags. Yes, these go into a different
  787. // variable for executables, probably due to a mistake made when duplicating
  788. // code between the Makefile executable and library generators.
  789. if (targetType == cmStateEnums::EXECUTABLE) {
  790. std::string t = vars["FLAGS"];
  791. localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
  792. t += lwyuFlags;
  793. vars["FLAGS"] = t;
  794. } else {
  795. std::string t = vars["ARCH_FLAGS"];
  796. localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
  797. vars["ARCH_FLAGS"] = t;
  798. t = "";
  799. t += lwyuFlags;
  800. localGen.AddLanguageFlags(t, TargetLinkLanguage, cfgName);
  801. vars["LANGUAGE_COMPILE_FLAGS"] = t;
  802. }
  803. if (this->GetGeneratorTarget()->HasSOName(cfgName)) {
  804. vars["SONAME_FLAG"] = mf->GetSONameFlag(this->TargetLinkLanguage);
  805. vars["SONAME"] = this->TargetNameSO;
  806. if (targetType == cmStateEnums::SHARED_LIBRARY) {
  807. std::string install_dir =
  808. this->GetGeneratorTarget()->GetInstallNameDirForBuildTree(cfgName);
  809. if (!install_dir.empty()) {
  810. vars["INSTALLNAME_DIR"] = localGen.ConvertToOutputFormat(
  811. install_dir, cmOutputConverter::SHELL);
  812. }
  813. }
  814. }
  815. cmNinjaDeps byproducts;
  816. if (!this->TargetNameImport.empty()) {
  817. const std::string impLibPath = localGen.ConvertToOutputFormat(
  818. targetOutputImplib, cmOutputConverter::SHELL);
  819. vars["TARGET_IMPLIB"] = impLibPath;
  820. EnsureParentDirectoryExists(impLibPath);
  821. if (genTarget.HasImportLibrary()) {
  822. byproducts.push_back(targetOutputImplib);
  823. }
  824. }
  825. if (!this->SetMsvcTargetPdbVariable(vars)) {
  826. // It is common to place debug symbols at a specific place,
  827. // so we need a plain target name in the rule available.
  828. std::string prefix;
  829. std::string base;
  830. std::string suffix;
  831. this->GetGeneratorTarget()->GetFullNameComponents(prefix, base, suffix);
  832. std::string dbg_suffix = ".dbg";
  833. // TODO: Where to document?
  834. if (mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX")) {
  835. dbg_suffix = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX");
  836. }
  837. vars["TARGET_PDB"] = base + suffix + dbg_suffix;
  838. }
  839. const std::string objPath = GetGeneratorTarget()->GetSupportDirectory();
  840. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  841. this->ConvertToNinjaPath(objPath), cmOutputConverter::SHELL);
  842. EnsureDirectoryExists(objPath);
  843. if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
  844. // ar.exe can't handle backslashes in rsp files (implicitly used by gcc)
  845. std::string& linkLibraries = vars["LINK_LIBRARIES"];
  846. std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/');
  847. std::string& link_path = vars["LINK_PATH"];
  848. std::replace(link_path.begin(), link_path.end(), '\\', '/');
  849. }
  850. const std::vector<cmCustomCommand>* cmdLists[3] = {
  851. &gt.GetPreBuildCommands(), &gt.GetPreLinkCommands(),
  852. &gt.GetPostBuildCommands()
  853. };
  854. std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
  855. std::vector<std::string>* cmdLineLists[3] = { &preLinkCmdLines,
  856. &preLinkCmdLines,
  857. &postBuildCmdLines };
  858. for (unsigned i = 0; i != 3; ++i) {
  859. for (std::vector<cmCustomCommand>::const_iterator ci =
  860. cmdLists[i]->begin();
  861. ci != cmdLists[i]->end(); ++ci) {
  862. cmCustomCommandGenerator ccg(*ci, cfgName, this->GetLocalGenerator());
  863. localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
  864. std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
  865. std::transform(ccByproducts.begin(), ccByproducts.end(),
  866. std::back_inserter(byproducts), MapToNinjaPath());
  867. }
  868. }
  869. // maybe create .def file from list of objects
  870. if ((gt.GetType() == cmStateEnums::SHARED_LIBRARY ||
  871. gt.IsExecutableWithExports()) &&
  872. this->GetMakefile()->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS")) {
  873. if (gt.GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS")) {
  874. std::string cmakeCommand =
  875. this->GetLocalGenerator()->ConvertToOutputFormat(
  876. cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
  877. std::string name_of_def_file = gt.GetSupportDirectory();
  878. name_of_def_file += "/" + gt.GetName();
  879. name_of_def_file += ".def";
  880. std::string cmd = cmakeCommand;
  881. cmd += " -E __create_def ";
  882. cmd += this->GetLocalGenerator()->ConvertToOutputFormat(
  883. name_of_def_file, cmOutputConverter::SHELL);
  884. cmd += " ";
  885. cmNinjaDeps objs = this->GetObjects();
  886. std::string obj_list_file = name_of_def_file;
  887. obj_list_file += ".objs";
  888. cmd += this->GetLocalGenerator()->ConvertToOutputFormat(
  889. obj_list_file, cmOutputConverter::SHELL);
  890. preLinkCmdLines.push_back(cmd);
  891. // create a list of obj files for the -E __create_def to read
  892. cmGeneratedFileStream fout(obj_list_file.c_str());
  893. for (cmNinjaDeps::iterator i = objs.begin(); i != objs.end(); ++i) {
  894. if (cmHasLiteralSuffix(*i, ".obj")) {
  895. fout << *i << "\n";
  896. }
  897. }
  898. }
  899. }
  900. // If we have any PRE_LINK commands, we need to go back to CMAKE_BINARY_DIR
  901. // for
  902. // the link commands.
  903. if (!preLinkCmdLines.empty()) {
  904. const std::string homeOutDir = localGen.ConvertToOutputFormat(
  905. localGen.GetBinaryDirectory(), cmOutputConverter::SHELL);
  906. preLinkCmdLines.push_back("cd " + homeOutDir);
  907. }
  908. vars["PRE_LINK"] = localGen.BuildCommandLine(preLinkCmdLines);
  909. std::string postBuildCmdLine = localGen.BuildCommandLine(postBuildCmdLines);
  910. cmNinjaVars symlinkVars;
  911. bool const symlinkNeeded =
  912. (targetOutput != targetOutputReal && !gt.IsFrameworkOnApple());
  913. if (!symlinkNeeded) {
  914. vars["POST_BUILD"] = postBuildCmdLine;
  915. } else {
  916. vars["POST_BUILD"] = cmGlobalNinjaGenerator::SHELL_NOOP;
  917. symlinkVars["POST_BUILD"] = postBuildCmdLine;
  918. }
  919. cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
  920. bool const lang_supports_response =
  921. !(this->TargetLinkLanguage == "RC" || this->TargetLinkLanguage == "CUDA");
  922. int commandLineLengthLimit = -1;
  923. if (!lang_supports_response || !this->ForceResponseFile()) {
  924. commandLineLengthLimit = calculateCommandLineLengthLimit(
  925. globalGen.GetRuleCmdLength(this->LanguageLinkerRule()));
  926. }
  927. const std::string rspfile =
  928. std::string(cmake::GetCMakeFilesDirectoryPostSlash()) + gt.GetName() +
  929. ".rsp";
  930. // Gather order-only dependencies.
  931. cmNinjaDeps orderOnlyDeps;
  932. this->GetLocalGenerator()->AppendTargetDepends(this->GetGeneratorTarget(),
  933. orderOnlyDeps);
  934. // Ninja should restat after linking if and only if there are byproducts.
  935. vars["RESTAT"] = byproducts.empty() ? "" : "1";
  936. for (cmNinjaDeps::const_iterator oi = byproducts.begin(),
  937. oe = byproducts.end();
  938. oi != oe; ++oi) {
  939. this->GetGlobalGenerator()->SeenCustomCommandOutput(*oi);
  940. outputs.push_back(*oi);
  941. }
  942. // Write the build statement for this target.
  943. bool usedResponseFile = false;
  944. globalGen.WriteBuild(this->GetBuildFileStream(), comment.str(),
  945. this->LanguageLinkerRule(), outputs,
  946. /*implicitOuts=*/cmNinjaDeps(), explicitDeps,
  947. implicitDeps, orderOnlyDeps, vars, rspfile,
  948. commandLineLengthLimit, &usedResponseFile);
  949. this->WriteLinkRule(usedResponseFile);
  950. if (symlinkNeeded) {
  951. if (targetType == cmStateEnums::EXECUTABLE) {
  952. globalGen.WriteBuild(
  953. this->GetBuildFileStream(),
  954. "Create executable symlink " + targetOutput,
  955. "CMAKE_SYMLINK_EXECUTABLE", cmNinjaDeps(1, targetOutput),
  956. /*implicitOuts=*/cmNinjaDeps(), cmNinjaDeps(1, targetOutputReal),
  957. emptyDeps, emptyDeps, symlinkVars);
  958. } else {
  959. cmNinjaDeps symlinks;
  960. std::string const soName =
  961. this->ConvertToNinjaPath(this->GetTargetFilePath(this->TargetNameSO));
  962. // If one link has to be created.
  963. if (targetOutputReal == soName || targetOutput == soName) {
  964. symlinkVars["SONAME"] = soName;
  965. } else {
  966. symlinkVars["SONAME"] = "";
  967. symlinks.push_back(soName);
  968. }
  969. symlinks.push_back(targetOutput);
  970. globalGen.WriteBuild(
  971. this->GetBuildFileStream(), "Create library symlink " + targetOutput,
  972. "CMAKE_SYMLINK_LIBRARY", symlinks,
  973. /*implicitOuts=*/cmNinjaDeps(), cmNinjaDeps(1, targetOutputReal),
  974. emptyDeps, emptyDeps, symlinkVars);
  975. }
  976. }
  977. // Add aliases for the file name and the target name.
  978. globalGen.AddTargetAlias(this->TargetNameOut, &gt);
  979. globalGen.AddTargetAlias(this->GetTargetName(), &gt);
  980. }
  981. void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()
  982. {
  983. // Write a phony output that depends on all object files.
  984. cmNinjaDeps outputs;
  985. this->GetLocalGenerator()->AppendTargetOutputs(this->GetGeneratorTarget(),
  986. outputs);
  987. cmNinjaDeps depends = this->GetObjects();
  988. this->GetGlobalGenerator()->WritePhonyBuild(
  989. this->GetBuildFileStream(), "Object library " + this->GetTargetName(),
  990. outputs, depends);
  991. // Add aliases for the target name.
  992. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  993. this->GetGeneratorTarget());
  994. }