cmExtraEclipseCDT4Generator.cxx 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. // add system defined c macros
  527. const char* cDefs=mf->GetDefinition("CMAKE_ECLIPSE_C_SYSTEM_DEFINED_MACROS");
  528. if(cDefs)
  529. {
  530. // Expand the list.
  531. std::vector<std::string> defs;
  532. cmSystemTools::ExpandListArgument(cDefs, defs, true);
  533. // the list must contain only definition-value pairs:
  534. if ((defs.size() % 2) == 0)
  535. {
  536. std::vector<std::string>::const_iterator di = defs.begin();
  537. while (di != defs.end())
  538. {
  539. std::string def = *di;
  540. ++di;
  541. std::string val;
  542. if (di != defs.end())
  543. {
  544. val = *di;
  545. ++di;
  546. }
  547. // insert the definition if not already added.
  548. if(emmited.find(def) == emmited.end())
  549. {
  550. emmited.insert(def);
  551. fout << "<pathentry kind=\"mac\" name=\"" << def
  552. << "\" path=\"\" value=\"" << this->EscapeForXML(val)
  553. << "\"/>\n";
  554. }
  555. }
  556. }
  557. }
  558. // add system defined c++ macros
  559. const char* cxxDefs = mf->GetDefinition(
  560. "CMAKE_ECLIPSE_CXX_SYSTEM_DEFINED_MACROS");
  561. if(cxxDefs)
  562. {
  563. // Expand the list.
  564. std::vector<std::string> defs;
  565. cmSystemTools::ExpandListArgument(cxxDefs, defs, true);
  566. // the list must contain only definition-value pairs:
  567. if ((defs.size() % 2) == 0)
  568. {
  569. std::vector<std::string>::const_iterator di = defs.begin();
  570. while (di != defs.end())
  571. {
  572. std::string def = *di;
  573. ++di;
  574. std::string val;
  575. if (di != defs.end())
  576. {
  577. val = *di;
  578. ++di;
  579. }
  580. // insert the definition if not already added.
  581. if(emmited.find(def) == emmited.end())
  582. {
  583. emmited.insert(def);
  584. fout << "<pathentry kind=\"mac\" name=\"" << def
  585. << "\" path=\"\" value=\"" << this->EscapeForXML(val)
  586. << "\"/>\n";
  587. }
  588. }
  589. }
  590. }
  591. // include dirs
  592. emmited.clear();
  593. for (std::vector<cmLocalGenerator*>::const_iterator
  594. it = this->GlobalGenerator->GetLocalGenerators().begin();
  595. it != this->GlobalGenerator->GetLocalGenerators().end();
  596. ++it)
  597. {
  598. const std::vector<std::string>& includeDirs
  599. = (*it)->GetMakefile()->GetIncludeDirectories();
  600. this->AppendIncludeDirectories(fout, includeDirs, emmited);
  601. }
  602. // now also the system include directories, in case we found them in
  603. // CMakeSystemSpecificInformation.cmake. This makes Eclipse find the
  604. // standard headers.
  605. mf->GetDefinition("CMAKE_ECLIPSE_C_SYSTEM_INCLUDE_DIRS");
  606. std::string compiler = mf->GetSafeDefinition("CMAKE_C_COMPILER");
  607. if (!compiler.empty())
  608. {
  609. std::string systemIncludeDirs = mf->GetSafeDefinition(
  610. "CMAKE_ECLIPSE_C_SYSTEM_INCLUDE_DIRS");
  611. std::vector<std::string> dirs;
  612. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  613. this->AppendIncludeDirectories(fout, dirs, emmited);
  614. }
  615. compiler = mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  616. if (!compiler.empty())
  617. {
  618. std::string systemIncludeDirs = mf->GetSafeDefinition(
  619. "CMAKE_ECLIPSE_CXX_SYSTEM_INCLUDE_DIRS");
  620. std::vector<std::string> dirs;
  621. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  622. this->AppendIncludeDirectories(fout, dirs, emmited);
  623. }
  624. fout << "</storageModule>\n";
  625. // add build targets
  626. fout <<
  627. "<storageModule moduleId=\"org.eclipse.cdt.make.core.buildtargets\">\n"
  628. "<buildTargets>\n"
  629. ;
  630. emmited.clear();
  631. const std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  632. cmGlobalGenerator* generator
  633. = const_cast<cmGlobalGenerator*>(this->GlobalGenerator);
  634. std::string allTarget;
  635. std::string cleanTarget;
  636. if (generator->GetAllTargetName())
  637. {
  638. allTarget = generator->GetAllTargetName();
  639. }
  640. if (generator->GetCleanTargetName())
  641. {
  642. cleanTarget = generator->GetCleanTargetName();
  643. }
  644. // add all executable and library targets and some of the GLOBAL
  645. // and UTILITY targets
  646. for (std::vector<cmLocalGenerator*>::const_iterator
  647. it = this->GlobalGenerator->GetLocalGenerators().begin();
  648. it != this->GlobalGenerator->GetLocalGenerators().end();
  649. ++it)
  650. {
  651. const cmTargets& targets = (*it)->GetMakefile()->GetTargets();
  652. cmMakefile* makefile=(*it)->GetMakefile();
  653. std::string subdir = (*it)->Convert(makefile->GetCurrentOutputDirectory(),
  654. cmLocalGenerator::HOME_OUTPUT);
  655. if (subdir == ".")
  656. {
  657. subdir = "";
  658. }
  659. for(cmTargets::const_iterator ti=targets.begin(); ti!=targets.end(); ++ti)
  660. {
  661. switch(ti->second.GetType())
  662. {
  663. case cmTarget::GLOBAL_TARGET:
  664. {
  665. bool insertTarget = false;
  666. // Only add the global targets from CMAKE_BINARY_DIR,
  667. // not from the subdirs
  668. if (subdir.empty())
  669. {
  670. insertTarget = true;
  671. // only add the "edit_cache" target if it's not ccmake, because
  672. // this will not work within the IDE
  673. if (ti->first == "edit_cache")
  674. {
  675. if (strstr(makefile->GetRequiredDefinition
  676. ("CMAKE_EDIT_COMMAND"), "ccmake")!=NULL)
  677. {
  678. insertTarget = false;
  679. }
  680. }
  681. }
  682. if (insertTarget)
  683. {
  684. this->AppendTarget(fout, ti->first, make, subdir, ": ");
  685. }
  686. }
  687. break;
  688. case cmTarget::UTILITY:
  689. // Add all utility targets, except the Nightly/Continuous/
  690. // Experimental-"sub"targets as e.g. NightlyStart
  691. if (((ti->first.find("Nightly")==0) &&(ti->first!="Nightly"))
  692. || ((ti->first.find("Continuous")==0)&&(ti->first!="Continuous"))
  693. || ((ti->first.find("Experimental")==0)
  694. && (ti->first!="Experimental")))
  695. {
  696. break;
  697. }
  698. this->AppendTarget(fout, ti->first, make, subdir, ": ");
  699. break;
  700. case cmTarget::EXECUTABLE:
  701. case cmTarget::STATIC_LIBRARY:
  702. case cmTarget::SHARED_LIBRARY:
  703. case cmTarget::MODULE_LIBRARY:
  704. {
  705. const char* prefix = (ti->second.GetType()==cmTarget::EXECUTABLE ?
  706. "[exe] " : "[lib] ");
  707. this->AppendTarget(fout, ti->first, make, subdir, prefix);
  708. std::string fastTarget = ti->first;
  709. fastTarget += "/fast";
  710. this->AppendTarget(fout, fastTarget, make, subdir, prefix);
  711. }
  712. break;
  713. // ignore these:
  714. case cmTarget::INSTALL_FILES:
  715. case cmTarget::INSTALL_PROGRAMS:
  716. case cmTarget::INSTALL_DIRECTORY:
  717. default:
  718. break;
  719. }
  720. }
  721. // insert the all and clean targets in every subdir
  722. if (!allTarget.empty())
  723. {
  724. this->AppendTarget(fout, allTarget, make, subdir, ": ");
  725. }
  726. if (!cleanTarget.empty())
  727. {
  728. this->AppendTarget(fout, cleanTarget, make, subdir, ": ");
  729. }
  730. //insert rules for compiling, preprocessing and assembling individual files
  731. cmLocalUnixMakefileGenerator3* lumg=(cmLocalUnixMakefileGenerator3*)*it;
  732. std::vector<std::string> objectFileTargets;
  733. lumg->GetIndividualFileTargets(objectFileTargets);
  734. for(std::vector<std::string>::const_iterator fit=objectFileTargets.begin();
  735. fit != objectFileTargets.end();
  736. ++fit)
  737. {
  738. const char* prefix = "[obj] ";
  739. if ((*fit)[fit->length()-1] == 's')
  740. {
  741. prefix = "[to asm] ";
  742. }
  743. else if ((*fit)[fit->length()-1] == 'i')
  744. {
  745. prefix = "[pre] ";
  746. }
  747. this->AppendTarget(fout, *fit, make, subdir, prefix);
  748. }
  749. }
  750. fout << "</buildTargets>\n"
  751. "</storageModule>\n"
  752. ;
  753. this->AppendStorageScanners(fout, *mf);
  754. fout << "</cconfiguration>\n"
  755. "</storageModule>\n"
  756. "<storageModule moduleId=\"cdtBuildSystem\" version=\"4.0.0\">\n"
  757. "<project id=\"" << mf->GetProjectName() << ".null.1\""
  758. " name=\"" << mf->GetProjectName() << "\"/>\n"
  759. "</storageModule>\n"
  760. "</cproject>\n"
  761. ;
  762. }
  763. //----------------------------------------------------------------------------
  764. cmExtraEclipseCDT4Generator::EclipseToolchainType
  765. cmExtraEclipseCDT4Generator::GetToolChainType(const cmMakefile& makefile)
  766. {
  767. if (makefile.IsSet("UNIX"))
  768. {
  769. if (makefile.IsSet("CYGWIN"))
  770. {
  771. return EclipseToolchainCygwin;
  772. }
  773. if (makefile.IsSet("APPLE" ))
  774. {
  775. return EclipseToolchainMacOSX;
  776. }
  777. // *** how do I determine if it is Solaris ???
  778. return EclipseToolchainLinux;
  779. }
  780. else if (makefile.IsSet("WIN32"))
  781. {
  782. if (makefile.IsSet("MINGW"))
  783. {
  784. return EclipseToolchainMinGW;
  785. }
  786. if (makefile.IsSet("MSYS" ))
  787. {
  788. return EclipseToolchainMinGW;
  789. }
  790. return EclipseToolchainOther;
  791. }
  792. else
  793. {
  794. return EclipseToolchainOther;
  795. }
  796. }
  797. std::string
  798. cmExtraEclipseCDT4Generator::GetEclipsePath(const std::string& path)
  799. {
  800. #if defined(__CYGWIN__)
  801. std::string cmd = "cygpath -m " + path;
  802. std::string out;
  803. if (!cmSystemTools::RunCommand(cmd.c_str(), out, 0, false))
  804. {
  805. return path;
  806. }
  807. else
  808. {
  809. out.erase(out.find_last_of('\n'));
  810. return out;
  811. }
  812. #else
  813. return path;
  814. #endif
  815. }
  816. std::string
  817. cmExtraEclipseCDT4Generator::GetPathBasename(const std::string& path)
  818. {
  819. std::string outputBasename = path;
  820. while (outputBasename.size() > 0 &&
  821. (outputBasename[outputBasename.size() - 1] == '/' ||
  822. outputBasename[outputBasename.size() - 1] == '\\'))
  823. {
  824. outputBasename.resize(outputBasename.size() - 1);
  825. }
  826. std::string::size_type loc = outputBasename.find_last_of("/\\");
  827. if (loc != std::string::npos)
  828. {
  829. outputBasename = outputBasename.substr(loc + 1);
  830. }
  831. return outputBasename;
  832. }
  833. std::string
  834. cmExtraEclipseCDT4Generator::GenerateProjectName(const std::string& name,
  835. const std::string& type,
  836. const std::string& path)
  837. {
  838. return name + (type.empty() ? "" : "-") + type + "@" + path;
  839. }
  840. std::string cmExtraEclipseCDT4Generator::EscapeForXML(const std::string& value)
  841. {
  842. std::string str = value;
  843. cmSystemTools::ReplaceString(str, "&", "&amp;");
  844. cmSystemTools::ReplaceString(str, "<", "&lt;");
  845. cmSystemTools::ReplaceString(str, ">", "&gt;");
  846. cmSystemTools::ReplaceString(str, "\"", "&quot;");
  847. // NOTE: This one is not necessary, since as of Eclipse CDT4 it will
  848. // automatically change this to the original value (').
  849. //cmSystemTools::ReplaceString(str, "'", "&apos;");
  850. return str;
  851. }
  852. //----------------------------------------------------------------------------
  853. // Helper functions
  854. //----------------------------------------------------------------------------
  855. void cmExtraEclipseCDT4Generator
  856. ::AppendStorageScanners(cmGeneratedFileStream& fout,
  857. const cmMakefile& makefile)
  858. {
  859. // we need the "make" and the C (or C++) compiler which are used, Alex
  860. std::string make = makefile.GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  861. std::string compiler = makefile.GetSafeDefinition("CMAKE_C_COMPILER");
  862. if (compiler.empty())
  863. {
  864. compiler = makefile.GetSafeDefinition("CMAKE_CXX_COMPILER");
  865. }
  866. if (compiler.empty()) //Hmm, what to do now ?
  867. {
  868. compiler = "gcc";
  869. }
  870. // the following right now hardcodes gcc behaviour :-/
  871. fout <<
  872. "<storageModule moduleId=\"scannerConfiguration\">\n"
  873. "<autodiscovery enabled=\"true\" problemReportingEnabled=\"true\""
  874. " selectedProfileId="
  875. "\"org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile\"/>\n"
  876. ;
  877. cmExtraEclipseCDT4Generator::AppendScannerProfile(fout,
  878. "org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile",
  879. true, "", true, "specsFile",
  880. "-E -P -v -dD ${plugin_state_location}/${specs_file}",
  881. compiler, true, true);
  882. cmExtraEclipseCDT4Generator::AppendScannerProfile(fout,
  883. "org.eclipse.cdt.make.core.GCCStandardMakePerFileProfile",
  884. true, "", true, "makefileGenerator",
  885. "-f ${project_name}_scd.mk",
  886. make, true, true);
  887. fout << "</storageModule>\n";
  888. }
  889. // The prefix is prepended before the actual name of the target. The purpose
  890. // of that is to sort the targets in the view of Eclipse, so that at first
  891. // the global/utility/all/clean targets appear ": ", then the executable
  892. // targets "[exe] ", then the libraries "[lib]", then the rules for the
  893. // object files "[obj]", then for preprocessing only "[pre] " and
  894. // finally the assembly files "[to asm] ". Note the "to" in "to asm",
  895. // without it, "asm" would be the first targets in the list, with the "to"
  896. // they are the last targets, which makes more sense.
  897. void cmExtraEclipseCDT4Generator::AppendTarget(cmGeneratedFileStream& fout,
  898. const std::string& target,
  899. const std::string& make,
  900. const std::string& path,
  901. const char* prefix)
  902. {
  903. fout <<
  904. "<target name=\"" << prefix << target << "\""
  905. " path=\"" << path.c_str() << "\""
  906. " targetID=\"org.eclipse.cdt.make.MakeTargetBuilder\">\n"
  907. "<buildCommand>"
  908. << cmExtraEclipseCDT4Generator::GetEclipsePath(make)
  909. << "</buildCommand>\n"
  910. "<buildArguments/>\n"
  911. "<buildTarget>" << target << "</buildTarget>\n"
  912. "<stopOnError>true</stopOnError>\n"
  913. "<useDefaultCommand>false</useDefaultCommand>\n"
  914. "</target>\n"
  915. ;
  916. }
  917. void cmExtraEclipseCDT4Generator
  918. ::AppendScannerProfile(cmGeneratedFileStream& fout,
  919. const std::string& profileID,
  920. bool openActionEnabled,
  921. const std::string& openActionFilePath,
  922. bool pParserEnabled,
  923. const std::string& scannerInfoProviderID,
  924. const std::string& runActionArguments,
  925. const std::string& runActionCommand,
  926. bool runActionUseDefault,
  927. bool sipParserEnabled)
  928. {
  929. fout <<
  930. "<profile id=\"" << profileID << "\">\n"
  931. "<buildOutputProvider>\n"
  932. "<openAction enabled=\"" << (openActionEnabled ? "true" : "false")
  933. << "\" filePath=\"" << openActionFilePath << "\"/>\n"
  934. "<parser enabled=\"" << (pParserEnabled ? "true" : "false") << "\"/>\n"
  935. "</buildOutputProvider>\n"
  936. "<scannerInfoProvider id=\"" << scannerInfoProviderID << "\">\n"
  937. "<runAction arguments=\"" << runActionArguments << "\""
  938. " command=\"" << runActionCommand
  939. << "\" useDefault=\"" << (runActionUseDefault ? "true":"false") << "\"/>\n"
  940. "<parser enabled=\"" << (sipParserEnabled ? "true" : "false") << "\"/>\n"
  941. "</scannerInfoProvider>\n"
  942. "</profile>\n"
  943. ;
  944. }
  945. void cmExtraEclipseCDT4Generator
  946. ::AppendLinkedResource (cmGeneratedFileStream& fout,
  947. const std::string& name,
  948. const std::string& path)
  949. {
  950. fout <<
  951. "\t\t<link>\n"
  952. "\t\t\t<name>" << name << "</name>\n"
  953. "\t\t\t<type>2</type>\n"
  954. "\t\t\t<location>"
  955. << path
  956. << "</location>\n"
  957. "\t\t</link>\n"
  958. ;
  959. }
  960. bool cmExtraEclipseCDT4Generator
  961. ::AppendOutLinkedResource(cmGeneratedFileStream& fout,
  962. const std::string& defname,
  963. const std::string& altdefname)
  964. {
  965. if (defname.empty() && altdefname.empty())
  966. {
  967. return false;
  968. }
  969. std::string outputPath = (defname.empty() ? altdefname : defname);
  970. if (!cmSystemTools::FileIsFullPath(outputPath.c_str()))
  971. {
  972. outputPath = this->HomeOutputDirectory + "/" + outputPath;
  973. }
  974. if (cmSystemTools::IsSubDirectory(outputPath.c_str(),
  975. this->HomeOutputDirectory.c_str()))
  976. {
  977. return false;
  978. }
  979. std::string name = this->GetPathBasename(outputPath);
  980. // make sure linked resource name is unique
  981. while (this->GlobalGenerator->GetProjectMap().find(name)
  982. != this->GlobalGenerator->GetProjectMap().end())
  983. {
  984. name += "_";
  985. }
  986. if (std::find(this->OutLinkedResources.begin(),
  987. this->OutLinkedResources.end(),
  988. name)
  989. != this->OutLinkedResources.end())
  990. {
  991. return false;
  992. }
  993. else
  994. {
  995. this->AppendLinkedResource(fout, name,
  996. this->GetEclipsePath(outputPath));
  997. this->OutLinkedResources.push_back(name);
  998. return true;
  999. }
  1000. }