cmNinjaNormalTargetGenerator.cxx 27 KB

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