cmNinjaTargetGenerator.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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 "cmOSXBundleGenerator.h"
  24. #include <algorithm>
  25. cmNinjaTargetGenerator *
  26. cmNinjaTargetGenerator::New(cmTarget* target)
  27. {
  28. switch (target->GetType())
  29. {
  30. case cmTarget::EXECUTABLE:
  31. case cmTarget::SHARED_LIBRARY:
  32. case cmTarget::STATIC_LIBRARY:
  33. case cmTarget::MODULE_LIBRARY:
  34. case cmTarget::OBJECT_LIBRARY:
  35. return new cmNinjaNormalTargetGenerator(target);
  36. case cmTarget::UTILITY:
  37. return new cmNinjaUtilityTargetGenerator(target);;
  38. case cmTarget::GLOBAL_TARGET: {
  39. // We only want to process global targets that live in the home
  40. // (i.e. top-level) directory. CMake creates copies of these targets
  41. // in every directory, which we don't need.
  42. cmMakefile *mf = target->GetMakefile();
  43. if (strcmp(mf->GetStartDirectory(), mf->GetHomeDirectory()) == 0)
  44. return new cmNinjaUtilityTargetGenerator(target);
  45. // else fallthrough
  46. }
  47. default:
  48. return 0;
  49. }
  50. }
  51. cmNinjaTargetGenerator::cmNinjaTargetGenerator(cmTarget* target)
  52. :
  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->WriteMacOSXContentBuildStatements(
  377. this->GeneratorTarget->HeaderSources);
  378. this->WriteMacOSXContentBuildStatements(this->GeneratorTarget->ExtraSources);
  379. for(std::vector<cmSourceFile*>::const_iterator
  380. si = this->GeneratorTarget->ExternalObjects.begin();
  381. si != this->GeneratorTarget->ExternalObjects.end(); ++si)
  382. {
  383. this->Objects.push_back(this->GetSourceFilePath(*si));
  384. }
  385. for(std::vector<cmSourceFile*>::const_iterator
  386. si = this->GeneratorTarget->ObjectSources.begin();
  387. si != this->GeneratorTarget->ObjectSources.end(); ++si)
  388. {
  389. this->WriteObjectBuildStatement(*si);
  390. }
  391. if(!this->GeneratorTarget->ModuleDefinitionFile.empty())
  392. {
  393. this->ModuleDefinitionFile = this->ConvertToNinjaPath(
  394. this->GeneratorTarget->ModuleDefinitionFile.c_str());
  395. }
  396. {
  397. // Add object library contents as external objects.
  398. std::vector<std::string> objs;
  399. this->GeneratorTarget->UseObjectLibraries(objs);
  400. for(std::vector<std::string>::iterator oi = objs.begin();
  401. oi != objs.end(); ++oi)
  402. {
  403. this->Objects.push_back(ConvertToNinjaPath(oi->c_str()));
  404. }
  405. }
  406. this->GetBuildFileStream() << "\n";
  407. }
  408. void
  409. cmNinjaTargetGenerator
  410. ::WriteObjectBuildStatement(cmSourceFile* source)
  411. {
  412. cmNinjaDeps emptyDeps;
  413. std::string comment;
  414. const char* language = source->GetLanguage();
  415. std::string rule = this->LanguageCompilerRule(language);
  416. cmNinjaDeps outputs;
  417. std::string objectFileName = this->GetObjectFilePath(source);
  418. outputs.push_back(objectFileName);
  419. // Add this object to the list of object files.
  420. this->Objects.push_back(objectFileName);
  421. cmNinjaDeps explicitDeps;
  422. std::string sourceFileName = this->GetSourceFilePath(source);
  423. explicitDeps.push_back(sourceFileName);
  424. // Ensure that the target dependencies are built before any source file in
  425. // the target, using order-only dependencies.
  426. cmNinjaDeps orderOnlyDeps;
  427. this->GetLocalGenerator()->AppendTargetDepends(this->Target, orderOnlyDeps);
  428. if(const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  429. std::vector<std::string> depList;
  430. cmSystemTools::ExpandListArgument(objectDeps, depList);
  431. std::transform(depList.begin(), depList.end(),
  432. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  433. }
  434. // Add order-only dependencies on custom command outputs.
  435. for(std::vector<cmSourceFile*>::const_iterator
  436. si = this->GeneratorTarget->CustomCommands.begin();
  437. si != this->GeneratorTarget->CustomCommands.end(); ++si)
  438. {
  439. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  440. const std::vector<std::string>& ccoutputs = cc->GetOutputs();
  441. std::transform(ccoutputs.begin(), ccoutputs.end(),
  442. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  443. }
  444. // If the source file is GENERATED and does not have a custom command
  445. // (either attached to this source file or another one), assume that one of
  446. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  447. // will rebuild the file.
  448. if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
  449. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  450. this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
  451. orderOnlyDeps);
  452. }
  453. cmNinjaVars vars;
  454. vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  455. vars["DEFINES"] = this->ComputeDefines(source, language);
  456. vars["DEP_FILE"] = objectFileName + ".d";;
  457. EnsureParentDirectoryExists(objectFileName);
  458. // TODO move to GetTargetPDB
  459. cmMakefile* mf = this->GetMakefile();
  460. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  461. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID"))
  462. {
  463. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  464. ConvertToNinjaPath(GetTargetPDB().c_str()).c_str(),
  465. cmLocalGenerator::SHELL);
  466. }
  467. if(this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS"))
  468. {
  469. cmLocalGenerator::RuleVariables compileObjectVars;
  470. std::string lang = language;
  471. compileObjectVars.Language = lang.c_str();
  472. std::string escapedSourceFileName = sourceFileName;
  473. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str()))
  474. {
  475. escapedSourceFileName = cmSystemTools::CollapseFullPath(
  476. escapedSourceFileName.c_str(),
  477. this->GetGlobalGenerator()->GetCMakeInstance()->
  478. GetHomeOutputDirectory());
  479. }
  480. escapedSourceFileName =
  481. this->LocalGenerator->ConvertToOutputFormat(
  482. escapedSourceFileName.c_str(), cmLocalGenerator::SHELL);
  483. compileObjectVars.Source = escapedSourceFileName.c_str();
  484. compileObjectVars.Object = objectFileName.c_str();
  485. compileObjectVars.Flags = vars["FLAGS"].c_str();
  486. compileObjectVars.Defines = vars["DEFINES"].c_str();
  487. // Rule for compiling object file.
  488. std::string compileCmdVar = "CMAKE_";
  489. compileCmdVar += language;
  490. compileCmdVar += "_COMPILE_OBJECT";
  491. std::string compileCmd =
  492. this->GetMakefile()->GetRequiredDefinition(compileCmdVar.c_str());
  493. std::vector<std::string> compileCmds;
  494. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  495. for (std::vector<std::string>::iterator i = compileCmds.begin();
  496. i != compileCmds.end(); ++i)
  497. this->GetLocalGenerator()->ExpandRuleVariables(*i, compileObjectVars);
  498. std::string cmdLine =
  499. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  500. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine,
  501. sourceFileName);
  502. }
  503. cmGlobalNinjaGenerator::WriteBuild(this->GetBuildFileStream(),
  504. comment,
  505. rule,
  506. outputs,
  507. explicitDeps,
  508. emptyDeps,
  509. orderOnlyDeps,
  510. vars);
  511. if(const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  512. std::vector<std::string> outputList;
  513. cmSystemTools::ExpandListArgument(objectOutputs, outputList);
  514. std::transform(outputList.begin(), outputList.end(), outputList.begin(),
  515. MapToNinjaPath());
  516. cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
  517. "Additional output files.",
  518. outputList,
  519. outputs);
  520. }
  521. }
  522. //----------------------------------------------------------------------------
  523. void
  524. cmNinjaTargetGenerator
  525. ::AddModuleDefinitionFlag(std::string& flags)
  526. {
  527. if(this->ModuleDefinitionFile.empty())
  528. {
  529. return;
  530. }
  531. // TODO: Create a per-language flag variable.
  532. const char* defFileFlag =
  533. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  534. if(!defFileFlag)
  535. {
  536. return;
  537. }
  538. // Append the flag and value. Use ConvertToLinkReference to help
  539. // vs6's "cl -link" pass it to the linker.
  540. std::string flag = defFileFlag;
  541. flag += (this->LocalGenerator->ConvertToLinkReference(
  542. this->ModuleDefinitionFile.c_str()));
  543. this->LocalGenerator->AppendFlags(flags, flag.c_str());
  544. }
  545. void
  546. cmNinjaTargetGenerator
  547. ::EnsureDirectoryExists(const std::string& dir)
  548. {
  549. cmSystemTools::MakeDirectory(dir.c_str());
  550. }
  551. void
  552. cmNinjaTargetGenerator
  553. ::EnsureParentDirectoryExists(const std::string& path)
  554. {
  555. EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path.c_str()));
  556. }
  557. //----------------------------------------------------------------------------
  558. // TODO: Re-factor with cmMakefileTargetGenerator::WriteMacOSXContentRules
  559. void cmNinjaTargetGenerator::WriteMacOSXContentBuildStatements(
  560. std::vector<cmSourceFile*> const& sources)
  561. {
  562. for(std::vector<cmSourceFile*>::const_iterator
  563. si = sources.begin(); si != sources.end(); ++si)
  564. {
  565. cmTarget::SourceFileFlags tsFlags =
  566. this->Target->GetTargetSourceFileFlags(*si);
  567. if(tsFlags.Type != cmTarget::SourceFileTypeNormal)
  568. {
  569. this->WriteMacOSXContentBuildStatement(**si, tsFlags.MacFolder);
  570. }
  571. }
  572. }
  573. //----------------------------------------------------------------------------
  574. // TODO: Re-factor with cmMakefileTargetGenerator::WriteMacOSXContentRules
  575. void cmNinjaTargetGenerator::WriteMacOSXContentBuildStatement(
  576. cmSourceFile& source, const char* pkgloc)
  577. {
  578. // Skip OS X content when not building a Framework or Bundle.
  579. if(this->OSXBundleGenerator->GetMacContentDirectory().empty())
  580. {
  581. return;
  582. }
  583. // Construct the full path to the content subdirectory.
  584. std::string macdir = this->OSXBundleGenerator->GetMacContentDirectory();
  585. macdir += pkgloc;
  586. cmSystemTools::MakeDirectory(macdir.c_str());
  587. // Record use of this content location. Only the first level
  588. // directory is needed.
  589. {
  590. std::string loc = pkgloc;
  591. loc = loc.substr(0, loc.find('/'));
  592. this->MacContentFolders.insert(loc);
  593. }
  594. // Get the input file location.
  595. std::string input = source.GetFullPath();
  596. input = this->GetLocalGenerator()->ConvertToNinjaPath(input.c_str());
  597. // Get the output file location.
  598. std::string output = macdir;
  599. output += "/";
  600. output += cmSystemTools::GetFilenameName(input);
  601. output = this->GetLocalGenerator()->ConvertToNinjaPath(output.c_str());
  602. // Write a build statement to copy the content into the bundle.
  603. this->GetGlobalGenerator()->WriteMacOSXContentBuild(input, output);
  604. // Add as a dependency of all target so that it gets called.
  605. this->GetGlobalGenerator()->AddDependencyToAll(output);
  606. }