cmNinjaNormalTargetGenerator.cxx 40 KB

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