cmExtraEclipseCDT4Generator.cxx 47 KB

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