cmExtraEclipseCDT4Generator.cxx 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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 "cmSourceFile.h"
  20. #include "cmSourceGroup.h"
  21. #include "cmState.h"
  22. #include "cmStateTypes.h"
  23. #include "cmStringAlgorithms.h"
  24. #include "cmSystemTools.h"
  25. #include "cmXMLWriter.h"
  26. #include "cmake.h"
  27. static void AppendAttribute(cmXMLWriter& xml, const char* keyval)
  28. {
  29. xml.StartElement("attribute");
  30. xml.Attribute("key", keyval);
  31. xml.Attribute("value", keyval);
  32. xml.EndElement();
  33. }
  34. template <typename T>
  35. void AppendDictionary(cmXMLWriter& xml, const char* key, T const& value)
  36. {
  37. xml.StartElement("dictionary");
  38. xml.Element("key", key);
  39. xml.Element("value", value);
  40. xml.EndElement();
  41. }
  42. cmExtraEclipseCDT4Generator::cmExtraEclipseCDT4Generator()
  43. {
  44. this->IsOutOfSourceBuild = false;
  45. this->GenerateSourceProject = false;
  46. this->SupportsVirtualFolders = true;
  47. this->GenerateLinkedResources = true;
  48. this->SupportsGmakeErrorParser = true;
  49. this->SupportsMachO64Parser = true;
  50. this->CEnabled = false;
  51. this->CXXEnabled = false;
  52. }
  53. cmExternalMakefileProjectGeneratorFactory*
  54. cmExtraEclipseCDT4Generator::GetFactory()
  55. {
  56. static cmExternalMakefileProjectGeneratorSimpleFactory<
  57. cmExtraEclipseCDT4Generator>
  58. factory("Eclipse CDT4", "Generates Eclipse CDT 4.0 project files.");
  59. if (factory.GetSupportedGlobalGenerators().empty()) {
  60. // TODO: Verify if __CYGWIN__ should be checked.
  61. //#if defined(_WIN32) && !defined(__CYGWIN__)
  62. #if defined(_WIN32)
  63. factory.AddSupportedGlobalGenerator("NMake Makefiles");
  64. factory.AddSupportedGlobalGenerator("MinGW Makefiles");
  65. // factory.AddSupportedGlobalGenerator("MSYS Makefiles");
  66. #endif
  67. factory.AddSupportedGlobalGenerator("Ninja");
  68. factory.AddSupportedGlobalGenerator("Unix Makefiles");
  69. }
  70. return &factory;
  71. }
  72. void cmExtraEclipseCDT4Generator::EnableLanguage(
  73. std::vector<std::string> const& languages, cmMakefile* /*unused*/,
  74. bool /*optional*/)
  75. {
  76. for (std::string const& l : languages) {
  77. if (l == "CXX") {
  78. this->Natures.insert("org.eclipse.cdt.core.ccnature");
  79. this->Natures.insert("org.eclipse.cdt.core.cnature");
  80. this->CXXEnabled = true;
  81. } else if (l == "C") {
  82. this->Natures.insert("org.eclipse.cdt.core.cnature");
  83. this->CEnabled = true;
  84. } else if (l == "Java") {
  85. this->Natures.insert("org.eclipse.jdt.core.javanature");
  86. }
  87. }
  88. }
  89. void cmExtraEclipseCDT4Generator::Generate()
  90. {
  91. const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
  92. const cmMakefile* mf = lg->GetMakefile();
  93. std::string eclipseVersion = mf->GetSafeDefinition("CMAKE_ECLIPSE_VERSION");
  94. cmsys::RegularExpression regex(".*([0-9]+\\.[0-9]+).*");
  95. if (regex.find(eclipseVersion)) {
  96. unsigned int majorVersion = 0;
  97. unsigned int minorVersion = 0;
  98. int res =
  99. sscanf(regex.match(1).c_str(), "%u.%u", &majorVersion, &minorVersion);
  100. if (res == 2) {
  101. int version = majorVersion * 1000 + minorVersion;
  102. if (version < 3006) // 3.6 is Helios
  103. {
  104. this->SupportsVirtualFolders = false;
  105. this->SupportsMachO64Parser = false;
  106. }
  107. if (version < 3007) // 3.7 is Indigo
  108. {
  109. this->SupportsGmakeErrorParser = false;
  110. }
  111. }
  112. }
  113. // TODO: Decide if these are local or member variables
  114. this->HomeDirectory = lg->GetSourceDirectory();
  115. this->HomeOutputDirectory = lg->GetBinaryDirectory();
  116. this->GenerateLinkedResources =
  117. mf->IsOn("CMAKE_ECLIPSE_GENERATE_LINKED_RESOURCES");
  118. this->IsOutOfSourceBuild =
  119. (this->HomeDirectory != this->HomeOutputDirectory);
  120. this->GenerateSourceProject =
  121. (this->IsOutOfSourceBuild &&
  122. mf->IsOn("CMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT"));
  123. if (!this->GenerateSourceProject &&
  124. (mf->IsOn("ECLIPSE_CDT4_GENERATE_SOURCE_PROJECT"))) {
  125. mf->IssueMessage(
  126. MessageType::WARNING,
  127. "ECLIPSE_CDT4_GENERATE_SOURCE_PROJECT is set to TRUE, "
  128. "but this variable is not supported anymore since CMake 2.8.7.\n"
  129. "Enable CMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT instead.");
  130. }
  131. if (cmSystemTools::IsSubDirectory(this->HomeOutputDirectory,
  132. this->HomeDirectory)) {
  133. mf->IssueMessage(MessageType::WARNING,
  134. "The build directory is a subdirectory "
  135. "of the source directory.\n"
  136. "This is not supported well by Eclipse. It is strongly "
  137. "recommended to use a build directory which is a "
  138. "sibling of the source directory.");
  139. }
  140. // NOTE: This is not good, since it pollutes the source tree. However,
  141. // Eclipse doesn't allow CVS/SVN to work when the .project is not in
  142. // the cvs/svn root directory. Hence, this is provided as an option.
  143. if (this->GenerateSourceProject) {
  144. // create .project file in the source tree
  145. this->CreateSourceProjectFile();
  146. }
  147. // create a .project file
  148. this->CreateProjectFile();
  149. // create a .cproject file
  150. this->CreateCProjectFile();
  151. // create resource settings
  152. this->CreateSettingsResourcePrefsFile();
  153. }
  154. void cmExtraEclipseCDT4Generator::CreateSettingsResourcePrefsFile()
  155. {
  156. const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
  157. cmMakefile* mf = lg->GetMakefile();
  158. const std::string filename =
  159. this->HomeOutputDirectory + "/.settings/org.eclipse.core.resources.prefs";
  160. cmGeneratedFileStream fout(filename);
  161. if (!fout) {
  162. return;
  163. }
  164. fout << "eclipse.preferences.version=1\n";
  165. const char* encoding = mf->GetDefinition("CMAKE_ECLIPSE_RESOURCE_ENCODING");
  166. if (encoding) {
  167. fout << "encoding/<project>=" << encoding << '\n';
  168. }
  169. }
  170. void cmExtraEclipseCDT4Generator::CreateSourceProjectFile()
  171. {
  172. assert(this->HomeDirectory != this->HomeOutputDirectory);
  173. // set up the project name: <project>-Source@<baseSourcePathName>
  174. const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
  175. std::string name = cmExtraEclipseCDT4Generator::GenerateProjectName(
  176. lg->GetProjectName(), "Source",
  177. cmExtraEclipseCDT4Generator::GetPathBasename(this->HomeDirectory));
  178. const std::string filename = this->HomeDirectory + "/.project";
  179. cmGeneratedFileStream fout(filename);
  180. if (!fout) {
  181. return;
  182. }
  183. cmXMLWriter xml(fout);
  184. xml.StartDocument("UTF-8");
  185. xml.StartElement("projectDescription");
  186. xml.Element("name", name);
  187. xml.Element("comment", "");
  188. xml.Element("projects", "");
  189. xml.Element("buildSpec", "");
  190. xml.Element("natures", "");
  191. xml.StartElement("linkedResources");
  192. if (this->SupportsVirtualFolders) {
  193. this->CreateLinksToSubprojects(xml, this->HomeDirectory);
  194. this->SrcLinkedResources.clear();
  195. }
  196. xml.EndElement(); // linkedResources
  197. xml.EndElement(); // projectDescription
  198. xml.EndDocument();
  199. }
  200. void cmExtraEclipseCDT4Generator::AddEnvVar(std::ostream& out,
  201. const char* envVar,
  202. cmLocalGenerator& lg)
  203. {
  204. cmMakefile* mf = lg.GetMakefile();
  205. // get the variables from the environment and from the cache and then
  206. // figure out which one to use:
  207. std::string envVarValue;
  208. const bool envVarSet = cmSystemTools::GetEnv(envVar, envVarValue);
  209. std::string cacheEntryName = cmStrCat("CMAKE_ECLIPSE_ENVVAR_", envVar);
  210. cmProp cacheValue = 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, cacheEntryName.c_str(),
  221. 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,
  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. const auto& 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 (cmProp 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 (const auto& lg : this->GlobalGenerator->GetLocalGenerators()) {
  427. cmMakefile* makefile = lg->GetMakefile();
  428. const auto& targets = lg->GetGeneratorTargets();
  429. for (const auto& 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. std::vector<cmSourceFile*> files;
  451. target->GetSourceFiles(
  452. files, makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  453. for (cmSourceFile* sf : files) {
  454. // Add the file to the list of sources.
  455. std::string const& source = sf->ResolveFullPath();
  456. cmSourceGroup* sourceGroup =
  457. makefile->FindSourceGroup(source, sourceGroups);
  458. sourceGroup->AssignSource(sf);
  459. }
  460. this->WriteGroups(sourceGroups, linkName2, xml);
  461. } break;
  462. // ignore all others:
  463. default:
  464. break;
  465. }
  466. }
  467. }
  468. }
  469. void cmExtraEclipseCDT4Generator::CreateLinksToSubprojects(
  470. cmXMLWriter& xml, const std::string& baseDir)
  471. {
  472. if (!this->GenerateLinkedResources) {
  473. return;
  474. }
  475. // for each sub project create a linked resource to the source dir
  476. // - only if it is an out-of-source build
  477. cmExtraEclipseCDT4Generator::AppendLinkedResource(
  478. xml, "[Subprojects]", "virtual:/virtual", VirtualFolder);
  479. for (auto const& it : this->GlobalGenerator->GetProjectMap()) {
  480. std::string linkSourceDirectory =
  481. cmExtraEclipseCDT4Generator::GetEclipsePath(
  482. it.second[0]->GetCurrentSourceDirectory());
  483. // a linked resource must not point to a parent directory of .project or
  484. // .project itself
  485. if ((baseDir != linkSourceDirectory) &&
  486. !cmSystemTools::IsSubDirectory(baseDir, linkSourceDirectory)) {
  487. std::string linkName = cmStrCat("[Subprojects]/", it.first);
  488. cmExtraEclipseCDT4Generator::AppendLinkedResource(
  489. xml, linkName,
  490. cmExtraEclipseCDT4Generator::GetEclipsePath(linkSourceDirectory),
  491. LinkToFolder);
  492. // Don't add it to the srcLinkedResources, because listing multiple
  493. // directories confuses the Eclipse indexer (#13596).
  494. }
  495. }
  496. }
  497. void cmExtraEclipseCDT4Generator::AppendIncludeDirectories(
  498. cmXMLWriter& xml, const std::vector<std::string>& includeDirs,
  499. std::set<std::string>& emittedDirs)
  500. {
  501. for (std::string const& inc : includeDirs) {
  502. if (!inc.empty()) {
  503. std::string dir = cmSystemTools::CollapseFullPath(inc);
  504. // handle framework include dirs on OSX, the remainder after the
  505. // Frameworks/ part has to be stripped
  506. // /System/Library/Frameworks/GLUT.framework/Headers
  507. cmsys::RegularExpression frameworkRx("(.+/Frameworks)/.+\\.framework/");
  508. if (frameworkRx.find(dir)) {
  509. dir = frameworkRx.match(1);
  510. }
  511. if (emittedDirs.find(dir) == emittedDirs.end()) {
  512. emittedDirs.insert(dir);
  513. xml.StartElement("pathentry");
  514. xml.Attribute("include",
  515. cmExtraEclipseCDT4Generator::GetEclipsePath(dir));
  516. xml.Attribute("kind", "inc");
  517. xml.Attribute("path", "");
  518. xml.Attribute("system", "true");
  519. xml.EndElement();
  520. }
  521. }
  522. }
  523. }
  524. void cmExtraEclipseCDT4Generator::CreateCProjectFile() const
  525. {
  526. std::set<std::string> emmited;
  527. const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
  528. const cmMakefile* mf = lg->GetMakefile();
  529. const std::string filename = this->HomeOutputDirectory + "/.cproject";
  530. cmGeneratedFileStream fout(filename);
  531. if (!fout) {
  532. return;
  533. }
  534. cmXMLWriter xml(fout);
  535. // add header
  536. xml.StartDocument("UTF-8");
  537. xml.ProcessingInstruction("fileVersion", "4.0.0");
  538. xml.StartElement("cproject");
  539. xml.StartElement("storageModule");
  540. xml.Attribute("moduleId", "org.eclipse.cdt.core.settings");
  541. xml.StartElement("cconfiguration");
  542. xml.Attribute("id", "org.eclipse.cdt.core.default.config.1");
  543. // Configuration settings...
  544. xml.StartElement("storageModule");
  545. xml.Attribute("buildSystemId",
  546. "org.eclipse.cdt.core.defaultConfigDataProvider");
  547. xml.Attribute("id", "org.eclipse.cdt.core.default.config.1");
  548. xml.Attribute("moduleId", "org.eclipse.cdt.core.settings");
  549. xml.Attribute("name", "Configuration");
  550. xml.Element("externalSettings");
  551. xml.StartElement("extensions");
  552. // TODO: refactor this out...
  553. std::string executableFormat =
  554. mf->GetSafeDefinition("CMAKE_EXECUTABLE_FORMAT");
  555. if (executableFormat == "ELF") {
  556. xml.StartElement("extension");
  557. xml.Attribute("id", "org.eclipse.cdt.core.ELF");
  558. xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
  559. xml.EndElement(); // extension
  560. xml.StartElement("extension");
  561. xml.Attribute("id", "org.eclipse.cdt.core.GNU_ELF");
  562. xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
  563. AppendAttribute(xml, "addr2line");
  564. AppendAttribute(xml, "c++filt");
  565. xml.EndElement(); // extension
  566. } else {
  567. std::string systemName = mf->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  568. if (systemName == "CYGWIN") {
  569. xml.StartElement("extension");
  570. xml.Attribute("id", "org.eclipse.cdt.core.Cygwin_PE");
  571. xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
  572. AppendAttribute(xml, "addr2line");
  573. AppendAttribute(xml, "c++filt");
  574. AppendAttribute(xml, "cygpath");
  575. AppendAttribute(xml, "nm");
  576. xml.EndElement(); // extension
  577. } else if (systemName == "Windows") {
  578. xml.StartElement("extension");
  579. xml.Attribute("id", "org.eclipse.cdt.core.PE");
  580. xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
  581. xml.EndElement(); // extension
  582. } else if (systemName == "Darwin") {
  583. xml.StartElement("extension");
  584. xml.Attribute("id",
  585. this->SupportsMachO64Parser
  586. ? "org.eclipse.cdt.core.MachO64"
  587. : "org.eclipse.cdt.core.MachO");
  588. xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
  589. AppendAttribute(xml, "c++filt");
  590. xml.EndElement(); // extension
  591. } else {
  592. // *** Should never get here ***
  593. xml.Element("error_toolchain_type");
  594. }
  595. }
  596. xml.EndElement(); // extensions
  597. xml.EndElement(); // storageModule
  598. // ???
  599. xml.StartElement("storageModule");
  600. xml.Attribute("moduleId", "org.eclipse.cdt.core.language.mapping");
  601. xml.Element("project-mappings");
  602. xml.EndElement(); // storageModule
  603. // ???
  604. xml.StartElement("storageModule");
  605. xml.Attribute("moduleId", "org.eclipse.cdt.core.externalSettings");
  606. xml.EndElement(); // storageModule
  607. // set the path entries (includes, libs, source dirs, etc.)
  608. xml.StartElement("storageModule");
  609. xml.Attribute("moduleId", "org.eclipse.cdt.core.pathentry");
  610. // for each sub project with a linked resource to the source dir:
  611. // - make it type 'src'
  612. // - and exclude it from type 'out'
  613. std::string excludeFromOut;
  614. /* I don't know what the pathentry kind="src" are good for, e.g.
  615. * autocompletion
  616. * works also without them. Done wrong, the indexer complains, see #12417
  617. * and #12213.
  618. * According to #13596, this entry at least limits the directories the
  619. * indexer is searching for files. So now the "src" entry contains only
  620. * the linked resource to CMAKE_SOURCE_DIR.
  621. * The CDT documentation is very terse on that:
  622. * "CDT_SOURCE: Entry kind constant describing a path entry identifying a
  623. * folder containing source code to be compiled."
  624. * Also on the cdt-dev list didn't bring any information:
  625. * http://web.archiveorange.com/archive/v/B4NlJDNIpYoOS1SbxFNy
  626. * Alex */
  627. // include subprojects directory to the src pathentry
  628. // eclipse cdt indexer uses this entries as reference to index source files
  629. if (this->GenerateLinkedResources) {
  630. xml.StartElement("pathentry");
  631. xml.Attribute("kind", "src");
  632. xml.Attribute("path", "[Subprojects]");
  633. xml.EndElement();
  634. }
  635. for (std::string const& p : this->SrcLinkedResources) {
  636. xml.StartElement("pathentry");
  637. xml.Attribute("kind", "src");
  638. xml.Attribute("path", p);
  639. xml.EndElement();
  640. // exclude source directory from output search path
  641. // - only if not named the same as an output directory
  642. if (!cmSystemTools::FileIsDirectory(
  643. std::string(this->HomeOutputDirectory + "/" + p))) {
  644. excludeFromOut += p + "/|";
  645. }
  646. }
  647. excludeFromOut += "**/CMakeFiles/";
  648. xml.StartElement("pathentry");
  649. xml.Attribute("excluding", excludeFromOut);
  650. xml.Attribute("kind", "out");
  651. xml.Attribute("path", "");
  652. xml.EndElement();
  653. // add pre-processor definitions to allow eclipse to gray out sections
  654. emmited.clear();
  655. for (const auto& lgen : this->GlobalGenerator->GetLocalGenerators()) {
  656. if (cmProp cdefs =
  657. lgen->GetMakefile()->GetProperty("COMPILE_DEFINITIONS")) {
  658. // Expand the list.
  659. std::vector<std::string> defs;
  660. cmGeneratorExpression::Split(*cdefs, defs);
  661. for (std::string const& d : defs) {
  662. if (cmGeneratorExpression::Find(d) != std::string::npos) {
  663. continue;
  664. }
  665. std::string::size_type equals = d.find('=', 0);
  666. std::string::size_type enddef = d.length();
  667. std::string def;
  668. std::string val;
  669. if (equals != std::string::npos && equals < enddef) {
  670. // we have -DFOO=BAR
  671. def = d.substr(0, equals);
  672. val = d.substr(equals + 1, enddef - equals + 1);
  673. } else {
  674. // we have -DFOO
  675. def = d;
  676. }
  677. // insert the definition if not already added.
  678. if (emmited.find(def) == emmited.end()) {
  679. emmited.insert(def);
  680. xml.StartElement("pathentry");
  681. xml.Attribute("kind", "mac");
  682. xml.Attribute("name", def);
  683. xml.Attribute("path", "");
  684. xml.Attribute("value", val);
  685. xml.EndElement();
  686. }
  687. }
  688. }
  689. }
  690. // add system defined c macros
  691. const char* cDefs =
  692. mf->GetDefinition("CMAKE_EXTRA_GENERATOR_C_SYSTEM_DEFINED_MACROS");
  693. if (this->CEnabled && cDefs) {
  694. // Expand the list.
  695. std::vector<std::string> defs = cmExpandedList(cDefs, true);
  696. // the list must contain only definition-value pairs:
  697. if ((defs.size() % 2) == 0) {
  698. auto di = defs.begin();
  699. while (di != defs.end()) {
  700. std::string def = *di;
  701. ++di;
  702. std::string val;
  703. if (di != defs.end()) {
  704. val = *di;
  705. ++di;
  706. }
  707. // insert the definition if not already added.
  708. if (emmited.find(def) == emmited.end()) {
  709. emmited.insert(def);
  710. xml.StartElement("pathentry");
  711. xml.Attribute("kind", "mac");
  712. xml.Attribute("name", def);
  713. xml.Attribute("path", "");
  714. xml.Attribute("value", val);
  715. xml.EndElement();
  716. }
  717. }
  718. }
  719. }
  720. // add system defined c++ macros
  721. const char* cxxDefs =
  722. mf->GetDefinition("CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS");
  723. if (this->CXXEnabled && cxxDefs) {
  724. // Expand the list.
  725. std::vector<std::string> defs = cmExpandedList(cxxDefs, true);
  726. // the list must contain only definition-value pairs:
  727. if ((defs.size() % 2) == 0) {
  728. auto di = defs.begin();
  729. while (di != defs.end()) {
  730. std::string def = *di;
  731. ++di;
  732. std::string val;
  733. if (di != defs.end()) {
  734. val = *di;
  735. ++di;
  736. }
  737. // insert the definition if not already added.
  738. if (emmited.find(def) == emmited.end()) {
  739. emmited.insert(def);
  740. xml.StartElement("pathentry");
  741. xml.Attribute("kind", "mac");
  742. xml.Attribute("name", def);
  743. xml.Attribute("path", "");
  744. xml.Attribute("value", val);
  745. xml.EndElement();
  746. }
  747. }
  748. }
  749. }
  750. // include dirs
  751. emmited.clear();
  752. for (const auto& lgen : this->GlobalGenerator->GetLocalGenerators()) {
  753. const auto& targets = lgen->GetGeneratorTargets();
  754. for (const auto& target : targets) {
  755. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  756. continue;
  757. }
  758. std::vector<std::string> includeDirs;
  759. std::string config = mf->GetSafeDefinition("CMAKE_BUILD_TYPE");
  760. lgen->GetIncludeDirectories(includeDirs, target.get(), "C", config);
  761. this->AppendIncludeDirectories(xml, includeDirs, emmited);
  762. }
  763. }
  764. // now also the system include directories, in case we found them in
  765. // CMakeSystemSpecificInformation.cmake. This makes Eclipse find the
  766. // standard headers.
  767. std::string compiler = mf->GetSafeDefinition("CMAKE_C_COMPILER");
  768. if (this->CEnabled && !compiler.empty()) {
  769. std::string systemIncludeDirs =
  770. mf->GetSafeDefinition("CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
  771. std::vector<std::string> dirs = cmExpandedList(systemIncludeDirs);
  772. this->AppendIncludeDirectories(xml, dirs, emmited);
  773. }
  774. compiler = mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  775. if (this->CXXEnabled && !compiler.empty()) {
  776. std::string systemIncludeDirs =
  777. mf->GetSafeDefinition("CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
  778. std::vector<std::string> dirs = cmExpandedList(systemIncludeDirs);
  779. this->AppendIncludeDirectories(xml, dirs, emmited);
  780. }
  781. xml.EndElement(); // storageModule
  782. // add build targets
  783. xml.StartElement("storageModule");
  784. xml.Attribute("moduleId", "org.eclipse.cdt.make.core.buildtargets");
  785. xml.StartElement("buildTargets");
  786. emmited.clear();
  787. const std::string& make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  788. const std::string& makeArgs =
  789. mf->GetSafeDefinition("CMAKE_ECLIPSE_MAKE_ARGUMENTS");
  790. cmGlobalGenerator* generator =
  791. const_cast<cmGlobalGenerator*>(this->GlobalGenerator);
  792. std::string allTarget;
  793. std::string cleanTarget;
  794. if (generator->GetAllTargetName()) {
  795. allTarget = generator->GetAllTargetName();
  796. }
  797. if (generator->GetCleanTargetName()) {
  798. cleanTarget = generator->GetCleanTargetName();
  799. }
  800. // add all executable and library targets and some of the GLOBAL
  801. // and UTILITY targets
  802. for (const auto& lgen : this->GlobalGenerator->GetLocalGenerators()) {
  803. const auto& targets = lgen->GetGeneratorTargets();
  804. std::string subdir = lgen->MaybeConvertToRelativePath(
  805. this->HomeOutputDirectory, lgen->GetCurrentBinaryDirectory());
  806. if (subdir == ".") {
  807. subdir.clear();
  808. }
  809. for (const auto& target : targets) {
  810. std::string targetName = target->GetName();
  811. switch (target->GetType()) {
  812. case cmStateEnums::GLOBAL_TARGET: {
  813. // Only add the global targets from CMAKE_BINARY_DIR,
  814. // not from the subdirs
  815. if (subdir.empty()) {
  816. cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
  817. makeArgs, subdir, ": ");
  818. }
  819. } break;
  820. case cmStateEnums::UTILITY:
  821. // Add all utility targets, except the Nightly/Continuous/
  822. // Experimental-"sub"targets as e.g. NightlyStart
  823. if ((cmHasLiteralPrefix(targetName, "Nightly") &&
  824. (targetName != "Nightly")) ||
  825. (cmHasLiteralPrefix(targetName, "Continuous") &&
  826. (targetName != "Continuous")) ||
  827. (cmHasLiteralPrefix(targetName, "Experimental") &&
  828. (targetName != "Experimental"))) {
  829. break;
  830. }
  831. cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
  832. makeArgs, subdir, ": ");
  833. break;
  834. case cmStateEnums::EXECUTABLE:
  835. case cmStateEnums::STATIC_LIBRARY:
  836. case cmStateEnums::SHARED_LIBRARY:
  837. case cmStateEnums::MODULE_LIBRARY:
  838. case cmStateEnums::OBJECT_LIBRARY: {
  839. const char* prefix =
  840. (target->GetType() == cmStateEnums::EXECUTABLE ? "[exe] "
  841. : "[lib] ");
  842. cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
  843. makeArgs, subdir, prefix);
  844. std::string fastTarget = cmStrCat(targetName, "/fast");
  845. cmExtraEclipseCDT4Generator::AppendTarget(xml, fastTarget, make,
  846. makeArgs, subdir, prefix);
  847. // Add Build and Clean targets in the virtual folder of targets:
  848. if (this->SupportsVirtualFolders) {
  849. std::string virtDir = cmStrCat("[Targets]/", prefix, targetName);
  850. std::string buildArgs =
  851. cmStrCat("-C \"", lgen->GetBinaryDirectory(), "\" ", makeArgs);
  852. cmExtraEclipseCDT4Generator::AppendTarget(
  853. xml, "Build", make, buildArgs, virtDir, "", targetName.c_str());
  854. std::string cleanArgs =
  855. cmStrCat("-E chdir \"", lgen->GetCurrentBinaryDirectory(),
  856. "\" \"", cmSystemTools::GetCMakeCommand(), "\" -P \"");
  857. cleanArgs += lgen->GetTargetDirectory(target.get());
  858. cleanArgs += "/cmake_clean.cmake\"";
  859. cmExtraEclipseCDT4Generator::AppendTarget(
  860. xml, "Clean", cmSystemTools::GetCMakeCommand(), cleanArgs,
  861. virtDir, "", "");
  862. }
  863. } break;
  864. case cmStateEnums::INTERFACE_LIBRARY:
  865. default:
  866. break;
  867. }
  868. }
  869. // insert the all and clean targets in every subdir
  870. if (!allTarget.empty()) {
  871. cmExtraEclipseCDT4Generator::AppendTarget(xml, allTarget, make, makeArgs,
  872. subdir, ": ");
  873. }
  874. if (!cleanTarget.empty()) {
  875. cmExtraEclipseCDT4Generator::AppendTarget(xml, cleanTarget, make,
  876. makeArgs, subdir, ": ");
  877. }
  878. // insert rules for compiling, preprocessing and assembling individual
  879. // files
  880. std::vector<std::string> objectFileTargets;
  881. lg->GetIndividualFileTargets(objectFileTargets);
  882. for (std::string const& f : objectFileTargets) {
  883. const char* prefix = "[obj] ";
  884. if (f.back() == 's') {
  885. prefix = "[to asm] ";
  886. } else if (f.back() == 'i') {
  887. prefix = "[pre] ";
  888. }
  889. cmExtraEclipseCDT4Generator::AppendTarget(xml, f, make, makeArgs, subdir,
  890. prefix);
  891. }
  892. }
  893. xml.EndElement(); // buildTargets
  894. xml.EndElement(); // storageModule
  895. cmExtraEclipseCDT4Generator::AppendStorageScanners(xml, *mf);
  896. xml.EndElement(); // cconfiguration
  897. xml.EndElement(); // storageModule
  898. xml.StartElement("storageModule");
  899. xml.Attribute("moduleId", "cdtBuildSystem");
  900. xml.Attribute("version", "4.0.0");
  901. xml.StartElement("project");
  902. xml.Attribute("id", std::string(lg->GetProjectName()) + ".null.1");
  903. xml.Attribute("name", lg->GetProjectName());
  904. xml.EndElement(); // project
  905. xml.EndElement(); // storageModule
  906. // Append additional cproject contents without applying any XML formatting
  907. if (cmProp extraCProjectContents =
  908. mf->GetState()->GetGlobalProperty("ECLIPSE_EXTRA_CPROJECT_CONTENTS")) {
  909. fout << *extraCProjectContents;
  910. }
  911. xml.EndElement(); // cproject
  912. }
  913. std::string cmExtraEclipseCDT4Generator::GetEclipsePath(
  914. const std::string& path)
  915. {
  916. #if defined(__CYGWIN__)
  917. std::string cmd = "cygpath -m " + path;
  918. std::string out;
  919. if (!cmSystemTools::RunSingleCommand(cmd.c_str(), &out, &out)) {
  920. return path;
  921. } else {
  922. out.erase(out.find_last_of('\n'));
  923. return out;
  924. }
  925. #else
  926. return path;
  927. #endif
  928. }
  929. std::string cmExtraEclipseCDT4Generator::GetPathBasename(
  930. const std::string& path)
  931. {
  932. std::string outputBasename = path;
  933. while (!outputBasename.empty() &&
  934. (outputBasename.back() == '/' || outputBasename.back() == '\\')) {
  935. outputBasename.resize(outputBasename.size() - 1);
  936. }
  937. std::string::size_type loc = outputBasename.find_last_of("/\\");
  938. if (loc != std::string::npos) {
  939. outputBasename = outputBasename.substr(loc + 1);
  940. }
  941. return outputBasename;
  942. }
  943. std::string cmExtraEclipseCDT4Generator::GenerateProjectName(
  944. const std::string& name, const std::string& type, const std::string& path)
  945. {
  946. return name + (type.empty() ? "" : "-") + type + "@" + path;
  947. }
  948. // Helper functions
  949. void cmExtraEclipseCDT4Generator::AppendStorageScanners(
  950. cmXMLWriter& xml, const cmMakefile& makefile)
  951. {
  952. // we need the "make" and the C (or C++) compiler which are used, Alex
  953. const std::string& make =
  954. makefile.GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  955. std::string compiler = makefile.GetSafeDefinition("CMAKE_C_COMPILER");
  956. std::string arg1 = makefile.GetSafeDefinition("CMAKE_C_COMPILER_ARG1");
  957. if (compiler.empty()) {
  958. compiler = makefile.GetSafeDefinition("CMAKE_CXX_COMPILER");
  959. arg1 = makefile.GetSafeDefinition("CMAKE_CXX_COMPILER_ARG1");
  960. }
  961. if (compiler.empty()) // Hmm, what to do now ?
  962. {
  963. compiler = "gcc";
  964. }
  965. // the following right now hardcodes gcc behaviour :-/
  966. std::string compilerArgs =
  967. "-E -P -v -dD ${plugin_state_location}/${specs_file}";
  968. if (!arg1.empty()) {
  969. arg1 += " ";
  970. compilerArgs = arg1 + compilerArgs;
  971. }
  972. xml.StartElement("storageModule");
  973. xml.Attribute("moduleId", "scannerConfiguration");
  974. xml.StartElement("autodiscovery");
  975. xml.Attribute("enabled", "true");
  976. xml.Attribute("problemReportingEnabled", "true");
  977. xml.Attribute("selectedProfileId",
  978. "org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile");
  979. xml.EndElement(); // autodiscovery
  980. cmExtraEclipseCDT4Generator::AppendScannerProfile(
  981. xml, "org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile", true,
  982. "", true, "specsFile", compilerArgs, compiler, true, true);
  983. cmExtraEclipseCDT4Generator::AppendScannerProfile(
  984. xml, "org.eclipse.cdt.make.core.GCCStandardMakePerFileProfile", true, "",
  985. true, "makefileGenerator", "-f ${project_name}_scd.mk", make, true, true);
  986. xml.EndElement(); // storageModule
  987. }
  988. // The prefix is prepended before the actual name of the target. The purpose
  989. // of that is to sort the targets in the view of Eclipse, so that at first
  990. // the global/utility/all/clean targets appear ": ", then the executable
  991. // targets "[exe] ", then the libraries "[lib]", then the rules for the
  992. // object files "[obj]", then for preprocessing only "[pre] " and
  993. // finally the assembly files "[to asm] ". Note the "to" in "to asm",
  994. // without it, "asm" would be the first targets in the list, with the "to"
  995. // they are the last targets, which makes more sense.
  996. void cmExtraEclipseCDT4Generator::AppendTarget(
  997. cmXMLWriter& xml, const std::string& target, const std::string& make,
  998. const std::string& makeArgs, const std::string& path, const char* prefix,
  999. const char* makeTarget)
  1000. {
  1001. xml.StartElement("target");
  1002. xml.Attribute("name", prefix + target);
  1003. xml.Attribute("path", path);
  1004. xml.Attribute("targetID", "org.eclipse.cdt.make.MakeTargetBuilder");
  1005. xml.Element("buildCommand",
  1006. cmExtraEclipseCDT4Generator::GetEclipsePath(make));
  1007. xml.Element("buildArguments", makeArgs);
  1008. xml.Element("buildTarget", makeTarget ? makeTarget : target.c_str());
  1009. xml.Element("stopOnError", "true");
  1010. xml.Element("useDefaultCommand", "false");
  1011. xml.EndElement();
  1012. }
  1013. void cmExtraEclipseCDT4Generator::AppendScannerProfile(
  1014. cmXMLWriter& xml, const std::string& profileID, bool openActionEnabled,
  1015. const std::string& openActionFilePath, bool pParserEnabled,
  1016. const std::string& scannerInfoProviderID,
  1017. const std::string& runActionArguments, const std::string& runActionCommand,
  1018. bool runActionUseDefault, bool sipParserEnabled)
  1019. {
  1020. xml.StartElement("profile");
  1021. xml.Attribute("id", profileID);
  1022. xml.StartElement("buildOutputProvider");
  1023. xml.StartElement("openAction");
  1024. xml.Attribute("enabled", openActionEnabled ? "true" : "false");
  1025. xml.Attribute("filePath", openActionFilePath);
  1026. xml.EndElement(); // openAction
  1027. xml.StartElement("parser");
  1028. xml.Attribute("enabled", pParserEnabled ? "true" : "false");
  1029. xml.EndElement(); // parser
  1030. xml.EndElement(); // buildOutputProvider
  1031. xml.StartElement("scannerInfoProvider");
  1032. xml.Attribute("id", scannerInfoProviderID);
  1033. xml.StartElement("runAction");
  1034. xml.Attribute("arguments", runActionArguments);
  1035. xml.Attribute("command", runActionCommand);
  1036. xml.Attribute("useDefault", runActionUseDefault ? "true" : "false");
  1037. xml.EndElement(); // runAction
  1038. xml.StartElement("parser");
  1039. xml.Attribute("enabled", sipParserEnabled ? "true" : "false");
  1040. xml.EndElement(); // parser
  1041. xml.EndElement(); // scannerInfoProvider
  1042. xml.EndElement(); // profile
  1043. }
  1044. void cmExtraEclipseCDT4Generator::AppendLinkedResource(cmXMLWriter& xml,
  1045. const std::string& name,
  1046. const std::string& path,
  1047. LinkType linkType)
  1048. {
  1049. const char* locationTag = "location";
  1050. int typeTag = 2;
  1051. if (linkType == VirtualFolder) // ... and not a linked folder
  1052. {
  1053. locationTag = "locationURI";
  1054. }
  1055. if (linkType == LinkToFile) {
  1056. typeTag = 1;
  1057. }
  1058. xml.StartElement("link");
  1059. xml.Element("name", name);
  1060. xml.Element("type", typeTag);
  1061. xml.Element(locationTag, path);
  1062. xml.EndElement();
  1063. }