cmExtraEclipseCDT4Generator.cxx 43 KB

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