cmExtraEclipseCDT4Generator.cxx 43 KB

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