cmNinjaTargetGenerator.cxx 25 KB

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