cmExtraEclipseCDT4Generator.cxx 47 KB

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