cmNinjaTargetGenerator.cxx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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
  232. ->GetObjectName(source);
  233. path += this->LocalGenerator->GetTargetDirectory(*this->Target);
  234. path += "/";
  235. path += objectName;
  236. return path;
  237. }
  238. std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
  239. {
  240. std::string dir = this->Target->GetDirectory(this->GetConfigName());
  241. return ConvertToNinjaPath(dir.c_str());
  242. }
  243. std::string
  244. cmNinjaTargetGenerator
  245. ::GetTargetFilePath(const std::string& name) const
  246. {
  247. std::string path = this->GetTargetOutputDir();
  248. if (path.empty() || path == ".")
  249. return name;
  250. path += "/";
  251. path += name;
  252. return path;
  253. }
  254. std::string cmNinjaTargetGenerator::GetTargetName() const
  255. {
  256. return this->Target->GetName();
  257. }
  258. bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(cmNinjaVars& vars) const
  259. {
  260. cmMakefile* mf = this->GetMakefile();
  261. if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
  262. mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID"))
  263. {
  264. std::string pdbPath;
  265. if(this->Target->GetType() == cmTarget::EXECUTABLE ||
  266. this->Target->GetType() == cmTarget::STATIC_LIBRARY ||
  267. this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  268. this->Target->GetType() == cmTarget::MODULE_LIBRARY)
  269. {
  270. pdbPath = this->Target->GetPDBDirectory(this->GetConfigName());
  271. pdbPath += "/";
  272. pdbPath += this->Target->GetPDBName(this->GetConfigName());
  273. }
  274. vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  275. ConvertToNinjaPath(pdbPath.c_str()).c_str(),
  276. cmLocalGenerator::SHELL);
  277. EnsureParentDirectoryExists(pdbPath);
  278. return true;
  279. }
  280. return false;
  281. }
  282. void
  283. cmNinjaTargetGenerator
  284. ::WriteLanguageRules(const std::string& language)
  285. {
  286. #ifdef NINJA_GEN_VERBOSE_FILES
  287. this->GetRulesFileStream()
  288. << "# Rules for language " << language << "\n\n";
  289. #endif
  290. this->WriteCompileRule(language);
  291. }
  292. void
  293. cmNinjaTargetGenerator
  294. ::WriteCompileRule(const std::string& lang)
  295. {
  296. cmLocalGenerator::RuleVariables vars;
  297. vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
  298. vars.CMTarget = this->GetTarget();
  299. vars.Language = lang.c_str();
  300. vars.Source = "$in";
  301. vars.Object = "$out";
  302. vars.Defines = "$DEFINES";
  303. vars.TargetPDB = "$TARGET_PDB";
  304. vars.ObjectDir = "$OBJECT_DIR";
  305. cmMakefile* mf = this->GetMakefile();
  306. const std::string cId = mf->GetDefinition("CMAKE_C_COMPILER_ID")
  307. ? mf->GetSafeDefinition("CMAKE_C_COMPILER_ID")
  308. : mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID");
  309. const bool usingMSVC = (cId == "MSVC" || cId == "Intel");
  310. // Tell ninja dependency format so all deps can be loaded into a database
  311. std::string deptype;
  312. std::string depfile;
  313. std::string cldeps;
  314. std::string flags = "$FLAGS";
  315. if (usingMSVC)
  316. {
  317. if (!mf->GetIsSourceFileTryCompile() && lang == "RC")
  318. {
  319. deptype = "gcc";
  320. depfile = "$DEP_FILE";
  321. const std::string cl = mf->GetDefinition("CMAKE_C_COMPILER") ?
  322. mf->GetSafeDefinition("CMAKE_C_COMPILER") :
  323. mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  324. cldeps = "\"";
  325. cldeps += mf->GetSafeDefinition("CMAKE_CMCLDEPS_EXECUTABLE");
  326. cldeps += "\" " + lang + " $in \"$DEP_FILE\" $out \"";
  327. cldeps += mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
  328. cldeps += "\" \"" + cl + "\" ";
  329. }
  330. else
  331. {
  332. deptype = "msvc";
  333. depfile = "";
  334. flags += " /showIncludes";
  335. }
  336. }
  337. else
  338. {
  339. deptype = "gcc";
  340. depfile = "$DEP_FILE";
  341. const std::string flagsName = "CMAKE_DEPFILE_FLAGS_" + lang;
  342. std::string depfileFlags = mf->GetSafeDefinition(flagsName.c_str());
  343. if (!depfileFlags.empty())
  344. {
  345. cmSystemTools::ReplaceString(depfileFlags, "<DEPFILE>", "$DEP_FILE");
  346. cmSystemTools::ReplaceString(depfileFlags, "<OBJECT>", "$out");
  347. cmSystemTools::ReplaceString(depfileFlags, "<CMAKE_C_COMPILER>",
  348. mf->GetDefinition("CMAKE_C_COMPILER"));
  349. flags += " " + depfileFlags;
  350. }
  351. }
  352. vars.Flags = flags.c_str();
  353. vars.DependencyFile = depfile.c_str();
  354. // Rule for compiling object file.
  355. const std::string cmdVar = std::string("CMAKE_") + lang + "_COMPILE_OBJECT";
  356. std::string compileCmd = mf->GetRequiredDefinition(cmdVar.c_str());
  357. std::vector<std::string> compileCmds;
  358. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  359. compileCmds.front().insert(0, cldeps);
  360. for (std::vector<std::string>::iterator i = compileCmds.begin();
  361. i != compileCmds.end(); ++i)
  362. this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
  363. std::string cmdLine =
  364. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  365. // Write the rule for compiling file of the given language.
  366. cmOStringStream comment;
  367. comment << "Rule for compiling " << lang << " files.";
  368. cmOStringStream description;
  369. description << "Building " << lang << " object $out";
  370. this->GetGlobalGenerator()->AddRule(this->LanguageCompilerRule(lang),
  371. cmdLine,
  372. description.str(),
  373. comment.str(),
  374. depfile,
  375. deptype,
  376. /*rspfile*/ "",
  377. /*rspcontent*/ "",
  378. /*restat*/ false,
  379. /*generator*/ false);
  380. }
  381. void
  382. cmNinjaTargetGenerator
  383. ::WriteObjectBuildStatements()
  384. {
  385. // Write comments.
  386. cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
  387. this->GetBuildFileStream()
  388. << "# Object build statements for "
  389. << cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
  390. << " target "
  391. << this->GetTargetName()
  392. << "\n\n";
  393. std::vector<cmSourceFile*> customCommands;
  394. this->GeneratorTarget->GetCustomCommands(customCommands);
  395. for(std::vector<cmSourceFile*>::const_iterator
  396. si = customCommands.begin();
  397. si != customCommands.end(); ++si)
  398. {
  399. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  400. this->GetLocalGenerator()->AddCustomCommandTarget(cc, this->GetTarget());
  401. }
  402. std::vector<cmSourceFile*> headerSources;
  403. this->GeneratorTarget->GetHeaderSources(headerSources);
  404. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  405. headerSources,
  406. this->MacOSXContentGenerator);
  407. std::vector<cmSourceFile*> extraSources;
  408. this->GeneratorTarget->GetExtraSources(extraSources);
  409. this->OSXBundleGenerator->GenerateMacOSXContentStatements(
  410. extraSources,
  411. this->MacOSXContentGenerator);
  412. std::vector<cmSourceFile*> externalObjects;
  413. this->GeneratorTarget->GetExternalObjects(externalObjects);
  414. for(std::vector<cmSourceFile*>::const_iterator
  415. si = externalObjects.begin();
  416. si != externalObjects.end(); ++si)
  417. {
  418. this->Objects.push_back(this->GetSourceFilePath(*si));
  419. }
  420. std::vector<cmSourceFile*> objectSources;
  421. this->GeneratorTarget->GetObjectSources(objectSources);
  422. for(std::vector<cmSourceFile*>::const_iterator
  423. si = objectSources.begin(); si != objectSources.end(); ++si)
  424. {
  425. this->WriteObjectBuildStatement(*si);
  426. }
  427. if(!this->GeneratorTarget->ModuleDefinitionFile.empty())
  428. {
  429. this->ModuleDefinitionFile = this->ConvertToNinjaPath(
  430. this->GeneratorTarget->ModuleDefinitionFile.c_str());
  431. }
  432. {
  433. // Add object library contents as external objects.
  434. std::vector<std::string> objs;
  435. this->GeneratorTarget->UseObjectLibraries(objs);
  436. for(std::vector<std::string>::iterator oi = objs.begin();
  437. oi != objs.end(); ++oi)
  438. {
  439. this->Objects.push_back(ConvertToNinjaPath(oi->c_str()));
  440. }
  441. }
  442. this->GetBuildFileStream() << "\n";
  443. }
  444. void
  445. cmNinjaTargetGenerator
  446. ::WriteObjectBuildStatement(cmSourceFile* source)
  447. {
  448. std::string comment;
  449. const std::string language = source->GetLanguage();
  450. std::string rule = this->LanguageCompilerRule(language);
  451. cmNinjaDeps outputs;
  452. std::string objectFileName = this->GetObjectFilePath(source);
  453. outputs.push_back(objectFileName);
  454. // Add this object to the list of object files.
  455. this->Objects.push_back(objectFileName);
  456. cmNinjaDeps explicitDeps;
  457. std::string sourceFileName;
  458. if (language == "RC")
  459. sourceFileName = source->GetFullPath();
  460. else
  461. sourceFileName = this->GetSourceFilePath(source);
  462. explicitDeps.push_back(sourceFileName);
  463. // Ensure that the target dependencies are built before any source file in
  464. // the target, using order-only dependencies.
  465. cmNinjaDeps orderOnlyDeps;
  466. this->GetLocalGenerator()->AppendTargetDepends(this->Target, orderOnlyDeps);
  467. cmNinjaDeps implicitDeps;
  468. if(const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
  469. std::vector<std::string> depList;
  470. cmSystemTools::ExpandListArgument(objectDeps, depList);
  471. std::transform(depList.begin(), depList.end(),
  472. std::back_inserter(implicitDeps), MapToNinjaPath());
  473. }
  474. // Add order-only dependencies on custom command outputs.
  475. std::vector<cmSourceFile*> customCommands;
  476. this->GeneratorTarget->GetCustomCommands(customCommands);
  477. for(std::vector<cmSourceFile*>::const_iterator
  478. si = customCommands.begin();
  479. si != customCommands.end(); ++si)
  480. {
  481. cmCustomCommand const* cc = (*si)->GetCustomCommand();
  482. const std::vector<std::string>& ccoutputs = cc->GetOutputs();
  483. std::transform(ccoutputs.begin(), ccoutputs.end(),
  484. std::back_inserter(orderOnlyDeps), MapToNinjaPath());
  485. }
  486. // If the source file is GENERATED and does not have a custom command
  487. // (either attached to this source file or another one), assume that one of
  488. // the target dependencies, OBJECT_DEPENDS or header file custom commands
  489. // will rebuild the file.
  490. if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
  491. !this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
  492. this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
  493. orderOnlyDeps);
  494. }
  495. cmNinjaVars vars;
  496. vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
  497. vars["DEFINES"] = this->ComputeDefines(source, language);
  498. if (needsDepFile(language)) {
  499. vars["DEP_FILE"] =
  500. cmGlobalNinjaGenerator::EncodeDepfileSpace(objectFileName + ".d");
  501. }
  502. EnsureParentDirectoryExists(objectFileName);
  503. std::string objectDir = this->Target->GetSupportDirectory();
  504. vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
  505. ConvertToNinjaPath(objectDir.c_str()).c_str(),
  506. cmLocalGenerator::SHELL);
  507. this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetTarget(), vars);
  508. this->SetMsvcTargetPdbVariable(vars);
  509. if(this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS"))
  510. {
  511. cmLocalGenerator::RuleVariables compileObjectVars;
  512. std::string lang = language;
  513. compileObjectVars.Language = lang.c_str();
  514. std::string escapedSourceFileName = sourceFileName;
  515. if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str()))
  516. {
  517. escapedSourceFileName = cmSystemTools::CollapseFullPath(
  518. escapedSourceFileName.c_str(),
  519. this->GetGlobalGenerator()->GetCMakeInstance()->
  520. GetHomeOutputDirectory());
  521. }
  522. escapedSourceFileName =
  523. this->LocalGenerator->ConvertToOutputFormat(
  524. escapedSourceFileName.c_str(), cmLocalGenerator::SHELL);
  525. compileObjectVars.Source = escapedSourceFileName.c_str();
  526. compileObjectVars.Object = objectFileName.c_str();
  527. compileObjectVars.ObjectDir = objectDir.c_str();
  528. compileObjectVars.Flags = vars["FLAGS"].c_str();
  529. compileObjectVars.Defines = vars["DEFINES"].c_str();
  530. // Rule for compiling object file.
  531. std::string compileCmdVar = "CMAKE_";
  532. compileCmdVar += language;
  533. compileCmdVar += "_COMPILE_OBJECT";
  534. std::string compileCmd =
  535. this->GetMakefile()->GetRequiredDefinition(compileCmdVar.c_str());
  536. std::vector<std::string> compileCmds;
  537. cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
  538. for (std::vector<std::string>::iterator i = compileCmds.begin();
  539. i != compileCmds.end(); ++i)
  540. this->GetLocalGenerator()->ExpandRuleVariables(*i, compileObjectVars);
  541. std::string cmdLine =
  542. this->GetLocalGenerator()->BuildCommandLine(compileCmds);
  543. this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine,
  544. sourceFileName);
  545. }
  546. this->GetGlobalGenerator()->WriteBuild(this->GetBuildFileStream(),
  547. comment,
  548. rule,
  549. outputs,
  550. explicitDeps,
  551. implicitDeps,
  552. orderOnlyDeps,
  553. vars);
  554. if(const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
  555. std::vector<std::string> outputList;
  556. cmSystemTools::ExpandListArgument(objectOutputs, outputList);
  557. std::transform(outputList.begin(), outputList.end(), outputList.begin(),
  558. MapToNinjaPath());
  559. this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
  560. "Additional output files.",
  561. outputList,
  562. outputs);
  563. }
  564. }
  565. //----------------------------------------------------------------------------
  566. void
  567. cmNinjaTargetGenerator
  568. ::AddModuleDefinitionFlag(std::string& flags)
  569. {
  570. if(this->ModuleDefinitionFile.empty())
  571. {
  572. return;
  573. }
  574. // TODO: Create a per-language flag variable.
  575. const char* defFileFlag =
  576. this->Makefile->GetDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  577. if(!defFileFlag)
  578. {
  579. return;
  580. }
  581. // Append the flag and value. Use ConvertToLinkReference to help
  582. // vs6's "cl -link" pass it to the linker.
  583. std::string flag = defFileFlag;
  584. flag += (this->LocalGenerator->ConvertToLinkReference(
  585. this->ModuleDefinitionFile.c_str()));
  586. this->LocalGenerator->AppendFlags(flags, flag.c_str());
  587. }
  588. void
  589. cmNinjaTargetGenerator
  590. ::EnsureDirectoryExists(const std::string& path) const
  591. {
  592. if (cmSystemTools::FileIsFullPath(path.c_str()))
  593. {
  594. cmSystemTools::MakeDirectory(path.c_str());
  595. }
  596. else
  597. {
  598. const std::string fullPath = std::string(this->GetGlobalGenerator()->
  599. GetCMakeInstance()->GetHomeOutputDirectory())
  600. + "/" + path;
  601. cmSystemTools::MakeDirectory(fullPath.c_str());
  602. }
  603. }
  604. void
  605. cmNinjaTargetGenerator
  606. ::EnsureParentDirectoryExists(const std::string& path) const
  607. {
  608. EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path.c_str()));
  609. }
  610. //----------------------------------------------------------------------------
  611. void
  612. cmNinjaTargetGenerator::MacOSXContentGeneratorType::operator()(
  613. cmSourceFile& source, const char* pkgloc)
  614. {
  615. // Skip OS X content when not building a Framework or Bundle.
  616. if(!this->Generator->GetTarget()->IsBundleOnApple())
  617. {
  618. return;
  619. }
  620. std::string macdir =
  621. this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
  622. // Get the input file location.
  623. std::string input = source.GetFullPath();
  624. input =
  625. this->Generator->GetLocalGenerator()->ConvertToNinjaPath(input.c_str());
  626. // Get the output file location.
  627. std::string output = macdir;
  628. output += "/";
  629. output += cmSystemTools::GetFilenameName(input);
  630. output =
  631. this->Generator->GetLocalGenerator()->ConvertToNinjaPath(output.c_str());
  632. // Write a build statement to copy the content into the bundle.
  633. this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(input,
  634. output);
  635. // Add as a dependency of all target so that it gets called.
  636. this->Generator->GetGlobalGenerator()->AddDependencyToAll(output);
  637. }
  638. void cmNinjaTargetGenerator::addPoolNinjaVariable(const char* pool_property,
  639. cmTarget* target,
  640. cmNinjaVars& vars)
  641. {
  642. const char* pool = target->GetProperty(pool_property);
  643. if (pool)
  644. {
  645. vars["pool"] = pool;
  646. }
  647. }