cmNinjaNormalTargetGenerator.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 <assert.h>
  19. #include <algorithm>
  20. #ifndef _WIN32
  21. #include <unistd.h>
  22. #endif
  23. cmNinjaNormalTargetGenerator::
  24. cmNinjaNormalTargetGenerator(cmTarget* target)
  25. : cmNinjaTargetGenerator(target)
  26. , TargetNameOut()
  27. , TargetNameSO()
  28. , TargetNameReal()
  29. , TargetNameImport()
  30. , TargetNamePDB()
  31. , TargetLinkLanguage(0)
  32. {
  33. cmOSXBundleGenerator::PrepareTargetProperties(target);
  34. this->TargetLinkLanguage = target->GetLinkerLanguage(this->GetConfigName());
  35. if (target->GetType() == cmTarget::EXECUTABLE)
  36. target->GetExecutableNames(this->TargetNameOut,
  37. this->TargetNameReal,
  38. this->TargetNameImport,
  39. this->TargetNamePDB,
  40. GetLocalGenerator()->GetConfigName());
  41. else
  42. target->GetLibraryNames(this->TargetNameOut,
  43. this->TargetNameSO,
  44. this->TargetNameReal,
  45. this->TargetNameImport,
  46. this->TargetNamePDB,
  47. GetLocalGenerator()->GetConfigName());
  48. if(target->GetType() != cmTarget::OBJECT_LIBRARY)
  49. {
  50. // on Windows the output dir is already needed at compile time
  51. // ensure the directory exists (OutDir test)
  52. EnsureDirectoryExists(target->GetDirectory(this->GetConfigName()));
  53. }
  54. this->OSXBundleGenerator = new cmOSXBundleGenerator(target,
  55. this->TargetNameOut,
  56. this->GetConfigName());
  57. this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
  58. }
  59. cmNinjaNormalTargetGenerator::~cmNinjaNormalTargetGenerator()
  60. {
  61. delete this->OSXBundleGenerator;
  62. }
  63. void cmNinjaNormalTargetGenerator::Generate()
  64. {
  65. if (!this->TargetLinkLanguage) {
  66. cmSystemTools::Error("CMake can not determine linker language for target:",
  67. this->GetTarget()->GetName());
  68. return;
  69. }
  70. // Write the rules for each language.
  71. this->WriteLanguagesRules();
  72. // Write the build statements
  73. this->WriteObjectBuildStatements();
  74. if(this->GetTarget()->GetType() == cmTarget::OBJECT_LIBRARY)
  75. {
  76. this->WriteObjectLibStatement();
  77. }
  78. else
  79. {
  80. this->WriteLinkRule(false); // write rule without rspfile support
  81. this->WriteLinkRule(true); // write rule with rspfile support
  82. this->WriteLinkStatement();
  83. }
  84. }
  85. void cmNinjaNormalTargetGenerator::WriteLanguagesRules()
  86. {
  87. #ifdef NINJA_GEN_VERBOSE_FILES
  88. cmGlobalNinjaGenerator::WriteDivider(this->GetRulesFileStream());
  89. this->GetRulesFileStream()
  90. << "# Rules for each languages for "
  91. << cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  92. << " target "
  93. << this->GetTargetName()
  94. << "\n\n";
  95. #endif
  96. std::set<cmStdString> languages;
  97. this->GetTarget()->GetLanguages(languages);
  98. for(std::set<cmStdString>::const_iterator l = languages.begin();
  99. l != languages.end();
  100. ++l)
  101. this->WriteLanguageRules(*l);
  102. }
  103. const char *cmNinjaNormalTargetGenerator::GetVisibleTypeName() const
  104. {
  105. switch (this->GetTarget()->GetType()) {
  106. case cmTarget::STATIC_LIBRARY:
  107. return "static library";
  108. case cmTarget::SHARED_LIBRARY:
  109. return "shared library";
  110. case cmTarget::MODULE_LIBRARY:
  111. if (this->GetTarget()->IsCFBundleOnApple())
  112. return "CFBundle shared module";
  113. else
  114. return "shared module";
  115. case cmTarget::EXECUTABLE:
  116. return "executable";
  117. default:
  118. return 0;
  119. }
  120. }
  121. std::string
  122. cmNinjaNormalTargetGenerator
  123. ::LanguageLinkerRule() const
  124. {
  125. return std::string(this->TargetLinkLanguage)
  126. + "_"
  127. + cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  128. + "_LINKER";
  129. }
  130. void
  131. cmNinjaNormalTargetGenerator
  132. ::WriteLinkRule(bool useResponseFile)
  133. {
  134. cmTarget::TargetType targetType = this->GetTarget()->GetType();
  135. std::string ruleName = this->LanguageLinkerRule();
  136. if (useResponseFile)
  137. ruleName += "_RSPFILE";
  138. // Select whether to use a response file for objects.
  139. std::string rspfile;
  140. std::string rspcontent;
  141. if (!this->GetGlobalGenerator()->HasRule(ruleName)) {
  142. cmLocalGenerator::RuleVariables vars;
  143. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  144. vars.CMTarget = this->GetTarget();
  145. vars.Language = this->TargetLinkLanguage;
  146. std::string responseFlag;
  147. if (!useResponseFile) {
  148. vars.Objects = "$in";
  149. vars.LinkLibraries = "$LINK_LIBRARIES";
  150. } else {
  151. // handle response file
  152. std::string cmakeLinkVar = std::string("CMAKE_") +
  153. this->TargetLinkLanguage + "_RESPONSE_FILE_LINK_FLAG";
  154. const char * flag = GetMakefile()->GetDefinition(cmakeLinkVar.c_str());
  155. if(flag) {
  156. responseFlag = flag;
  157. } else {
  158. responseFlag = "@";
  159. }
  160. rspfile = "$out.rsp";
  161. responseFlag += rspfile;
  162. rspcontent = "$in $LINK_LIBRARIES";
  163. vars.Objects = responseFlag.c_str();
  164. vars.LinkLibraries = "";
  165. }
  166. vars.ObjectDir = "$OBJECT_DIR";
  167. // TODO:
  168. // Makefile generator expands <TARGET> to the plain target name
  169. // with suffix. $out expands to a relative path. This difference
  170. // could make trouble when switching to Ninja generator. Maybe
  171. // using TARGET_NAME and RuleVariables::TargetName is a fix.
  172. vars.Target = "$out";
  173. vars.SONameFlag = "$SONAME_FLAG";
  174. vars.TargetSOName = "$SONAME";
  175. vars.TargetInstallNameDir = "$INSTALLNAME_DIR";
  176. vars.TargetPDB = "$TARGET_PDB";
  177. // Setup the target version.
  178. std::string targetVersionMajor;
  179. std::string targetVersionMinor;
  180. {
  181. cmOStringStream majorStream;
  182. cmOStringStream minorStream;
  183. int major;
  184. int minor;
  185. this->GetTarget()->GetTargetVersion(major, minor);
  186. majorStream << major;
  187. minorStream << minor;
  188. targetVersionMajor = majorStream.str();
  189. targetVersionMinor = minorStream.str();
  190. }
  191. vars.TargetVersionMajor = targetVersionMajor.c_str();
  192. vars.TargetVersionMinor = targetVersionMinor.c_str();
  193. vars.Flags = "$FLAGS";
  194. vars.LinkFlags = "$LINK_FLAGS";
  195. std::string langFlags;
  196. if (targetType != cmTarget::EXECUTABLE) {
  197. this->GetLocalGenerator()->AddLanguageFlags(langFlags,
  198. this->TargetLinkLanguage,
  199. this->GetConfigName());
  200. langFlags += " $ARCH_FLAGS";
  201. vars.LanguageCompileFlags = langFlags.c_str();
  202. }
  203. // Rule for linking library/executable.
  204. std::vector<std::string> linkCmds = this->ComputeLinkCmd();
  205. for(std::vector<std::string>::iterator i = linkCmds.begin();
  206. i != linkCmds.end();
  207. ++i)
  208. {
  209. this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
  210. }
  211. linkCmds.insert(linkCmds.begin(), "$PRE_LINK");
  212. linkCmds.push_back("$POST_BUILD");
  213. std::string linkCmd =
  214. this->GetLocalGenerator()->BuildCommandLine(linkCmds);
  215. // Write the linker rule with response file if needed.
  216. cmOStringStream comment;
  217. comment << "Rule for linking " << this->TargetLinkLanguage << " "
  218. << this->GetVisibleTypeName() << ".";
  219. cmOStringStream description;
  220. description << "Linking " << this->TargetLinkLanguage << " "
  221. << this->GetVisibleTypeName() << " $out";
  222. this->GetGlobalGenerator()->AddRule(ruleName,
  223. linkCmd,
  224. description.str(),
  225. comment.str(),
  226. /*depfile*/ "",
  227. rspfile,
  228. rspcontent);
  229. }
  230. if (this->TargetNameOut != this->TargetNameReal) {
  231. std::string cmakeCommand =
  232. this->GetLocalGenerator()->ConvertToOutputFormat(
  233. this->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND"),
  234. cmLocalGenerator::SHELL);
  235. if (targetType == cmTarget::EXECUTABLE)
  236. this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_EXECUTABLE",
  237. cmakeCommand +
  238. " -E cmake_symlink_executable"
  239. " $in $out && $POST_BUILD",
  240. "Creating executable symlink $out",
  241. "Rule for creating executable symlink.");
  242. else
  243. this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_LIBRARY",
  244. cmakeCommand +
  245. " -E cmake_symlink_library"
  246. " $in $SONAME $out && $POST_BUILD",
  247. "Creating library symlink $out",
  248. "Rule for creating library symlink.");
  249. }
  250. }
  251. std::vector<std::string>
  252. cmNinjaNormalTargetGenerator
  253. ::ComputeLinkCmd()
  254. {
  255. std::vector<std::string> linkCmds;
  256. cmTarget::TargetType targetType = this->GetTarget()->GetType();
  257. switch (targetType) {
  258. case cmTarget::STATIC_LIBRARY: {
  259. // Check if you have a non archive way to create the static library.
  260. {
  261. std::string linkCmdVar = "CMAKE_";
  262. linkCmdVar += this->TargetLinkLanguage;
  263. linkCmdVar += "_CREATE_STATIC_LIBRARY";
  264. if (const char *linkCmd =
  265. this->GetMakefile()->GetDefinition(linkCmdVar.c_str()))
  266. {
  267. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  268. return linkCmds;
  269. }
  270. }
  271. // We have archive link commands set. First, delete the existing archive.
  272. std::string cmakeCommand =
  273. this->GetLocalGenerator()->ConvertToOutputFormat(
  274. this->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND"),
  275. cmLocalGenerator::SHELL);
  276. linkCmds.push_back(cmakeCommand + " -E remove $out");
  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 =
  283. this->GetMakefile()->GetRequiredDefinition(linkCmdVar.c_str());
  284. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  285. }
  286. {
  287. std::string linkCmdVar = "CMAKE_";
  288. linkCmdVar += this->TargetLinkLanguage;
  289. linkCmdVar += "_ARCHIVE_FINISH";
  290. const char *linkCmd =
  291. this->GetMakefile()->GetRequiredDefinition(linkCmdVar.c_str());
  292. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  293. }
  294. return linkCmds;
  295. }
  296. case cmTarget::SHARED_LIBRARY:
  297. case cmTarget::MODULE_LIBRARY:
  298. case cmTarget::EXECUTABLE: {
  299. std::string linkCmdVar = "CMAKE_";
  300. linkCmdVar += this->TargetLinkLanguage;
  301. switch (targetType) {
  302. case cmTarget::SHARED_LIBRARY:
  303. linkCmdVar += "_CREATE_SHARED_LIBRARY";
  304. break;
  305. case cmTarget::MODULE_LIBRARY:
  306. linkCmdVar += "_CREATE_SHARED_MODULE";
  307. break;
  308. case cmTarget::EXECUTABLE:
  309. linkCmdVar += "_LINK_EXECUTABLE";
  310. break;
  311. default:
  312. assert(0 && "Unexpected target type");
  313. }
  314. const char *linkCmd =
  315. this->GetMakefile()->GetRequiredDefinition(linkCmdVar.c_str());
  316. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  317. return linkCmds;
  318. }
  319. default:
  320. assert(0 && "Unexpected target type");
  321. }
  322. return std::vector<std::string>();
  323. }
  324. void cmNinjaNormalTargetGenerator::WriteLinkStatement()
  325. {
  326. cmTarget::TargetType targetType = this->GetTarget()->GetType();
  327. std::string targetOutput = ConvertToNinjaPath(
  328. this->GetTarget()->GetFullPath(this->GetConfigName()).c_str());
  329. std::string targetOutputReal = ConvertToNinjaPath(
  330. this->GetTarget()->GetFullPath(this->GetConfigName(),
  331. /*implib=*/false,
  332. /*realpath=*/true).c_str());
  333. std::string targetOutputImplib = ConvertToNinjaPath(
  334. this->GetTarget()->GetFullPath(this->GetConfigName(),
  335. /*implib=*/true).c_str());
  336. if (this->GetTarget()->IsAppBundleOnApple())
  337. {
  338. // Create the app bundle
  339. std::string outpath;
  340. this->OSXBundleGenerator->CreateAppBundle(this->TargetNameOut, outpath);
  341. // Calculate the output path
  342. targetOutput = outpath + this->TargetNameOut;
  343. targetOutput = this->ConvertToNinjaPath(targetOutput.c_str());
  344. targetOutputReal = outpath + this->TargetNameReal;
  345. targetOutputReal = this->ConvertToNinjaPath(targetOutputReal.c_str());
  346. }
  347. else if (this->GetTarget()->IsFrameworkOnApple())
  348. {
  349. // Create the library framework.
  350. this->OSXBundleGenerator->CreateFramework(this->TargetNameOut);
  351. }
  352. else if(this->GetTarget()->IsCFBundleOnApple())
  353. {
  354. // Create the core foundation bundle.
  355. std::string outpath;
  356. this->OSXBundleGenerator->CreateCFBundle(this->TargetNameOut, outpath);
  357. }
  358. // Write comments.
  359. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  360. this->GetBuildFileStream()
  361. << "# Link build statements for "
  362. << cmTarget::GetTargetTypeName(targetType)
  363. << " target "
  364. << this->GetTargetName()
  365. << "\n\n";
  366. cmNinjaDeps emptyDeps;
  367. cmNinjaVars vars;
  368. // Compute the comment.
  369. cmOStringStream 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. this->GetLocalGenerator()->GetTargetFlags(vars["LINK_LIBRARIES"],
  379. vars["FLAGS"],
  380. vars["LINK_FLAGS"],
  381. this->GetGeneratorTarget());
  382. this->AddModuleDefinitionFlag(vars["LINK_FLAGS"]);
  383. // Compute architecture specific link flags. Yes, these go into a different
  384. // variable for executables, probably due to a mistake made when duplicating
  385. // code between the Makefile executable and library generators.
  386. std::string flags = (targetType == cmTarget::EXECUTABLE
  387. ? vars["FLAGS"]
  388. : vars["ARCH_FLAGS"]);
  389. this->GetLocalGenerator()->AddArchitectureFlags(flags,
  390. this->GetGeneratorTarget(),
  391. this->TargetLinkLanguage,
  392. this->GetConfigName());
  393. if (targetType == cmTarget::EXECUTABLE) {
  394. vars["FLAGS"] = flags;
  395. } else {
  396. vars["ARCH_FLAGS"] = flags;
  397. }
  398. if (this->GetTarget()->HasSOName(this->GetConfigName())) {
  399. vars["SONAME_FLAG"] =
  400. this->GetMakefile()->GetSONameFlag(this->TargetLinkLanguage);
  401. vars["SONAME"] = this->TargetNameSO;
  402. if (targetType == cmTarget::SHARED_LIBRARY) {
  403. std::string install_name_dir = this->GetTarget()
  404. ->GetInstallNameDirForBuildTree(this->GetConfigName());
  405. if (!install_name_dir.empty()) {
  406. vars["INSTALLNAME_DIR"] =
  407. this->GetLocalGenerator()->Convert(install_name_dir.c_str(),
  408. cmLocalGenerator::NONE,
  409. cmLocalGenerator::SHELL, false);
  410. }
  411. }
  412. }
  413. if (!this->TargetNameImport.empty()) {
  414. const std::string impLibPath = this->GetLocalGenerator()
  415. ->ConvertToOutputFormat(targetOutputImplib.c_str(),
  416. cmLocalGenerator::SHELL);
  417. vars["TARGET_IMPLIB"] = impLibPath;
  418. EnsureParentDirectoryExists(impLibPath);
  419. }
  420. cmMakefile* mf = this->GetMakefile();
  421. if (!this->SetMsvcTargetPdbVariable(vars))
  422. {
  423. // It is common to place debug symbols at a specific place,
  424. // so we need a plain target name in the rule available.
  425. std::string prefix;
  426. std::string base;
  427. std::string suffix;
  428. this->GetTarget()->GetFullNameComponents(prefix, base, suffix);
  429. std::string dbg_suffix = ".dbg";
  430. // TODO: Where to document?
  431. if (mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX"))
  432. dbg_suffix = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX");
  433. vars["TARGET_PDB"] = base + suffix + dbg_suffix;
  434. }
  435. if (mf->IsOn("CMAKE_COMPILER_IS_MINGW"))
  436. {
  437. const std::string objPath = GetTarget()->GetSupportDirectory();
  438. vars["OBJECT_DIR"] = ConvertToNinjaPath(objPath.c_str());
  439. EnsureDirectoryExists(objPath);
  440. // ar.exe can't handle backslashes in rsp files (implictly used by gcc)
  441. std::string& linkLibraries = vars["LINK_LIBRARIES"];
  442. std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/');
  443. }
  444. std::vector<cmCustomCommand> *cmdLists[3] = {
  445. &this->GetTarget()->GetPreBuildCommands(),
  446. &this->GetTarget()->GetPreLinkCommands(),
  447. &this->GetTarget()->GetPostBuildCommands()
  448. };
  449. std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
  450. std::vector<std::string> *cmdLineLists[3] = {
  451. &preLinkCmdLines,
  452. &preLinkCmdLines,
  453. &postBuildCmdLines
  454. };
  455. for (unsigned i = 0; i != 3; ++i) {
  456. for (std::vector<cmCustomCommand>::const_iterator
  457. ci = cmdLists[i]->begin();
  458. ci != cmdLists[i]->end(); ++ci) {
  459. this->GetLocalGenerator()->AppendCustomCommandLines(&*ci,
  460. *cmdLineLists[i]);
  461. }
  462. }
  463. // If we have any PRE_LINK commands, we need to go back to HOME_OUTPUT for
  464. // the link commands.
  465. if (!preLinkCmdLines.empty()) {
  466. const std::string homeOutDir = this->GetLocalGenerator()
  467. ->ConvertToOutputFormat(this->GetMakefile()->GetHomeOutputDirectory(),
  468. cmLocalGenerator::SHELL);
  469. preLinkCmdLines.push_back("cd " + homeOutDir);
  470. }
  471. vars["PRE_LINK"] =
  472. this->GetLocalGenerator()->BuildCommandLine(preLinkCmdLines);
  473. std::string postBuildCmdLine =
  474. this->GetLocalGenerator()->BuildCommandLine(postBuildCmdLines);
  475. cmNinjaVars symlinkVars;
  476. if (targetOutput == targetOutputReal) {
  477. vars["POST_BUILD"] = postBuildCmdLine;
  478. } else {
  479. vars["POST_BUILD"] = ":";
  480. symlinkVars["POST_BUILD"] = postBuildCmdLine;
  481. }
  482. int linkRuleLength = this->GetGlobalGenerator()->
  483. GetRuleCmdLength(this->LanguageLinkerRule());
  484. #ifdef _WIN32
  485. int commandLineLengthLimit = 8000 - linkRuleLength;
  486. #elif defined(__linux) || defined(__APPLE__)
  487. // for instance ARG_MAX is 2096152 on Ubuntu or 262144 on Mac
  488. int commandLineLengthLimit = ((int)sysconf(_SC_ARG_MAX))
  489. - linkRuleLength - 1000;
  490. #else
  491. int commandLineLengthLimit = -1;
  492. #endif
  493. // Write the build statement for this target.
  494. cmGlobalNinjaGenerator::WriteBuild(this->GetBuildFileStream(),
  495. comment.str(),
  496. this->LanguageLinkerRule(),
  497. outputs,
  498. explicitDeps,
  499. implicitDeps,
  500. emptyDeps,
  501. vars,
  502. commandLineLengthLimit);
  503. if (targetOutput != targetOutputReal) {
  504. if (targetType == cmTarget::EXECUTABLE) {
  505. cmGlobalNinjaGenerator::WriteBuild(this->GetBuildFileStream(),
  506. "Create executable symlink " + targetOutput,
  507. "CMAKE_SYMLINK_EXECUTABLE",
  508. cmNinjaDeps(1, targetOutput),
  509. cmNinjaDeps(1, targetOutputReal),
  510. emptyDeps,
  511. emptyDeps,
  512. symlinkVars);
  513. } else {
  514. cmNinjaDeps symlinks;
  515. const std::string soName = this->GetTargetFilePath(this->TargetNameSO);
  516. // If one link has to be created.
  517. if (targetOutputReal == soName || targetOutput == soName) {
  518. symlinkVars["SONAME"] = soName;
  519. } else {
  520. symlinkVars["SONAME"] = "";
  521. symlinks.push_back(soName);
  522. }
  523. symlinks.push_back(targetOutput);
  524. cmGlobalNinjaGenerator::WriteBuild(this->GetBuildFileStream(),
  525. "Create library symlink " + targetOutput,
  526. "CMAKE_SYMLINK_LIBRARY",
  527. symlinks,
  528. cmNinjaDeps(1, targetOutputReal),
  529. emptyDeps,
  530. emptyDeps,
  531. symlinkVars);
  532. }
  533. }
  534. if (!this->TargetNameImport.empty()) {
  535. // Since using multiple outputs would mess up the $out variable, use an
  536. // alias for the import library.
  537. cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
  538. "Alias for import library.",
  539. cmNinjaDeps(1, targetOutputImplib),
  540. cmNinjaDeps(1, targetOutputReal));
  541. }
  542. // Add aliases for the file name and the target name.
  543. this->GetGlobalGenerator()->AddTargetAlias(this->TargetNameOut,
  544. this->GetTarget());
  545. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  546. this->GetTarget());
  547. }
  548. //----------------------------------------------------------------------------
  549. void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()
  550. {
  551. // Write a phony output that depends on all object files.
  552. cmNinjaDeps outputs;
  553. this->GetLocalGenerator()->AppendTargetOutputs(this->GetTarget(), outputs);
  554. cmNinjaDeps depends = this->GetObjects();
  555. cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
  556. "Object library "
  557. + this->GetTargetName(),
  558. outputs,
  559. depends);
  560. // Add aliases for the target name.
  561. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  562. this->GetTarget());
  563. }