cmGlobalKdevelopGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. Copyright (c) 2004 Alexander Neundorf, [email protected]. All rights reserved.
  9. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  10. This software is distributed WITHOUT ANY WARRANTY; without even
  11. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12. PURPOSE. See the above copyright notices for more information.
  13. =========================================================================*/
  14. #include "cmGlobalKdevelopGenerator.h"
  15. #include "cmLocalKdevelopGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmake.h"
  18. #include "cmSourceFile.h"
  19. #include "cmGeneratedFileStream.h"
  20. cmGlobalKdevelopGenerator::cmGlobalKdevelopGenerator()
  21. {
  22. // This type of makefile always requires unix style paths
  23. this->ForceUnixPaths = true;
  24. this->FindMakeProgramFile = "CMakeUnixFindMake.cmake";
  25. this->ToolSupportsColor = false;
  26. }
  27. ///! Create a local generator appropriate to this Global Generator
  28. cmLocalGenerator *cmGlobalKdevelopGenerator::CreateLocalGenerator()
  29. {
  30. cmLocalGenerator *lg = new cmLocalKdevelopGenerator;
  31. lg->SetGlobalGenerator(this);
  32. return lg;
  33. }
  34. //----------------------------------------------------------------------------
  35. void cmGlobalKdevelopGenerator::GetDocumentation(cmDocumentationEntry& entry) const
  36. {
  37. entry.name = this->GetName();
  38. entry.brief = "Generates KDevelop 3 project files.";
  39. entry.full =
  40. "Project files for KDevelop 3 will be created in the top directory and in every subdirectory "
  41. "which features a CMakeLists.txt file containing a PROJECT() call. "
  42. "If you change the settings using KDevelop cmake will try its best "
  43. "to keep your changes when regenerating the project files. "
  44. "Additionally a hierarchy of UNIX makefiles is generated into the build tree. Any "
  45. "standard UNIX-style make program can build the project through the "
  46. "default make target. A \"make install\" target is also provided.";
  47. }
  48. void cmGlobalKdevelopGenerator::Generate()
  49. {
  50. this->cmGlobalUnixMakefileGenerator3::Generate();
  51. // for each sub project in the project create
  52. // a kdevelop project
  53. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  54. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  55. {
  56. cmMakefile* mf = it->second[0]->GetMakefile();
  57. std::string outputDir=mf->GetStartOutputDirectory();
  58. std::string projectDir=mf->GetHomeDirectory();
  59. std::string projectName=mf->GetProjectName();
  60. std::string cmakeFilePattern("CMakeLists.txt;*.cmake;");
  61. std::string fileToOpen;
  62. std::vector<cmLocalGenerator*>& lgs= it->second;
  63. // create the project.kdevelop.filelist file
  64. if(!this->CreateFilelistFile(it->second[0], lgs,
  65. outputDir, projectDir,
  66. projectName, cmakeFilePattern, fileToOpen))
  67. {
  68. cmSystemTools::Error("Can not create filelist file");
  69. return;
  70. }
  71. //try to find the name of an executable so we have something to
  72. //run from kdevelop for now just pick the first executable found
  73. std::string executable;
  74. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  75. lg!=lgs.end(); lg++)
  76. {
  77. cmMakefile* makefile=(*lg)->GetMakefile();
  78. cmTargets& targets=makefile->GetTargets();
  79. for (cmTargets::iterator ti = targets.begin();
  80. ti != targets.end(); ti++)
  81. {
  82. if (ti->second.GetType()==cmTarget::EXECUTABLE)
  83. {
  84. executable = ti->second.GetProperty("LOCATION");
  85. break;
  86. }
  87. }
  88. if (!executable.empty())
  89. {
  90. break;
  91. }
  92. }
  93. // now create a project file
  94. this->CreateProjectFile(outputDir, projectDir, projectName,
  95. executable, cmakeFilePattern, fileToOpen);
  96. }
  97. }
  98. bool cmGlobalKdevelopGenerator
  99. ::CreateFilelistFile(cmLocalGenerator* ,
  100. std::vector<cmLocalGenerator*>& lgs,
  101. const std::string& outputDir,
  102. const std::string& projectDirIn,
  103. const std::string& projectname,
  104. std::string& cmakeFilePattern,
  105. std::string& fileToOpen)
  106. {
  107. std::string projectDir = projectDirIn + "/";
  108. std::string filename = outputDir+ "/" + projectname +".kdevelop.filelist";
  109. std::set<cmStdString> files;
  110. std::string tmp;
  111. for (std::vector<cmLocalGenerator*>::const_iterator it=lgs.begin();
  112. it!=lgs.end(); it++)
  113. {
  114. cmMakefile* makefile=(*it)->GetMakefile();
  115. const std::vector<std::string>& listFiles=makefile->GetListFiles();
  116. for (std::vector<std::string>::const_iterator lt=listFiles.begin();
  117. lt!=listFiles.end(); lt++)
  118. {
  119. tmp=*lt;
  120. cmSystemTools::ReplaceString(tmp, projectDir.c_str(), "");
  121. // make sure the file is part of this source tree
  122. if ((tmp[0]!='/') && (strstr(tmp.c_str(), "CMakeFiles/")==0))
  123. {
  124. files.insert(tmp);
  125. tmp=cmSystemTools::GetFilenameName(tmp);
  126. //add all files which dont match the default
  127. // */CMakeLists.txt;*cmake; to the file pattern
  128. if ((tmp!="CMakeLists.txt")
  129. && (strstr(tmp.c_str(), ".cmake")==0))
  130. {
  131. cmakeFilePattern+=tmp+";";
  132. }
  133. }
  134. }
  135. //get all sources
  136. cmTargets& targets=makefile->GetTargets();
  137. for (cmTargets::iterator ti = targets.begin();
  138. ti != targets.end(); ti++)
  139. {
  140. const std::vector<cmSourceFile*>& sources=ti->second.GetSourceFiles();
  141. for (std::vector<cmSourceFile*>::const_iterator si=sources.begin();
  142. si!=sources.end(); si++)
  143. {
  144. tmp=(*si)->GetFullPath();
  145. cmSystemTools::ReplaceString(tmp, projectDir.c_str(), "");
  146. if ((tmp[0]!='/') && (strstr(tmp.c_str(), "CMakeFiles/")==0))
  147. {
  148. files.insert(tmp);
  149. }
  150. }
  151. for (std::vector<std::string>::const_iterator lt=listFiles.begin();
  152. lt!=listFiles.end(); lt++)
  153. {
  154. tmp=*lt;
  155. cmSystemTools::ReplaceString(tmp, projectDir.c_str(), "");
  156. if ((tmp[0]!='/') && (strstr(tmp.c_str(), "CMakeFiles/")==0))
  157. {
  158. files.insert(tmp.c_str());
  159. }
  160. }
  161. }
  162. }
  163. //check if the output file already exists and read it
  164. //insert all files which exist into the set of files
  165. std::ifstream oldFilelist(filename.c_str());
  166. if (oldFilelist)
  167. {
  168. while (cmSystemTools::GetLineFromStream(oldFilelist, tmp))
  169. {
  170. if (tmp[0]=='/')
  171. {
  172. continue;
  173. }
  174. std::string completePath=projectDir+tmp;
  175. if (cmSystemTools::FileExists(completePath.c_str()))
  176. {
  177. files.insert(tmp);
  178. }
  179. }
  180. oldFilelist.close();
  181. }
  182. //now write the new filename
  183. cmGeneratedFileStream fout(filename.c_str());
  184. if(!fout)
  185. {
  186. return false;
  187. }
  188. fileToOpen="";
  189. for (std::set<cmStdString>::const_iterator it=files.begin();
  190. it!=files.end(); it++)
  191. {
  192. // get the full path to the file
  193. tmp=cmSystemTools::CollapseFullPath(it->c_str(), projectDir.c_str());
  194. // just select the first source file
  195. if (fileToOpen.empty())
  196. {
  197. std::string ext = cmSystemTools::GetFilenameExtension(tmp);
  198. if ((ext==".c") || (ext==".cc") || (ext==".cpp") || (ext==".C") || (ext==".h"))
  199. {
  200. fileToOpen=tmp;
  201. }
  202. }
  203. // make it relative to the project dir
  204. cmSystemTools::ReplaceString(tmp, projectDir.c_str(), "");
  205. // only put relative paths
  206. if (tmp.size() && tmp[0] != '/')
  207. {
  208. fout << tmp.c_str() <<"\n";
  209. }
  210. }
  211. return true;
  212. }
  213. /* create the project file, if it already exists, merge it with the
  214. existing one, otherwise create a new one */
  215. void
  216. cmGlobalKdevelopGenerator::CreateProjectFile(const std::string& outputDir,
  217. const std::string& projectDir,
  218. const std::string& projectname,
  219. const std::string& executable,
  220. const std::string& cmakeFilePattern,
  221. const std::string& fileToOpen)
  222. {
  223. std::string filename=outputDir+"/";
  224. filename+=projectname+".kdevelop";
  225. std::string sessionFilename=outputDir+"/";
  226. sessionFilename+=projectname+".kdevses";
  227. if (cmSystemTools::FileExists(filename.c_str()))
  228. {
  229. this->MergeProjectFiles(outputDir, projectDir, filename,
  230. executable, cmakeFilePattern, fileToOpen, sessionFilename);
  231. }
  232. else
  233. {
  234. this->CreateNewProjectFile(outputDir, projectDir, filename,
  235. executable, cmakeFilePattern, fileToOpen, sessionFilename);
  236. }
  237. }
  238. void
  239. cmGlobalKdevelopGenerator::MergeProjectFiles(const std::string& outputDir,
  240. const std::string& projectDir,
  241. const std::string& filename,
  242. const std::string& executable,
  243. const std::string& cmakeFilePattern,
  244. const std::string& fileToOpen,
  245. const std::string& sessionFilename)
  246. {
  247. std::ifstream oldProjectFile(filename.c_str());
  248. if (!oldProjectFile)
  249. {
  250. this->CreateNewProjectFile(outputDir, projectDir, filename, executable, cmakeFilePattern, fileToOpen, sessionFilename);
  251. return;
  252. }
  253. /* Read the existing project file (line by line), copy all lines
  254. into the new project file, except the ones which can be reliably
  255. set from contents of the CMakeLists.txt */
  256. std::string tmp;
  257. std::vector<std::string> lines;
  258. while (cmSystemTools::GetLineFromStream(oldProjectFile, tmp))
  259. {
  260. lines.push_back(tmp);
  261. }
  262. oldProjectFile.close();
  263. cmGeneratedFileStream fout(filename.c_str());
  264. if(!fout)
  265. {
  266. return;
  267. }
  268. for (std::vector<std::string>::const_iterator it=lines.begin();
  269. it!=lines.end(); it++)
  270. {
  271. const char* line=(*it).c_str();
  272. // skip these tags as they are always replaced
  273. if ((strstr(line, "<projectdirectory>")!=0)
  274. || (strstr(line, "<projectmanagement>")!=0)
  275. || (strstr(line, "<absoluteprojectpath>")!=0)
  276. || (strstr(line, "<filelistdirectory>")!=0)
  277. || (strstr(line, "<buildtool>")!=0)
  278. || (strstr(line, "<builddir>")!=0))
  279. {
  280. continue;
  281. }
  282. // output the line from the file if it is not one of the above tags
  283. fout<<*it<<"\n";
  284. // if this is the <general> tag output the stuff that goes in the general tag
  285. if (strstr(line, "<general>"))
  286. {
  287. fout<<" <projectmanagement>KDevCustomProject</projectmanagement>\n";
  288. fout<<" <projectdirectory>"<<projectDir.c_str()<<"</projectdirectory>\n"; //this one is important
  289. fout<<" <absoluteprojectpath>true</absoluteprojectpath>\n"; //and this one
  290. }
  291. // inside kdevcustomproject the <filelistdirectory> must be put
  292. if (strstr(line, "<kdevcustomproject>"))
  293. {
  294. fout<<" <filelistdirectory>"<<outputDir.c_str()
  295. <<"</filelistdirectory>\n";
  296. }
  297. // buildtool and builddir go inside <build>
  298. if (strstr(line, "<build>"))
  299. {
  300. fout<<" <buildtool>make</buildtool>\n";
  301. fout<<" <builddir>"<<outputDir.c_str()<<"</builddir>\n";
  302. }
  303. }
  304. }
  305. void
  306. cmGlobalKdevelopGenerator::CreateNewProjectFile(const std::string& outputDir,
  307. const std::string& projectDir,
  308. const std::string& filename,
  309. const std::string& executable,
  310. const std::string& cmakeFilePattern,
  311. const std::string& fileToOpen,
  312. const std::string& sessionFilename)
  313. {
  314. cmGeneratedFileStream fout(filename.c_str());
  315. if(!fout)
  316. {
  317. return;
  318. }
  319. fout<<"<?xml version = '1.0'?>\n";
  320. fout<<"<kdevelop>\n";
  321. fout<<" <general>\n";
  322. fout<<" <author></author>\n";
  323. fout<<" <email></email>\n";
  324. fout<<" <version>$VERSION$</version>\n";
  325. fout<<" <projectmanagement>KDevCustomProject</projectmanagement>\n";
  326. fout<<" <primarylanguage>C++</primarylanguage>\n";
  327. fout<<" <ignoreparts/>\n";
  328. fout<<" <projectdirectory>"<<projectDir.c_str()<<"</projectdirectory>\n"; //this one is important
  329. fout<<" <absoluteprojectpath>true</absoluteprojectpath>\n"; //and this one
  330. fout<<" <secondaryLanguages>\n";
  331. fout<<" <language>C</language>\n";
  332. fout<<" </secondaryLanguages>\n";
  333. fout<<" </general>\n";
  334. fout<<" <kdevcustomproject>\n";
  335. fout<<" <filelistdirectory>"<<outputDir.c_str()<<"</filelistdirectory>\n";
  336. fout<<" <run>\n";
  337. fout<<" <mainprogram>"<<executable.c_str()<<"</mainprogram>\n";
  338. fout<<" <directoryradio>custom</directoryradio>\n";
  339. fout<<" <customdirectory>/</customdirectory>\n";
  340. fout<<" <programargs></programargs>\n";
  341. fout<<" <terminal>false</terminal>\n";
  342. fout<<" <autocompile>true</autocompile>\n";
  343. fout<<" <envvars/>\n";
  344. fout<<" </run>\n";
  345. fout<<" <build>\n";
  346. fout<<" <buildtool>make</buildtool>\n"; //this one is important
  347. fout<<" <builddir>"<<outputDir.c_str()<<"</builddir>\n"; //and this one
  348. fout<<" </build>\n";
  349. fout<<" <make>\n";
  350. fout<<" <abortonerror>false</abortonerror>\n";
  351. fout<<" <numberofjobs>1</numberofjobs>\n";
  352. fout<<" <dontact>false</dontact>\n";
  353. fout<<" <makebin></makebin>\n";
  354. fout<<" <selectedenvironment>default</selectedenvironment>\n";
  355. fout<<" <environments>\n";
  356. fout<<" <default/>\n";
  357. fout<<" </environments>\n";
  358. fout<<" </make>\n";
  359. fout<<" </kdevcustomproject>\n";
  360. fout<<" <kdevfilecreate>\n";
  361. fout<<" <filetypes/>\n";
  362. fout<<" <useglobaltypes>\n";
  363. fout<<" <type ext=\"ui\" />\n";
  364. fout<<" <type ext=\"cpp\" />\n";
  365. fout<<" <type ext=\"h\" />\n";
  366. fout<<" </useglobaltypes>\n";
  367. fout<<" </kdevfilecreate>\n";
  368. fout<<" <kdevdoctreeview>\n";
  369. fout<<" <projectdoc>\n";
  370. fout<<" <userdocDir>html/</userdocDir>\n";
  371. fout<<" <apidocDir>html/</apidocDir>\n";
  372. fout<<" </projectdoc>\n";
  373. fout<<" <ignoreqt_xml/>\n";
  374. fout<<" <ignoredoxygen/>\n";
  375. fout<<" <ignorekdocs/>\n";
  376. fout<<" <ignoretocs/>\n";
  377. fout<<" <ignoredevhelp/>\n";
  378. fout<<" </kdevdoctreeview>\n";
  379. fout<<" <cppsupportpart>\n";
  380. fout<<" <filetemplates>\n";
  381. fout<<" <interfacesuffix>.h</interfacesuffix>\n";
  382. fout<<" <implementationsuffix>.cpp</implementationsuffix>\n";
  383. fout<<" </filetemplates>\n";
  384. fout<<" </cppsupportpart>\n";
  385. fout<<" <kdevcppsupport>\n";
  386. fout<<" <codecompletion>\n";
  387. fout<<" <includeGlobalFunctions>true</includeGlobalFunctions>\n";
  388. fout<<" <includeTypes>true</includeTypes>\n";
  389. fout<<" <includeEnums>true</includeEnums>\n";
  390. fout<<" <includeTypedefs>false</includeTypedefs>\n";
  391. fout<<" <automaticCodeCompletion>true</automaticCodeCompletion>\n";
  392. fout<<" <automaticArgumentsHint>true</automaticArgumentsHint>\n";
  393. fout<<" <automaticHeaderCompletion>true</automaticHeaderCompletion>\n";
  394. fout<<" <codeCompletionDelay>250</codeCompletionDelay>\n";
  395. fout<<" <argumentsHintDelay>400</argumentsHintDelay>\n";
  396. fout<<" <headerCompletionDelay>250</headerCompletionDelay>\n";
  397. fout<<" </codecompletion>\n";
  398. fout<<" <references/>\n";
  399. fout<<" </kdevcppsupport>\n";
  400. fout<<" <kdevfileview>\n";
  401. fout<<" <groups>\n";
  402. fout<<" <group pattern=\""<<cmakeFilePattern.c_str()<<"\" name=\"CMake\" />\n";
  403. fout<<" <group pattern=\"*.h;*.hxx\" name=\"Header\" />\n";
  404. fout<<" <group pattern=\"*.cpp;*.c;*.C;*.cxx\" name=\"Sources\" />\n";
  405. fout<<" <group pattern=\"*.ui\" name=\"Qt Designer files\" />\n";
  406. fout<<" <hidenonprojectfiles>true</hidenonprojectfiles>\n";
  407. fout<<" </groups>\n";
  408. fout<<" <tree>\n";
  409. fout<<" <hidepatterns>*.o,*.lo,CVS,*~,cmake*</hidepatterns>\n";
  410. fout<<" <hidenonprojectfiles>true</hidenonprojectfiles>\n";
  411. fout<<" </tree>\n";
  412. fout<<" </kdevfileview>\n";
  413. fout<<"</kdevelop>\n";
  414. if (sessionFilename.empty())
  415. return;
  416. // and a session file, so that kdevelop opens a file if it opens the project the first time
  417. cmGeneratedFileStream devses(sessionFilename.c_str());
  418. if(!devses)
  419. {
  420. return;
  421. }
  422. devses<<"<?xml version = '1.0' encoding = \'UTF-8\'?>\n";
  423. devses<<"<!DOCTYPE KDevPrjSession>\n";
  424. devses<<"<KDevPrjSession>\n";
  425. devses<<" <DocsAndViews NumberOfDocuments=\"1\" >\n";
  426. devses<<" <Doc0 NumberOfViews=\"1\" URL=\"file://"<<fileToOpen.c_str()<<"\" >\n";
  427. devses<<" <View0 line=\"0\" Type=\"Source\" />\n";
  428. devses<<" </Doc0>\n";
  429. devses<<" </DocsAndViews>\n";
  430. devses<<"</KDevPrjSession>\n";
  431. }