cmExtraEclipseCDT4Generator.cxx 38 KB

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