cmExtraEclipseCDT4Generator.cxx 46 KB

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