cmNinjaNormalTargetGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2011 Peter Collingbourne <[email protected]>
  4. Copyright 2011 Nicolas Despres <[email protected]>
  5. Distributed under the OSI-approved BSD License (the "License");
  6. see accompanying file Copyright.txt for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even the
  8. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. See the License for more information.
  10. ============================================================================*/
  11. #include "cmNinjaNormalTargetGenerator.h"
  12. #include "cmLocalNinjaGenerator.h"
  13. #include "cmGlobalNinjaGenerator.h"
  14. #include "cmSourceFile.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmMakefile.h"
  17. #include "cmOSXBundleGenerator.h"
  18. #include "cmGeneratorTarget.h"
  19. #include "cmCustomCommandGenerator.h"
  20. #include "cmAlgorithms.h"
  21. #include <assert.h>
  22. #include <algorithm>
  23. #include <limits>
  24. #ifndef _WIN32
  25. #include <unistd.h>
  26. #endif
  27. cmNinjaNormalTargetGenerator::
  28. cmNinjaNormalTargetGenerator(cmGeneratorTarget* target)
  29. : cmNinjaTargetGenerator(target)
  30. , TargetNameOut()
  31. , TargetNameSO()
  32. , TargetNameReal()
  33. , TargetNameImport()
  34. , TargetNamePDB()
  35. , TargetLinkLanguage("")
  36. {
  37. this->TargetLinkLanguage = target->Target
  38. ->GetLinkerLanguage(this->GetConfigName());
  39. if (target->GetType() == cmTarget::EXECUTABLE)
  40. target->Target->GetExecutableNames(this->TargetNameOut,
  41. this->TargetNameReal,
  42. this->TargetNameImport,
  43. this->TargetNamePDB,
  44. GetLocalGenerator()->GetConfigName());
  45. else
  46. target->Target->GetLibraryNames(this->TargetNameOut,
  47. this->TargetNameSO,
  48. this->TargetNameReal,
  49. this->TargetNameImport,
  50. this->TargetNamePDB,
  51. GetLocalGenerator()->GetConfigName());
  52. if(target->GetType() != cmTarget::OBJECT_LIBRARY)
  53. {
  54. // on Windows the output dir is already needed at compile time
  55. // ensure the directory exists (OutDir test)
  56. EnsureDirectoryExists(target->Target->GetDirectory(this->GetConfigName()));
  57. }
  58. this->OSXBundleGenerator = new cmOSXBundleGenerator(target,
  59. this->GetConfigName());
  60. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  61. }
  62. cmNinjaNormalTargetGenerator::~cmNinjaNormalTargetGenerator()
  63. {
  64. delete this->OSXBundleGenerator;
  65. }
  66. void cmNinjaNormalTargetGenerator::Generate()
  67. {
  68. if (this->TargetLinkLanguage.empty()) {
  69. cmSystemTools::Error("CMake can not determine linker language for "
  70. "target: ",
  71. this->GetTarget()->GetName().c_str());
  72. return;
  73. }
  74. // Write the rules for each language.
  75. this->WriteLanguagesRules();
  76. // Write the build statements
  77. this->WriteObjectBuildStatements();
  78. if(this->GetTarget()->GetType() == cmTarget::OBJECT_LIBRARY)
  79. {
  80. this->WriteObjectLibStatement();
  81. }
  82. else
  83. {
  84. this->WriteLinkStatement();
  85. }
  86. }
  87. void cmNinjaNormalTargetGenerator::WriteLanguagesRules()
  88. {
  89. #ifdef NINJA_GEN_VERBOSE_FILES
  90. cmGlobalNinjaGenerator::WriteDivider(this->GetRulesFileStream());
  91. this->GetRulesFileStream()
  92. << "# Rules for each languages for "
  93. << cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  94. << " target "
  95. << this->GetTargetName()
  96. << "\n\n";
  97. #endif
  98. // Write rules for languages compiled in this target.
  99. std::set<std::string> languages;
  100. std::vector<cmSourceFile*> sourceFiles;
  101. this->GetTarget()->GetSourceFiles(sourceFiles,
  102. this->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  103. for(std::vector<cmSourceFile*>::const_iterator
  104. i = sourceFiles.begin(); i != sourceFiles.end(); ++i)
  105. {
  106. const std::string& lang = (*i)->GetLanguage();
  107. if(!lang.empty())
  108. {
  109. languages.insert(lang);
  110. }
  111. }
  112. for(std::set<std::string>::const_iterator l = languages.begin();
  113. l != languages.end();
  114. ++l)
  115. {
  116. this->WriteLanguageRules(*l);
  117. }
  118. }
  119. const char *cmNinjaNormalTargetGenerator::GetVisibleTypeName() const
  120. {
  121. switch (this->GetTarget()->GetType()) {
  122. case cmTarget::STATIC_LIBRARY:
  123. return "static library";
  124. case cmTarget::SHARED_LIBRARY:
  125. return "shared library";
  126. case cmTarget::MODULE_LIBRARY:
  127. if (this->GetTarget()->IsCFBundleOnApple())
  128. return "CFBundle shared module";
  129. else
  130. return "shared module";
  131. case cmTarget::EXECUTABLE:
  132. return "executable";
  133. default:
  134. return 0;
  135. }
  136. }
  137. std::string
  138. cmNinjaNormalTargetGenerator
  139. ::LanguageLinkerRule() const
  140. {
  141. return this->TargetLinkLanguage
  142. + "_"
  143. + cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  144. + "_LINKER__"
  145. + cmGlobalNinjaGenerator::EncodeRuleName(this->GetTarget()->GetName())
  146. ;
  147. }
  148. void
  149. cmNinjaNormalTargetGenerator
  150. ::WriteLinkRule(bool useResponseFile)
  151. {
  152. cmTarget::TargetType targetType = this->GetTarget()->GetType();
  153. std::string ruleName = this->LanguageLinkerRule();
  154. // Select whether to use a response file for objects.
  155. std::string rspfile;
  156. std::string rspcontent;
  157. if (!this->GetGlobalGenerator()->HasRule(ruleName)) {
  158. cmLocalGenerator::RuleVariables vars;
  159. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  160. vars.CMTarget = this->GetTarget();
  161. vars.Language = this->TargetLinkLanguage.c_str();
  162. std::string responseFlag;
  163. if (!useResponseFile) {
  164. vars.Objects = "$in";
  165. vars.LinkLibraries = "$LINK_PATH $LINK_LIBRARIES";
  166. } else {
  167. std::string cmakeVarLang = "CMAKE_";
  168. cmakeVarLang += this->TargetLinkLanguage;
  169. // build response file name
  170. std::string cmakeLinkVar = cmakeVarLang + "_RESPONSE_FILE_LINK_FLAG";
  171. const char * flag = GetMakefile()->GetDefinition(cmakeLinkVar);
  172. if(flag) {
  173. responseFlag = flag;
  174. } else {
  175. responseFlag = "@";
  176. }
  177. rspfile = "$RSP_FILE";
  178. responseFlag += rspfile;
  179. // build response file content
  180. if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
  181. rspcontent = "$in";
  182. } else {
  183. rspcontent = "$in_newline";
  184. }
  185. rspcontent += " $LINK_PATH $LINK_LIBRARIES";
  186. vars.Objects = responseFlag.c_str();
  187. vars.LinkLibraries = "";
  188. }
  189. vars.ObjectDir = "$OBJECT_DIR";
  190. vars.Target = "$TARGET_FILE";
  191. vars.SONameFlag = "$SONAME_FLAG";
  192. vars.TargetSOName = "$SONAME";
  193. vars.TargetInstallNameDir = "$INSTALLNAME_DIR";
  194. vars.TargetPDB = "$TARGET_PDB";
  195. // Setup the target version.
  196. std::string targetVersionMajor;
  197. std::string targetVersionMinor;
  198. {
  199. std::ostringstream majorStream;
  200. std::ostringstream minorStream;
  201. int major;
  202. int minor;
  203. this->GetTarget()->GetTargetVersion(major, minor);
  204. majorStream << major;
  205. minorStream << minor;
  206. targetVersionMajor = majorStream.str();
  207. targetVersionMinor = minorStream.str();
  208. }
  209. vars.TargetVersionMajor = targetVersionMajor.c_str();
  210. vars.TargetVersionMinor = targetVersionMinor.c_str();
  211. vars.Flags = "$FLAGS";
  212. vars.LinkFlags = "$LINK_FLAGS";
  213. std::string langFlags;
  214. if (targetType != cmTarget::EXECUTABLE)
  215. {
  216. langFlags += "$LANGUAGE_COMPILE_FLAGS $ARCH_FLAGS";
  217. vars.LanguageCompileFlags = langFlags.c_str();
  218. }
  219. // Rule for linking library/executable.
  220. std::vector<std::string> linkCmds = this->ComputeLinkCmd();
  221. for(std::vector<std::string>::iterator i = linkCmds.begin();
  222. i != linkCmds.end();
  223. ++i)
  224. {
  225. this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
  226. }
  227. linkCmds.insert(linkCmds.begin(), "$PRE_LINK");
  228. linkCmds.push_back("$POST_BUILD");
  229. std::string linkCmd =
  230. this->GetLocalGenerator()->BuildCommandLine(linkCmds);
  231. // Write the linker rule with response file if needed.
  232. std::ostringstream comment;
  233. comment << "Rule for linking " << this->TargetLinkLanguage << " "
  234. << this->GetVisibleTypeName() << ".";
  235. std::ostringstream description;
  236. description << "Linking " << this->TargetLinkLanguage << " "
  237. << this->GetVisibleTypeName() << " $TARGET_FILE";
  238. this->GetGlobalGenerator()->AddRule(ruleName,
  239. linkCmd,
  240. description.str(),
  241. comment.str(),
  242. /*depfile*/ "",
  243. /*deptype*/ "",
  244. rspfile,
  245. rspcontent,
  246. /*restat*/ "$RESTAT",
  247. /*generator*/ false);
  248. }
  249. if (this->TargetNameOut != this->TargetNameReal &&
  250. !this->GetTarget()->IsFrameworkOnApple()) {
  251. std::string cmakeCommand =
  252. this->GetLocalGenerator()->ConvertToOutputFormat(
  253. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  254. if (targetType == cmTarget::EXECUTABLE)
  255. this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_EXECUTABLE",
  256. cmakeCommand +
  257. " -E cmake_symlink_executable"
  258. " $in $out && $POST_BUILD",
  259. "Creating executable symlink $out",
  260. "Rule for creating "
  261. "executable symlink.",
  262. /*depfile*/ "",
  263. /*deptype*/ "",
  264. /*rspfile*/ "",
  265. /*rspcontent*/ "",
  266. /*restat*/ "",
  267. /*generator*/ false);
  268. else
  269. this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_LIBRARY",
  270. cmakeCommand +
  271. " -E cmake_symlink_library"
  272. " $in $SONAME $out && $POST_BUILD",
  273. "Creating library symlink $out",
  274. "Rule for creating "
  275. "library symlink.",
  276. /*depfile*/ "",
  277. /*deptype*/ "",
  278. /*rspfile*/ "",
  279. /*rspcontent*/ "",
  280. /*restat*/ "",
  281. /*generator*/ false);
  282. }
  283. }
  284. std::vector<std::string>
  285. cmNinjaNormalTargetGenerator
  286. ::ComputeLinkCmd()
  287. {
  288. std::vector<std::string> linkCmds;
  289. cmMakefile* mf = this->GetMakefile();
  290. {
  291. std::string linkCmdVar = this->GetGeneratorTarget()
  292. ->GetCreateRuleVariable(this->TargetLinkLanguage, this->GetConfigName());
  293. const char *linkCmd = mf->GetDefinition(linkCmdVar);
  294. if (linkCmd)
  295. {
  296. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  297. return linkCmds;
  298. }
  299. }
  300. switch (this->GetTarget()->GetType()) {
  301. case cmTarget::STATIC_LIBRARY: {
  302. // We have archive link commands set. First, delete the existing archive.
  303. {
  304. std::string cmakeCommand =
  305. this->GetLocalGenerator()->ConvertToOutputFormat(
  306. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  307. linkCmds.push_back(cmakeCommand + " -E remove $TARGET_FILE");
  308. }
  309. // TODO: Use ARCHIVE_APPEND for archives over a certain size.
  310. {
  311. std::string linkCmdVar = "CMAKE_";
  312. linkCmdVar += this->TargetLinkLanguage;
  313. linkCmdVar += "_ARCHIVE_CREATE";
  314. const char *linkCmd = mf->GetRequiredDefinition(linkCmdVar);
  315. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  316. }
  317. {
  318. std::string linkCmdVar = "CMAKE_";
  319. linkCmdVar += this->TargetLinkLanguage;
  320. linkCmdVar += "_ARCHIVE_FINISH";
  321. const char *linkCmd = mf->GetRequiredDefinition(linkCmdVar);
  322. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  323. }
  324. return linkCmds;
  325. }
  326. case cmTarget::SHARED_LIBRARY:
  327. case cmTarget::MODULE_LIBRARY:
  328. case cmTarget::EXECUTABLE:
  329. break;
  330. default:
  331. assert(0 && "Unexpected target type");
  332. }
  333. return std::vector<std::string>();
  334. }
  335. static int calculateCommandLineLengthLimit(int linkRuleLength)
  336. {
  337. static int const limits[] = {
  338. #ifdef _WIN32
  339. 8000,
  340. #endif
  341. #if defined(__APPLE__) || defined(__HAIKU__) || defined(__linux)
  342. // for instance ARG_MAX is 2096152 on Ubuntu or 262144 on Mac
  343. ((int)sysconf(_SC_ARG_MAX)) - 1000,
  344. #endif
  345. #if defined(__linux)
  346. // #define MAX_ARG_STRLEN (PAGE_SIZE * 32) in Linux's binfmts.h
  347. ((int)sysconf(_SC_PAGESIZE) * 32) - 1000,
  348. #endif
  349. std::numeric_limits<int>::max()
  350. };
  351. size_t const arrSz = cmArraySize(limits);
  352. int const sz = *std::min_element(limits, limits + arrSz);
  353. if (sz == std::numeric_limits<int>::max())
  354. {
  355. return -1;
  356. }
  357. return sz - linkRuleLength;
  358. }
  359. void cmNinjaNormalTargetGenerator::WriteLinkStatement()
  360. {
  361. cmTarget& target = *this->GetTarget();
  362. const std::string cfgName = this->GetConfigName();
  363. std::string targetOutput = ConvertToNinjaPath(
  364. target.GetFullPath(cfgName));
  365. std::string targetOutputReal = ConvertToNinjaPath(
  366. target.GetFullPath(cfgName,
  367. /*implib=*/false,
  368. /*realpath=*/true));
  369. std::string targetOutputImplib = ConvertToNinjaPath(
  370. target.GetFullPath(cfgName,
  371. /*implib=*/true));
  372. if (target.IsAppBundleOnApple())
  373. {
  374. // Create the app bundle
  375. std::string outpath = target.GetDirectory(cfgName);
  376. this->OSXBundleGenerator->CreateAppBundle(this->TargetNameOut, outpath);
  377. // Calculate the output path
  378. targetOutput = outpath;
  379. targetOutput += "/";
  380. targetOutput += this->TargetNameOut;
  381. targetOutput = this->ConvertToNinjaPath(targetOutput);
  382. targetOutputReal = outpath;
  383. targetOutputReal += "/";
  384. targetOutputReal += this->TargetNameReal;
  385. targetOutputReal = this->ConvertToNinjaPath(targetOutputReal);
  386. }
  387. else if (target.IsFrameworkOnApple())
  388. {
  389. // Create the library framework.
  390. this->OSXBundleGenerator->CreateFramework(this->TargetNameOut,
  391. target.GetDirectory(cfgName));
  392. }
  393. else if(target.IsCFBundleOnApple())
  394. {
  395. // Create the core foundation bundle.
  396. this->OSXBundleGenerator->CreateCFBundle(this->TargetNameOut,
  397. target.GetDirectory(cfgName));
  398. }
  399. // Write comments.
  400. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  401. const cmTarget::TargetType targetType = target.GetType();
  402. this->GetBuildFileStream()
  403. << "# Link build statements for "
  404. << cmTarget::GetTargetTypeName(targetType)
  405. << " target "
  406. << this->GetTargetName()
  407. << "\n\n";
  408. cmNinjaDeps emptyDeps;
  409. cmNinjaVars vars;
  410. // Compute the comment.
  411. std::ostringstream comment;
  412. comment <<
  413. "Link the " << this->GetVisibleTypeName() << " " << targetOutputReal;
  414. // Compute outputs.
  415. cmNinjaDeps outputs;
  416. outputs.push_back(targetOutputReal);
  417. // Compute specific libraries to link with.
  418. cmNinjaDeps explicitDeps = this->GetObjects();
  419. cmNinjaDeps implicitDeps = this->ComputeLinkDeps();
  420. cmMakefile* mf = this->GetMakefile();
  421. std::string frameworkPath;
  422. std::string linkPath;
  423. cmGeneratorTarget& genTarget = *this->GetGeneratorTarget();
  424. std::string createRule =
  425. genTarget.GetCreateRuleVariable(this->TargetLinkLanguage,
  426. this->GetConfigName());
  427. bool useWatcomQuote = mf->IsOn(createRule+"_USE_WATCOM_QUOTE");
  428. cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator();
  429. vars["TARGET_FILE"] =
  430. localGen.ConvertToOutputFormat(targetOutputReal, cmLocalGenerator::SHELL);
  431. localGen.GetTargetFlags(vars["LINK_LIBRARIES"],
  432. vars["FLAGS"],
  433. vars["LINK_FLAGS"],
  434. frameworkPath,
  435. linkPath,
  436. &genTarget,
  437. useWatcomQuote);
  438. if(this->GetMakefile()->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS")
  439. && target.GetType() == cmTarget::SHARED_LIBRARY)
  440. {
  441. if(target.GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS"))
  442. {
  443. std::string dllname = targetOutput;
  444. std::string name_of_def_file
  445. = target.GetSupportDirectory();
  446. name_of_def_file += "/" + target.GetName();
  447. name_of_def_file += ".def ";
  448. vars["LINK_FLAGS"] += " /DEF:";
  449. vars["LINK_FLAGS"] += this->GetLocalGenerator()
  450. ->ConvertToOutputFormat(name_of_def_file.c_str(),
  451. cmLocalGenerator::SHELL);
  452. }
  453. }
  454. this->addPoolNinjaVariable("JOB_POOL_LINK", &target, vars);
  455. this->AddModuleDefinitionFlag(vars["LINK_FLAGS"]);
  456. vars["LINK_FLAGS"] = cmGlobalNinjaGenerator
  457. ::EncodeLiteral(vars["LINK_FLAGS"]);
  458. vars["LINK_PATH"] = frameworkPath + linkPath;
  459. // Compute architecture specific link flags. Yes, these go into a different
  460. // variable for executables, probably due to a mistake made when duplicating
  461. // code between the Makefile executable and library generators.
  462. if (targetType == cmTarget::EXECUTABLE)
  463. {
  464. std::string t = vars["FLAGS"];
  465. localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
  466. vars["FLAGS"] = t;
  467. }
  468. else
  469. {
  470. std::string t = vars["ARCH_FLAGS"];
  471. localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
  472. vars["ARCH_FLAGS"] = t;
  473. t = "";
  474. localGen.AddLanguageFlags(t, TargetLinkLanguage, cfgName);
  475. vars["LANGUAGE_COMPILE_FLAGS"] = t;
  476. }
  477. if (target.HasSOName(cfgName))
  478. {
  479. vars["SONAME_FLAG"] = mf->GetSONameFlag(this->TargetLinkLanguage);
  480. vars["SONAME"] = this->TargetNameSO;
  481. if (targetType == cmTarget::SHARED_LIBRARY)
  482. {
  483. std::string install_dir = target.GetInstallNameDirForBuildTree(cfgName);
  484. if (!install_dir.empty())
  485. {
  486. vars["INSTALLNAME_DIR"] = localGen.Convert(install_dir,
  487. cmLocalGenerator::NONE,
  488. cmLocalGenerator::SHELL);
  489. }
  490. }
  491. }
  492. if (!this->TargetNameImport.empty())
  493. {
  494. const std::string impLibPath = localGen.ConvertToOutputFormat(
  495. targetOutputImplib,
  496. cmLocalGenerator::SHELL);
  497. vars["TARGET_IMPLIB"] = impLibPath;
  498. EnsureParentDirectoryExists(impLibPath);
  499. if(target.HasImportLibrary())
  500. {
  501. outputs.push_back(targetOutputImplib);
  502. }
  503. }
  504. if (!this->SetMsvcTargetPdbVariable(vars))
  505. {
  506. // It is common to place debug symbols at a specific place,
  507. // so we need a plain target name in the rule available.
  508. std::string prefix;
  509. std::string base;
  510. std::string suffix;
  511. target.GetFullNameComponents(prefix, base, suffix);
  512. std::string dbg_suffix = ".dbg";
  513. // TODO: Where to document?
  514. if (mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX"))
  515. {
  516. dbg_suffix = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX");
  517. }
  518. vars["TARGET_PDB"] = base + suffix + dbg_suffix;
  519. }
  520. if (this->GetGlobalGenerator()->IsGCCOnWindows())
  521. {
  522. const std::string objPath = GetTarget()->GetSupportDirectory();
  523. vars["OBJECT_DIR"] = ConvertToNinjaPath(objPath);
  524. EnsureDirectoryExists(objPath);
  525. // ar.exe can't handle backslashes in rsp files (implicitly used by gcc)
  526. std::string& linkLibraries = vars["LINK_LIBRARIES"];
  527. std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/');
  528. std::string& link_path = vars["LINK_PATH"];
  529. std::replace(link_path.begin(), link_path.end(), '\\', '/');
  530. }
  531. const std::vector<cmCustomCommand> *cmdLists[3] = {
  532. &target.GetPreBuildCommands(),
  533. &target.GetPreLinkCommands(),
  534. &target.GetPostBuildCommands()
  535. };
  536. std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
  537. std::vector<std::string> *cmdLineLists[3] = {
  538. &preLinkCmdLines,
  539. &preLinkCmdLines,
  540. &postBuildCmdLines
  541. };
  542. cmNinjaDeps byproducts;
  543. for (unsigned i = 0; i != 3; ++i)
  544. {
  545. for (std::vector<cmCustomCommand>::const_iterator
  546. ci = cmdLists[i]->begin();
  547. ci != cmdLists[i]->end(); ++ci)
  548. {
  549. cmCustomCommandGenerator ccg(*ci, cfgName, mf);
  550. localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
  551. std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
  552. std::transform(ccByproducts.begin(), ccByproducts.end(),
  553. std::back_inserter(byproducts), MapToNinjaPath());
  554. }
  555. }
  556. // maybe create .def file from list of objects
  557. if (target.GetType() == cmTarget::SHARED_LIBRARY &&
  558. this->GetMakefile()->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS"))
  559. {
  560. if(target.GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS"))
  561. {
  562. std::string cmakeCommand =
  563. this->GetLocalGenerator()->ConvertToOutputFormat(
  564. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  565. std::string dllname = targetOutput;
  566. std::string name_of_def_file
  567. = target.GetSupportDirectory();
  568. name_of_def_file += "/" + target.GetName();
  569. name_of_def_file += ".def";
  570. std::string cmd = cmakeCommand;
  571. cmd += " -E __create_def ";
  572. cmd += this->GetLocalGenerator()
  573. ->ConvertToOutputFormat(name_of_def_file.c_str(),
  574. cmLocalGenerator::SHELL);
  575. cmd += " ";
  576. cmNinjaDeps objs = this->GetObjects();
  577. std::string obj_list_file = name_of_def_file;
  578. obj_list_file += ".objs";
  579. cmd += this->GetLocalGenerator()
  580. ->ConvertToOutputFormat(obj_list_file.c_str(),
  581. cmLocalGenerator::SHELL);
  582. preLinkCmdLines.push_back(cmd);
  583. // create a list of obj files for the -E __create_def to read
  584. cmGeneratedFileStream fout(obj_list_file.c_str());
  585. for(cmNinjaDeps::iterator i=objs.begin(); i != objs.end(); ++i)
  586. {
  587. if(cmHasLiteralSuffix(*i, ".obj"))
  588. {
  589. fout << *i << "\n";
  590. }
  591. }
  592. }
  593. }
  594. // If we have any PRE_LINK commands, we need to go back to HOME_OUTPUT for
  595. // the link commands.
  596. if (!preLinkCmdLines.empty())
  597. {
  598. const std::string homeOutDir = localGen.ConvertToOutputFormat(
  599. mf->GetHomeOutputDirectory(),
  600. cmLocalGenerator::SHELL);
  601. preLinkCmdLines.push_back("cd " + homeOutDir);
  602. }
  603. vars["PRE_LINK"] = localGen.BuildCommandLine(preLinkCmdLines);
  604. std::string postBuildCmdLine = localGen.BuildCommandLine(postBuildCmdLines);
  605. cmNinjaVars symlinkVars;
  606. if (targetOutput == targetOutputReal)
  607. {
  608. vars["POST_BUILD"] = postBuildCmdLine;
  609. }
  610. else
  611. {
  612. vars["POST_BUILD"] = ":";
  613. symlinkVars["POST_BUILD"] = postBuildCmdLine;
  614. }
  615. cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
  616. int commandLineLengthLimit = 1;
  617. const char* forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
  618. if (!mf->IsDefinitionSet(forceRspFile) &&
  619. cmSystemTools::GetEnv(forceRspFile) == 0)
  620. {
  621. commandLineLengthLimit = calculateCommandLineLengthLimit(
  622. globalGen.GetRuleCmdLength(this->LanguageLinkerRule()));
  623. }
  624. const std::string rspfile =
  625. std::string(cmake::GetCMakeFilesDirectoryPostSlash())
  626. + target.GetName() + ".rsp";
  627. // Gather order-only dependencies.
  628. cmNinjaDeps orderOnlyDeps;
  629. this->GetLocalGenerator()->AppendTargetDepends(this->GetTarget(),
  630. orderOnlyDeps);
  631. // Ninja should restat after linking if and only if there are byproducts.
  632. vars["RESTAT"] = byproducts.empty()? "" : "1";
  633. for (cmNinjaDeps::const_iterator oi = byproducts.begin(),
  634. oe = byproducts.end();
  635. oi != oe; ++oi)
  636. {
  637. this->GetGlobalGenerator()->SeenCustomCommandOutput(*oi);
  638. outputs.push_back(*oi);
  639. }
  640. // Write the build statement for this target.
  641. bool usedResponseFile = false;
  642. globalGen.WriteBuild(this->GetBuildFileStream(),
  643. comment.str(),
  644. this->LanguageLinkerRule(),
  645. outputs,
  646. explicitDeps,
  647. implicitDeps,
  648. orderOnlyDeps,
  649. vars,
  650. rspfile,
  651. commandLineLengthLimit,
  652. &usedResponseFile);
  653. this->WriteLinkRule(usedResponseFile);
  654. if (targetOutput != targetOutputReal && !target.IsFrameworkOnApple())
  655. {
  656. if (targetType == cmTarget::EXECUTABLE)
  657. {
  658. globalGen.WriteBuild(this->GetBuildFileStream(),
  659. "Create executable symlink " + targetOutput,
  660. "CMAKE_SYMLINK_EXECUTABLE",
  661. cmNinjaDeps(1, targetOutput),
  662. cmNinjaDeps(1, targetOutputReal),
  663. emptyDeps,
  664. emptyDeps,
  665. symlinkVars);
  666. }
  667. else
  668. {
  669. cmNinjaDeps symlinks;
  670. const std::string soName = this->GetTargetFilePath(this->TargetNameSO);
  671. // If one link has to be created.
  672. if (targetOutputReal == soName || targetOutput == soName)
  673. {
  674. symlinkVars["SONAME"] = soName;
  675. }
  676. else
  677. {
  678. symlinkVars["SONAME"] = "";
  679. symlinks.push_back(soName);
  680. }
  681. symlinks.push_back(targetOutput);
  682. globalGen.WriteBuild(this->GetBuildFileStream(),
  683. "Create library symlink " + targetOutput,
  684. "CMAKE_SYMLINK_LIBRARY",
  685. symlinks,
  686. cmNinjaDeps(1, targetOutputReal),
  687. emptyDeps,
  688. emptyDeps,
  689. symlinkVars);
  690. }
  691. }
  692. // Add aliases for the file name and the target name.
  693. globalGen.AddTargetAlias(this->TargetNameOut, &target);
  694. globalGen.AddTargetAlias(this->GetTargetName(), &target);
  695. }
  696. //----------------------------------------------------------------------------
  697. void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()
  698. {
  699. // Write a phony output that depends on all object files.
  700. cmNinjaDeps outputs;
  701. this->GetLocalGenerator()->AppendTargetOutputs(this->GetTarget(), outputs);
  702. cmNinjaDeps depends = this->GetObjects();
  703. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  704. "Object library "
  705. + this->GetTargetName(),
  706. outputs,
  707. depends);
  708. // Add aliases for the target name.
  709. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  710. this->GetTarget());
  711. }