cmNinjaTargetGenerator.cxx 26 KB

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