cmGlobalKdevelopGenerator.cxx 17 KB

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