cmNinjaTargetGenerator.cxx 24 KB

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