cmNinjaNormalTargetGenerator.cxx 39 KB

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