cmExtraSublimeTextGenerator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. #if defined(_WIN32)
  23. # define PATH_SEP "\\"
  24. #else
  25. # define PATH_SEP "/"
  26. #endif
  27. /*
  28. Sublime Text 2 Generator
  29. Author: Morné Chamberlain
  30. This generator was initially based off of the CodeBlocks generator.
  31. Some useful URLs:
  32. Homepage:
  33. http://www.sublimetext.com/
  34. File format docs:
  35. http://www.sublimetext.com/docs/2/projects.html
  36. http://sublimetext.info/docs/en/reference/build_systems.html
  37. */
  38. //----------------------------------------------------------------------------
  39. void cmExtraSublimeTextGenerator
  40. ::GetDocumentation(cmDocumentationEntry& entry, const char*) const
  41. {
  42. entry.Name = this->GetName();
  43. entry.Brief = "Generates Sublime Text 2 project files.";
  44. entry.Full =
  45. "Project files for Sublime Text 2 will be created in the top directory "
  46. "and in every subdirectory which features a CMakeLists.txt file "
  47. "containing a PROJECT() call. "
  48. "Additionally a hierarchy of makefiles is generated into the "
  49. "build tree. The appropriate make program can build the project through "
  50. "the default make target. A \"make install\" target is also provided.";
  51. }
  52. cmExtraSublimeTextGenerator::cmExtraSublimeTextGenerator()
  53. :cmExternalMakefileProjectGenerator()
  54. {
  55. #if defined(_WIN32)
  56. this->SupportedGlobalGenerators.push_back("MinGW Makefiles");
  57. this->SupportedGlobalGenerators.push_back("NMake Makefiles");
  58. // disable until somebody actually tests it:
  59. // this->SupportedGlobalGenerators.push_back("MSYS Makefiles");
  60. #endif
  61. this->SupportedGlobalGenerators.push_back("Ninja");
  62. this->SupportedGlobalGenerators.push_back("Unix Makefiles");
  63. }
  64. void cmExtraSublimeTextGenerator::Generate()
  65. {
  66. // for each sub project in the project create a sublime text 2 project
  67. for (std::map<cmStdString, std::vector<cmLocalGenerator*> >::const_iterator
  68. it = this->GlobalGenerator->GetProjectMap().begin();
  69. it!= this->GlobalGenerator->GetProjectMap().end();
  70. ++it)
  71. {
  72. // create a project file
  73. this->CreateProjectFile(it->second);
  74. }
  75. }
  76. void cmExtraSublimeTextGenerator::CreateProjectFile(
  77. const std::vector<cmLocalGenerator*>& lgs)
  78. {
  79. const cmMakefile* mf=lgs[0]->GetMakefile();
  80. std::string outputDir=mf->GetStartOutputDirectory();
  81. std::string projectName=mf->GetProjectName();
  82. std::string filename=outputDir+"/";
  83. filename+=projectName+".sublime-project";
  84. this->CreateNewProjectFile(lgs, filename);
  85. }
  86. void cmExtraSublimeTextGenerator
  87. ::CreateNewProjectFile(const std::vector<cmLocalGenerator*>& lgs,
  88. const std::string& filename)
  89. {
  90. const cmMakefile* mf=lgs[0]->GetMakefile();
  91. cmGeneratedFileStream fout(filename.c_str());
  92. if(!fout)
  93. {
  94. return;
  95. }
  96. // Collect all files, this includes source files and list files
  97. std::vector<std::string> allFiles;
  98. GetFileList(lgs, allFiles);
  99. // A set of folders to include in the project
  100. std::set<std::string> folderIncludePatternsSet;
  101. std::stringstream fileIncludePatternsStream;
  102. GetFileStringAndFolderSet(lgs, mf, allFiles, fileIncludePatternsStream,
  103. folderIncludePatternsSet);
  104. // Write the folder entries to the project file
  105. const std::string &homeRelative = cmSystemTools::RelativePath(
  106. lgs[0]->GetMakefile()->GetHomeOutputDirectory(),
  107. lgs[0]->GetMakefile()->GetHomeDirectory());
  108. fout << "{\n";
  109. fout << "\t\"folders\":\n\t[\n\t";
  110. fout << "\t{\n\t\t\t\"path\": \"" << homeRelative << "\",\n";
  111. fout << "\t\t\t\"folder_include_patterns\": [";
  112. std::set<std::string>::const_iterator folderIter =
  113. folderIncludePatternsSet.begin();
  114. while (folderIter != folderIncludePatternsSet.end())
  115. {
  116. fout << "\"" << *folderIter << "\"";
  117. folderIter++;
  118. if (folderIter != folderIncludePatternsSet.end())
  119. {
  120. fout << ", ";
  121. }
  122. }
  123. fout << "],\n";
  124. fout << "\t\t\t\"file_include_patterns\": [" <<
  125. fileIncludePatternsStream.str() << "]\n";
  126. fout << "\t\t},\n\t";
  127. // In order for SublimeClang's path resolution to work, the directory that
  128. // contains the sublime-project file must be included here. We just ensure
  129. // that no files or subfolders are included
  130. fout << "\t{\n\t\t\t\"path\": \"./\",\n";
  131. fout << "\t\t\t\"folder_exclude_patterns\": [\"*\"],\n";
  132. fout << "\t\t\t\"file_exclude_patterns\": [\"*\"]\n";
  133. fout << "\t\t}\n\t";
  134. // End of the folders section
  135. fout << "]";
  136. // Write the beginning of the build systems section to the project file
  137. fout << ",\n\t\"build_systems\":\n\t[\n\t";
  138. // Set of include directories over all targets (sublime text/sublimeclang
  139. // doesn't currently support these settings per build system, only project
  140. // wide
  141. std::set<std::string> includeDirs;
  142. std::set<std::string> defines;
  143. AppendAllTargets(lgs, mf, fout, includeDirs, defines);
  144. // End of build_systems
  145. fout << "\n\t]";
  146. // Write the settings section with sublimeclang options
  147. fout << ",\n\t\"settings\":\n\t{\n\t";
  148. fout << "\t\"sublimeclang_options\":\n\t\t[\n\t\t";
  149. std::set<std::string>::const_iterator stringSetIter = includeDirs.begin();
  150. while (stringSetIter != includeDirs.end())
  151. {
  152. const std::string &includeDir = *stringSetIter;
  153. const std::string &relative = cmSystemTools::RelativePath(
  154. lgs[0]->GetMakefile()->GetHomeOutputDirectory(),
  155. includeDir.c_str());
  156. // It appears that a relative path to the sublime-project file doesn't
  157. // always work. So we use ${folder:${project_path:<project_filename>}}
  158. // that SublimeClang will expand to the correct path
  159. fout << "\t\"-I${folder:${project_path:" << mf->GetProjectName() <<
  160. ".sublime-project}}/" << relative << "\"";
  161. stringSetIter++;
  162. if ((stringSetIter != includeDirs.end()) || (!defines.empty()))
  163. {
  164. fout << ",";
  165. }
  166. fout << "\n\t\t";
  167. }
  168. stringSetIter = defines.begin();
  169. while (stringSetIter != defines.end())
  170. {
  171. fout << "\t\"-D" << *stringSetIter << "\"";
  172. stringSetIter++;
  173. if (stringSetIter != defines.end())
  174. {
  175. fout << ",";
  176. }
  177. fout << "\n\t\t";
  178. }
  179. // End of the sublimeclang_options section
  180. fout << "]\n\t";
  181. // End of the settings section
  182. fout << "}\n";
  183. // End of file
  184. fout << "}";
  185. }
  186. void cmExtraSublimeTextGenerator
  187. ::GetFileList(const std::vector<cmLocalGenerator*>& lgs,
  188. std::vector<std::string>& allFiles)
  189. {
  190. for (std::vector<cmLocalGenerator *>::const_iterator
  191. it = lgs.begin();
  192. it != lgs.end();
  193. ++it)
  194. {
  195. cmMakefile* makefile=(*it)->GetMakefile();
  196. // Add list files
  197. const std::vector<std::string> & listFiles =
  198. makefile->GetListFiles();
  199. allFiles.insert(allFiles.end(), listFiles.begin(), listFiles.end());
  200. // Add source files
  201. cmTargets& targets=makefile->GetTargets();
  202. for (cmTargets::iterator ti = targets.begin();
  203. ti != targets.end(); ti++)
  204. {
  205. switch(ti->second.GetType())
  206. {
  207. case cmTarget::EXECUTABLE:
  208. case cmTarget::STATIC_LIBRARY:
  209. case cmTarget::SHARED_LIBRARY:
  210. case cmTarget::MODULE_LIBRARY:
  211. case cmTarget::OBJECT_LIBRARY:
  212. case cmTarget::UTILITY: // can have sources since 2.6.3
  213. {
  214. const std::vector<cmSourceFile*>&sources=ti->second.GetSourceFiles();
  215. for (std::vector<cmSourceFile*>::const_iterator si=sources.begin();
  216. si!=sources.end(); si++)
  217. {
  218. // don't add source files which have the GENERATED property set:
  219. if ((*si)->GetPropertyAsBool("GENERATED"))
  220. {
  221. continue;
  222. }
  223. allFiles.push_back((*si)->GetFullPath());
  224. }
  225. }
  226. default: // intended fallthrough
  227. break;
  228. }
  229. }
  230. }
  231. }
  232. void cmExtraSublimeTextGenerator::
  233. GetFileStringAndFolderSet(const std::vector<cmLocalGenerator*>& lgs,
  234. const cmMakefile* mf,
  235. const std::vector<std::string>& allFiles,
  236. std::stringstream& fileIncludePatternsStream,
  237. std::set<std::string>& folderIncludePatternsSet)
  238. {
  239. const char* cmakeRoot = mf->GetDefinition("CMAKE_ROOT");
  240. for (std::vector<std::string>::const_iterator jt = allFiles.begin();
  241. jt != allFiles.end();
  242. ++jt)
  243. {
  244. // don't put cmake's own files into the project (#12110):
  245. if (jt->find(cmakeRoot) == 0)
  246. {
  247. continue;
  248. }
  249. const std::string &relative = cmSystemTools::RelativePath(
  250. lgs[0]->GetMakefile()->GetHomeDirectory(),
  251. jt->c_str());
  252. // Split filename from path
  253. std::string fileName = cmSystemTools::GetFilenameName(relative);
  254. std::string path = "";
  255. if (fileName.length() < relative.length())
  256. {
  257. path = relative.substr(0, relative.length() - fileName.length() - 1);
  258. }
  259. // We don't want paths with CMakeFiles in them
  260. if (relative.find("CMakeFiles") == std::string::npos)
  261. {
  262. if (fileIncludePatternsStream.tellp() > 0)
  263. {
  264. fileIncludePatternsStream << ", ";
  265. }
  266. fileIncludePatternsStream << "\"" << relative << "\"";
  267. if ((!path.empty()) && (folderIncludePatternsSet.find(path) ==
  268. folderIncludePatternsSet.end()))
  269. {
  270. folderIncludePatternsSet.insert(path);
  271. std::string::size_type splitIndex = path.rfind(PATH_SEP);
  272. std::string splitPath = path;
  273. while (splitIndex != std::string::npos)
  274. {
  275. splitPath = splitPath.substr(0, splitIndex);
  276. if ((splitPath.empty()) ||
  277. (folderIncludePatternsSet.insert(splitPath).second == false))
  278. {
  279. // If the path is already in the set then all of its
  280. // parents are as well
  281. break;
  282. }
  283. splitIndex = splitPath.rfind(PATH_SEP);
  284. }
  285. }
  286. }
  287. }
  288. }
  289. void cmExtraSublimeTextGenerator::
  290. AppendAllTargets(const std::vector<cmLocalGenerator*>& lgs,
  291. const cmMakefile* mf,
  292. cmGeneratedFileStream& fout,
  293. std::set<std::string>& includeDirs,
  294. std::set<std::string>& defines)
  295. {
  296. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  297. std::string compiler = "";
  298. this->AppendTarget(fout, "all", 0, make.c_str(), mf, compiler.c_str(),
  299. includeDirs, defines, true);
  300. this->AppendTarget(fout, "clean", 0, make.c_str(), mf, compiler.c_str(),
  301. includeDirs, defines, false);
  302. // add all executable and library targets and some of the GLOBAL
  303. // and UTILITY targets
  304. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  305. lg!=lgs.end(); lg++)
  306. {
  307. cmMakefile* makefile=(*lg)->GetMakefile();
  308. cmTargets& targets=makefile->GetTargets();
  309. for (cmTargets::iterator ti = targets.begin();
  310. ti != targets.end(); ti++)
  311. {
  312. switch(ti->second.GetType())
  313. {
  314. case cmTarget::GLOBAL_TARGET:
  315. {
  316. bool insertTarget = false;
  317. // Only add the global targets from CMAKE_BINARY_DIR,
  318. // not from the subdirs
  319. if (strcmp(makefile->GetStartOutputDirectory(),
  320. makefile->GetHomeOutputDirectory())==0)
  321. {
  322. insertTarget = true;
  323. // only add the "edit_cache" target if it's not ccmake, because
  324. // this will not work within the IDE
  325. if (ti->first == "edit_cache")
  326. {
  327. const char* editCommand = makefile->GetDefinition
  328. ("CMAKE_EDIT_COMMAND");
  329. if (editCommand == 0)
  330. {
  331. insertTarget = false;
  332. }
  333. else if (strstr(editCommand, "ccmake")!=NULL)
  334. {
  335. insertTarget = false;
  336. }
  337. }
  338. }
  339. if (insertTarget)
  340. {
  341. this->AppendTarget(fout, ti->first.c_str(), 0,
  342. make.c_str(), makefile, compiler.c_str(),
  343. includeDirs, defines, false);
  344. }
  345. }
  346. break;
  347. case cmTarget::UTILITY:
  348. // Add all utility targets, except the Nightly/Continuous/
  349. // Experimental-"sub"targets as e.g. NightlyStart
  350. if (((ti->first.find("Nightly")==0) &&(ti->first!="Nightly"))
  351. || ((ti->first.find("Continuous")==0)&&(ti->first!="Continuous"))
  352. || ((ti->first.find("Experimental")==0)
  353. && (ti->first!="Experimental")))
  354. {
  355. break;
  356. }
  357. this->AppendTarget(fout, ti->first.c_str(), 0,
  358. make.c_str(), makefile, compiler.c_str(),
  359. includeDirs, defines, false);
  360. break;
  361. case cmTarget::EXECUTABLE:
  362. case cmTarget::STATIC_LIBRARY:
  363. case cmTarget::SHARED_LIBRARY:
  364. case cmTarget::MODULE_LIBRARY:
  365. case cmTarget::OBJECT_LIBRARY:
  366. {
  367. this->AppendTarget(fout, ti->first.c_str(), &ti->second,
  368. make.c_str(), makefile, compiler.c_str(),
  369. includeDirs, defines, false);
  370. std::string fastTarget = ti->first;
  371. fastTarget += "/fast";
  372. this->AppendTarget(fout, fastTarget.c_str(), &ti->second,
  373. make.c_str(), makefile, compiler.c_str(),
  374. includeDirs, defines, false);
  375. }
  376. break;
  377. default:
  378. break;
  379. }
  380. }
  381. }
  382. }
  383. // Generate the build_system entry for one target
  384. void cmExtraSublimeTextGenerator::AppendTarget(cmGeneratedFileStream& fout,
  385. const char* targetName,
  386. cmTarget* target,
  387. const char* make,
  388. const cmMakefile* makefile,
  389. const char* compiler,
  390. std::set<std::string>&
  391. includeDirs,
  392. std::set<std::string>& defines,
  393. bool firstTarget)
  394. {
  395. if (target != 0)
  396. {
  397. // the compilerdefines for this target
  398. cmGeneratorTarget *gtgt = this->GlobalGenerator
  399. ->GetGeneratorTarget(target);
  400. std::string cdefs = gtgt->GetCompileDefinitions();
  401. if(!cdefs.empty())
  402. {
  403. // Expand the list.
  404. std::vector<std::string> defs;
  405. cmSystemTools::ExpandListArgument(cdefs.c_str(), defs);
  406. for(std::vector<std::string>::const_iterator di = defs.begin();
  407. di != defs.end(); ++di)
  408. {
  409. cmXMLSafe safedef(di->c_str());
  410. defines.insert(safedef.str());
  411. }
  412. }
  413. // the include directories for this target
  414. std::vector<std::string> includes;
  415. target->GetMakefile()->GetLocalGenerator()->
  416. GetIncludeDirectories(includes, gtgt);
  417. for(std::vector<std::string>::const_iterator dirIt=includes.begin();
  418. dirIt != includes.end();
  419. ++dirIt)
  420. {
  421. includeDirs.insert(*dirIt);
  422. }
  423. std::string systemIncludeDirs = makefile->GetSafeDefinition(
  424. "CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
  425. if (!systemIncludeDirs.empty())
  426. {
  427. std::vector<std::string> dirs;
  428. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  429. for(std::vector<std::string>::const_iterator dirIt=dirs.begin();
  430. dirIt != dirs.end();
  431. ++dirIt)
  432. {
  433. includeDirs.insert(*dirIt);
  434. }
  435. }
  436. systemIncludeDirs = makefile->GetSafeDefinition(
  437. "CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
  438. if (!systemIncludeDirs.empty())
  439. {
  440. std::vector<std::string> dirs;
  441. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  442. for(std::vector<std::string>::const_iterator dirIt=dirs.begin();
  443. dirIt != dirs.end();
  444. ++dirIt)
  445. {
  446. includeDirs.insert(*dirIt);
  447. }
  448. }
  449. }
  450. // Write out the build_system data for this target
  451. std::string makefileName = makefile->GetHomeOutputDirectory();
  452. // Ninja uses ninja.build files (look for a way to get the output file name
  453. // from cmMakefile)
  454. if (strcmp(this->GlobalGenerator->GetName(), "Ninja")==0)
  455. {
  456. makefileName += "/build.ninja";
  457. }
  458. else
  459. {
  460. makefileName += "/Makefile";
  461. }
  462. if (!firstTarget)
  463. {
  464. fout << ",\n\t";
  465. }
  466. fout << "\t{\n\t\t\t\"name\": \"" << makefile->GetProjectName() << " - " <<
  467. targetName << "\",\n";
  468. fout << "\t\t\t\"cmd\": [" <<
  469. this->BuildMakeCommand(make, makefileName.c_str(), targetName) <<
  470. "],\n";
  471. fout << "\t\t\t\"working_dir\": \"${project_path}\",\n";
  472. fout << "\t\t\t\"file_regex\": \"^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$\"\n";
  473. fout << "\t\t}";
  474. }
  475. // Create the command line for building the given target using the selected
  476. // make
  477. std::string cmExtraSublimeTextGenerator::BuildMakeCommand(
  478. const std::string& make, const char* makefile, const char* target)
  479. {
  480. std::string command = "\"";
  481. command += make + "\"";
  482. if (strcmp(this->GlobalGenerator->GetName(), "NMake Makefiles")==0)
  483. {
  484. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  485. command += ", \"/NOLOGO\", \"/f\", \"";
  486. command += makefileName + "\"";
  487. command += ", \"VERBOSE=1\", \"";
  488. command += target;
  489. command += "\"";
  490. }
  491. else if (strcmp(this->GlobalGenerator->GetName(), "Ninja")==0)
  492. {
  493. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  494. command += ", \"-f\", \"";
  495. command += makefileName + "\"";
  496. command += ", \"-v\", \"";
  497. command += target;
  498. command += "\"";
  499. }
  500. else
  501. {
  502. std::string makefileName;
  503. if (strcmp(this->GlobalGenerator->GetName(), "MinGW Makefiles")==0)
  504. {
  505. // no escaping of spaces in this case, see
  506. // http://public.kitware.com/Bug/view.php?id=10014
  507. makefileName = makefile;
  508. }
  509. else
  510. {
  511. makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  512. }
  513. command += ", \"-f\", \"";
  514. command += makefileName + "\"";
  515. command += ", \"VERBOSE=1\", \"";
  516. command += target;
  517. command += "\"";
  518. }
  519. return command;
  520. }