cmNinjaNormalTargetGenerator.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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->GetGeneratorTarget()->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. cmGeneratorTarget& gt = *this->GetGeneratorTarget();
  364. const std::string cfgName = this->GetConfigName();
  365. std::string targetOutput = ConvertToNinjaPath(
  366. gt.GetFullPath(cfgName));
  367. std::string targetOutputReal = ConvertToNinjaPath(
  368. gt.GetFullPath(cfgName,
  369. /*implib=*/false,
  370. /*realpath=*/true));
  371. std::string targetOutputImplib = ConvertToNinjaPath(
  372. gt.GetFullPath(cfgName,
  373. /*implib=*/true));
  374. if (gt.IsAppBundleOnApple())
  375. {
  376. // Create the app bundle
  377. std::string outpath = gt.GetDirectory(cfgName);
  378. this->OSXBundleGenerator->CreateAppBundle(this->TargetNameOut, outpath);
  379. // Calculate the output path
  380. targetOutput = outpath;
  381. targetOutput += "/";
  382. targetOutput += this->TargetNameOut;
  383. targetOutput = this->ConvertToNinjaPath(targetOutput);
  384. targetOutputReal = outpath;
  385. targetOutputReal += "/";
  386. targetOutputReal += this->TargetNameReal;
  387. targetOutputReal = this->ConvertToNinjaPath(targetOutputReal);
  388. }
  389. else if (gt.IsFrameworkOnApple())
  390. {
  391. // Create the library framework.
  392. this->OSXBundleGenerator->CreateFramework(this->TargetNameOut,
  393. gt.GetDirectory(cfgName));
  394. }
  395. else if(gt.IsCFBundleOnApple())
  396. {
  397. // Create the core foundation bundle.
  398. this->OSXBundleGenerator->CreateCFBundle(this->TargetNameOut,
  399. gt.GetDirectory(cfgName));
  400. }
  401. // Write comments.
  402. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  403. const cmState::TargetType targetType = gt.GetType();
  404. this->GetBuildFileStream()
  405. << "# Link build statements for "
  406. << cmState::GetTargetTypeName(targetType)
  407. << " target "
  408. << this->GetTargetName()
  409. << "\n\n";
  410. cmNinjaDeps emptyDeps;
  411. cmNinjaVars vars;
  412. // Compute the comment.
  413. std::ostringstream comment;
  414. comment <<
  415. "Link the " << this->GetVisibleTypeName() << " " << targetOutputReal;
  416. // Compute outputs.
  417. cmNinjaDeps outputs;
  418. outputs.push_back(targetOutputReal);
  419. // Compute specific libraries to link with.
  420. cmNinjaDeps explicitDeps = this->GetObjects();
  421. cmNinjaDeps implicitDeps = this->ComputeLinkDeps();
  422. cmMakefile* mf = this->GetMakefile();
  423. std::string frameworkPath;
  424. std::string linkPath;
  425. cmGeneratorTarget& genTarget = *this->GetGeneratorTarget();
  426. std::string createRule =
  427. genTarget.GetCreateRuleVariable(this->TargetLinkLanguage,
  428. this->GetConfigName());
  429. bool useWatcomQuote = mf->IsOn(createRule+"_USE_WATCOM_QUOTE");
  430. cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator();
  431. vars["TARGET_FILE"] =
  432. localGen.ConvertToOutputFormat(targetOutputReal, cmLocalGenerator::SHELL);
  433. localGen.GetTargetFlags(vars["LINK_LIBRARIES"],
  434. vars["FLAGS"],
  435. vars["LINK_FLAGS"],
  436. frameworkPath,
  437. linkPath,
  438. &genTarget,
  439. useWatcomQuote);
  440. if(this->GetMakefile()->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS")
  441. && gt.GetType() == cmState::SHARED_LIBRARY)
  442. {
  443. if(gt.GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS"))
  444. {
  445. std::string name_of_def_file
  446. = gt.GetSupportDirectory();
  447. name_of_def_file += "/" + gt.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. // Add OS X version flags, if any.
  456. if(this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY ||
  457. this->GeneratorTarget->GetType() == cmState::MODULE_LIBRARY)
  458. {
  459. this->AppendOSXVerFlag(vars["LINK_FLAGS"], this->TargetLinkLanguage,
  460. "COMPATIBILITY", true);
  461. this->AppendOSXVerFlag(vars["LINK_FLAGS"], this->TargetLinkLanguage,
  462. "CURRENT", false);
  463. }
  464. this->addPoolNinjaVariable("JOB_POOL_LINK", &gt, vars);
  465. this->AddModuleDefinitionFlag(vars["LINK_FLAGS"]);
  466. vars["LINK_FLAGS"] = cmGlobalNinjaGenerator
  467. ::EncodeLiteral(vars["LINK_FLAGS"]);
  468. vars["MANIFESTS"] = this->GetManifests();
  469. vars["LINK_PATH"] = frameworkPath + linkPath;
  470. // Compute architecture specific link flags. Yes, these go into a different
  471. // variable for executables, probably due to a mistake made when duplicating
  472. // code between the Makefile executable and library generators.
  473. if (targetType == cmState::EXECUTABLE)
  474. {
  475. std::string t = vars["FLAGS"];
  476. localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
  477. vars["FLAGS"] = t;
  478. }
  479. else
  480. {
  481. std::string t = vars["ARCH_FLAGS"];
  482. localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
  483. vars["ARCH_FLAGS"] = t;
  484. t = "";
  485. localGen.AddLanguageFlags(t, TargetLinkLanguage, cfgName);
  486. vars["LANGUAGE_COMPILE_FLAGS"] = t;
  487. }
  488. if (this->GetGeneratorTarget()->HasSOName(cfgName))
  489. {
  490. vars["SONAME_FLAG"] = mf->GetSONameFlag(this->TargetLinkLanguage);
  491. vars["SONAME"] = this->TargetNameSO;
  492. if (targetType == cmState::SHARED_LIBRARY)
  493. {
  494. std::string install_dir =
  495. this->GetGeneratorTarget()->GetInstallNameDirForBuildTree(cfgName);
  496. if (!install_dir.empty())
  497. {
  498. vars["INSTALLNAME_DIR"] = localGen.Convert(install_dir,
  499. cmLocalGenerator::NONE,
  500. cmLocalGenerator::SHELL);
  501. }
  502. }
  503. }
  504. cmNinjaDeps byproducts;
  505. if (!this->TargetNameImport.empty())
  506. {
  507. const std::string impLibPath = localGen.ConvertToOutputFormat(
  508. targetOutputImplib,
  509. cmLocalGenerator::SHELL);
  510. vars["TARGET_IMPLIB"] = impLibPath;
  511. EnsureParentDirectoryExists(impLibPath);
  512. if(genTarget.HasImportLibrary())
  513. {
  514. byproducts.push_back(targetOutputImplib);
  515. }
  516. }
  517. if (!this->SetMsvcTargetPdbVariable(vars))
  518. {
  519. // It is common to place debug symbols at a specific place,
  520. // so we need a plain target name in the rule available.
  521. std::string prefix;
  522. std::string base;
  523. std::string suffix;
  524. this->GetGeneratorTarget()->GetFullNameComponents(prefix, base, suffix);
  525. std::string dbg_suffix = ".dbg";
  526. // TODO: Where to document?
  527. if (mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX"))
  528. {
  529. dbg_suffix = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX");
  530. }
  531. vars["TARGET_PDB"] = base + suffix + dbg_suffix;
  532. }
  533. const std::string objPath = GetGeneratorTarget()->GetSupportDirectory();
  534. vars["OBJECT_DIR"] =
  535. this->GetLocalGenerator()->ConvertToOutputFormat(
  536. this->ConvertToNinjaPath(objPath), cmLocalGenerator::SHELL);
  537. EnsureDirectoryExists(objPath);
  538. if (this->GetGlobalGenerator()->IsGCCOnWindows())
  539. {
  540. // ar.exe can't handle backslashes in rsp files (implicitly used by gcc)
  541. std::string& linkLibraries = vars["LINK_LIBRARIES"];
  542. std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/');
  543. std::string& link_path = vars["LINK_PATH"];
  544. std::replace(link_path.begin(), link_path.end(), '\\', '/');
  545. }
  546. const std::vector<cmCustomCommand> *cmdLists[3] = {
  547. &gt.GetPreBuildCommands(),
  548. &gt.GetPreLinkCommands(),
  549. &gt.GetPostBuildCommands()
  550. };
  551. std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
  552. std::vector<std::string> *cmdLineLists[3] = {
  553. &preLinkCmdLines,
  554. &preLinkCmdLines,
  555. &postBuildCmdLines
  556. };
  557. for (unsigned i = 0; i != 3; ++i)
  558. {
  559. for (std::vector<cmCustomCommand>::const_iterator
  560. ci = cmdLists[i]->begin();
  561. ci != cmdLists[i]->end(); ++ci)
  562. {
  563. cmCustomCommandGenerator ccg(*ci, cfgName, this->GetLocalGenerator());
  564. localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
  565. std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
  566. std::transform(ccByproducts.begin(), ccByproducts.end(),
  567. std::back_inserter(byproducts), MapToNinjaPath());
  568. }
  569. }
  570. // maybe create .def file from list of objects
  571. if (gt.GetType() == cmState::SHARED_LIBRARY &&
  572. this->GetMakefile()->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS"))
  573. {
  574. if(gt.GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS"))
  575. {
  576. std::string cmakeCommand =
  577. this->GetLocalGenerator()->ConvertToOutputFormat(
  578. cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
  579. std::string name_of_def_file
  580. = gt.GetSupportDirectory();
  581. name_of_def_file += "/" + gt.GetName();
  582. name_of_def_file += ".def";
  583. std::string cmd = cmakeCommand;
  584. cmd += " -E __create_def ";
  585. cmd += this->GetLocalGenerator()
  586. ->ConvertToOutputFormat(name_of_def_file.c_str(),
  587. cmLocalGenerator::SHELL);
  588. cmd += " ";
  589. cmNinjaDeps objs = this->GetObjects();
  590. std::string obj_list_file = name_of_def_file;
  591. obj_list_file += ".objs";
  592. cmd += this->GetLocalGenerator()
  593. ->ConvertToOutputFormat(obj_list_file.c_str(),
  594. cmLocalGenerator::SHELL);
  595. preLinkCmdLines.push_back(cmd);
  596. // create a list of obj files for the -E __create_def to read
  597. cmGeneratedFileStream fout(obj_list_file.c_str());
  598. for(cmNinjaDeps::iterator i=objs.begin(); i != objs.end(); ++i)
  599. {
  600. if(cmHasLiteralSuffix(*i, ".obj"))
  601. {
  602. fout << *i << "\n";
  603. }
  604. }
  605. }
  606. }
  607. // If we have any PRE_LINK commands, we need to go back to HOME_OUTPUT for
  608. // the link commands.
  609. if (!preLinkCmdLines.empty())
  610. {
  611. const std::string homeOutDir = localGen.ConvertToOutputFormat(
  612. localGen.GetBinaryDirectory(),
  613. cmLocalGenerator::SHELL);
  614. preLinkCmdLines.push_back("cd " + homeOutDir);
  615. }
  616. vars["PRE_LINK"] = localGen.BuildCommandLine(preLinkCmdLines);
  617. std::string postBuildCmdLine = localGen.BuildCommandLine(postBuildCmdLines);
  618. cmNinjaVars symlinkVars;
  619. if (targetOutput == targetOutputReal)
  620. {
  621. vars["POST_BUILD"] = postBuildCmdLine;
  622. }
  623. else
  624. {
  625. vars["POST_BUILD"] = ":";
  626. symlinkVars["POST_BUILD"] = postBuildCmdLine;
  627. }
  628. cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
  629. int commandLineLengthLimit = 1;
  630. const char* forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
  631. if (!mf->IsDefinitionSet(forceRspFile) &&
  632. cmSystemTools::GetEnv(forceRspFile) == 0)
  633. {
  634. commandLineLengthLimit = calculateCommandLineLengthLimit(
  635. globalGen.GetRuleCmdLength(this->LanguageLinkerRule()));
  636. }
  637. const std::string rspfile =
  638. std::string(cmake::GetCMakeFilesDirectoryPostSlash())
  639. + gt.GetName() + ".rsp";
  640. // Gather order-only dependencies.
  641. cmNinjaDeps orderOnlyDeps;
  642. this->GetLocalGenerator()->AppendTargetDepends(this->GetGeneratorTarget(),
  643. orderOnlyDeps);
  644. // Ninja should restat after linking if and only if there are byproducts.
  645. vars["RESTAT"] = byproducts.empty()? "" : "1";
  646. for (cmNinjaDeps::const_iterator oi = byproducts.begin(),
  647. oe = byproducts.end();
  648. oi != oe; ++oi)
  649. {
  650. this->GetGlobalGenerator()->SeenCustomCommandOutput(*oi);
  651. outputs.push_back(*oi);
  652. }
  653. // Write the build statement for this target.
  654. bool usedResponseFile = false;
  655. globalGen.WriteBuild(this->GetBuildFileStream(),
  656. comment.str(),
  657. this->LanguageLinkerRule(),
  658. outputs,
  659. explicitDeps,
  660. implicitDeps,
  661. orderOnlyDeps,
  662. vars,
  663. rspfile,
  664. commandLineLengthLimit,
  665. &usedResponseFile);
  666. this->WriteLinkRule(usedResponseFile);
  667. if (targetOutput != targetOutputReal && !gt.IsFrameworkOnApple())
  668. {
  669. if (targetType == cmState::EXECUTABLE)
  670. {
  671. globalGen.WriteBuild(this->GetBuildFileStream(),
  672. "Create executable symlink " + targetOutput,
  673. "CMAKE_SYMLINK_EXECUTABLE",
  674. cmNinjaDeps(1, targetOutput),
  675. cmNinjaDeps(1, targetOutputReal),
  676. emptyDeps,
  677. emptyDeps,
  678. symlinkVars);
  679. }
  680. else
  681. {
  682. cmNinjaDeps symlinks;
  683. const std::string soName = this->GetTargetFilePath(this->TargetNameSO);
  684. // If one link has to be created.
  685. if (targetOutputReal == soName || targetOutput == soName)
  686. {
  687. symlinkVars["SONAME"] = soName;
  688. }
  689. else
  690. {
  691. symlinkVars["SONAME"] = "";
  692. symlinks.push_back(soName);
  693. }
  694. symlinks.push_back(targetOutput);
  695. globalGen.WriteBuild(this->GetBuildFileStream(),
  696. "Create library symlink " + targetOutput,
  697. "CMAKE_SYMLINK_LIBRARY",
  698. symlinks,
  699. cmNinjaDeps(1, targetOutputReal),
  700. emptyDeps,
  701. emptyDeps,
  702. symlinkVars);
  703. }
  704. }
  705. // Add aliases for the file name and the target name.
  706. globalGen.AddTargetAlias(this->TargetNameOut, &gt);
  707. globalGen.AddTargetAlias(this->GetTargetName(), &gt);
  708. }
  709. //----------------------------------------------------------------------------
  710. void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()
  711. {
  712. // Write a phony output that depends on all object files.
  713. cmNinjaDeps outputs;
  714. this->GetLocalGenerator()->AppendTargetOutputs(this->GetGeneratorTarget(),
  715. outputs);
  716. cmNinjaDeps depends = this->GetObjects();
  717. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  718. "Object library "
  719. + this->GetTargetName(),
  720. outputs,
  721. depends);
  722. // Add aliases for the target name.
  723. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  724. this->GetGeneratorTarget());
  725. }