cmGlobalKdevelopGenerator.cxx 15 KB

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