cmExtraSublimeTextGenerator.cxx 19 KB

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