cmNinjaTargetGenerator.cxx 23 KB

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