cmExtraSublimeTextGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 "cmExtraSublimeTextGenerator.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. Sublime Text 2 Generator
  24. Author: Morné Chamberlain
  25. This generator was initially based off of the CodeBlocks generator.
  26. Some useful URLs:
  27. Homepage:
  28. http://www.sublimetext.com/
  29. File format docs:
  30. http://www.sublimetext.com/docs/2/projects.html
  31. http://sublimetext.info/docs/en/reference/build_systems.html
  32. */
  33. //----------------------------------------------------------------------------
  34. void cmExtraSublimeTextGenerator
  35. ::GetDocumentation(cmDocumentationEntry& entry, const char*) const
  36. {
  37. entry.Name = this->GetName();
  38. entry.Brief = "Generates Sublime Text 2 project files.";
  39. entry.Full =
  40. "Project files for Sublime Text 2 will be created in the top directory "
  41. "and in every subdirectory which features a CMakeLists.txt file "
  42. "containing a PROJECT() call. "
  43. "Additionally Makefiles (or build.ninja files) are generated into the "
  44. "build tree. The appropriate make program can build the project through "
  45. "the default make target. A \"make install\" target is also provided.";
  46. }
  47. cmExtraSublimeTextGenerator::cmExtraSublimeTextGenerator()
  48. :cmExternalMakefileProjectGenerator()
  49. {
  50. #if defined(_WIN32)
  51. this->SupportedGlobalGenerators.push_back("MinGW Makefiles");
  52. this->SupportedGlobalGenerators.push_back("NMake Makefiles");
  53. // disable until somebody actually tests it:
  54. // this->SupportedGlobalGenerators.push_back("MSYS Makefiles");
  55. #endif
  56. this->SupportedGlobalGenerators.push_back("Ninja");
  57. this->SupportedGlobalGenerators.push_back("Unix Makefiles");
  58. }
  59. void cmExtraSublimeTextGenerator::Generate()
  60. {
  61. // for each sub project in the project create a sublime text 2 project
  62. for (std::map<cmStdString, std::vector<cmLocalGenerator*> >::const_iterator
  63. it = this->GlobalGenerator->GetProjectMap().begin();
  64. it!= this->GlobalGenerator->GetProjectMap().end();
  65. ++it)
  66. {
  67. // create a project file
  68. this->CreateProjectFile(it->second);
  69. }
  70. }
  71. void cmExtraSublimeTextGenerator::CreateProjectFile(
  72. const std::vector<cmLocalGenerator*>& lgs)
  73. {
  74. const cmMakefile* mf=lgs[0]->GetMakefile();
  75. std::string outputDir=mf->GetStartOutputDirectory();
  76. std::string projectName=mf->GetProjectName();
  77. std::string filename=outputDir+"/";
  78. filename+=projectName+".sublime-project";
  79. this->CreateNewProjectFile(lgs, filename);
  80. }
  81. void cmExtraSublimeTextGenerator
  82. ::CreateNewProjectFile(const std::vector<cmLocalGenerator*>& lgs,
  83. const std::string& filename)
  84. {
  85. const cmMakefile* mf=lgs[0]->GetMakefile();
  86. cmGeneratedFileStream fout(filename.c_str());
  87. if(!fout)
  88. {
  89. return;
  90. }
  91. const std::string &sourceRootRelativeToOutput = cmSystemTools::RelativePath(
  92. lgs[0]->GetMakefile()->GetHomeOutputDirectory(),
  93. lgs[0]->GetMakefile()->GetHomeDirectory());
  94. const std::string &outputRelativeToSourceRoot = cmSystemTools::RelativePath(
  95. lgs[0]->GetMakefile()->GetHomeDirectory(),
  96. lgs[0]->GetMakefile()->GetHomeOutputDirectory());
  97. // Write the folder entries to the project file
  98. fout << "{\n";
  99. fout << "\t\"folders\":\n\t[\n\t";
  100. fout << "\t{\n\t\t\t\"path\": \"" << sourceRootRelativeToOutput << "\",\n";
  101. fout << "\t\t\t\"folder_exclude_patterns\": [\"" <<
  102. outputRelativeToSourceRoot << "\"],\n";
  103. fout << "\t\t\t\"file_exclude_patterns\": []\n";
  104. fout << "\t\t},\n\t";
  105. // In order for SublimeClang's path resolution to work, the directory that
  106. // contains the sublime-project file must be included here. We just ensure
  107. // that no files or subfolders are included
  108. fout << "\t{\n\t\t\t\"path\": \"./\",\n";
  109. fout << "\t\t\t\"folder_exclude_patterns\": [\"*\"],\n";
  110. fout << "\t\t\t\"file_exclude_patterns\": [\"*\"]\n";
  111. fout << "\t\t}\n\t";
  112. // End of the folders section
  113. fout << "]";
  114. // Write the beginning of the build systems section to the project file
  115. fout << ",\n\t\"build_systems\":\n\t[\n\t";
  116. // Set of include directories over all targets (sublime text/sublimeclang
  117. // doesn't currently support these settings per build system, only project
  118. // wide
  119. std::set<std::string> includeDirs;
  120. std::set<std::string> defines;
  121. AppendAllTargets(lgs, mf, fout, includeDirs, defines);
  122. // End of build_systems
  123. fout << "\n\t]";
  124. // Write the settings section with sublimeclang options
  125. fout << ",\n\t\"settings\":\n\t{\n\t";
  126. // Check if the CMAKE_SUBLIMECLANG_DISABLED flag has been set. If it has
  127. // disable sublimeclang for this project.
  128. const char* sublimeclangDisabledValue =
  129. lgs[0]->GetMakefile()->GetSafeDefinition("CMAKE_SUBLIMECLANG_DISABLED");
  130. bool sublimeclangEnabled = cmSystemTools::IsOff(sublimeclangDisabledValue);
  131. // Per project sublimeclang settings override default and user settings,
  132. // so we only write the sublimeclang_enabled setting to the project file
  133. // if it is set to be disabled.
  134. if (!sublimeclangEnabled)
  135. {
  136. fout << "\t\"sublimeclang_enabled\": false,\n\t";
  137. }
  138. fout << "\t\"sublimeclang_options\":\n\t\t[\n\t\t";
  139. std::set<std::string>::const_iterator stringSetIter = includeDirs.begin();
  140. while (stringSetIter != includeDirs.end())
  141. {
  142. const std::string &includeDir = *stringSetIter;
  143. const std::string &relative = cmSystemTools::RelativePath(
  144. lgs[0]->GetMakefile()->GetHomeOutputDirectory(),
  145. includeDir.c_str());
  146. // It appears that a relative path to the sublime-project file doesn't
  147. // always work. So we use ${folder:${project_path:<project_filename>}}
  148. // that SublimeClang will expand to the correct path
  149. fout << "\t\"-I${folder:${project_path:" << mf->GetProjectName() <<
  150. ".sublime-project}}/" << relative << "\"";
  151. stringSetIter++;
  152. if ((stringSetIter != includeDirs.end()) || (!defines.empty()))
  153. {
  154. fout << ",";
  155. }
  156. fout << "\n\t\t";
  157. }
  158. stringSetIter = defines.begin();
  159. while (stringSetIter != defines.end())
  160. {
  161. fout << "\t\"-D" << *stringSetIter << "\"";
  162. stringSetIter++;
  163. if (stringSetIter != defines.end())
  164. {
  165. fout << ",";
  166. }
  167. fout << "\n\t\t";
  168. }
  169. // End of the sublimeclang_options section
  170. fout << "]\n\t";
  171. // End of the settings section
  172. fout << "}\n";
  173. // End of file
  174. fout << "}";
  175. }
  176. void cmExtraSublimeTextGenerator::
  177. AppendAllTargets(const std::vector<cmLocalGenerator*>& lgs,
  178. const cmMakefile* mf,
  179. cmGeneratedFileStream& fout,
  180. std::set<std::string>& includeDirs,
  181. std::set<std::string>& defines)
  182. {
  183. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  184. std::string compiler = "";
  185. this->AppendTarget(fout, "all", 0, make.c_str(), mf, compiler.c_str(),
  186. includeDirs, defines, true);
  187. this->AppendTarget(fout, "clean", 0, make.c_str(), mf, compiler.c_str(),
  188. includeDirs, defines, false);
  189. // add all executable and library targets and some of the GLOBAL
  190. // and UTILITY targets
  191. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  192. lg!=lgs.end(); lg++)
  193. {
  194. cmMakefile* makefile=(*lg)->GetMakefile();
  195. cmTargets& targets=makefile->GetTargets();
  196. for (cmTargets::iterator ti = targets.begin();
  197. ti != targets.end(); ti++)
  198. {
  199. switch(ti->second.GetType())
  200. {
  201. case cmTarget::GLOBAL_TARGET:
  202. {
  203. bool insertTarget = false;
  204. // Only add the global targets from CMAKE_BINARY_DIR,
  205. // not from the subdirs
  206. if (strcmp(makefile->GetStartOutputDirectory(),
  207. makefile->GetHomeOutputDirectory())==0)
  208. {
  209. insertTarget = true;
  210. // only add the "edit_cache" target if it's not ccmake, because
  211. // this will not work within the IDE
  212. if (ti->first == "edit_cache")
  213. {
  214. const char* editCommand = makefile->GetDefinition
  215. ("CMAKE_EDIT_COMMAND");
  216. if (editCommand == 0)
  217. {
  218. insertTarget = false;
  219. }
  220. else if (strstr(editCommand, "ccmake")!=NULL)
  221. {
  222. insertTarget = false;
  223. }
  224. }
  225. }
  226. if (insertTarget)
  227. {
  228. this->AppendTarget(fout, ti->first.c_str(), 0,
  229. make.c_str(), makefile, compiler.c_str(),
  230. includeDirs, defines, false);
  231. }
  232. }
  233. break;
  234. case cmTarget::UTILITY:
  235. // Add all utility targets, except the Nightly/Continuous/
  236. // Experimental-"sub"targets as e.g. NightlyStart
  237. if (((ti->first.find("Nightly")==0) &&(ti->first!="Nightly"))
  238. || ((ti->first.find("Continuous")==0)&&(ti->first!="Continuous"))
  239. || ((ti->first.find("Experimental")==0)
  240. && (ti->first!="Experimental")))
  241. {
  242. break;
  243. }
  244. this->AppendTarget(fout, ti->first.c_str(), 0,
  245. make.c_str(), makefile, compiler.c_str(),
  246. includeDirs, defines, false);
  247. break;
  248. case cmTarget::EXECUTABLE:
  249. case cmTarget::STATIC_LIBRARY:
  250. case cmTarget::SHARED_LIBRARY:
  251. case cmTarget::MODULE_LIBRARY:
  252. case cmTarget::OBJECT_LIBRARY:
  253. {
  254. this->AppendTarget(fout, ti->first.c_str(), &ti->second,
  255. make.c_str(), makefile, compiler.c_str(),
  256. includeDirs, defines, false);
  257. std::string fastTarget = ti->first;
  258. fastTarget += "/fast";
  259. this->AppendTarget(fout, fastTarget.c_str(), &ti->second,
  260. make.c_str(), makefile, compiler.c_str(),
  261. includeDirs, defines, false);
  262. }
  263. break;
  264. default:
  265. break;
  266. }
  267. }
  268. }
  269. }
  270. // Generate the build_system entry for one target
  271. void cmExtraSublimeTextGenerator::AppendTarget(cmGeneratedFileStream& fout,
  272. const char* targetName,
  273. cmTarget* target,
  274. const char* make,
  275. const cmMakefile* makefile,
  276. const char* compiler,
  277. std::set<std::string>&
  278. includeDirs,
  279. std::set<std::string>& defines,
  280. bool firstTarget)
  281. {
  282. if (target != 0)
  283. {
  284. // the compilerdefines for this target
  285. cmGeneratorTarget *gtgt = this->GlobalGenerator
  286. ->GetGeneratorTarget(target);
  287. std::string cdefs = gtgt->GetCompileDefinitions();
  288. if(!cdefs.empty())
  289. {
  290. // Expand the list.
  291. std::vector<std::string> defs;
  292. cmSystemTools::ExpandListArgument(cdefs.c_str(), defs);
  293. for(std::vector<std::string>::const_iterator di = defs.begin();
  294. di != defs.end(); ++di)
  295. {
  296. cmXMLSafe safedef(di->c_str());
  297. defines.insert(safedef.str());
  298. }
  299. }
  300. // the include directories for this target
  301. std::vector<std::string> includes;
  302. target->GetMakefile()->GetLocalGenerator()->
  303. GetIncludeDirectories(includes, gtgt);
  304. for(std::vector<std::string>::const_iterator dirIt=includes.begin();
  305. dirIt != includes.end();
  306. ++dirIt)
  307. {
  308. includeDirs.insert(*dirIt);
  309. }
  310. std::string systemIncludeDirs = makefile->GetSafeDefinition(
  311. "CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
  312. if (!systemIncludeDirs.empty())
  313. {
  314. std::vector<std::string> dirs;
  315. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  316. for(std::vector<std::string>::const_iterator dirIt=dirs.begin();
  317. dirIt != dirs.end();
  318. ++dirIt)
  319. {
  320. includeDirs.insert(*dirIt);
  321. }
  322. }
  323. systemIncludeDirs = makefile->GetSafeDefinition(
  324. "CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
  325. if (!systemIncludeDirs.empty())
  326. {
  327. std::vector<std::string> dirs;
  328. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  329. for(std::vector<std::string>::const_iterator dirIt=dirs.begin();
  330. dirIt != dirs.end();
  331. ++dirIt)
  332. {
  333. includeDirs.insert(*dirIt);
  334. }
  335. }
  336. }
  337. // Ninja uses ninja.build files (look for a way to get the output file name
  338. // from cmMakefile or something)
  339. std::string makefileName;
  340. if (strcmp(this->GlobalGenerator->GetName(), "Ninja")==0)
  341. {
  342. makefileName = "build.ninja";
  343. }
  344. else
  345. {
  346. makefileName = "Makefile";
  347. }
  348. if (!firstTarget)
  349. {
  350. fout << ",\n\t";
  351. }
  352. fout << "\t{\n\t\t\t\"name\": \"" << makefile->GetProjectName() << " - " <<
  353. targetName << "\",\n";
  354. fout << "\t\t\t\"cmd\": [" <<
  355. this->BuildMakeCommand(make, makefileName.c_str(), targetName) <<
  356. "],\n";
  357. fout << "\t\t\t\"working_dir\": \"${project_path}\",\n";
  358. fout << "\t\t\t\"file_regex\": \"^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$\"\n";
  359. fout << "\t\t}";
  360. }
  361. // Create the command line for building the given target using the selected
  362. // make
  363. std::string cmExtraSublimeTextGenerator::BuildMakeCommand(
  364. const std::string& make, const char* makefile, const char* target)
  365. {
  366. std::string command = "\"";
  367. command += make + "\"";
  368. if (strcmp(this->GlobalGenerator->GetName(), "NMake Makefiles")==0)
  369. {
  370. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  371. command += ", \"/NOLOGO\", \"/f\", \"";
  372. command += makefileName + "\"";
  373. command += ", \"VERBOSE=1\", \"";
  374. command += target;
  375. command += "\"";
  376. }
  377. else if (strcmp(this->GlobalGenerator->GetName(), "Ninja")==0)
  378. {
  379. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  380. command += ", \"-f\", \"";
  381. command += makefileName + "\"";
  382. command += ", \"-v\", \"";
  383. command += target;
  384. command += "\"";
  385. }
  386. else
  387. {
  388. std::string makefileName;
  389. if (strcmp(this->GlobalGenerator->GetName(), "MinGW Makefiles")==0)
  390. {
  391. // no escaping of spaces in this case, see
  392. // http://public.kitware.com/Bug/view.php?id=10014
  393. makefileName = makefile;
  394. }
  395. else
  396. {
  397. makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  398. }
  399. command += ", \"-f\", \"";
  400. command += makefileName + "\"";
  401. command += ", \"VERBOSE=1\", \"";
  402. command += target;
  403. command += "\"";
  404. }
  405. return command;
  406. }