cmNinjaNormalTargetGenerator.cxx 40 KB

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