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. cmLocalGenerator* lg = this->GlobalGenerator->GetLocalGenerators()[0];
  43. const cmMakefile* mf = lg->GetMakefile();
  44. this->ProjectName = this->GenerateProjectName(lg->GetProjectName(),
  45. mf->GetSafeDefinition("CMAKE_BUILD_TYPE"),
  46. this->GetPathBasename(lg->GetBinaryDirectory()));
  47. this->UseNinja = (this->GlobalGenerator->GetName() == "Ninja");
  48. this->CreateKateProjectFile(lg);
  49. this->CreateDummyKateProjectFile(lg);
  50. }
  51. void cmExtraKateGenerator::CreateKateProjectFile(
  52. const cmLocalGenerator* lg) const
  53. {
  54. std::string filename = lg->GetBinaryDirectory();
  55. filename += "/.kateproject";
  56. cmGeneratedFileStream fout(filename.c_str());
  57. if (!fout)
  58. {
  59. return;
  60. }
  61. fout <<
  62. "{\n"
  63. "\t\"name\": \"" << this->ProjectName << "\",\n"
  64. "\t\"directory\": \"" << lg->GetSourceDirectory() << "\",\n"
  65. "\t\"files\": [ { " << this->GenerateFilesString(lg) << "} ],\n";
  66. this->WriteTargets(lg, fout);
  67. fout << "}\n";
  68. }
  69. void
  70. cmExtraKateGenerator::WriteTargets(const cmLocalGenerator* lg,
  71. cmGeneratedFileStream& fout) const
  72. {
  73. cmMakefile const* mf = lg->GetMakefile();
  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 = lg->GetBinaryDirectory();
  78. fout <<
  79. "\t\"build\": {\n"
  80. "\t\t\"directory\": \"" << lg->GetBinaryDirectory() << "\",\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 = (*it)->GetCurrentBinaryDirectory();
  107. bool topLevel = (currentDir == (*it)->GetBinaryDirectory());
  108. for(cmTargets::const_iterator ti=targets.begin(); ti!=targets.end(); ++ti)
  109. {
  110. switch(ti->second.GetType())
  111. {
  112. case cmState::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 cmState::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 cmState::EXECUTABLE:
  157. case cmState::STATIC_LIBRARY:
  158. case cmState::SHARED_LIBRARY:
  159. case cmState::MODULE_LIBRARY:
  160. case cmState::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(
  207. const cmLocalGenerator* lg) const
  208. {
  209. std::string filename = lg->GetBinaryDirectory();
  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 " << cmSystemTools::GetCMakeCommand()
  219. << ", do not edit.\n";
  220. }
  221. std::string
  222. cmExtraKateGenerator::GenerateFilesString(const cmLocalGenerator* lg) const
  223. {
  224. std::string s = lg->GetSourceDirectory();
  225. s += "/.git";
  226. if(cmSystemTools::FileExists(s.c_str()))
  227. {
  228. return std::string("\"git\": 1 ");
  229. }
  230. s = lg->GetSourceDirectory();
  231. s += "/.svn";
  232. if(cmSystemTools::FileExists(s.c_str()))
  233. {
  234. return std::string("\"svn\": 1 ");
  235. }
  236. s = lg->GetSourceDirectory();
  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. }