cmNinjaNormalTargetGenerator.cxx 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  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. , 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. std::unique_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. std::unique_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_co_compile --lwyu=";
  440. cmGeneratorTarget& gt = *this->GetGeneratorTarget();
  441. const std::string cfgName = this->GetConfigName();
  442. std::string targetOutputReal = this->ConvertToNinjaPath(
  443. gt.GetFullPath(cfgName, cmStateEnums::RuntimeBinaryArtifact,
  444. /*realname=*/true));
  445. cmakeCommand += targetOutputReal;
  446. cmakeCommand += " || true";
  447. linkCmds.push_back(std::move(cmakeCommand));
  448. }
  449. return linkCmds;
  450. }
  451. }
  452. switch (this->GetGeneratorTarget()->GetType()) {
  453. case cmStateEnums::STATIC_LIBRARY: {
  454. // We have archive link commands set. First, delete the existing archive.
  455. {
  456. std::string cmakeCommand =
  457. this->GetLocalGenerator()->ConvertToOutputFormat(
  458. cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
  459. linkCmds.push_back(cmakeCommand + " -E remove $TARGET_FILE");
  460. }
  461. // TODO: Use ARCHIVE_APPEND for archives over a certain size.
  462. {
  463. std::string linkCmdVar = "CMAKE_";
  464. linkCmdVar += this->TargetLinkLanguage;
  465. linkCmdVar += "_ARCHIVE_CREATE";
  466. linkCmdVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  467. linkCmdVar, this->TargetLinkLanguage, this->GetConfigName());
  468. const char* linkCmd = mf->GetRequiredDefinition(linkCmdVar);
  469. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  470. }
  471. {
  472. std::string linkCmdVar = "CMAKE_";
  473. linkCmdVar += this->TargetLinkLanguage;
  474. linkCmdVar += "_ARCHIVE_FINISH";
  475. linkCmdVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
  476. linkCmdVar, this->TargetLinkLanguage, this->GetConfigName());
  477. const char* linkCmd = mf->GetRequiredDefinition(linkCmdVar);
  478. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  479. }
  480. return linkCmds;
  481. }
  482. case cmStateEnums::SHARED_LIBRARY:
  483. case cmStateEnums::MODULE_LIBRARY:
  484. case cmStateEnums::EXECUTABLE:
  485. break;
  486. default:
  487. assert(false && "Unexpected target type");
  488. }
  489. return std::vector<std::string>();
  490. }
  491. void cmNinjaNormalTargetGenerator::WriteDeviceLinkStatement()
  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();
  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 =
  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() << "# Link build statements for "
  676. << 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();
  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()) {
  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.c_str());
  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);
  850. std::string postBuildCmdLine = localGen.BuildCommandLine(postBuildCmdLines);
  851. cmNinjaVars symlinkVars;
  852. bool const symlinkNeeded =
  853. (targetOutput != targetOutputReal && !gt.IsFrameworkOnApple());
  854. if (!symlinkNeeded) {
  855. vars["POST_BUILD"] = postBuildCmdLine;
  856. } else {
  857. vars["POST_BUILD"] = cmGlobalNinjaGenerator::SHELL_NOOP;
  858. symlinkVars["POST_BUILD"] = postBuildCmdLine;
  859. }
  860. cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
  861. bool const lang_supports_response =
  862. !(this->TargetLinkLanguage == "RC" || this->TargetLinkLanguage == "CUDA");
  863. int commandLineLengthLimit = -1;
  864. if (!lang_supports_response || !this->ForceResponseFile()) {
  865. commandLineLengthLimit =
  866. static_cast<int>(cmSystemTools::CalculateCommandLineLengthLimit()) -
  867. globalGen.GetRuleCmdLength(this->LanguageLinkerRule());
  868. }
  869. const std::string rspfile =
  870. std::string(cmake::GetCMakeFilesDirectoryPostSlash()) + gt.GetName() +
  871. ".rsp";
  872. // Gather order-only dependencies.
  873. cmNinjaDeps orderOnlyDeps;
  874. this->GetLocalGenerator()->AppendTargetDepends(this->GetGeneratorTarget(),
  875. orderOnlyDeps);
  876. // Ninja should restat after linking if and only if there are byproducts.
  877. vars["RESTAT"] = byproducts.empty() ? "" : "1";
  878. for (std::string const& o : byproducts) {
  879. this->GetGlobalGenerator()->SeenCustomCommandOutput(o);
  880. outputs.push_back(o);
  881. }
  882. // Write the build statement for this target.
  883. bool usedResponseFile = false;
  884. globalGen.WriteBuild(this->GetBuildFileStream(), comment.str(),
  885. this->LanguageLinkerRule(), outputs,
  886. /*implicitOuts=*/cmNinjaDeps(), explicitDeps,
  887. implicitDeps, orderOnlyDeps, vars, rspfile,
  888. commandLineLengthLimit, &usedResponseFile);
  889. this->WriteLinkRule(usedResponseFile);
  890. if (symlinkNeeded) {
  891. if (targetType == cmStateEnums::EXECUTABLE) {
  892. globalGen.WriteBuild(
  893. this->GetBuildFileStream(),
  894. "Create executable symlink " + targetOutput,
  895. "CMAKE_SYMLINK_EXECUTABLE", cmNinjaDeps(1, targetOutput),
  896. /*implicitOuts=*/cmNinjaDeps(), cmNinjaDeps(1, targetOutputReal),
  897. emptyDeps, emptyDeps, symlinkVars);
  898. } else {
  899. cmNinjaDeps symlinks;
  900. std::string const soName =
  901. this->ConvertToNinjaPath(this->GetTargetFilePath(this->TargetNameSO));
  902. // If one link has to be created.
  903. if (targetOutputReal == soName || targetOutput == soName) {
  904. symlinkVars["SONAME"] = soName;
  905. } else {
  906. symlinkVars["SONAME"].clear();
  907. symlinks.push_back(soName);
  908. }
  909. symlinks.push_back(targetOutput);
  910. globalGen.WriteBuild(
  911. this->GetBuildFileStream(), "Create library symlink " + targetOutput,
  912. "CMAKE_SYMLINK_LIBRARY", symlinks,
  913. /*implicitOuts=*/cmNinjaDeps(), cmNinjaDeps(1, targetOutputReal),
  914. emptyDeps, emptyDeps, symlinkVars);
  915. }
  916. }
  917. // Add aliases for the file name and the target name.
  918. globalGen.AddTargetAlias(this->TargetNameOut, &gt);
  919. globalGen.AddTargetAlias(this->GetTargetName(), &gt);
  920. }
  921. void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()
  922. {
  923. // Write a phony output that depends on all object files.
  924. cmNinjaDeps outputs;
  925. this->GetLocalGenerator()->AppendTargetOutputs(this->GetGeneratorTarget(),
  926. outputs);
  927. cmNinjaDeps depends = this->GetObjects();
  928. this->GetGlobalGenerator()->WritePhonyBuild(
  929. this->GetBuildFileStream(), "Object library " + this->GetTargetName(),
  930. outputs, depends);
  931. // Add aliases for the target name.
  932. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  933. this->GetGeneratorTarget());
  934. }