cmExtraEclipseCDT4Generator.cxx 43 KB

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