cmGlobalKdevelopGenerator.cxx 18 KB

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