cmLocalKdevelopGenerator.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 "cmGlobalGenerator.h"
  15. #include "cmLocalKdevelopGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmSystemTools.h"
  18. #include "cmSourceFile.h"
  19. #include "cmMakeDepend.h"
  20. #include "cmCacheManager.h"
  21. #include "cmGeneratedFileStream.h"
  22. #include "cmake.h"
  23. #include <cmsys/RegularExpression.hxx>
  24. cmLocalKdevelopGenerator::cmLocalKdevelopGenerator()
  25. :cmLocalUnixMakefileGenerator()
  26. {
  27. }
  28. cmLocalKdevelopGenerator::~cmLocalKdevelopGenerator()
  29. {
  30. }
  31. void cmLocalKdevelopGenerator::Generate(bool fromTheTop)
  32. {
  33. cmLocalUnixMakefileGenerator::Generate(fromTheTop);
  34. if ( m_GlobalGenerator->GetCMakeInstance()->GetLocal() )
  35. {
  36. return;
  37. }
  38. bool containsTargets=false;
  39. std::string executable;
  40. cmTargets& targets=m_Makefile->GetTargets();
  41. for (cmTargets::const_iterator ti = targets.begin(); ti != targets.end(); ti++)
  42. {
  43. switch (ti->second.GetType())
  44. {
  45. case cmTarget::EXECUTABLE:
  46. executable=ti->first;
  47. case cmTarget::STATIC_LIBRARY:
  48. case cmTarget::SHARED_LIBRARY:
  49. case cmTarget::MODULE_LIBRARY:
  50. case cmTarget::UTILITY:
  51. containsTargets=true;
  52. break;
  53. case cmTarget::INSTALL_FILES:
  54. case cmTarget::INSTALL_PROGRAMS:
  55. default:
  56. break;
  57. }
  58. }
  59. if (containsTargets)
  60. {
  61. std::string filelistDir=m_Makefile->GetHomeOutputDirectory();
  62. //build the project name by taking the subdir
  63. std::vector<cmLocalGenerator *> lgs;
  64. m_GlobalGenerator->GetLocalGenerators(lgs);
  65. std::string projectName=lgs[0]->GetMakefile()->GetProjectName();
  66. std::string cmakeFilePattern("CMakeLists.txt;*.cmake;");
  67. if (!this->CreateFilelistFile(filelistDir, projectName, cmakeFilePattern))
  68. {
  69. return;
  70. }
  71. this->CreateProjectFile(filelistDir, projectName, executable, cmakeFilePattern);
  72. }
  73. }
  74. void cmLocalKdevelopGenerator::CreateProjectFile(const std::string& dir,
  75. const std::string& projectname,
  76. const std::string& executable,
  77. const std::string& cmakeFilePattern)
  78. {
  79. std::string filename=m_Makefile->GetStartOutputDirectory();
  80. filename+="/";
  81. filename+=projectname+".kdevelop";
  82. if (cmSystemTools::FileExists(filename.c_str()))
  83. {
  84. this->MergeProjectFiles(dir, filename, executable, cmakeFilePattern);
  85. }
  86. else
  87. {
  88. this->CreateNewProjectFile(dir, filename, executable, cmakeFilePattern);
  89. }
  90. }
  91. void cmLocalKdevelopGenerator::MergeProjectFiles(const std::string& dir,
  92. const std::string& filename,
  93. const std::string& executable,
  94. const std::string& cmakeFilePattern)
  95. {
  96. std::ifstream oldProjectFile(filename.c_str());
  97. if (!oldProjectFile)
  98. {
  99. this->CreateNewProjectFile(dir, filename, executable, cmakeFilePattern);
  100. return;
  101. }
  102. std::string tmp;
  103. std::vector<std::string> lines;
  104. while (cmSystemTools::GetLineFromStream(oldProjectFile, tmp))
  105. {
  106. lines.push_back(tmp);
  107. }
  108. oldProjectFile.close();
  109. cmGeneratedFileStream tempFile(filename.c_str());
  110. tempFile.SetAlwaysCopy(true);
  111. std::ostream& fout = tempFile.GetStream();
  112. if(!fout)
  113. {
  114. cmSystemTools::Error("Error can not open for write: ", filename.c_str());
  115. return;
  116. }
  117. for (std::vector<std::string>::const_iterator it=lines.begin();
  118. it!=lines.end(); it++)
  119. {
  120. const char* line=(*it).c_str();
  121. if ((strstr(line, "<projectdirectory>")!=0)
  122. || (strstr(line, "<projectmanagement>")!=0)
  123. || (strstr(line, "<absoluteprojectpath>")!=0)
  124. || (strstr(line, "<buildtool>")!=0)
  125. || (strstr(line, "<builddir>")!=0))
  126. {
  127. continue;
  128. }
  129. fout<<*it<<"\n";
  130. if (strstr(line, "<general>"))
  131. {
  132. fout<<" <projectmanagement>KDevCustomProject</projectmanagement>\n";
  133. fout<<" <projectdirectory>"<<dir.c_str()<<"</projectdirectory>\n"; //this one is important
  134. fout<<" <absoluteprojectpath>true</absoluteprojectpath>\n"; //and this one
  135. }
  136. if (strstr(line, "<build>"))
  137. {
  138. fout<<" <buildtool>make</buildtool>\n"; //this one is important
  139. fout<<" <builddir>"<<m_Makefile->GetStartOutputDirectory()<<"</builddir>\n"; //and this one
  140. }
  141. }
  142. }
  143. void cmLocalKdevelopGenerator::CreateNewProjectFile(const std::string& dir, const std::string& filename,
  144. const std::string& executable, const std::string& cmakeFilePattern)
  145. {
  146. cmGeneratedFileStream tempFile(filename.c_str());
  147. tempFile.SetAlwaysCopy(true);
  148. std::ostream& fout = tempFile.GetStream();
  149. if(!fout)
  150. {
  151. cmSystemTools::Error("Error can not open for write: ", filename.c_str());
  152. return;
  153. }
  154. fout<<"<?xml version = '1.0'?>\n";
  155. fout<<"<kdevelop>\n";
  156. fout<<" <general>\n";
  157. fout<<" <author></author>\n";
  158. fout<<" <email></email>\n";
  159. fout<<" <version>$VERSION$</version>\n";
  160. fout<<" <projectmanagement>KDevCustomProject</projectmanagement>\n";
  161. fout<<" <primarylanguage>C++</primarylanguage>\n";
  162. fout<<" <ignoreparts/>\n";
  163. fout<<" <projectdirectory>"<<dir.c_str()<<"</projectdirectory>\n"; //this one is important
  164. fout<<" <absoluteprojectpath>true</absoluteprojectpath>\n"; //and this one
  165. fout<<" <secondaryLanguages>\n";
  166. fout<<" <language>C</language>\n";
  167. fout<<" </secondaryLanguages>\n";
  168. fout<<" </general>\n";
  169. fout<<" <kdevcustomproject>\n";
  170. fout<<" <run>\n";
  171. fout<<" <mainprogram>"<<m_Makefile->GetStartOutputDirectory()<<"/"<<executable.c_str()<<"</mainprogram>\n";
  172. fout<<" <directoryradio>custom</directoryradio>\n";
  173. fout<<" <customdirectory>/</customdirectory>\n";
  174. fout<<" <programargs></programargs>\n";
  175. fout<<" <terminal>false</terminal>\n";
  176. fout<<" <autocompile>true</autocompile>\n";
  177. fout<<" <envvars/>\n";
  178. fout<<" </run>\n";
  179. fout<<" <build>\n";
  180. fout<<" <buildtool>make</buildtool>\n"; //this one is important
  181. fout<<" <builddir>"<<m_Makefile->GetStartOutputDirectory()<<"</builddir>\n"; //and this one
  182. fout<<" </build>\n";
  183. fout<<" <make>\n";
  184. fout<<" <abortonerror>false</abortonerror>\n";
  185. fout<<" <numberofjobs>1</numberofjobs>\n";
  186. fout<<" <dontact>false</dontact>\n";
  187. fout<<" <makebin></makebin>\n";
  188. fout<<" <selectedenvironment>default</selectedenvironment>\n";
  189. fout<<" <environments>\n";
  190. fout<<" <default/>\n";
  191. fout<<" </environments>\n";
  192. fout<<" </make>\n";
  193. fout<<" </kdevcustomproject>\n";
  194. fout<<" <kdevfilecreate>\n";
  195. fout<<" <filetypes/>\n";
  196. fout<<" <useglobaltypes>\n";
  197. fout<<" <type ext=\"ui\" />\n";
  198. fout<<" <type ext=\"cpp\" />\n";
  199. fout<<" <type ext=\"h\" />\n";
  200. fout<<" </useglobaltypes>\n";
  201. fout<<" </kdevfilecreate>\n";
  202. fout<<" <kdevdoctreeview>\n";
  203. fout<<" <projectdoc>\n";
  204. fout<<" <userdocDir>html/</userdocDir>\n";
  205. fout<<" <apidocDir>html/</apidocDir>\n";
  206. fout<<" </projectdoc>\n";
  207. fout<<" <ignoreqt_xml/>\n";
  208. fout<<" <ignoredoxygen/>\n";
  209. fout<<" <ignorekdocs/>\n";
  210. fout<<" <ignoretocs/>\n";
  211. fout<<" <ignoredevhelp/>\n";
  212. fout<<" </kdevdoctreeview>\n";
  213. fout<<" <cppsupportpart>\n";
  214. fout<<" <filetemplates>\n";
  215. fout<<" <interfacesuffix>.h</interfacesuffix>\n";
  216. fout<<" <implementationsuffix>.cpp</implementationsuffix>\n";
  217. fout<<" </filetemplates>\n";
  218. fout<<" </cppsupportpart>\n";
  219. fout<<" <kdevcppsupport>\n";
  220. fout<<" <codecompletion>\n";
  221. fout<<" <includeGlobalFunctions>true</includeGlobalFunctions>\n";
  222. fout<<" <includeTypes>true</includeTypes>\n";
  223. fout<<" <includeEnums>true</includeEnums>\n";
  224. fout<<" <includeTypedefs>false</includeTypedefs>\n";
  225. fout<<" <automaticCodeCompletion>true</automaticCodeCompletion>\n";
  226. fout<<" <automaticArgumentsHint>true</automaticArgumentsHint>\n";
  227. fout<<" <automaticHeaderCompletion>true</automaticHeaderCompletion>\n";
  228. fout<<" <codeCompletionDelay>250</codeCompletionDelay>\n";
  229. fout<<" <argumentsHintDelay>400</argumentsHintDelay>\n";
  230. fout<<" <headerCompletionDelay>250</headerCompletionDelay>\n";
  231. fout<<" </codecompletion>\n";
  232. fout<<" <references/>\n";
  233. fout<<" </kdevcppsupport>\n";
  234. fout<<" <kdevfileview>\n";
  235. fout<<" <groups>\n";
  236. fout<<" <group pattern=\""<<cmakeFilePattern.c_str()<<"\" name=\"CMake\" />\n";
  237. fout<<" <group pattern=\"*.h;*.hxx\" name=\"Header\" />\n";
  238. fout<<" <group pattern=\"*.cpp;*.c;*.C;*.cxx\" name=\"Sources\" />\n";
  239. fout<<" <group pattern=\"*.ui\" name=\"Qt Designer files\" />\n";
  240. fout<<" <hidenonprojectfiles>true</hidenonprojectfiles>\n";
  241. fout<<" </groups>\n";
  242. fout<<" <tree>\n";
  243. fout<<" <hidepatterns>*.o,*.lo,CVS,*~,cmake*</hidepatterns>\n";
  244. fout<<" <hidenonprojectfiles>true</hidenonprojectfiles>\n";
  245. fout<<" </tree>\n";
  246. fout<<" </kdevfileview>\n";
  247. fout<<"</kdevelop>\n";
  248. }
  249. bool cmLocalKdevelopGenerator::CreateFilelistFile(const std::string& _dir,
  250. const std::string& projectname,
  251. std::string& cmakeFilePattern)
  252. {
  253. std::string filelistDir=_dir+"/";
  254. std::string filename=filelistDir+projectname+".kdevelop.filelist";
  255. std::set<cmStdString> files;
  256. //get all cmake files
  257. std::string tmp;
  258. const std::vector<std::string>& listFiles=m_Makefile->GetListFiles();
  259. for (std::vector<std::string>::const_iterator it=listFiles.begin(); it!=listFiles.end(); it++)
  260. {
  261. tmp=*it;
  262. cmSystemTools::ReplaceString(tmp, filelistDir.c_str(), "");
  263. if (tmp[0]!='/')
  264. {
  265. files.insert(tmp);
  266. tmp=cmSystemTools::GetFilenameName(tmp);
  267. //add all files which dont match the default */CMakeLists.txt;*cmake; to the file pattern
  268. if ((tmp!="CMakeLists.txt")
  269. && (strstr(tmp.c_str(), ".cmake")==0))
  270. {
  271. cmakeFilePattern+=tmp+";";
  272. }
  273. }
  274. }
  275. //get all sources
  276. cmTargets& targets=m_Makefile->GetTargets();
  277. for (cmTargets::const_iterator ti = targets.begin(); ti != targets.end(); ti++)
  278. {
  279. const std::vector<cmSourceFile*>& sources=ti->second.GetSourceFiles();
  280. for (std::vector<cmSourceFile*>::const_iterator it=sources.begin();
  281. it!=sources.end(); it++)
  282. {
  283. files.insert((*it)->GetFullPath());
  284. }
  285. for (std::vector<std::string>::const_iterator it=listFiles.begin();
  286. it!=listFiles.end(); it++)
  287. {
  288. files.insert(it->c_str());
  289. }
  290. }
  291. //check if the output file already exists and read it
  292. //insert all files which exist into the set of files
  293. std::ifstream oldFilelist(filename.c_str());
  294. if (oldFilelist)
  295. {
  296. while (cmSystemTools::GetLineFromStream(oldFilelist, tmp))
  297. {
  298. if (tmp[0]=='/')
  299. {
  300. continue;
  301. }
  302. std::string completePath=filelistDir+tmp;
  303. if (cmSystemTools::FileExists(completePath.c_str()))
  304. {
  305. files.insert(tmp);
  306. }
  307. }
  308. oldFilelist.close();
  309. }
  310. cmGeneratedFileStream tempFile(filename.c_str());
  311. tempFile.SetAlwaysCopy(true);
  312. std::ostream& fout = tempFile.GetStream();
  313. if(!fout)
  314. {
  315. cmSystemTools::Error("Error can not open for write: ", filename.c_str());
  316. return false;
  317. }
  318. for (std::set<cmStdString>::const_iterator it=files.begin(); it!=files.end(); it++)
  319. {
  320. fout<< cmSystemTools::RelativePath(_dir.c_str(), it->c_str())<<"\n";
  321. }
  322. return true;
  323. }