cmNinjaNormalTargetGenerator.cxx 40 KB

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