cmNinjaNormalTargetGenerator.cxx 25 KB

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