cmExtraEclipseCDT4Generator.cxx 47 KB

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