cmNinjaNormalTargetGenerator.cxx 39 KB

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