cmNinjaTargetGenerator.cxx 24 KB

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