cmNinjaNormalTargetGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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->GetLinkerLanguage(this->GetConfigName());
  38. if (target->GetType() == cmTarget::EXECUTABLE)
  39. this->GetGeneratorTarget()->GetExecutableNames(this->TargetNameOut,
  40. this->TargetNameReal,
  41. this->TargetNameImport,
  42. this->TargetNamePDB,
  43. GetLocalGenerator()->GetConfigName());
  44. else
  45. this->GetGeneratorTarget()->GetLibraryNames(this->TargetNameOut,
  46. this->TargetNameSO,
  47. this->TargetNameReal,
  48. this->TargetNameImport,
  49. this->TargetNamePDB,
  50. GetLocalGenerator()->GetConfigName());
  51. if(target->GetType() != cmTarget::OBJECT_LIBRARY)
  52. {
  53. // on Windows the output dir is already needed at compile time
  54. // ensure the directory exists (OutDir test)
  55. EnsureDirectoryExists(target->Target->GetDirectory(this->GetConfigName()));
  56. }
  57. this->OSXBundleGenerator = new cmOSXBundleGenerator(target,
  58. this->GetConfigName());
  59. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  60. }
  61. cmNinjaNormalTargetGenerator::~cmNinjaNormalTargetGenerator()
  62. {
  63. delete this->OSXBundleGenerator;
  64. }
  65. void cmNinjaNormalTargetGenerator::Generate()
  66. {
  67. if (this->TargetLinkLanguage.empty()) {
  68. cmSystemTools::Error("CMake can not determine linker language for "
  69. "target: ",
  70. this->GetTarget()->GetName().c_str());
  71. return;
  72. }
  73. // Write the rules for each language.
  74. this->WriteLanguagesRules();
  75. // Write the build statements
  76. this->WriteObjectBuildStatements();
  77. if(this->GetTarget()->GetType() == cmTarget::OBJECT_LIBRARY)
  78. {
  79. this->WriteObjectLibStatement();
  80. }
  81. else
  82. {
  83. this->WriteLinkStatement();
  84. }
  85. }
  86. void cmNinjaNormalTargetGenerator::WriteLanguagesRules()
  87. {
  88. #ifdef NINJA_GEN_VERBOSE_FILES
  89. cmGlobalNinjaGenerator::WriteDivider(this->GetRulesFileStream());
  90. this->GetRulesFileStream()
  91. << "# Rules for each languages for "
  92. << cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  93. << " target "
  94. << this->GetTargetName()
  95. << "\n\n";
  96. #endif
  97. // Write rules for languages compiled in this target.
  98. std::set<std::string> languages;
  99. std::vector<cmSourceFile*> sourceFiles;
  100. this->GetGeneratorTarget()->GetSourceFiles(sourceFiles,
  101. this->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  102. for(std::vector<cmSourceFile*>::const_iterator
  103. i = sourceFiles.begin(); i != sourceFiles.end(); ++i)
  104. {
  105. const std::string& lang = (*i)->GetLanguage();
  106. if(!lang.empty())
  107. {
  108. languages.insert(lang);
  109. }
  110. }
  111. for(std::set<std::string>::const_iterator l = languages.begin();
  112. l != languages.end();
  113. ++l)
  114. {
  115. this->WriteLanguageRules(*l);
  116. }
  117. }
  118. const char *cmNinjaNormalTargetGenerator::GetVisibleTypeName() const
  119. {
  120. switch (this->GetTarget()->GetType()) {
  121. case cmTarget::STATIC_LIBRARY:
  122. return "static library";
  123. case cmTarget::SHARED_LIBRARY:
  124. return "shared library";
  125. case cmTarget::MODULE_LIBRARY:
  126. if (this->GetTarget()->IsCFBundleOnApple())
  127. return "CFBundle shared module";
  128. else
  129. return "shared module";
  130. case cmTarget::EXECUTABLE:
  131. return "executable";
  132. default:
  133. return 0;
  134. }
  135. }
  136. std::string
  137. cmNinjaNormalTargetGenerator
  138. ::LanguageLinkerRule() const
  139. {
  140. return this->TargetLinkLanguage
  141. + "_"
  142. + cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  143. + "_LINKER__"
  144. + cmGlobalNinjaGenerator::EncodeRuleName(this->GetTarget()->GetName())
  145. ;
  146. }
  147. void
  148. cmNinjaNormalTargetGenerator
  149. ::WriteLinkRule(bool useResponseFile)
  150. {
  151. cmTarget::TargetType targetType = this->GetTarget()->GetType();
  152. std::string ruleName = this->LanguageLinkerRule();
  153. // Select whether to use a response file for objects.
  154. std::string rspfile;
  155. std::string rspcontent;
  156. if (!this->GetGlobalGenerator()->HasRule(ruleName)) {
  157. cmLocalGenerator::RuleVariables vars;
  158. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  159. vars.CMTarget = this->GetTarget();
  160. vars.Language = this->TargetLinkLanguage.c_str();
  161. std::string responseFlag;
  162. if (!useResponseFile) {
  163. vars.Objects = "$in";
  164. vars.LinkLibraries = "$LINK_PATH $LINK_LIBRARIES";
  165. } else {
  166. std::string cmakeVarLang = "CMAKE_";
  167. cmakeVarLang += this->TargetLinkLanguage;
  168. // build response file name
  169. std::string cmakeLinkVar = cmakeVarLang + "_RESPONSE_FILE_LINK_FLAG";
  170. const char * flag = GetMakefile()->GetDefinition(cmakeLinkVar);
  171. if(flag) {
  172. responseFlag = flag;
  173. } else {
  174. responseFlag = "@";
  175. }
  176. rspfile = "$RSP_FILE";
  177. responseFlag += rspfile;
  178. // build response file content
  179. if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
  180. rspcontent = "$in";
  181. } else {
  182. rspcontent = "$in_newline";
  183. }
  184. rspcontent += " $LINK_PATH $LINK_LIBRARIES";
  185. vars.Objects = responseFlag.c_str();
  186. vars.LinkLibraries = "";
  187. }
  188. vars.ObjectDir = "$OBJECT_DIR";
  189. vars.Target = "$TARGET_FILE";
  190. vars.SONameFlag = "$SONAME_FLAG";
  191. vars.TargetSOName = "$SONAME";
  192. vars.TargetInstallNameDir = "$INSTALLNAME_DIR";
  193. vars.TargetPDB = "$TARGET_PDB";
  194. // Setup the target version.
  195. std::string targetVersionMajor;
  196. std::string targetVersionMinor;
  197. {
  198. std::ostringstream majorStream;
  199. std::ostringstream minorStream;
  200. int major;
  201. int minor;
  202. this->GetTarget()->GetTargetVersion(major, minor);
  203. majorStream << major;
  204. minorStream << minor;
  205. targetVersionMajor = majorStream.str();
  206. targetVersionMinor = minorStream.str();
  207. }
  208. vars.TargetVersionMajor = targetVersionMajor.c_str();
  209. vars.TargetVersionMinor = targetVersionMinor.c_str();
  210. vars.Flags = "$FLAGS";
  211. vars.LinkFlags = "$LINK_FLAGS";
  212. vars.Manifests = "$MANIFESTS";
  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. cmGeneratorTarget& gt = *this->GetGeneratorTarget();
  363. const std::string cfgName = this->GetConfigName();
  364. std::string targetOutput = ConvertToNinjaPath(
  365. gt.GetFullPath(cfgName));
  366. std::string targetOutputReal = ConvertToNinjaPath(
  367. gt.GetFullPath(cfgName,
  368. /*implib=*/false,
  369. /*realpath=*/true));
  370. std::string targetOutputImplib = ConvertToNinjaPath(
  371. gt.GetFullPath(cfgName,
  372. /*implib=*/true));
  373. if (target.IsAppBundleOnApple())
  374. {
  375. // Create the app bundle
  376. std::string outpath = target.GetDirectory(cfgName);
  377. this->OSXBundleGenerator->CreateAppBundle(this->TargetNameOut, outpath);
  378. // Calculate the output path
  379. targetOutput = outpath;
  380. targetOutput += "/";
  381. targetOutput += this->TargetNameOut;
  382. targetOutput = this->ConvertToNinjaPath(targetOutput);
  383. targetOutputReal = outpath;
  384. targetOutputReal += "/";
  385. targetOutputReal += this->TargetNameReal;
  386. targetOutputReal = this->ConvertToNinjaPath(targetOutputReal);
  387. }
  388. else if (target.IsFrameworkOnApple())
  389. {
  390. // Create the library framework.
  391. this->OSXBundleGenerator->CreateFramework(this->TargetNameOut,
  392. target.GetDirectory(cfgName));
  393. }
  394. else if(target.IsCFBundleOnApple())
  395. {
  396. // Create the core foundation bundle.
  397. this->OSXBundleGenerator->CreateCFBundle(this->TargetNameOut,
  398. target.GetDirectory(cfgName));
  399. }
  400. // Write comments.
  401. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  402. const cmTarget::TargetType targetType = target.GetType();
  403. this->GetBuildFileStream()
  404. << "# Link build statements for "
  405. << cmTarget::GetTargetTypeName(targetType)
  406. << " target "
  407. << this->GetTargetName()
  408. << "\n\n";
  409. cmNinjaDeps emptyDeps;
  410. cmNinjaVars vars;
  411. // Compute the comment.
  412. std::ostringstream comment;
  413. comment <<
  414. "Link the " << this->GetVisibleTypeName() << " " << targetOutputReal;
  415. // Compute outputs.
  416. cmNinjaDeps outputs;
  417. outputs.push_back(targetOutputReal);
  418. // Compute specific libraries to link with.
  419. cmNinjaDeps explicitDeps = this->GetObjects();
  420. cmNinjaDeps implicitDeps = this->ComputeLinkDeps();
  421. cmMakefile* mf = this->GetMakefile();
  422. std::string frameworkPath;
  423. std::string linkPath;
  424. cmGeneratorTarget& genTarget = *this->GetGeneratorTarget();
  425. std::string createRule =
  426. genTarget.GetCreateRuleVariable(this->TargetLinkLanguage,
  427. this->GetConfigName());
  428. bool useWatcomQuote = mf->IsOn(createRule+"_USE_WATCOM_QUOTE");
  429. cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator();
  430. vars["TARGET_FILE"] =
  431. localGen.ConvertToOutputFormat(targetOutputReal, cmLocalGenerator::SHELL);
  432. localGen.GetTargetFlags(vars["LINK_LIBRARIES"],
  433. vars["FLAGS"],
  434. vars["LINK_FLAGS"],
  435. frameworkPath,
  436. linkPath,
  437. &genTarget,
  438. useWatcomQuote);
  439. if(this->GetMakefile()->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS")
  440. && target.GetType() == cmTarget::SHARED_LIBRARY)
  441. {
  442. if(target.GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS"))
  443. {
  444. std::string dllname = targetOutput;
  445. std::string name_of_def_file
  446. = target.GetSupportDirectory();
  447. name_of_def_file += "/" + target.GetName();
  448. name_of_def_file += ".def ";
  449. vars["LINK_FLAGS"] += " /DEF:";
  450. vars["LINK_FLAGS"] += this->GetLocalGenerator()
  451. ->ConvertToOutputFormat(name_of_def_file.c_str(),
  452. cmLocalGenerator::SHELL);
  453. }
  454. }
  455. this->addPoolNinjaVariable("JOB_POOL_LINK", &target, vars);
  456. this->AddModuleDefinitionFlag(vars["LINK_FLAGS"]);
  457. vars["LINK_FLAGS"] = cmGlobalNinjaGenerator
  458. ::EncodeLiteral(vars["LINK_FLAGS"]);
  459. vars["MANIFESTS"] = this->GetManifests();
  460. vars["LINK_PATH"] = frameworkPath + linkPath;
  461. // Compute architecture specific link flags. Yes, these go into a different
  462. // variable for executables, probably due to a mistake made when duplicating
  463. // code between the Makefile executable and library generators.
  464. if (targetType == cmTarget::EXECUTABLE)
  465. {
  466. std::string t = vars["FLAGS"];
  467. localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
  468. vars["FLAGS"] = t;
  469. }
  470. else
  471. {
  472. std::string t = vars["ARCH_FLAGS"];
  473. localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
  474. vars["ARCH_FLAGS"] = t;
  475. t = "";
  476. localGen.AddLanguageFlags(t, TargetLinkLanguage, cfgName);
  477. vars["LANGUAGE_COMPILE_FLAGS"] = t;
  478. }
  479. if (this->GetGeneratorTarget()->HasSOName(cfgName))
  480. {
  481. vars["SONAME_FLAG"] = mf->GetSONameFlag(this->TargetLinkLanguage);
  482. vars["SONAME"] = this->TargetNameSO;
  483. if (targetType == cmTarget::SHARED_LIBRARY)
  484. {
  485. std::string install_dir =
  486. this->GetGeneratorTarget()->GetInstallNameDirForBuildTree(cfgName);
  487. if (!install_dir.empty())
  488. {
  489. vars["INSTALLNAME_DIR"] = localGen.Convert(install_dir,
  490. cmLocalGenerator::NONE,
  491. cmLocalGenerator::SHELL);
  492. }
  493. }
  494. }
  495. cmNinjaDeps byproducts;
  496. if (!this->TargetNameImport.empty())
  497. {
  498. const std::string impLibPath = localGen.ConvertToOutputFormat(
  499. targetOutputImplib,
  500. cmLocalGenerator::SHELL);
  501. vars["TARGET_IMPLIB"] = impLibPath;
  502. EnsureParentDirectoryExists(impLibPath);
  503. if(target.HasImportLibrary())
  504. {
  505. byproducts.push_back(targetOutputImplib);
  506. }
  507. }
  508. if (!this->SetMsvcTargetPdbVariable(vars))
  509. {
  510. // It is common to place debug symbols at a specific place,
  511. // so we need a plain target name in the rule available.
  512. std::string prefix;
  513. std::string base;
  514. std::string suffix;
  515. this->GetGeneratorTarget()->GetFullNameComponents(prefix, base, suffix);
  516. std::string dbg_suffix = ".dbg";
  517. // TODO: Where to document?
  518. if (mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX"))
  519. {
  520. dbg_suffix = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX");
  521. }
  522. vars["TARGET_PDB"] = base + suffix + dbg_suffix;
  523. }
  524. const std::string objPath = GetTarget()->GetSupportDirectory();
  525. vars["OBJECT_DIR"] =
  526. this->GetLocalGenerator()->ConvertToOutputFormat(
  527. this->ConvertToNinjaPath(objPath), cmLocalGenerator::SHELL);
  528. EnsureDirectoryExists(objPath);
  529. if (this->GetGlobalGenerator()->IsGCCOnWindows())
  530. {
  531. // ar.exe can't handle backslashes in rsp files (implicitly used by gcc)
  532. std::string& linkLibraries = vars["LINK_LIBRARIES"];
  533. std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/');
  534. std::string& link_path = vars["LINK_PATH"];
  535. std::replace(link_path.begin(), link_path.end(), '\\', '/');
  536. }
  537. const std::vector<cmCustomCommand> *cmdLists[3] = {
  538. &target.GetPreBuildCommands(),
  539. &target.GetPreLinkCommands(),
  540. &target.GetPostBuildCommands()
  541. };
  542. std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
  543. std::vector<std::string> *cmdLineLists[3] = {
  544. &preLinkCmdLines,
  545. &preLinkCmdLines,
  546. &postBuildCmdLines
  547. };
  548. for (unsigned i = 0; i != 3; ++i)
  549. {
  550. for (std::vector<cmCustomCommand>::const_iterator
  551. ci = cmdLists[i]->begin();
  552. ci != cmdLists[i]->end(); ++ci)
  553. {
  554. cmCustomCommandGenerator ccg(*ci, cfgName, this->GetLocalGenerator());
  555. localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
  556. std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
  557. std::transform(ccByproducts.begin(), ccByproducts.end(),
  558. std::back_inserter(byproducts), MapToNinjaPath());
  559. }
  560. }
  561. // maybe create .def file from list of objects
  562. if (target.GetType() == cmTarget::SHARED_LIBRARY &&
  563. this->GetMakefile()->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS"))
  564. {
  565. if(target.GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS"))
  566. {
  567. std::string cmakeCommand =
  568. this->GetLocalGenerator()->ConvertToOutputFormat(
  569. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  570. std::string name_of_def_file
  571. = target.GetSupportDirectory();
  572. name_of_def_file += "/" + target.GetName();
  573. name_of_def_file += ".def";
  574. std::string cmd = cmakeCommand;
  575. cmd += " -E __create_def ";
  576. cmd += this->GetLocalGenerator()
  577. ->ConvertToOutputFormat(name_of_def_file.c_str(),
  578. cmLocalGenerator::SHELL);
  579. cmd += " ";
  580. cmNinjaDeps objs = this->GetObjects();
  581. std::string obj_list_file = name_of_def_file;
  582. obj_list_file += ".objs";
  583. cmd += this->GetLocalGenerator()
  584. ->ConvertToOutputFormat(obj_list_file.c_str(),
  585. cmLocalGenerator::SHELL);
  586. preLinkCmdLines.push_back(cmd);
  587. // create a list of obj files for the -E __create_def to read
  588. cmGeneratedFileStream fout(obj_list_file.c_str());
  589. for(cmNinjaDeps::iterator i=objs.begin(); i != objs.end(); ++i)
  590. {
  591. if(cmHasLiteralSuffix(*i, ".obj"))
  592. {
  593. fout << *i << "\n";
  594. }
  595. }
  596. }
  597. }
  598. // If we have any PRE_LINK commands, we need to go back to HOME_OUTPUT for
  599. // the link commands.
  600. if (!preLinkCmdLines.empty())
  601. {
  602. const std::string homeOutDir = localGen.ConvertToOutputFormat(
  603. mf->GetHomeOutputDirectory(),
  604. cmLocalGenerator::SHELL);
  605. preLinkCmdLines.push_back("cd " + homeOutDir);
  606. }
  607. vars["PRE_LINK"] = localGen.BuildCommandLine(preLinkCmdLines);
  608. std::string postBuildCmdLine = localGen.BuildCommandLine(postBuildCmdLines);
  609. cmNinjaVars symlinkVars;
  610. if (targetOutput == targetOutputReal)
  611. {
  612. vars["POST_BUILD"] = postBuildCmdLine;
  613. }
  614. else
  615. {
  616. vars["POST_BUILD"] = ":";
  617. symlinkVars["POST_BUILD"] = postBuildCmdLine;
  618. }
  619. cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
  620. int commandLineLengthLimit = 1;
  621. const char* forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
  622. if (!mf->IsDefinitionSet(forceRspFile) &&
  623. cmSystemTools::GetEnv(forceRspFile) == 0)
  624. {
  625. commandLineLengthLimit = calculateCommandLineLengthLimit(
  626. globalGen.GetRuleCmdLength(this->LanguageLinkerRule()));
  627. }
  628. const std::string rspfile =
  629. std::string(cmake::GetCMakeFilesDirectoryPostSlash())
  630. + target.GetName() + ".rsp";
  631. // Gather order-only dependencies.
  632. cmNinjaDeps orderOnlyDeps;
  633. this->GetLocalGenerator()->AppendTargetDepends(this->GetTarget(),
  634. orderOnlyDeps);
  635. // Ninja should restat after linking if and only if there are byproducts.
  636. vars["RESTAT"] = byproducts.empty()? "" : "1";
  637. for (cmNinjaDeps::const_iterator oi = byproducts.begin(),
  638. oe = byproducts.end();
  639. oi != oe; ++oi)
  640. {
  641. this->GetGlobalGenerator()->SeenCustomCommandOutput(*oi);
  642. outputs.push_back(*oi);
  643. }
  644. // Write the build statement for this target.
  645. bool usedResponseFile = false;
  646. globalGen.WriteBuild(this->GetBuildFileStream(),
  647. comment.str(),
  648. this->LanguageLinkerRule(),
  649. outputs,
  650. explicitDeps,
  651. implicitDeps,
  652. orderOnlyDeps,
  653. vars,
  654. rspfile,
  655. commandLineLengthLimit,
  656. &usedResponseFile);
  657. this->WriteLinkRule(usedResponseFile);
  658. if (targetOutput != targetOutputReal && !target.IsFrameworkOnApple())
  659. {
  660. if (targetType == cmTarget::EXECUTABLE)
  661. {
  662. globalGen.WriteBuild(this->GetBuildFileStream(),
  663. "Create executable symlink " + targetOutput,
  664. "CMAKE_SYMLINK_EXECUTABLE",
  665. cmNinjaDeps(1, targetOutput),
  666. cmNinjaDeps(1, targetOutputReal),
  667. emptyDeps,
  668. emptyDeps,
  669. symlinkVars);
  670. }
  671. else
  672. {
  673. cmNinjaDeps symlinks;
  674. const std::string soName = this->GetTargetFilePath(this->TargetNameSO);
  675. // If one link has to be created.
  676. if (targetOutputReal == soName || targetOutput == soName)
  677. {
  678. symlinkVars["SONAME"] = soName;
  679. }
  680. else
  681. {
  682. symlinkVars["SONAME"] = "";
  683. symlinks.push_back(soName);
  684. }
  685. symlinks.push_back(targetOutput);
  686. globalGen.WriteBuild(this->GetBuildFileStream(),
  687. "Create library symlink " + targetOutput,
  688. "CMAKE_SYMLINK_LIBRARY",
  689. symlinks,
  690. cmNinjaDeps(1, targetOutputReal),
  691. emptyDeps,
  692. emptyDeps,
  693. symlinkVars);
  694. }
  695. }
  696. // Add aliases for the file name and the target name.
  697. globalGen.AddTargetAlias(this->TargetNameOut, &target);
  698. globalGen.AddTargetAlias(this->GetTargetName(), &target);
  699. }
  700. //----------------------------------------------------------------------------
  701. void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()
  702. {
  703. // Write a phony output that depends on all object files.
  704. cmNinjaDeps outputs;
  705. this->GetLocalGenerator()->AppendTargetOutputs(this->GetTarget(), outputs);
  706. cmNinjaDeps depends = this->GetObjects();
  707. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  708. "Object library "
  709. + this->GetTargetName(),
  710. outputs,
  711. depends);
  712. // Add aliases for the target name.
  713. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  714. this->GetTarget());
  715. }