cmNinjaTargetGenerator.cxx 23 KB

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