cmExtraEclipseCDT4Generator.cxx 47 KB

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