cmExtraEclipseCDT4Generator.cxx 43 KB

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