cmExtraSublimeTextGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. // Write the folder entries to the project file
  95. fout << "{\n";
  96. fout << "\t\"folders\":\n\t[\n\t";
  97. if (!sourceRootRelativeToOutput.empty())
  98. {
  99. fout << "\t{\n\t\t\t\"path\": \"" << sourceRootRelativeToOutput << "\"";
  100. const std::string &outputRelativeToSourceRoot =
  101. cmSystemTools::RelativePath(lgs[0]->GetMakefile()->GetHomeDirectory(),
  102. lgs[0]->GetMakefile()->
  103. GetHomeOutputDirectory());
  104. fout << ",\n\t\t\t\"folder_exclude_patterns\": [\"" <<
  105. outputRelativeToSourceRoot << "\"]";
  106. }
  107. else
  108. {
  109. fout << "\t{\n\t\t\t\"path\": \"./\"";
  110. }
  111. fout << "\n\t\t}";
  112. // In order for SublimeClang's path resolution to work, the directory that
  113. // contains the sublime-project file must be included here. We just ensure
  114. // that no files or subfolders are included.
  115. // In the case of an in-source build, however, this must not be used, since
  116. // it'll end up excluding the source directory.
  117. if (!sourceRootRelativeToOutput.empty())
  118. {
  119. fout << ",\n\t\t{\n\t\t\t\"path\": \"./\",\n";
  120. fout << "\t\t\t\"folder_exclude_patterns\": [\"*\"],\n";
  121. fout << "\t\t\t\"file_exclude_patterns\": [\"*\"]\n";
  122. fout << "\t\t}";
  123. }
  124. // End of the folders section
  125. fout << "\n\t]";
  126. // Write the beginning of the build systems section to the project file
  127. fout << ",\n\t\"build_systems\":\n\t[\n\t";
  128. // Set of include directories over all targets (sublime text/sublimeclang
  129. // doesn't currently support these settings per build system, only project
  130. // wide
  131. std::set<std::string> includeDirs;
  132. std::set<std::string> defines;
  133. AppendAllTargets(lgs, mf, fout, includeDirs, defines);
  134. // End of build_systems
  135. fout << "\n\t]";
  136. // Write the settings section with sublimeclang options
  137. fout << ",\n\t\"settings\":\n\t{\n\t";
  138. // Check if the CMAKE_SUBLIMECLANG_DISABLED flag has been set. If it has
  139. // disable sublimeclang for this project.
  140. const char* sublimeclangDisabledValue =
  141. lgs[0]->GetMakefile()->GetSafeDefinition("CMAKE_SUBLIMECLANG_DISABLED");
  142. bool sublimeclangEnabled = cmSystemTools::IsOff(sublimeclangDisabledValue);
  143. // Per project sublimeclang settings override default and user settings,
  144. // so we only write the sublimeclang_enabled setting to the project file
  145. // if it is set to be disabled.
  146. if (!sublimeclangEnabled)
  147. {
  148. fout << "\t\"sublimeclang_enabled\": false,\n\t";
  149. }
  150. fout << "\t\"sublimeclang_options\":\n\t\t[\n\t\t";
  151. std::set<std::string>::const_iterator stringSetIter = includeDirs.begin();
  152. while (stringSetIter != includeDirs.end())
  153. {
  154. const std::string &includeDir = *stringSetIter;
  155. const std::string &relative = cmSystemTools::RelativePath(
  156. lgs[0]->GetMakefile()->GetHomeOutputDirectory(),
  157. includeDir.c_str());
  158. // It appears that a relative path to the sublime-project file doesn't
  159. // always work. So we use ${folder:${project_path:<project_filename>}}
  160. // that SublimeClang will expand to the correct path
  161. fout << "\t\"-I${folder:${project_path:" << mf->GetProjectName() <<
  162. ".sublime-project}}/" << relative << "\"";
  163. stringSetIter++;
  164. if ((stringSetIter != includeDirs.end()) || (!defines.empty()))
  165. {
  166. fout << ",";
  167. }
  168. fout << "\n\t\t";
  169. }
  170. stringSetIter = defines.begin();
  171. while (stringSetIter != defines.end())
  172. {
  173. fout << "\t\"-D" << *stringSetIter << "\"";
  174. stringSetIter++;
  175. if (stringSetIter != defines.end())
  176. {
  177. fout << ",";
  178. }
  179. fout << "\n\t\t";
  180. }
  181. // End of the sublimeclang_options section
  182. fout << "]\n\t";
  183. // End of the settings section
  184. fout << "}\n";
  185. // End of file
  186. fout << "}";
  187. }
  188. void cmExtraSublimeTextGenerator::
  189. AppendAllTargets(const std::vector<cmLocalGenerator*>& lgs,
  190. const cmMakefile* mf,
  191. cmGeneratedFileStream& fout,
  192. std::set<std::string>& includeDirs,
  193. std::set<std::string>& defines)
  194. {
  195. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  196. std::string compiler = "";
  197. this->AppendTarget(fout, "all", 0, make.c_str(), mf, compiler.c_str(),
  198. includeDirs, defines, true);
  199. this->AppendTarget(fout, "clean", 0, make.c_str(), mf, compiler.c_str(),
  200. includeDirs, defines, false);
  201. // add all executable and library targets and some of the GLOBAL
  202. // and UTILITY targets
  203. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  204. lg!=lgs.end(); lg++)
  205. {
  206. cmMakefile* makefile=(*lg)->GetMakefile();
  207. cmTargets& targets=makefile->GetTargets();
  208. for (cmTargets::iterator ti = targets.begin();
  209. ti != targets.end(); ti++)
  210. {
  211. switch(ti->second.GetType())
  212. {
  213. case cmTarget::GLOBAL_TARGET:
  214. {
  215. bool insertTarget = false;
  216. // Only add the global targets from CMAKE_BINARY_DIR,
  217. // not from the subdirs
  218. if (strcmp(makefile->GetStartOutputDirectory(),
  219. makefile->GetHomeOutputDirectory())==0)
  220. {
  221. insertTarget = true;
  222. // only add the "edit_cache" target if it's not ccmake, because
  223. // this will not work within the IDE
  224. if (ti->first == "edit_cache")
  225. {
  226. const char* editCommand = makefile->GetDefinition
  227. ("CMAKE_EDIT_COMMAND");
  228. if (editCommand == 0)
  229. {
  230. insertTarget = false;
  231. }
  232. else if (strstr(editCommand, "ccmake")!=NULL)
  233. {
  234. insertTarget = false;
  235. }
  236. }
  237. }
  238. if (insertTarget)
  239. {
  240. this->AppendTarget(fout, ti->first.c_str(), 0,
  241. make.c_str(), makefile, compiler.c_str(),
  242. includeDirs, defines, false);
  243. }
  244. }
  245. break;
  246. case cmTarget::UTILITY:
  247. // Add all utility targets, except the Nightly/Continuous/
  248. // Experimental-"sub"targets as e.g. NightlyStart
  249. if (((ti->first.find("Nightly")==0) &&(ti->first!="Nightly"))
  250. || ((ti->first.find("Continuous")==0)&&(ti->first!="Continuous"))
  251. || ((ti->first.find("Experimental")==0)
  252. && (ti->first!="Experimental")))
  253. {
  254. break;
  255. }
  256. this->AppendTarget(fout, ti->first.c_str(), 0,
  257. make.c_str(), makefile, compiler.c_str(),
  258. includeDirs, defines, false);
  259. break;
  260. case cmTarget::EXECUTABLE:
  261. case cmTarget::STATIC_LIBRARY:
  262. case cmTarget::SHARED_LIBRARY:
  263. case cmTarget::MODULE_LIBRARY:
  264. case cmTarget::OBJECT_LIBRARY:
  265. {
  266. this->AppendTarget(fout, ti->first.c_str(), &ti->second,
  267. make.c_str(), makefile, compiler.c_str(),
  268. includeDirs, defines, false);
  269. std::string fastTarget = ti->first;
  270. fastTarget += "/fast";
  271. this->AppendTarget(fout, fastTarget.c_str(), &ti->second,
  272. make.c_str(), makefile, compiler.c_str(),
  273. includeDirs, defines, false);
  274. }
  275. break;
  276. default:
  277. break;
  278. }
  279. }
  280. }
  281. }
  282. // Generate the build_system entry for one target
  283. void cmExtraSublimeTextGenerator::AppendTarget(cmGeneratedFileStream& fout,
  284. const char* targetName,
  285. cmTarget* target,
  286. const char* make,
  287. const cmMakefile* makefile,
  288. const char* compiler,
  289. std::set<std::string>&
  290. includeDirs,
  291. std::set<std::string>& defines,
  292. bool firstTarget)
  293. {
  294. if (target != 0)
  295. {
  296. // the compilerdefines for this target
  297. cmGeneratorTarget *gtgt = this->GlobalGenerator
  298. ->GetGeneratorTarget(target);
  299. std::string cdefs = gtgt->GetCompileDefinitions();
  300. if(!cdefs.empty())
  301. {
  302. // Expand the list.
  303. std::vector<std::string> defs;
  304. cmSystemTools::ExpandListArgument(cdefs.c_str(), defs);
  305. for(std::vector<std::string>::const_iterator di = defs.begin();
  306. di != defs.end(); ++di)
  307. {
  308. cmXMLSafe safedef(di->c_str());
  309. defines.insert(safedef.str());
  310. }
  311. }
  312. // the include directories for this target
  313. std::vector<std::string> includes;
  314. target->GetMakefile()->GetLocalGenerator()->
  315. GetIncludeDirectories(includes, gtgt);
  316. for(std::vector<std::string>::const_iterator dirIt=includes.begin();
  317. dirIt != includes.end();
  318. ++dirIt)
  319. {
  320. includeDirs.insert(*dirIt);
  321. }
  322. std::string systemIncludeDirs = makefile->GetSafeDefinition(
  323. "CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
  324. if (!systemIncludeDirs.empty())
  325. {
  326. std::vector<std::string> dirs;
  327. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  328. for(std::vector<std::string>::const_iterator dirIt=dirs.begin();
  329. dirIt != dirs.end();
  330. ++dirIt)
  331. {
  332. includeDirs.insert(*dirIt);
  333. }
  334. }
  335. systemIncludeDirs = makefile->GetSafeDefinition(
  336. "CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
  337. if (!systemIncludeDirs.empty())
  338. {
  339. std::vector<std::string> dirs;
  340. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  341. for(std::vector<std::string>::const_iterator dirIt=dirs.begin();
  342. dirIt != dirs.end();
  343. ++dirIt)
  344. {
  345. includeDirs.insert(*dirIt);
  346. }
  347. }
  348. }
  349. // Ninja uses ninja.build files (look for a way to get the output file name
  350. // from cmMakefile or something)
  351. std::string makefileName;
  352. if (strcmp(this->GlobalGenerator->GetName(), "Ninja")==0)
  353. {
  354. makefileName = "build.ninja";
  355. }
  356. else
  357. {
  358. makefileName = "Makefile";
  359. }
  360. if (!firstTarget)
  361. {
  362. fout << ",\n\t";
  363. }
  364. fout << "\t{\n\t\t\t\"name\": \"" << makefile->GetProjectName() << " - " <<
  365. targetName << "\",\n";
  366. fout << "\t\t\t\"cmd\": [" <<
  367. this->BuildMakeCommand(make, makefileName.c_str(), targetName) <<
  368. "],\n";
  369. fout << "\t\t\t\"working_dir\": \"${project_path}\",\n";
  370. fout << "\t\t\t\"file_regex\": \"^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$\"\n";
  371. fout << "\t\t}";
  372. }
  373. // Create the command line for building the given target using the selected
  374. // make
  375. std::string cmExtraSublimeTextGenerator::BuildMakeCommand(
  376. const std::string& make, const char* makefile, const char* target)
  377. {
  378. std::string command = "\"";
  379. command += make + "\"";
  380. if (strcmp(this->GlobalGenerator->GetName(), "NMake Makefiles")==0)
  381. {
  382. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  383. command += ", \"/NOLOGO\", \"/f\", \"";
  384. command += makefileName + "\"";
  385. command += ", \"VERBOSE=1\", \"";
  386. command += target;
  387. command += "\"";
  388. }
  389. else if (strcmp(this->GlobalGenerator->GetName(), "Ninja")==0)
  390. {
  391. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  392. command += ", \"-f\", \"";
  393. command += makefileName + "\"";
  394. command += ", \"-v\", \"";
  395. command += target;
  396. command += "\"";
  397. }
  398. else
  399. {
  400. std::string makefileName;
  401. if (strcmp(this->GlobalGenerator->GetName(), "MinGW Makefiles")==0)
  402. {
  403. // no escaping of spaces in this case, see
  404. // http://public.kitware.com/Bug/view.php?id=10014
  405. makefileName = makefile;
  406. }
  407. else
  408. {
  409. makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  410. }
  411. command += ", \"-f\", \"";
  412. command += makefileName + "\"";
  413. command += ", \"VERBOSE=1\", \"";
  414. command += target;
  415. command += "\"";
  416. }
  417. return command;
  418. }