1
0

cmExtraSublimeTextGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 char*) 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<cmStdString, 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.c_str(), *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.c_str(), *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.c_str(), *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.c_str(), *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 (strcmp(this->GlobalGenerator->GetName(), "Ninja")==0)
  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. if (strcmp(this->GlobalGenerator->GetName(), "NMake Makefiles")==0)
  297. {
  298. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  299. command += ", \"/NOLOGO\", \"/f\", \"";
  300. command += makefileName + "\"";
  301. command += ", \"VERBOSE=1\", \"";
  302. command += target;
  303. command += "\"";
  304. }
  305. else if (strcmp(this->GlobalGenerator->GetName(), "Ninja")==0)
  306. {
  307. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  308. command += ", \"-f\", \"";
  309. command += makefileName + "\"";
  310. command += ", \"-v\", \"";
  311. command += target;
  312. command += "\"";
  313. }
  314. else
  315. {
  316. std::string makefileName;
  317. if (strcmp(this->GlobalGenerator->GetName(), "MinGW Makefiles")==0)
  318. {
  319. // no escaping of spaces in this case, see
  320. // http://public.kitware.com/Bug/view.php?id=10014
  321. makefileName = makefile;
  322. }
  323. else
  324. {
  325. makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  326. }
  327. command += ", \"-f\", \"";
  328. command += makefileName + "\"";
  329. command += ", \"VERBOSE=1\", \"";
  330. command += target;
  331. command += "\"";
  332. }
  333. return command;
  334. }
  335. // TODO: Most of the code is picked up from the Ninja generator, refactor it.
  336. std::string
  337. cmExtraSublimeTextGenerator::ComputeFlagsForObject(cmSourceFile* source,
  338. cmLocalGenerator* lg,
  339. cmTarget *target,
  340. cmGeneratorTarget* gtgt)
  341. {
  342. std::string flags;
  343. cmMakefile *makefile = lg->GetMakefile();
  344. std::string language = source->GetLanguage();
  345. if (language.empty())
  346. {
  347. language = "C";
  348. }
  349. const char* config = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  350. // Add language-specific flags.
  351. lg->AddLanguageFlags(flags, language, config);
  352. lg->AddArchitectureFlags(flags, gtgt, language, config);
  353. // TODO: Fortran support.
  354. // // Fortran-specific flags computed for this target.
  355. // if(*l == "Fortran")
  356. // {
  357. // this->AddFortranFlags(flags);
  358. // }
  359. // Add shared-library flags if needed.
  360. lg->AddCMP0018Flags(flags, target, language, config);
  361. // Add include directory flags.
  362. {
  363. std::vector<std::string> includes;
  364. lg->GetIncludeDirectories(includes, gtgt, language, config);
  365. std::string includeFlags =
  366. lg->GetIncludeFlags(includes, gtgt, language, true); // full include paths
  367. lg->AppendFlags(flags, includeFlags.c_str());
  368. }
  369. // Append old-style preprocessor definition flags.
  370. lg->AppendFlags(flags, makefile->GetDefineFlags());
  371. // Add target-specific flags.
  372. lg->AddCompileOptions(flags, target, language, config);
  373. // Add source file specific flags.
  374. lg->AppendFlags(flags, source->GetProperty("COMPILE_FLAGS"));
  375. // TODO: Handle Apple frameworks.
  376. return flags;
  377. }
  378. // TODO: Refactor with
  379. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  380. std::string
  381. cmExtraSublimeTextGenerator::
  382. ComputeDefines(cmSourceFile *source, cmLocalGenerator* lg, cmTarget *target,
  383. cmGeneratorTarget*)
  384. {
  385. std::set<std::string> defines;
  386. cmMakefile *makefile = lg->GetMakefile();
  387. const std::string& language = source->GetLanguage();
  388. const char* config = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  389. // Add the export symbol definition for shared library objects.
  390. if(const char* exportMacro = target->GetExportMacro())
  391. {
  392. lg->AppendDefines(defines, exportMacro);
  393. }
  394. // Add preprocessor definitions for this target and configuration.
  395. lg->AddCompileDefinitions(defines, target, config);
  396. lg->AppendDefines(defines, source->GetProperty("COMPILE_DEFINITIONS"));
  397. {
  398. std::string defPropName = "COMPILE_DEFINITIONS_";
  399. defPropName += cmSystemTools::UpperCase(config);
  400. lg->AppendDefines(defines, source->GetProperty(defPropName.c_str()));
  401. }
  402. std::string definesString;
  403. lg->JoinDefines(defines, definesString, language);
  404. return definesString;
  405. }