cmGlobalKdevelopGenerator.cxx 19 KB

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