cmNinjaTargetGenerator.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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(cmTarget* 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->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. : Target(target),
  52. Makefile(target->GetMakefile()),
  53. LocalGenerator(
  54. static_cast<cmLocalNinjaGenerator*>(Makefile->GetLocalGenerator())),
  55. Objects()
  56. {
  57. this->GeneratorTarget =
  58. this->GetGlobalGenerator()->GetGeneratorTarget(target);
  59. }
  60. cmNinjaTargetGenerator::~cmNinjaTargetGenerator()
  61. {
  62. }
  63. cmGeneratedFileStream& cmNinjaTargetGenerator::GetBuildFileStream() const
  64. {
  65. return *this->GetGlobalGenerator()->GetBuildFileStream();
  66. }
  67. cmGeneratedFileStream& cmNinjaTargetGenerator::GetRulesFileStream() const
  68. {
  69. return *this->GetGlobalGenerator()->GetRulesFileStream();
  70. }
  71. cmGlobalNinjaGenerator* cmNinjaTargetGenerator::GetGlobalGenerator() const
  72. {
  73. return this->LocalGenerator->GetGlobalNinjaGenerator();
  74. }
  75. const char* cmNinjaTargetGenerator::GetConfigName() const
  76. {
  77. return this->LocalGenerator->ConfigName.c_str();
  78. }
  79. // TODO: Picked up from cmMakefileTargetGenerator. Refactor it.
  80. const char* cmNinjaTargetGenerator::GetFeature(const char* feature)
  81. {
  82. return this->Target->GetFeature(feature, this->GetConfigName());
  83. }
  84. // TODO: Picked up from cmMakefileTargetGenerator. Refactor it.
  85. bool cmNinjaTargetGenerator::GetFeatureAsBool(const char* feature)
  86. {
  87. return cmSystemTools::IsOn(this->GetFeature(feature));
  88. }
  89. // TODO: Picked up from cmMakefileTargetGenerator. Refactor it.
  90. void cmNinjaTargetGenerator::AddFeatureFlags(std::string& flags,
  91. const char* lang)
  92. {
  93. // Add language-specific flags.
  94. this->LocalGenerator->AddLanguageFlags(flags, lang, this->GetConfigName());
  95. if(this->GetFeatureAsBool("INTERPROCEDURAL_OPTIMIZATION"))
  96. {
  97. this->LocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  98. }
  99. }
  100. // TODO: Most of the code is picked up from
  101. // void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink),
  102. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
  103. // Refactor it.
  104. std::string
  105. cmNinjaTargetGenerator::ComputeFlagsForObject(cmSourceFile *source,
  106. const std::string& language)
  107. {
  108. std::string flags;
  109. this->AddFeatureFlags(flags, language.c_str());
  110. this->GetLocalGenerator()->AddArchitectureFlags(flags,
  111. this->GetTarget(),
  112. language.c_str(),
  113. this->GetConfigName());
  114. // TODO: Fortran support.
  115. // // Fortran-specific flags computed for this target.
  116. // if(*l == "Fortran")
  117. // {
  118. // this->AddFortranFlags(flags);
  119. // }
  120. // Add shared-library flags if needed.
  121. this->LocalGenerator->AddCMP0018Flags(flags, this->Target,
  122. language.c_str());
  123. // Add include directory flags.
  124. {
  125. std::vector<std::string> includes;
  126. this->LocalGenerator->GetIncludeDirectories(includes, this->Target,
  127. language.c_str());
  128. std::string includeFlags =
  129. this->LocalGenerator->GetIncludeFlags(includes, language.c_str(),
  130. language == "RC" ? true : false); // full include paths for RC
  131. // needed by cmcldeps
  132. if(cmGlobalNinjaGenerator::IsMinGW())
  133. cmSystemTools::ReplaceString(includeFlags, "\\", "/");
  134. this->LocalGenerator->AppendFlags(flags, includeFlags.c_str());
  135. }
  136. // Append old-style preprocessor definition flags.
  137. this->LocalGenerator->AppendFlags(flags, this->Makefile->GetDefineFlags());
  138. // Add target-specific and source-specific flags.
  139. this->LocalGenerator->AppendFlags(flags,
  140. this->Target->GetProperty("COMPILE_FLAGS"));
  141. this->LocalGenerator->AppendFlags(flags,
  142. source->GetProperty("COMPILE_FLAGS"));
  143. // TODO: Handle Apple frameworks.
  144. return flags;
  145. }
  146. // TODO: Refactor with
  147. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  148. std::string
  149. cmNinjaTargetGenerator::
  150. ComputeDefines(cmSourceFile *source, const std::string& language)
  151. {
  152. std::string defines;
  153. // Add the export symbol definition for shared library objects.
  154. if(const char* exportMacro = this->Target->GetExportMacro())
  155. {
  156. this->LocalGenerator->AppendDefines(defines, exportMacro,
  157. language.c_str());
  158. }
  159. // Add preprocessor definitions for this target and configuration.
  160. this->LocalGenerator->AppendDefines
  161. (defines,
  162. this->Makefile->GetProperty("COMPILE_DEFINITIONS"),
  163. language.c_str());
  164. this->LocalGenerator->AppendDefines
  165. (defines,
  166. this->Target->GetProperty("COMPILE_DEFINITIONS"),
  167. language.c_str());
  168. this->LocalGenerator->AppendDefines
  169. (defines,
  170. source->GetProperty("COMPILE_DEFINITIONS"),
  171. language.c_str());
  172. {
  173. std::string defPropName = "COMPILE_DEFINITIONS_";
  174. defPropName += cmSystemTools::UpperCase(this->GetConfigName());
  175. this->LocalGenerator->AppendDefines
  176. (defines,
  177. this->Makefile->GetProperty(defPropName.c_str()),
  178. language.c_str());
  179. this->LocalGenerator->AppendDefines
  180. (defines,
  181. this->Target->GetProperty(defPropName.c_str()),
  182. language.c_str());
  183. this->LocalGenerator->AppendDefines
  184. (defines,
  185. source->GetProperty(defPropName.c_str()),
  186. language.c_str());
  187. }
  188. return defines;
  189. }
  190. cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps() const
  191. {
  192. // Static libraries never depend on other targets for linking.
  193. if (this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  194. this->Target->GetType() == cmTarget::OBJECT_LIBRARY)
  195. return cmNinjaDeps();
  196. cmComputeLinkInformation* cli =
  197. this->Target->GetLinkInformation(this->GetConfigName());
  198. if(!cli)
  199. return cmNinjaDeps();
  200. const std::vector<std::string> &deps = cli->GetDepends();
  201. cmNinjaDeps result(deps.size());
  202. std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
  203. // Add a dependency on the link definitions file, if any.
  204. if(!this->ModuleDefinitionFile.empty())
  205. {
  206. result.push_back(this->ModuleDefinitionFile);
  207. }
  208. return result;
  209. }
  210. std::string
  211. cmNinjaTargetGenerator
  212. ::GetSourceFilePath(cmSourceFile* source) const
  213. {
  214. return ConvertToNinjaPath(source->GetFullPath().c_str());
  215. }
  216. std::string
  217. cmNinjaTargetGenerator
  218. ::GetObjectFilePath(cmSourceFile* source) const
  219. {
  220. std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
  221. if(!path.empty())
  222. path += "/";
  223. std::string const& objectName = this->GeneratorTarget->Objects[source];
  224. path += this->LocalGenerator->GetTargetDirectory(*this->Target);
  225. path += "/";
  226. path += objectName;
  227. return path;
  228. }
  229. std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
  230. {
  231. std::string dir = this->Target->GetDirectory(this->GetConfigName());
  232. return ConvertToNinjaPath(dir.c_str());
  233. }
  234. std::string
  235. cmNinjaTargetGenerator
  236. ::GetTargetFilePath(const std::string& name) const
  237. {
  238. std::string path = this->GetTargetOutputDir();
  239. if (path.empty() || path == ".")
  240. return name;
  241. path += "/";
  242. path += name;
  243. return path;
  244. }
  245. std::string cmNinjaTargetGenerator::GetTargetName() const
  246. {
  247. return this->Target->GetName();
  248. }
  249. std::string cmNinjaTargetGenerator::GetTargetPDB() const
  250. {
  251. std::string targetFullPathPDB;
  252. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  253. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  254. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  255. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  256. {
  257. targetFullPathPDB = this->Target->GetDirectory(this->GetConfigName());
  258. targetFullPathPDB += "/";
  259. targetFullPathPDB += this->Target->GetPDBName(this->GetConfigName());
  260. }
  261. return targetFullPathPDB.c_str();
  262. }
  263. void
  264. cmNinjaTargetGenerator
  265. ::WriteLanguageRules(const std::string& language)
  266. {
  267. #ifdef NINJA_GEN_VERBOSE_FILES
  268. this->GetRulesFileStream()
  269. << "# Rules for language " << language << "\n\n";
  270. #endif
  271. this->WriteCompileRule(language);
  272. }
  273. void
  274. cmNinjaTargetGenerator
  275. ::WriteCompileRule(const std::string& language)
  276. {
  277. cmLocalGenerator::RuleVariables vars;
  278. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  279. vars.CMTarget = this->GetTarget();
  280. std::string lang = language;
  281. vars.Language = lang.c_str();
  282. vars.Source = "$in";
  283. vars.Object = "$out";
  284. std::string flags = "$FLAGS";
  285. vars.Defines = "$DEFINES";
  286. vars.TargetPDB = "$TARGET_PDB";
  287. cmMakefile* mf = this->GetMakefile();
  288. bool useClDeps = false;
  289. std::string clDepsBinary;
  290. std::string clShowPrefix;
  291. if (lang == "C" || lang == "CXX" || lang == "RC")
  292. {
  293. const char* depsPtr = mf->GetDefinition("CMAKE_CMCLDEPS_EXECUTABLE");
  294. const char* showPtr = mf->GetDefinition("CMAKE_CL_SHOWINCLUDE_PREFIX");
  295. if (depsPtr && showPtr)
  296. {
  297. // don't wrap for try_compile,
  298. // TODO but why doesn't it work with cmcldeps?
  299. const std::string projectName = mf->GetProjectName() ?
  300. mf->GetProjectName() : "";
  301. if (projectName != "CMAKE_TRY_COMPILE")
  302. {
  303. useClDeps = true;
  304. std::string qu = "\"";
  305. clDepsBinary = qu + depsPtr + qu;
  306. clShowPrefix = qu + showPtr + qu;
  307. vars.DependencyFile = "$DEP_FILE";
  308. }
  309. }
  310. }
  311. std::string depfile;
  312. std::string depfileFlagsName = "CMAKE_DEPFILE_FLAGS_" + language;
  313. const char *depfileFlags = mf->GetDefinition(depfileFlagsName.c_str());
  314. if (depfileFlags || useClDeps) {
  315. std::string depFlagsStr = depfileFlags ? depfileFlags : "";
  316. depfile = "$DEP_FILE";
  317. cmSystemTools::ReplaceString(depFlagsStr, "<DEPFILE>", "\"$DEP_FILE\"");
  318. cmSystemTools::ReplaceString(depFlagsStr, "<OBJECT>", "$out");
  319. cmSystemTools::ReplaceString(depFlagsStr, "<CMAKE_C_COMPILER>",
  320. mf->GetDefinition("CMAKE_C_COMPILER"));
  321. flags += " " + depFlagsStr;
  322. }
  323. vars.Flags = flags.c_str();
  324. // Rule for compiling object file.
  325. std::string compileCmdVar = "CMAKE_";
  326. compileCmdVar += language;
  327. compileCmdVar += "_COMPILE_OBJECT";
  328. std::string compileCmd = mf->GetRequiredDefinition(compileCmdVar.c_str());
  329. std::vector<std::string> compileCmds;
  330. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  331. for (std::vector<std::string>::iterator i = compileCmds.begin();
  332. i != compileCmds.end(); ++i)
  333. this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
  334. std::string cmdLine =
  335. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  336. if(useClDeps)
  337. {
  338. std::string cl = mf->GetDefinition("CMAKE_C_COMPILER");
  339. cl = "\"" + cl + "\" ";
  340. cmdLine = clDepsBinary + " " + lang + " $in \"$DEP_FILE\" $out "
  341. + clShowPrefix + " " + cl + cmdLine;
  342. }
  343. // Write the rule for compiling file of the given language.
  344. cmOStringStream comment;
  345. comment << "Rule for compiling " << language << " files.";
  346. cmOStringStream description;
  347. description << "Building " << language << " object $out";
  348. this->GetGlobalGenerator()->AddRule(this->LanguageCompilerRule(language),
  349. cmdLine,
  350. description.str(),
  351. comment.str(),
  352. depfile);
  353. }
  354. void
  355. cmNinjaTargetGenerator
  356. ::WriteObjectBuildStatements()
  357. {
  358. // Write comments.
  359. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  360. this->GetBuildFileStream()
  361. << "# Object build statements for "
  362. << cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  363. << " target "
  364. << this->GetTargetName()
  365. << "\n\n";
  366. for(std::vector<cmSourceFile*>::const_iterator
  367. si = this->GeneratorTarget->CustomCommands.begin();
  368. si != this->GeneratorTarget->CustomCommands.end(); ++si)
  369. {
  370. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  371. this->GetLocalGenerator()->AddCustomCommandTarget(cc, this->GetTarget());
  372. }
  373. for(std::vector<cmSourceFile*>::const_iterator
  374. si = this->GeneratorTarget->ExternalObjects.begin();
  375. si != this->GeneratorTarget->ExternalObjects.end(); ++si)
  376. {
  377. this->Objects.push_back(this->GetSourceFilePath(*si));
  378. }
  379. for(std::vector<cmSourceFile*>::const_iterator
  380. si = this->GeneratorTarget->ObjectSources.begin();
  381. si != this->GeneratorTarget->ObjectSources.end(); ++si)
  382. {
  383. this->WriteObjectBuildStatement(*si);
  384. }
  385. if(!this->GeneratorTarget->ModuleDefinitionFile.empty())
  386. {
  387. this->ModuleDefinitionFile = this->ConvertToNinjaPath(
  388. this->GeneratorTarget->ModuleDefinitionFile.c_str());
  389. }
  390. {
  391. // Add object library contents as external objects.
  392. std::vector<std::string> objs;
  393. this->GeneratorTarget->UseObjectLibraries(objs);
  394. for(std::vector<std::string>::iterator oi = objs.begin();
  395. oi != objs.end(); ++oi)
  396. {
  397. this->Objects.push_back(ConvertToNinjaPath(oi->c_str()));
  398. }
  399. }
  400. this->GetBuildFileStream() << "\n";
  401. }
  402. void
  403. cmNinjaTargetGenerator
  404. ::WriteObjectBuildStatement(cmSourceFile* source)
  405. {
  406. cmNinjaDeps emptyDeps;
  407. std::string comment;
  408. const std::string language = source->GetLanguage();
  409. std::string rule = this->LanguageCompilerRule(language);
  410. cmNinjaDeps outputs;
  411. std::string objectFileName = this->GetObjectFilePath(source);
  412. outputs.push_back(objectFileName);
  413. // Add this object to the list of object files.
  414. this->Objects.push_back(objectFileName);
  415. cmNinjaDeps explicitDeps;
  416. std::string sourceFileName;
  417. if (language == "RC")
  418. sourceFileName = source->GetFullPath();
  419. else
  420. sourceFileName = this->GetSourceFilePath(source);
  421. explicitDeps.push_back(sourceFileName);
  422. // Ensure that the target dependencies are built before any source file in
  423. // the target, using order-only dependencies.
  424. cmNinjaDeps orderOnlyDeps;
  425. this->GetLocalGenerator()->AppendTargetDepends(this->Target, orderOnlyDeps);
  426. if(const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  427. std::vector<std::string> depList;
  428. cmSystemTools::ExpandListArgument(objectDeps, depList);
  429. std::transform(depList.begin(), depList.end(),
  430. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  431. }
  432. // Add order-only dependencies on custom command outputs.
  433. for(std::vector<cmSourceFile*>::const_iterator
  434. si = this->GeneratorTarget->CustomCommands.begin();
  435. si != this->GeneratorTarget->CustomCommands.end(); ++si)
  436. {
  437. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  438. const std::vector<std::string>& ccoutputs = cc->GetOutputs();
  439. std::transform(ccoutputs.begin(), ccoutputs.end(),
  440. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  441. }
  442. // If the source file is GENERATED and does not have a custom command
  443. // (either attached to this source file or another one), assume that one of
  444. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  445. // will rebuild the file.
  446. if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
  447. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  448. this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
  449. orderOnlyDeps);
  450. }
  451. cmNinjaVars vars;
  452. vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  453. vars["DEFINES"] = this->ComputeDefines(source, language);
  454. vars["DEP_FILE"] = objectFileName + ".d";;
  455. EnsureParentDirectoryExists(objectFileName);
  456. // TODO move to GetTargetPDB
  457. cmMakefile* mf = this->GetMakefile();
  458. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  459. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID"))
  460. {
  461. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  462. ConvertToNinjaPath(GetTargetPDB().c_str()).c_str(),
  463. cmLocalGenerator::SHELL);
  464. }
  465. if(this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS"))
  466. {
  467. cmLocalGenerator::RuleVariables compileObjectVars;
  468. std::string lang = language;
  469. compileObjectVars.Language = lang.c_str();
  470. std::string escapedSourceFileName = sourceFileName;
  471. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str()))
  472. {
  473. escapedSourceFileName = cmSystemTools::CollapseFullPath(
  474. escapedSourceFileName.c_str(),
  475. this->GetGlobalGenerator()->GetCMakeInstance()->
  476. GetHomeOutputDirectory());
  477. }
  478. escapedSourceFileName =
  479. this->LocalGenerator->ConvertToOutputFormat(
  480. escapedSourceFileName.c_str(), cmLocalGenerator::SHELL);
  481. compileObjectVars.Source = escapedSourceFileName.c_str();
  482. compileObjectVars.Object = objectFileName.c_str();
  483. compileObjectVars.Flags = vars["FLAGS"].c_str();
  484. compileObjectVars.Defines = vars["DEFINES"].c_str();
  485. // Rule for compiling object file.
  486. std::string compileCmdVar = "CMAKE_";
  487. compileCmdVar += language;
  488. compileCmdVar += "_COMPILE_OBJECT";
  489. std::string compileCmd =
  490. this->GetMakefile()->GetRequiredDefinition(compileCmdVar.c_str());
  491. std::vector<std::string> compileCmds;
  492. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  493. for (std::vector<std::string>::iterator i = compileCmds.begin();
  494. i != compileCmds.end(); ++i)
  495. this->GetLocalGenerator()->ExpandRuleVariables(*i, compileObjectVars);
  496. std::string cmdLine =
  497. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  498. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine,
  499. sourceFileName);
  500. }
  501. cmGlobalNinjaGenerator::WriteBuild(this->GetBuildFileStream(),
  502. comment,
  503. rule,
  504. outputs,
  505. explicitDeps,
  506. emptyDeps,
  507. orderOnlyDeps,
  508. vars);
  509. if(const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  510. std::vector<std::string> outputList;
  511. cmSystemTools::ExpandListArgument(objectOutputs, outputList);
  512. std::transform(outputList.begin(), outputList.end(), outputList.begin(),
  513. MapToNinjaPath());
  514. cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
  515. "Additional output files.",
  516. outputList,
  517. outputs);
  518. }
  519. }
  520. //----------------------------------------------------------------------------
  521. void
  522. cmNinjaTargetGenerator
  523. ::AddModuleDefinitionFlag(std::string& flags)
  524. {
  525. if(this->ModuleDefinitionFile.empty())
  526. {
  527. return;
  528. }
  529. // TODO: Create a per-language flag variable.
  530. const char* defFileFlag =
  531. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  532. if(!defFileFlag)
  533. {
  534. return;
  535. }
  536. // Append the flag and value. Use ConvertToLinkReference to help
  537. // vs6's "cl -link" pass it to the linker.
  538. std::string flag = defFileFlag;
  539. flag += (this->LocalGenerator->ConvertToLinkReference(
  540. this->ModuleDefinitionFile.c_str()));
  541. this->LocalGenerator->AppendFlags(flags, flag.c_str());
  542. }
  543. void
  544. cmNinjaTargetGenerator
  545. ::EnsureDirectoryExists(const std::string& dir)
  546. {
  547. cmSystemTools::MakeDirectory(dir.c_str());
  548. }
  549. void
  550. cmNinjaTargetGenerator
  551. ::EnsureParentDirectoryExists(const std::string& path)
  552. {
  553. EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path.c_str()));
  554. }