cmNinjaTargetGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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 cmSystemTools::IsOn(this->GetFeature(feature));
  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().c_str());
  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.c_str());
  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.c_str()),
  295. cmLocalGenerator::SHELL);
  296. vars["TARGET_COMPILE_PDB"] =
  297. this->GetLocalGenerator()->ConvertToOutputFormat(
  298. ConvertToNinjaPath(compilePdbPath.c_str()),
  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. cmMakefile* mf = this->GetMakefile();
  331. const std::string cId = mf->GetDefinition("CMAKE_C_COMPILER_ID")
  332. ? mf->GetSafeDefinition("CMAKE_C_COMPILER_ID")
  333. : mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID");
  334. const bool usingMSVC = (cId == "MSVC" || cId == "Intel");
  335. // Tell ninja dependency format so all deps can be loaded into a database
  336. std::string deptype;
  337. std::string depfile;
  338. std::string cldeps;
  339. std::string flags = "$FLAGS";
  340. if (usingMSVC)
  341. {
  342. if (!mf->GetIsSourceFileTryCompile() && lang == "RC")
  343. {
  344. deptype = "gcc";
  345. depfile = "$DEP_FILE";
  346. const std::string cl = mf->GetDefinition("CMAKE_C_COMPILER") ?
  347. mf->GetSafeDefinition("CMAKE_C_COMPILER") :
  348. mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  349. cldeps = "\"";
  350. cldeps += mf->GetSafeDefinition("CMAKE_CMCLDEPS_EXECUTABLE");
  351. cldeps += "\" " + lang + " $in \"$DEP_FILE\" $out \"";
  352. cldeps += mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  353. cldeps += "\" \"" + cl + "\" ";
  354. }
  355. else
  356. {
  357. deptype = "msvc";
  358. depfile = "";
  359. flags += " /showIncludes";
  360. }
  361. }
  362. else
  363. {
  364. deptype = "gcc";
  365. const char* langdeptype = mf->GetDefinition("CMAKE_NINJA_DEPTYPE_" + lang);
  366. if (langdeptype)
  367. {
  368. deptype = langdeptype;
  369. }
  370. depfile = "$DEP_FILE";
  371. const std::string flagsName = "CMAKE_DEPFILE_FLAGS_" + lang;
  372. std::string depfileFlags = mf->GetSafeDefinition(flagsName);
  373. if (!depfileFlags.empty())
  374. {
  375. cmSystemTools::ReplaceString(depfileFlags, "<DEPFILE>", "$DEP_FILE");
  376. cmSystemTools::ReplaceString(depfileFlags, "<OBJECT>", "$out");
  377. cmSystemTools::ReplaceString(depfileFlags, "<CMAKE_C_COMPILER>",
  378. mf->GetDefinition("CMAKE_C_COMPILER"));
  379. flags += " " + depfileFlags;
  380. }
  381. }
  382. vars.Flags = flags.c_str();
  383. vars.DependencyFile = depfile.c_str();
  384. // Rule for compiling object file.
  385. const std::string cmdVar = std::string("CMAKE_") + lang + "_COMPILE_OBJECT";
  386. std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
  387. std::vector<std::string> compileCmds;
  388. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  389. compileCmds.front().insert(0, cldeps);
  390. for (std::vector<std::string>::iterator i = compileCmds.begin();
  391. i != compileCmds.end(); ++i)
  392. this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
  393. std::string cmdLine =
  394. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  395. // Write the rule for compiling file of the given language.
  396. cmOStringStream comment;
  397. comment << "Rule for compiling " << lang << " files.";
  398. cmOStringStream description;
  399. description << "Building " << lang << " object $out";
  400. this->GetGlobalGenerator()->AddRule(this->LanguageCompilerRule(lang),
  401. cmdLine,
  402. description.str(),
  403. comment.str(),
  404. depfile,
  405. deptype,
  406. /*rspfile*/ "",
  407. /*rspcontent*/ "",
  408. /*restat*/ false,
  409. /*generator*/ false);
  410. }
  411. void
  412. cmNinjaTargetGenerator
  413. ::WriteObjectBuildStatements()
  414. {
  415. // Write comments.
  416. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  417. this->GetBuildFileStream()
  418. << "# Object build statements for "
  419. << cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  420. << " target "
  421. << this->GetTargetName()
  422. << "\n\n";
  423. std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  424. std::vector<cmSourceFile const*> customCommands;
  425. this->GeneratorTarget->GetCustomCommands(customCommands, config);
  426. for(std::vector<cmSourceFile const*>::const_iterator
  427. si = customCommands.begin();
  428. si != customCommands.end(); ++si)
  429. {
  430. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  431. this->GetLocalGenerator()->AddCustomCommandTarget(cc, this->GetTarget());
  432. // Record the custom commands for this target. The container is used
  433. // in WriteObjectBuildStatement when called in a loop below.
  434. this->CustomCommands.push_back((*si)->GetCustomCommand());
  435. }
  436. std::vector<cmSourceFile const*> headerSources;
  437. this->GeneratorTarget->GetHeaderSources(headerSources, config);
  438. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  439. headerSources,
  440. this->MacOSXContentGenerator);
  441. std::vector<cmSourceFile const*> extraSources;
  442. this->GeneratorTarget->GetExtraSources(extraSources, config);
  443. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  444. extraSources,
  445. this->MacOSXContentGenerator);
  446. std::vector<cmSourceFile const*> externalObjects;
  447. this->GeneratorTarget->GetExternalObjects(externalObjects, config);
  448. for(std::vector<cmSourceFile const*>::const_iterator
  449. si = externalObjects.begin();
  450. si != externalObjects.end(); ++si)
  451. {
  452. this->Objects.push_back(this->GetSourceFilePath(*si));
  453. }
  454. cmNinjaDeps orderOnlyDeps;
  455. this->GetLocalGenerator()->AppendTargetDepends(this->Target, orderOnlyDeps);
  456. cmNinjaDeps orderOnlyTarget;
  457. orderOnlyTarget.push_back(this->OrderDependsTargetForTarget());
  458. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  459. "Order-only phony target for "
  460. + this->GetTargetName(),
  461. orderOnlyTarget,
  462. cmNinjaDeps(),
  463. cmNinjaDeps(),
  464. orderOnlyDeps);
  465. std::vector<cmSourceFile const*> objectSources;
  466. this->GeneratorTarget->GetObjectSources(objectSources, config);
  467. for(std::vector<cmSourceFile const*>::const_iterator
  468. si = objectSources.begin(); si != objectSources.end(); ++si)
  469. {
  470. this->WriteObjectBuildStatement(*si);
  471. }
  472. std::string def = this->GeneratorTarget->GetModuleDefinitionFile(config);
  473. if(!def.empty())
  474. {
  475. this->ModuleDefinitionFile = this->ConvertToNinjaPath(def.c_str());
  476. }
  477. this->GetBuildFileStream() << "\n";
  478. }
  479. void
  480. cmNinjaTargetGenerator
  481. ::WriteObjectBuildStatement(cmSourceFile const* source)
  482. {
  483. std::string comment;
  484. const std::string language = source->GetLanguage();
  485. std::string rule = this->LanguageCompilerRule(language);
  486. cmNinjaDeps outputs;
  487. std::string objectFileName = this->GetObjectFilePath(source);
  488. outputs.push_back(objectFileName);
  489. // Add this object to the list of object files.
  490. this->Objects.push_back(objectFileName);
  491. cmNinjaDeps explicitDeps;
  492. std::string sourceFileName;
  493. if (language == "RC")
  494. sourceFileName = source->GetFullPath();
  495. else
  496. sourceFileName = this->GetSourceFilePath(source);
  497. explicitDeps.push_back(sourceFileName);
  498. cmNinjaDeps implicitDeps;
  499. if(const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  500. std::vector<std::string> depList;
  501. cmSystemTools::ExpandListArgument(objectDeps, depList);
  502. std::transform(depList.begin(), depList.end(),
  503. std::back_inserter(implicitDeps), MapToNinjaPath());
  504. }
  505. cmNinjaDeps orderOnlyDeps;
  506. orderOnlyDeps.push_back(this->OrderDependsTargetForTarget());
  507. // Add order-only dependencies on custom command outputs.
  508. for(std::vector<cmCustomCommand const*>::const_iterator
  509. cci = this->CustomCommands.begin();
  510. cci != this->CustomCommands.end(); ++cci)
  511. {
  512. cmCustomCommand const* cc = *cci;
  513. cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
  514. this->GetMakefile());
  515. const std::vector<std::string>& ccoutputs = ccg.GetOutputs();
  516. std::transform(ccoutputs.begin(), ccoutputs.end(),
  517. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  518. }
  519. // If the source file is GENERATED and does not have a custom command
  520. // (either attached to this source file or another one), assume that one of
  521. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  522. // will rebuild the file.
  523. if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
  524. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  525. this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
  526. orderOnlyDeps);
  527. }
  528. cmNinjaVars vars;
  529. vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  530. vars["DEFINES"] = this->ComputeDefines(source, language);
  531. if (needsDepFile(language)) {
  532. vars["DEP_FILE"] =
  533. cmGlobalNinjaGenerator::EncodeDepfileSpace(objectFileName + ".d");
  534. }
  535. EnsureParentDirectoryExists(objectFileName);
  536. std::string objectDir = this->Target->GetSupportDirectory();
  537. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  538. ConvertToNinjaPath(objectDir.c_str()),
  539. cmLocalGenerator::SHELL);
  540. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetTarget(), vars);
  541. this->SetMsvcTargetPdbVariable(vars);
  542. if(this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS"))
  543. {
  544. cmLocalGenerator::RuleVariables compileObjectVars;
  545. std::string lang = language;
  546. compileObjectVars.Language = lang.c_str();
  547. std::string escapedSourceFileName = sourceFileName;
  548. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str()))
  549. {
  550. escapedSourceFileName = cmSystemTools::CollapseFullPath(
  551. escapedSourceFileName.c_str(),
  552. this->GetGlobalGenerator()->GetCMakeInstance()->
  553. GetHomeOutputDirectory());
  554. }
  555. escapedSourceFileName =
  556. this->LocalGenerator->ConvertToOutputFormat(
  557. escapedSourceFileName, cmLocalGenerator::SHELL);
  558. compileObjectVars.Source = escapedSourceFileName.c_str();
  559. compileObjectVars.Object = objectFileName.c_str();
  560. compileObjectVars.ObjectDir = objectDir.c_str();
  561. compileObjectVars.Flags = vars["FLAGS"].c_str();
  562. compileObjectVars.Defines = vars["DEFINES"].c_str();
  563. // Rule for compiling object file.
  564. std::string compileCmdVar = "CMAKE_";
  565. compileCmdVar += language;
  566. compileCmdVar += "_COMPILE_OBJECT";
  567. std::string compileCmd =
  568. this->GetMakefile()->GetRequiredDefinition(compileCmdVar);
  569. std::vector<std::string> compileCmds;
  570. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  571. for (std::vector<std::string>::iterator i = compileCmds.begin();
  572. i != compileCmds.end(); ++i)
  573. this->GetLocalGenerator()->ExpandRuleVariables(*i, compileObjectVars);
  574. std::string cmdLine =
  575. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  576. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine,
  577. sourceFileName);
  578. }
  579. this->GetGlobalGenerator()->WriteBuild(this->GetBuildFileStream(),
  580. comment,
  581. rule,
  582. outputs,
  583. explicitDeps,
  584. implicitDeps,
  585. orderOnlyDeps,
  586. vars);
  587. if(const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  588. std::vector<std::string> outputList;
  589. cmSystemTools::ExpandListArgument(objectOutputs, outputList);
  590. std::transform(outputList.begin(), outputList.end(), outputList.begin(),
  591. MapToNinjaPath());
  592. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  593. "Additional output files.",
  594. outputList,
  595. outputs);
  596. }
  597. }
  598. //----------------------------------------------------------------------------
  599. void
  600. cmNinjaTargetGenerator
  601. ::AddModuleDefinitionFlag(std::string& flags)
  602. {
  603. if(this->ModuleDefinitionFile.empty())
  604. {
  605. return;
  606. }
  607. // TODO: Create a per-language flag variable.
  608. const char* defFileFlag =
  609. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  610. if(!defFileFlag)
  611. {
  612. return;
  613. }
  614. // Append the flag and value. Use ConvertToLinkReference to help
  615. // vs6's "cl -link" pass it to the linker.
  616. std::string flag = defFileFlag;
  617. flag += (this->LocalGenerator->ConvertToLinkReference(
  618. this->ModuleDefinitionFile));
  619. this->LocalGenerator->AppendFlags(flags, flag);
  620. }
  621. void
  622. cmNinjaTargetGenerator
  623. ::EnsureDirectoryExists(const std::string& path) const
  624. {
  625. if (cmSystemTools::FileIsFullPath(path.c_str()))
  626. {
  627. cmSystemTools::MakeDirectory(path.c_str());
  628. }
  629. else
  630. {
  631. const std::string fullPath = std::string(this->GetGlobalGenerator()->
  632. GetCMakeInstance()->GetHomeOutputDirectory())
  633. + "/" + path;
  634. cmSystemTools::MakeDirectory(fullPath.c_str());
  635. }
  636. }
  637. void
  638. cmNinjaTargetGenerator
  639. ::EnsureParentDirectoryExists(const std::string& path) const
  640. {
  641. EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path.c_str()));
  642. }
  643. //----------------------------------------------------------------------------
  644. void
  645. cmNinjaTargetGenerator::MacOSXContentGeneratorType::operator()(
  646. cmSourceFile const& source, const char* pkgloc)
  647. {
  648. // Skip OS X content when not building a Framework or Bundle.
  649. if(!this->Generator->GetTarget()->IsBundleOnApple())
  650. {
  651. return;
  652. }
  653. std::string macdir =
  654. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  655. // Get the input file location.
  656. std::string input = source.GetFullPath();
  657. input =
  658. this->Generator->GetLocalGenerator()->ConvertToNinjaPath(input.c_str());
  659. // Get the output file location.
  660. std::string output = macdir;
  661. output += "/";
  662. output += cmSystemTools::GetFilenameName(input);
  663. output =
  664. this->Generator->GetLocalGenerator()->ConvertToNinjaPath(output.c_str());
  665. // Write a build statement to copy the content into the bundle.
  666. this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(input,
  667. output);
  668. // Add as a dependency of all target so that it gets called.
  669. this->Generator->GetGlobalGenerator()->AddDependencyToAll(output);
  670. }
  671. void cmNinjaTargetGenerator::addPoolNinjaVariable(
  672. const std::string& pool_property,
  673. cmTarget* target,
  674. cmNinjaVars& vars)
  675. {
  676. const char* pool = target->GetProperty(pool_property);
  677. if (pool)
  678. {
  679. vars["pool"] = pool;
  680. }
  681. }