cmExtraSublimeTextGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. bool insertTarget = false;
  154. // Only add the global targets from CMAKE_BINARY_DIR,
  155. // not from the subdirs
  156. if (strcmp(makefile->GetStartOutputDirectory(),
  157. makefile->GetHomeOutputDirectory())==0)
  158. {
  159. insertTarget = true;
  160. // only add the "edit_cache" target if it's not ccmake, because
  161. // this will not work within the IDE
  162. if (ti->first == "edit_cache")
  163. {
  164. const char* editCommand = makefile->GetDefinition
  165. ("CMAKE_EDIT_COMMAND");
  166. if (editCommand == 0)
  167. {
  168. insertTarget = false;
  169. }
  170. else if (strstr(editCommand, "ccmake")!=NULL)
  171. {
  172. insertTarget = false;
  173. }
  174. }
  175. }
  176. if (insertTarget)
  177. {
  178. this->AppendTarget(fout, ti->first.c_str(), *lg, 0,
  179. make.c_str(), makefile, compiler.c_str(),
  180. sourceFileFlags, false);
  181. }
  182. }
  183. break;
  184. case cmTarget::UTILITY:
  185. // Add all utility targets, except the Nightly/Continuous/
  186. // Experimental-"sub"targets as e.g. NightlyStart
  187. if (((ti->first.find("Nightly")==0) &&(ti->first!="Nightly"))
  188. || ((ti->first.find("Continuous")==0)&&(ti->first!="Continuous"))
  189. || ((ti->first.find("Experimental")==0)
  190. && (ti->first!="Experimental")))
  191. {
  192. break;
  193. }
  194. this->AppendTarget(fout, ti->first.c_str(), *lg, 0,
  195. make.c_str(), makefile, compiler.c_str(),
  196. sourceFileFlags, false);
  197. break;
  198. case cmTarget::EXECUTABLE:
  199. case cmTarget::STATIC_LIBRARY:
  200. case cmTarget::SHARED_LIBRARY:
  201. case cmTarget::MODULE_LIBRARY:
  202. case cmTarget::OBJECT_LIBRARY:
  203. {
  204. this->AppendTarget(fout, ti->first.c_str(), *lg, &ti->second,
  205. make.c_str(), makefile, compiler.c_str(),
  206. sourceFileFlags, false);
  207. std::string fastTarget = ti->first;
  208. fastTarget += "/fast";
  209. this->AppendTarget(fout, fastTarget.c_str(), *lg, &ti->second,
  210. make.c_str(), makefile, compiler.c_str(),
  211. sourceFileFlags, false);
  212. }
  213. break;
  214. default:
  215. break;
  216. }
  217. }
  218. }
  219. }
  220. void cmExtraSublimeTextGenerator::
  221. AppendTarget(cmGeneratedFileStream& fout,
  222. const char* targetName,
  223. cmLocalGenerator* lg,
  224. cmTarget* target,
  225. const char* make,
  226. const cmMakefile* makefile,
  227. const char*, //compiler
  228. MapSourceFileFlags& sourceFileFlags,
  229. bool firstTarget)
  230. {
  231. if (target != 0)
  232. {
  233. cmGeneratorTarget *gtgt = this->GlobalGenerator
  234. ->GetGeneratorTarget(target);
  235. std::vector<cmSourceFile*> const& sourceFiles = target->GetSourceFiles();
  236. std::vector<cmSourceFile*>::const_iterator sourceFilesEnd =
  237. sourceFiles.end();
  238. for (std::vector<cmSourceFile*>::const_iterator iter =
  239. sourceFiles.begin(); iter != sourceFilesEnd; ++iter)
  240. {
  241. cmSourceFile* sourceFile = *iter;
  242. MapSourceFileFlags::iterator sourceFileFlagsIter =
  243. sourceFileFlags.find(sourceFile->GetFullPath());
  244. if (sourceFileFlagsIter == sourceFileFlags.end())
  245. {
  246. sourceFileFlagsIter =
  247. sourceFileFlags.insert(MapSourceFileFlags::value_type(
  248. sourceFile->GetFullPath(), std::vector<std::string>())).first;
  249. }
  250. std::vector<std::string>& flags = sourceFileFlagsIter->second;
  251. std::string flagsString =
  252. this->ComputeFlagsForObject(*iter, lg, target, gtgt);
  253. std::string definesString =
  254. this->ComputeDefines(*iter, lg, target, gtgt);
  255. flags.clear();
  256. cmsys::RegularExpression flagRegex;
  257. // Regular expression to extract compiler flags from a string
  258. // https://gist.github.com/3944250
  259. const char* regexString =
  260. "(^|[ ])-[DIOUWfgs][^= ]+(=\\\"[^\"]+\\\"|=[^\"][^ ]+)?";
  261. flagRegex.compile(regexString);
  262. std::string workString = flagsString + " " + definesString;
  263. while (flagRegex.find(workString))
  264. {
  265. std::string::size_type start = flagRegex.start();
  266. if (workString[start] == ' ')
  267. {
  268. start++;
  269. }
  270. flags.push_back(workString.substr(start,
  271. flagRegex.end() - start));
  272. if (flagRegex.end() < workString.size())
  273. {
  274. workString = workString.substr(flagRegex.end());
  275. }
  276. else
  277. {
  278. workString = "";
  279. }
  280. }
  281. }
  282. }
  283. // Ninja uses ninja.build files (look for a way to get the output file name
  284. // from cmMakefile or something)
  285. std::string makefileName;
  286. if (strcmp(this->GlobalGenerator->GetName(), "Ninja")==0)
  287. {
  288. makefileName = "build.ninja";
  289. }
  290. else
  291. {
  292. makefileName = "Makefile";
  293. }
  294. if (!firstTarget)
  295. {
  296. fout << ",\n\t";
  297. }
  298. fout << "\t{\n\t\t\t\"name\": \"" << makefile->GetProjectName() << " - " <<
  299. targetName << "\",\n";
  300. fout << "\t\t\t\"cmd\": [" <<
  301. this->BuildMakeCommand(make, makefileName.c_str(), targetName) <<
  302. "],\n";
  303. fout << "\t\t\t\"working_dir\": \"${project_path}\",\n";
  304. fout << "\t\t\t\"file_regex\": \"^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$\"\n";
  305. fout << "\t\t}";
  306. }
  307. // Create the command line for building the given target using the selected
  308. // make
  309. std::string cmExtraSublimeTextGenerator::BuildMakeCommand(
  310. const std::string& make, const char* makefile, const char* target)
  311. {
  312. std::string command = "\"";
  313. command += make + "\"";
  314. if (strcmp(this->GlobalGenerator->GetName(), "NMake Makefiles")==0)
  315. {
  316. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  317. command += ", \"/NOLOGO\", \"/f\", \"";
  318. command += makefileName + "\"";
  319. command += ", \"VERBOSE=1\", \"";
  320. command += target;
  321. command += "\"";
  322. }
  323. else if (strcmp(this->GlobalGenerator->GetName(), "Ninja")==0)
  324. {
  325. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  326. command += ", \"-f\", \"";
  327. command += makefileName + "\"";
  328. command += ", \"-v\", \"";
  329. command += target;
  330. command += "\"";
  331. }
  332. else
  333. {
  334. std::string makefileName;
  335. if (strcmp(this->GlobalGenerator->GetName(), "MinGW Makefiles")==0)
  336. {
  337. // no escaping of spaces in this case, see
  338. // http://public.kitware.com/Bug/view.php?id=10014
  339. makefileName = makefile;
  340. }
  341. else
  342. {
  343. makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  344. }
  345. command += ", \"-f\", \"";
  346. command += makefileName + "\"";
  347. command += ", \"VERBOSE=1\", \"";
  348. command += target;
  349. command += "\"";
  350. }
  351. return command;
  352. }
  353. // TODO: Most of the code is picked up from the Ninja generator, refactor it.
  354. std::string
  355. cmExtraSublimeTextGenerator::ComputeFlagsForObject(cmSourceFile* source,
  356. cmLocalGenerator* lg,
  357. cmTarget *target,
  358. cmGeneratorTarget* gtgt)
  359. {
  360. std::string flags;
  361. cmMakefile *makefile = lg->GetMakefile();
  362. const char* language = source->GetLanguage();
  363. if (language == NULL)
  364. {
  365. language = "C";
  366. }
  367. const char* config = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  368. // Add language-specific flags.
  369. lg->AddLanguageFlags(flags, language, config);
  370. lg->AddArchitectureFlags(flags, gtgt, language, config);
  371. // TODO: Fortran support.
  372. // // Fortran-specific flags computed for this target.
  373. // if(*l == "Fortran")
  374. // {
  375. // this->AddFortranFlags(flags);
  376. // }
  377. // Add shared-library flags if needed.
  378. lg->AddCMP0018Flags(flags, target, language, config);
  379. // Add include directory flags.
  380. {
  381. std::vector<std::string> includes;
  382. lg->GetIncludeDirectories(includes, gtgt, language, config);
  383. std::string includeFlags =
  384. lg->GetIncludeFlags(includes, gtgt, language, true); // full include paths
  385. lg->AppendFlags(flags, includeFlags.c_str());
  386. }
  387. // Append old-style preprocessor definition flags.
  388. lg->AppendFlags(flags, makefile->GetDefineFlags());
  389. // Add target-specific flags.
  390. lg->AddCompileOptions(flags, target, config, language);
  391. // Add source file specific flags.
  392. lg->AppendFlags(flags, source->GetProperty("COMPILE_FLAGS"));
  393. // TODO: Handle Apple frameworks.
  394. return flags;
  395. }
  396. // TODO: Refactor with
  397. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  398. std::string
  399. cmExtraSublimeTextGenerator::
  400. ComputeDefines(cmSourceFile *source, cmLocalGenerator* lg, cmTarget *target,
  401. cmGeneratorTarget*)
  402. {
  403. std::set<std::string> defines;
  404. cmMakefile *makefile = lg->GetMakefile();
  405. const char* language = source->GetLanguage();
  406. if (language == NULL)
  407. {
  408. language = "";
  409. }
  410. const char* config = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  411. // Add the export symbol definition for shared library objects.
  412. if(const char* exportMacro = target->GetExportMacro())
  413. {
  414. lg->AppendDefines(defines, exportMacro);
  415. }
  416. // Add preprocessor definitions for this target and configuration.
  417. lg->AddCompileDefinitions(defines, target, config);
  418. lg->AppendDefines(defines, source->GetProperty("COMPILE_DEFINITIONS"));
  419. {
  420. std::string defPropName = "COMPILE_DEFINITIONS_";
  421. defPropName += cmSystemTools::UpperCase(config);
  422. lg->AppendDefines(defines, source->GetProperty(defPropName.c_str()));
  423. }
  424. std::string definesString;
  425. lg->JoinDefines(defines, definesString, language);
  426. return definesString;
  427. }