cmake/Source/cmExtraSublimeTextGenerator.cxx

464 lines
16 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2013-03-16 19:13:01 +02:00
#include "cmExtraSublimeTextGenerator.h"
2016-07-09 11:21:54 +02:00
2017-07-20 19:35:53 +02:00
#include "cmsys/RegularExpression.hxx"
2017-04-14 19:02:05 +02:00
#include <set>
#include <sstream>
#include <string.h>
#include <utility>
2013-03-16 19:13:01 +02:00
#include "cmGeneratedFileStream.h"
2017-04-14 19:02:05 +02:00
#include "cmGeneratorExpression.h"
2013-03-16 19:13:01 +02:00
#include "cmGeneratorTarget.h"
2016-10-30 18:24:19 +01:00
#include "cmGlobalGenerator.h"
2013-03-16 19:13:01 +02:00
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
#include "cmSourceFile.h"
2017-04-14 19:02:05 +02:00
#include "cmStateTypes.h"
2013-03-16 19:13:01 +02:00
#include "cmSystemTools.h"
2017-04-14 19:02:05 +02:00
#include "cmake.h"
2013-03-16 19:13:01 +02:00
/*
Sublime Text 2 Generator
Author: Morné Chamberlain
This generator was initially based off of the CodeBlocks generator.
Some useful URLs:
Homepage:
http://www.sublimetext.com/
File format docs:
http://www.sublimetext.com/docs/2/projects.html
http://sublimetext.info/docs/en/reference/build_systems.html
*/
2016-10-30 18:24:19 +01:00
cmExternalMakefileProjectGeneratorFactory*
cmExtraSublimeTextGenerator::GetFactory()
2013-03-16 19:13:01 +02:00
{
2016-10-30 18:24:19 +01:00
static cmExternalMakefileProjectGeneratorSimpleFactory<
cmExtraSublimeTextGenerator>
factory("Sublime Text 2", "Generates Sublime Text 2 project files.");
if (factory.GetSupportedGlobalGenerators().empty()) {
#if defined(_WIN32)
factory.AddSupportedGlobalGenerator("MinGW Makefiles");
factory.AddSupportedGlobalGenerator("NMake Makefiles");
// disable until somebody actually tests it:
// factory.AddSupportedGlobalGenerator("MSYS Makefiles");
#endif
factory.AddSupportedGlobalGenerator("Ninja");
factory.AddSupportedGlobalGenerator("Unix Makefiles");
}
return &factory;
2013-03-16 19:13:01 +02:00
}
cmExtraSublimeTextGenerator::cmExtraSublimeTextGenerator()
2016-07-09 11:21:54 +02:00
: cmExternalMakefileProjectGenerator()
2013-03-16 19:13:01 +02:00
{
2017-04-14 19:02:05 +02:00
this->ExcludeBuildFolder = false;
2013-03-16 19:13:01 +02:00
}
void cmExtraSublimeTextGenerator::Generate()
{
2017-04-14 19:02:05 +02:00
this->ExcludeBuildFolder = this->GlobalGenerator->GlobalSettingIsOn(
"CMAKE_SUBLIME_TEXT_2_EXCLUDE_BUILD_TREE");
this->EnvSettings = this->GlobalGenerator->GetSafeGlobalSetting(
"CMAKE_SUBLIME_TEXT_2_ENV_SETTINGS");
2013-03-16 19:13:01 +02:00
// for each sub project in the project create a sublime text 2 project
2018-01-26 17:06:56 +01:00
for (auto const& it : this->GlobalGenerator->GetProjectMap()) {
2013-03-16 19:13:01 +02:00
// create a project file
2018-01-26 17:06:56 +01:00
this->CreateProjectFile(it.second);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
}
void cmExtraSublimeTextGenerator::CreateProjectFile(
2016-07-09 11:21:54 +02:00
const std::vector<cmLocalGenerator*>& lgs)
2013-03-16 19:13:01 +02:00
{
2016-07-09 11:21:54 +02:00
std::string outputDir = lgs[0]->GetCurrentBinaryDirectory();
std::string projectName = lgs[0]->GetProjectName();
2013-03-16 19:13:01 +02:00
const std::string filename =
2016-07-09 11:21:54 +02:00
outputDir + "/" + projectName + ".sublime-project";
2013-03-16 19:13:01 +02:00
this->CreateNewProjectFile(lgs, filename);
}
2016-07-09 11:21:54 +02:00
void cmExtraSublimeTextGenerator::CreateNewProjectFile(
const std::vector<cmLocalGenerator*>& lgs, const std::string& filename)
2013-03-16 19:13:01 +02:00
{
2016-07-09 11:21:54 +02:00
const cmMakefile* mf = lgs[0]->GetMakefile();
2017-04-14 19:02:05 +02:00
2013-03-16 19:13:01 +02:00
cmGeneratedFileStream fout(filename.c_str());
2016-07-09 11:21:54 +02:00
if (!fout) {
2013-03-16 19:13:01 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
const std::string& sourceRootRelativeToOutput = cmSystemTools::RelativePath(
lgs[0]->GetBinaryDirectory(), lgs[0]->GetSourceDirectory());
2013-03-16 19:13:01 +02:00
// Write the folder entries to the project file
fout << "{\n";
fout << "\t\"folders\":\n\t[\n\t";
2016-07-09 11:21:54 +02:00
if (!sourceRootRelativeToOutput.empty()) {
fout << "\t{\n\t\t\t\"path\": \"" << sourceRootRelativeToOutput << "\"";
const std::string& outputRelativeToSourceRoot =
cmSystemTools::RelativePath(lgs[0]->GetSourceDirectory(),
lgs[0]->GetBinaryDirectory());
if ((!outputRelativeToSourceRoot.empty()) &&
2013-03-16 19:13:01 +02:00
((outputRelativeToSourceRoot.length() < 3) ||
2016-07-09 11:21:54 +02:00
(outputRelativeToSourceRoot.substr(0, 3) != "../"))) {
2017-04-14 19:02:05 +02:00
if (this->ExcludeBuildFolder) {
fout << ",\n\t\t\t\"folder_exclude_patterns\": [\""
<< outputRelativeToSourceRoot << "\"]";
}
2013-03-16 19:13:01 +02:00
}
2016-07-09 11:21:54 +02:00
} else {
fout << "\t{\n\t\t\t\"path\": \"./\"";
}
2013-03-16 19:13:01 +02:00
fout << "\n\t\t}";
// End of the folders section
fout << "\n\t]";
// Write the beginning of the build systems section to the project file
fout << ",\n\t\"build_systems\":\n\t[\n\t";
// Set of include directories over all targets (sublime text/sublimeclang
// doesn't currently support these settings per build system, only project
// wide
MapSourceFileFlags sourceFileFlags;
AppendAllTargets(lgs, mf, fout, sourceFileFlags);
// End of build_systems
fout << "\n\t]";
2017-04-14 19:02:05 +02:00
std::string systemName = mf->GetSafeDefinition("CMAKE_SYSTEM_NAME");
std::vector<std::string> tokens;
cmSystemTools::ExpandListArgument(this->EnvSettings, tokens);
if (!this->EnvSettings.empty()) {
fout << ",";
fout << "\n\t\"env\":";
fout << "\n\t{";
fout << "\n\t\t" << systemName << ":";
fout << "\n\t\t{";
2018-01-26 17:06:56 +01:00
for (std::string const& t : tokens) {
size_t const pos = t.find_first_of('=');
2017-04-14 19:02:05 +02:00
if (pos != std::string::npos) {
2018-01-26 17:06:56 +01:00
std::string varName = t.substr(0, pos);
std::string varValue = t.substr(pos + 1);
2017-04-14 19:02:05 +02:00
fout << "\n\t\t\t\"" << varName << "\":\"" << varValue << "\"";
} else {
std::ostringstream e;
e << "Could not parse Env Vars specified in "
"\"CMAKE_SUBLIME_TEXT_2_ENV_SETTINGS\""
2018-01-26 17:06:56 +01:00
<< ", corrupted string " << t;
2017-04-14 19:02:05 +02:00
mf->IssueMessage(cmake::FATAL_ERROR, e.str());
}
}
fout << "\n\t\t}";
fout << "\n\t}";
}
fout << "\n}";
2013-03-16 19:13:01 +02:00
}
2016-07-09 11:21:54 +02:00
void cmExtraSublimeTextGenerator::AppendAllTargets(
const std::vector<cmLocalGenerator*>& lgs, const cmMakefile* mf,
cmGeneratedFileStream& fout, MapSourceFileFlags& sourceFileFlags)
2013-03-16 19:13:01 +02:00
{
std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
2017-04-14 19:02:05 +02:00
std::string compiler;
2016-07-09 11:21:54 +02:00
if (!lgs.empty()) {
2018-01-26 17:06:56 +01:00
this->AppendTarget(fout, "all", lgs[0], nullptr, make.c_str(), mf,
2016-07-09 11:21:54 +02:00
compiler.c_str(), sourceFileFlags, true);
2018-01-26 17:06:56 +01:00
this->AppendTarget(fout, "clean", lgs[0], nullptr, make.c_str(), mf,
2016-07-09 11:21:54 +02:00
compiler.c_str(), sourceFileFlags, false);
}
2013-03-16 19:13:01 +02:00
// add all executable and library targets and some of the GLOBAL
// and UTILITY targets
2018-01-26 17:06:56 +01:00
for (cmLocalGenerator* lg : lgs) {
cmMakefile* makefile = lg->GetMakefile();
const std::vector<cmGeneratorTarget*>& targets = lg->GetGeneratorTargets();
for (cmGeneratorTarget* target : targets) {
std::string targetName = target->GetName();
switch (target->GetType()) {
2017-04-14 19:02:05 +02:00
case cmStateEnums::GLOBAL_TARGET: {
2013-03-16 19:13:01 +02:00
// Only add the global targets from CMAKE_BINARY_DIR,
// not from the subdirs
2018-04-23 21:13:27 +02:00
if (lg->GetCurrentBinaryDirectory() == lg->GetBinaryDirectory()) {
2018-01-26 17:06:56 +01:00
this->AppendTarget(fout, targetName, lg, nullptr, make.c_str(),
2016-07-09 11:21:54 +02:00
makefile, compiler.c_str(), sourceFileFlags,
false);
2013-03-16 19:13:01 +02:00
}
2016-07-09 11:21:54 +02:00
} break;
2017-04-14 19:02:05 +02:00
case cmStateEnums::UTILITY:
2013-03-16 19:13:01 +02:00
// Add all utility targets, except the Nightly/Continuous/
// Experimental-"sub"targets as e.g. NightlyStart
2016-07-09 11:21:54 +02:00
if (((targetName.find("Nightly") == 0) &&
(targetName != "Nightly")) ||
((targetName.find("Continuous") == 0) &&
(targetName != "Continuous")) ||
((targetName.find("Experimental") == 0) &&
(targetName != "Experimental"))) {
2013-03-16 19:13:01 +02:00
break;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2018-01-26 17:06:56 +01:00
this->AppendTarget(fout, targetName, lg, nullptr, make.c_str(),
2016-10-30 18:24:19 +01:00
makefile, compiler.c_str(), sourceFileFlags,
false);
2013-03-16 19:13:01 +02:00
break;
2017-04-14 19:02:05 +02:00
case cmStateEnums::EXECUTABLE:
case cmStateEnums::STATIC_LIBRARY:
case cmStateEnums::SHARED_LIBRARY:
case cmStateEnums::MODULE_LIBRARY:
case cmStateEnums::OBJECT_LIBRARY: {
2018-01-26 17:06:56 +01:00
this->AppendTarget(fout, targetName, lg, target, make.c_str(),
2016-07-09 11:21:54 +02:00
makefile, compiler.c_str(), sourceFileFlags,
false);
2016-03-13 13:35:51 +01:00
std::string fastTarget = targetName;
2013-03-16 19:13:01 +02:00
fastTarget += "/fast";
2018-01-26 17:06:56 +01:00
this->AppendTarget(fout, fastTarget, lg, target, make.c_str(),
2016-07-09 11:21:54 +02:00
makefile, compiler.c_str(), sourceFileFlags,
false);
} break;
2013-03-16 19:13:01 +02:00
default:
break;
}
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
}
2016-07-09 11:21:54 +02:00
void cmExtraSublimeTextGenerator::AppendTarget(
cmGeneratedFileStream& fout, const std::string& targetName,
cmLocalGenerator* lg, cmGeneratorTarget* target, const char* make,
2016-10-30 18:24:19 +01:00
const cmMakefile* makefile, const char* /*compiler*/,
2016-07-09 11:21:54 +02:00
MapSourceFileFlags& sourceFileFlags, bool firstTarget)
2013-03-16 19:13:01 +02:00
{
2018-01-26 17:06:56 +01:00
if (target != nullptr) {
2016-07-09 11:21:54 +02:00
std::vector<cmSourceFile*> sourceFiles;
target->GetSourceFiles(sourceFiles,
makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
2018-01-26 17:06:56 +01:00
for (cmSourceFile* sourceFile : sourceFiles) {
2016-07-09 11:21:54 +02:00
MapSourceFileFlags::iterator sourceFileFlagsIter =
sourceFileFlags.find(sourceFile->GetFullPath());
if (sourceFileFlagsIter == sourceFileFlags.end()) {
sourceFileFlagsIter =
sourceFileFlags
.insert(MapSourceFileFlags::value_type(sourceFile->GetFullPath(),
std::vector<std::string>()))
.first;
}
std::vector<std::string>& flags = sourceFileFlagsIter->second;
2018-01-26 17:06:56 +01:00
std::string flagsString =
this->ComputeFlagsForObject(sourceFile, lg, target);
std::string definesString = this->ComputeDefines(sourceFile, lg, target);
2018-04-23 21:13:27 +02:00
std::string includesString =
this->ComputeIncludes(sourceFile, lg, target);
2016-07-09 11:21:54 +02:00
flags.clear();
cmsys::RegularExpression flagRegex;
// Regular expression to extract compiler flags from a string
// https://gist.github.com/3944250
const char* regexString =
"(^|[ ])-[DIOUWfgs][^= ]+(=\\\"[^\"]+\\\"|=[^\"][^ ]+)?";
flagRegex.compile(regexString);
2018-04-23 21:13:27 +02:00
std::string workString =
flagsString + " " + definesString + " " + includesString;
2016-07-09 11:21:54 +02:00
while (flagRegex.find(workString)) {
std::string::size_type start = flagRegex.start();
if (workString[start] == ' ') {
start++;
}
flags.push_back(workString.substr(start, flagRegex.end() - start));
if (flagRegex.end() < workString.size()) {
workString = workString.substr(flagRegex.end());
} else {
2018-01-26 17:06:56 +01:00
workString.clear();
2013-03-16 19:13:01 +02:00
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
// Ninja uses ninja.build files (look for a way to get the output file name
// from cmMakefile or something)
std::string makefileName;
2016-07-09 11:21:54 +02:00
if (this->GlobalGenerator->GetName() == "Ninja") {
makefileName = "build.ninja";
} else {
makefileName = "Makefile";
}
if (!firstTarget) {
2013-03-16 19:13:01 +02:00
fout << ",\n\t";
2016-07-09 11:21:54 +02:00
}
fout << "\t{\n\t\t\t\"name\": \"" << lg->GetProjectName() << " - "
<< targetName << "\",\n";
fout << "\t\t\t\"cmd\": ["
<< this->BuildMakeCommand(make, makefileName.c_str(), targetName)
<< "],\n";
2013-03-16 19:13:01 +02:00
fout << "\t\t\t\"working_dir\": \"${project_path}\",\n";
2017-04-14 19:02:05 +02:00
fout << "\t\t\t\"file_regex\": \""
"^(..[^:]*)(?::|\\\\()([0-9]+)(?::|\\\\))(?:([0-9]+):)?\\\\s*(.*)"
"\"\n";
2013-03-16 19:13:01 +02:00
fout << "\t\t}";
}
// Create the command line for building the given target using the selected
// make
std::string cmExtraSublimeTextGenerator::BuildMakeCommand(
2016-07-09 11:21:54 +02:00
const std::string& make, const char* makefile, const std::string& target)
2013-03-16 19:13:01 +02:00
{
std::string command = "\"";
command += make + "\"";
2015-04-27 22:25:09 +02:00
std::string generator = this->GlobalGenerator->GetName();
2016-07-09 11:21:54 +02:00
if (generator == "NMake Makefiles") {
2013-03-16 19:13:01 +02:00
std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
command += ", \"/NOLOGO\", \"/f\", \"";
command += makefileName + "\"";
2017-04-14 19:02:05 +02:00
command += ", \"" + target + "\"";
2016-07-09 11:21:54 +02:00
} else if (generator == "Ninja") {
2013-03-16 19:13:01 +02:00
std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
command += ", \"-f\", \"";
command += makefileName + "\"";
2017-04-14 19:02:05 +02:00
command += ", \"" + target + "\"";
2016-07-09 11:21:54 +02:00
} else {
2013-03-16 19:13:01 +02:00
std::string makefileName;
2016-07-09 11:21:54 +02:00
if (generator == "MinGW Makefiles") {
// no escaping of spaces in this case, see
2016-10-30 18:24:19 +01:00
// https://gitlab.kitware.com/cmake/cmake/issues/10014
2016-07-09 11:21:54 +02:00
makefileName = makefile;
} else {
makefileName = cmSystemTools::ConvertToOutputPath(makefile);
}
2013-03-16 19:13:01 +02:00
command += ", \"-f\", \"";
command += makefileName + "\"";
2017-04-14 19:02:05 +02:00
command += ", \"" + target + "\"";
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return command;
}
// TODO: Most of the code is picked up from the Ninja generator, refactor it.
2016-07-09 11:21:54 +02:00
std::string cmExtraSublimeTextGenerator::ComputeFlagsForObject(
cmSourceFile* source, cmLocalGenerator* lg, cmGeneratorTarget* gtgt)
2013-03-16 19:13:01 +02:00
{
std::string flags;
2015-04-27 22:25:09 +02:00
std::string language = source->GetLanguage();
2016-07-09 11:21:54 +02:00
if (language.empty()) {
language = "C";
}
2016-10-30 18:24:19 +01:00
std::string const& config =
lg->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE");
2013-03-16 19:13:01 +02:00
2016-10-30 18:24:19 +01:00
lg->GetTargetCompileFlags(gtgt, config, language, flags);
2013-03-16 19:13:01 +02:00
2018-04-23 21:13:27 +02:00
// Add source file specific flags.
cmGeneratorExpressionInterpreter genexInterpreter(lg, gtgt, config,
gtgt->GetName(), language);
const std::string COMPILE_FLAGS("COMPILE_FLAGS");
if (const char* cflags = source->GetProperty(COMPILE_FLAGS)) {
lg->AppendFlags(flags, genexInterpreter.Evaluate(cflags, COMPILE_FLAGS));
2013-03-16 19:13:01 +02:00
}
2018-04-23 21:13:27 +02:00
const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
if (const char* coptions = source->GetProperty(COMPILE_OPTIONS)) {
lg->AppendCompileOptions(
flags, genexInterpreter.Evaluate(coptions, COMPILE_OPTIONS));
2017-04-14 19:02:05 +02:00
}
2013-03-16 19:13:01 +02:00
return flags;
}
// TODO: Refactor with
// void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
2016-07-09 11:21:54 +02:00
std::string cmExtraSublimeTextGenerator::ComputeDefines(
cmSourceFile* source, cmLocalGenerator* lg, cmGeneratorTarget* target)
2013-03-16 19:13:01 +02:00
{
std::set<std::string> defines;
2016-07-09 11:21:54 +02:00
cmMakefile* makefile = lg->GetMakefile();
2015-04-27 22:25:09 +02:00
const std::string& language = source->GetLanguage();
const std::string& config = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
2018-04-23 21:13:27 +02:00
cmGeneratorExpressionInterpreter genexInterpreter(
lg, target, config, target->GetName(), language);
2013-03-16 19:13:01 +02:00
// Add the export symbol definition for shared library objects.
2016-07-09 11:21:54 +02:00
if (const char* exportMacro = target->GetExportMacro()) {
2013-03-16 19:13:01 +02:00
lg->AppendDefines(defines, exportMacro);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
// Add preprocessor definitions for this target and configuration.
2015-08-17 11:37:30 +02:00
lg->AddCompileDefinitions(defines, target, config, language);
2018-04-23 21:13:27 +02:00
const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
if (const char* compile_defs = source->GetProperty(COMPILE_DEFINITIONS)) {
lg->AppendDefines(
defines, genexInterpreter.Evaluate(compile_defs, COMPILE_DEFINITIONS));
}
std::string defPropName = "COMPILE_DEFINITIONS_";
defPropName += cmSystemTools::UpperCase(config);
if (const char* config_compile_defs = source->GetProperty(defPropName)) {
2018-08-09 18:06:22 +02:00
lg->AppendDefines(
defines,
genexInterpreter.Evaluate(config_compile_defs, COMPILE_DEFINITIONS));
2013-03-16 19:13:01 +02:00
}
std::string definesString;
lg->JoinDefines(defines, definesString, language);
return definesString;
}
2018-04-23 21:13:27 +02:00
std::string cmExtraSublimeTextGenerator::ComputeIncludes(
cmSourceFile* source, cmLocalGenerator* lg, cmGeneratorTarget* target)
{
std::vector<std::string> includes;
cmMakefile* makefile = lg->GetMakefile();
const std::string& language = source->GetLanguage();
const std::string& config = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
cmGeneratorExpressionInterpreter genexInterpreter(
lg, target, config, target->GetName(), language);
// Add include directories for this source file
const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
if (const char* cincludes = source->GetProperty(INCLUDE_DIRECTORIES)) {
lg->AppendIncludeDirectories(
includes, genexInterpreter.Evaluate(cincludes, INCLUDE_DIRECTORIES),
*source);
}
// Add include directory flags.
lg->GetIncludeDirectories(includes, target, language, config);
std::string includesString =
lg->GetIncludeFlags(includes, target, language, true, false, config);
return includesString;
}
bool cmExtraSublimeTextGenerator::Open(const std::string& bindir,
const std::string& projectName,
bool dryRun)
{
const char* sublExecutable =
this->GlobalGenerator->GetCMakeInstance()->GetCacheDefinition(
"CMAKE_SUBLIMETEXT_EXECUTABLE");
if (!sublExecutable) {
return false;
}
if (cmSystemTools::IsNOTFOUND(sublExecutable)) {
return false;
}
std::string filename = bindir + "/" + projectName + ".sublime-project";
if (dryRun) {
return cmSystemTools::FileExists(filename, true);
}
return cmSystemTools::RunSingleCommand(
{ sublExecutable, "--project", filename });
}