1
0

cmExtraEclipseCDT4Generator.cxx 43 KB

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