cmExtraEclipseCDT4Generator.cxx 47 KB

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