cmExtraEclipseCDT4Generator.cxx 38 KB

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