cmExtraEclipseCDT4Generator.cxx 47 KB

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