cmGlobalKdevelopGenerator.cxx 18 KB

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