cmExtraEclipseCDT4Generator.cxx 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  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 "cmSourceFile.h"
  19. #include "cmSystemTools.h"
  20. #include <stdlib.h>
  21. #include <assert.h>
  22. //----------------------------------------------------------------------------
  23. cmExtraEclipseCDT4Generator
  24. ::cmExtraEclipseCDT4Generator() : cmExternalMakefileProjectGenerator()
  25. {
  26. // TODO: Verify if __CYGWIN__ should be checked.
  27. //#if defined(_WIN32) && !defined(__CYGWIN__)
  28. #if defined(_WIN32)
  29. this->SupportedGlobalGenerators.push_back("NMake Makefiles");
  30. this->SupportedGlobalGenerators.push_back("MinGW Makefiles");
  31. // this->SupportedGlobalGenerators.push_back("MSYS Makefiles");
  32. #endif
  33. this->SupportedGlobalGenerators.push_back("Ninja");
  34. this->SupportedGlobalGenerators.push_back("Unix Makefiles");
  35. this->SupportsVirtualFolders = true;
  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. "In out of source builds, a linked resource to the top level source "
  46. "directory will be created."
  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::Generate()
  53. {
  54. const cmMakefile* mf
  55. = this->GlobalGenerator->GetLocalGenerators()[0]->GetMakefile();
  56. std::string eclipseVersion = mf->GetSafeDefinition("CMAKE_ECLIPSE_VERSION");
  57. cmsys::RegularExpression regex(".*([0-9]+\\.[0-9]+).*");
  58. if (regex.find(eclipseVersion.c_str()))
  59. {
  60. unsigned int majorVersion = 0;
  61. unsigned int minorVersion = 0;
  62. int res=sscanf(regex.match(1).c_str(), "%u.%u", &majorVersion,
  63. &minorVersion);
  64. if (res == 2)
  65. {
  66. int version = majorVersion * 1000 + minorVersion;
  67. if (version < 3006) // 3.6 is Helios
  68. {
  69. this->SupportsVirtualFolders = false;
  70. }
  71. }
  72. }
  73. // TODO: Decide if these are local or member variables
  74. this->HomeDirectory = mf->GetHomeDirectory();
  75. this->HomeOutputDirectory = mf->GetHomeOutputDirectory();
  76. this->IsOutOfSourceBuild = (this->HomeDirectory!=this->HomeOutputDirectory);
  77. this->GenerateSourceProject = (this->IsOutOfSourceBuild &&
  78. mf->IsOn("CMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT"));
  79. if ((this->GenerateSourceProject == false)
  80. && (mf->IsOn("ECLIPSE_CDT4_GENERATE_SOURCE_PROJECT")))
  81. {
  82. mf->IssueMessage(cmake::WARNING,
  83. "ECLIPSE_CDT4_GENERATE_SOURCE_PROJECT is set to TRUE, "
  84. "but this variable is not supported anymore since CMake 2.8.7.\n"
  85. "Enable CMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT instead.");
  86. }
  87. if (cmSystemTools::IsSubDirectory(this->HomeOutputDirectory.c_str(),
  88. this->HomeDirectory.c_str()))
  89. {
  90. mf->IssueMessage(cmake::WARNING, "The build directory is a subdirectory "
  91. "of the source directory.\n"
  92. "This is not supported well by Eclipse. It is strongly "
  93. "recommended to use a build directory which is a "
  94. "sibling of the source directory.");
  95. }
  96. // NOTE: This is not good, since it pollutes the source tree. However,
  97. // Eclipse doesn't allow CVS/SVN to work when the .project is not in
  98. // the cvs/svn root directory. Hence, this is provided as an option.
  99. if (this->GenerateSourceProject)
  100. {
  101. // create .project file in the source tree
  102. this->CreateSourceProjectFile();
  103. }
  104. // create a .project file
  105. this->CreateProjectFile();
  106. // create a .cproject file
  107. this->CreateCProjectFile();
  108. }
  109. void cmExtraEclipseCDT4Generator::CreateSourceProjectFile()
  110. {
  111. assert(this->HomeDirectory != this->HomeOutputDirectory);
  112. // set up the project name: <project>-Source@<baseSourcePathName>
  113. const cmMakefile* mf
  114. = this->GlobalGenerator->GetLocalGenerators()[0]->GetMakefile();
  115. std::string name = this->GenerateProjectName(mf->GetProjectName(), "Source",
  116. this->GetPathBasename(this->HomeDirectory));
  117. const std::string filename = this->HomeDirectory + "/.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>" << this->EscapeForXML(name) << "</name>\n"
  127. "\t<comment></comment>\n"
  128. "\t<projects>\n"
  129. "\t</projects>\n"
  130. "\t<buildSpec>\n"
  131. "\t</buildSpec>\n"
  132. "\t<natures>\n"
  133. "\t</natures>\n"
  134. "\t<linkedResources>\n";
  135. if (this->SupportsVirtualFolders)
  136. {
  137. this->CreateLinksToSubprojects(fout, this->HomeDirectory);
  138. this->SrcLinkedResources.clear();
  139. }
  140. fout <<
  141. "\t</linkedResources>\n"
  142. "</projectDescription>\n"
  143. ;
  144. }
  145. //----------------------------------------------------------------------------
  146. void cmExtraEclipseCDT4Generator::AddEnvVar(cmGeneratedFileStream& fout,
  147. const char* envVar, cmMakefile* mf)
  148. {
  149. // get the variables from the environment and from the cache and then
  150. // figure out which one to use:
  151. const char* envVarValue = getenv(envVar);
  152. std::string cacheEntryName = "CMAKE_ECLIPSE_ENVVAR_";
  153. cacheEntryName += envVar;
  154. const char* cacheValue = mf->GetCacheManager()->GetCacheValue(
  155. cacheEntryName.c_str());
  156. // now we have both, decide which one to use
  157. std::string valueToUse;
  158. if (envVarValue==0 && cacheValue==0)
  159. {
  160. // nothing known, do nothing
  161. valueToUse = "";
  162. }
  163. else if (envVarValue!=0 && cacheValue==0)
  164. {
  165. // The variable is in the env, but not in the cache. Use it and put it
  166. // in the cache
  167. valueToUse = envVarValue;
  168. mf->AddCacheDefinition(cacheEntryName.c_str(), valueToUse.c_str(),
  169. cacheEntryName.c_str(), cmCacheManager::STRING,
  170. true);
  171. mf->GetCacheManager()->SaveCache(mf->GetHomeOutputDirectory());
  172. }
  173. else if (envVarValue==0 && cacheValue!=0)
  174. {
  175. // It is already in the cache, but not in the env, so use it from the cache
  176. valueToUse = cacheValue;
  177. }
  178. else
  179. {
  180. // It is both in the cache and in the env.
  181. // Use the version from the env. except if the value from the env is
  182. // completely contained in the value from the cache (for the case that we
  183. // now have a PATH without MSVC dirs in the env. but had the full PATH with
  184. // all MSVC dirs during the cmake run which stored the var in the cache:
  185. valueToUse = cacheValue;
  186. if (valueToUse.find(envVarValue) == std::string::npos)
  187. {
  188. valueToUse = envVarValue;
  189. mf->AddCacheDefinition(cacheEntryName.c_str(), valueToUse.c_str(),
  190. cacheEntryName.c_str(), cmCacheManager::STRING,
  191. true);
  192. mf->GetCacheManager()->SaveCache(mf->GetHomeOutputDirectory());
  193. }
  194. }
  195. if (!valueToUse.empty())
  196. {
  197. fout << envVar << "=" << valueToUse << "|";
  198. }
  199. }
  200. //----------------------------------------------------------------------------
  201. void cmExtraEclipseCDT4Generator::CreateProjectFile()
  202. {
  203. cmMakefile* mf
  204. = this->GlobalGenerator->GetLocalGenerators()[0]->GetMakefile();
  205. const std::string filename = this->HomeOutputDirectory + "/.project";
  206. cmGeneratedFileStream fout(filename.c_str());
  207. if (!fout)
  208. {
  209. return;
  210. }
  211. std::string compilerId = mf->GetSafeDefinition("CMAKE_C_COMPILER_ID");
  212. if (compilerId.empty()) // no C compiler, try the C++ compiler:
  213. {
  214. compilerId = mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID");
  215. }
  216. fout <<
  217. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  218. "<projectDescription>\n"
  219. "\t<name>" <<
  220. this->GenerateProjectName(mf->GetProjectName(),
  221. mf->GetSafeDefinition("CMAKE_BUILD_TYPE"),
  222. this->GetPathBasename(this->HomeOutputDirectory))
  223. << "</name>\n"
  224. "\t<comment></comment>\n"
  225. "\t<projects>\n"
  226. "\t</projects>\n"
  227. "\t<buildSpec>\n"
  228. "\t\t<buildCommand>\n"
  229. "\t\t\t<name>org.eclipse.cdt.make.core.makeBuilder</name>\n"
  230. "\t\t\t<triggers>clean,full,incremental,</triggers>\n"
  231. "\t\t\t<arguments>\n"
  232. ;
  233. // use clean target
  234. fout <<
  235. "\t\t\t\t<dictionary>\n"
  236. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.cleanBuildTarget</key>\n"
  237. "\t\t\t\t\t<value>clean</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.enableCleanBuild</key>\n"
  241. "\t\t\t\t\t<value>true</value>\n"
  242. "\t\t\t\t</dictionary>\n"
  243. "\t\t\t\t<dictionary>\n"
  244. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.append_environment</key>\n"
  245. "\t\t\t\t\t<value>true</value>\n"
  246. "\t\t\t\t</dictionary>\n"
  247. "\t\t\t\t<dictionary>\n"
  248. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.stopOnError</key>\n"
  249. "\t\t\t\t\t<value>true</value>\n"
  250. "\t\t\t\t</dictionary>\n"
  251. ;
  252. // set the make command
  253. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  254. const std::string makeArgs = mf->GetSafeDefinition(
  255. "CMAKE_ECLIPSE_MAKE_ARGUMENTS");
  256. fout <<
  257. "\t\t\t\t<dictionary>\n"
  258. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.enabledIncrementalBuild</key>\n"
  259. "\t\t\t\t\t<value>true</value>\n"
  260. "\t\t\t\t</dictionary>\n"
  261. "\t\t\t\t<dictionary>\n"
  262. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.build.command</key>\n"
  263. "\t\t\t\t\t<value>" << this->GetEclipsePath(make) << "</value>\n"
  264. "\t\t\t\t</dictionary>\n"
  265. "\t\t\t\t<dictionary>\n"
  266. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.contents</key>\n"
  267. "\t\t\t\t\t<value>org.eclipse.cdt.make.core.activeConfigSettings</value>\n"
  268. "\t\t\t\t</dictionary>\n"
  269. "\t\t\t\t<dictionary>\n"
  270. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.build.target.inc</key>\n"
  271. "\t\t\t\t\t<value>all</value>\n"
  272. "\t\t\t\t</dictionary>\n"
  273. "\t\t\t\t<dictionary>\n"
  274. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.build.arguments</key>\n"
  275. "\t\t\t\t\t<value>" << makeArgs << "</value>\n"
  276. "\t\t\t\t</dictionary>\n"
  277. "\t\t\t\t<dictionary>\n"
  278. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.buildLocation</key>\n"
  279. "\t\t\t\t\t<value>"
  280. << this->GetEclipsePath(this->HomeOutputDirectory) << "</value>\n"
  281. "\t\t\t\t</dictionary>\n"
  282. "\t\t\t\t<dictionary>\n"
  283. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>\n"
  284. "\t\t\t\t\t<value>false</value>\n"
  285. "\t\t\t\t</dictionary>\n"
  286. ;
  287. // set project specific environment
  288. fout <<
  289. "\t\t\t\t<dictionary>\n"
  290. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.environment</key>\n"
  291. "\t\t\t\t\t<value>VERBOSE=1|CMAKE_NO_VERBOSE=1|" //verbose Makefile output
  292. ;
  293. // set vsvars32.bat environment available at CMake time,
  294. // but not necessarily when eclipse is open
  295. if (compilerId == "MSVC")
  296. {
  297. AddEnvVar(fout, "PATH", mf);
  298. AddEnvVar(fout, "INCLUDE", mf);
  299. AddEnvVar(fout, "LIB", mf);
  300. AddEnvVar(fout, "LIBPATH", mf);
  301. }
  302. else if (compilerId == "Intel")
  303. {
  304. // if the env.var is set, use this one and put it in the cache
  305. // if the env.var is not set, but the value is in the cache,
  306. // use it from the cache:
  307. AddEnvVar(fout, "INTEL_LICENSE_FILE", mf);
  308. }
  309. fout <<
  310. "</value>\n"
  311. "\t\t\t\t</dictionary>\n"
  312. ;
  313. fout <<
  314. "\t\t\t\t<dictionary>\n"
  315. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.enableFullBuild</key>\n"
  316. "\t\t\t\t\t<value>true</value>\n"
  317. "\t\t\t\t</dictionary>\n"
  318. "\t\t\t\t<dictionary>\n"
  319. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.build.target.auto</key>\n"
  320. "\t\t\t\t\t<value>all</value>\n"
  321. "\t\t\t\t</dictionary>\n"
  322. "\t\t\t\t<dictionary>\n"
  323. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.enableAutoBuild</key>\n"
  324. "\t\t\t\t\t<value>false</value>\n"
  325. "\t\t\t\t</dictionary>\n"
  326. "\t\t\t\t<dictionary>\n"
  327. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.build.target.clean</key>\n"
  328. "\t\t\t\t\t<value>clean</value>\n"
  329. "\t\t\t\t</dictionary>\n"
  330. "\t\t\t\t<dictionary>\n"
  331. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.fullBuildTarget</key>\n"
  332. "\t\t\t\t\t<value>all</value>\n"
  333. "\t\t\t\t</dictionary>\n"
  334. "\t\t\t\t<dictionary>\n"
  335. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.buildArguments</key>\n"
  336. "\t\t\t\t\t<value></value>\n"
  337. "\t\t\t\t</dictionary>\n"
  338. "\t\t\t\t<dictionary>\n"
  339. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.build.location</key>\n"
  340. "\t\t\t\t\t<value>"
  341. << this->GetEclipsePath(this->HomeOutputDirectory) << "</value>\n"
  342. "\t\t\t\t</dictionary>\n"
  343. "\t\t\t\t<dictionary>\n"
  344. "\t\t\t\t\t<key>org.eclipse.cdt.make.core.autoBuildTarget</key>\n"
  345. "\t\t\t\t\t<value>all</value>\n"
  346. "\t\t\t\t</dictionary>\n"
  347. ;
  348. // set error parsers
  349. fout <<
  350. "\t\t\t\t<dictionary>\n"
  351. "\t\t\t\t\t<key>org.eclipse.cdt.core.errorOutputParser</key>\n"
  352. "\t\t\t\t\t<value>"
  353. ;
  354. if (compilerId == "MSVC")
  355. {
  356. fout << "org.eclipse.cdt.core.VCErrorParser;";
  357. }
  358. else if (compilerId == "Intel")
  359. {
  360. fout << "org.eclipse.cdt.core.ICCErrorParser;";
  361. }
  362. fout <<
  363. "org.eclipse.cdt.core.MakeErrorParser;"
  364. "org.eclipse.cdt.core.GCCErrorParser;"
  365. "org.eclipse.cdt.core.GASErrorParser;"
  366. "org.eclipse.cdt.core.GLDErrorParser;"
  367. "</value>\n"
  368. "\t\t\t\t</dictionary>\n"
  369. ;
  370. fout <<
  371. "\t\t\t</arguments>\n"
  372. "\t\t</buildCommand>\n"
  373. "\t\t<buildCommand>\n"
  374. "\t\t\t<name>org.eclipse.cdt.make.core.ScannerConfigBuilder</name>\n"
  375. "\t\t\t<arguments>\n"
  376. "\t\t\t</arguments>\n"
  377. "\t\t</buildCommand>\n"
  378. "\t</buildSpec>\n"
  379. ;
  380. // set natures for c/c++ projects
  381. fout <<
  382. "\t<natures>\n"
  383. // TODO: ccnature only if it is c++ ???
  384. "\t\t<nature>org.eclipse.cdt.core.ccnature</nature>\n"
  385. "\t\t<nature>org.eclipse.cdt.make.core.makeNature</nature>\n"
  386. "\t\t<nature>org.eclipse.cdt.make.core.ScannerConfigNature</nature>\n"
  387. "\t\t<nature>org.eclipse.cdt.core.cnature</nature>\n"
  388. "\t</natures>\n"
  389. ;
  390. fout << "\t<linkedResources>\n";
  391. // create linked resources
  392. if (this->IsOutOfSourceBuild)
  393. {
  394. // create a linked resource to CMAKE_SOURCE_DIR
  395. // (this is not done anymore for each project because of
  396. // http://public.kitware.com/Bug/view.php?id=9978 and because I found it
  397. // actually quite confusing in bigger projects with many directories and
  398. // projects, Alex
  399. std::string sourceLinkedResourceName = "[Source directory]";
  400. std::string linkSourceDirectory = this->GetEclipsePath(
  401. mf->GetStartDirectory());
  402. // .project dir can't be subdir of a linked resource dir
  403. if (!cmSystemTools::IsSubDirectory(this->HomeOutputDirectory.c_str(),
  404. linkSourceDirectory.c_str()))
  405. {
  406. this->AppendLinkedResource(fout, sourceLinkedResourceName,
  407. this->GetEclipsePath(linkSourceDirectory),
  408. LinkToFolder);
  409. this->SrcLinkedResources.push_back(sourceLinkedResourceName);
  410. }
  411. }
  412. if (this->SupportsVirtualFolders)
  413. {
  414. this->CreateLinksToSubprojects(fout, this->HomeOutputDirectory);
  415. this->CreateLinksForTargets(fout);
  416. }
  417. fout << "\t</linkedResources>\n";
  418. fout << "</projectDescription>\n";
  419. }
  420. //----------------------------------------------------------------------------
  421. void cmExtraEclipseCDT4Generator::CreateLinksForTargets(
  422. cmGeneratedFileStream& fout)
  423. {
  424. std::string linkName = "[Targets]";
  425. this->AppendLinkedResource(fout, linkName, "virtual:/virtual",VirtualFolder);
  426. for (std::vector<cmLocalGenerator*>::const_iterator
  427. lgIt = this->GlobalGenerator->GetLocalGenerators().begin();
  428. lgIt != this->GlobalGenerator->GetLocalGenerators().end();
  429. ++lgIt)
  430. {
  431. cmMakefile* makefile = (*lgIt)->GetMakefile();
  432. const cmTargets& targets = makefile->GetTargets();
  433. for(cmTargets::const_iterator ti=targets.begin(); ti!=targets.end();++ti)
  434. {
  435. std::string linkName2 = linkName;
  436. linkName2 += "/";
  437. switch(ti->second.GetType())
  438. {
  439. case cmTarget::EXECUTABLE:
  440. case cmTarget::STATIC_LIBRARY:
  441. case cmTarget::SHARED_LIBRARY:
  442. case cmTarget::MODULE_LIBRARY:
  443. case cmTarget::OBJECT_LIBRARY:
  444. {
  445. const char* prefix = (ti->second.GetType()==cmTarget::EXECUTABLE ?
  446. "[exe] " : "[lib] ");
  447. linkName2 += prefix;
  448. linkName2 += ti->first;
  449. this->AppendLinkedResource(fout, linkName2, "virtual:/virtual",
  450. VirtualFolder);
  451. std::vector<cmSourceGroup> sourceGroups=makefile->GetSourceGroups();
  452. // get the files from the source lists then add them to the groups
  453. cmTarget* tgt = const_cast<cmTarget*>(&ti->second);
  454. std::vector<cmSourceFile*>const & files = tgt->GetSourceFiles();
  455. for(std::vector<cmSourceFile*>::const_iterator sfIt = files.begin();
  456. sfIt != files.end();
  457. sfIt++)
  458. {
  459. // Add the file to the list of sources.
  460. std::string source = (*sfIt)->GetFullPath();
  461. cmSourceGroup& sourceGroup =
  462. makefile->FindSourceGroup(source.c_str(), sourceGroups);
  463. sourceGroup.AssignSource(*sfIt);
  464. }
  465. for(std::vector<cmSourceGroup>::iterator sgIt = sourceGroups.begin();
  466. sgIt != sourceGroups.end();
  467. ++sgIt)
  468. {
  469. std::string linkName3 = linkName2;
  470. linkName3 += "/";
  471. linkName3 += sgIt->GetFullName();
  472. this->AppendLinkedResource(fout, linkName3, "virtual:/virtual",
  473. VirtualFolder);
  474. std::vector<const cmSourceFile*> sFiles = sgIt->GetSourceFiles();
  475. for(std::vector<const cmSourceFile*>::const_iterator fileIt =
  476. sFiles.begin();
  477. fileIt != sFiles.end();
  478. ++fileIt)
  479. {
  480. std::string linkName4 = linkName3;
  481. linkName4 += "/";
  482. linkName4 +=
  483. cmSystemTools::GetFilenameName((*fileIt)->GetFullPath());
  484. this->AppendLinkedResource(fout, linkName4,
  485. (*fileIt)->GetFullPath(), LinkToFile);
  486. }
  487. }
  488. }
  489. break;
  490. // ignore all others:
  491. default:
  492. break;
  493. }
  494. }
  495. }
  496. }
  497. //----------------------------------------------------------------------------
  498. void cmExtraEclipseCDT4Generator::CreateLinksToSubprojects(
  499. cmGeneratedFileStream& fout, const std::string& baseDir)
  500. {
  501. // for each sub project create a linked resource to the source dir
  502. // - only if it is an out-of-source build
  503. this->AppendLinkedResource(fout, "[Subprojects]",
  504. "virtual:/virtual", VirtualFolder);
  505. for (std::map<cmStdString, std::vector<cmLocalGenerator*> >::const_iterator
  506. it = this->GlobalGenerator->GetProjectMap().begin();
  507. it != this->GlobalGenerator->GetProjectMap().end();
  508. ++it)
  509. {
  510. std::string linkSourceDirectory = this->GetEclipsePath(
  511. it->second[0]->GetMakefile()->GetStartDirectory());
  512. // a linked resource must not point to a parent directory of .project or
  513. // .project itself
  514. if ((baseDir != linkSourceDirectory) &&
  515. !cmSystemTools::IsSubDirectory(baseDir.c_str(),
  516. linkSourceDirectory.c_str()))
  517. {
  518. std::string linkName = "[Subprojects]/";
  519. linkName += it->first;
  520. this->AppendLinkedResource(fout, linkName,
  521. this->GetEclipsePath(linkSourceDirectory),
  522. LinkToFolder
  523. );
  524. this->SrcLinkedResources.push_back(it->first);
  525. }
  526. }
  527. }
  528. //----------------------------------------------------------------------------
  529. void cmExtraEclipseCDT4Generator::AppendIncludeDirectories(
  530. cmGeneratedFileStream& fout,
  531. const std::vector<std::string>& includeDirs,
  532. std::set<std::string>& emittedDirs)
  533. {
  534. for(std::vector<std::string>::const_iterator inc = includeDirs.begin();
  535. inc != includeDirs.end();
  536. ++inc)
  537. {
  538. if (!inc->empty())
  539. {
  540. std::string dir = cmSystemTools::CollapseFullPath(inc->c_str());
  541. // handle framework include dirs on OSX, the remainder after the
  542. // Frameworks/ part has to be stripped
  543. // /System/Library/Frameworks/GLUT.framework/Headers
  544. cmsys::RegularExpression frameworkRx("(.+/Frameworks)/.+\\.framework/");
  545. if(frameworkRx.find(dir.c_str()))
  546. {
  547. dir = frameworkRx.match(1);
  548. }
  549. if(emittedDirs.find(dir) == emittedDirs.end())
  550. {
  551. emittedDirs.insert(dir);
  552. fout << "<pathentry include=\""
  553. << cmExtraEclipseCDT4Generator::EscapeForXML(
  554. cmExtraEclipseCDT4Generator::GetEclipsePath(dir))
  555. << "\" kind=\"inc\" path=\"\" system=\"true\"/>\n";
  556. }
  557. }
  558. }
  559. }
  560. //----------------------------------------------------------------------------
  561. void cmExtraEclipseCDT4Generator::CreateCProjectFile() const
  562. {
  563. std::set<std::string> emmited;
  564. const cmMakefile* mf
  565. = this->GlobalGenerator->GetLocalGenerators()[0]->GetMakefile();
  566. const std::string filename = this->HomeOutputDirectory + "/.cproject";
  567. cmGeneratedFileStream fout(filename.c_str());
  568. if (!fout)
  569. {
  570. return;
  571. }
  572. // add header
  573. fout <<
  574. "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
  575. "<?fileVersion 4.0.0?>\n\n"
  576. "<cproject>\n"
  577. "<storageModule moduleId=\"org.eclipse.cdt.core.settings\">\n"
  578. ;
  579. fout << "<cconfiguration id=\"org.eclipse.cdt.core.default.config.1\">\n";
  580. // Configuration settings...
  581. fout <<
  582. "<storageModule"
  583. " buildSystemId=\"org.eclipse.cdt.core.defaultConfigDataProvider\""
  584. " id=\"org.eclipse.cdt.core.default.config.1\""
  585. " moduleId=\"org.eclipse.cdt.core.settings\" name=\"Configuration\">\n"
  586. "<externalSettings/>\n"
  587. "<extensions>\n"
  588. ;
  589. // TODO: refactor this out...
  590. std::string executableFormat = mf->GetSafeDefinition(
  591. "CMAKE_EXECUTABLE_FORMAT");
  592. if (executableFormat == "ELF")
  593. {
  594. fout << "<extension id=\"org.eclipse.cdt.core.ELF\""
  595. " point=\"org.eclipse.cdt.core.BinaryParser\"/>\n"
  596. ;
  597. fout << "<extension id=\"org.eclipse.cdt.core.GNU_ELF\""
  598. " point=\"org.eclipse.cdt.core.BinaryParser\">\n"
  599. "<attribute key=\"addr2line\" value=\"addr2line\"/>\n"
  600. "<attribute key=\"c++filt\" value=\"c++filt\"/>\n"
  601. "</extension>\n"
  602. ;
  603. }
  604. else
  605. {
  606. std::string systemName = mf->GetSafeDefinition("CMAKE_SYSTEM_NAME");
  607. if (systemName == "CYGWIN")
  608. {
  609. fout << "<extension id=\"org.eclipse.cdt.core.Cygwin_PE\""
  610. " point=\"org.eclipse.cdt.core.BinaryParser\">\n"
  611. "<attribute key=\"addr2line\" value=\"addr2line\"/>\n"
  612. "<attribute key=\"c++filt\" value=\"c++filt\"/>\n"
  613. "<attribute key=\"cygpath\" value=\"cygpath\"/>\n"
  614. "<attribute key=\"nm\" value=\"nm\"/>\n"
  615. "</extension>\n"
  616. ;
  617. }
  618. else if (systemName == "Windows")
  619. {
  620. fout << "<extension id=\"org.eclipse.cdt.core.PE\""
  621. " point=\"org.eclipse.cdt.core.BinaryParser\"/>\n"
  622. ;
  623. }
  624. else if (systemName == "Darwin")
  625. {
  626. fout << "<extension id=\"org.eclipse.cdt.core.MachO\""
  627. " point=\"org.eclipse.cdt.core.BinaryParser\">\n"
  628. "<attribute key=\"c++filt\" value=\"c++filt\"/>\n"
  629. "</extension>\n"
  630. ;
  631. }
  632. else
  633. {
  634. // *** Should never get here ***
  635. fout << "<error_toolchain_type/>\n";
  636. }
  637. }
  638. fout << "</extensions>\n"
  639. "</storageModule>\n"
  640. ;
  641. // ???
  642. fout <<
  643. "<storageModule moduleId=\"org.eclipse.cdt.core.language.mapping\">\n"
  644. "<project-mappings/>\n"
  645. "</storageModule>\n"
  646. ;
  647. // ???
  648. fout<<"<storageModule moduleId=\"org.eclipse.cdt.core.externalSettings\"/>\n"
  649. ;
  650. // set the path entries (includes, libs, source dirs, etc.)
  651. fout << "<storageModule moduleId=\"org.eclipse.cdt.core.pathentry\">\n"
  652. ;
  653. // for each sub project with a linked resource to the source dir:
  654. // - make it type 'src'
  655. // - and exclude it from type 'out'
  656. std::string excludeFromOut;
  657. /* I don't know what the pathentry kind="src" are good for, e.g. autocompletion
  658. * works also without them. Done wrong, the indexer complains, see #12417
  659. * and #12213.
  660. * The CDT documentation is very terse on that:
  661. * "CDT_SOURCE: Entry kind constant describing a path entry identifying a
  662. * folder containing source code to be compiled."
  663. * Also on the cdt-dev list didn't bring any information:
  664. * http://web.archiveorange.com/archive/v/B4NlJDNIpYoOS1SbxFNy
  665. * So I'm disabling them for now, hoping that somebody will report if something
  666. * is not workging anymore.
  667. * Alex */
  668. #ifdef DO_CREATE_SRC_PATH_ENTRIES
  669. for (std::vector<std::string>::const_iterator
  670. it = this->SrcLinkedResources.begin();
  671. it != this->SrcLinkedResources.end();
  672. ++it)
  673. {
  674. fout << "<pathentry kind=\"src\" path=\"" << this->EscapeForXML(*it)
  675. << "\"/>\n";
  676. // exlude source directory from output search path
  677. // - only if not named the same as an output directory
  678. if (!cmSystemTools::FileIsDirectory(
  679. std::string(this->HomeOutputDirectory + "/" + *it).c_str()))
  680. {
  681. excludeFromOut += this->EscapeForXML(*it) + "/|";
  682. }
  683. }
  684. #endif
  685. excludeFromOut += "**/CMakeFiles/";
  686. fout << "<pathentry excluding=\"" << excludeFromOut
  687. << "\" kind=\"out\" path=\"\"/>\n";
  688. // add pre-processor definitions to allow eclipse to gray out sections
  689. emmited.clear();
  690. for (std::vector<cmLocalGenerator*>::const_iterator
  691. it = this->GlobalGenerator->GetLocalGenerators().begin();
  692. it != this->GlobalGenerator->GetLocalGenerators().end();
  693. ++it)
  694. {
  695. if(const char* cdefs = (*it)->GetMakefile()->GetProperty(
  696. "COMPILE_DEFINITIONS"))
  697. {
  698. // Expand the list.
  699. std::vector<std::string> defs;
  700. cmSystemTools::ExpandListArgument(cdefs, defs);
  701. for(std::vector<std::string>::const_iterator di = defs.begin();
  702. di != defs.end(); ++di)
  703. {
  704. std::string::size_type equals = di->find('=', 0);
  705. std::string::size_type enddef = di->length();
  706. std::string def;
  707. std::string val;
  708. if (equals != std::string::npos && equals < enddef)
  709. {
  710. // we have -DFOO=BAR
  711. def = di->substr(0, equals);
  712. val = di->substr(equals + 1, enddef - equals + 1);
  713. }
  714. else
  715. {
  716. // we have -DFOO
  717. def = *di;
  718. }
  719. // insert the definition if not already added.
  720. if(emmited.find(def) == emmited.end())
  721. {
  722. emmited.insert(def);
  723. fout << "<pathentry kind=\"mac\" name=\"" << def
  724. << "\" path=\"\" value=\"" << this->EscapeForXML(val)
  725. << "\"/>\n";
  726. }
  727. }
  728. }
  729. }
  730. // add system defined c macros
  731. const char* cDefs=mf->GetDefinition(
  732. "CMAKE_EXTRA_GENERATOR_C_SYSTEM_DEFINED_MACROS");
  733. if(cDefs)
  734. {
  735. // Expand the list.
  736. std::vector<std::string> defs;
  737. cmSystemTools::ExpandListArgument(cDefs, defs, true);
  738. // the list must contain only definition-value pairs:
  739. if ((defs.size() % 2) == 0)
  740. {
  741. std::vector<std::string>::const_iterator di = defs.begin();
  742. while (di != defs.end())
  743. {
  744. std::string def = *di;
  745. ++di;
  746. std::string val;
  747. if (di != defs.end())
  748. {
  749. val = *di;
  750. ++di;
  751. }
  752. // insert the definition if not already added.
  753. if(emmited.find(def) == emmited.end())
  754. {
  755. emmited.insert(def);
  756. fout << "<pathentry kind=\"mac\" name=\"" << def
  757. << "\" path=\"\" value=\"" << this->EscapeForXML(val)
  758. << "\"/>\n";
  759. }
  760. }
  761. }
  762. }
  763. // add system defined c++ macros
  764. const char* cxxDefs = mf->GetDefinition(
  765. "CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS");
  766. if(cxxDefs)
  767. {
  768. // Expand the list.
  769. std::vector<std::string> defs;
  770. cmSystemTools::ExpandListArgument(cxxDefs, defs, true);
  771. // the list must contain only definition-value pairs:
  772. if ((defs.size() % 2) == 0)
  773. {
  774. std::vector<std::string>::const_iterator di = defs.begin();
  775. while (di != defs.end())
  776. {
  777. std::string def = *di;
  778. ++di;
  779. std::string val;
  780. if (di != defs.end())
  781. {
  782. val = *di;
  783. ++di;
  784. }
  785. // insert the definition if not already added.
  786. if(emmited.find(def) == emmited.end())
  787. {
  788. emmited.insert(def);
  789. fout << "<pathentry kind=\"mac\" name=\"" << def
  790. << "\" path=\"\" value=\"" << this->EscapeForXML(val)
  791. << "\"/>\n";
  792. }
  793. }
  794. }
  795. }
  796. // include dirs
  797. emmited.clear();
  798. for (std::vector<cmLocalGenerator*>::const_iterator
  799. it = this->GlobalGenerator->GetLocalGenerators().begin();
  800. it != this->GlobalGenerator->GetLocalGenerators().end();
  801. ++it)
  802. {
  803. cmGeneratorTargetsType targets = (*it)->GetMakefile()
  804. ->GetGeneratorTargets();
  805. for (cmGeneratorTargetsType::iterator l = targets.begin();
  806. l != targets.end(); ++l)
  807. {
  808. std::vector<std::string> includeDirs;
  809. (*it)->GetIncludeDirectories(includeDirs, l->second);
  810. this->AppendIncludeDirectories(fout, includeDirs, emmited);
  811. }
  812. }
  813. // now also the system include directories, in case we found them in
  814. // CMakeSystemSpecificInformation.cmake. This makes Eclipse find the
  815. // standard headers.
  816. std::string compiler = mf->GetSafeDefinition("CMAKE_C_COMPILER");
  817. if (!compiler.empty())
  818. {
  819. std::string systemIncludeDirs = mf->GetSafeDefinition(
  820. "CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
  821. std::vector<std::string> dirs;
  822. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  823. this->AppendIncludeDirectories(fout, dirs, emmited);
  824. }
  825. compiler = mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
  826. if (!compiler.empty())
  827. {
  828. std::string systemIncludeDirs = mf->GetSafeDefinition(
  829. "CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
  830. std::vector<std::string> dirs;
  831. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  832. this->AppendIncludeDirectories(fout, dirs, emmited);
  833. }
  834. fout << "</storageModule>\n";
  835. // add build targets
  836. fout <<
  837. "<storageModule moduleId=\"org.eclipse.cdt.make.core.buildtargets\">\n"
  838. "<buildTargets>\n"
  839. ;
  840. emmited.clear();
  841. const std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  842. const std::string makeArgs = mf->GetSafeDefinition(
  843. "CMAKE_ECLIPSE_MAKE_ARGUMENTS");
  844. const std::string cmake = mf->GetRequiredDefinition("CMAKE_COMMAND");
  845. cmGlobalGenerator* generator
  846. = const_cast<cmGlobalGenerator*>(this->GlobalGenerator);
  847. std::string allTarget;
  848. std::string cleanTarget;
  849. if (generator->GetAllTargetName())
  850. {
  851. allTarget = generator->GetAllTargetName();
  852. }
  853. if (generator->GetCleanTargetName())
  854. {
  855. cleanTarget = generator->GetCleanTargetName();
  856. }
  857. // add all executable and library targets and some of the GLOBAL
  858. // and UTILITY targets
  859. for (std::vector<cmLocalGenerator*>::const_iterator
  860. it = this->GlobalGenerator->GetLocalGenerators().begin();
  861. it != this->GlobalGenerator->GetLocalGenerators().end();
  862. ++it)
  863. {
  864. const cmTargets& targets = (*it)->GetMakefile()->GetTargets();
  865. cmMakefile* makefile=(*it)->GetMakefile();
  866. std::string subdir = (*it)->Convert(makefile->GetCurrentOutputDirectory(),
  867. cmLocalGenerator::HOME_OUTPUT);
  868. if (subdir == ".")
  869. {
  870. subdir = "";
  871. }
  872. for(cmTargets::const_iterator ti=targets.begin(); ti!=targets.end(); ++ti)
  873. {
  874. switch(ti->second.GetType())
  875. {
  876. case cmTarget::GLOBAL_TARGET:
  877. {
  878. bool insertTarget = false;
  879. // Only add the global targets from CMAKE_BINARY_DIR,
  880. // not from the subdirs
  881. if (subdir.empty())
  882. {
  883. insertTarget = true;
  884. // only add the "edit_cache" target if it's not ccmake, because
  885. // this will not work within the IDE
  886. if (ti->first == "edit_cache")
  887. {
  888. const char* editCommand = makefile->GetDefinition
  889. ("CMAKE_EDIT_COMMAND");
  890. if (editCommand == 0)
  891. {
  892. insertTarget = false;
  893. }
  894. else if (strstr(editCommand, "ccmake")!=NULL)
  895. {
  896. insertTarget = false;
  897. }
  898. }
  899. }
  900. if (insertTarget)
  901. {
  902. this->AppendTarget(fout, ti->first, make, makeArgs, subdir, ": ");
  903. }
  904. }
  905. break;
  906. case cmTarget::UTILITY:
  907. // Add all utility targets, except the Nightly/Continuous/
  908. // Experimental-"sub"targets as e.g. NightlyStart
  909. if (((ti->first.find("Nightly")==0) &&(ti->first!="Nightly"))
  910. || ((ti->first.find("Continuous")==0)&&(ti->first!="Continuous"))
  911. || ((ti->first.find("Experimental")==0)
  912. && (ti->first!="Experimental")))
  913. {
  914. break;
  915. }
  916. this->AppendTarget(fout, ti->first, make, makeArgs, subdir, ": ");
  917. break;
  918. case cmTarget::EXECUTABLE:
  919. case cmTarget::STATIC_LIBRARY:
  920. case cmTarget::SHARED_LIBRARY:
  921. case cmTarget::MODULE_LIBRARY:
  922. case cmTarget::OBJECT_LIBRARY:
  923. {
  924. const char* prefix = (ti->second.GetType()==cmTarget::EXECUTABLE ?
  925. "[exe] " : "[lib] ");
  926. this->AppendTarget(fout, ti->first, make, makeArgs, subdir, prefix);
  927. std::string fastTarget = ti->first;
  928. fastTarget += "/fast";
  929. this->AppendTarget(fout, fastTarget, make, makeArgs, subdir, prefix);
  930. // Add Build and Clean targets in the virtual folder of targets:
  931. if (this->SupportsVirtualFolders)
  932. {
  933. std::string virtDir = "[Targets]/";
  934. virtDir += prefix;
  935. virtDir += ti->first;
  936. std::string buildArgs = "-C \"";
  937. buildArgs += makefile->GetHomeOutputDirectory();
  938. buildArgs += "\" ";
  939. buildArgs += makeArgs;
  940. this->AppendTarget(fout, "Build", make, buildArgs, virtDir, "",
  941. ti->first.c_str());
  942. std::string cleanArgs = "-E chdir \"";
  943. cleanArgs += makefile->GetCurrentOutputDirectory();
  944. cleanArgs += "\" \"";
  945. cleanArgs += cmake;
  946. cleanArgs += "\" -P \"";
  947. cleanArgs += (*it)->GetTargetDirectory(ti->second);
  948. cleanArgs += "/cmake_clean.cmake\"";
  949. this->AppendTarget(fout, "Clean", cmake, cleanArgs, virtDir, "", "");
  950. }
  951. }
  952. break;
  953. default:
  954. break;
  955. }
  956. }
  957. // insert the all and clean targets in every subdir
  958. if (!allTarget.empty())
  959. {
  960. this->AppendTarget(fout, allTarget, make, makeArgs, subdir, ": ");
  961. }
  962. if (!cleanTarget.empty())
  963. {
  964. this->AppendTarget(fout, cleanTarget, make, makeArgs, subdir, ": ");
  965. }
  966. //insert rules for compiling, preprocessing and assembling individual files
  967. std::vector<std::string> objectFileTargets;
  968. (*it)->GetIndividualFileTargets(objectFileTargets);
  969. for(std::vector<std::string>::const_iterator fit=objectFileTargets.begin();
  970. fit != objectFileTargets.end();
  971. ++fit)
  972. {
  973. const char* prefix = "[obj] ";
  974. if ((*fit)[fit->length()-1] == 's')
  975. {
  976. prefix = "[to asm] ";
  977. }
  978. else if ((*fit)[fit->length()-1] == 'i')
  979. {
  980. prefix = "[pre] ";
  981. }
  982. this->AppendTarget(fout, *fit, make, makeArgs, subdir, prefix);
  983. }
  984. }
  985. fout << "</buildTargets>\n"
  986. "</storageModule>\n"
  987. ;
  988. this->AppendStorageScanners(fout, *mf);
  989. fout << "</cconfiguration>\n"
  990. "</storageModule>\n"
  991. "<storageModule moduleId=\"cdtBuildSystem\" version=\"4.0.0\">\n"
  992. "<project id=\"" << this->EscapeForXML(mf->GetProjectName())
  993. << ".null.1\" name=\"" << this->EscapeForXML(mf->GetProjectName())
  994. << "\"/>\n"
  995. "</storageModule>\n"
  996. "</cproject>\n"
  997. ;
  998. }
  999. //----------------------------------------------------------------------------
  1000. std::string
  1001. cmExtraEclipseCDT4Generator::GetEclipsePath(const std::string& path)
  1002. {
  1003. #if defined(__CYGWIN__)
  1004. std::string cmd = "cygpath -m " + path;
  1005. std::string out;
  1006. if (!cmSystemTools::RunCommand(cmd.c_str(), out, 0, false))
  1007. {
  1008. return path;
  1009. }
  1010. else
  1011. {
  1012. out.erase(out.find_last_of('\n'));
  1013. return out;
  1014. }
  1015. #else
  1016. return path;
  1017. #endif
  1018. }
  1019. std::string
  1020. cmExtraEclipseCDT4Generator::GetPathBasename(const std::string& path)
  1021. {
  1022. std::string outputBasename = path;
  1023. while (outputBasename.size() > 0 &&
  1024. (outputBasename[outputBasename.size() - 1] == '/' ||
  1025. outputBasename[outputBasename.size() - 1] == '\\'))
  1026. {
  1027. outputBasename.resize(outputBasename.size() - 1);
  1028. }
  1029. std::string::size_type loc = outputBasename.find_last_of("/\\");
  1030. if (loc != std::string::npos)
  1031. {
  1032. outputBasename = outputBasename.substr(loc + 1);
  1033. }
  1034. return outputBasename;
  1035. }
  1036. std::string
  1037. cmExtraEclipseCDT4Generator::GenerateProjectName(const std::string& name,
  1038. const std::string& type,
  1039. const std::string& path)
  1040. {
  1041. return cmExtraEclipseCDT4Generator::EscapeForXML(name)
  1042. +(type.empty() ? "" : "-") + type + "@" + path;
  1043. }
  1044. std::string cmExtraEclipseCDT4Generator::EscapeForXML(const std::string& value)
  1045. {
  1046. std::string str = value;
  1047. cmSystemTools::ReplaceString(str, "&", "&amp;");
  1048. cmSystemTools::ReplaceString(str, "<", "&lt;");
  1049. cmSystemTools::ReplaceString(str, ">", "&gt;");
  1050. cmSystemTools::ReplaceString(str, "\"", "&quot;");
  1051. // NOTE: This one is not necessary, since as of Eclipse CDT4 it will
  1052. // automatically change this to the original value (').
  1053. //cmSystemTools::ReplaceString(str, "'", "&apos;");
  1054. return str;
  1055. }
  1056. //----------------------------------------------------------------------------
  1057. // Helper functions
  1058. //----------------------------------------------------------------------------
  1059. void cmExtraEclipseCDT4Generator
  1060. ::AppendStorageScanners(cmGeneratedFileStream& fout,
  1061. const cmMakefile& makefile)
  1062. {
  1063. // we need the "make" and the C (or C++) compiler which are used, Alex
  1064. std::string make = makefile.GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  1065. std::string compiler = makefile.GetSafeDefinition("CMAKE_C_COMPILER");
  1066. std::string arg1 = makefile.GetSafeDefinition("CMAKE_C_COMPILER_ARG1");
  1067. if (compiler.empty())
  1068. {
  1069. compiler = makefile.GetSafeDefinition("CMAKE_CXX_COMPILER");
  1070. arg1 = makefile.GetSafeDefinition("CMAKE_CXX_COMPILER_ARG1");
  1071. }
  1072. if (compiler.empty()) //Hmm, what to do now ?
  1073. {
  1074. compiler = "gcc";
  1075. }
  1076. // the following right now hardcodes gcc behaviour :-/
  1077. std::string compilerArgs =
  1078. "-E -P -v -dD ${plugin_state_location}/${specs_file}";
  1079. if (!arg1.empty())
  1080. {
  1081. arg1 += " ";
  1082. compilerArgs = arg1 + compilerArgs;
  1083. }
  1084. fout <<
  1085. "<storageModule moduleId=\"scannerConfiguration\">\n"
  1086. "<autodiscovery enabled=\"true\" problemReportingEnabled=\"true\""
  1087. " selectedProfileId="
  1088. "\"org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile\"/>\n"
  1089. ;
  1090. cmExtraEclipseCDT4Generator::AppendScannerProfile(fout,
  1091. "org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile",
  1092. true, "", true, "specsFile",
  1093. compilerArgs,
  1094. compiler, true, true);
  1095. cmExtraEclipseCDT4Generator::AppendScannerProfile(fout,
  1096. "org.eclipse.cdt.make.core.GCCStandardMakePerFileProfile",
  1097. true, "", true, "makefileGenerator",
  1098. "-f ${project_name}_scd.mk",
  1099. make, true, true);
  1100. fout << "</storageModule>\n";
  1101. }
  1102. // The prefix is prepended before the actual name of the target. The purpose
  1103. // of that is to sort the targets in the view of Eclipse, so that at first
  1104. // the global/utility/all/clean targets appear ": ", then the executable
  1105. // targets "[exe] ", then the libraries "[lib]", then the rules for the
  1106. // object files "[obj]", then for preprocessing only "[pre] " and
  1107. // finally the assembly files "[to asm] ". Note the "to" in "to asm",
  1108. // without it, "asm" would be the first targets in the list, with the "to"
  1109. // they are the last targets, which makes more sense.
  1110. void cmExtraEclipseCDT4Generator::AppendTarget(cmGeneratedFileStream& fout,
  1111. const std::string& target,
  1112. const std::string& make,
  1113. const std::string& makeArgs,
  1114. const std::string& path,
  1115. const char* prefix,
  1116. const char* makeTarget
  1117. )
  1118. {
  1119. std::string targetXml = cmExtraEclipseCDT4Generator::EscapeForXML(target);
  1120. std::string makeTargetXml = targetXml;
  1121. if (makeTarget != NULL)
  1122. {
  1123. makeTargetXml = cmExtraEclipseCDT4Generator::EscapeForXML(makeTarget);
  1124. }
  1125. cmExtraEclipseCDT4Generator::EscapeForXML(target);
  1126. std::string pathXml = cmExtraEclipseCDT4Generator::EscapeForXML(path);
  1127. fout <<
  1128. "<target name=\"" << prefix << targetXml << "\""
  1129. " path=\"" << pathXml.c_str() << "\""
  1130. " targetID=\"org.eclipse.cdt.make.MakeTargetBuilder\">\n"
  1131. "<buildCommand>"
  1132. << cmExtraEclipseCDT4Generator::GetEclipsePath(make)
  1133. << "</buildCommand>\n"
  1134. "<buildArguments>" << makeArgs << "</buildArguments>\n"
  1135. "<buildTarget>" << makeTargetXml << "</buildTarget>\n"
  1136. "<stopOnError>true</stopOnError>\n"
  1137. "<useDefaultCommand>false</useDefaultCommand>\n"
  1138. "</target>\n"
  1139. ;
  1140. }
  1141. void cmExtraEclipseCDT4Generator
  1142. ::AppendScannerProfile(cmGeneratedFileStream& fout,
  1143. const std::string& profileID,
  1144. bool openActionEnabled,
  1145. const std::string& openActionFilePath,
  1146. bool pParserEnabled,
  1147. const std::string& scannerInfoProviderID,
  1148. const std::string& runActionArguments,
  1149. const std::string& runActionCommand,
  1150. bool runActionUseDefault,
  1151. bool sipParserEnabled)
  1152. {
  1153. fout <<
  1154. "<profile id=\"" << profileID << "\">\n"
  1155. "<buildOutputProvider>\n"
  1156. "<openAction enabled=\"" << (openActionEnabled ? "true" : "false")
  1157. << "\" filePath=\"" << openActionFilePath << "\"/>\n"
  1158. "<parser enabled=\"" << (pParserEnabled ? "true" : "false") << "\"/>\n"
  1159. "</buildOutputProvider>\n"
  1160. "<scannerInfoProvider id=\"" << scannerInfoProviderID << "\">\n"
  1161. "<runAction arguments=\"" << runActionArguments << "\""
  1162. " command=\"" << runActionCommand
  1163. << "\" useDefault=\"" << (runActionUseDefault ? "true":"false") << "\"/>\n"
  1164. "<parser enabled=\"" << (sipParserEnabled ? "true" : "false") << "\"/>\n"
  1165. "</scannerInfoProvider>\n"
  1166. "</profile>\n"
  1167. ;
  1168. }
  1169. void cmExtraEclipseCDT4Generator
  1170. ::AppendLinkedResource (cmGeneratedFileStream& fout,
  1171. const std::string& name,
  1172. const std::string& path,
  1173. LinkType linkType)
  1174. {
  1175. const char* locationTag = "location";
  1176. const char* typeTag = "2";
  1177. if (linkType == VirtualFolder) // ... and not a linked folder
  1178. {
  1179. locationTag = "locationURI";
  1180. }
  1181. if (linkType == LinkToFile)
  1182. {
  1183. typeTag = "1";
  1184. }
  1185. fout <<
  1186. "\t\t<link>\n"
  1187. "\t\t\t<name>"
  1188. << cmExtraEclipseCDT4Generator::EscapeForXML(name)
  1189. << "</name>\n"
  1190. "\t\t\t<type>" << typeTag << "</type>\n"
  1191. "\t\t\t<" << locationTag << ">"
  1192. << cmExtraEclipseCDT4Generator::EscapeForXML(path)
  1193. << "</" << locationTag << ">\n"
  1194. "\t\t</link>\n"
  1195. ;
  1196. }