cmExtraEclipseCDT4Generator.cxx 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. Copyright (c) 2004 Alexander Neundorf [email protected], All rights reserved.
  9. Copyright (c) 2007 Miguel A. Figueroa-Villanueva. All rights reserved.
  10. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  11. This software is distributed WITHOUT ANY WARRANTY; without even
  12. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE. See the above copyright notices for more information.
  14. =========================================================================*/
  15. #include "cmExtraEclipseCDT4Generator.h"
  16. #include "cmGlobalUnixMakefileGenerator3.h"
  17. #include "cmLocalUnixMakefileGenerator3.h"
  18. #include "cmMakefile.h"
  19. #include "cmGeneratedFileStream.h"
  20. #include "cmTarget.h"
  21. #include "cmSystemTools.h"
  22. #include <stdlib.h>
  23. #include <assert.h>
  24. //----------------------------------------------------------------------------
  25. cmExtraEclipseCDT4Generator
  26. ::cmExtraEclipseCDT4Generator() : cmExternalMakefileProjectGenerator()
  27. {
  28. // TODO: Verify if __CYGWIN__ should be checked.
  29. //#if defined(_WIN32) && !defined(__CYGWIN__)
  30. #if defined(_WIN32)
  31. this->SupportedGlobalGenerators.push_back("NMake Makefiles");
  32. this->SupportedGlobalGenerators.push_back("MinGW Makefiles");
  33. // this->SupportedGlobalGenerators.push_back("MSYS Makefiles");
  34. #endif
  35. this->SupportedGlobalGenerators.push_back("Unix Makefiles");
  36. }
  37. //----------------------------------------------------------------------------
  38. void cmExtraEclipseCDT4Generator
  39. ::GetDocumentation(cmDocumentationEntry& entry, const char*) const
  40. {
  41. entry.Name = this->GetName();
  42. entry.Brief = "Generates Eclipse CDT 4.0 project files.";
  43. entry.Full =
  44. "Project files for Eclipse will be created in the top directory "
  45. "and will have a linked resource to every subdirectory which "
  46. "features a CMakeLists.txt file containing a PROJECT() call."
  47. "Additionally a hierarchy of makefiles is generated into the "
  48. "build tree. The appropriate make program can build the project through "
  49. "the default make target. A \"make install\" target is also provided.";
  50. }
  51. //----------------------------------------------------------------------------
  52. void cmExtraEclipseCDT4Generator
  53. ::SetGlobalGenerator(cmGlobalGenerator* generator)
  54. {
  55. cmExternalMakefileProjectGenerator::SetGlobalGenerator(generator);
  56. cmGlobalUnixMakefileGenerator3* mf
  57. = static_cast<cmGlobalUnixMakefileGenerator3*>(generator);
  58. mf->SetToolSupportsColor(true);
  59. }
  60. //----------------------------------------------------------------------------
  61. void cmExtraEclipseCDT4Generator::Generate()
  62. {
  63. const cmMakefile* mf
  64. = this->GlobalGenerator->GetLocalGenerators()[0]->GetMakefile();
  65. // TODO: Decide if these are local or member variables
  66. this->HomeDirectory = mf->GetHomeDirectory();
  67. this->HomeOutputDirectory = mf->GetHomeOutputDirectory();
  68. this->IsOutOfSourceBuild = (this->HomeDirectory!=this->HomeOutputDirectory);
  69. this->GenerateSourceProject = (this->IsOutOfSourceBuild &&
  70. mf->IsOn("ECLIPSE_CDT4_GENERATE_SOURCE_PROJECT"));
  71. // NOTE: This is not good, since it pollutes the source tree. However,
  72. // Eclipse doesn't allow CVS/SVN to work when the .project is not in
  73. // the cvs/svn root directory. Hence, this is provided as an option.
  74. if (this->GenerateSourceProject)
  75. {
  76. // create .project file in the source tree
  77. this->CreateSourceProjectFile();
  78. }
  79. // create a .project file
  80. this->CreateProjectFile();
  81. // create a .cproject file
  82. this->CreateCProjectFile();
  83. }
  84. void cmExtraEclipseCDT4Generator::CreateSourceProjectFile() const
  85. {
  86. assert(this->HomeDirectory != this->HomeOutputDirectory);
  87. // set up the project name: <project>-Source@<baseSourcePathName>
  88. const cmMakefile* mf
  89. = this->GlobalGenerator->GetLocalGenerators()[0]->GetMakefile();
  90. std::string name = this->GenerateProjectName(mf->GetProjectName(), "Source",
  91. this->GetPathBasename(this->HomeDirectory));
  92. const std::string filename = this->HomeDirectory + "/.project";
  93. cmGeneratedFileStream fout(filename.c_str());
  94. if (!fout)
  95. {
  96. return;
  97. }
  98. fout <<
  99. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  100. "<projectDescription>\n"
  101. "\t<name>" << name << "</name>\n"
  102. "\t<comment></comment>\n"
  103. "\t<projects>\n"
  104. "\t</projects>\n"
  105. "\t<buildSpec>\n"
  106. "\t</buildSpec>\n"
  107. "\t<natures>\n"
  108. "\t</natures>\n"
  109. "</projectDescription>\n"
  110. ;
  111. }
  112. //----------------------------------------------------------------------------
  113. void cmExtraEclipseCDT4Generator::CreateProjectFile()
  114. {
  115. const cmMakefile* mf
  116. = this->GlobalGenerator->GetLocalGenerators()[0]->GetMakefile();
  117. const std::string filename = this->HomeOutputDirectory + "/.project";
  118. cmGeneratedFileStream fout(filename.c_str());
  119. if (!fout)
  120. {
  121. return;
  122. }
  123. fout <<
  124. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  125. "<projectDescription>\n"
  126. "\t<name>" <<
  127. this->GenerateProjectName(mf->GetProjectName(),
  128. mf->GetDefinition("CMAKE_BUILD_TYPE"),
  129. this->GetPathBasename(this->HomeOutputDirectory))
  130. << "</name>\n"
  131. "\t<comment></comment>\n"
  132. "\t<projects>\n"
  133. "\t</projects>\n"
  134. "\t<buildSpec>\n"
  135. "\t\t<buildCommand>\n"
  136. "\t\t\t<name>org.eclipse.cdt.make.core.makeBuilder</name>\n"
  137. "\t\t\t<triggers>clean,full,incremental,</triggers>\n"
  138. "\t\t\t<arguments>\n"
  139. ;
  140. // use clean target
  141. fout <<
  142. "\t\t\t\t<dictionary>\n"
  143. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.cleanBuildTarget</key>\n"
  144. "\t\t\t\t\t<value>clean</value>\n"
  145. "\t\t\t\t</dictionary>\n"
  146. "\t\t\t\t<dictionary>\n"
  147. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.enableCleanBuild</key>\n"
  148. "\t\t\t\t\t<value>true</value>\n"
  149. "\t\t\t\t</dictionary>\n"
  150. "\t\t\t\t<dictionary>\n"
  151. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.append_environment</key>\n"
  152. "\t\t\t\t\t<value>true</value>\n"
  153. "\t\t\t\t</dictionary>\n"
  154. "\t\t\t\t<dictionary>\n"
  155. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.stopOnError</key>\n"
  156. "\t\t\t\t\t<value>true</value>\n"
  157. "\t\t\t\t</dictionary>\n"
  158. ;
  159. // set the make command
  160. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  161. fout <<
  162. "\t\t\t\t<dictionary>\n"
  163. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.enabledIncrementalBuild</key>\n"
  164. "\t\t\t\t\t<value>true</value>\n"
  165. "\t\t\t\t</dictionary>\n"
  166. "\t\t\t\t<dictionary>\n"
  167. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.build.command</key>\n"
  168. "\t\t\t\t\t<value>" + this->GetEclipsePath(make) + "</value>\n"
  169. "\t\t\t\t</dictionary>\n"
  170. "\t\t\t\t<dictionary>\n"
  171. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.contents</key>\n"
  172. "\t\t\t\t\t<value>org.eclipse.cdt.make.core.activeConfigSettings</value>\n"
  173. "\t\t\t\t</dictionary>\n"
  174. "\t\t\t\t<dictionary>\n"
  175. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.build.target.inc</key>\n"
  176. "\t\t\t\t\t<value>all</value>\n"
  177. "\t\t\t\t</dictionary>\n"
  178. "\t\t\t\t<dictionary>\n"
  179. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.build.arguments</key>\n"
  180. "\t\t\t\t\t<value></value>\n"
  181. "\t\t\t\t</dictionary>\n"
  182. "\t\t\t\t<dictionary>\n"
  183. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.buildLocation</key>\n"
  184. "\t\t\t\t\t<value>"
  185. << this->GetEclipsePath(this->HomeOutputDirectory) << "</value>\n"
  186. "\t\t\t\t</dictionary>\n"
  187. "\t\t\t\t<dictionary>\n"
  188. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>\n"
  189. "\t\t\t\t\t<value>false</value>\n"
  190. "\t\t\t\t</dictionary>\n"
  191. ;
  192. // set project specific environment
  193. fout <<
  194. "\t\t\t\t<dictionary>\n"
  195. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.environment</key>\n"
  196. "\t\t\t\t\t<value>VERBOSE=1|</value>\n" // enforce VERBOSE Makefile output
  197. "\t\t\t\t\t<value>"
  198. ;
  199. // set vsvars32.bat environment available at CMake time,
  200. // but not necessarily when eclipse is open
  201. if (make.find("nmake") != std::string::npos)
  202. {
  203. if (getenv("PATH"))
  204. {
  205. fout << "PATH=" << getenv("PATH") << "|";
  206. }
  207. if (getenv("INCLUDE"))
  208. {
  209. fout << "INCLUDE=" << getenv("INCLUDE") << "|";
  210. }
  211. if (getenv("LIB"))
  212. {
  213. fout << "LIB=" << getenv("LIB") << "|";
  214. }
  215. if (getenv("LIBPATH"))
  216. {
  217. fout << "LIBPATH=" << getenv("LIBPATH") << "|";
  218. }
  219. }
  220. fout <<
  221. "</value>\n"
  222. "\t\t\t\t</dictionary>\n"
  223. ;
  224. fout <<
  225. "\t\t\t\t<dictionary>\n"
  226. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.enableFullBuild</key>\n"
  227. "\t\t\t\t\t<value>true</value>\n"
  228. "\t\t\t\t</dictionary>\n"
  229. "\t\t\t\t<dictionary>\n"
  230. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.build.target.auto</key>\n"
  231. "\t\t\t\t\t<value>all</value>\n"
  232. "\t\t\t\t</dictionary>\n"
  233. "\t\t\t\t<dictionary>\n"
  234. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.enableAutoBuild</key>\n"
  235. "\t\t\t\t\t<value>false</value>\n"
  236. "\t\t\t\t</dictionary>\n"
  237. "\t\t\t\t<dictionary>\n"
  238. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.build.target.clean</key>\n"
  239. "\t\t\t\t\t<value>clean</value>\n"
  240. "\t\t\t\t</dictionary>\n"
  241. "\t\t\t\t<dictionary>\n"
  242. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.fullBuildTarget</key>\n"
  243. "\t\t\t\t\t<value>all</value>\n"
  244. "\t\t\t\t</dictionary>\n"
  245. "\t\t\t\t<dictionary>\n"
  246. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.buildArguments</key>\n"
  247. "\t\t\t\t\t<value></value>\n"
  248. "\t\t\t\t</dictionary>\n"
  249. "\t\t\t\t<dictionary>\n"
  250. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.build.location</key>\n"
  251. "\t\t\t\t\t<value>"
  252. << this->GetEclipsePath(this->HomeOutputDirectory) << "</value>\n"
  253. "\t\t\t\t</dictionary>\n"
  254. "\t\t\t\t<dictionary>\n"
  255. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.autoBuildTarget</key>\n"
  256. "\t\t\t\t\t<value>all</value>\n"
  257. "\t\t\t\t</dictionary>\n"
  258. ;
  259. // set error parsers
  260. fout <<
  261. "\t\t\t\t<dictionary>\n"
  262. "\t\t\t\t\t<key>org.eclipse.cdt.core.errorOutputParser</key>\n"
  263. "\t\t\t\t\t<value>"
  264. ;
  265. if (this->GetToolChainType(*mf) == EclipseToolchainOther)
  266. {
  267. fout << "org.eclipse.cdt.core.VCErrorParser;";
  268. }
  269. fout <<
  270. "org.eclipse.cdt.core.MakeErrorParser;"
  271. "org.eclipse.cdt.core.GCCErrorParser;"
  272. "org.eclipse.cdt.core.GASErrorParser;"
  273. "org.eclipse.cdt.core.GLDErrorParser;"
  274. "</value>\n"
  275. "\t\t\t\t</dictionary>\n"
  276. ;
  277. fout <<
  278. "\t\t\t</arguments>\n"
  279. "\t\t</buildCommand>\n"
  280. "\t\t<buildCommand>\n"
  281. "\t\t\t<name>org.eclipse.cdt.make.core.ScannerConfigBuilder</name>\n"
  282. "\t\t\t<arguments>\n"
  283. "\t\t\t</arguments>\n"
  284. "\t\t</buildCommand>\n"
  285. "\t</buildSpec>\n"
  286. ;
  287. // set natures for c/c++ projects
  288. fout <<
  289. "\t<natures>\n"
  290. // TODO: ccnature only if it is c++ ???
  291. "\t\t<nature>org.eclipse.cdt.core.ccnature</nature>\n"
  292. "\t\t<nature>org.eclipse.cdt.make.core.makeNature</nature>\n"
  293. "\t\t<nature>org.eclipse.cdt.make.core.ScannerConfigNature</nature>\n"
  294. "\t\t<nature>org.eclipse.cdt.core.cnature</nature>\n"
  295. "\t</natures>\n"
  296. ;
  297. // TODO: refactor this
  298. // create linked resources
  299. if (this->IsOutOfSourceBuild)
  300. {
  301. fout << "\t<linkedResources>\n";
  302. // for each sub project create a linked resource to the source dir
  303. // - only if it is an out-of-source build
  304. for (std::map<cmStdString, std::vector<cmLocalGenerator*> >::const_iterator
  305. it = this->GlobalGenerator->GetProjectMap().begin();
  306. it != this->GlobalGenerator->GetProjectMap().end();
  307. ++it)
  308. {
  309. std::string linkSourceDirectory = this->GetEclipsePath(
  310. it->second[0]->GetMakefile()->GetStartDirectory());
  311. // .project dir can't be subdir of a linked resource dir
  312. if (!cmSystemTools::IsSubDirectory(this->HomeOutputDirectory.c_str(),
  313. linkSourceDirectory.c_str()))
  314. {
  315. this->AppendLinkedResource(fout, it->first,
  316. this->GetEclipsePath(linkSourceDirectory));
  317. this->SrcLinkedResources.push_back(it->first);
  318. }
  319. }
  320. // for EXECUTABLE_OUTPUT_PATH when not in binary dir
  321. this->AppendOutLinkedResource(fout,
  322. mf->GetSafeDefinition("CMAKE_RUNTIME_OUTPUT_DIRECTORY"),
  323. mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH"));
  324. // for LIBRARY_OUTPUT_PATH when not in binary dir
  325. this->AppendOutLinkedResource(fout,
  326. mf->GetSafeDefinition("CMAKE_LIBRARY_OUTPUT_DIRECTORY"),
  327. mf->GetSafeDefinition("LIBRARY_OUTPUT_PATH"));
  328. fout << "\t</linkedResources>\n";
  329. }
  330. fout << "</projectDescription>\n";
  331. }
  332. //----------------------------------------------------------------------------
  333. void cmExtraEclipseCDT4Generator::AppendIncludeDirectories(
  334. cmGeneratedFileStream& fout,
  335. const std::vector<std::string>& includeDirs,
  336. std::set<std::string>& emittedDirs)
  337. {
  338. for(std::vector<std::string>::const_iterator inc = includeDirs.begin();
  339. inc != includeDirs.end();
  340. ++inc)
  341. {
  342. if (!inc->empty())
  343. {
  344. std::string dir = cmSystemTools::CollapseFullPath(inc->c_str());
  345. if(emittedDirs.find(dir) == emittedDirs.end())
  346. {
  347. emittedDirs.insert(dir);
  348. fout << "<pathentry include=\""
  349. << cmExtraEclipseCDT4Generator::GetEclipsePath(dir)
  350. << "\" kind=\"inc\" path=\"\" system=\"true\"/>\n";
  351. }
  352. }
  353. }
  354. }
  355. //----------------------------------------------------------------------------
  356. void cmExtraEclipseCDT4Generator::CreateCProjectFile() const
  357. {
  358. std::set<std::string> emmited;
  359. const cmMakefile* mf
  360. = this->GlobalGenerator->GetLocalGenerators()[0]->GetMakefile();
  361. const std::string filename = this->HomeOutputDirectory + "/.cproject";
  362. cmGeneratedFileStream fout(filename.c_str());
  363. if (!fout)
  364. {
  365. return;
  366. }
  367. // add header
  368. fout <<
  369. "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
  370. "<?fileVersion 4.0.0?>\n\n"
  371. "<cproject>\n"
  372. "<storageModule moduleId=\"org.eclipse.cdt.core.settings\">\n"
  373. ;
  374. fout << "<cconfiguration id=\"org.eclipse.cdt.core.default.config.1\">\n";
  375. // Configuration settings...
  376. fout <<
  377. "<storageModule"
  378. " buildSystemId=\"org.eclipse.cdt.core.defaultConfigDataProvider\""
  379. " id=\"org.eclipse.cdt.core.default.config.1\""
  380. " moduleId=\"org.eclipse.cdt.core.settings\" name=\"Configuration\">\n"
  381. "<externalSettings/>\n"
  382. "<extensions>\n"
  383. ;
  384. // TODO: refactor this out...
  385. switch (this->GetToolChainType(*mf))
  386. {
  387. case EclipseToolchainLinux :
  388. fout << "<extension id=\"org.eclipse.cdt.core.ELF\""
  389. " point=\"org.eclipse.cdt.core.BinaryParser\"/>\n"
  390. ;
  391. fout << "<extension id=\"org.eclipse.cdt.core.GNU_ELF\""
  392. " point=\"org.eclipse.cdt.core.BinaryParser\">\n"
  393. "<attribute key=\"addr2line\" value=\"addr2line\"/>\n"
  394. "<attribute key=\"c++filt\" value=\"c++filt\"/>\n"
  395. "</extension>\n"
  396. ;
  397. break;
  398. case EclipseToolchainCygwin :
  399. fout << "<extension id=\"org.eclipse.cdt.core.Cygwin_PE\""
  400. " point=\"org.eclipse.cdt.core.BinaryParser\">\n"
  401. "<attribute key=\"addr2line\" value=\"addr2line\"/>\n"
  402. "<attribute key=\"c++filt\" value=\"c++filt\"/>\n"
  403. "<attribute key=\"cygpath\" value=\"cygpath\"/>\n"
  404. "<attribute key=\"nm\" value=\"nm\"/>\n"
  405. "</extension>\n"
  406. ;
  407. break;
  408. case EclipseToolchainMinGW :
  409. fout << "<extension id=\"org.eclipse.cdt.core.PE\""
  410. " point=\"org.eclipse.cdt.core.BinaryParser\"/>\n"
  411. ;
  412. break;
  413. case EclipseToolchainSolaris :
  414. fout << "<extension id=\"org.eclipse.cdt.core.ELF\""
  415. " point=\"org.eclipse.cdt.core.BinaryParser\"/>\n"
  416. ;
  417. break;
  418. case EclipseToolchainMacOSX :
  419. fout << "<extension id=\"org.eclipse.cdt.core.MachO\""
  420. " point=\"org.eclipse.cdt.core.BinaryParser\">\n"
  421. "<attribute key=\"c++filt\" value=\"c++filt\"/>\n"
  422. "</extension>\n"
  423. ;
  424. break;
  425. case EclipseToolchainOther :
  426. fout << "<extension id=\"org.eclipse.cdt.core.PE\""
  427. " point=\"org.eclipse.cdt.core.BinaryParser\"/>\n"
  428. ;
  429. fout << "<extension id=\"org.eclipse.cdt.core.ELF\""
  430. " point=\"org.eclipse.cdt.core.BinaryParser\"/>\n"
  431. ;
  432. break;
  433. default :
  434. // *** Should never get here ***
  435. fout << "<error_toolchain_type/>\n";
  436. }
  437. fout << "</extensions>\n"
  438. "</storageModule>\n"
  439. ;
  440. // ???
  441. fout <<
  442. "<storageModule moduleId=\"org.eclipse.cdt.core.language.mapping\">\n"
  443. "<project-mappings/>\n"
  444. "</storageModule>\n"
  445. ;
  446. // ???
  447. fout<<"<storageModule moduleId=\"org.eclipse.cdt.core.externalSettings\"/>\n"
  448. ;
  449. // set the path entries (includes, libs, source dirs, etc.)
  450. fout << "<storageModule moduleId=\"org.eclipse.cdt.core.pathentry\">\n"
  451. ;
  452. // for each sub project with a linked resource to the source dir:
  453. // - make it type 'src'
  454. // - and exclude it from type 'out'
  455. std::string excludeFromOut;
  456. for (std::vector<std::string>::const_iterator
  457. it = this->SrcLinkedResources.begin();
  458. it != this->SrcLinkedResources.end();
  459. ++it)
  460. {
  461. fout << "<pathentry kind=\"src\" path=\"" << *it << "\"/>\n";
  462. // exlude source directory from output search path
  463. // - only if not named the same as an output directory
  464. if (!cmSystemTools::FileIsDirectory(
  465. std::string(this->HomeOutputDirectory + "/" + *it).c_str()))
  466. {
  467. excludeFromOut += *it + "/|";
  468. }
  469. }
  470. excludeFromOut += "**/CMakeFiles/";
  471. fout << "<pathentry excluding=\"" << excludeFromOut
  472. << "\" kind=\"out\" path=\"\"/>\n";
  473. // add output entry for EXECUTABLE_OUTPUT_PATH and LIBRARY_OUTPUT_PATH
  474. // - if it is a subdir of homeOutputDirectory, there is no need to add it
  475. // - if it is not then create a linked resource and add the linked name
  476. // but check it doesn't conflict with other linked resources names
  477. for (std::vector<std::string>::const_iterator
  478. it = this->OutLinkedResources.begin();
  479. it != this->OutLinkedResources.end();
  480. ++it)
  481. {
  482. fout << "<pathentry kind=\"out\" path=\"" << *it << "\"/>\n";
  483. }
  484. // add pre-processor definitions to allow eclipse to gray out sections
  485. emmited.clear();
  486. for (std::vector<cmLocalGenerator*>::const_iterator
  487. it = this->GlobalGenerator->GetLocalGenerators().begin();
  488. it != this->GlobalGenerator->GetLocalGenerators().end();
  489. ++it)
  490. {
  491. if(const char* cdefs = (*it)->GetMakefile()->GetProperty(
  492. "COMPILE_DEFINITIONS"))
  493. {
  494. // Expand the list.
  495. std::vector<std::string> defs;
  496. cmSystemTools::ExpandListArgument(cdefs, defs);
  497. for(std::vector<std::string>::const_iterator di = defs.begin();
  498. di != defs.end(); ++di)
  499. {
  500. std::string::size_type equals = di->find('=', 0);
  501. std::string::size_type enddef = di->length();
  502. std::string def;
  503. std::string val;
  504. if (equals != std::string::npos && equals < enddef)
  505. {
  506. // we have -DFOO=BAR
  507. def = di->substr(0, equals);
  508. val = di->substr(equals + 1, enddef - equals + 1);
  509. }
  510. else
  511. {
  512. // we have -DFOO
  513. def = *di;
  514. }
  515. // insert the definition if not already added.
  516. if(emmited.find(def) == emmited.end())
  517. {
  518. emmited.insert(def);
  519. fout << "<pathentry kind=\"mac\" name=\"" << def
  520. << "\" path=\"\" value=\"" << this->EscapeForXML(val)
  521. << "\"/>\n";
  522. }
  523. }
  524. }
  525. }
  526. // include dirs
  527. emmited.clear();
  528. for (std::vector<cmLocalGenerator*>::const_iterator
  529. it = this->GlobalGenerator->GetLocalGenerators().begin();
  530. it != this->GlobalGenerator->GetLocalGenerators().end();
  531. ++it)
  532. {
  533. const std::vector<std::string>& includeDirs
  534. = (*it)->GetMakefile()->GetIncludeDirectories();
  535. this->AppendIncludeDirectories(fout, includeDirs, emmited);
  536. }
  537. // now also the system include directories, in case we found them in
  538. // CMakeSystemSpecificInformation.cmake. This makes Eclipse find the
  539. // standard headers.
  540. mf->GetDefinition("CMAKE_ECLIPSE_C_SYSTEM_INCLUDE_DIRS");
  541. std::string compiler = mf->GetSafeDefinition("CMAKE_C_COMPILER");
  542. if (!compiler.empty())
  543. {
  544. std::string systemIncludeDirs = mf->GetSafeDefinition(
  545. "CMAKE_ECLIPSE_C_SYSTEM_INCLUDE_DIRS");
  546. std::vector<std::string> dirs;
  547. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  548. this->AppendIncludeDirectories(fout, dirs, emmited);
  549. }
  550. compiler = mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  551. if (!compiler.empty())
  552. {
  553. std::string systemIncludeDirs = mf->GetSafeDefinition(
  554. "CMAKE_ECLIPSE_CXX_SYSTEM_INCLUDE_DIRS");
  555. std::vector<std::string> dirs;
  556. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  557. this->AppendIncludeDirectories(fout, dirs, emmited);
  558. }
  559. fout << "</storageModule>\n";
  560. // add build targets
  561. fout <<
  562. "<storageModule moduleId=\"org.eclipse.cdt.make.core.buildtargets\">\n"
  563. "<buildTargets>\n"
  564. ;
  565. emmited.clear();
  566. const std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  567. cmGlobalGenerator* generator
  568. = const_cast<cmGlobalGenerator*>(this->GlobalGenerator);
  569. if (generator->GetAllTargetName())
  570. {
  571. emmited.insert(generator->GetAllTargetName());
  572. this->AppendTarget(fout, generator->GetAllTargetName(), make);
  573. }
  574. if (generator->GetCleanTargetName())
  575. {
  576. emmited.insert(generator->GetCleanTargetName());
  577. this->AppendTarget(fout, generator->GetCleanTargetName(), make);
  578. }
  579. // add all executable and library targets and some of the GLOBAL
  580. // and UTILITY targets
  581. for (std::vector<cmLocalGenerator*>::const_iterator
  582. it = this->GlobalGenerator->GetLocalGenerators().begin();
  583. it != this->GlobalGenerator->GetLocalGenerators().end();
  584. ++it)
  585. {
  586. const cmTargets& targets = (*it)->GetMakefile()->GetTargets();
  587. cmMakefile* makefile=(*it)->GetMakefile();
  588. for(cmTargets::const_iterator ti=targets.begin(); ti!=targets.end(); ++ti)
  589. {
  590. switch(ti->second.GetType())
  591. {
  592. case cmTarget::GLOBAL_TARGET:
  593. {
  594. bool insertTarget = false;
  595. // Only add the global targets from CMAKE_BINARY_DIR,
  596. // not from the subdirs
  597. if (strcmp(makefile->GetStartOutputDirectory(),
  598. makefile->GetHomeOutputDirectory())==0)
  599. {
  600. insertTarget = true;
  601. // only add the "edit_cache" target if it's not ccmake, because
  602. // this will not work within the IDE
  603. if (ti->first == "edit_cache")
  604. {
  605. if (strstr(makefile->GetRequiredDefinition
  606. ("CMAKE_EDIT_COMMAND"), "ccmake")!=NULL)
  607. {
  608. insertTarget = false;
  609. }
  610. }
  611. }
  612. if (insertTarget)
  613. {
  614. this->AppendTarget(fout, ti->first, make);
  615. }
  616. }
  617. break;
  618. case cmTarget::UTILITY:
  619. // Add all utility targets, except the Nightly/Continuous/
  620. // Experimental-"sub"targets as e.g. NightlyStart
  621. if (((ti->first.find("Nightly")==0) &&(ti->first!="Nightly"))
  622. || ((ti->first.find("Continuous")==0)&&(ti->first!="Continuous"))
  623. || ((ti->first.find("Experimental")==0)
  624. && (ti->first!="Experimental")))
  625. {
  626. break;
  627. }
  628. this->AppendTarget(fout, ti->first, make);
  629. break;
  630. case cmTarget::EXECUTABLE:
  631. case cmTarget::STATIC_LIBRARY:
  632. case cmTarget::SHARED_LIBRARY:
  633. case cmTarget::MODULE_LIBRARY:
  634. {
  635. this->AppendTarget(fout, ti->first, make);
  636. std::string fastTarget = ti->first;
  637. fastTarget += "/fast";
  638. this->AppendTarget(fout, fastTarget, make);
  639. }
  640. break;
  641. // ignore these:
  642. case cmTarget::INSTALL_FILES:
  643. case cmTarget::INSTALL_PROGRAMS:
  644. case cmTarget::INSTALL_DIRECTORY:
  645. default:
  646. break;
  647. }
  648. }
  649. }
  650. fout << "</buildTargets>\n"
  651. "</storageModule>\n"
  652. ;
  653. this->AppendStorageScanners(fout, *mf);
  654. fout << "</cconfiguration>\n"
  655. "</storageModule>\n"
  656. "<storageModule moduleId=\"cdtBuildSystem\" version=\"4.0.0\">\n"
  657. "<project id=\"" << mf->GetProjectName() << ".null.1\""
  658. " name=\"" << mf->GetProjectName() << "\"/>\n"
  659. "</storageModule>\n"
  660. "</cproject>\n"
  661. ;
  662. }
  663. //----------------------------------------------------------------------------
  664. cmExtraEclipseCDT4Generator::EclipseToolchainType
  665. cmExtraEclipseCDT4Generator::GetToolChainType(const cmMakefile& makefile)
  666. {
  667. if (makefile.IsSet("UNIX"))
  668. {
  669. if (makefile.IsSet("CYGWIN"))
  670. {
  671. return EclipseToolchainCygwin;
  672. }
  673. if (makefile.IsSet("APPLE" ))
  674. {
  675. return EclipseToolchainMacOSX;
  676. }
  677. // *** how do I determine if it is Solaris ???
  678. return EclipseToolchainLinux;
  679. }
  680. else if (makefile.IsSet("WIN32"))
  681. {
  682. if (makefile.IsSet("MINGW"))
  683. {
  684. return EclipseToolchainMinGW;
  685. }
  686. if (makefile.IsSet("MSYS" ))
  687. {
  688. return EclipseToolchainMinGW;
  689. }
  690. return EclipseToolchainOther;
  691. }
  692. else
  693. {
  694. return EclipseToolchainOther;
  695. }
  696. }
  697. std::string
  698. cmExtraEclipseCDT4Generator::GetEclipsePath(const std::string& path)
  699. {
  700. #if defined(__CYGWIN__)
  701. std::string cmd = "cygpath -m " + path;
  702. std::string out;
  703. if (!cmSystemTools::RunCommand(cmd.c_str(), out, 0, false))
  704. {
  705. return path;
  706. }
  707. else
  708. {
  709. out.erase(out.find_last_of('\n'));
  710. return out;
  711. }
  712. #else
  713. return path;
  714. #endif
  715. }
  716. std::string
  717. cmExtraEclipseCDT4Generator::GetPathBasename(const std::string& path)
  718. {
  719. std::string outputBasename = path;
  720. while (outputBasename.size() > 0 &&
  721. (outputBasename[outputBasename.size() - 1] == '/' ||
  722. outputBasename[outputBasename.size() - 1] == '\\'))
  723. {
  724. outputBasename.resize(outputBasename.size() - 1);
  725. }
  726. std::string::size_type loc = outputBasename.find_last_of("/\\");
  727. if (loc != std::string::npos)
  728. {
  729. outputBasename = outputBasename.substr(loc + 1);
  730. }
  731. return outputBasename;
  732. }
  733. std::string
  734. cmExtraEclipseCDT4Generator::GenerateProjectName(const std::string& name,
  735. const std::string& type,
  736. const std::string& path)
  737. {
  738. return name + (type.empty() ? "" : "-") + type + "@" + path;
  739. }
  740. std::string cmExtraEclipseCDT4Generator::EscapeForXML(const std::string& value)
  741. {
  742. std::string str = value;
  743. cmSystemTools::ReplaceString(str, "&", "&amp;");
  744. cmSystemTools::ReplaceString(str, "<", "&lt;");
  745. cmSystemTools::ReplaceString(str, ">", "&gt;");
  746. cmSystemTools::ReplaceString(str, "\"", "&quot;");
  747. // NOTE: This one is not necessary, since as of Eclipse CDT4 it will
  748. // automatically change this to the original value (').
  749. //cmSystemTools::ReplaceString(str, "'", "&apos;");
  750. return str;
  751. }
  752. //----------------------------------------------------------------------------
  753. // Helper functions
  754. //----------------------------------------------------------------------------
  755. void cmExtraEclipseCDT4Generator
  756. ::AppendStorageScanners(cmGeneratedFileStream& fout,
  757. const cmMakefile& makefile)
  758. {
  759. // we need the "make" and the C (or C++) compiler which are used, Alex
  760. std::string make = makefile.GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  761. std::string compiler = makefile.GetSafeDefinition("CMAKE_C_COMPILER");
  762. if (compiler.empty())
  763. {
  764. compiler = makefile.GetSafeDefinition("CMAKE_CXX_COMPILER");
  765. }
  766. if (compiler.empty()) //Hmm, what to do now ?
  767. {
  768. compiler = "gcc";
  769. }
  770. // the following right now hardcodes gcc behaviour :-/
  771. fout <<
  772. "<storageModule moduleId=\"scannerConfiguration\">\n"
  773. "<autodiscovery enabled=\"true\" problemReportingEnabled=\"true\""
  774. " selectedProfileId="
  775. "\"org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile\"/>\n"
  776. ;
  777. cmExtraEclipseCDT4Generator::AppendScannerProfile(fout,
  778. "org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile",
  779. true, "", true, "specsFile",
  780. "-E -P -v -dD ${plugin_state_location}/${specs_file}",
  781. compiler, true, true);
  782. cmExtraEclipseCDT4Generator::AppendScannerProfile(fout,
  783. "org.eclipse.cdt.make.core.GCCStandardMakePerFileProfile",
  784. true, "", true, "makefileGenerator",
  785. "-f ${project_name}_scd.mk",
  786. make, true, true);
  787. fout << "</storageModule>\n";
  788. }
  789. void cmExtraEclipseCDT4Generator::AppendTarget(cmGeneratedFileStream& fout,
  790. const std::string& target,
  791. const std::string& make)
  792. {
  793. fout <<
  794. "<target name=\"" << target << "\""
  795. " path=\"\""
  796. " targetID=\"org.eclipse.cdt.make.MakeTargetBuilder\">\n"
  797. "<buildCommand>"
  798. << cmExtraEclipseCDT4Generator::GetEclipsePath(make)
  799. << "</buildCommand>\n"
  800. "<buildArguments/>\n"
  801. "<buildTarget>" << target << "</buildTarget>\n"
  802. "<stopOnError>true</stopOnError>\n"
  803. "<useDefaultCommand>false</useDefaultCommand>\n"
  804. "</target>\n"
  805. ;
  806. }
  807. void cmExtraEclipseCDT4Generator
  808. ::AppendScannerProfile(cmGeneratedFileStream& fout,
  809. const std::string& profileID,
  810. bool openActionEnabled,
  811. const std::string& openActionFilePath,
  812. bool pParserEnabled,
  813. const std::string& scannerInfoProviderID,
  814. const std::string& runActionArguments,
  815. const std::string& runActionCommand,
  816. bool runActionUseDefault,
  817. bool sipParserEnabled)
  818. {
  819. fout <<
  820. "<profile id=\"" << profileID << "\">\n"
  821. "<buildOutputProvider>\n"
  822. "<openAction enabled=\"" << (openActionEnabled ? "true" : "false")
  823. << "\" filePath=\"" << openActionFilePath << "\"/>\n"
  824. "<parser enabled=\"" << (pParserEnabled ? "true" : "false") << "\"/>\n"
  825. "</buildOutputProvider>\n"
  826. "<scannerInfoProvider id=\"" << scannerInfoProviderID << "\">\n"
  827. "<runAction arguments=\"" << runActionArguments << "\""
  828. " command=\"" << runActionCommand
  829. << "\" useDefault=\"" << (runActionUseDefault ? "true":"false") << "\"/>\n"
  830. "<parser enabled=\"" << (sipParserEnabled ? "true" : "false") << "\"/>\n"
  831. "</scannerInfoProvider>\n"
  832. "</profile>\n"
  833. ;
  834. }
  835. void cmExtraEclipseCDT4Generator
  836. ::AppendLinkedResource (cmGeneratedFileStream& fout,
  837. const std::string& name,
  838. const std::string& path)
  839. {
  840. fout <<
  841. "\t\t<link>\n"
  842. "\t\t\t<name>" << name << "</name>\n"
  843. "\t\t\t<type>2</type>\n"
  844. "\t\t\t<location>"
  845. << path
  846. << "</location>\n"
  847. "\t\t</link>\n"
  848. ;
  849. }
  850. bool cmExtraEclipseCDT4Generator
  851. ::AppendOutLinkedResource(cmGeneratedFileStream& fout,
  852. const std::string& defname,
  853. const std::string& altdefname)
  854. {
  855. if (defname.empty() && altdefname.empty())
  856. {
  857. return false;
  858. }
  859. std::string outputPath = (defname.empty() ? altdefname : defname);
  860. if (!cmSystemTools::FileIsFullPath(outputPath.c_str()))
  861. {
  862. outputPath = this->HomeOutputDirectory + "/" + outputPath;
  863. }
  864. if (cmSystemTools::IsSubDirectory(outputPath.c_str(),
  865. this->HomeOutputDirectory.c_str()))
  866. {
  867. return false;
  868. }
  869. std::string name = this->GetPathBasename(outputPath);
  870. // make sure linked resource name is unique
  871. while (this->GlobalGenerator->GetProjectMap().find(name)
  872. != this->GlobalGenerator->GetProjectMap().end())
  873. {
  874. name += "_";
  875. }
  876. if (std::find(this->OutLinkedResources.begin(),
  877. this->OutLinkedResources.end(),
  878. name)
  879. != this->OutLinkedResources.end())
  880. {
  881. return false;
  882. }
  883. else
  884. {
  885. this->AppendLinkedResource(fout, name,
  886. this->GetEclipsePath(outputPath));
  887. this->OutLinkedResources.push_back(name);
  888. return true;
  889. }
  890. }