cmExtraEclipseCDT4Generator.cxx 47 KB

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