cmNinjaNormalTargetGenerator.cxx 40 KB

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