cmExtraEclipseCDT4Generator.cxx 47 KB

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