cmExtraEclipseCDT4Generator.cxx 46 KB

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