cmNinjaNormalTargetGenerator.cxx 41 KB

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