cmGlobalKdevelopGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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->ToolSupportsColorVT100 = 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 it=lgs.begin();
  75. it!=lgs.end(); it++)
  76. {
  77. cmMakefile* makefile=(*it)->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. break;
  90. }
  91. // now create a project file
  92. this->CreateProjectFile(outputDir, projectDir, projectName,
  93. executable, cmakeFilePattern, fileToOpen);
  94. }
  95. }
  96. bool cmGlobalKdevelopGenerator
  97. ::CreateFilelistFile(cmLocalGenerator* ,
  98. std::vector<cmLocalGenerator*>& lgs,
  99. const std::string& outputDir,
  100. const std::string& projectDirIn,
  101. const std::string& projectname,
  102. std::string& cmakeFilePattern,
  103. std::string& fileToOpen)
  104. {
  105. std::string projectDir = projectDirIn + "/";
  106. std::string filename = outputDir+ "/" + projectname +".kdevelop.filelist";
  107. std::set<cmStdString> files;
  108. std::string tmp;
  109. for (std::vector<cmLocalGenerator*>::const_iterator it=lgs.begin();
  110. it!=lgs.end(); it++)
  111. {
  112. cmMakefile* makefile=(*it)->GetMakefile();
  113. const std::vector<std::string>& listFiles=makefile->GetListFiles();
  114. for (std::vector<std::string>::const_iterator lt=listFiles.begin();
  115. lt!=listFiles.end(); lt++)
  116. {
  117. tmp=*lt;
  118. cmSystemTools::ReplaceString(tmp, projectDir.c_str(), "");
  119. // make sure the file is part of this source tree
  120. if ((tmp[0]!='/') && (strstr(tmp.c_str(), "CMakeFiles/")==0))
  121. {
  122. files.insert(tmp);
  123. tmp=cmSystemTools::GetFilenameName(tmp);
  124. //add all files which dont match the default
  125. // */CMakeLists.txt;*cmake; to the file pattern
  126. if ((tmp!="CMakeLists.txt")
  127. && (strstr(tmp.c_str(), ".cmake")==0))
  128. {
  129. cmakeFilePattern+=tmp+";";
  130. }
  131. }
  132. }
  133. //get all sources
  134. cmTargets& targets=makefile->GetTargets();
  135. for (cmTargets::iterator ti = targets.begin();
  136. ti != targets.end(); ti++)
  137. {
  138. const std::vector<cmSourceFile*>& sources=ti->second.GetSourceFiles();
  139. for (std::vector<cmSourceFile*>::const_iterator si=sources.begin();
  140. si!=sources.end(); si++)
  141. {
  142. tmp=(*si)->GetFullPath();
  143. cmSystemTools::ReplaceString(tmp, projectDir.c_str(), "");
  144. if ((tmp[0]!='/') && (strstr(tmp.c_str(), "CMakeFiles/")==0))
  145. {
  146. files.insert(tmp);
  147. }
  148. }
  149. for (std::vector<std::string>::const_iterator lt=listFiles.begin();
  150. lt!=listFiles.end(); lt++)
  151. {
  152. tmp=*lt;
  153. cmSystemTools::ReplaceString(tmp, projectDir.c_str(), "");
  154. if ((tmp[0]!='/') && (strstr(tmp.c_str(), "CMakeFiles/")==0))
  155. {
  156. files.insert(tmp.c_str());
  157. }
  158. }
  159. }
  160. }
  161. //check if the output file already exists and read it
  162. //insert all files which exist into the set of files
  163. std::ifstream oldFilelist(filename.c_str());
  164. if (oldFilelist)
  165. {
  166. while (cmSystemTools::GetLineFromStream(oldFilelist, tmp))
  167. {
  168. if (tmp[0]=='/')
  169. {
  170. continue;
  171. }
  172. std::string completePath=projectDir+tmp;
  173. if (cmSystemTools::FileExists(completePath.c_str()))
  174. {
  175. files.insert(tmp);
  176. }
  177. }
  178. oldFilelist.close();
  179. }
  180. //now write the new filename
  181. cmGeneratedFileStream fout(filename.c_str());
  182. if(!fout)
  183. {
  184. return false;
  185. }
  186. fileToOpen="";
  187. for (std::set<cmStdString>::const_iterator it=files.begin();
  188. it!=files.end(); it++)
  189. {
  190. // get the full path to the file
  191. tmp=cmSystemTools::CollapseFullPath(it->c_str(), projectDir.c_str());
  192. // just select the first source file
  193. if (fileToOpen.empty())
  194. {
  195. std::string ext = cmSystemTools::GetFilenameExtension(tmp);
  196. if ((ext==".c") || (ext==".cc") || (ext==".cpp") || (ext==".C") || (ext==".h"))
  197. {
  198. fileToOpen=tmp;
  199. }
  200. }
  201. // make it relative to the project dir
  202. cmSystemTools::ReplaceString(tmp, projectDir.c_str(), "");
  203. // only put relative paths
  204. if (tmp.size() && tmp[0] != '/')
  205. {
  206. fout << tmp.c_str() <<"\n";
  207. }
  208. }
  209. return true;
  210. }
  211. /* create the project file, if it already exists, merge it with the
  212. existing one, otherwise create a new one */
  213. void
  214. cmGlobalKdevelopGenerator::CreateProjectFile(const std::string& outputDir,
  215. const std::string& projectDir,
  216. const std::string& projectname,
  217. const std::string& executable,
  218. const std::string& cmakeFilePattern,
  219. const std::string& fileToOpen)
  220. {
  221. std::string filename=outputDir+"/";
  222. filename+=projectname+".kdevelop";
  223. std::string sessionFilename=outputDir+"/";
  224. sessionFilename+=projectname+".kdevses";
  225. if (cmSystemTools::FileExists(filename.c_str()))
  226. {
  227. this->MergeProjectFiles(outputDir, projectDir, filename,
  228. executable, cmakeFilePattern, fileToOpen, sessionFilename);
  229. }
  230. else
  231. {
  232. this->CreateNewProjectFile(outputDir, projectDir, filename,
  233. executable, cmakeFilePattern, fileToOpen, sessionFilename);
  234. }
  235. }
  236. void
  237. cmGlobalKdevelopGenerator::MergeProjectFiles(const std::string& outputDir,
  238. const std::string& projectDir,
  239. const std::string& filename,
  240. const std::string& executable,
  241. const std::string& cmakeFilePattern,
  242. const std::string& fileToOpen,
  243. const std::string& sessionFilename)
  244. {
  245. std::ifstream oldProjectFile(filename.c_str());
  246. if (!oldProjectFile)
  247. {
  248. this->CreateNewProjectFile(outputDir, projectDir, filename, executable, cmakeFilePattern, fileToOpen, sessionFilename);
  249. return;
  250. }
  251. /* Read the existing project file (line by line), copy all lines
  252. into the new project file, except the ones which can be reliably
  253. set from contents of the CMakeLists.txt */
  254. std::string tmp;
  255. std::vector<std::string> lines;
  256. while (cmSystemTools::GetLineFromStream(oldProjectFile, tmp))
  257. {
  258. lines.push_back(tmp);
  259. }
  260. oldProjectFile.close();
  261. cmGeneratedFileStream fout(filename.c_str());
  262. if(!fout)
  263. {
  264. return;
  265. }
  266. for (std::vector<std::string>::const_iterator it=lines.begin();
  267. it!=lines.end(); it++)
  268. {
  269. const char* line=(*it).c_str();
  270. // skip these tags as they are always replaced
  271. if ((strstr(line, "<projectdirectory>")!=0)
  272. || (strstr(line, "<projectmanagement>")!=0)
  273. || (strstr(line, "<absoluteprojectpath>")!=0)
  274. || (strstr(line, "<filelistdirectory>")!=0)
  275. || (strstr(line, "<buildtool>")!=0)
  276. || (strstr(line, "<builddir>")!=0))
  277. {
  278. continue;
  279. }
  280. // output the line from the file if it is not one of the above tags
  281. fout<<*it<<"\n";
  282. // if this is the <general> tag output the stuff that goes in the general tag
  283. if (strstr(line, "<general>"))
  284. {
  285. fout<<" <projectmanagement>KDevCustomProject</projectmanagement>\n";
  286. fout<<" <projectdirectory>"<<projectDir.c_str()<<"</projectdirectory>\n"; //this one is important
  287. fout<<" <absoluteprojectpath>true</absoluteprojectpath>\n"; //and this one
  288. }
  289. // inside kdevcustomproject the <filelistdirectory> must be put
  290. if (strstr(line, "<kdevcustomproject>"))
  291. {
  292. fout<<" <filelistdirectory>"<<outputDir.c_str()
  293. <<"</filelistdirectory>\n";
  294. }
  295. // buildtool and builddir go inside <build>
  296. if (strstr(line, "<build>"))
  297. {
  298. fout<<" <buildtool>make</buildtool>\n";
  299. fout<<" <builddir>"<<outputDir.c_str()<<"</builddir>\n";
  300. }
  301. }
  302. }
  303. void
  304. cmGlobalKdevelopGenerator::CreateNewProjectFile(const std::string& outputDir,
  305. const std::string& projectDir,
  306. const std::string& filename,
  307. const std::string& executable,
  308. const std::string& cmakeFilePattern,
  309. const std::string& fileToOpen,
  310. const std::string& sessionFilename)
  311. {
  312. cmGeneratedFileStream fout(filename.c_str());
  313. if(!fout)
  314. {
  315. return;
  316. }
  317. fout<<"<?xml version = '1.0'?>\n";
  318. fout<<"<kdevelop>\n";
  319. fout<<" <general>\n";
  320. fout<<" <author></author>\n";
  321. fout<<" <email></email>\n";
  322. fout<<" <version>$VERSION$</version>\n";
  323. fout<<" <projectmanagement>KDevCustomProject</projectmanagement>\n";
  324. fout<<" <primarylanguage>C++</primarylanguage>\n";
  325. fout<<" <ignoreparts/>\n";
  326. fout<<" <projectdirectory>"<<projectDir.c_str()<<"</projectdirectory>\n"; //this one is important
  327. fout<<" <absoluteprojectpath>true</absoluteprojectpath>\n"; //and this one
  328. fout<<" <secondaryLanguages>\n";
  329. fout<<" <language>C</language>\n";
  330. fout<<" </secondaryLanguages>\n";
  331. fout<<" </general>\n";
  332. fout<<" <kdevcustomproject>\n";
  333. fout<<" <filelistdirectory>"<<outputDir.c_str()<<"</filelistdirectory>\n";
  334. fout<<" <run>\n";
  335. fout<<" <mainprogram>"<<executable.c_str()<<"</mainprogram>\n";
  336. fout<<" <directoryradio>custom</directoryradio>\n";
  337. fout<<" <customdirectory>/</customdirectory>\n";
  338. fout<<" <programargs></programargs>\n";
  339. fout<<" <terminal>false</terminal>\n";
  340. fout<<" <autocompile>true</autocompile>\n";
  341. fout<<" <envvars/>\n";
  342. fout<<" </run>\n";
  343. fout<<" <build>\n";
  344. fout<<" <buildtool>make</buildtool>\n"; //this one is important
  345. fout<<" <builddir>"<<outputDir.c_str()<<"</builddir>\n"; //and this one
  346. fout<<" </build>\n";
  347. fout<<" <make>\n";
  348. fout<<" <abortonerror>false</abortonerror>\n";
  349. fout<<" <numberofjobs>1</numberofjobs>\n";
  350. fout<<" <dontact>false</dontact>\n";
  351. fout<<" <makebin></makebin>\n";
  352. fout<<" <selectedenvironment>default</selectedenvironment>\n";
  353. fout<<" <environments>\n";
  354. fout<<" <default/>\n";
  355. fout<<" </environments>\n";
  356. fout<<" </make>\n";
  357. fout<<" </kdevcustomproject>\n";
  358. fout<<" <kdevfilecreate>\n";
  359. fout<<" <filetypes/>\n";
  360. fout<<" <useglobaltypes>\n";
  361. fout<<" <type ext=\"ui\" />\n";
  362. fout<<" <type ext=\"cpp\" />\n";
  363. fout<<" <type ext=\"h\" />\n";
  364. fout<<" </useglobaltypes>\n";
  365. fout<<" </kdevfilecreate>\n";
  366. fout<<" <kdevdoctreeview>\n";
  367. fout<<" <projectdoc>\n";
  368. fout<<" <userdocDir>html/</userdocDir>\n";
  369. fout<<" <apidocDir>html/</apidocDir>\n";
  370. fout<<" </projectdoc>\n";
  371. fout<<" <ignoreqt_xml/>\n";
  372. fout<<" <ignoredoxygen/>\n";
  373. fout<<" <ignorekdocs/>\n";
  374. fout<<" <ignoretocs/>\n";
  375. fout<<" <ignoredevhelp/>\n";
  376. fout<<" </kdevdoctreeview>\n";
  377. fout<<" <cppsupportpart>\n";
  378. fout<<" <filetemplates>\n";
  379. fout<<" <interfacesuffix>.h</interfacesuffix>\n";
  380. fout<<" <implementationsuffix>.cpp</implementationsuffix>\n";
  381. fout<<" </filetemplates>\n";
  382. fout<<" </cppsupportpart>\n";
  383. fout<<" <kdevcppsupport>\n";
  384. fout<<" <codecompletion>\n";
  385. fout<<" <includeGlobalFunctions>true</includeGlobalFunctions>\n";
  386. fout<<" <includeTypes>true</includeTypes>\n";
  387. fout<<" <includeEnums>true</includeEnums>\n";
  388. fout<<" <includeTypedefs>false</includeTypedefs>\n";
  389. fout<<" <automaticCodeCompletion>true</automaticCodeCompletion>\n";
  390. fout<<" <automaticArgumentsHint>true</automaticArgumentsHint>\n";
  391. fout<<" <automaticHeaderCompletion>true</automaticHeaderCompletion>\n";
  392. fout<<" <codeCompletionDelay>250</codeCompletionDelay>\n";
  393. fout<<" <argumentsHintDelay>400</argumentsHintDelay>\n";
  394. fout<<" <headerCompletionDelay>250</headerCompletionDelay>\n";
  395. fout<<" </codecompletion>\n";
  396. fout<<" <references/>\n";
  397. fout<<" </kdevcppsupport>\n";
  398. fout<<" <kdevfileview>\n";
  399. fout<<" <groups>\n";
  400. fout<<" <group pattern=\""<<cmakeFilePattern.c_str()<<"\" name=\"CMake\" />\n";
  401. fout<<" <group pattern=\"*.h;*.hxx\" name=\"Header\" />\n";
  402. fout<<" <group pattern=\"*.cpp;*.c;*.C;*.cxx\" name=\"Sources\" />\n";
  403. fout<<" <group pattern=\"*.ui\" name=\"Qt Designer files\" />\n";
  404. fout<<" <hidenonprojectfiles>true</hidenonprojectfiles>\n";
  405. fout<<" </groups>\n";
  406. fout<<" <tree>\n";
  407. fout<<" <hidepatterns>*.o,*.lo,CVS,*~,cmake*</hidepatterns>\n";
  408. fout<<" <hidenonprojectfiles>true</hidenonprojectfiles>\n";
  409. fout<<" </tree>\n";
  410. fout<<" </kdevfileview>\n";
  411. fout<<"</kdevelop>\n";
  412. if (sessionFilename.empty())
  413. return;
  414. // and a session file, so that kdevelop opens a file if it opens the project the first time
  415. cmGeneratedFileStream devses(sessionFilename.c_str());
  416. if(!devses)
  417. {
  418. return;
  419. }
  420. devses<<"<?xml version = '1.0' encoding = \'UTF-8\'?>\n";
  421. devses<<"<!DOCTYPE KDevPrjSession>\n";
  422. devses<<"<KDevPrjSession>\n";
  423. devses<<" <DocsAndViews NumberOfDocuments=\"1\" >\n";
  424. devses<<" <Doc0 NumberOfViews=\"1\" URL=\"file://"<<fileToOpen.c_str()<<"\" >\n";
  425. devses<<" <View0 line=\"0\" Type=\"Source\" />\n";
  426. devses<<" </Doc0>\n";
  427. devses<<" </DocsAndViews>\n";
  428. devses<<"</KDevPrjSession>\n";
  429. }