cmNinjaTargetGenerator.cxx 27 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 "cmNinjaTargetGenerator.h"
  12. #include "cmGlobalNinjaGenerator.h"
  13. #include "cmLocalNinjaGenerator.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGeneratorTarget.h"
  16. #include "cmNinjaNormalTargetGenerator.h"
  17. #include "cmNinjaUtilityTargetGenerator.h"
  18. #include "cmSystemTools.h"
  19. #include "cmMakefile.h"
  20. #include "cmComputeLinkInformation.h"
  21. #include "cmSourceFile.h"
  22. #include "cmCustomCommandGenerator.h"
  23. #include <algorithm>
  24. cmNinjaTargetGenerator *
  25. cmNinjaTargetGenerator::New(cmGeneratorTarget* target)
  26. {
  27. switch (target->GetType())
  28. {
  29. case cmTarget::EXECUTABLE:
  30. case cmTarget::SHARED_LIBRARY:
  31. case cmTarget::STATIC_LIBRARY:
  32. case cmTarget::MODULE_LIBRARY:
  33. case cmTarget::OBJECT_LIBRARY:
  34. return new cmNinjaNormalTargetGenerator(target);
  35. case cmTarget::UTILITY:
  36. return new cmNinjaUtilityTargetGenerator(target);;
  37. case cmTarget::GLOBAL_TARGET: {
  38. // We only want to process global targets that live in the home
  39. // (i.e. top-level) directory. CMake creates copies of these targets
  40. // in every directory, which we don't need.
  41. cmMakefile *mf = target->Target->GetMakefile();
  42. if (strcmp(mf->GetStartDirectory(), mf->GetHomeDirectory()) == 0)
  43. return new cmNinjaUtilityTargetGenerator(target);
  44. // else fallthrough
  45. }
  46. default:
  47. return 0;
  48. }
  49. }
  50. cmNinjaTargetGenerator::cmNinjaTargetGenerator(cmTarget* target)
  51. :
  52. MacOSXContentGenerator(0),
  53. OSXBundleGenerator(0),
  54. MacContentFolders(),
  55. Target(target),
  56. Makefile(target->GetMakefile()),
  57. LocalGenerator(
  58. static_cast<cmLocalNinjaGenerator*>(Makefile->GetLocalGenerator())),
  59. Objects()
  60. {
  61. this->GeneratorTarget =
  62. this->GetGlobalGenerator()->GetGeneratorTarget(target);
  63. MacOSXContentGenerator = new MacOSXContentGeneratorType(this);
  64. }
  65. cmNinjaTargetGenerator::~cmNinjaTargetGenerator()
  66. {
  67. delete this->MacOSXContentGenerator;
  68. }
  69. cmGeneratedFileStream& cmNinjaTargetGenerator::GetBuildFileStream() const
  70. {
  71. return *this->GetGlobalGenerator()->GetBuildFileStream();
  72. }
  73. cmGeneratedFileStream& cmNinjaTargetGenerator::GetRulesFileStream() const
  74. {
  75. return *this->GetGlobalGenerator()->GetRulesFileStream();
  76. }
  77. cmGlobalNinjaGenerator* cmNinjaTargetGenerator::GetGlobalGenerator() const
  78. {
  79. return this->LocalGenerator->GetGlobalNinjaGenerator();
  80. }
  81. std::string const& cmNinjaTargetGenerator::GetConfigName() const
  82. {
  83. return this->LocalGenerator->GetConfigName();
  84. }
  85. // TODO: Picked up from cmMakefileTargetGenerator. Refactor it.
  86. const char* cmNinjaTargetGenerator::GetFeature(const std::string& feature)
  87. {
  88. return this->Target->GetFeature(feature, this->GetConfigName());
  89. }
  90. // TODO: Picked up from cmMakefileTargetGenerator. Refactor it.
  91. bool cmNinjaTargetGenerator::GetFeatureAsBool(const std::string& feature)
  92. {
  93. return this->Target->GetFeatureAsBool(feature, this->GetConfigName());
  94. }
  95. // TODO: Picked up from cmMakefileTargetGenerator. Refactor it.
  96. void cmNinjaTargetGenerator::AddFeatureFlags(std::string& flags,
  97. const std::string& lang)
  98. {
  99. // Add language-specific flags.
  100. this->LocalGenerator->AddLanguageFlags(flags, lang, this->GetConfigName());
  101. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION"))
  102. {
  103. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  104. }
  105. }
  106. std::string
  107. cmNinjaTargetGenerator::OrderDependsTargetForTarget()
  108. {
  109. return "cmake_order_depends_target_" + this->GetTargetName();
  110. }
  111. // TODO: Most of the code is picked up from
  112. // void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink),
  113. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  114. // Refactor it.
  115. std::string
  116. cmNinjaTargetGenerator::ComputeFlagsForObject(cmSourceFile const* source,
  117. const std::string& language)
  118. {
  119. // TODO: Fortran support.
  120. // // Fortran-specific flags computed for this target.
  121. // if(*l == "Fortran")
  122. // {
  123. // this->AddFortranFlags(flags);
  124. // }
  125. bool hasLangCached = this->LanguageFlags.count(language) != 0;
  126. std::string& languageFlags = this->LanguageFlags[language];
  127. if(!hasLangCached)
  128. {
  129. this->AddFeatureFlags(languageFlags, language);
  130. this->GetLocalGenerator()->AddArchitectureFlags(languageFlags,
  131. this->GeneratorTarget,
  132. language,
  133. this->GetConfigName());
  134. // Add shared-library flags if needed.
  135. this->LocalGenerator->AddCMP0018Flags(languageFlags, this->Target,
  136. language,
  137. this->GetConfigName());
  138. this->LocalGenerator->AddVisibilityPresetFlags(languageFlags, this->Target,
  139. language);
  140. std::vector<std::string> includes;
  141. this->LocalGenerator->GetIncludeDirectories(includes,
  142. this->GeneratorTarget,
  143. language,
  144. this->GetConfigName());
  145. // Add include directory flags.
  146. std::string includeFlags =
  147. this->LocalGenerator->GetIncludeFlags(includes, this->GeneratorTarget,
  148. language,
  149. language == "RC" ? true : false); // full include paths for RC
  150. // needed by cmcldeps
  151. if(cmGlobalNinjaGenerator::IsMinGW())
  152. cmSystemTools::ReplaceString(includeFlags, "\\", "/");
  153. this->LocalGenerator->AppendFlags(languageFlags, includeFlags);
  154. // Append old-style preprocessor definition flags.
  155. this->LocalGenerator->AppendFlags(languageFlags,
  156. this->Makefile->GetDefineFlags());
  157. // Add target-specific flags.
  158. this->LocalGenerator->AddCompileOptions(languageFlags, this->Target,
  159. language,
  160. this->GetConfigName());
  161. }
  162. std::string flags = languageFlags;
  163. // Add source file specific flags.
  164. this->LocalGenerator->AppendFlags(flags,
  165. source->GetProperty("COMPILE_FLAGS"));
  166. // TODO: Handle Apple frameworks.
  167. return flags;
  168. }
  169. bool cmNinjaTargetGenerator::needsDepFile(const std::string& lang)
  170. {
  171. cmMakefile* mf = this->GetMakefile();
  172. const bool usingMSVC = std::string("MSVC") ==
  173. (mf->GetDefinition("CMAKE_C_COMPILER_ID") ?
  174. mf->GetSafeDefinition("CMAKE_C_COMPILER_ID") :
  175. mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID"));
  176. return !usingMSVC || lang == "RC";
  177. }
  178. // TODO: Refactor with
  179. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  180. std::string
  181. cmNinjaTargetGenerator::
  182. ComputeDefines(cmSourceFile const* source, const std::string& language)
  183. {
  184. std::set<std::string> defines;
  185. // Add the export symbol definition for shared library objects.
  186. if(const char* exportMacro = this->Target->GetExportMacro())
  187. {
  188. this->LocalGenerator->AppendDefines(defines, exportMacro);
  189. }
  190. // Add preprocessor definitions for this target and configuration.
  191. this->LocalGenerator->AddCompileDefinitions(defines, this->Target,
  192. this->GetConfigName());
  193. this->LocalGenerator->AppendDefines
  194. (defines,
  195. source->GetProperty("COMPILE_DEFINITIONS"));
  196. {
  197. std::string defPropName = "COMPILE_DEFINITIONS_";
  198. defPropName += cmSystemTools::UpperCase(this->GetConfigName());
  199. this->LocalGenerator->AppendDefines
  200. (defines,
  201. source->GetProperty(defPropName));
  202. }
  203. std::string definesString;
  204. this->LocalGenerator->JoinDefines(defines, definesString,
  205. language);
  206. return definesString;
  207. }
  208. cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps() const
  209. {
  210. // Static libraries never depend on other targets for linking.
  211. if (this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  212. this->Target->GetType() == cmTarget::OBJECT_LIBRARY)
  213. return cmNinjaDeps();
  214. cmComputeLinkInformation* cli =
  215. this->Target->GetLinkInformation(this->GetConfigName());
  216. if(!cli)
  217. return cmNinjaDeps();
  218. const std::vector<std::string> &deps = cli->GetDepends();
  219. cmNinjaDeps result(deps.size());
  220. std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
  221. // Add a dependency on the link definitions file, if any.
  222. if(!this->ModuleDefinitionFile.empty())
  223. {
  224. result.push_back(this->ModuleDefinitionFile);
  225. }
  226. return result;
  227. }
  228. std::string
  229. cmNinjaTargetGenerator
  230. ::GetSourceFilePath(cmSourceFile const* source) const
  231. {
  232. return ConvertToNinjaPath(source->GetFullPath());
  233. }
  234. std::string
  235. cmNinjaTargetGenerator
  236. ::GetObjectFilePath(cmSourceFile const* source) const
  237. {
  238. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  239. if(!path.empty())
  240. path += "/";
  241. std::string const& objectName = this->GeneratorTarget
  242. ->GetObjectName(source);
  243. path += this->LocalGenerator->GetTargetDirectory(*this->Target);
  244. path += "/";
  245. path += objectName;
  246. return path;
  247. }
  248. std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
  249. {
  250. std::string dir = this->Target->GetDirectory(this->GetConfigName());
  251. return ConvertToNinjaPath(dir);
  252. }
  253. std::string
  254. cmNinjaTargetGenerator
  255. ::GetTargetFilePath(const std::string& name) const
  256. {
  257. std::string path = this->GetTargetOutputDir();
  258. if (path.empty() || path == ".")
  259. return name;
  260. path += "/";
  261. path += name;
  262. return path;
  263. }
  264. std::string cmNinjaTargetGenerator::GetTargetName() const
  265. {
  266. return this->Target->GetName();
  267. }
  268. bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(cmNinjaVars& vars) const
  269. {
  270. cmMakefile* mf = this->GetMakefile();
  271. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  272. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID"))
  273. {
  274. std::string pdbPath;
  275. std::string compilePdbPath;
  276. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  277. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  278. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  279. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  280. {
  281. pdbPath = this->Target->GetPDBDirectory(this->GetConfigName());
  282. pdbPath += "/";
  283. pdbPath += this->Target->GetPDBName(this->GetConfigName());
  284. }
  285. if(this->Target->GetType() <= cmTarget::OBJECT_LIBRARY)
  286. {
  287. compilePdbPath = this->Target->GetCompilePDBPath(this->GetConfigName());
  288. if(compilePdbPath.empty())
  289. {
  290. compilePdbPath = this->Target->GetSupportDirectory() + "/";
  291. }
  292. }
  293. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  294. ConvertToNinjaPath(pdbPath),
  295. cmLocalGenerator::SHELL);
  296. vars["TARGET_COMPILE_PDB"] =
  297. this->GetLocalGenerator()->ConvertToOutputFormat(
  298. ConvertToNinjaPath(compilePdbPath),
  299. cmLocalGenerator::SHELL);
  300. EnsureParentDirectoryExists(pdbPath);
  301. EnsureParentDirectoryExists(compilePdbPath);
  302. return true;
  303. }
  304. return false;
  305. }
  306. void
  307. cmNinjaTargetGenerator
  308. ::WriteLanguageRules(const std::string& language)
  309. {
  310. #ifdef NINJA_GEN_VERBOSE_FILES
  311. this->GetRulesFileStream()
  312. << "# Rules for language " << language << "\n\n";
  313. #endif
  314. this->WriteCompileRule(language);
  315. }
  316. void
  317. cmNinjaTargetGenerator
  318. ::WriteCompileRule(const std::string& lang)
  319. {
  320. cmLocalGenerator::RuleVariables vars;
  321. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  322. vars.CMTarget = this->GetTarget();
  323. vars.Language = lang.c_str();
  324. vars.Source = "$in";
  325. vars.Object = "$out";
  326. vars.Defines = "$DEFINES";
  327. vars.TargetPDB = "$TARGET_PDB";
  328. vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
  329. vars.ObjectDir = "$OBJECT_DIR";
  330. vars.ObjectFileDir = "$OBJECT_FILE_DIR";
  331. cmMakefile* mf = this->GetMakefile();
  332. const std::string cId = mf->GetDefinition("CMAKE_C_COMPILER_ID")
  333. ? mf->GetSafeDefinition("CMAKE_C_COMPILER_ID")
  334. : mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID");
  335. const std::string sId = mf->GetDefinition("CMAKE_C_SIMULATE_ID")
  336. ? mf->GetSafeDefinition("CMAKE_C_SIMULATE_ID")
  337. : mf->GetSafeDefinition("CMAKE_CXX_SIMULATE_ID");
  338. const bool usingMSVC = (cId == "MSVC" || sId == "MSVC");
  339. // Tell ninja dependency format so all deps can be loaded into a database
  340. std::string deptype;
  341. std::string depfile;
  342. std::string cldeps;
  343. std::string flags = "$FLAGS";
  344. if (usingMSVC)
  345. {
  346. if (!mf->GetIsSourceFileTryCompile() && lang == "RC")
  347. {
  348. deptype = "gcc";
  349. depfile = "$DEP_FILE";
  350. const std::string cl = mf->GetDefinition("CMAKE_C_COMPILER") ?
  351. mf->GetSafeDefinition("CMAKE_C_COMPILER") :
  352. mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  353. cldeps = "\"";
  354. cldeps += mf->GetSafeDefinition("CMAKE_CMCLDEPS_EXECUTABLE");
  355. cldeps += "\" " + lang + " $in \"$DEP_FILE\" $out \"";
  356. cldeps += mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  357. cldeps += "\" \"" + cl + "\" ";
  358. }
  359. else
  360. {
  361. deptype = "msvc";
  362. depfile = "";
  363. flags += " /showIncludes";
  364. }
  365. }
  366. else
  367. {
  368. deptype = "gcc";
  369. const char* langdeptype = mf->GetDefinition("CMAKE_NINJA_DEPTYPE_" + lang);
  370. if (langdeptype)
  371. {
  372. deptype = langdeptype;
  373. }
  374. depfile = "$DEP_FILE";
  375. const std::string flagsName = "CMAKE_DEPFILE_FLAGS_" + lang;
  376. std::string depfileFlags = mf->GetSafeDefinition(flagsName);
  377. if (!depfileFlags.empty())
  378. {
  379. cmSystemTools::ReplaceString(depfileFlags, "<DEPFILE>", "$DEP_FILE");
  380. cmSystemTools::ReplaceString(depfileFlags, "<OBJECT>", "$out");
  381. cmSystemTools::ReplaceString(depfileFlags, "<CMAKE_C_COMPILER>",
  382. mf->GetDefinition("CMAKE_C_COMPILER"));
  383. flags += " " + depfileFlags;
  384. }
  385. }
  386. vars.Flags = flags.c_str();
  387. vars.DependencyFile = depfile.c_str();
  388. // Rule for compiling object file.
  389. const std::string cmdVar = std::string("CMAKE_") + lang + "_COMPILE_OBJECT";
  390. std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
  391. std::vector<std::string> compileCmds;
  392. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  393. compileCmds.front().insert(0, cldeps);
  394. for (std::vector<std::string>::iterator i = compileCmds.begin();
  395. i != compileCmds.end(); ++i)
  396. this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
  397. std::string cmdLine =
  398. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  399. // Write the rule for compiling file of the given language.
  400. cmOStringStream comment;
  401. comment << "Rule for compiling " << lang << " files.";
  402. cmOStringStream description;
  403. description << "Building " << lang << " object $out";
  404. this->GetGlobalGenerator()->AddRule(this->LanguageCompilerRule(lang),
  405. cmdLine,
  406. description.str(),
  407. comment.str(),
  408. depfile,
  409. deptype,
  410. /*rspfile*/ "",
  411. /*rspcontent*/ "",
  412. /*restat*/ false,
  413. /*generator*/ false);
  414. }
  415. void
  416. cmNinjaTargetGenerator
  417. ::WriteObjectBuildStatements()
  418. {
  419. // Write comments.
  420. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  421. this->GetBuildFileStream()
  422. << "# Object build statements for "
  423. << cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  424. << " target "
  425. << this->GetTargetName()
  426. << "\n\n";
  427. std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  428. std::vector<cmSourceFile const*> customCommands;
  429. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  430. for(std::vector<cmSourceFile const*>::const_iterator
  431. si = customCommands.begin();
  432. si != customCommands.end(); ++si)
  433. {
  434. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  435. this->GetLocalGenerator()->AddCustomCommandTarget(cc, this->GetTarget());
  436. // Record the custom commands for this target. The container is used
  437. // in WriteObjectBuildStatement when called in a loop below.
  438. this->CustomCommands.push_back(cc);
  439. }
  440. std::vector<cmSourceFile const*> headerSources;
  441. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  442. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  443. headerSources,
  444. this->MacOSXContentGenerator);
  445. std::vector<cmSourceFile const*> extraSources;
  446. this->GeneratorTarget->GetExtraSources(extraSources, config);
  447. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  448. extraSources,
  449. this->MacOSXContentGenerator);
  450. std::vector<cmSourceFile const*> externalObjects;
  451. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  452. for(std::vector<cmSourceFile const*>::const_iterator
  453. si = externalObjects.begin();
  454. si != externalObjects.end(); ++si)
  455. {
  456. this->Objects.push_back(this->GetSourceFilePath(*si));
  457. }
  458. cmNinjaDeps orderOnlyDeps;
  459. this->GetLocalGenerator()->AppendTargetDepends(this->Target, orderOnlyDeps);
  460. // Add order-only dependencies on custom command outputs.
  461. for(std::vector<cmCustomCommand const*>::const_iterator
  462. cci = this->CustomCommands.begin();
  463. cci != this->CustomCommands.end(); ++cci)
  464. {
  465. cmCustomCommand const* cc = *cci;
  466. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
  467. this->GetMakefile());
  468. const std::vector<std::string>& ccoutputs = ccg.GetOutputs();
  469. std::transform(ccoutputs.begin(), ccoutputs.end(),
  470. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  471. }
  472. if (!orderOnlyDeps.empty())
  473. {
  474. cmNinjaDeps orderOnlyTarget;
  475. orderOnlyTarget.push_back(this->OrderDependsTargetForTarget());
  476. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  477. "Order-only phony target for "
  478. + this->GetTargetName(),
  479. orderOnlyTarget,
  480. cmNinjaDeps(),
  481. cmNinjaDeps(),
  482. orderOnlyDeps);
  483. }
  484. std::vector<cmSourceFile const*> objectSources;
  485. this->GeneratorTarget->GetObjectSources(objectSources, config);
  486. for(std::vector<cmSourceFile const*>::const_iterator
  487. si = objectSources.begin(); si != objectSources.end(); ++si)
  488. {
  489. this->WriteObjectBuildStatement(*si, !orderOnlyDeps.empty());
  490. }
  491. std::string def = this->GeneratorTarget->GetModuleDefinitionFile(config);
  492. if(!def.empty())
  493. {
  494. this->ModuleDefinitionFile = this->ConvertToNinjaPath(def);
  495. }
  496. this->GetBuildFileStream() << "\n";
  497. }
  498. void
  499. cmNinjaTargetGenerator
  500. ::WriteObjectBuildStatement(
  501. cmSourceFile const* source, bool writeOrderDependsTargetForTarget)
  502. {
  503. std::string comment;
  504. const std::string language = source->GetLanguage();
  505. std::string rule = this->LanguageCompilerRule(language);
  506. cmNinjaDeps outputs;
  507. std::string objectFileName = this->GetObjectFilePath(source);
  508. outputs.push_back(objectFileName);
  509. // Add this object to the list of object files.
  510. this->Objects.push_back(objectFileName);
  511. cmNinjaDeps explicitDeps;
  512. std::string sourceFileName;
  513. if (language == "RC")
  514. sourceFileName = source->GetFullPath();
  515. else
  516. sourceFileName = this->GetSourceFilePath(source);
  517. explicitDeps.push_back(sourceFileName);
  518. cmNinjaDeps implicitDeps;
  519. if(const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  520. std::vector<std::string> depList;
  521. cmSystemTools::ExpandListArgument(objectDeps, depList);
  522. std::transform(depList.begin(), depList.end(),
  523. std::back_inserter(implicitDeps), MapToNinjaPath());
  524. }
  525. cmNinjaDeps orderOnlyDeps;
  526. if (writeOrderDependsTargetForTarget)
  527. {
  528. orderOnlyDeps.push_back(this->OrderDependsTargetForTarget());
  529. }
  530. // If the source file is GENERATED and does not have a custom command
  531. // (either attached to this source file or another one), assume that one of
  532. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  533. // will rebuild the file.
  534. if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
  535. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  536. this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
  537. orderOnlyDeps);
  538. }
  539. cmNinjaVars vars;
  540. vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  541. vars["DEFINES"] = this->ComputeDefines(source, language);
  542. if (needsDepFile(language)) {
  543. vars["DEP_FILE"] =
  544. cmGlobalNinjaGenerator::EncodeDepfileSpace(objectFileName + ".d");
  545. }
  546. EnsureParentDirectoryExists(objectFileName);
  547. std::string objectDir = this->Target->GetSupportDirectory();
  548. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  549. ConvertToNinjaPath(objectDir),
  550. cmLocalGenerator::SHELL);
  551. std::string objectFileDir = cmSystemTools::GetFilenamePath(objectFileName);
  552. vars["OBJECT_FILE_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  553. ConvertToNinjaPath(objectFileDir),
  554. cmLocalGenerator::SHELL);
  555. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetTarget(), vars);
  556. this->SetMsvcTargetPdbVariable(vars);
  557. if(this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS"))
  558. {
  559. cmLocalGenerator::RuleVariables compileObjectVars;
  560. std::string lang = language;
  561. compileObjectVars.Language = lang.c_str();
  562. std::string escapedSourceFileName = sourceFileName;
  563. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str()))
  564. {
  565. escapedSourceFileName = cmSystemTools::CollapseFullPath(
  566. escapedSourceFileName,
  567. this->GetGlobalGenerator()->GetCMakeInstance()->
  568. GetHomeOutputDirectory());
  569. }
  570. escapedSourceFileName =
  571. this->LocalGenerator->ConvertToOutputFormat(
  572. escapedSourceFileName, cmLocalGenerator::SHELL);
  573. compileObjectVars.Source = escapedSourceFileName.c_str();
  574. compileObjectVars.Object = objectFileName.c_str();
  575. compileObjectVars.ObjectDir = objectDir.c_str();
  576. compileObjectVars.ObjectFileDir = objectFileDir.c_str();
  577. compileObjectVars.Flags = vars["FLAGS"].c_str();
  578. compileObjectVars.Defines = vars["DEFINES"].c_str();
  579. // Rule for compiling object file.
  580. std::string compileCmdVar = "CMAKE_";
  581. compileCmdVar += language;
  582. compileCmdVar += "_COMPILE_OBJECT";
  583. std::string compileCmd =
  584. this->GetMakefile()->GetRequiredDefinition(compileCmdVar);
  585. std::vector<std::string> compileCmds;
  586. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  587. for (std::vector<std::string>::iterator i = compileCmds.begin();
  588. i != compileCmds.end(); ++i)
  589. this->GetLocalGenerator()->ExpandRuleVariables(*i, compileObjectVars);
  590. std::string cmdLine =
  591. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  592. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine,
  593. sourceFileName);
  594. }
  595. this->GetGlobalGenerator()->WriteBuild(this->GetBuildFileStream(),
  596. comment,
  597. rule,
  598. outputs,
  599. explicitDeps,
  600. implicitDeps,
  601. orderOnlyDeps,
  602. vars);
  603. if(const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  604. std::vector<std::string> outputList;
  605. cmSystemTools::ExpandListArgument(objectOutputs, outputList);
  606. std::transform(outputList.begin(), outputList.end(), outputList.begin(),
  607. MapToNinjaPath());
  608. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  609. "Additional output files.",
  610. outputList,
  611. outputs);
  612. }
  613. }
  614. //----------------------------------------------------------------------------
  615. void
  616. cmNinjaTargetGenerator
  617. ::AddModuleDefinitionFlag(std::string& flags)
  618. {
  619. if(this->ModuleDefinitionFile.empty())
  620. {
  621. return;
  622. }
  623. // TODO: Create a per-language flag variable.
  624. const char* defFileFlag =
  625. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  626. if(!defFileFlag)
  627. {
  628. return;
  629. }
  630. // Append the flag and value. Use ConvertToLinkReference to help
  631. // vs6's "cl -link" pass it to the linker.
  632. std::string flag = defFileFlag;
  633. flag += (this->LocalGenerator->ConvertToLinkReference(
  634. this->ModuleDefinitionFile));
  635. this->LocalGenerator->AppendFlags(flags, flag);
  636. }
  637. void
  638. cmNinjaTargetGenerator
  639. ::EnsureDirectoryExists(const std::string& path) const
  640. {
  641. if (cmSystemTools::FileIsFullPath(path.c_str()))
  642. {
  643. cmSystemTools::MakeDirectory(path.c_str());
  644. }
  645. else
  646. {
  647. const std::string fullPath = std::string(this->GetGlobalGenerator()->
  648. GetCMakeInstance()->GetHomeOutputDirectory())
  649. + "/" + path;
  650. cmSystemTools::MakeDirectory(fullPath.c_str());
  651. }
  652. }
  653. void
  654. cmNinjaTargetGenerator
  655. ::EnsureParentDirectoryExists(const std::string& path) const
  656. {
  657. EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path));
  658. }
  659. //----------------------------------------------------------------------------
  660. void
  661. cmNinjaTargetGenerator::MacOSXContentGeneratorType::operator()(
  662. cmSourceFile const& source, const char* pkgloc)
  663. {
  664. // Skip OS X content when not building a Framework or Bundle.
  665. if(!this->Generator->GetTarget()->IsBundleOnApple())
  666. {
  667. return;
  668. }
  669. std::string macdir =
  670. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  671. // Get the input file location.
  672. std::string input = source.GetFullPath();
  673. input =
  674. this->Generator->GetLocalGenerator()->ConvertToNinjaPath(input);
  675. // Get the output file location.
  676. std::string output = macdir;
  677. output += "/";
  678. output += cmSystemTools::GetFilenameName(input);
  679. output =
  680. this->Generator->GetLocalGenerator()->ConvertToNinjaPath(output);
  681. // Write a build statement to copy the content into the bundle.
  682. this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(input,
  683. output);
  684. // Add as a dependency of all target so that it gets called.
  685. this->Generator->GetGlobalGenerator()->AddDependencyToAll(output);
  686. }
  687. void cmNinjaTargetGenerator::addPoolNinjaVariable(
  688. const std::string& pool_property,
  689. cmTarget* target,
  690. cmNinjaVars& vars)
  691. {
  692. const char* pool = target->GetProperty(pool_property);
  693. if (pool)
  694. {
  695. vars["pool"] = pool;
  696. }
  697. }