cmExtraSublimeTextGenerator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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. /* Some useful URLs:
  28. Homepage:
  29. http://www.sublimetext.com/
  30. File format docs:
  31. http://www.sublimetext.com/docs/2/projects.html
  32. http://sublimetext.info/docs/en/reference/build_systems.html
  33. */
  34. //----------------------------------------------------------------------------
  35. void cmExtraSublimeTextGenerator
  36. ::GetDocumentation(cmDocumentationEntry& entry, const char*) const
  37. {
  38. entry.Name = this->GetName();
  39. entry.Brief = "Generates Sublime Text 2 project files.";
  40. entry.Full =
  41. "Project files for Sublime Text 2 will be created in the top directory "
  42. "and in every subdirectory which features a CMakeLists.txt file "
  43. "containing a PROJECT() call. "
  44. "Additionally a hierarchy of makefiles is generated into the "
  45. "build tree. The appropriate make program can build the project through "
  46. "the default make target. A \"make install\" target is also provided.";
  47. }
  48. cmExtraSublimeTextGenerator::cmExtraSublimeTextGenerator()
  49. :cmExternalMakefileProjectGenerator()
  50. {
  51. #if defined(_WIN32)
  52. this->SupportedGlobalGenerators.push_back("MinGW Makefiles");
  53. this->SupportedGlobalGenerators.push_back("NMake Makefiles");
  54. // disable until somebody actually tests it:
  55. // this->SupportedGlobalGenerators.push_back("MSYS Makefiles");
  56. #endif
  57. this->SupportedGlobalGenerators.push_back("Ninja");
  58. this->SupportedGlobalGenerators.push_back("Unix Makefiles");
  59. }
  60. void cmExtraSublimeTextGenerator::Generate()
  61. {
  62. // for each sub project in the project create a sublime text 2 project
  63. for (std::map<cmStdString, std::vector<cmLocalGenerator*> >::const_iterator
  64. it = this->GlobalGenerator->GetProjectMap().begin();
  65. it!= this->GlobalGenerator->GetProjectMap().end();
  66. ++it)
  67. {
  68. // create a project file
  69. this->CreateProjectFile(it->second);
  70. }
  71. }
  72. /* create the project file */
  73. void cmExtraSublimeTextGenerator::CreateProjectFile(
  74. const std::vector<cmLocalGenerator*>& lgs)
  75. {
  76. const cmMakefile* mf=lgs[0]->GetMakefile();
  77. std::string outputDir=mf->GetStartOutputDirectory();
  78. std::string projectName=mf->GetProjectName();
  79. std::string filename=outputDir+"/";
  80. filename+=projectName+".sublime-project";
  81. this->CreateNewProjectFile(lgs, filename);
  82. }
  83. void cmExtraSublimeTextGenerator
  84. ::CreateNewProjectFile(const std::vector<cmLocalGenerator*>& lgs,
  85. const std::string& filename)
  86. {
  87. const cmMakefile* mf=lgs[0]->GetMakefile();
  88. cmGeneratedFileStream fout(filename.c_str());
  89. if(!fout)
  90. {
  91. return;
  92. }
  93. // Collect all files, this includes source files and list files
  94. std::vector<std::string> allFiles;
  95. GetFileList(lgs, allFiles);
  96. // A set of folders to include in the project
  97. std::set<std::string> folderIncludePatternsSet;
  98. std::stringstream fileIncludePatternsStream;
  99. GetFileStringAndFolderSet(lgs, mf, allFiles, fileIncludePatternsStream,
  100. folderIncludePatternsSet);
  101. // Write the folder entries to the project file
  102. const std::string &homeRelative = cmSystemTools::RelativePath(
  103. lgs[0]->GetMakefile()->GetHomeOutputDirectory(),
  104. lgs[0]->GetMakefile()->GetHomeDirectory());
  105. fout << "{\n";
  106. fout << "\t\"folders\":\n\t[\n\t";
  107. fout << "\t{\n\t\t\t\"path\": \"" << homeRelative << "\",\n";
  108. fout << "\t\t\t\"folder_include_patterns\": [";
  109. std::set<std::string>::const_iterator folderIter =
  110. folderIncludePatternsSet.begin();
  111. while (folderIter != folderIncludePatternsSet.end())
  112. {
  113. fout << "\"" << *folderIter << "\"";
  114. folderIter++;
  115. if (folderIter != folderIncludePatternsSet.end())
  116. {
  117. fout << ", ";
  118. }
  119. }
  120. fout << "],\n";
  121. fout << "\t\t\t\"file_include_patterns\": [" <<
  122. fileIncludePatternsStream.str() << "]\n";
  123. fout << "\t\t},\n\t";
  124. // In order for SublimeClang's path resolution to work, the directory that
  125. // contains the sublime-project file must be included here. We just ensure
  126. // that no files or subfolders are included
  127. fout << "\t{\n\t\t\t\"path\": \"./\",\n";
  128. fout << "\t\t\t\"folder_exclude_patterns\": [\"*\"],\n";
  129. fout << "\t\t\t\"file_exclude_patterns\": [\"*\"]\n";
  130. fout << "\t\t}\n\t";
  131. // End of the folders section
  132. fout << "]";
  133. // Write the beginning of the build systems section to the project file
  134. fout << ",\n\t\"build_systems\":\n\t[\n\t";
  135. // Set of include directories over all targets (sublime text/sublimeclang
  136. // doesn't currently support these settings per build system, only project
  137. // wide
  138. std::set<std::string> includeDirs;
  139. std::set<std::string> defines;
  140. AppendAllTargets(lgs, mf, fout, includeDirs, defines);
  141. // End of build_systems
  142. fout << "\n\t]";
  143. // Write the settings section with sublimeclang options
  144. fout << ",\n\t\"settings\":\n\t{\n\t";
  145. fout << "\t\"sublimeclang_options\":\n\t\t[\n\t\t";
  146. std::set<std::string>::const_iterator stringSetIter = includeDirs.begin();
  147. while (stringSetIter != includeDirs.end())
  148. {
  149. const std::string &includeDir = *stringSetIter;
  150. const std::string &relative = cmSystemTools::RelativePath(
  151. lgs[0]->GetMakefile()->GetHomeOutputDirectory(),
  152. includeDir.c_str());
  153. // It appears that a relative path to the sublime-project file doesn't
  154. // always work. So we use ${folder:${project_path:<project_filename>}}
  155. // that SublimeClang will expand to the correct path
  156. fout << "\t\"-I${folder:${project_path:" << mf->GetProjectName() <<
  157. ".sublime-project}}/" << relative << "\"";
  158. stringSetIter++;
  159. if ((stringSetIter != includeDirs.end()) || (!defines.empty()))
  160. {
  161. fout << ",";
  162. }
  163. fout << "\n\t\t";
  164. }
  165. stringSetIter = defines.begin();
  166. while (stringSetIter != defines.end())
  167. {
  168. fout << "\t\"-D" << *stringSetIter << "\"";
  169. stringSetIter++;
  170. if (stringSetIter != defines.end())
  171. {
  172. fout << ",";
  173. }
  174. fout << "\n\t\t";
  175. }
  176. // End of the sublimeclang_options section
  177. fout << "]\n\t";
  178. // End of the settings section
  179. fout << "}\n";
  180. // End of file
  181. fout << "}";
  182. }
  183. // Get a list of all files and a set of all directories and subdirectories
  184. // with source files
  185. void cmExtraSublimeTextGenerator
  186. ::GetFileList(const std::vector<cmLocalGenerator*>& lgs,
  187. std::vector<std::string>& allFiles)
  188. {
  189. for (std::vector<cmLocalGenerator *>::const_iterator
  190. it = lgs.begin();
  191. it != lgs.end();
  192. ++it)
  193. {
  194. cmMakefile* makefile=(*it)->GetMakefile();
  195. // Add list files
  196. const std::vector<std::string> & listFiles =
  197. makefile->GetListFiles();
  198. allFiles.insert(allFiles.end(), listFiles.begin(), listFiles.end());
  199. // Add source files
  200. cmTargets& targets=makefile->GetTargets();
  201. for (cmTargets::iterator ti = targets.begin();
  202. ti != targets.end(); ti++)
  203. {
  204. switch(ti->second.GetType())
  205. {
  206. case cmTarget::EXECUTABLE:
  207. case cmTarget::STATIC_LIBRARY:
  208. case cmTarget::SHARED_LIBRARY:
  209. case cmTarget::MODULE_LIBRARY:
  210. case cmTarget::OBJECT_LIBRARY:
  211. case cmTarget::UTILITY: // can have sources since 2.6.3
  212. {
  213. const std::vector<cmSourceFile*>&sources=ti->second.GetSourceFiles();
  214. for (std::vector<cmSourceFile*>::const_iterator si=sources.begin();
  215. si!=sources.end(); si++)
  216. {
  217. // don't add source files which have the GENERATED property set:
  218. if ((*si)->GetPropertyAsBool("GENERATED"))
  219. {
  220. continue;
  221. }
  222. allFiles.push_back((*si)->GetFullPath());
  223. }
  224. }
  225. default: // intended fallthrough
  226. break;
  227. }
  228. }
  229. }
  230. }
  231. void cmExtraSublimeTextGenerator::
  232. GetFileStringAndFolderSet(const std::vector<cmLocalGenerator*>& lgs,
  233. const cmMakefile* mf,
  234. const std::vector<std::string>& allFiles,
  235. std::stringstream& fileIncludePatternsStream,
  236. std::set<std::string>& folderIncludePatternsSet)
  237. {
  238. // Convert
  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->GetStartOutputDirectory();
  452. makefileName += "/Makefile";
  453. if (!firstTarget)
  454. {
  455. fout << ",\n\t";
  456. }
  457. fout << "\t{\n\t\t\t\"name\": \"" << makefile->GetProjectName() << " - " <<
  458. targetName << "\",\n";
  459. fout << "\t\t\t\"cmd\": [" <<
  460. this->BuildMakeCommand(make, makefileName.c_str(), targetName) <<
  461. "],\n";
  462. fout << "\t\t\t\"working_dir\": \"${project_path}\",\n";
  463. fout << "\t\t\t\"file_regex\": \"^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$\"\n";
  464. fout << "\t\t}";
  465. }
  466. // Create the command line for building the given target using the selected
  467. // make
  468. std::string cmExtraSublimeTextGenerator::BuildMakeCommand(
  469. const std::string& make, const char* makefile, const char* target)
  470. {
  471. std::string command = "\"";
  472. command += make + "\"";
  473. if (strcmp(this->GlobalGenerator->GetName(), "NMake Makefiles")==0)
  474. {
  475. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  476. command += ", \"/NOLOGO\", \"/f\", \"";
  477. command += makefileName + "\"";
  478. command += ", \"VERBOSE=1\", \"";
  479. command += target;
  480. command += "\"";
  481. }
  482. else if (strcmp(this->GlobalGenerator->GetName(), "MinGW Makefiles")==0)
  483. {
  484. // no escaping of spaces in this case, see
  485. // http://public.kitware.com/Bug/view.php?id=10014
  486. std::string makefileName = makefile;
  487. command += ", \"-f\", \"";
  488. command += makefileName + "\"";
  489. command += ", \"VERBOSE=1\", \"";
  490. command += target;
  491. command += "\"";
  492. }
  493. else
  494. {
  495. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  496. command += ", \"-f\", \"";
  497. command += makefileName + "\"";
  498. command += ", \"VERBOSE=1\", \"";
  499. command += target;
  500. command += "\"";
  501. }
  502. return command;
  503. }