cmExtraSublimeTextGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 "cmake.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmGeneratorTarget.h"
  15. #include "cmGlobalUnixMakefileGenerator3.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmLocalUnixMakefileGenerator3.h"
  18. #include "cmMakefile.h"
  19. #include "cmSourceFile.h"
  20. #include "cmSystemTools.h"
  21. #include "cmTarget.h"
  22. #include "cmXMLSafe.h"
  23. #include <cmsys/SystemTools.hxx>
  24. /*
  25. Sublime Text 2 Generator
  26. Author: Morné Chamberlain
  27. This generator was initially based off of the CodeBlocks generator.
  28. Some useful URLs:
  29. Homepage:
  30. http://www.sublimetext.com/
  31. File format docs:
  32. http://www.sublimetext.com/docs/2/projects.html
  33. http://sublimetext.info/docs/en/reference/build_systems.html
  34. */
  35. //----------------------------------------------------------------------------
  36. void cmExtraSublimeTextGenerator
  37. ::GetDocumentation(cmDocumentationEntry& entry, const std::string&) const
  38. {
  39. entry.Name = this->GetName();
  40. entry.Brief = "Generates Sublime Text 2 project files.";
  41. }
  42. cmExtraSublimeTextGenerator::cmExtraSublimeTextGenerator()
  43. :cmExternalMakefileProjectGenerator()
  44. {
  45. #if defined(_WIN32)
  46. this->SupportedGlobalGenerators.push_back("MinGW Makefiles");
  47. this->SupportedGlobalGenerators.push_back("NMake Makefiles");
  48. // disable until somebody actually tests it:
  49. // this->SupportedGlobalGenerators.push_back("MSYS Makefiles");
  50. #endif
  51. this->SupportedGlobalGenerators.push_back("Ninja");
  52. this->SupportedGlobalGenerators.push_back("Unix Makefiles");
  53. }
  54. void cmExtraSublimeTextGenerator::Generate()
  55. {
  56. // for each sub project in the project create a sublime text 2 project
  57. for (std::map<std::string, std::vector<cmLocalGenerator*> >::const_iterator
  58. it = this->GlobalGenerator->GetProjectMap().begin();
  59. it!= this->GlobalGenerator->GetProjectMap().end();
  60. ++it)
  61. {
  62. // create a project file
  63. this->CreateProjectFile(it->second);
  64. }
  65. }
  66. void cmExtraSublimeTextGenerator::CreateProjectFile(
  67. const std::vector<cmLocalGenerator*>& lgs)
  68. {
  69. const cmMakefile* mf=lgs[0]->GetMakefile();
  70. std::string outputDir=mf->GetStartOutputDirectory();
  71. std::string projectName=mf->GetProjectName();
  72. const std::string filename =
  73. outputDir + "/" + projectName + ".sublime-project";
  74. this->CreateNewProjectFile(lgs, filename);
  75. }
  76. void cmExtraSublimeTextGenerator
  77. ::CreateNewProjectFile(const std::vector<cmLocalGenerator*>& lgs,
  78. const std::string& filename)
  79. {
  80. const cmMakefile* mf=lgs[0]->GetMakefile();
  81. cmGeneratedFileStream fout(filename.c_str());
  82. if(!fout)
  83. {
  84. return;
  85. }
  86. const std::string &sourceRootRelativeToOutput = cmSystemTools::RelativePath(
  87. mf->GetHomeOutputDirectory(),
  88. mf->GetHomeDirectory());
  89. // Write the folder entries to the project file
  90. fout << "{\n";
  91. fout << "\t\"folders\":\n\t[\n\t";
  92. if (!sourceRootRelativeToOutput.empty())
  93. {
  94. fout << "\t{\n\t\t\t\"path\": \"" << sourceRootRelativeToOutput << "\"";
  95. const std::string &outputRelativeToSourceRoot =
  96. cmSystemTools::RelativePath(mf->GetHomeDirectory(),
  97. mf->GetHomeOutputDirectory());
  98. if ((!outputRelativeToSourceRoot.empty()) &&
  99. ((outputRelativeToSourceRoot.length() < 3) ||
  100. (outputRelativeToSourceRoot.substr(0, 3) != "../")))
  101. {
  102. fout << ",\n\t\t\t\"folder_exclude_patterns\": [\"" <<
  103. outputRelativeToSourceRoot << "\"]";
  104. }
  105. }
  106. else
  107. {
  108. fout << "\t{\n\t\t\t\"path\": \"./\"";
  109. }
  110. fout << "\n\t\t}";
  111. // End of the folders section
  112. fout << "\n\t]";
  113. // Write the beginning of the build systems section to the project file
  114. fout << ",\n\t\"build_systems\":\n\t[\n\t";
  115. // Set of include directories over all targets (sublime text/sublimeclang
  116. // doesn't currently support these settings per build system, only project
  117. // wide
  118. MapSourceFileFlags sourceFileFlags;
  119. AppendAllTargets(lgs, mf, fout, sourceFileFlags);
  120. // End of build_systems
  121. fout << "\n\t]";
  122. fout << "\n\t}";
  123. }
  124. void cmExtraSublimeTextGenerator::
  125. AppendAllTargets(const std::vector<cmLocalGenerator*>& lgs,
  126. const cmMakefile* mf,
  127. cmGeneratedFileStream& fout,
  128. MapSourceFileFlags& sourceFileFlags)
  129. {
  130. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  131. std::string compiler = "";
  132. if (!lgs.empty())
  133. {
  134. this->AppendTarget(fout, "all", lgs[0], 0, make.c_str(), mf,
  135. compiler.c_str(), sourceFileFlags, true);
  136. this->AppendTarget(fout, "clean", lgs[0], 0, make.c_str(), mf,
  137. compiler.c_str(), sourceFileFlags, false);
  138. }
  139. // add all executable and library targets and some of the GLOBAL
  140. // and UTILITY targets
  141. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  142. lg!=lgs.end(); lg++)
  143. {
  144. cmMakefile* makefile=(*lg)->GetMakefile();
  145. cmTargets& targets=makefile->GetTargets();
  146. for (cmTargets::iterator ti = targets.begin();
  147. ti != targets.end(); ti++)
  148. {
  149. switch(ti->second.GetType())
  150. {
  151. case cmTarget::GLOBAL_TARGET:
  152. {
  153. // Only add the global targets from CMAKE_BINARY_DIR,
  154. // not from the subdirs
  155. if (strcmp(makefile->GetStartOutputDirectory(),
  156. makefile->GetHomeOutputDirectory())==0)
  157. {
  158. this->AppendTarget(fout, ti->first, *lg, 0,
  159. make.c_str(), makefile, compiler.c_str(),
  160. sourceFileFlags, false);
  161. }
  162. }
  163. break;
  164. case cmTarget::UTILITY:
  165. // Add all utility targets, except the Nightly/Continuous/
  166. // Experimental-"sub"targets as e.g. NightlyStart
  167. if (((ti->first.find("Nightly")==0) &&(ti->first!="Nightly"))
  168. || ((ti->first.find("Continuous")==0)&&(ti->first!="Continuous"))
  169. || ((ti->first.find("Experimental")==0)
  170. && (ti->first!="Experimental")))
  171. {
  172. break;
  173. }
  174. this->AppendTarget(fout, ti->first, *lg, 0,
  175. make.c_str(), makefile, compiler.c_str(),
  176. sourceFileFlags, false);
  177. break;
  178. case cmTarget::EXECUTABLE:
  179. case cmTarget::STATIC_LIBRARY:
  180. case cmTarget::SHARED_LIBRARY:
  181. case cmTarget::MODULE_LIBRARY:
  182. case cmTarget::OBJECT_LIBRARY:
  183. {
  184. this->AppendTarget(fout, ti->first, *lg, &ti->second,
  185. make.c_str(), makefile, compiler.c_str(),
  186. sourceFileFlags, false);
  187. std::string fastTarget = ti->first;
  188. fastTarget += "/fast";
  189. this->AppendTarget(fout, fastTarget, *lg, &ti->second,
  190. make.c_str(), makefile, compiler.c_str(),
  191. sourceFileFlags, false);
  192. }
  193. break;
  194. default:
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. void cmExtraSublimeTextGenerator::
  201. AppendTarget(cmGeneratedFileStream& fout,
  202. const std::string& targetName,
  203. cmLocalGenerator* lg,
  204. cmTarget* target,
  205. const char* make,
  206. const cmMakefile* makefile,
  207. const char*, //compiler
  208. MapSourceFileFlags& sourceFileFlags,
  209. bool firstTarget)
  210. {
  211. if (target != 0)
  212. {
  213. cmGeneratorTarget *gtgt = this->GlobalGenerator
  214. ->GetGeneratorTarget(target);
  215. std::vector<cmSourceFile*> sourceFiles;
  216. target->GetSourceFiles(sourceFiles);
  217. std::vector<cmSourceFile*>::const_iterator sourceFilesEnd =
  218. sourceFiles.end();
  219. for (std::vector<cmSourceFile*>::const_iterator iter =
  220. sourceFiles.begin(); iter != sourceFilesEnd; ++iter)
  221. {
  222. cmSourceFile* sourceFile = *iter;
  223. MapSourceFileFlags::iterator sourceFileFlagsIter =
  224. sourceFileFlags.find(sourceFile->GetFullPath());
  225. if (sourceFileFlagsIter == sourceFileFlags.end())
  226. {
  227. sourceFileFlagsIter =
  228. sourceFileFlags.insert(MapSourceFileFlags::value_type(
  229. sourceFile->GetFullPath(), std::vector<std::string>())).first;
  230. }
  231. std::vector<std::string>& flags = sourceFileFlagsIter->second;
  232. std::string flagsString =
  233. this->ComputeFlagsForObject(*iter, lg, target, gtgt);
  234. std::string definesString =
  235. this->ComputeDefines(*iter, lg, target, gtgt);
  236. flags.clear();
  237. cmsys::RegularExpression flagRegex;
  238. // Regular expression to extract compiler flags from a string
  239. // https://gist.github.com/3944250
  240. const char* regexString =
  241. "(^|[ ])-[DIOUWfgs][^= ]+(=\\\"[^\"]+\\\"|=[^\"][^ ]+)?";
  242. flagRegex.compile(regexString);
  243. std::string workString = flagsString + " " + definesString;
  244. while (flagRegex.find(workString))
  245. {
  246. std::string::size_type start = flagRegex.start();
  247. if (workString[start] == ' ')
  248. {
  249. start++;
  250. }
  251. flags.push_back(workString.substr(start,
  252. flagRegex.end() - start));
  253. if (flagRegex.end() < workString.size())
  254. {
  255. workString = workString.substr(flagRegex.end());
  256. }
  257. else
  258. {
  259. workString = "";
  260. }
  261. }
  262. }
  263. }
  264. // Ninja uses ninja.build files (look for a way to get the output file name
  265. // from cmMakefile or something)
  266. std::string makefileName;
  267. if (this->GlobalGenerator->GetName() == "Ninja")
  268. {
  269. makefileName = "build.ninja";
  270. }
  271. else
  272. {
  273. makefileName = "Makefile";
  274. }
  275. if (!firstTarget)
  276. {
  277. fout << ",\n\t";
  278. }
  279. fout << "\t{\n\t\t\t\"name\": \"" << makefile->GetProjectName() << " - " <<
  280. targetName << "\",\n";
  281. fout << "\t\t\t\"cmd\": [" <<
  282. this->BuildMakeCommand(make, makefileName.c_str(), targetName) <<
  283. "],\n";
  284. fout << "\t\t\t\"working_dir\": \"${project_path}\",\n";
  285. fout << "\t\t\t\"file_regex\": \"^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$\"\n";
  286. fout << "\t\t}";
  287. }
  288. // Create the command line for building the given target using the selected
  289. // make
  290. std::string cmExtraSublimeTextGenerator::BuildMakeCommand(
  291. const std::string& make, const char* makefile,
  292. const std::string& target)
  293. {
  294. std::string command = "\"";
  295. command += make + "\"";
  296. std::string generator = this->GlobalGenerator->GetName();
  297. if (generator == "NMake Makefiles")
  298. {
  299. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  300. command += ", \"/NOLOGO\", \"/f\", \"";
  301. command += makefileName + "\"";
  302. command += ", \"VERBOSE=1\", \"";
  303. command += target;
  304. command += "\"";
  305. }
  306. else if (generator == "Ninja")
  307. {
  308. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  309. command += ", \"-f\", \"";
  310. command += makefileName + "\"";
  311. command += ", \"-v\", \"";
  312. command += target;
  313. command += "\"";
  314. }
  315. else
  316. {
  317. std::string makefileName;
  318. if (generator == "MinGW Makefiles")
  319. {
  320. // no escaping of spaces in this case, see
  321. // http://public.kitware.com/Bug/view.php?id=10014
  322. makefileName = makefile;
  323. }
  324. else
  325. {
  326. makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  327. }
  328. command += ", \"-f\", \"";
  329. command += makefileName + "\"";
  330. command += ", \"VERBOSE=1\", \"";
  331. command += target;
  332. command += "\"";
  333. }
  334. return command;
  335. }
  336. // TODO: Most of the code is picked up from the Ninja generator, refactor it.
  337. std::string
  338. cmExtraSublimeTextGenerator::ComputeFlagsForObject(cmSourceFile* source,
  339. cmLocalGenerator* lg,
  340. cmTarget *target,
  341. cmGeneratorTarget* gtgt)
  342. {
  343. std::string flags;
  344. cmMakefile *makefile = lg->GetMakefile();
  345. std::string language = source->GetLanguage();
  346. if (language.empty())
  347. {
  348. language = "C";
  349. }
  350. const std::string& config = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  351. // Add language-specific flags.
  352. lg->AddLanguageFlags(flags, language, config);
  353. lg->AddArchitectureFlags(flags, gtgt, language, config);
  354. // TODO: Fortran support.
  355. // // Fortran-specific flags computed for this target.
  356. // if(*l == "Fortran")
  357. // {
  358. // this->AddFortranFlags(flags);
  359. // }
  360. // Add shared-library flags if needed.
  361. lg->AddCMP0018Flags(flags, target, language, config);
  362. // Add include directory flags.
  363. {
  364. std::vector<std::string> includes;
  365. lg->GetIncludeDirectories(includes, gtgt, language, config);
  366. std::string includeFlags =
  367. lg->GetIncludeFlags(includes, gtgt, language, true); // full include paths
  368. lg->AppendFlags(flags, includeFlags.c_str());
  369. }
  370. // Append old-style preprocessor definition flags.
  371. lg->AppendFlags(flags, makefile->GetDefineFlags());
  372. // Add target-specific flags.
  373. lg->AddCompileOptions(flags, target, language, config);
  374. // Add source file specific flags.
  375. lg->AppendFlags(flags, source->GetProperty("COMPILE_FLAGS"));
  376. // TODO: Handle Apple frameworks.
  377. return flags;
  378. }
  379. // TODO: Refactor with
  380. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  381. std::string
  382. cmExtraSublimeTextGenerator::
  383. ComputeDefines(cmSourceFile *source, cmLocalGenerator* lg, cmTarget *target,
  384. cmGeneratorTarget*)
  385. {
  386. std::set<std::string> defines;
  387. cmMakefile *makefile = lg->GetMakefile();
  388. const std::string& language = source->GetLanguage();
  389. const std::string& config = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  390. // Add the export symbol definition for shared library objects.
  391. if(const char* exportMacro = target->GetExportMacro())
  392. {
  393. lg->AppendDefines(defines, exportMacro);
  394. }
  395. // Add preprocessor definitions for this target and configuration.
  396. lg->AddCompileDefinitions(defines, target, config);
  397. lg->AppendDefines(defines, source->GetProperty("COMPILE_DEFINITIONS"));
  398. {
  399. std::string defPropName = "COMPILE_DEFINITIONS_";
  400. defPropName += cmSystemTools::UpperCase(config);
  401. lg->AppendDefines(defines, source->GetProperty(defPropName));
  402. }
  403. std::string definesString;
  404. lg->JoinDefines(defines, definesString, language);
  405. return definesString;
  406. }