cmExtraKateGenerator.cxx 11 KB

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