cmExtraEclipseCDT4Generator.cxx 43 KB

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