cmNinjaTargetGenerator.cxx 23 KB

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