cmNinjaTargetGenerator.cxx 22 KB

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