cmExtraKateGenerator.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2004-2009 Kitware, Inc.
  4. Copyright 2004 Alexander Neundorf ([email protected])
  5. Distributed under the OSI-approved BSD License (the "License");
  6. see accompanying file Copyright.txt for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even the
  8. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. See the License for more information.
  10. ============================================================================*/
  11. #include "cmExtraKateGenerator.h"
  12. #include "cmGlobalUnixMakefileGenerator3.h"
  13. #include "cmLocalUnixMakefileGenerator3.h"
  14. #include "cmMakefile.h"
  15. #include "cmake.h"
  16. #include "cmSourceFile.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include "cmTarget.h"
  19. #include "cmSystemTools.h"
  20. #include "cmXMLSafe.h"
  21. #include <cmsys/SystemTools.hxx>
  22. //----------------------------------------------------------------------------
  23. void cmExtraKateGenerator
  24. ::GetDocumentation(cmDocumentationEntry& entry, const std::string&) const
  25. {
  26. entry.Name = this->GetName();
  27. entry.Brief = "Generates Kate project files.";
  28. }
  29. cmExtraKateGenerator::cmExtraKateGenerator()
  30. :cmExternalMakefileProjectGenerator()
  31. {
  32. #if defined(_WIN32)
  33. this->SupportedGlobalGenerators.push_back("MinGW Makefiles");
  34. this->SupportedGlobalGenerators.push_back("NMake Makefiles");
  35. // disable until somebody actually tests it:
  36. // this->SupportedGlobalGenerators.push_back("MSYS Makefiles");
  37. #endif
  38. this->SupportedGlobalGenerators.push_back("Ninja");
  39. this->SupportedGlobalGenerators.push_back("Unix Makefiles");
  40. }
  41. void cmExtraKateGenerator::Generate()
  42. {
  43. const cmMakefile* mf
  44. = this->GlobalGenerator->GetLocalGenerators()[0]->GetMakefile();
  45. this->ProjectName = this->GenerateProjectName(mf->GetProjectName(),
  46. mf->GetSafeDefinition("CMAKE_BUILD_TYPE"),
  47. this->GetPathBasename(mf->GetHomeOutputDirectory()));
  48. this->UseNinja = (this->GlobalGenerator->GetName() == "Ninja");
  49. this->CreateKateProjectFile(mf);
  50. this->CreateDummyKateProjectFile(mf);
  51. }
  52. void cmExtraKateGenerator::CreateKateProjectFile(const cmMakefile* mf) const
  53. {
  54. std::string filename = mf->GetHomeOutputDirectory();
  55. filename += "/.kateproject";
  56. cmGeneratedFileStream fout(filename.c_str());
  57. if (!fout)
  58. {
  59. return;
  60. }
  61. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  62. std::string args = mf->GetSafeDefinition("CMAKE_KATE_MAKE_ARGUMENTS");
  63. fout <<
  64. "{\n"
  65. "\t\"name\": \"" << this->ProjectName << "\",\n"
  66. "\t\"directory\": \"" << mf->GetHomeDirectory() << "\",\n"
  67. "\t\"files\": [ { " << this->GenerateFilesString(mf) << "} ],\n";
  68. this->WriteTargets(mf, fout);
  69. fout << "}\n";
  70. }
  71. void
  72. cmExtraKateGenerator::WriteTargets(const cmMakefile* mf,
  73. cmGeneratedFileStream& fout) const
  74. {
  75. const std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  76. const std::string makeArgs = mf->GetSafeDefinition(
  77. "CMAKE_KATE_MAKE_ARGUMENTS");
  78. const char* homeOutputDir = mf->GetHomeOutputDirectory();
  79. fout <<
  80. "\t\"build\": {\n"
  81. "\t\t\"directory\": \"" << mf->GetHomeOutputDirectory() << "\",\n"
  82. "\t\t\"default_target\": \"all\",\n"
  83. "\t\t\"clean_target\": \"clean\",\n";
  84. // build, clean and quick are for the build plugin kate <= 4.12:
  85. fout << "\t\t\"build\": \"" << make << " -C \\\"" << homeOutputDir
  86. << "\\\" " << makeArgs << " " << "all\",\n";
  87. fout << "\t\t\"clean\": \"" << make << " -C \\\"" << homeOutputDir
  88. << "\\\" " << makeArgs << " " << "clean\",\n";
  89. fout << "\t\t\"quick\": \"" << make << " -C \\\"" << homeOutputDir
  90. << "\\\" " << makeArgs << " " << "install\",\n";
  91. // this is for kate >= 4.13:
  92. fout <<
  93. "\t\t\"targets\":[\n";
  94. this->AppendTarget(fout, "all", make, makeArgs,
  95. homeOutputDir, homeOutputDir);
  96. this->AppendTarget(fout, "clean", make, makeArgs,
  97. homeOutputDir, homeOutputDir);
  98. // add all executable and library targets and some of the GLOBAL
  99. // and UTILITY targets
  100. for (std::vector<cmLocalGenerator*>::const_iterator
  101. it = this->GlobalGenerator->GetLocalGenerators().begin();
  102. it != this->GlobalGenerator->GetLocalGenerators().end();
  103. ++it)
  104. {
  105. const cmTargets& targets = (*it)->GetMakefile()->GetTargets();
  106. cmMakefile* makefile=(*it)->GetMakefile();
  107. std::string currentDir = makefile->GetCurrentOutputDirectory();
  108. bool topLevel = (currentDir == makefile->GetHomeOutputDirectory());
  109. for(cmTargets::const_iterator ti=targets.begin(); ti!=targets.end(); ++ti)
  110. {
  111. switch(ti->second.GetType())
  112. {
  113. case cmTarget::GLOBAL_TARGET:
  114. {
  115. bool insertTarget = false;
  116. // Only add the global targets from CMAKE_BINARY_DIR,
  117. // not from the subdirs
  118. if (topLevel)
  119. {
  120. insertTarget = true;
  121. // only add the "edit_cache" target if it's not ccmake, because
  122. // this will not work within the IDE
  123. if (ti->first == "edit_cache")
  124. {
  125. const char* editCommand = makefile->GetDefinition
  126. ("CMAKE_EDIT_COMMAND");
  127. if (editCommand == 0)
  128. {
  129. insertTarget = false;
  130. }
  131. else if (strstr(editCommand, "ccmake")!=NULL)
  132. {
  133. insertTarget = false;
  134. }
  135. }
  136. }
  137. if (insertTarget)
  138. {
  139. this->AppendTarget(fout, ti->first, make, makeArgs,
  140. currentDir, homeOutputDir);
  141. }
  142. }
  143. break;
  144. case cmTarget::UTILITY:
  145. // Add all utility targets, except the Nightly/Continuous/
  146. // Experimental-"sub"targets as e.g. NightlyStart
  147. if (((ti->first.find("Nightly")==0) &&(ti->first!="Nightly"))
  148. || ((ti->first.find("Continuous")==0)&&(ti->first!="Continuous"))
  149. || ((ti->first.find("Experimental")==0)
  150. && (ti->first!="Experimental")))
  151. {
  152. break;
  153. }
  154. this->AppendTarget(fout, ti->first, make, makeArgs,
  155. currentDir, homeOutputDir);
  156. break;
  157. case cmTarget::EXECUTABLE:
  158. case cmTarget::STATIC_LIBRARY:
  159. case cmTarget::SHARED_LIBRARY:
  160. case cmTarget::MODULE_LIBRARY:
  161. case cmTarget::OBJECT_LIBRARY:
  162. {
  163. this->AppendTarget(fout, ti->first, make, makeArgs,
  164. currentDir, homeOutputDir);
  165. std::string fastTarget = ti->first;
  166. fastTarget += "/fast";
  167. this->AppendTarget(fout, fastTarget, make, makeArgs,
  168. currentDir, homeOutputDir);
  169. }
  170. break;
  171. default:
  172. break;
  173. }
  174. }
  175. //insert rules for compiling, preprocessing and assembling individual files
  176. std::vector<std::string> objectFileTargets;
  177. (*it)->GetIndividualFileTargets(objectFileTargets);
  178. for(std::vector<std::string>::const_iterator fit=objectFileTargets.begin();
  179. fit != objectFileTargets.end();
  180. ++fit)
  181. {
  182. this->AppendTarget(fout, *fit, make, makeArgs, currentDir,homeOutputDir);
  183. }
  184. }
  185. fout <<
  186. "\t] }\n";
  187. }
  188. void
  189. cmExtraKateGenerator::AppendTarget(cmGeneratedFileStream& fout,
  190. const std::string& target,
  191. const std::string& make,
  192. const std::string& makeArgs,
  193. const std::string& path,
  194. const char* homeOutputDir
  195. ) const
  196. {
  197. static char JsonSep = ' ';
  198. fout <<
  199. "\t\t\t" << JsonSep << "{\"name\":\"" << target << "\", "
  200. "\"build_cmd\":\"" << make
  201. << " -C \\\"" << (this->UseNinja ? homeOutputDir : path.c_str())
  202. << "\\\" " << makeArgs << " "
  203. << target << "\"}\n";
  204. JsonSep = ',';
  205. }
  206. void
  207. cmExtraKateGenerator::CreateDummyKateProjectFile(const cmMakefile* mf) const
  208. {
  209. std::string filename = mf->GetHomeOutputDirectory();
  210. filename += "/";
  211. filename += this->ProjectName;
  212. filename += ".kateproject";
  213. cmGeneratedFileStream fout(filename.c_str());
  214. if (!fout)
  215. {
  216. return;
  217. }
  218. fout << "#Generated by " << mf->GetRequiredDefinition("CMAKE_COMMAND")
  219. << ", do not edit.\n";
  220. }
  221. std::string
  222. cmExtraKateGenerator::GenerateFilesString(const cmMakefile* mf) const
  223. {
  224. std::string s = mf->GetHomeDirectory();
  225. s += "/.git";
  226. if(cmSystemTools::FileExists(s.c_str()))
  227. {
  228. return std::string("\"git\": 1 ");
  229. }
  230. s = mf->GetHomeDirectory();
  231. s += "/.svn";
  232. if(cmSystemTools::FileExists(s.c_str()))
  233. {
  234. return std::string("\"svn\": 1 ");
  235. }
  236. s = mf->GetHomeDirectory();
  237. s += "/";
  238. std::set<std::string> files;
  239. std::string tmp;
  240. const std::vector<cmLocalGenerator *>& lgs =
  241. this->GlobalGenerator->GetLocalGenerators();
  242. for (std::vector<cmLocalGenerator*>::const_iterator it=lgs.begin();
  243. it!=lgs.end(); it++)
  244. {
  245. cmMakefile* makefile=(*it)->GetMakefile();
  246. const std::vector<std::string>& listFiles=makefile->GetListFiles();
  247. for (std::vector<std::string>::const_iterator lt=listFiles.begin();
  248. lt!=listFiles.end(); lt++)
  249. {
  250. tmp=*lt;
  251. {
  252. files.insert(tmp);
  253. }
  254. }
  255. const std::vector<cmSourceFile*>& sources = makefile->GetSourceFiles();
  256. for (std::vector<cmSourceFile*>::const_iterator sfIt = sources.begin();
  257. sfIt != sources.end(); sfIt++)
  258. {
  259. cmSourceFile* sf = *sfIt;
  260. if (sf->GetPropertyAsBool("GENERATED"))
  261. {
  262. continue;
  263. }
  264. tmp = sf->GetFullPath();
  265. files.insert(tmp);
  266. }
  267. }
  268. const char* sep = "";
  269. tmp = "\"list\": [";
  270. for(std::set<std::string>::const_iterator it = files.begin();
  271. it != files.end(); ++it)
  272. {
  273. tmp += sep;
  274. tmp += " \"";
  275. tmp += *it;
  276. tmp += "\"";
  277. sep = ",";
  278. }
  279. tmp += "] ";
  280. return tmp;
  281. }
  282. std::string cmExtraKateGenerator::GenerateProjectName(const std::string& name,
  283. const std::string& type,
  284. const std::string& path) const
  285. {
  286. return name + (type.empty() ? "" : "-") + type + "@" + path;
  287. }
  288. std::string cmExtraKateGenerator::GetPathBasename(const std::string& path)const
  289. {
  290. std::string outputBasename = path;
  291. while (!outputBasename.empty() &&
  292. (outputBasename[outputBasename.size() - 1] == '/' ||
  293. outputBasename[outputBasename.size() - 1] == '\\'))
  294. {
  295. outputBasename.resize(outputBasename.size() - 1);
  296. }
  297. std::string::size_type loc = outputBasename.find_last_of("/\\");
  298. if (loc != std::string::npos)
  299. {
  300. outputBasename = outputBasename.substr(loc + 1);
  301. }
  302. return outputBasename;
  303. }