cmGlobalKdevelopGenerator.cxx 18 KB

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