cmExtraEclipseCDT4Generator.cxx 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmExtraEclipseCDT4Generator.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <cstdio>
  7. #include <map>
  8. #include <memory>
  9. #include <sstream>
  10. #include <utility>
  11. #include "cmsys/RegularExpression.hxx"
  12. #include "cmGeneratedFileStream.h"
  13. #include "cmGeneratorExpression.h"
  14. #include "cmGeneratorTarget.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmMakefile.h"
  18. #include "cmMessageType.h"
  19. #include "cmProperty.h"
  20. #include "cmSourceFile.h"
  21. #include "cmSourceGroup.h"
  22. #include "cmState.h"
  23. #include "cmStateTypes.h"
  24. #include "cmStringAlgorithms.h"
  25. #include "cmSystemTools.h"
  26. #include "cmXMLWriter.h"
  27. #include "cmake.h"
  28. static void AppendAttribute(cmXMLWriter& xml, const char* keyval)
  29. {
  30. xml.StartElement("attribute");
  31. xml.Attribute("key", keyval);
  32. xml.Attribute("value", keyval);
  33. xml.EndElement();
  34. }
  35. template <typename T>
  36. void AppendDictionary(cmXMLWriter& xml, const char* key, T const& value)
  37. {
  38. xml.StartElement("dictionary");
  39. xml.Element("key", key);
  40. xml.Element("value", value);
  41. xml.EndElement();
  42. }
  43. cmExtraEclipseCDT4Generator::cmExtraEclipseCDT4Generator()
  44. {
  45. this->IsOutOfSourceBuild = false;
  46. this->GenerateSourceProject = false;
  47. this->SupportsVirtualFolders = true;
  48. this->GenerateLinkedResources = true;
  49. this->SupportsGmakeErrorParser = true;
  50. this->SupportsMachO64Parser = true;
  51. this->CEnabled = false;
  52. this->CXXEnabled = false;
  53. }
  54. cmExternalMakefileProjectGeneratorFactory*
  55. cmExtraEclipseCDT4Generator::GetFactory()
  56. {
  57. static cmExternalMakefileProjectGeneratorSimpleFactory<
  58. cmExtraEclipseCDT4Generator>
  59. factory("Eclipse CDT4", "Generates Eclipse CDT 4.0 project files.");
  60. if (factory.GetSupportedGlobalGenerators().empty()) {
  61. // TODO: Verify if __CYGWIN__ should be checked.
  62. //#if defined(_WIN32) && !defined(__CYGWIN__)
  63. #if defined(_WIN32)
  64. factory.AddSupportedGlobalGenerator("NMake Makefiles");
  65. factory.AddSupportedGlobalGenerator("MinGW Makefiles");
  66. // factory.AddSupportedGlobalGenerator("MSYS Makefiles");
  67. #endif
  68. factory.AddSupportedGlobalGenerator("Ninja");
  69. factory.AddSupportedGlobalGenerator("Unix Makefiles");
  70. }
  71. return &factory;
  72. }
  73. void cmExtraEclipseCDT4Generator::EnableLanguage(
  74. std::vector<std::string> const& languages, cmMakefile* /*unused*/,
  75. bool /*optional*/)
  76. {
  77. for (std::string const& l : languages) {
  78. if (l == "CXX") {
  79. this->Natures.insert("org.eclipse.cdt.core.ccnature");
  80. this->Natures.insert("org.eclipse.cdt.core.cnature");
  81. this->CXXEnabled = true;
  82. } else if (l == "C") {
  83. this->Natures.insert("org.eclipse.cdt.core.cnature");
  84. this->CEnabled = true;
  85. } else if (l == "Java") {
  86. this->Natures.insert("org.eclipse.jdt.core.javanature");
  87. }
  88. }
  89. }
  90. void cmExtraEclipseCDT4Generator::Generate()
  91. {
  92. const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
  93. const cmMakefile* mf = lg->GetMakefile();
  94. std::string eclipseVersion = mf->GetSafeDefinition("CMAKE_ECLIPSE_VERSION");
  95. cmsys::RegularExpression regex(".*([0-9]+\\.[0-9]+).*");
  96. if (regex.find(eclipseVersion)) {
  97. unsigned int majorVersion = 0;
  98. unsigned int minorVersion = 0;
  99. int res =
  100. sscanf(regex.match(1).c_str(), "%u.%u", &majorVersion, &minorVersion);
  101. if (res == 2) {
  102. int version = majorVersion * 1000 + minorVersion;
  103. if (version < 3006) // 3.6 is Helios
  104. {
  105. this->SupportsVirtualFolders = false;
  106. this->SupportsMachO64Parser = false;
  107. }
  108. if (version < 3007) // 3.7 is Indigo
  109. {
  110. this->SupportsGmakeErrorParser = false;
  111. }
  112. }
  113. }
  114. // TODO: Decide if these are local or member variables
  115. this->HomeDirectory = lg->GetSourceDirectory();
  116. this->HomeOutputDirectory = lg->GetBinaryDirectory();
  117. this->GenerateLinkedResources =
  118. mf->IsOn("CMAKE_ECLIPSE_GENERATE_LINKED_RESOURCES");
  119. this->IsOutOfSourceBuild =
  120. (this->HomeDirectory != this->HomeOutputDirectory);
  121. this->GenerateSourceProject =
  122. (this->IsOutOfSourceBuild &&
  123. mf->IsOn("CMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT"));
  124. if (!this->GenerateSourceProject &&
  125. (mf->IsOn("ECLIPSE_CDT4_GENERATE_SOURCE_PROJECT"))) {
  126. mf->IssueMessage(
  127. MessageType::WARNING,
  128. "ECLIPSE_CDT4_GENERATE_SOURCE_PROJECT is set to TRUE, "
  129. "but this variable is not supported anymore since CMake 2.8.7.\n"
  130. "Enable CMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT instead.");
  131. }
  132. if (cmSystemTools::IsSubDirectory(this->HomeOutputDirectory,
  133. this->HomeDirectory)) {
  134. mf->IssueMessage(MessageType::WARNING,
  135. "The build directory is a subdirectory "
  136. "of the source directory.\n"
  137. "This is not supported well by Eclipse. It is strongly "
  138. "recommended to use a build directory which is a "
  139. "sibling of the source directory.");
  140. }
  141. // NOTE: This is not good, since it pollutes the source tree. However,
  142. // Eclipse doesn't allow CVS/SVN to work when the .project is not in
  143. // the cvs/svn root directory. Hence, this is provided as an option.
  144. if (this->GenerateSourceProject) {
  145. // create .project file in the source tree
  146. this->CreateSourceProjectFile();
  147. }
  148. // create a .project file
  149. this->CreateProjectFile();
  150. // create a .cproject file
  151. this->CreateCProjectFile();
  152. // create resource settings
  153. this->CreateSettingsResourcePrefsFile();
  154. }
  155. void cmExtraEclipseCDT4Generator::CreateSettingsResourcePrefsFile()
  156. {
  157. const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
  158. cmMakefile* mf = lg->GetMakefile();
  159. const std::string filename =
  160. this->HomeOutputDirectory + "/.settings/org.eclipse.core.resources.prefs";
  161. cmGeneratedFileStream fout(filename);
  162. if (!fout) {
  163. return;
  164. }
  165. fout << "eclipse.preferences.version=1\n";
  166. const char* encoding = mf->GetDefinition("CMAKE_ECLIPSE_RESOURCE_ENCODING");
  167. if (encoding) {
  168. fout << "encoding/<project>=" << encoding << '\n';
  169. }
  170. }
  171. void cmExtraEclipseCDT4Generator::CreateSourceProjectFile()
  172. {
  173. assert(this->HomeDirectory != this->HomeOutputDirectory);
  174. // set up the project name: <project>-Source@<baseSourcePathName>
  175. const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
  176. std::string name = cmExtraEclipseCDT4Generator::GenerateProjectName(
  177. lg->GetProjectName(), "Source",
  178. cmExtraEclipseCDT4Generator::GetPathBasename(this->HomeDirectory));
  179. const std::string filename = this->HomeDirectory + "/.project";
  180. cmGeneratedFileStream fout(filename);
  181. if (!fout) {
  182. return;
  183. }
  184. cmXMLWriter xml(fout);
  185. xml.StartDocument("UTF-8");
  186. xml.StartElement("projectDescription");
  187. xml.Element("name", name);
  188. xml.Element("comment", "");
  189. xml.Element("projects", "");
  190. xml.Element("buildSpec", "");
  191. xml.Element("natures", "");
  192. xml.StartElement("linkedResources");
  193. if (this->SupportsVirtualFolders) {
  194. this->CreateLinksToSubprojects(xml, this->HomeDirectory);
  195. this->SrcLinkedResources.clear();
  196. }
  197. xml.EndElement(); // linkedResources
  198. xml.EndElement(); // projectDescription
  199. xml.EndDocument();
  200. }
  201. void cmExtraEclipseCDT4Generator::AddEnvVar(std::ostream& out,
  202. const char* envVar,
  203. cmLocalGenerator& lg)
  204. {
  205. cmMakefile* mf = lg.GetMakefile();
  206. // get the variables from the environment and from the cache and then
  207. // figure out which one to use:
  208. std::string envVarValue;
  209. const bool envVarSet = cmSystemTools::GetEnv(envVar, envVarValue);
  210. std::string cacheEntryName = cmStrCat("CMAKE_ECLIPSE_ENVVAR_", envVar);
  211. cmProp cacheValue = lg.GetState()->GetInitializedCacheValue(cacheEntryName);
  212. // now we have both, decide which one to use
  213. std::string valueToUse;
  214. if (!envVarSet && cacheValue == nullptr) {
  215. // nothing known, do nothing
  216. valueToUse.clear();
  217. } else if (envVarSet && cacheValue == nullptr) {
  218. // The variable is in the env, but not in the cache. Use it and put it
  219. // in the cache
  220. valueToUse = envVarValue;
  221. mf->AddCacheDefinition(cacheEntryName, valueToUse, cacheEntryName.c_str(),
  222. cmStateEnums::STRING, true);
  223. mf->GetCMakeInstance()->SaveCache(lg.GetBinaryDirectory());
  224. } else if (!envVarSet && cacheValue != nullptr) {
  225. // It is already in the cache, but not in the env, so use it from the cache
  226. valueToUse = *cacheValue;
  227. } else {
  228. // It is both in the cache and in the env.
  229. // Use the version from the env. except if the value from the env is
  230. // completely contained in the value from the cache (for the case that we
  231. // now have a PATH without MSVC dirs in the env. but had the full PATH with
  232. // all MSVC dirs during the cmake run which stored the var in the cache:
  233. valueToUse = *cacheValue;
  234. if (valueToUse.find(envVarValue) == std::string::npos) {
  235. valueToUse = envVarValue;
  236. mf->AddCacheDefinition(cacheEntryName, valueToUse,
  237. cacheEntryName.c_str(), cmStateEnums::STRING,
  238. true);
  239. mf->GetCMakeInstance()->SaveCache(lg.GetBinaryDirectory());
  240. }
  241. }
  242. if (!valueToUse.empty()) {
  243. out << envVar << "=" << valueToUse << "|";
  244. }
  245. }
  246. void cmExtraEclipseCDT4Generator::CreateProjectFile()
  247. {
  248. const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
  249. cmMakefile* mf = lg->GetMakefile();
  250. const std::string filename = this->HomeOutputDirectory + "/.project";
  251. cmGeneratedFileStream fout(filename);
  252. if (!fout) {
  253. return;
  254. }
  255. std::string compilerId = mf->GetSafeDefinition("CMAKE_C_COMPILER_ID");
  256. if (compilerId.empty()) // no C compiler, try the C++ compiler:
  257. {
  258. compilerId = mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID");
  259. }
  260. cmXMLWriter xml(fout);
  261. xml.StartDocument("UTF-8");
  262. xml.StartElement("projectDescription");
  263. xml.Element("name",
  264. cmExtraEclipseCDT4Generator::GenerateProjectName(
  265. lg->GetProjectName(),
  266. mf->GetSafeDefinition("CMAKE_BUILD_TYPE"),
  267. cmExtraEclipseCDT4Generator::GetPathBasename(
  268. this->HomeOutputDirectory)));
  269. xml.Element("comment", "");
  270. xml.Element("projects", "");
  271. xml.StartElement("buildSpec");
  272. xml.StartElement("buildCommand");
  273. xml.Element("name", "org.eclipse.cdt.make.core.makeBuilder");
  274. xml.Element("triggers", "clean,full,incremental,");
  275. xml.StartElement("arguments");
  276. // use clean target
  277. AppendDictionary(xml, "org.eclipse.cdt.make.core.cleanBuildTarget", "clean");
  278. AppendDictionary(xml, "org.eclipse.cdt.make.core.enableCleanBuild", "true");
  279. AppendDictionary(xml, "org.eclipse.cdt.make.core.append_environment",
  280. "true");
  281. AppendDictionary(xml, "org.eclipse.cdt.make.core.stopOnError", "true");
  282. // set the make command
  283. AppendDictionary(xml, "org.eclipse.cdt.make.core.enabledIncrementalBuild",
  284. "true");
  285. AppendDictionary(xml, "org.eclipse.cdt.make.core.build.command",
  286. cmExtraEclipseCDT4Generator::GetEclipsePath(
  287. mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM")));
  288. AppendDictionary(xml, "org.eclipse.cdt.make.core.contents",
  289. "org.eclipse.cdt.make.core.activeConfigSettings");
  290. AppendDictionary(xml, "org.eclipse.cdt.make.core.build.target.inc", "all");
  291. AppendDictionary(xml, "org.eclipse.cdt.make.core.build.arguments",
  292. mf->GetSafeDefinition("CMAKE_ECLIPSE_MAKE_ARGUMENTS"));
  293. AppendDictionary(
  294. xml, "org.eclipse.cdt.make.core.buildLocation",
  295. cmExtraEclipseCDT4Generator::GetEclipsePath(this->HomeOutputDirectory));
  296. AppendDictionary(xml, "org.eclipse.cdt.make.core.useDefaultBuildCmd",
  297. "false");
  298. // set project specific environment
  299. std::ostringstream environment;
  300. environment << "VERBOSE=1|CMAKE_NO_VERBOSE=1|"; // verbose Makefile output
  301. // set vsvars32.bat environment available at CMake time,
  302. // but not necessarily when eclipse is open
  303. if (compilerId == "MSVC") {
  304. AddEnvVar(environment, "PATH", *lg);
  305. AddEnvVar(environment, "INCLUDE", *lg);
  306. AddEnvVar(environment, "LIB", *lg);
  307. AddEnvVar(environment, "LIBPATH", *lg);
  308. } else if (compilerId == "Intel") {
  309. // if the env.var is set, use this one and put it in the cache
  310. // if the env.var is not set, but the value is in the cache,
  311. // use it from the cache:
  312. AddEnvVar(environment, "INTEL_LICENSE_FILE", *lg);
  313. }
  314. AppendDictionary(xml, "org.eclipse.cdt.make.core.environment",
  315. environment.str());
  316. AppendDictionary(xml, "org.eclipse.cdt.make.core.enableFullBuild", "true");
  317. AppendDictionary(xml, "org.eclipse.cdt.make.core.build.target.auto", "all");
  318. AppendDictionary(xml, "org.eclipse.cdt.make.core.enableAutoBuild", "false");
  319. AppendDictionary(xml, "org.eclipse.cdt.make.core.build.target.clean",
  320. "clean");
  321. AppendDictionary(xml, "org.eclipse.cdt.make.core.fullBuildTarget", "all");
  322. AppendDictionary(xml, "org.eclipse.cdt.make.core.buildArguments", "");
  323. AppendDictionary(
  324. xml, "org.eclipse.cdt.make.core.build.location",
  325. cmExtraEclipseCDT4Generator::GetEclipsePath(this->HomeOutputDirectory));
  326. AppendDictionary(xml, "org.eclipse.cdt.make.core.autoBuildTarget", "all");
  327. // set error parsers
  328. std::ostringstream errorOutputParser;
  329. if (compilerId == "MSVC") {
  330. errorOutputParser << "org.eclipse.cdt.core.VCErrorParser;";
  331. } else if (compilerId == "Intel") {
  332. errorOutputParser << "org.eclipse.cdt.core.ICCErrorParser;";
  333. }
  334. if (this->SupportsGmakeErrorParser) {
  335. errorOutputParser << "org.eclipse.cdt.core.GmakeErrorParser;";
  336. } else {
  337. errorOutputParser << "org.eclipse.cdt.core.MakeErrorParser;";
  338. }
  339. errorOutputParser << "org.eclipse.cdt.core.GCCErrorParser;"
  340. "org.eclipse.cdt.core.GASErrorParser;"
  341. "org.eclipse.cdt.core.GLDErrorParser;";
  342. AppendDictionary(xml, "org.eclipse.cdt.core.errorOutputParser",
  343. errorOutputParser.str());
  344. xml.EndElement(); // arguments
  345. xml.EndElement(); // buildCommand
  346. xml.StartElement("buildCommand");
  347. xml.Element("name", "org.eclipse.cdt.make.core.ScannerConfigBuilder");
  348. xml.StartElement("arguments");
  349. xml.EndElement(); // arguments
  350. xml.EndElement(); // buildCommand
  351. xml.EndElement(); // buildSpec
  352. // set natures for c/c++ projects
  353. xml.StartElement("natures");
  354. xml.Element("nature", "org.eclipse.cdt.make.core.makeNature");
  355. xml.Element("nature", "org.eclipse.cdt.make.core.ScannerConfigNature");
  356. for (std::string const& n : this->Natures) {
  357. xml.Element("nature", n);
  358. }
  359. if (cmProp extraNaturesProp =
  360. mf->GetState()->GetGlobalProperty("ECLIPSE_EXTRA_NATURES")) {
  361. std::vector<std::string> extraNatures = cmExpandedList(*extraNaturesProp);
  362. for (std::string const& n : extraNatures) {
  363. xml.Element("nature", n);
  364. }
  365. }
  366. xml.EndElement(); // natures
  367. xml.StartElement("linkedResources");
  368. // create linked resources
  369. if (this->IsOutOfSourceBuild) {
  370. // create a linked resource to CMAKE_SOURCE_DIR
  371. // (this is not done anymore for each project because of
  372. // https://gitlab.kitware.com/cmake/cmake/-/issues/9978 and because I found
  373. // it actually quite confusing in bigger projects with many directories and
  374. // projects, Alex
  375. std::string sourceLinkedResourceName = "[Source directory]";
  376. std::string linkSourceDirectory =
  377. cmExtraEclipseCDT4Generator::GetEclipsePath(
  378. lg->GetCurrentSourceDirectory());
  379. // .project dir can't be subdir of a linked resource dir
  380. if (!cmSystemTools::IsSubDirectory(this->HomeOutputDirectory,
  381. linkSourceDirectory)) {
  382. cmExtraEclipseCDT4Generator::AppendLinkedResource(
  383. xml, sourceLinkedResourceName,
  384. cmExtraEclipseCDT4Generator::GetEclipsePath(linkSourceDirectory),
  385. LinkToFolder);
  386. this->SrcLinkedResources.push_back(std::move(sourceLinkedResourceName));
  387. }
  388. }
  389. if (this->SupportsVirtualFolders) {
  390. this->CreateLinksToSubprojects(xml, this->HomeOutputDirectory);
  391. this->CreateLinksForTargets(xml);
  392. }
  393. xml.EndElement(); // linkedResources
  394. xml.EndElement(); // projectDescription
  395. }
  396. void cmExtraEclipseCDT4Generator::WriteGroups(
  397. std::vector<cmSourceGroup> const& sourceGroups, std::string& linkName,
  398. cmXMLWriter& xml)
  399. {
  400. for (cmSourceGroup const& sg : sourceGroups) {
  401. std::string linkName3 = cmStrCat(linkName, '/', sg.GetFullName());
  402. std::replace(linkName3.begin(), linkName3.end(), '\\', '/');
  403. cmExtraEclipseCDT4Generator::AppendLinkedResource(
  404. xml, linkName3, "virtual:/virtual", VirtualFolder);
  405. std::vector<cmSourceGroup> const& children = sg.GetGroupChildren();
  406. if (!children.empty()) {
  407. this->WriteGroups(children, linkName, xml);
  408. }
  409. std::vector<const cmSourceFile*> sFiles = sg.GetSourceFiles();
  410. for (cmSourceFile const* file : sFiles) {
  411. std::string const& fullPath = file->GetFullPath();
  412. if (!cmSystemTools::FileIsDirectory(fullPath)) {
  413. std::string linkName4 =
  414. cmStrCat(linkName3, '/', cmSystemTools::GetFilenameName(fullPath));
  415. cmExtraEclipseCDT4Generator::AppendLinkedResource(
  416. xml, linkName4,
  417. cmExtraEclipseCDT4Generator::GetEclipsePath(fullPath), LinkToFile);
  418. }
  419. }
  420. }
  421. }
  422. void cmExtraEclipseCDT4Generator::CreateLinksForTargets(cmXMLWriter& xml)
  423. {
  424. std::string linkName = "[Targets]";
  425. cmExtraEclipseCDT4Generator::AppendLinkedResource(
  426. xml, linkName, "virtual:/virtual", VirtualFolder);
  427. for (const auto& lg : this->GlobalGenerator->GetLocalGenerators()) {
  428. cmMakefile* makefile = lg->GetMakefile();
  429. const auto& targets = lg->GetGeneratorTargets();
  430. for (const auto& target : targets) {
  431. std::string linkName2 = cmStrCat(linkName, '/');
  432. switch (target->GetType()) {
  433. case cmStateEnums::EXECUTABLE:
  434. case cmStateEnums::STATIC_LIBRARY:
  435. case cmStateEnums::SHARED_LIBRARY:
  436. case cmStateEnums::MODULE_LIBRARY:
  437. case cmStateEnums::OBJECT_LIBRARY: {
  438. const char* prefix =
  439. (target->GetType() == cmStateEnums::EXECUTABLE ? "[exe] "
  440. : "[lib] ");
  441. linkName2 += prefix;
  442. linkName2 += target->GetName();
  443. cmExtraEclipseCDT4Generator::AppendLinkedResource(
  444. xml, linkName2, "virtual:/virtual", VirtualFolder);
  445. if (!this->GenerateLinkedResources) {
  446. break; // skip generating the linked resources to the source files
  447. }
  448. std::vector<cmSourceGroup> sourceGroups =
  449. makefile->GetSourceGroups();
  450. // get the files from the source lists then add them to the groups
  451. std::vector<cmSourceFile*> files;
  452. target->GetSourceFiles(
  453. files, makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  454. for (cmSourceFile* sf : files) {
  455. // Add the file to the list of sources.
  456. std::string const& source = sf->ResolveFullPath();
  457. cmSourceGroup* sourceGroup =
  458. makefile->FindSourceGroup(source, sourceGroups);
  459. sourceGroup->AssignSource(sf);
  460. }
  461. this->WriteGroups(sourceGroups, linkName2, xml);
  462. } break;
  463. // ignore all others:
  464. default:
  465. break;
  466. }
  467. }
  468. }
  469. }
  470. void cmExtraEclipseCDT4Generator::CreateLinksToSubprojects(
  471. cmXMLWriter& xml, const std::string& baseDir)
  472. {
  473. if (!this->GenerateLinkedResources) {
  474. return;
  475. }
  476. // for each sub project create a linked resource to the source dir
  477. // - only if it is an out-of-source build
  478. cmExtraEclipseCDT4Generator::AppendLinkedResource(
  479. xml, "[Subprojects]", "virtual:/virtual", VirtualFolder);
  480. for (auto const& it : this->GlobalGenerator->GetProjectMap()) {
  481. std::string linkSourceDirectory =
  482. cmExtraEclipseCDT4Generator::GetEclipsePath(
  483. it.second[0]->GetCurrentSourceDirectory());
  484. // a linked resource must not point to a parent directory of .project or
  485. // .project itself
  486. if ((baseDir != linkSourceDirectory) &&
  487. !cmSystemTools::IsSubDirectory(baseDir, linkSourceDirectory)) {
  488. std::string linkName = cmStrCat("[Subprojects]/", it.first);
  489. cmExtraEclipseCDT4Generator::AppendLinkedResource(
  490. xml, linkName,
  491. cmExtraEclipseCDT4Generator::GetEclipsePath(linkSourceDirectory),
  492. LinkToFolder);
  493. // Don't add it to the srcLinkedResources, because listing multiple
  494. // directories confuses the Eclipse indexer (#13596).
  495. }
  496. }
  497. }
  498. void cmExtraEclipseCDT4Generator::AppendIncludeDirectories(
  499. cmXMLWriter& xml, const std::vector<std::string>& includeDirs,
  500. std::set<std::string>& emittedDirs)
  501. {
  502. for (std::string const& inc : includeDirs) {
  503. if (!inc.empty()) {
  504. std::string dir = cmSystemTools::CollapseFullPath(inc);
  505. // handle framework include dirs on OSX, the remainder after the
  506. // Frameworks/ part has to be stripped
  507. // /System/Library/Frameworks/GLUT.framework/Headers
  508. cmsys::RegularExpression frameworkRx("(.+/Frameworks)/.+\\.framework/");
  509. if (frameworkRx.find(dir)) {
  510. dir = frameworkRx.match(1);
  511. }
  512. if (emittedDirs.find(dir) == emittedDirs.end()) {
  513. emittedDirs.insert(dir);
  514. xml.StartElement("pathentry");
  515. xml.Attribute("include",
  516. cmExtraEclipseCDT4Generator::GetEclipsePath(dir));
  517. xml.Attribute("kind", "inc");
  518. xml.Attribute("path", "");
  519. xml.Attribute("system", "true");
  520. xml.EndElement();
  521. }
  522. }
  523. }
  524. }
  525. void cmExtraEclipseCDT4Generator::CreateCProjectFile() const
  526. {
  527. std::set<std::string> emmited;
  528. const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
  529. const cmMakefile* mf = lg->GetMakefile();
  530. const std::string filename = this->HomeOutputDirectory + "/.cproject";
  531. cmGeneratedFileStream fout(filename);
  532. if (!fout) {
  533. return;
  534. }
  535. cmXMLWriter xml(fout);
  536. // add header
  537. xml.StartDocument("UTF-8");
  538. xml.ProcessingInstruction("fileVersion", "4.0.0");
  539. xml.StartElement("cproject");
  540. xml.StartElement("storageModule");
  541. xml.Attribute("moduleId", "org.eclipse.cdt.core.settings");
  542. xml.StartElement("cconfiguration");
  543. xml.Attribute("id", "org.eclipse.cdt.core.default.config.1");
  544. // Configuration settings...
  545. xml.StartElement("storageModule");
  546. xml.Attribute("buildSystemId",
  547. "org.eclipse.cdt.core.defaultConfigDataProvider");
  548. xml.Attribute("id", "org.eclipse.cdt.core.default.config.1");
  549. xml.Attribute("moduleId", "org.eclipse.cdt.core.settings");
  550. xml.Attribute("name", "Configuration");
  551. xml.Element("externalSettings");
  552. xml.StartElement("extensions");
  553. // TODO: refactor this out...
  554. std::string executableFormat =
  555. mf->GetSafeDefinition("CMAKE_EXECUTABLE_FORMAT");
  556. if (executableFormat == "ELF") {
  557. xml.StartElement("extension");
  558. xml.Attribute("id", "org.eclipse.cdt.core.ELF");
  559. xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
  560. xml.EndElement(); // extension
  561. xml.StartElement("extension");
  562. xml.Attribute("id", "org.eclipse.cdt.core.GNU_ELF");
  563. xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
  564. AppendAttribute(xml, "addr2line");
  565. AppendAttribute(xml, "c++filt");
  566. xml.EndElement(); // extension
  567. } else {
  568. std::string systemName = mf->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  569. if (systemName == "CYGWIN") {
  570. xml.StartElement("extension");
  571. xml.Attribute("id", "org.eclipse.cdt.core.Cygwin_PE");
  572. xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
  573. AppendAttribute(xml, "addr2line");
  574. AppendAttribute(xml, "c++filt");
  575. AppendAttribute(xml, "cygpath");
  576. AppendAttribute(xml, "nm");
  577. xml.EndElement(); // extension
  578. } else if (systemName == "Windows") {
  579. xml.StartElement("extension");
  580. xml.Attribute("id", "org.eclipse.cdt.core.PE");
  581. xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
  582. xml.EndElement(); // extension
  583. } else if (systemName == "Darwin") {
  584. xml.StartElement("extension");
  585. xml.Attribute("id",
  586. this->SupportsMachO64Parser
  587. ? "org.eclipse.cdt.core.MachO64"
  588. : "org.eclipse.cdt.core.MachO");
  589. xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
  590. AppendAttribute(xml, "c++filt");
  591. xml.EndElement(); // extension
  592. } else {
  593. // *** Should never get here ***
  594. xml.Element("error_toolchain_type");
  595. }
  596. }
  597. xml.EndElement(); // extensions
  598. xml.EndElement(); // storageModule
  599. // ???
  600. xml.StartElement("storageModule");
  601. xml.Attribute("moduleId", "org.eclipse.cdt.core.language.mapping");
  602. xml.Element("project-mappings");
  603. xml.EndElement(); // storageModule
  604. // ???
  605. xml.StartElement("storageModule");
  606. xml.Attribute("moduleId", "org.eclipse.cdt.core.externalSettings");
  607. xml.EndElement(); // storageModule
  608. // set the path entries (includes, libs, source dirs, etc.)
  609. xml.StartElement("storageModule");
  610. xml.Attribute("moduleId", "org.eclipse.cdt.core.pathentry");
  611. // for each sub project with a linked resource to the source dir:
  612. // - make it type 'src'
  613. // - and exclude it from type 'out'
  614. std::string excludeFromOut;
  615. /* I don't know what the pathentry kind="src" are good for, e.g.
  616. * autocompletion
  617. * works also without them. Done wrong, the indexer complains, see #12417
  618. * and #12213.
  619. * According to #13596, this entry at least limits the directories the
  620. * indexer is searching for files. So now the "src" entry contains only
  621. * the linked resource to CMAKE_SOURCE_DIR.
  622. * The CDT documentation is very terse on that:
  623. * "CDT_SOURCE: Entry kind constant describing a path entry identifying a
  624. * folder containing source code to be compiled."
  625. * Also on the cdt-dev list didn't bring any information:
  626. * http://web.archiveorange.com/archive/v/B4NlJDNIpYoOS1SbxFNy
  627. * Alex */
  628. // include subprojects directory to the src pathentry
  629. // eclipse cdt indexer uses this entries as reference to index source files
  630. if (this->GenerateLinkedResources) {
  631. xml.StartElement("pathentry");
  632. xml.Attribute("kind", "src");
  633. xml.Attribute("path", "[Subprojects]");
  634. xml.EndElement();
  635. }
  636. for (std::string const& p : this->SrcLinkedResources) {
  637. xml.StartElement("pathentry");
  638. xml.Attribute("kind", "src");
  639. xml.Attribute("path", p);
  640. xml.EndElement();
  641. // exclude source directory from output search path
  642. // - only if not named the same as an output directory
  643. if (!cmSystemTools::FileIsDirectory(
  644. std::string(this->HomeOutputDirectory + "/" + p))) {
  645. excludeFromOut += p + "/|";
  646. }
  647. }
  648. excludeFromOut += "**/CMakeFiles/";
  649. xml.StartElement("pathentry");
  650. xml.Attribute("excluding", excludeFromOut);
  651. xml.Attribute("kind", "out");
  652. xml.Attribute("path", "");
  653. xml.EndElement();
  654. // add pre-processor definitions to allow eclipse to gray out sections
  655. emmited.clear();
  656. for (const auto& lgen : this->GlobalGenerator->GetLocalGenerators()) {
  657. if (cmProp cdefs =
  658. lgen->GetMakefile()->GetProperty("COMPILE_DEFINITIONS")) {
  659. // Expand the list.
  660. std::vector<std::string> defs;
  661. cmGeneratorExpression::Split(*cdefs, defs);
  662. for (std::string const& d : defs) {
  663. if (cmGeneratorExpression::Find(d) != std::string::npos) {
  664. continue;
  665. }
  666. std::string::size_type equals = d.find('=', 0);
  667. std::string::size_type enddef = d.length();
  668. std::string def;
  669. std::string val;
  670. if (equals != std::string::npos && equals < enddef) {
  671. // we have -DFOO=BAR
  672. def = d.substr(0, equals);
  673. val = d.substr(equals + 1, enddef - equals + 1);
  674. } else {
  675. // we have -DFOO
  676. def = d;
  677. }
  678. // insert the definition if not already added.
  679. if (emmited.find(def) == emmited.end()) {
  680. emmited.insert(def);
  681. xml.StartElement("pathentry");
  682. xml.Attribute("kind", "mac");
  683. xml.Attribute("name", def);
  684. xml.Attribute("path", "");
  685. xml.Attribute("value", val);
  686. xml.EndElement();
  687. }
  688. }
  689. }
  690. }
  691. // add system defined c macros
  692. const char* cDefs =
  693. mf->GetDefinition("CMAKE_EXTRA_GENERATOR_C_SYSTEM_DEFINED_MACROS");
  694. if (this->CEnabled && cDefs) {
  695. // Expand the list.
  696. std::vector<std::string> defs = cmExpandedList(cDefs, true);
  697. // the list must contain only definition-value pairs:
  698. if ((defs.size() % 2) == 0) {
  699. auto di = defs.begin();
  700. while (di != defs.end()) {
  701. std::string def = *di;
  702. ++di;
  703. std::string val;
  704. if (di != defs.end()) {
  705. val = *di;
  706. ++di;
  707. }
  708. // insert the definition if not already added.
  709. if (emmited.find(def) == emmited.end()) {
  710. emmited.insert(def);
  711. xml.StartElement("pathentry");
  712. xml.Attribute("kind", "mac");
  713. xml.Attribute("name", def);
  714. xml.Attribute("path", "");
  715. xml.Attribute("value", val);
  716. xml.EndElement();
  717. }
  718. }
  719. }
  720. }
  721. // add system defined c++ macros
  722. const char* cxxDefs =
  723. mf->GetDefinition("CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS");
  724. if (this->CXXEnabled && cxxDefs) {
  725. // Expand the list.
  726. std::vector<std::string> defs = cmExpandedList(cxxDefs, true);
  727. // the list must contain only definition-value pairs:
  728. if ((defs.size() % 2) == 0) {
  729. auto di = defs.begin();
  730. while (di != defs.end()) {
  731. std::string def = *di;
  732. ++di;
  733. std::string val;
  734. if (di != defs.end()) {
  735. val = *di;
  736. ++di;
  737. }
  738. // insert the definition if not already added.
  739. if (emmited.find(def) == emmited.end()) {
  740. emmited.insert(def);
  741. xml.StartElement("pathentry");
  742. xml.Attribute("kind", "mac");
  743. xml.Attribute("name", def);
  744. xml.Attribute("path", "");
  745. xml.Attribute("value", val);
  746. xml.EndElement();
  747. }
  748. }
  749. }
  750. }
  751. // include dirs
  752. emmited.clear();
  753. for (const auto& lgen : this->GlobalGenerator->GetLocalGenerators()) {
  754. const auto& targets = lgen->GetGeneratorTargets();
  755. for (const auto& target : targets) {
  756. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  757. continue;
  758. }
  759. std::vector<std::string> includeDirs;
  760. std::string config = mf->GetSafeDefinition("CMAKE_BUILD_TYPE");
  761. lgen->GetIncludeDirectories(includeDirs, target.get(), "C", config);
  762. this->AppendIncludeDirectories(xml, includeDirs, emmited);
  763. }
  764. }
  765. // now also the system include directories, in case we found them in
  766. // CMakeSystemSpecificInformation.cmake. This makes Eclipse find the
  767. // standard headers.
  768. std::string compiler = mf->GetSafeDefinition("CMAKE_C_COMPILER");
  769. if (this->CEnabled && !compiler.empty()) {
  770. std::string systemIncludeDirs =
  771. mf->GetSafeDefinition("CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
  772. std::vector<std::string> dirs = cmExpandedList(systemIncludeDirs);
  773. this->AppendIncludeDirectories(xml, dirs, emmited);
  774. }
  775. compiler = mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  776. if (this->CXXEnabled && !compiler.empty()) {
  777. std::string systemIncludeDirs =
  778. mf->GetSafeDefinition("CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
  779. std::vector<std::string> dirs = cmExpandedList(systemIncludeDirs);
  780. this->AppendIncludeDirectories(xml, dirs, emmited);
  781. }
  782. xml.EndElement(); // storageModule
  783. // add build targets
  784. xml.StartElement("storageModule");
  785. xml.Attribute("moduleId", "org.eclipse.cdt.make.core.buildtargets");
  786. xml.StartElement("buildTargets");
  787. emmited.clear();
  788. const std::string& make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  789. const std::string& makeArgs =
  790. mf->GetSafeDefinition("CMAKE_ECLIPSE_MAKE_ARGUMENTS");
  791. cmGlobalGenerator* generator =
  792. const_cast<cmGlobalGenerator*>(this->GlobalGenerator);
  793. std::string allTarget;
  794. std::string cleanTarget;
  795. if (generator->GetAllTargetName()) {
  796. allTarget = generator->GetAllTargetName();
  797. }
  798. if (generator->GetCleanTargetName()) {
  799. cleanTarget = generator->GetCleanTargetName();
  800. }
  801. // add all executable and library targets and some of the GLOBAL
  802. // and UTILITY targets
  803. for (const auto& lgen : this->GlobalGenerator->GetLocalGenerators()) {
  804. const auto& targets = lgen->GetGeneratorTargets();
  805. std::string subdir = lgen->MaybeConvertToRelativePath(
  806. this->HomeOutputDirectory, lgen->GetCurrentBinaryDirectory());
  807. if (subdir == ".") {
  808. subdir.clear();
  809. }
  810. for (const auto& target : targets) {
  811. std::string targetName = target->GetName();
  812. switch (target->GetType()) {
  813. case cmStateEnums::GLOBAL_TARGET: {
  814. // Only add the global targets from CMAKE_BINARY_DIR,
  815. // not from the subdirs
  816. if (subdir.empty()) {
  817. cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
  818. makeArgs, subdir, ": ");
  819. }
  820. } break;
  821. case cmStateEnums::UTILITY:
  822. // Add all utility targets, except the Nightly/Continuous/
  823. // Experimental-"sub"targets as e.g. NightlyStart
  824. if ((cmHasLiteralPrefix(targetName, "Nightly") &&
  825. (targetName != "Nightly")) ||
  826. (cmHasLiteralPrefix(targetName, "Continuous") &&
  827. (targetName != "Continuous")) ||
  828. (cmHasLiteralPrefix(targetName, "Experimental") &&
  829. (targetName != "Experimental"))) {
  830. break;
  831. }
  832. cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
  833. makeArgs, subdir, ": ");
  834. break;
  835. case cmStateEnums::EXECUTABLE:
  836. case cmStateEnums::STATIC_LIBRARY:
  837. case cmStateEnums::SHARED_LIBRARY:
  838. case cmStateEnums::MODULE_LIBRARY:
  839. case cmStateEnums::OBJECT_LIBRARY: {
  840. const char* prefix =
  841. (target->GetType() == cmStateEnums::EXECUTABLE ? "[exe] "
  842. : "[lib] ");
  843. cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
  844. makeArgs, subdir, prefix);
  845. std::string fastTarget = cmStrCat(targetName, "/fast");
  846. cmExtraEclipseCDT4Generator::AppendTarget(xml, fastTarget, make,
  847. makeArgs, subdir, prefix);
  848. // Add Build and Clean targets in the virtual folder of targets:
  849. if (this->SupportsVirtualFolders) {
  850. std::string virtDir = cmStrCat("[Targets]/", prefix, targetName);
  851. std::string buildArgs =
  852. cmStrCat("-C \"", lgen->GetBinaryDirectory(), "\" ", makeArgs);
  853. cmExtraEclipseCDT4Generator::AppendTarget(
  854. xml, "Build", make, buildArgs, virtDir, "", targetName.c_str());
  855. std::string cleanArgs =
  856. cmStrCat("-E chdir \"", lgen->GetCurrentBinaryDirectory(),
  857. "\" \"", cmSystemTools::GetCMakeCommand(), "\" -P \"");
  858. cleanArgs += lgen->GetTargetDirectory(target.get());
  859. cleanArgs += "/cmake_clean.cmake\"";
  860. cmExtraEclipseCDT4Generator::AppendTarget(
  861. xml, "Clean", cmSystemTools::GetCMakeCommand(), cleanArgs,
  862. virtDir, "", "");
  863. }
  864. } break;
  865. case cmStateEnums::INTERFACE_LIBRARY:
  866. default:
  867. break;
  868. }
  869. }
  870. // insert the all and clean targets in every subdir
  871. if (!allTarget.empty()) {
  872. cmExtraEclipseCDT4Generator::AppendTarget(xml, allTarget, make, makeArgs,
  873. subdir, ": ");
  874. }
  875. if (!cleanTarget.empty()) {
  876. cmExtraEclipseCDT4Generator::AppendTarget(xml, cleanTarget, make,
  877. makeArgs, subdir, ": ");
  878. }
  879. // insert rules for compiling, preprocessing and assembling individual
  880. // files
  881. std::vector<std::string> objectFileTargets;
  882. lg->GetIndividualFileTargets(objectFileTargets);
  883. for (std::string const& f : objectFileTargets) {
  884. const char* prefix = "[obj] ";
  885. if (f.back() == 's') {
  886. prefix = "[to asm] ";
  887. } else if (f.back() == 'i') {
  888. prefix = "[pre] ";
  889. }
  890. cmExtraEclipseCDT4Generator::AppendTarget(xml, f, make, makeArgs, subdir,
  891. prefix);
  892. }
  893. }
  894. xml.EndElement(); // buildTargets
  895. xml.EndElement(); // storageModule
  896. cmExtraEclipseCDT4Generator::AppendStorageScanners(xml, *mf);
  897. xml.EndElement(); // cconfiguration
  898. xml.EndElement(); // storageModule
  899. xml.StartElement("storageModule");
  900. xml.Attribute("moduleId", "cdtBuildSystem");
  901. xml.Attribute("version", "4.0.0");
  902. xml.StartElement("project");
  903. xml.Attribute("id", std::string(lg->GetProjectName()) + ".null.1");
  904. xml.Attribute("name", lg->GetProjectName());
  905. xml.EndElement(); // project
  906. xml.EndElement(); // storageModule
  907. // Append additional cproject contents without applying any XML formatting
  908. if (cmProp extraCProjectContents =
  909. mf->GetState()->GetGlobalProperty("ECLIPSE_EXTRA_CPROJECT_CONTENTS")) {
  910. fout << *extraCProjectContents;
  911. }
  912. xml.EndElement(); // cproject
  913. }
  914. std::string cmExtraEclipseCDT4Generator::GetEclipsePath(
  915. const std::string& path)
  916. {
  917. #if defined(__CYGWIN__)
  918. std::string cmd = "cygpath -m " + path;
  919. std::string out;
  920. if (!cmSystemTools::RunSingleCommand(cmd.c_str(), &out, &out)) {
  921. return path;
  922. } else {
  923. out.erase(out.find_last_of('\n'));
  924. return out;
  925. }
  926. #else
  927. return path;
  928. #endif
  929. }
  930. std::string cmExtraEclipseCDT4Generator::GetPathBasename(
  931. const std::string& path)
  932. {
  933. std::string outputBasename = path;
  934. while (!outputBasename.empty() &&
  935. (outputBasename.back() == '/' || outputBasename.back() == '\\')) {
  936. outputBasename.resize(outputBasename.size() - 1);
  937. }
  938. std::string::size_type loc = outputBasename.find_last_of("/\\");
  939. if (loc != std::string::npos) {
  940. outputBasename = outputBasename.substr(loc + 1);
  941. }
  942. return outputBasename;
  943. }
  944. std::string cmExtraEclipseCDT4Generator::GenerateProjectName(
  945. const std::string& name, const std::string& type, const std::string& path)
  946. {
  947. return name + (type.empty() ? "" : "-") + type + "@" + path;
  948. }
  949. // Helper functions
  950. void cmExtraEclipseCDT4Generator::AppendStorageScanners(
  951. cmXMLWriter& xml, const cmMakefile& makefile)
  952. {
  953. // we need the "make" and the C (or C++) compiler which are used, Alex
  954. const std::string& make =
  955. makefile.GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  956. std::string compiler = makefile.GetSafeDefinition("CMAKE_C_COMPILER");
  957. std::string arg1 = makefile.GetSafeDefinition("CMAKE_C_COMPILER_ARG1");
  958. if (compiler.empty()) {
  959. compiler = makefile.GetSafeDefinition("CMAKE_CXX_COMPILER");
  960. arg1 = makefile.GetSafeDefinition("CMAKE_CXX_COMPILER_ARG1");
  961. }
  962. if (compiler.empty()) // Hmm, what to do now ?
  963. {
  964. compiler = "gcc";
  965. }
  966. // the following right now hardcodes gcc behaviour :-/
  967. std::string compilerArgs =
  968. "-E -P -v -dD ${plugin_state_location}/${specs_file}";
  969. if (!arg1.empty()) {
  970. arg1 += " ";
  971. compilerArgs = arg1 + compilerArgs;
  972. }
  973. xml.StartElement("storageModule");
  974. xml.Attribute("moduleId", "scannerConfiguration");
  975. xml.StartElement("autodiscovery");
  976. xml.Attribute("enabled", "true");
  977. xml.Attribute("problemReportingEnabled", "true");
  978. xml.Attribute("selectedProfileId",
  979. "org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile");
  980. xml.EndElement(); // autodiscovery
  981. cmExtraEclipseCDT4Generator::AppendScannerProfile(
  982. xml, "org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile", true,
  983. "", true, "specsFile", compilerArgs, compiler, true, true);
  984. cmExtraEclipseCDT4Generator::AppendScannerProfile(
  985. xml, "org.eclipse.cdt.make.core.GCCStandardMakePerFileProfile", true, "",
  986. true, "makefileGenerator", "-f ${project_name}_scd.mk", make, true, true);
  987. xml.EndElement(); // storageModule
  988. }
  989. // The prefix is prepended before the actual name of the target. The purpose
  990. // of that is to sort the targets in the view of Eclipse, so that at first
  991. // the global/utility/all/clean targets appear ": ", then the executable
  992. // targets "[exe] ", then the libraries "[lib]", then the rules for the
  993. // object files "[obj]", then for preprocessing only "[pre] " and
  994. // finally the assembly files "[to asm] ". Note the "to" in "to asm",
  995. // without it, "asm" would be the first targets in the list, with the "to"
  996. // they are the last targets, which makes more sense.
  997. void cmExtraEclipseCDT4Generator::AppendTarget(
  998. cmXMLWriter& xml, const std::string& target, const std::string& make,
  999. const std::string& makeArgs, const std::string& path, const char* prefix,
  1000. const char* makeTarget)
  1001. {
  1002. xml.StartElement("target");
  1003. xml.Attribute("name", prefix + target);
  1004. xml.Attribute("path", path);
  1005. xml.Attribute("targetID", "org.eclipse.cdt.make.MakeTargetBuilder");
  1006. xml.Element("buildCommand",
  1007. cmExtraEclipseCDT4Generator::GetEclipsePath(make));
  1008. xml.Element("buildArguments", makeArgs);
  1009. xml.Element("buildTarget", makeTarget ? makeTarget : target.c_str());
  1010. xml.Element("stopOnError", "true");
  1011. xml.Element("useDefaultCommand", "false");
  1012. xml.EndElement();
  1013. }
  1014. void cmExtraEclipseCDT4Generator::AppendScannerProfile(
  1015. cmXMLWriter& xml, const std::string& profileID, bool openActionEnabled,
  1016. const std::string& openActionFilePath, bool pParserEnabled,
  1017. const std::string& scannerInfoProviderID,
  1018. const std::string& runActionArguments, const std::string& runActionCommand,
  1019. bool runActionUseDefault, bool sipParserEnabled)
  1020. {
  1021. xml.StartElement("profile");
  1022. xml.Attribute("id", profileID);
  1023. xml.StartElement("buildOutputProvider");
  1024. xml.StartElement("openAction");
  1025. xml.Attribute("enabled", openActionEnabled ? "true" : "false");
  1026. xml.Attribute("filePath", openActionFilePath);
  1027. xml.EndElement(); // openAction
  1028. xml.StartElement("parser");
  1029. xml.Attribute("enabled", pParserEnabled ? "true" : "false");
  1030. xml.EndElement(); // parser
  1031. xml.EndElement(); // buildOutputProvider
  1032. xml.StartElement("scannerInfoProvider");
  1033. xml.Attribute("id", scannerInfoProviderID);
  1034. xml.StartElement("runAction");
  1035. xml.Attribute("arguments", runActionArguments);
  1036. xml.Attribute("command", runActionCommand);
  1037. xml.Attribute("useDefault", runActionUseDefault ? "true" : "false");
  1038. xml.EndElement(); // runAction
  1039. xml.StartElement("parser");
  1040. xml.Attribute("enabled", sipParserEnabled ? "true" : "false");
  1041. xml.EndElement(); // parser
  1042. xml.EndElement(); // scannerInfoProvider
  1043. xml.EndElement(); // profile
  1044. }
  1045. void cmExtraEclipseCDT4Generator::AppendLinkedResource(cmXMLWriter& xml,
  1046. const std::string& name,
  1047. const std::string& path,
  1048. LinkType linkType)
  1049. {
  1050. const char* locationTag = "location";
  1051. int typeTag = 2;
  1052. if (linkType == VirtualFolder) // ... and not a linked folder
  1053. {
  1054. locationTag = "locationURI";
  1055. }
  1056. if (linkType == LinkToFile) {
  1057. typeTag = 1;
  1058. }
  1059. xml.StartElement("link");
  1060. xml.Element("name", name);
  1061. xml.Element("type", typeTag);
  1062. xml.Element(locationTag, path);
  1063. xml.EndElement();
  1064. }