cmExtraEclipseCDT4Generator.cxx 47 KB

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