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