1
0

cmExtraEclipseCDT4Generator.cxx 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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 "cmsys/RegularExpression.hxx"
  5. #include <algorithm>
  6. #include <assert.h>
  7. #include <map>
  8. #include <sstream>
  9. #include <stdio.h>
  10. #include <utility>
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmGeneratorExpression.h"
  13. #include "cmGeneratorTarget.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmMessageType.h"
  18. #include "cmSourceFile.h"
  19. #include "cmSourceGroup.h"
  20. #include "cmState.h"
  21. #include "cmStateTypes.h"
  22. #include "cmStringAlgorithms.h"
  23. #include "cmSystemTools.h"
  24. #include "cmXMLWriter.h"
  25. #include "cmake.h"
  26. static void AppendAttribute(cmXMLWriter& xml, const char* keyval)
  27. {
  28. xml.StartElement("attribute");
  29. xml.Attribute("key", keyval);
  30. xml.Attribute("value", keyval);
  31. xml.EndElement();
  32. }
  33. template <typename T>
  34. void AppendDictionary(cmXMLWriter& xml, const char* key, T const& value)
  35. {
  36. xml.StartElement("dictionary");
  37. xml.Element("key", key);
  38. xml.Element("value", value);
  39. xml.EndElement();
  40. }
  41. cmExtraEclipseCDT4Generator::cmExtraEclipseCDT4Generator()
  42. {
  43. this->IsOutOfSourceBuild = false;
  44. this->GenerateSourceProject = false;
  45. this->SupportsVirtualFolders = true;
  46. this->GenerateLinkedResources = true;
  47. this->SupportsGmakeErrorParser = true;
  48. this->SupportsMachO64Parser = true;
  49. this->CEnabled = false;
  50. this->CXXEnabled = false;
  51. }
  52. cmExternalMakefileProjectGeneratorFactory*
  53. cmExtraEclipseCDT4Generator::GetFactory()
  54. {
  55. static cmExternalMakefileProjectGeneratorSimpleFactory<
  56. cmExtraEclipseCDT4Generator>
  57. factory("Eclipse CDT4", "Generates Eclipse CDT 4.0 project files.");
  58. if (factory.GetSupportedGlobalGenerators().empty()) {
  59. // TODO: Verify if __CYGWIN__ should be checked.
  60. //#if defined(_WIN32) && !defined(__CYGWIN__)
  61. #if defined(_WIN32)
  62. factory.AddSupportedGlobalGenerator("NMake Makefiles");
  63. factory.AddSupportedGlobalGenerator("MinGW Makefiles");
  64. // factory.AddSupportedGlobalGenerator("MSYS Makefiles");
  65. #endif
  66. factory.AddSupportedGlobalGenerator("Ninja");
  67. factory.AddSupportedGlobalGenerator("Unix Makefiles");
  68. }
  69. return &factory;
  70. }
  71. void cmExtraEclipseCDT4Generator::EnableLanguage(
  72. std::vector<std::string> const& languages, cmMakefile* /*unused*/,
  73. bool /*optional*/)
  74. {
  75. for (std::string const& l : languages) {
  76. if (l == "CXX") {
  77. this->Natures.insert("org.eclipse.cdt.core.ccnature");
  78. this->Natures.insert("org.eclipse.cdt.core.cnature");
  79. this->CXXEnabled = true;
  80. } else if (l == "C") {
  81. this->Natures.insert("org.eclipse.cdt.core.cnature");
  82. this->CEnabled = true;
  83. } else if (l == "Java") {
  84. this->Natures.insert("org.eclipse.jdt.core.javanature");
  85. }
  86. }
  87. }
  88. void cmExtraEclipseCDT4Generator::Generate()
  89. {
  90. cmLocalGenerator* lg = this->GlobalGenerator->GetLocalGenerators()[0];
  91. const cmMakefile* mf = lg->GetMakefile();
  92. std::string eclipseVersion = mf->GetSafeDefinition("CMAKE_ECLIPSE_VERSION");
  93. cmsys::RegularExpression regex(".*([0-9]+\\.[0-9]+).*");
  94. if (regex.find(eclipseVersion)) {
  95. unsigned int majorVersion = 0;
  96. unsigned int minorVersion = 0;
  97. int res =
  98. sscanf(regex.match(1).c_str(), "%u.%u", &majorVersion, &minorVersion);
  99. if (res == 2) {
  100. int version = majorVersion * 1000 + minorVersion;
  101. if (version < 3006) // 3.6 is Helios
  102. {
  103. this->SupportsVirtualFolders = false;
  104. this->SupportsMachO64Parser = false;
  105. }
  106. if (version < 3007) // 3.7 is Indigo
  107. {
  108. this->SupportsGmakeErrorParser = false;
  109. }
  110. }
  111. }
  112. // TODO: Decide if these are local or member variables
  113. this->HomeDirectory = lg->GetSourceDirectory();
  114. this->HomeOutputDirectory = lg->GetBinaryDirectory();
  115. this->GenerateLinkedResources =
  116. mf->IsOn("CMAKE_ECLIPSE_GENERATE_LINKED_RESOURCES");
  117. this->IsOutOfSourceBuild =
  118. (this->HomeDirectory != this->HomeOutputDirectory);
  119. this->GenerateSourceProject =
  120. (this->IsOutOfSourceBuild &&
  121. mf->IsOn("CMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT"));
  122. if (!this->GenerateSourceProject &&
  123. (mf->IsOn("ECLIPSE_CDT4_GENERATE_SOURCE_PROJECT"))) {
  124. mf->IssueMessage(
  125. MessageType::WARNING,
  126. "ECLIPSE_CDT4_GENERATE_SOURCE_PROJECT is set to TRUE, "
  127. "but this variable is not supported anymore since CMake 2.8.7.\n"
  128. "Enable CMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT instead.");
  129. }
  130. if (cmSystemTools::IsSubDirectory(this->HomeOutputDirectory,
  131. this->HomeDirectory)) {
  132. mf->IssueMessage(MessageType::WARNING,
  133. "The build directory is a subdirectory "
  134. "of the source directory.\n"
  135. "This is not supported well by Eclipse. It is strongly "
  136. "recommended to use a build directory which is a "
  137. "sibling of the source directory.");
  138. }
  139. // NOTE: This is not good, since it pollutes the source tree. However,
  140. // Eclipse doesn't allow CVS/SVN to work when the .project is not in
  141. // the cvs/svn root directory. Hence, this is provided as an option.
  142. if (this->GenerateSourceProject) {
  143. // create .project file in the source tree
  144. this->CreateSourceProjectFile();
  145. }
  146. // create a .project file
  147. this->CreateProjectFile();
  148. // create a .cproject file
  149. this->CreateCProjectFile();
  150. // create resource settings
  151. this->CreateSettingsResourcePrefsFile();
  152. }
  153. void cmExtraEclipseCDT4Generator::CreateSettingsResourcePrefsFile()
  154. {
  155. cmLocalGenerator* lg = this->GlobalGenerator->GetLocalGenerators()[0];
  156. cmMakefile* mf = lg->GetMakefile();
  157. const std::string filename =
  158. this->HomeOutputDirectory + "/.settings/org.eclipse.core.resources.prefs";
  159. cmGeneratedFileStream fout(filename);
  160. if (!fout) {
  161. return;
  162. }
  163. fout << "eclipse.preferences.version=1" << std::endl;
  164. const char* encoding = mf->GetDefinition("CMAKE_ECLIPSE_RESOURCE_ENCODING");
  165. if (encoding) {
  166. fout << "encoding/<project>=" << encoding << std::endl;
  167. }
  168. }
  169. void cmExtraEclipseCDT4Generator::CreateSourceProjectFile()
  170. {
  171. assert(this->HomeDirectory != this->HomeOutputDirectory);
  172. // set up the project name: <project>-Source@<baseSourcePathName>
  173. cmLocalGenerator* lg = this->GlobalGenerator->GetLocalGenerators()[0];
  174. std::string name = cmExtraEclipseCDT4Generator::GenerateProjectName(
  175. lg->GetProjectName(), "Source",
  176. cmExtraEclipseCDT4Generator::GetPathBasename(this->HomeDirectory));
  177. const std::string filename = this->HomeDirectory + "/.project";
  178. cmGeneratedFileStream fout(filename);
  179. if (!fout) {
  180. return;
  181. }
  182. cmXMLWriter xml(fout);
  183. xml.StartDocument("UTF-8");
  184. xml.StartElement("projectDescription");
  185. xml.Element("name", name);
  186. xml.Element("comment", "");
  187. xml.Element("projects", "");
  188. xml.Element("buildSpec", "");
  189. xml.Element("natures", "");
  190. xml.StartElement("linkedResources");
  191. if (this->SupportsVirtualFolders) {
  192. this->CreateLinksToSubprojects(xml, this->HomeDirectory);
  193. this->SrcLinkedResources.clear();
  194. }
  195. xml.EndElement(); // linkedResources
  196. xml.EndElement(); // projectDescription
  197. xml.EndDocument();
  198. }
  199. void cmExtraEclipseCDT4Generator::AddEnvVar(std::ostream& out,
  200. const char* envVar,
  201. cmLocalGenerator* lg)
  202. {
  203. cmMakefile* mf = lg->GetMakefile();
  204. // get the variables from the environment and from the cache and then
  205. // figure out which one to use:
  206. std::string envVarValue;
  207. const bool envVarSet = cmSystemTools::GetEnv(envVar, envVarValue);
  208. std::string cacheEntryName = cmStrCat("CMAKE_ECLIPSE_ENVVAR_", envVar);
  209. const std::string* cacheValue =
  210. lg->GetState()->GetInitializedCacheValue(cacheEntryName);
  211. // now we have both, decide which one to use
  212. std::string valueToUse;
  213. if (!envVarSet && cacheValue == nullptr) {
  214. // nothing known, do nothing
  215. valueToUse.clear();
  216. } else if (envVarSet && cacheValue == nullptr) {
  217. // The variable is in the env, but not in the cache. Use it and put it
  218. // in the cache
  219. valueToUse = envVarValue;
  220. mf->AddCacheDefinition(cacheEntryName, valueToUse.c_str(),
  221. cacheEntryName.c_str(), cmStateEnums::STRING, true);
  222. mf->GetCMakeInstance()->SaveCache(lg->GetBinaryDirectory());
  223. } else if (!envVarSet && cacheValue != nullptr) {
  224. // It is already in the cache, but not in the env, so use it from the cache
  225. valueToUse = *cacheValue;
  226. } else {
  227. // It is both in the cache and in the env.
  228. // Use the version from the env. except if the value from the env is
  229. // completely contained in the value from the cache (for the case that we
  230. // now have a PATH without MSVC dirs in the env. but had the full PATH with
  231. // all MSVC dirs during the cmake run which stored the var in the cache:
  232. valueToUse = *cacheValue;
  233. if (valueToUse.find(envVarValue) == std::string::npos) {
  234. valueToUse = envVarValue;
  235. mf->AddCacheDefinition(cacheEntryName, valueToUse.c_str(),
  236. cacheEntryName.c_str(), cmStateEnums::STRING,
  237. true);
  238. mf->GetCMakeInstance()->SaveCache(lg->GetBinaryDirectory());
  239. }
  240. }
  241. if (!valueToUse.empty()) {
  242. out << envVar << "=" << valueToUse << "|";
  243. }
  244. }
  245. void cmExtraEclipseCDT4Generator::CreateProjectFile()
  246. {
  247. cmLocalGenerator* lg = this->GlobalGenerator->GetLocalGenerators()[0];
  248. cmMakefile* mf = lg->GetMakefile();
  249. const std::string filename = this->HomeOutputDirectory + "/.project";
  250. cmGeneratedFileStream fout(filename);
  251. if (!fout) {
  252. return;
  253. }
  254. std::string compilerId = mf->GetSafeDefinition("CMAKE_C_COMPILER_ID");
  255. if (compilerId.empty()) // no C compiler, try the C++ compiler:
  256. {
  257. compilerId = mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID");
  258. }
  259. cmXMLWriter xml(fout);
  260. xml.StartDocument("UTF-8");
  261. xml.StartElement("projectDescription");
  262. xml.Element("name",
  263. cmExtraEclipseCDT4Generator::GenerateProjectName(
  264. lg->GetProjectName(),
  265. mf->GetSafeDefinition("CMAKE_BUILD_TYPE"),
  266. cmExtraEclipseCDT4Generator::GetPathBasename(
  267. this->HomeOutputDirectory)));
  268. xml.Element("comment", "");
  269. xml.Element("projects", "");
  270. xml.StartElement("buildSpec");
  271. xml.StartElement("buildCommand");
  272. xml.Element("name", "org.eclipse.cdt.make.core.makeBuilder");
  273. xml.Element("triggers", "clean,full,incremental,");
  274. xml.StartElement("arguments");
  275. // use clean target
  276. AppendDictionary(xml, "org.eclipse.cdt.make.core.cleanBuildTarget", "clean");
  277. AppendDictionary(xml, "org.eclipse.cdt.make.core.enableCleanBuild", "true");
  278. AppendDictionary(xml, "org.eclipse.cdt.make.core.append_environment",
  279. "true");
  280. AppendDictionary(xml, "org.eclipse.cdt.make.core.stopOnError", "true");
  281. // set the make command
  282. AppendDictionary(xml, "org.eclipse.cdt.make.core.enabledIncrementalBuild",
  283. "true");
  284. AppendDictionary(xml, "org.eclipse.cdt.make.core.build.command",
  285. cmExtraEclipseCDT4Generator::GetEclipsePath(
  286. mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM")));
  287. AppendDictionary(xml, "org.eclipse.cdt.make.core.contents",
  288. "org.eclipse.cdt.make.core.activeConfigSettings");
  289. AppendDictionary(xml, "org.eclipse.cdt.make.core.build.target.inc", "all");
  290. AppendDictionary(xml, "org.eclipse.cdt.make.core.build.arguments",
  291. mf->GetSafeDefinition("CMAKE_ECLIPSE_MAKE_ARGUMENTS"));
  292. AppendDictionary(
  293. xml, "org.eclipse.cdt.make.core.buildLocation",
  294. cmExtraEclipseCDT4Generator::GetEclipsePath(this->HomeOutputDirectory));
  295. AppendDictionary(xml, "org.eclipse.cdt.make.core.useDefaultBuildCmd",
  296. "false");
  297. // set project specific environment
  298. std::ostringstream environment;
  299. environment << "VERBOSE=1|CMAKE_NO_VERBOSE=1|"; // verbose Makefile output
  300. // set vsvars32.bat environment available at CMake time,
  301. // but not necessarily when eclipse is open
  302. if (compilerId == "MSVC") {
  303. AddEnvVar(environment, "PATH", lg);
  304. AddEnvVar(environment, "INCLUDE", lg);
  305. AddEnvVar(environment, "LIB", lg);
  306. AddEnvVar(environment, "LIBPATH", lg);
  307. } else if (compilerId == "Intel") {
  308. // if the env.var is set, use this one and put it in the cache
  309. // if the env.var is not set, but the value is in the cache,
  310. // use it from the cache:
  311. AddEnvVar(environment, "INTEL_LICENSE_FILE", lg);
  312. }
  313. AppendDictionary(xml, "org.eclipse.cdt.make.core.environment",
  314. environment.str());
  315. AppendDictionary(xml, "org.eclipse.cdt.make.core.enableFullBuild", "true");
  316. AppendDictionary(xml, "org.eclipse.cdt.make.core.build.target.auto", "all");
  317. AppendDictionary(xml, "org.eclipse.cdt.make.core.enableAutoBuild", "false");
  318. AppendDictionary(xml, "org.eclipse.cdt.make.core.build.target.clean",
  319. "clean");
  320. AppendDictionary(xml, "org.eclipse.cdt.make.core.fullBuildTarget", "all");
  321. AppendDictionary(xml, "org.eclipse.cdt.make.core.buildArguments", "");
  322. AppendDictionary(
  323. xml, "org.eclipse.cdt.make.core.build.location",
  324. cmExtraEclipseCDT4Generator::GetEclipsePath(this->HomeOutputDirectory));
  325. AppendDictionary(xml, "org.eclipse.cdt.make.core.autoBuildTarget", "all");
  326. // set error parsers
  327. std::ostringstream errorOutputParser;
  328. if (compilerId == "MSVC") {
  329. errorOutputParser << "org.eclipse.cdt.core.VCErrorParser;";
  330. } else if (compilerId == "Intel") {
  331. errorOutputParser << "org.eclipse.cdt.core.ICCErrorParser;";
  332. }
  333. if (this->SupportsGmakeErrorParser) {
  334. errorOutputParser << "org.eclipse.cdt.core.GmakeErrorParser;";
  335. } else {
  336. errorOutputParser << "org.eclipse.cdt.core.MakeErrorParser;";
  337. }
  338. errorOutputParser << "org.eclipse.cdt.core.GCCErrorParser;"
  339. "org.eclipse.cdt.core.GASErrorParser;"
  340. "org.eclipse.cdt.core.GLDErrorParser;";
  341. AppendDictionary(xml, "org.eclipse.cdt.core.errorOutputParser",
  342. errorOutputParser.str());
  343. xml.EndElement(); // arguments
  344. xml.EndElement(); // buildCommand
  345. xml.StartElement("buildCommand");
  346. xml.Element("name", "org.eclipse.cdt.make.core.ScannerConfigBuilder");
  347. xml.StartElement("arguments");
  348. xml.EndElement(); // arguments
  349. xml.EndElement(); // buildCommand
  350. xml.EndElement(); // buildSpec
  351. // set natures for c/c++ projects
  352. xml.StartElement("natures");
  353. xml.Element("nature", "org.eclipse.cdt.make.core.makeNature");
  354. xml.Element("nature", "org.eclipse.cdt.make.core.ScannerConfigNature");
  355. for (std::string const& n : this->Natures) {
  356. xml.Element("nature", n);
  357. }
  358. if (const char* extraNaturesProp =
  359. mf->GetState()->GetGlobalProperty("ECLIPSE_EXTRA_NATURES")) {
  360. std::vector<std::string> extraNatures = cmExpandedList(extraNaturesProp);
  361. for (std::string const& n : extraNatures) {
  362. xml.Element("nature", n);
  363. }
  364. }
  365. xml.EndElement(); // natures
  366. xml.StartElement("linkedResources");
  367. // create linked resources
  368. if (this->IsOutOfSourceBuild) {
  369. // create a linked resource to CMAKE_SOURCE_DIR
  370. // (this is not done anymore for each project because of
  371. // https://gitlab.kitware.com/cmake/cmake/issues/9978 and because I found
  372. // it actually quite confusing in bigger projects with many directories and
  373. // projects, Alex
  374. std::string sourceLinkedResourceName = "[Source directory]";
  375. std::string linkSourceDirectory =
  376. cmExtraEclipseCDT4Generator::GetEclipsePath(
  377. lg->GetCurrentSourceDirectory());
  378. // .project dir can't be subdir of a linked resource dir
  379. if (!cmSystemTools::IsSubDirectory(this->HomeOutputDirectory,
  380. linkSourceDirectory)) {
  381. cmExtraEclipseCDT4Generator::AppendLinkedResource(
  382. xml, sourceLinkedResourceName,
  383. cmExtraEclipseCDT4Generator::GetEclipsePath(linkSourceDirectory),
  384. LinkToFolder);
  385. this->SrcLinkedResources.push_back(std::move(sourceLinkedResourceName));
  386. }
  387. }
  388. if (this->SupportsVirtualFolders) {
  389. this->CreateLinksToSubprojects(xml, this->HomeOutputDirectory);
  390. this->CreateLinksForTargets(xml);
  391. }
  392. xml.EndElement(); // linkedResources
  393. xml.EndElement(); // projectDescription
  394. }
  395. void cmExtraEclipseCDT4Generator::WriteGroups(
  396. std::vector<cmSourceGroup> const& sourceGroups, std::string& linkName,
  397. cmXMLWriter& xml)
  398. {
  399. for (cmSourceGroup const& sg : sourceGroups) {
  400. std::string linkName3 = cmStrCat(linkName, '/', sg.GetFullName());
  401. std::replace(linkName3.begin(), linkName3.end(), '\\', '/');
  402. cmExtraEclipseCDT4Generator::AppendLinkedResource(
  403. xml, linkName3, "virtual:/virtual", VirtualFolder);
  404. std::vector<cmSourceGroup> const& children = sg.GetGroupChildren();
  405. if (!children.empty()) {
  406. this->WriteGroups(children, linkName, xml);
  407. }
  408. std::vector<const cmSourceFile*> sFiles = sg.GetSourceFiles();
  409. for (cmSourceFile const* file : sFiles) {
  410. std::string const& fullPath = file->GetFullPath();
  411. if (!cmSystemTools::FileIsDirectory(fullPath)) {
  412. std::string linkName4 =
  413. cmStrCat(linkName3, '/', cmSystemTools::GetFilenameName(fullPath));
  414. cmExtraEclipseCDT4Generator::AppendLinkedResource(
  415. xml, linkName4,
  416. cmExtraEclipseCDT4Generator::GetEclipsePath(fullPath), LinkToFile);
  417. }
  418. }
  419. }
  420. }
  421. void cmExtraEclipseCDT4Generator::CreateLinksForTargets(cmXMLWriter& xml)
  422. {
  423. std::string linkName = "[Targets]";
  424. cmExtraEclipseCDT4Generator::AppendLinkedResource(
  425. xml, linkName, "virtual:/virtual", VirtualFolder);
  426. for (cmLocalGenerator* lg : this->GlobalGenerator->GetLocalGenerators()) {
  427. cmMakefile* makefile = lg->GetMakefile();
  428. const std::vector<cmGeneratorTarget*>& targets = lg->GetGeneratorTargets();
  429. for (cmGeneratorTarget* target : targets) {
  430. std::string linkName2 = cmStrCat(linkName, '/');
  431. switch (target->GetType()) {
  432. case cmStateEnums::EXECUTABLE:
  433. case cmStateEnums::STATIC_LIBRARY:
  434. case cmStateEnums::SHARED_LIBRARY:
  435. case cmStateEnums::MODULE_LIBRARY:
  436. case cmStateEnums::OBJECT_LIBRARY: {
  437. const char* prefix =
  438. (target->GetType() == cmStateEnums::EXECUTABLE ? "[exe] "
  439. : "[lib] ");
  440. linkName2 += prefix;
  441. linkName2 += target->GetName();
  442. cmExtraEclipseCDT4Generator::AppendLinkedResource(
  443. xml, linkName2, "virtual:/virtual", VirtualFolder);
  444. if (!this->GenerateLinkedResources) {
  445. break; // skip generating the linked resources to the source files
  446. }
  447. std::vector<cmSourceGroup> sourceGroups =
  448. makefile->GetSourceGroups();
  449. // get the files from the source lists then add them to the groups
  450. cmGeneratorTarget* gt = const_cast<cmGeneratorTarget*>(target);
  451. std::vector<cmSourceFile*> files;
  452. gt->GetSourceFiles(files,
  453. 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. cmLocalGenerator* 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 (cmLocalGenerator* lgen : this->GlobalGenerator->GetLocalGenerators()) {
  657. if (const char* 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 (cmLocalGenerator* lgen : this->GlobalGenerator->GetLocalGenerators()) {
  754. const std::vector<cmGeneratorTarget*>& targets =
  755. lgen->GetGeneratorTargets();
  756. for (cmGeneratorTarget* target : targets) {
  757. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  758. continue;
  759. }
  760. std::vector<std::string> includeDirs;
  761. std::string config = mf->GetSafeDefinition("CMAKE_BUILD_TYPE");
  762. lgen->GetIncludeDirectories(includeDirs, target, "C", config);
  763. this->AppendIncludeDirectories(xml, includeDirs, emmited);
  764. }
  765. }
  766. // now also the system include directories, in case we found them in
  767. // CMakeSystemSpecificInformation.cmake. This makes Eclipse find the
  768. // standard headers.
  769. std::string compiler = mf->GetSafeDefinition("CMAKE_C_COMPILER");
  770. if (this->CEnabled && !compiler.empty()) {
  771. std::string systemIncludeDirs =
  772. mf->GetSafeDefinition("CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
  773. std::vector<std::string> dirs = cmExpandedList(systemIncludeDirs);
  774. this->AppendIncludeDirectories(xml, dirs, emmited);
  775. }
  776. compiler = mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  777. if (this->CXXEnabled && !compiler.empty()) {
  778. std::string systemIncludeDirs =
  779. mf->GetSafeDefinition("CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
  780. std::vector<std::string> dirs = cmExpandedList(systemIncludeDirs);
  781. this->AppendIncludeDirectories(xml, dirs, emmited);
  782. }
  783. xml.EndElement(); // storageModule
  784. // add build targets
  785. xml.StartElement("storageModule");
  786. xml.Attribute("moduleId", "org.eclipse.cdt.make.core.buildtargets");
  787. xml.StartElement("buildTargets");
  788. emmited.clear();
  789. const std::string& make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  790. const std::string& makeArgs =
  791. mf->GetSafeDefinition("CMAKE_ECLIPSE_MAKE_ARGUMENTS");
  792. cmGlobalGenerator* generator =
  793. const_cast<cmGlobalGenerator*>(this->GlobalGenerator);
  794. std::string allTarget;
  795. std::string cleanTarget;
  796. if (generator->GetAllTargetName()) {
  797. allTarget = generator->GetAllTargetName();
  798. }
  799. if (generator->GetCleanTargetName()) {
  800. cleanTarget = generator->GetCleanTargetName();
  801. }
  802. // add all executable and library targets and some of the GLOBAL
  803. // and UTILITY targets
  804. for (cmLocalGenerator* lgen : this->GlobalGenerator->GetLocalGenerators()) {
  805. const std::vector<cmGeneratorTarget*>& targets =
  806. lgen->GetGeneratorTargets();
  807. std::string subdir = lgen->MaybeConvertToRelativePath(
  808. this->HomeOutputDirectory, lgen->GetCurrentBinaryDirectory());
  809. if (subdir == ".") {
  810. subdir.clear();
  811. }
  812. for (cmGeneratorTarget* target : targets) {
  813. std::string targetName = target->GetName();
  814. switch (target->GetType()) {
  815. case cmStateEnums::GLOBAL_TARGET: {
  816. // Only add the global targets from CMAKE_BINARY_DIR,
  817. // not from the subdirs
  818. if (subdir.empty()) {
  819. cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
  820. makeArgs, subdir, ": ");
  821. }
  822. } break;
  823. case cmStateEnums::UTILITY:
  824. // Add all utility targets, except the Nightly/Continuous/
  825. // Experimental-"sub"targets as e.g. NightlyStart
  826. if (((targetName.find("Nightly") == 0) &&
  827. (targetName != "Nightly")) ||
  828. ((targetName.find("Continuous") == 0) &&
  829. (targetName != "Continuous")) ||
  830. ((targetName.find("Experimental") == 0) &&
  831. (targetName != "Experimental"))) {
  832. break;
  833. }
  834. cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
  835. makeArgs, subdir, ": ");
  836. break;
  837. case cmStateEnums::EXECUTABLE:
  838. case cmStateEnums::STATIC_LIBRARY:
  839. case cmStateEnums::SHARED_LIBRARY:
  840. case cmStateEnums::MODULE_LIBRARY:
  841. case cmStateEnums::OBJECT_LIBRARY: {
  842. const char* prefix =
  843. (target->GetType() == cmStateEnums::EXECUTABLE ? "[exe] "
  844. : "[lib] ");
  845. cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
  846. makeArgs, subdir, prefix);
  847. std::string fastTarget = cmStrCat(targetName, "/fast");
  848. cmExtraEclipseCDT4Generator::AppendTarget(xml, fastTarget, make,
  849. makeArgs, subdir, prefix);
  850. // Add Build and Clean targets in the virtual folder of targets:
  851. if (this->SupportsVirtualFolders) {
  852. std::string virtDir = cmStrCat("[Targets]/", prefix, targetName);
  853. std::string buildArgs =
  854. cmStrCat("-C \"", lgen->GetBinaryDirectory(), "\" ", makeArgs);
  855. cmExtraEclipseCDT4Generator::AppendTarget(
  856. xml, "Build", make, buildArgs, virtDir, "", targetName.c_str());
  857. std::string cleanArgs =
  858. cmStrCat("-E chdir \"", lgen->GetCurrentBinaryDirectory(),
  859. "\" \"", cmSystemTools::GetCMakeCommand(), "\" -P \"");
  860. cmGeneratorTarget* gt = target;
  861. cleanArgs += lgen->GetTargetDirectory(gt);
  862. cleanArgs += "/cmake_clean.cmake\"";
  863. cmExtraEclipseCDT4Generator::AppendTarget(
  864. xml, "Clean", cmSystemTools::GetCMakeCommand(), cleanArgs,
  865. virtDir, "", "");
  866. }
  867. } break;
  868. case cmStateEnums::INTERFACE_LIBRARY:
  869. break;
  870. default:
  871. break;
  872. }
  873. }
  874. // insert the all and clean targets in every subdir
  875. if (!allTarget.empty()) {
  876. cmExtraEclipseCDT4Generator::AppendTarget(xml, allTarget, make, makeArgs,
  877. subdir, ": ");
  878. }
  879. if (!cleanTarget.empty()) {
  880. cmExtraEclipseCDT4Generator::AppendTarget(xml, cleanTarget, make,
  881. makeArgs, subdir, ": ");
  882. }
  883. // insert rules for compiling, preprocessing and assembling individual
  884. // files
  885. std::vector<std::string> objectFileTargets;
  886. lg->GetIndividualFileTargets(objectFileTargets);
  887. for (std::string const& f : objectFileTargets) {
  888. const char* prefix = "[obj] ";
  889. if (f.back() == 's') {
  890. prefix = "[to asm] ";
  891. } else if (f.back() == 'i') {
  892. prefix = "[pre] ";
  893. }
  894. cmExtraEclipseCDT4Generator::AppendTarget(xml, f, make, makeArgs, subdir,
  895. prefix);
  896. }
  897. }
  898. xml.EndElement(); // buildTargets
  899. xml.EndElement(); // storageModule
  900. cmExtraEclipseCDT4Generator::AppendStorageScanners(xml, *mf);
  901. xml.EndElement(); // cconfiguration
  902. xml.EndElement(); // storageModule
  903. xml.StartElement("storageModule");
  904. xml.Attribute("moduleId", "cdtBuildSystem");
  905. xml.Attribute("version", "4.0.0");
  906. xml.StartElement("project");
  907. xml.Attribute("id", std::string(lg->GetProjectName()) + ".null.1");
  908. xml.Attribute("name", lg->GetProjectName());
  909. xml.EndElement(); // project
  910. xml.EndElement(); // storageModule
  911. // Append additional cproject contents without applying any XML formatting
  912. if (const char* extraCProjectContents =
  913. mf->GetState()->GetGlobalProperty("ECLIPSE_EXTRA_CPROJECT_CONTENTS")) {
  914. fout << extraCProjectContents;
  915. }
  916. xml.EndElement(); // cproject
  917. }
  918. std::string cmExtraEclipseCDT4Generator::GetEclipsePath(
  919. const std::string& path)
  920. {
  921. #if defined(__CYGWIN__)
  922. std::string cmd = "cygpath -m " + path;
  923. std::string out;
  924. if (!cmSystemTools::RunSingleCommand(cmd.c_str(), &out, &out)) {
  925. return path;
  926. } else {
  927. out.erase(out.find_last_of('\n'));
  928. return out;
  929. }
  930. #else
  931. return path;
  932. #endif
  933. }
  934. std::string cmExtraEclipseCDT4Generator::GetPathBasename(
  935. const std::string& path)
  936. {
  937. std::string outputBasename = path;
  938. while (!outputBasename.empty() &&
  939. (outputBasename.back() == '/' || outputBasename.back() == '\\')) {
  940. outputBasename.resize(outputBasename.size() - 1);
  941. }
  942. std::string::size_type loc = outputBasename.find_last_of("/\\");
  943. if (loc != std::string::npos) {
  944. outputBasename = outputBasename.substr(loc + 1);
  945. }
  946. return outputBasename;
  947. }
  948. std::string cmExtraEclipseCDT4Generator::GenerateProjectName(
  949. const std::string& name, const std::string& type, const std::string& path)
  950. {
  951. return name + (type.empty() ? "" : "-") + type + "@" + path;
  952. }
  953. // Helper functions
  954. void cmExtraEclipseCDT4Generator::AppendStorageScanners(
  955. cmXMLWriter& xml, const cmMakefile& makefile)
  956. {
  957. // we need the "make" and the C (or C++) compiler which are used, Alex
  958. const std::string& make =
  959. makefile.GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  960. std::string compiler = makefile.GetSafeDefinition("CMAKE_C_COMPILER");
  961. std::string arg1 = makefile.GetSafeDefinition("CMAKE_C_COMPILER_ARG1");
  962. if (compiler.empty()) {
  963. compiler = makefile.GetSafeDefinition("CMAKE_CXX_COMPILER");
  964. arg1 = makefile.GetSafeDefinition("CMAKE_CXX_COMPILER_ARG1");
  965. }
  966. if (compiler.empty()) // Hmm, what to do now ?
  967. {
  968. compiler = "gcc";
  969. }
  970. // the following right now hardcodes gcc behaviour :-/
  971. std::string compilerArgs =
  972. "-E -P -v -dD ${plugin_state_location}/${specs_file}";
  973. if (!arg1.empty()) {
  974. arg1 += " ";
  975. compilerArgs = arg1 + compilerArgs;
  976. }
  977. xml.StartElement("storageModule");
  978. xml.Attribute("moduleId", "scannerConfiguration");
  979. xml.StartElement("autodiscovery");
  980. xml.Attribute("enabled", "true");
  981. xml.Attribute("problemReportingEnabled", "true");
  982. xml.Attribute("selectedProfileId",
  983. "org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile");
  984. xml.EndElement(); // autodiscovery
  985. cmExtraEclipseCDT4Generator::AppendScannerProfile(
  986. xml, "org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile", true,
  987. "", true, "specsFile", compilerArgs, compiler, true, true);
  988. cmExtraEclipseCDT4Generator::AppendScannerProfile(
  989. xml, "org.eclipse.cdt.make.core.GCCStandardMakePerFileProfile", true, "",
  990. true, "makefileGenerator", "-f ${project_name}_scd.mk", make, true, true);
  991. xml.EndElement(); // storageModule
  992. }
  993. // The prefix is prepended before the actual name of the target. The purpose
  994. // of that is to sort the targets in the view of Eclipse, so that at first
  995. // the global/utility/all/clean targets appear ": ", then the executable
  996. // targets "[exe] ", then the libraries "[lib]", then the rules for the
  997. // object files "[obj]", then for preprocessing only "[pre] " and
  998. // finally the assembly files "[to asm] ". Note the "to" in "to asm",
  999. // without it, "asm" would be the first targets in the list, with the "to"
  1000. // they are the last targets, which makes more sense.
  1001. void cmExtraEclipseCDT4Generator::AppendTarget(
  1002. cmXMLWriter& xml, const std::string& target, const std::string& make,
  1003. const std::string& makeArgs, const std::string& path, const char* prefix,
  1004. const char* makeTarget)
  1005. {
  1006. xml.StartElement("target");
  1007. xml.Attribute("name", prefix + target);
  1008. xml.Attribute("path", path);
  1009. xml.Attribute("targetID", "org.eclipse.cdt.make.MakeTargetBuilder");
  1010. xml.Element("buildCommand",
  1011. cmExtraEclipseCDT4Generator::GetEclipsePath(make));
  1012. xml.Element("buildArguments", makeArgs);
  1013. xml.Element("buildTarget", makeTarget ? makeTarget : target.c_str());
  1014. xml.Element("stopOnError", "true");
  1015. xml.Element("useDefaultCommand", "false");
  1016. xml.EndElement();
  1017. }
  1018. void cmExtraEclipseCDT4Generator::AppendScannerProfile(
  1019. cmXMLWriter& xml, const std::string& profileID, bool openActionEnabled,
  1020. const std::string& openActionFilePath, bool pParserEnabled,
  1021. const std::string& scannerInfoProviderID,
  1022. const std::string& runActionArguments, const std::string& runActionCommand,
  1023. bool runActionUseDefault, bool sipParserEnabled)
  1024. {
  1025. xml.StartElement("profile");
  1026. xml.Attribute("id", profileID);
  1027. xml.StartElement("buildOutputProvider");
  1028. xml.StartElement("openAction");
  1029. xml.Attribute("enabled", openActionEnabled ? "true" : "false");
  1030. xml.Attribute("filePath", openActionFilePath);
  1031. xml.EndElement(); // openAction
  1032. xml.StartElement("parser");
  1033. xml.Attribute("enabled", pParserEnabled ? "true" : "false");
  1034. xml.EndElement(); // parser
  1035. xml.EndElement(); // buildOutputProvider
  1036. xml.StartElement("scannerInfoProvider");
  1037. xml.Attribute("id", scannerInfoProviderID);
  1038. xml.StartElement("runAction");
  1039. xml.Attribute("arguments", runActionArguments);
  1040. xml.Attribute("command", runActionCommand);
  1041. xml.Attribute("useDefault", runActionUseDefault ? "true" : "false");
  1042. xml.EndElement(); // runAction
  1043. xml.StartElement("parser");
  1044. xml.Attribute("enabled", sipParserEnabled ? "true" : "false");
  1045. xml.EndElement(); // parser
  1046. xml.EndElement(); // scannerInfoProvider
  1047. xml.EndElement(); // profile
  1048. }
  1049. void cmExtraEclipseCDT4Generator::AppendLinkedResource(cmXMLWriter& xml,
  1050. const std::string& name,
  1051. const std::string& path,
  1052. LinkType linkType)
  1053. {
  1054. const char* locationTag = "location";
  1055. int typeTag = 2;
  1056. if (linkType == VirtualFolder) // ... and not a linked folder
  1057. {
  1058. locationTag = "locationURI";
  1059. }
  1060. if (linkType == LinkToFile) {
  1061. typeTag = 1;
  1062. }
  1063. xml.StartElement("link");
  1064. xml.Element("name", name);
  1065. xml.Element("type", typeTag);
  1066. xml.Element(locationTag, path);
  1067. xml.EndElement();
  1068. }