cmExtraEclipseCDT4Generator.cxx 47 KB

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