cmGlobalKdevelopGenerator.cxx 17 KB

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