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