cmExtraEclipseCDT4Generator.cxx 43 KB

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