cmExtraSublimeTextGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  218. std::vector<cmSourceFile*>::const_iterator sourceFilesEnd =
  219. sourceFiles.end();
  220. for (std::vector<cmSourceFile*>::const_iterator iter =
  221. sourceFiles.begin(); iter != sourceFilesEnd; ++iter)
  222. {
  223. cmSourceFile* sourceFile = *iter;
  224. MapSourceFileFlags::iterator sourceFileFlagsIter =
  225. sourceFileFlags.find(sourceFile->GetFullPath());
  226. if (sourceFileFlagsIter == sourceFileFlags.end())
  227. {
  228. sourceFileFlagsIter =
  229. sourceFileFlags.insert(MapSourceFileFlags::value_type(
  230. sourceFile->GetFullPath(), std::vector<std::string>())).first;
  231. }
  232. std::vector<std::string>& flags = sourceFileFlagsIter->second;
  233. std::string flagsString =
  234. this->ComputeFlagsForObject(*iter, lg, target, gtgt);
  235. std::string definesString =
  236. this->ComputeDefines(*iter, lg, target, gtgt);
  237. flags.clear();
  238. cmsys::RegularExpression flagRegex;
  239. // Regular expression to extract compiler flags from a string
  240. // https://gist.github.com/3944250
  241. const char* regexString =
  242. "(^|[ ])-[DIOUWfgs][^= ]+(=\\\"[^\"]+\\\"|=[^\"][^ ]+)?";
  243. flagRegex.compile(regexString);
  244. std::string workString = flagsString + " " + definesString;
  245. while (flagRegex.find(workString))
  246. {
  247. std::string::size_type start = flagRegex.start();
  248. if (workString[start] == ' ')
  249. {
  250. start++;
  251. }
  252. flags.push_back(workString.substr(start,
  253. flagRegex.end() - start));
  254. if (flagRegex.end() < workString.size())
  255. {
  256. workString = workString.substr(flagRegex.end());
  257. }
  258. else
  259. {
  260. workString = "";
  261. }
  262. }
  263. }
  264. }
  265. // Ninja uses ninja.build files (look for a way to get the output file name
  266. // from cmMakefile or something)
  267. std::string makefileName;
  268. if (this->GlobalGenerator->GetName() == "Ninja")
  269. {
  270. makefileName = "build.ninja";
  271. }
  272. else
  273. {
  274. makefileName = "Makefile";
  275. }
  276. if (!firstTarget)
  277. {
  278. fout << ",\n\t";
  279. }
  280. fout << "\t{\n\t\t\t\"name\": \"" << makefile->GetProjectName() << " - " <<
  281. targetName << "\",\n";
  282. fout << "\t\t\t\"cmd\": [" <<
  283. this->BuildMakeCommand(make, makefileName.c_str(), targetName) <<
  284. "],\n";
  285. fout << "\t\t\t\"working_dir\": \"${project_path}\",\n";
  286. fout << "\t\t\t\"file_regex\": \"^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$\"\n";
  287. fout << "\t\t}";
  288. }
  289. // Create the command line for building the given target using the selected
  290. // make
  291. std::string cmExtraSublimeTextGenerator::BuildMakeCommand(
  292. const std::string& make, const char* makefile,
  293. const std::string& target)
  294. {
  295. std::string command = "\"";
  296. command += make + "\"";
  297. std::string generator = this->GlobalGenerator->GetName();
  298. if (generator == "NMake Makefiles")
  299. {
  300. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  301. command += ", \"/NOLOGO\", \"/f\", \"";
  302. command += makefileName + "\"";
  303. command += ", \"VERBOSE=1\", \"";
  304. command += target;
  305. command += "\"";
  306. }
  307. else if (generator == "Ninja")
  308. {
  309. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  310. command += ", \"-f\", \"";
  311. command += makefileName + "\"";
  312. command += ", \"-v\", \"";
  313. command += target;
  314. command += "\"";
  315. }
  316. else
  317. {
  318. std::string makefileName;
  319. if (generator == "MinGW Makefiles")
  320. {
  321. // no escaping of spaces in this case, see
  322. // http://public.kitware.com/Bug/view.php?id=10014
  323. makefileName = makefile;
  324. }
  325. else
  326. {
  327. makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  328. }
  329. command += ", \"-f\", \"";
  330. command += makefileName + "\"";
  331. command += ", \"VERBOSE=1\", \"";
  332. command += target;
  333. command += "\"";
  334. }
  335. return command;
  336. }
  337. // TODO: Most of the code is picked up from the Ninja generator, refactor it.
  338. std::string
  339. cmExtraSublimeTextGenerator::ComputeFlagsForObject(cmSourceFile* source,
  340. cmLocalGenerator* lg,
  341. cmTarget *target,
  342. cmGeneratorTarget* gtgt)
  343. {
  344. std::string flags;
  345. cmMakefile *makefile = lg->GetMakefile();
  346. std::string language = source->GetLanguage();
  347. if (language.empty())
  348. {
  349. language = "C";
  350. }
  351. const std::string& config = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  352. // Add language-specific flags.
  353. lg->AddLanguageFlags(flags, language, config);
  354. lg->AddArchitectureFlags(flags, gtgt, language, config);
  355. // TODO: Fortran support.
  356. // // Fortran-specific flags computed for this target.
  357. // if(*l == "Fortran")
  358. // {
  359. // this->AddFortranFlags(flags);
  360. // }
  361. // Add shared-library flags if needed.
  362. lg->AddCMP0018Flags(flags, target, language, config);
  363. // Add include directory flags.
  364. {
  365. std::vector<std::string> includes;
  366. lg->GetIncludeDirectories(includes, gtgt, language, config);
  367. std::string includeFlags =
  368. lg->GetIncludeFlags(includes, gtgt, language, true); // full include paths
  369. lg->AppendFlags(flags, includeFlags);
  370. }
  371. // Append old-style preprocessor definition flags.
  372. lg->AppendFlags(flags, makefile->GetDefineFlags());
  373. // Add target-specific flags.
  374. lg->AddCompileOptions(flags, target, language, config);
  375. // Add source file specific flags.
  376. lg->AppendFlags(flags, source->GetProperty("COMPILE_FLAGS"));
  377. // TODO: Handle Apple frameworks.
  378. return flags;
  379. }
  380. // TODO: Refactor with
  381. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  382. std::string
  383. cmExtraSublimeTextGenerator::
  384. ComputeDefines(cmSourceFile *source, cmLocalGenerator* lg, cmTarget *target,
  385. cmGeneratorTarget*)
  386. {
  387. std::set<std::string> defines;
  388. cmMakefile *makefile = lg->GetMakefile();
  389. const std::string& language = source->GetLanguage();
  390. const std::string& config = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  391. // Add the export symbol definition for shared library objects.
  392. if(const char* exportMacro = target->GetExportMacro())
  393. {
  394. lg->AppendDefines(defines, exportMacro);
  395. }
  396. // Add preprocessor definitions for this target and configuration.
  397. lg->AddCompileDefinitions(defines, target, config, language);
  398. lg->AppendDefines(defines, source->GetProperty("COMPILE_DEFINITIONS"));
  399. {
  400. std::string defPropName = "COMPILE_DEFINITIONS_";
  401. defPropName += cmSystemTools::UpperCase(config);
  402. lg->AppendDefines(defines, source->GetProperty(defPropName));
  403. }
  404. std::string definesString;
  405. lg->JoinDefines(defines, definesString, language);
  406. return definesString;
  407. }