cmGlobalKdevelopGenerator.cxx 17 KB

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