cmNinjaNormalTargetGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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 <assert.h>
  18. #include <algorithm>
  19. #ifndef _WIN32
  20. #include <unistd.h>
  21. #endif
  22. cmNinjaNormalTargetGenerator::
  23. cmNinjaNormalTargetGenerator(cmTarget* target)
  24. : cmNinjaTargetGenerator(target)
  25. , TargetNameOut()
  26. , TargetNameSO()
  27. , TargetNameReal()
  28. , TargetNameImport()
  29. , TargetNamePDB()
  30. , TargetLinkLanguage(0)
  31. , MacContentDirectory()
  32. , FrameworkVersion()
  33. {
  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. // TODO: Factor with the cmMakefileExecutableTargetGenerator constructor.
  55. if(target->IsAppBundleOnApple())
  56. {
  57. this->MacContentDirectory = target->GetDirectory(this->GetConfigName());
  58. this->MacContentDirectory += "/";
  59. this->MacContentDirectory += this->TargetNameOut;
  60. this->MacContentDirectory += ".app/Contents/";
  61. }
  62. // TODO: Factor with the cmMakefileLibraryTargetGenerator constructor.
  63. else if(target->IsFrameworkOnApple())
  64. {
  65. this->FrameworkVersion = target->GetFrameworkVersion();
  66. this->MacContentDirectory = target->GetDirectory(this->GetConfigName());
  67. this->MacContentDirectory += "/";
  68. this->MacContentDirectory += this->TargetNameOut;
  69. this->MacContentDirectory += ".framework/Versions/";
  70. this->MacContentDirectory += this->FrameworkVersion;
  71. this->MacContentDirectory += "/";
  72. }
  73. else if(target->IsCFBundleOnApple())
  74. {
  75. this->MacContentDirectory = target->GetDirectory(this->GetConfigName());
  76. this->MacContentDirectory += "/";
  77. this->MacContentDirectory += this->TargetNameOut;
  78. this->MacContentDirectory += ".";
  79. const char *ext = target->GetProperty("BUNDLE_EXTENSION");
  80. if (!ext)
  81. {
  82. ext = "bundle";
  83. }
  84. this->MacContentDirectory += ext;
  85. this->MacContentDirectory += "/Contents/";
  86. }
  87. }
  88. cmNinjaNormalTargetGenerator::~cmNinjaNormalTargetGenerator()
  89. {
  90. }
  91. void cmNinjaNormalTargetGenerator::Generate()
  92. {
  93. if (!this->TargetLinkLanguage) {
  94. cmSystemTools::Error("CMake can not determine linker language for target:",
  95. this->GetTarget()->GetName());
  96. return;
  97. }
  98. // Write the rules for each language.
  99. this->WriteLanguagesRules();
  100. // Write the build statements
  101. this->WriteObjectBuildStatements();
  102. if(this->GetTarget()->GetType() == cmTarget::OBJECT_LIBRARY)
  103. {
  104. this->WriteObjectLibStatement();
  105. }
  106. else
  107. {
  108. this->WriteLinkRule(false); // write rule without rspfile support
  109. this->WriteLinkRule(true); // write rule with rspfile support
  110. this->WriteLinkStatement();
  111. }
  112. }
  113. void cmNinjaNormalTargetGenerator::WriteLanguagesRules()
  114. {
  115. #ifdef NINJA_GEN_VERBOSE_FILES
  116. cmGlobalNinjaGenerator::WriteDivider(this->GetRulesFileStream());
  117. this->GetRulesFileStream()
  118. << "# Rules for each languages for "
  119. << cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  120. << " target "
  121. << this->GetTargetName()
  122. << "\n\n";
  123. #endif
  124. std::set<cmStdString> languages;
  125. this->GetTarget()->GetLanguages(languages);
  126. for(std::set<cmStdString>::const_iterator l = languages.begin();
  127. l != languages.end();
  128. ++l)
  129. this->WriteLanguageRules(*l);
  130. }
  131. const char *cmNinjaNormalTargetGenerator::GetVisibleTypeName() const
  132. {
  133. switch (this->GetTarget()->GetType()) {
  134. case cmTarget::STATIC_LIBRARY:
  135. return "static library";
  136. case cmTarget::SHARED_LIBRARY:
  137. return "shared library";
  138. case cmTarget::MODULE_LIBRARY:
  139. return "shared module";
  140. case cmTarget::EXECUTABLE:
  141. return "executable";
  142. default:
  143. return 0;
  144. }
  145. }
  146. std::string
  147. cmNinjaNormalTargetGenerator
  148. ::LanguageLinkerRule() const
  149. {
  150. return std::string(this->TargetLinkLanguage)
  151. + "_"
  152. + cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  153. + "_LINKER";
  154. }
  155. void
  156. cmNinjaNormalTargetGenerator
  157. ::WriteLinkRule(bool useResponseFile)
  158. {
  159. cmTarget::TargetType targetType = this->GetTarget()->GetType();
  160. std::string ruleName = this->LanguageLinkerRule();
  161. if (useResponseFile)
  162. ruleName += "_RSPFILE";
  163. // Select whether to use a response file for objects.
  164. std::string rspfile;
  165. std::string rspcontent;
  166. if (!this->GetGlobalGenerator()->HasRule(ruleName)) {
  167. cmLocalGenerator::RuleVariables vars;
  168. vars.RuleLauncher = "RULE_LAUNCH_LINK";
  169. vars.CMTarget = this->GetTarget();
  170. vars.Language = this->TargetLinkLanguage;
  171. std::string responseFlag;
  172. if (!useResponseFile) {
  173. vars.Objects = "$in";
  174. vars.LinkLibraries = "$LINK_LIBRARIES";
  175. } else {
  176. // handle response file
  177. std::string cmakeLinkVar = std::string("CMAKE_") +
  178. this->TargetLinkLanguage + "_RESPONSE_FILE_LINK_FLAG";
  179. const char * flag = GetMakefile()->GetDefinition(cmakeLinkVar.c_str());
  180. if(flag) {
  181. responseFlag = flag;
  182. } else {
  183. responseFlag = "@";
  184. }
  185. rspfile = "$out.rsp";
  186. responseFlag += rspfile;
  187. rspcontent = "$in $LINK_LIBRARIES";
  188. vars.Objects = responseFlag.c_str();
  189. vars.LinkLibraries = "";
  190. }
  191. vars.ObjectDir = "$OBJECT_DIR";
  192. vars.Target = "$out";
  193. vars.SONameFlag = "$SONAME_FLAG";
  194. vars.TargetSOName = "$SONAME";
  195. vars.TargetInstallNameDir = "$INSTALLNAME_DIR";
  196. vars.TargetPDB = "$TARGET_PDB";
  197. // Setup the target version.
  198. std::string targetVersionMajor;
  199. std::string targetVersionMinor;
  200. {
  201. cmOStringStream majorStream;
  202. cmOStringStream minorStream;
  203. int major;
  204. int minor;
  205. this->GetTarget()->GetTargetVersion(major, minor);
  206. majorStream << major;
  207. minorStream << minor;
  208. targetVersionMajor = majorStream.str();
  209. targetVersionMinor = minorStream.str();
  210. }
  211. vars.TargetVersionMajor = targetVersionMajor.c_str();
  212. vars.TargetVersionMinor = targetVersionMinor.c_str();
  213. vars.Flags = "$FLAGS";
  214. vars.LinkFlags = "$LINK_FLAGS";
  215. std::string langFlags;
  216. if (targetType != cmTarget::EXECUTABLE) {
  217. this->GetLocalGenerator()->AddLanguageFlags(langFlags,
  218. this->TargetLinkLanguage,
  219. this->GetConfigName());
  220. langFlags += " $ARCH_FLAGS";
  221. vars.LanguageCompileFlags = langFlags.c_str();
  222. }
  223. // Rule for linking library/executable.
  224. std::vector<std::string> linkCmds = this->ComputeLinkCmd();
  225. for(std::vector<std::string>::iterator i = linkCmds.begin();
  226. i != linkCmds.end();
  227. ++i)
  228. {
  229. this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
  230. }
  231. linkCmds.insert(linkCmds.begin(), "$PRE_LINK");
  232. linkCmds.push_back("$POST_BUILD");
  233. std::string linkCmd =
  234. this->GetLocalGenerator()->BuildCommandLine(linkCmds);
  235. // Write the linker rule with response file if needed.
  236. cmOStringStream comment;
  237. comment << "Rule for linking " << this->TargetLinkLanguage << " "
  238. << this->GetVisibleTypeName() << ".";
  239. cmOStringStream description;
  240. description << "Linking " << this->TargetLinkLanguage << " "
  241. << this->GetVisibleTypeName() << " $out";
  242. this->GetGlobalGenerator()->AddRule(ruleName,
  243. linkCmd,
  244. description.str(),
  245. comment.str(),
  246. /*depfile*/ "",
  247. rspfile,
  248. rspcontent);
  249. }
  250. if (this->TargetNameOut != this->TargetNameReal) {
  251. std::string cmakeCommand =
  252. this->GetLocalGenerator()->ConvertToOutputFormat(
  253. this->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND"),
  254. cmLocalGenerator::SHELL);
  255. if (targetType == cmTarget::EXECUTABLE)
  256. this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_EXECUTABLE",
  257. cmakeCommand +
  258. " -E cmake_symlink_executable"
  259. " $in $out && $POST_BUILD",
  260. "Creating executable symlink $out",
  261. "Rule for creating executable symlink.");
  262. else
  263. this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_LIBRARY",
  264. cmakeCommand +
  265. " -E cmake_symlink_library"
  266. " $in $SONAME $out && $POST_BUILD",
  267. "Creating library symlink $out",
  268. "Rule for creating library symlink.");
  269. }
  270. }
  271. std::vector<std::string>
  272. cmNinjaNormalTargetGenerator
  273. ::ComputeLinkCmd()
  274. {
  275. std::vector<std::string> linkCmds;
  276. cmTarget::TargetType targetType = this->GetTarget()->GetType();
  277. switch (targetType) {
  278. case cmTarget::STATIC_LIBRARY: {
  279. // Check if you have a non archive way to create the static library.
  280. {
  281. std::string linkCmdVar = "CMAKE_";
  282. linkCmdVar += this->TargetLinkLanguage;
  283. linkCmdVar += "_CREATE_STATIC_LIBRARY";
  284. if (const char *linkCmd =
  285. this->GetMakefile()->GetDefinition(linkCmdVar.c_str()))
  286. {
  287. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  288. return linkCmds;
  289. }
  290. }
  291. // We have archive link commands set. First, delete the existing archive.
  292. std::string cmakeCommand =
  293. this->GetLocalGenerator()->ConvertToOutputFormat(
  294. this->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND"),
  295. cmLocalGenerator::SHELL);
  296. linkCmds.push_back(cmakeCommand + " -E remove $out");
  297. // TODO: Use ARCHIVE_APPEND for archives over a certain size.
  298. {
  299. std::string linkCmdVar = "CMAKE_";
  300. linkCmdVar += this->TargetLinkLanguage;
  301. linkCmdVar += "_ARCHIVE_CREATE";
  302. const char *linkCmd =
  303. this->GetMakefile()->GetRequiredDefinition(linkCmdVar.c_str());
  304. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  305. }
  306. {
  307. std::string linkCmdVar = "CMAKE_";
  308. linkCmdVar += this->TargetLinkLanguage;
  309. linkCmdVar += "_ARCHIVE_FINISH";
  310. const char *linkCmd =
  311. this->GetMakefile()->GetRequiredDefinition(linkCmdVar.c_str());
  312. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  313. }
  314. return linkCmds;
  315. }
  316. case cmTarget::SHARED_LIBRARY:
  317. case cmTarget::MODULE_LIBRARY:
  318. case cmTarget::EXECUTABLE: {
  319. std::string linkCmdVar = "CMAKE_";
  320. linkCmdVar += this->TargetLinkLanguage;
  321. switch (targetType) {
  322. case cmTarget::SHARED_LIBRARY:
  323. linkCmdVar += "_CREATE_SHARED_LIBRARY";
  324. break;
  325. case cmTarget::MODULE_LIBRARY:
  326. linkCmdVar += "_CREATE_SHARED_MODULE";
  327. break;
  328. case cmTarget::EXECUTABLE:
  329. linkCmdVar += "_LINK_EXECUTABLE";
  330. break;
  331. default:
  332. assert(0 && "Unexpected target type");
  333. }
  334. const char *linkCmd =
  335. this->GetMakefile()->GetRequiredDefinition(linkCmdVar.c_str());
  336. cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
  337. return linkCmds;
  338. }
  339. default:
  340. assert(0 && "Unexpected target type");
  341. }
  342. return std::vector<std::string>();
  343. }
  344. void cmNinjaNormalTargetGenerator::WriteLinkStatement()
  345. {
  346. cmTarget::TargetType targetType = this->GetTarget()->GetType();
  347. std::string targetOutput = ConvertToNinjaPath(
  348. this->GetTarget()->GetFullPath(this->GetConfigName()).c_str());
  349. std::string targetOutputReal = ConvertToNinjaPath(
  350. this->GetTarget()->GetFullPath(this->GetConfigName(),
  351. /*implib=*/false,
  352. /*realpath=*/true).c_str());
  353. std::string targetOutputImplib = ConvertToNinjaPath(
  354. this->GetTarget()->GetFullPath(this->GetConfigName(),
  355. /*implib=*/true).c_str());
  356. if (this->GetTarget()->IsAppBundleOnApple())
  357. {
  358. // Create the app bundle
  359. std::string outpath;
  360. this->CreateAppBundle(this->TargetNameOut, outpath);
  361. // Calculate the output path
  362. targetOutput = outpath + this->TargetNameOut;
  363. targetOutput = this->ConvertToNinjaPath(targetOutput.c_str());
  364. targetOutputReal = outpath + this->TargetNameReal;
  365. targetOutputReal = this->ConvertToNinjaPath(targetOutputReal.c_str());
  366. }
  367. else if (this->GetTarget()->IsFrameworkOnApple())
  368. {
  369. // Create the library framework.
  370. this->CreateFramework(this->TargetNameOut);
  371. }
  372. // Write comments.
  373. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  374. this->GetBuildFileStream()
  375. << "# Link build statements for "
  376. << cmTarget::GetTargetTypeName(targetType)
  377. << " target "
  378. << this->GetTargetName()
  379. << "\n\n";
  380. cmNinjaDeps emptyDeps;
  381. cmNinjaVars vars;
  382. // Compute the comment.
  383. cmOStringStream comment;
  384. comment << "Link the " << this->GetVisibleTypeName() << " "
  385. << targetOutputReal;
  386. // Compute outputs.
  387. cmNinjaDeps outputs;
  388. outputs.push_back(targetOutputReal);
  389. // Compute specific libraries to link with.
  390. cmNinjaDeps explicitDeps = this->GetObjects();
  391. cmNinjaDeps implicitDeps = this->ComputeLinkDeps();
  392. this->GetLocalGenerator()->GetTargetFlags(vars["LINK_LIBRARIES"],
  393. vars["FLAGS"],
  394. vars["LINK_FLAGS"],
  395. *this->GetTarget());
  396. this->AddModuleDefinitionFlag(vars["LINK_FLAGS"]);
  397. // Compute architecture specific link flags. Yes, these go into a different
  398. // variable for executables, probably due to a mistake made when duplicating
  399. // code between the Makefile executable and library generators.
  400. std::string flags = (targetType == cmTarget::EXECUTABLE
  401. ? vars["FLAGS"]
  402. : vars["ARCH_FLAGS"]);
  403. this->GetLocalGenerator()->AddArchitectureFlags(flags,
  404. this->GetTarget(),
  405. this->TargetLinkLanguage,
  406. this->GetConfigName());
  407. if (targetType == cmTarget::EXECUTABLE) {
  408. vars["FLAGS"] = flags;
  409. } else {
  410. vars["ARCH_FLAGS"] = flags;
  411. }
  412. if (this->GetTarget()->HasSOName(this->GetConfigName())) {
  413. vars["SONAME_FLAG"] =
  414. this->GetMakefile()->GetSONameFlag(this->TargetLinkLanguage);
  415. vars["SONAME"] = this->TargetNameSO;
  416. if (targetType == cmTarget::SHARED_LIBRARY) {
  417. std::string install_name_dir = this->GetTarget()
  418. ->GetInstallNameDirForBuildTree(this->GetConfigName());
  419. if (!install_name_dir.empty()) {
  420. vars["INSTALLNAME_DIR"] =
  421. this->GetLocalGenerator()->Convert(install_name_dir.c_str(),
  422. cmLocalGenerator::NONE,
  423. cmLocalGenerator::SHELL, false);
  424. }
  425. }
  426. }
  427. std::string path;
  428. if (!this->TargetNameImport.empty()) {
  429. path = this->GetLocalGenerator()->ConvertToOutputFormat(
  430. targetOutputImplib.c_str(), cmLocalGenerator::SHELL);
  431. vars["TARGET_IMPLIB"] = path;
  432. EnsureParentDirectoryExists(path);
  433. }
  434. // TODO move to GetTargetPDB
  435. cmMakefile* mf = this->GetMakefile();
  436. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  437. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID"))
  438. {
  439. path = this->GetTargetPDB();
  440. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  441. ConvertToNinjaPath(path.c_str()).c_str(),
  442. cmLocalGenerator::SHELL);
  443. EnsureParentDirectoryExists(path);
  444. }
  445. if (mf->IsOn("CMAKE_COMPILER_IS_MINGW"))
  446. {
  447. path = GetTarget()->GetSupportDirectory();
  448. vars["OBJECT_DIR"] = ConvertToNinjaPath(path.c_str());
  449. EnsureDirectoryExists(path);
  450. // ar.exe can't handle backslashes in rsp files (implictly used by gcc)
  451. std::string& linkLibraries = vars["LINK_LIBRARIES"];
  452. std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/');
  453. }
  454. std::vector<cmCustomCommand> *cmdLists[3] = {
  455. &this->GetTarget()->GetPreBuildCommands(),
  456. &this->GetTarget()->GetPreLinkCommands(),
  457. &this->GetTarget()->GetPostBuildCommands()
  458. };
  459. std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
  460. std::vector<std::string> *cmdLineLists[3] = {
  461. &preLinkCmdLines,
  462. &preLinkCmdLines,
  463. &postBuildCmdLines
  464. };
  465. for (unsigned i = 0; i != 3; ++i) {
  466. for (std::vector<cmCustomCommand>::const_iterator
  467. ci = cmdLists[i]->begin();
  468. ci != cmdLists[i]->end(); ++ci) {
  469. this->GetLocalGenerator()->AppendCustomCommandLines(&*ci,
  470. *cmdLineLists[i]);
  471. }
  472. }
  473. // If we have any PRE_LINK commands, we need to go back to HOME_OUTPUT for
  474. // the link commands.
  475. if (!preLinkCmdLines.empty()) {
  476. path = this->GetLocalGenerator()->ConvertToOutputFormat(
  477. this->GetMakefile()->GetHomeOutputDirectory(),
  478. cmLocalGenerator::SHELL);
  479. preLinkCmdLines.push_back("cd " + path);
  480. }
  481. vars["PRE_LINK"] =
  482. this->GetLocalGenerator()->BuildCommandLine(preLinkCmdLines);
  483. std::string postBuildCmdLine =
  484. this->GetLocalGenerator()->BuildCommandLine(postBuildCmdLines);
  485. cmNinjaVars symlinkVars;
  486. if (targetOutput == targetOutputReal) {
  487. vars["POST_BUILD"] = postBuildCmdLine;
  488. } else {
  489. vars["POST_BUILD"] = ":";
  490. symlinkVars["POST_BUILD"] = postBuildCmdLine;
  491. }
  492. int linkRuleLength = this->GetGlobalGenerator()->
  493. GetRuleCmdLength(this->LanguageLinkerRule());
  494. #ifdef _WIN32
  495. int commandLineLengthLimit = 8000 - linkRuleLength;
  496. #elif defined(__linux) || defined(__APPLE__)
  497. // for instance ARG_MAX is 2096152 on Ubuntu or 262144 on Mac
  498. int commandLineLengthLimit = sysconf(_SC_ARG_MAX) - linkRuleLength - 1000;
  499. #else
  500. int commandLineLengthLimit = -1;
  501. #endif
  502. // Write the build statement for this target.
  503. cmGlobalNinjaGenerator::WriteBuild(this->GetBuildFileStream(),
  504. comment.str(),
  505. this->LanguageLinkerRule(),
  506. outputs,
  507. explicitDeps,
  508. implicitDeps,
  509. emptyDeps,
  510. vars,
  511. commandLineLengthLimit);
  512. if (targetOutput != targetOutputReal) {
  513. if (targetType == cmTarget::EXECUTABLE) {
  514. cmGlobalNinjaGenerator::WriteBuild(this->GetBuildFileStream(),
  515. "Create executable symlink " + targetOutput,
  516. "CMAKE_SYMLINK_EXECUTABLE",
  517. cmNinjaDeps(1, targetOutput),
  518. cmNinjaDeps(1, targetOutputReal),
  519. emptyDeps,
  520. emptyDeps,
  521. symlinkVars);
  522. } else {
  523. cmNinjaDeps symlinks;
  524. const std::string soName = this->GetTargetFilePath(this->TargetNameSO);
  525. // If one link has to be created.
  526. if (targetOutputReal == soName || targetOutput == soName) {
  527. symlinkVars["SONAME"] = soName;
  528. } else {
  529. symlinkVars["SONAME"] = "";
  530. symlinks.push_back(soName);
  531. }
  532. symlinks.push_back(targetOutput);
  533. cmGlobalNinjaGenerator::WriteBuild(this->GetBuildFileStream(),
  534. "Create library symlink " + targetOutput,
  535. "CMAKE_SYMLINK_LIBRARY",
  536. symlinks,
  537. cmNinjaDeps(1, targetOutputReal),
  538. emptyDeps,
  539. emptyDeps,
  540. symlinkVars);
  541. }
  542. }
  543. if (!this->TargetNameImport.empty()) {
  544. // Since using multiple outputs would mess up the $out variable, use an
  545. // alias for the import library.
  546. cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
  547. "Alias for import library.",
  548. cmNinjaDeps(1, targetOutputImplib),
  549. cmNinjaDeps(1, targetOutputReal));
  550. }
  551. // Add aliases for the file name and the target name.
  552. this->GetGlobalGenerator()->AddTargetAlias(this->TargetNameOut,
  553. this->GetTarget());
  554. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  555. this->GetTarget());
  556. }
  557. //----------------------------------------------------------------------------
  558. void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()
  559. {
  560. // Write a phony output that depends on all object files.
  561. cmNinjaDeps outputs;
  562. this->GetLocalGenerator()->AppendTargetOutputs(this->GetTarget(), outputs);
  563. cmNinjaDeps depends = this->GetObjects();
  564. cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
  565. "Object library "
  566. + this->GetTargetName(),
  567. outputs,
  568. depends);
  569. // Add aliases for the target name.
  570. this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
  571. this->GetTarget());
  572. }
  573. // TODO: Factor with cmMakefileExecutableTargetGenerator::CreateAppBundle().
  574. void
  575. cmNinjaNormalTargetGenerator::CreateAppBundle(const std::string& targetName,
  576. std::string& outpath)
  577. {
  578. // Compute bundle directory names.
  579. outpath = this->MacContentDirectory;
  580. outpath += "MacOS";
  581. cmSystemTools::MakeDirectory(outpath.c_str());
  582. this->GetMakefile()->AddCMakeOutputFile(outpath.c_str());
  583. outpath += "/";
  584. // Configure the Info.plist file. Note that it needs the executable name
  585. // to be set.
  586. std::string plist = this->MacContentDirectory + "Info.plist";
  587. this->GetLocalGenerator()->GenerateAppleInfoPList(this->GetTarget(),
  588. targetName.c_str(),
  589. plist.c_str());
  590. this->GetMakefile()->AddCMakeOutputFile(plist.c_str());
  591. }
  592. // TODO: Factor with cmMakefileLibraryTargetGenerator::CreateFramework().
  593. void
  594. cmNinjaNormalTargetGenerator::CreateFramework(std::string const& targetName)
  595. {
  596. // Create the Resources directory.
  597. std::string resources = this->MacContentDirectory + "Resources/";
  598. cmSystemTools::MakeDirectory(resources.c_str());
  599. // Configure the Info.plist file into the Resources directory.
  600. std::set<cmStdString> macContentFolders;
  601. macContentFolders.insert("Resources");
  602. std::string plist = resources + "Info.plist";
  603. this->GetLocalGenerator()->GenerateFrameworkInfoPList(this->GetTarget(),
  604. targetName.c_str(),
  605. plist.c_str());
  606. // TODO: Use the cmMakefileTargetGenerator::ExtraFiles vector to
  607. // drive rules to create these files at build time.
  608. std::string oldName;
  609. std::string newName;
  610. // Compute the location of the top-level foo.framework directory.
  611. std::string top = this->GetTarget()->GetDirectory(this->GetConfigName());
  612. top += "/";
  613. top += this->TargetNameOut;
  614. top += ".framework/";
  615. // Make foo.framework/Versions
  616. std::string versions = top;
  617. versions += "Versions";
  618. cmSystemTools::MakeDirectory(versions.c_str());
  619. // Make foo.framework/Versions/version
  620. std::string version = versions;
  621. version += "/";
  622. version += this->FrameworkVersion;
  623. cmSystemTools::MakeDirectory(version.c_str());
  624. // Current -> version
  625. oldName = this->FrameworkVersion;
  626. newName = versions;
  627. newName += "/Current";
  628. cmSystemTools::RemoveFile(newName.c_str());
  629. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  630. this->GetMakefile()->AddCMakeOutputFile(newName.c_str());
  631. // foo -> Versions/Current/foo
  632. oldName = "Versions/Current/";
  633. oldName += this->TargetNameOut;
  634. newName = top;
  635. newName += this->TargetNameOut;
  636. cmSystemTools::RemoveFile(newName.c_str());
  637. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  638. this->GetMakefile()->AddCMakeOutputFile(newName.c_str());
  639. // Resources -> Versions/Current/Resources
  640. if(macContentFolders.find("Resources") != macContentFolders.end())
  641. {
  642. oldName = "Versions/Current/Resources";
  643. newName = top;
  644. newName += "Resources";
  645. cmSystemTools::RemoveFile(newName.c_str());
  646. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  647. this->GetMakefile()->AddCMakeOutputFile(newName.c_str());
  648. }
  649. // Headers -> Versions/Current/Headers
  650. if(macContentFolders.find("Headers") != macContentFolders.end())
  651. {
  652. oldName = "Versions/Current/Headers";
  653. newName = top;
  654. newName += "Headers";
  655. cmSystemTools::RemoveFile(newName.c_str());
  656. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  657. this->GetMakefile()->AddCMakeOutputFile(newName.c_str());
  658. }
  659. // PrivateHeaders -> Versions/Current/PrivateHeaders
  660. if(macContentFolders.find("PrivateHeaders") != macContentFolders.end())
  661. {
  662. oldName = "Versions/Current/PrivateHeaders";
  663. newName = top;
  664. newName += "PrivateHeaders";
  665. cmSystemTools::RemoveFile(newName.c_str());
  666. cmSystemTools::CreateSymlink(oldName.c_str(), newName.c_str());
  667. this->GetMakefile()->AddCMakeOutputFile(newName.c_str());
  668. }
  669. }