cmake/Source/cmExtraEclipseCDT4Generator.cxx

1211 lines
42 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. */
#include "cmExtraEclipseCDT4Generator.h"
2016-07-09 11:21:54 +02:00
2017-04-14 19:02:05 +02:00
#include <algorithm>
2020-02-01 23:06:01 +01:00
#include <cassert>
#include <cstdio>
2019-11-11 23:01:05 +01:00
#include <map>
2020-08-30 11:54:41 +02:00
#include <memory>
2017-04-14 19:02:05 +02:00
#include <sstream>
#include <utility>
2020-02-01 23:06:01 +01:00
#include "cmsys/RegularExpression.hxx"
2016-07-09 11:21:54 +02:00
#include "cmGeneratedFileStream.h"
2016-10-30 18:24:19 +01:00
#include "cmGeneratorExpression.h"
#include "cmGeneratorTarget.h"
#include "cmGlobalGenerator.h"
2023-07-02 19:51:09 +02:00
#include "cmList.h"
2016-10-30 18:24:19 +01:00
#include "cmLocalGenerator.h"
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2016-07-09 11:21:54 +02:00
#include "cmSourceFile.h"
2016-10-30 18:24:19 +01:00
#include "cmSourceGroup.h"
2015-08-17 11:37:30 +02:00
#include "cmState.h"
2017-04-14 19:02:05 +02:00
#include "cmStateTypes.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2016-10-30 18:24:19 +01:00
#include "cmSystemTools.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2016-07-09 11:21:54 +02:00
#include "cmXMLWriter.h"
2016-10-30 18:24:19 +01:00
#include "cmake.h"
2016-07-09 11:21:54 +02:00
static void AppendAttribute(cmXMLWriter& xml, const char* keyval)
{
xml.StartElement("attribute");
xml.Attribute("key", keyval);
xml.Attribute("value", keyval);
xml.EndElement();
}
template <typename T>
void AppendDictionary(cmXMLWriter& xml, const char* key, T const& value)
{
xml.StartElement("dictionary");
xml.Element("key", key);
xml.Element("value", value);
xml.EndElement();
}
cmExtraEclipseCDT4Generator::cmExtraEclipseCDT4Generator()
{
2020-02-01 23:06:01 +01:00
this->IsOutOfSourceBuild = false;
this->GenerateSourceProject = false;
2012-02-18 12:40:36 +02:00
this->SupportsVirtualFolders = true;
2013-03-16 19:13:01 +02:00
this->GenerateLinkedResources = true;
2013-11-03 12:27:13 +02:00
this->SupportsGmakeErrorParser = true;
2014-08-03 19:52:23 +02:00
this->SupportsMachO64Parser = true;
2016-07-09 11:21:54 +02:00
this->CEnabled = false;
this->CXXEnabled = false;
}
2016-10-30 18:24:19 +01:00
cmExternalMakefileProjectGeneratorFactory*
cmExtraEclipseCDT4Generator::GetFactory()
{
2016-10-30 18:24:19 +01:00
static cmExternalMakefileProjectGeneratorSimpleFactory<
cmExtraEclipseCDT4Generator>
2023-07-02 19:51:09 +02:00
factory("Eclipse CDT4",
"Generates Eclipse CDT 4.0 project files (deprecated).");
2016-10-30 18:24:19 +01:00
if (factory.GetSupportedGlobalGenerators().empty()) {
// TODO: Verify if __CYGWIN__ should be checked.
2023-05-23 16:38:00 +02:00
// #if defined(_WIN32) && !defined(__CYGWIN__)
2016-10-30 18:24:19 +01:00
#if defined(_WIN32)
factory.AddSupportedGlobalGenerator("NMake Makefiles");
factory.AddSupportedGlobalGenerator("MinGW Makefiles");
// factory.AddSupportedGlobalGenerator("MSYS Makefiles");
#endif
factory.AddSupportedGlobalGenerator("Ninja");
factory.AddSupportedGlobalGenerator("Unix Makefiles");
}
return &factory;
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
void cmExtraEclipseCDT4Generator::EnableLanguage(
2016-10-30 18:24:19 +01:00
std::vector<std::string> const& languages, cmMakefile* /*unused*/,
bool /*optional*/)
2014-08-03 19:52:23 +02:00
{
2018-01-26 17:06:56 +01:00
for (std::string const& l : languages) {
if (l == "CXX") {
2014-08-03 19:52:23 +02:00
this->Natures.insert("org.eclipse.cdt.core.ccnature");
2015-04-27 22:25:09 +02:00
this->Natures.insert("org.eclipse.cdt.core.cnature");
2016-07-09 11:21:54 +02:00
this->CXXEnabled = true;
2018-01-26 17:06:56 +01:00
} else if (l == "C") {
2014-08-03 19:52:23 +02:00
this->Natures.insert("org.eclipse.cdt.core.cnature");
2016-07-09 11:21:54 +02:00
this->CEnabled = true;
2018-01-26 17:06:56 +01:00
} else if (l == "Java") {
2014-08-03 19:52:23 +02:00
this->Natures.insert("org.eclipse.jdt.core.javanature");
}
2016-07-09 11:21:54 +02:00
}
}
void cmExtraEclipseCDT4Generator::Generate()
{
2020-08-30 11:54:41 +02:00
const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
2016-03-13 13:35:51 +01:00
const cmMakefile* mf = lg->GetMakefile();
2012-02-18 12:40:36 +02:00
std::string eclipseVersion = mf->GetSafeDefinition("CMAKE_ECLIPSE_VERSION");
cmsys::RegularExpression regex(".*([0-9]+\\.[0-9]+).*");
2018-10-28 12:09:07 +01:00
if (regex.find(eclipseVersion)) {
2012-02-18 12:40:36 +02:00
unsigned int majorVersion = 0;
unsigned int minorVersion = 0;
2016-07-09 11:21:54 +02:00
int res =
sscanf(regex.match(1).c_str(), "%u.%u", &majorVersion, &minorVersion);
if (res == 2) {
2012-02-18 12:40:36 +02:00
int version = majorVersion * 1000 + minorVersion;
if (version < 3006) // 3.6 is Helios
2016-07-09 11:21:54 +02:00
{
2012-02-18 12:40:36 +02:00
this->SupportsVirtualFolders = false;
2014-08-03 19:52:23 +02:00
this->SupportsMachO64Parser = false;
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
if (version < 3007) // 3.7 is Indigo
2016-07-09 11:21:54 +02:00
{
2013-11-03 12:27:13 +02:00
this->SupportsGmakeErrorParser = false;
2012-02-18 12:40:36 +02:00
}
}
2016-07-09 11:21:54 +02:00
}
2012-02-18 12:40:36 +02:00
// TODO: Decide if these are local or member variables
2016-07-09 11:21:54 +02:00
this->HomeDirectory = lg->GetSourceDirectory();
2016-03-13 13:35:51 +01:00
this->HomeOutputDirectory = lg->GetBinaryDirectory();
2016-07-09 11:21:54 +02:00
this->GenerateLinkedResources =
mf->IsOn("CMAKE_ECLIPSE_GENERATE_LINKED_RESOURCES");
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
this->IsOutOfSourceBuild =
(this->HomeDirectory != this->HomeOutputDirectory);
2016-07-09 11:21:54 +02:00
this->GenerateSourceProject =
(this->IsOutOfSourceBuild &&
mf->IsOn("CMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT"));
2012-02-18 12:40:36 +02:00
2016-10-30 18:24:19 +01:00
if (!this->GenerateSourceProject &&
2016-07-09 11:21:54 +02:00
(mf->IsOn("ECLIPSE_CDT4_GENERATE_SOURCE_PROJECT"))) {
mf->IssueMessage(
2019-11-11 23:01:05 +01:00
MessageType::WARNING,
2016-07-09 11:21:54 +02:00
"ECLIPSE_CDT4_GENERATE_SOURCE_PROJECT is set to TRUE, "
"but this variable is not supported anymore since CMake 2.8.7.\n"
"Enable CMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT instead.");
}
2012-02-18 12:40:36 +02:00
2015-04-27 22:25:09 +02:00
if (cmSystemTools::IsSubDirectory(this->HomeOutputDirectory,
2016-07-09 11:21:54 +02:00
this->HomeDirectory)) {
2019-11-11 23:01:05 +01:00
mf->IssueMessage(MessageType::WARNING,
2016-07-09 11:21:54 +02:00
"The build directory is a subdirectory "
2012-02-18 12:40:36 +02:00
"of the source directory.\n"
"This is not supported well by Eclipse. It is strongly "
"recommended to use a build directory which is a "
"sibling of the source directory.");
2016-07-09 11:21:54 +02:00
}
// NOTE: This is not good, since it pollutes the source tree. However,
// Eclipse doesn't allow CVS/SVN to work when the .project is not in
// the cvs/svn root directory. Hence, this is provided as an option.
2016-07-09 11:21:54 +02:00
if (this->GenerateSourceProject) {
// create .project file in the source tree
this->CreateSourceProjectFile();
2016-07-09 11:21:54 +02:00
}
// create a .project file
this->CreateProjectFile();
// create a .cproject file
this->CreateCProjectFile();
2020-02-01 23:06:01 +01:00
// create resource settings
this->CreateSettingsResourcePrefsFile();
}
void cmExtraEclipseCDT4Generator::CreateSettingsResourcePrefsFile()
{
2020-08-30 11:54:41 +02:00
const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
2020-02-01 23:06:01 +01:00
cmMakefile* mf = lg->GetMakefile();
const std::string filename =
this->HomeOutputDirectory + "/.settings/org.eclipse.core.resources.prefs";
cmGeneratedFileStream fout(filename);
if (!fout) {
return;
}
2020-08-30 11:54:41 +02:00
fout << "eclipse.preferences.version=1\n";
2021-11-20 13:41:27 +01:00
cmValue encoding = mf->GetDefinition("CMAKE_ECLIPSE_RESOURCE_ENCODING");
2020-02-01 23:06:01 +01:00
if (encoding) {
2021-09-14 00:13:48 +02:00
fout << "encoding/<project>=" << *encoding << '\n';
2020-02-01 23:06:01 +01:00
}
}
2012-02-18 12:40:36 +02:00
void cmExtraEclipseCDT4Generator::CreateSourceProjectFile()
{
assert(this->HomeDirectory != this->HomeOutputDirectory);
// set up the project name: <project>-Source@<baseSourcePathName>
2020-08-30 11:54:41 +02:00
const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
2019-11-11 23:01:05 +01:00
std::string name = cmExtraEclipseCDT4Generator::GenerateProjectName(
lg->GetProjectName(), "Source",
cmExtraEclipseCDT4Generator::GetPathBasename(this->HomeDirectory));
const std::string filename = this->HomeDirectory + "/.project";
2018-10-28 12:09:07 +01:00
cmGeneratedFileStream fout(filename);
2016-07-09 11:21:54 +02:00
if (!fout) {
return;
2016-07-09 11:21:54 +02:00
}
cmXMLWriter xml(fout);
xml.StartDocument("UTF-8");
xml.StartElement("projectDescription");
xml.Element("name", name);
xml.Element("comment", "");
xml.Element("projects", "");
xml.Element("buildSpec", "");
xml.Element("natures", "");
xml.StartElement("linkedResources");
if (this->SupportsVirtualFolders) {
this->CreateLinksToSubprojects(xml, this->HomeDirectory);
2012-02-18 12:40:36 +02:00
this->SrcLinkedResources.clear();
2016-07-09 11:21:54 +02:00
}
2012-02-18 12:40:36 +02:00
2016-07-09 11:21:54 +02:00
xml.EndElement(); // linkedResources
xml.EndElement(); // projectDescription
xml.EndDocument();
}
2016-07-09 11:21:54 +02:00
void cmExtraEclipseCDT4Generator::AddEnvVar(std::ostream& out,
2016-03-13 13:35:51 +01:00
const char* envVar,
2020-08-30 11:54:41 +02:00
cmLocalGenerator& lg)
2010-03-17 14:00:29 +02:00
{
2020-08-30 11:54:41 +02:00
cmMakefile* mf = lg.GetMakefile();
2016-03-13 13:35:51 +01:00
2011-01-16 11:35:12 +01:00
// get the variables from the environment and from the cache and then
2010-03-17 14:00:29 +02:00
// figure out which one to use:
2016-10-30 18:24:19 +01:00
std::string envVarValue;
const bool envVarSet = cmSystemTools::GetEnv(envVar, envVarValue);
2010-03-17 14:00:29 +02:00
2020-02-01 23:06:01 +01:00
std::string cacheEntryName = cmStrCat("CMAKE_ECLIPSE_ENVVAR_", envVar);
2021-11-20 13:41:27 +01:00
cmValue cacheValue = lg.GetState()->GetInitializedCacheValue(cacheEntryName);
2010-03-17 14:00:29 +02:00
// now we have both, decide which one to use
std::string valueToUse;
2021-09-14 00:13:48 +02:00
if (!envVarSet && !cacheValue) {
2010-03-17 14:00:29 +02:00
// nothing known, do nothing
2018-01-26 17:06:56 +01:00
valueToUse.clear();
2021-09-14 00:13:48 +02:00
} else if (envVarSet && !cacheValue) {
2011-01-16 11:35:12 +01:00
// The variable is in the env, but not in the cache. Use it and put it
2010-03-17 14:00:29 +02:00
// in the cache
valueToUse = envVarValue;
2023-07-02 19:51:09 +02:00
mf->AddCacheDefinition(cacheEntryName, valueToUse, cacheEntryName,
2020-08-30 11:54:41 +02:00
cmStateEnums::STRING, true);
mf->GetCMakeInstance()->SaveCache(lg.GetBinaryDirectory());
2021-09-14 00:13:48 +02:00
} else if (!envVarSet && cacheValue) {
2010-03-17 14:00:29 +02:00
// It is already in the cache, but not in the env, so use it from the cache
2018-10-28 12:09:07 +01:00
valueToUse = *cacheValue;
2016-07-09 11:21:54 +02:00
} else {
2010-03-17 14:00:29 +02:00
// It is both in the cache and in the env.
// Use the version from the env. except if the value from the env is
// completely contained in the value from the cache (for the case that we
// now have a PATH without MSVC dirs in the env. but had the full PATH with
// all MSVC dirs during the cmake run which stored the var in the cache:
2018-10-28 12:09:07 +01:00
valueToUse = *cacheValue;
2016-07-09 11:21:54 +02:00
if (valueToUse.find(envVarValue) == std::string::npos) {
2010-03-17 14:00:29 +02:00
valueToUse = envVarValue;
2023-07-02 19:51:09 +02:00
mf->AddCacheDefinition(cacheEntryName, valueToUse, cacheEntryName,
cmStateEnums::STRING, true);
2020-08-30 11:54:41 +02:00
mf->GetCMakeInstance()->SaveCache(lg.GetBinaryDirectory());
2010-03-17 14:00:29 +02:00
}
2016-07-09 11:21:54 +02:00
}
2010-03-17 14:00:29 +02:00
2016-07-09 11:21:54 +02:00
if (!valueToUse.empty()) {
out << envVar << "=" << valueToUse << "|";
}
2010-03-17 14:00:29 +02:00
}
void cmExtraEclipseCDT4Generator::CreateProjectFile()
{
2020-08-30 11:54:41 +02:00
const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
2016-03-13 13:35:51 +01:00
cmMakefile* mf = lg->GetMakefile();
const std::string filename = this->HomeOutputDirectory + "/.project";
2018-10-28 12:09:07 +01:00
cmGeneratedFileStream fout(filename);
2016-07-09 11:21:54 +02:00
if (!fout) {
return;
2016-07-09 11:21:54 +02:00
}
2010-03-17 14:00:29 +02:00
std::string compilerId = mf->GetSafeDefinition("CMAKE_C_COMPILER_ID");
2016-07-09 11:21:54 +02:00
if (compilerId.empty()) // no C compiler, try the C++ compiler:
{
2010-03-17 14:00:29 +02:00
compilerId = mf->GetSafeDefinition("CMAKE_CXX_COMPILER_ID");
2016-07-09 11:21:54 +02:00
}
cmXMLWriter xml(fout);
xml.StartDocument("UTF-8");
xml.StartElement("projectDescription");
2010-03-17 14:00:29 +02:00
2018-08-09 18:06:22 +02:00
xml.Element("name",
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::GenerateProjectName(
2018-08-09 18:06:22 +02:00
lg->GetProjectName(),
mf->GetSafeDefinition("CMAKE_BUILD_TYPE"),
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::GetPathBasename(
this->HomeOutputDirectory)));
2016-07-09 11:21:54 +02:00
xml.Element("comment", "");
xml.Element("projects", "");
xml.StartElement("buildSpec");
xml.StartElement("buildCommand");
xml.Element("name", "org.eclipse.cdt.make.core.makeBuilder");
xml.Element("triggers", "clean,full,incremental,");
xml.StartElement("arguments");
// use clean target
2016-07-09 11:21:54 +02:00
AppendDictionary(xml, "org.eclipse.cdt.make.core.cleanBuildTarget", "clean");
AppendDictionary(xml, "org.eclipse.cdt.make.core.enableCleanBuild", "true");
AppendDictionary(xml, "org.eclipse.cdt.make.core.append_environment",
"true");
AppendDictionary(xml, "org.eclipse.cdt.make.core.stopOnError", "true");
// set the make command
2016-07-09 11:21:54 +02:00
AppendDictionary(xml, "org.eclipse.cdt.make.core.enabledIncrementalBuild",
"true");
2019-11-11 23:01:05 +01:00
AppendDictionary(xml, "org.eclipse.cdt.make.core.build.command",
cmExtraEclipseCDT4Generator::GetEclipsePath(
mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM")));
2016-07-09 11:21:54 +02:00
AppendDictionary(xml, "org.eclipse.cdt.make.core.contents",
"org.eclipse.cdt.make.core.activeConfigSettings");
AppendDictionary(xml, "org.eclipse.cdt.make.core.build.target.inc", "all");
AppendDictionary(xml, "org.eclipse.cdt.make.core.build.arguments",
mf->GetSafeDefinition("CMAKE_ECLIPSE_MAKE_ARGUMENTS"));
2019-11-11 23:01:05 +01:00
AppendDictionary(
xml, "org.eclipse.cdt.make.core.buildLocation",
cmExtraEclipseCDT4Generator::GetEclipsePath(this->HomeOutputDirectory));
2016-07-09 11:21:54 +02:00
AppendDictionary(xml, "org.eclipse.cdt.make.core.useDefaultBuildCmd",
"false");
// set project specific environment
2016-10-30 18:24:19 +01:00
std::ostringstream environment;
2016-07-09 11:21:54 +02:00
environment << "VERBOSE=1|CMAKE_NO_VERBOSE=1|"; // verbose Makefile output
// set vsvars32.bat environment available at CMake time,
// but not necessarily when eclipse is open
2016-07-09 11:21:54 +02:00
if (compilerId == "MSVC") {
2020-08-30 11:54:41 +02:00
AddEnvVar(environment, "PATH", *lg);
AddEnvVar(environment, "INCLUDE", *lg);
AddEnvVar(environment, "LIB", *lg);
AddEnvVar(environment, "LIBPATH", *lg);
2016-07-09 11:21:54 +02:00
} else if (compilerId == "Intel") {
2010-03-17 14:00:29 +02:00
// if the env.var is set, use this one and put it in the cache
2011-01-16 11:35:12 +01:00
// if the env.var is not set, but the value is in the cache,
2010-03-17 14:00:29 +02:00
// use it from the cache:
2020-08-30 11:54:41 +02:00
AddEnvVar(environment, "INTEL_LICENSE_FILE", *lg);
2016-07-09 11:21:54 +02:00
}
AppendDictionary(xml, "org.eclipse.cdt.make.core.environment",
environment.str());
AppendDictionary(xml, "org.eclipse.cdt.make.core.enableFullBuild", "true");
AppendDictionary(xml, "org.eclipse.cdt.make.core.build.target.auto", "all");
AppendDictionary(xml, "org.eclipse.cdt.make.core.enableAutoBuild", "false");
AppendDictionary(xml, "org.eclipse.cdt.make.core.build.target.clean",
"clean");
AppendDictionary(xml, "org.eclipse.cdt.make.core.fullBuildTarget", "all");
AppendDictionary(xml, "org.eclipse.cdt.make.core.buildArguments", "");
2019-11-11 23:01:05 +01:00
AppendDictionary(
xml, "org.eclipse.cdt.make.core.build.location",
cmExtraEclipseCDT4Generator::GetEclipsePath(this->HomeOutputDirectory));
2016-07-09 11:21:54 +02:00
AppendDictionary(xml, "org.eclipse.cdt.make.core.autoBuildTarget", "all");
// set error parsers
2016-10-30 18:24:19 +01:00
std::ostringstream errorOutputParser;
2016-07-09 11:21:54 +02:00
if (compilerId == "MSVC") {
errorOutputParser << "org.eclipse.cdt.core.VCErrorParser;";
} else if (compilerId == "Intel") {
errorOutputParser << "org.eclipse.cdt.core.ICCErrorParser;";
}
if (this->SupportsGmakeErrorParser) {
errorOutputParser << "org.eclipse.cdt.core.GmakeErrorParser;";
} else {
errorOutputParser << "org.eclipse.cdt.core.MakeErrorParser;";
}
errorOutputParser << "org.eclipse.cdt.core.GCCErrorParser;"
"org.eclipse.cdt.core.GASErrorParser;"
"org.eclipse.cdt.core.GLDErrorParser;";
AppendDictionary(xml, "org.eclipse.cdt.core.errorOutputParser",
errorOutputParser.str());
xml.EndElement(); // arguments
xml.EndElement(); // buildCommand
xml.StartElement("buildCommand");
xml.Element("name", "org.eclipse.cdt.make.core.ScannerConfigBuilder");
xml.StartElement("arguments");
xml.EndElement(); // arguments
xml.EndElement(); // buildCommand
xml.EndElement(); // buildSpec
// set natures for c/c++ projects
2016-07-09 11:21:54 +02:00
xml.StartElement("natures");
xml.Element("nature", "org.eclipse.cdt.make.core.makeNature");
xml.Element("nature", "org.eclipse.cdt.make.core.ScannerConfigNature");
2018-01-26 17:06:56 +01:00
for (std::string const& n : this->Natures) {
xml.Element("nature", n);
2016-07-09 11:21:54 +02:00
}
2021-11-20 13:41:27 +01:00
if (cmValue extraNaturesProp =
2016-07-09 11:21:54 +02:00
mf->GetState()->GetGlobalProperty("ECLIPSE_EXTRA_NATURES")) {
2023-07-02 19:51:09 +02:00
cmList extraNatures{ *extraNaturesProp };
2018-01-26 17:06:56 +01:00
for (std::string const& n : extraNatures) {
xml.Element("nature", n);
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
xml.EndElement(); // natures
2016-07-09 11:21:54 +02:00
xml.StartElement("linkedResources");
// create linked resources
2016-07-09 11:21:54 +02:00
if (this->IsOutOfSourceBuild) {
2010-03-17 14:00:29 +02:00
// create a linked resource to CMAKE_SOURCE_DIR
2011-01-16 11:35:12 +01:00
// (this is not done anymore for each project because of
2020-08-30 11:54:41 +02:00
// https://gitlab.kitware.com/cmake/cmake/-/issues/9978 and because I found
2016-10-30 18:24:19 +01:00
// it actually quite confusing in bigger projects with many directories and
2010-03-17 14:00:29 +02:00
// projects, Alex
std::string sourceLinkedResourceName = "[Source directory]";
2016-07-09 11:21:54 +02:00
std::string linkSourceDirectory =
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::GetEclipsePath(
lg->GetCurrentSourceDirectory());
2010-03-17 14:00:29 +02:00
// .project dir can't be subdir of a linked resource dir
2015-04-27 22:25:09 +02:00
if (!cmSystemTools::IsSubDirectory(this->HomeOutputDirectory,
2016-07-09 11:21:54 +02:00
linkSourceDirectory)) {
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendLinkedResource(
xml, sourceLinkedResourceName,
cmExtraEclipseCDT4Generator::GetEclipsePath(linkSourceDirectory),
LinkToFolder);
2018-04-23 21:13:27 +02:00
this->SrcLinkedResources.push_back(std::move(sourceLinkedResourceName));
2011-06-19 15:41:06 +03:00
}
2016-07-09 11:21:54 +02:00
}
2011-06-19 15:41:06 +03:00
2016-07-09 11:21:54 +02:00
if (this->SupportsVirtualFolders) {
this->CreateLinksToSubprojects(xml, this->HomeOutputDirectory);
2012-02-18 12:40:36 +02:00
2016-07-09 11:21:54 +02:00
this->CreateLinksForTargets(xml);
}
2012-02-18 12:40:36 +02:00
2016-07-09 11:21:54 +02:00
xml.EndElement(); // linkedResources
xml.EndElement(); // projectDescription
2012-02-18 12:40:36 +02:00
}
2016-07-09 11:21:54 +02:00
void cmExtraEclipseCDT4Generator::WriteGroups(
std::vector<cmSourceGroup> const& sourceGroups, std::string& linkName,
cmXMLWriter& xml)
{
2018-01-26 17:06:56 +01:00
for (cmSourceGroup const& sg : sourceGroups) {
2020-02-01 23:06:01 +01:00
std::string linkName3 = cmStrCat(linkName, '/', sg.GetFullName());
2016-07-09 11:21:54 +02:00
std::replace(linkName3.begin(), linkName3.end(), '\\', '/');
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendLinkedResource(
xml, linkName3, "virtual:/virtual", VirtualFolder);
2018-01-26 17:06:56 +01:00
std::vector<cmSourceGroup> const& children = sg.GetGroupChildren();
2016-07-09 11:21:54 +02:00
if (!children.empty()) {
this->WriteGroups(children, linkName, xml);
}
2018-01-26 17:06:56 +01:00
std::vector<const cmSourceFile*> sFiles = sg.GetSourceFiles();
for (cmSourceFile const* file : sFiles) {
std::string const& fullPath = file->GetFullPath();
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::FileIsDirectory(fullPath)) {
2020-02-01 23:06:01 +01:00
std::string linkName4 =
cmStrCat(linkName3, '/', cmSystemTools::GetFilenameName(fullPath));
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendLinkedResource(
xml, linkName4,
cmExtraEclipseCDT4Generator::GetEclipsePath(fullPath), LinkToFile);
2016-07-09 11:21:54 +02:00
}
}
}
}
2012-02-18 12:40:36 +02:00
2016-07-09 11:21:54 +02:00
void cmExtraEclipseCDT4Generator::CreateLinksForTargets(cmXMLWriter& xml)
2012-02-18 12:40:36 +02:00
{
std::string linkName = "[Targets]";
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendLinkedResource(
xml, linkName, "virtual:/virtual", VirtualFolder);
2012-02-18 12:40:36 +02:00
2020-08-30 11:54:41 +02:00
for (const auto& lg : this->GlobalGenerator->GetLocalGenerators()) {
2018-01-26 17:06:56 +01:00
cmMakefile* makefile = lg->GetMakefile();
2020-08-30 11:54:41 +02:00
const auto& targets = lg->GetGeneratorTargets();
2012-02-18 12:40:36 +02:00
2020-08-30 11:54:41 +02:00
for (const auto& target : targets) {
2020-02-01 23:06:01 +01:00
std::string linkName2 = cmStrCat(linkName, '/');
2018-01-26 17:06:56 +01:00
switch (target->GetType()) {
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: {
2016-07-09 11:21:54 +02:00
const char* prefix =
2018-01-26 17:06:56 +01:00
(target->GetType() == cmStateEnums::EXECUTABLE ? "[exe] "
: "[lib] ");
2012-02-18 12:40:36 +02:00
linkName2 += prefix;
2018-01-26 17:06:56 +01:00
linkName2 += target->GetName();
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendLinkedResource(
xml, linkName2, "virtual:/virtual", VirtualFolder);
2016-07-09 11:21:54 +02:00
if (!this->GenerateLinkedResources) {
2013-03-16 19:13:01 +02:00
break; // skip generating the linked resources to the source files
2016-07-09 11:21:54 +02:00
}
std::vector<cmSourceGroup> sourceGroups =
makefile->GetSourceGroups();
2012-02-18 12:40:36 +02:00
// get the files from the source lists then add them to the groups
2014-08-03 19:52:23 +02:00
std::vector<cmSourceFile*> files;
2020-08-30 11:54:41 +02:00
target->GetSourceFiles(
files, makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
2018-01-26 17:06:56 +01:00
for (cmSourceFile* sf : files) {
2012-02-18 12:40:36 +02:00
// Add the file to the list of sources.
2020-02-01 23:06:01 +01:00
std::string const& source = sf->ResolveFullPath();
2014-08-03 19:52:23 +02:00
cmSourceGroup* sourceGroup =
2018-04-23 21:13:27 +02:00
makefile->FindSourceGroup(source, sourceGroups);
2018-01-26 17:06:56 +01:00
sourceGroup->AssignSource(sf);
2012-02-18 12:40:36 +02:00
}
2016-07-09 11:21:54 +02:00
this->WriteGroups(sourceGroups, linkName2, xml);
} break;
2012-02-18 12:40:36 +02:00
// ignore all others:
default:
break;
}
}
2016-07-09 11:21:54 +02:00
}
2012-02-18 12:40:36 +02:00
}
void cmExtraEclipseCDT4Generator::CreateLinksToSubprojects(
2016-07-09 11:21:54 +02:00
cmXMLWriter& xml, const std::string& baseDir)
2012-02-18 12:40:36 +02:00
{
2016-07-09 11:21:54 +02:00
if (!this->GenerateLinkedResources) {
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
2011-06-19 15:41:06 +03:00
// for each sub project create a linked resource to the source dir
// - only if it is an out-of-source build
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendLinkedResource(
xml, "[Subprojects]", "virtual:/virtual", VirtualFolder);
2018-01-26 17:06:56 +01:00
for (auto const& it : this->GlobalGenerator->GetProjectMap()) {
2016-07-09 11:21:54 +02:00
std::string linkSourceDirectory =
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::GetEclipsePath(
it.second[0]->GetCurrentSourceDirectory());
2011-06-19 15:41:06 +03:00
// a linked resource must not point to a parent directory of .project or
// .project itself
2012-02-18 12:40:36 +02:00
if ((baseDir != linkSourceDirectory) &&
2016-07-09 11:21:54 +02:00
!cmSystemTools::IsSubDirectory(baseDir, linkSourceDirectory)) {
2020-02-01 23:06:01 +01:00
std::string linkName = cmStrCat("[Subprojects]/", it.first);
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendLinkedResource(
xml, linkName,
cmExtraEclipseCDT4Generator::GetEclipsePath(linkSourceDirectory),
LinkToFolder);
2013-03-16 19:13:01 +02:00
// Don't add it to the srcLinkedResources, because listing multiple
// directories confuses the Eclipse indexer (#13596).
}
2016-07-09 11:21:54 +02:00
}
}
2009-05-01 17:43:35 +03:00
void cmExtraEclipseCDT4Generator::AppendIncludeDirectories(
2016-07-09 11:21:54 +02:00
cmXMLWriter& xml, const std::vector<std::string>& includeDirs,
std::set<std::string>& emittedDirs)
2009-05-01 17:43:35 +03:00
{
2018-01-26 17:06:56 +01:00
for (std::string const& inc : includeDirs) {
if (!inc.empty()) {
std::string dir = cmSystemTools::CollapseFullPath(inc);
2013-03-16 19:13:01 +02:00
// handle framework include dirs on OSX, the remainder after the
// Frameworks/ part has to be stripped
// /System/Library/Frameworks/GLUT.framework/Headers
cmsys::RegularExpression frameworkRx("(.+/Frameworks)/.+\\.framework/");
2018-10-28 12:09:07 +01:00
if (frameworkRx.find(dir)) {
2013-03-16 19:13:01 +02:00
dir = frameworkRx.match(1);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (emittedDirs.find(dir) == emittedDirs.end()) {
2009-05-01 17:43:35 +03:00
emittedDirs.insert(dir);
2016-07-09 11:21:54 +02:00
xml.StartElement("pathentry");
xml.Attribute("include",
cmExtraEclipseCDT4Generator::GetEclipsePath(dir));
xml.Attribute("kind", "inc");
xml.Attribute("path", "");
xml.Attribute("system", "true");
xml.EndElement();
2009-05-01 17:43:35 +03:00
}
}
2016-07-09 11:21:54 +02:00
}
2009-05-01 17:43:35 +03:00
}
void cmExtraEclipseCDT4Generator::CreateCProjectFile() const
{
2021-09-14 00:13:48 +02:00
std::set<std::string> emitted;
2011-01-16 11:35:12 +01:00
2020-08-30 11:54:41 +02:00
const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
2016-03-13 13:35:51 +01:00
const cmMakefile* mf = lg->GetMakefile();
const std::string filename = this->HomeOutputDirectory + "/.cproject";
2018-10-28 12:09:07 +01:00
cmGeneratedFileStream fout(filename);
2016-07-09 11:21:54 +02:00
if (!fout) {
return;
2016-07-09 11:21:54 +02:00
}
cmXMLWriter xml(fout);
// add header
2016-07-09 11:21:54 +02:00
xml.StartDocument("UTF-8");
xml.ProcessingInstruction("fileVersion", "4.0.0");
xml.StartElement("cproject");
xml.StartElement("storageModule");
xml.Attribute("moduleId", "org.eclipse.cdt.core.settings");
2016-07-09 11:21:54 +02:00
xml.StartElement("cconfiguration");
xml.Attribute("id", "org.eclipse.cdt.core.default.config.1");
// Configuration settings...
2016-07-09 11:21:54 +02:00
xml.StartElement("storageModule");
xml.Attribute("buildSystemId",
"org.eclipse.cdt.core.defaultConfigDataProvider");
xml.Attribute("id", "org.eclipse.cdt.core.default.config.1");
xml.Attribute("moduleId", "org.eclipse.cdt.core.settings");
xml.Attribute("name", "Configuration");
xml.Element("externalSettings");
xml.StartElement("extensions");
// TODO: refactor this out...
2016-07-09 11:21:54 +02:00
std::string executableFormat =
mf->GetSafeDefinition("CMAKE_EXECUTABLE_FORMAT");
if (executableFormat == "ELF") {
xml.StartElement("extension");
xml.Attribute("id", "org.eclipse.cdt.core.ELF");
xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
xml.EndElement(); // extension
xml.StartElement("extension");
xml.Attribute("id", "org.eclipse.cdt.core.GNU_ELF");
xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
AppendAttribute(xml, "addr2line");
AppendAttribute(xml, "c++filt");
xml.EndElement(); // extension
} else {
2010-03-17 14:00:29 +02:00
std::string systemName = mf->GetSafeDefinition("CMAKE_SYSTEM_NAME");
2021-09-14 00:13:48 +02:00
if (systemName == "CYGWIN" || systemName == "MSYS") {
2016-07-09 11:21:54 +02:00
xml.StartElement("extension");
xml.Attribute("id", "org.eclipse.cdt.core.Cygwin_PE");
xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
AppendAttribute(xml, "addr2line");
AppendAttribute(xml, "c++filt");
AppendAttribute(xml, "cygpath");
AppendAttribute(xml, "nm");
xml.EndElement(); // extension
} else if (systemName == "Windows") {
xml.StartElement("extension");
xml.Attribute("id", "org.eclipse.cdt.core.PE");
xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
xml.EndElement(); // extension
} else if (systemName == "Darwin") {
xml.StartElement("extension");
2018-08-09 18:06:22 +02:00
xml.Attribute("id",
this->SupportsMachO64Parser
2016-07-09 11:21:54 +02:00
? "org.eclipse.cdt.core.MachO64"
: "org.eclipse.cdt.core.MachO");
xml.Attribute("point", "org.eclipse.cdt.core.BinaryParser");
AppendAttribute(xml, "c++filt");
xml.EndElement(); // extension
} else {
// *** Should never get here ***
2016-07-09 11:21:54 +02:00
xml.Element("error_toolchain_type");
2010-03-17 14:00:29 +02:00
}
2016-07-09 11:21:54 +02:00
}
2010-03-17 14:00:29 +02:00
2016-07-09 11:21:54 +02:00
xml.EndElement(); // extensions
xml.EndElement(); // storageModule
2011-01-16 11:35:12 +01:00
// ???
2016-07-09 11:21:54 +02:00
xml.StartElement("storageModule");
xml.Attribute("moduleId", "org.eclipse.cdt.core.language.mapping");
xml.Element("project-mappings");
xml.EndElement(); // storageModule
// ???
2016-07-09 11:21:54 +02:00
xml.StartElement("storageModule");
xml.Attribute("moduleId", "org.eclipse.cdt.core.externalSettings");
xml.EndElement(); // storageModule
// set the path entries (includes, libs, source dirs, etc.)
2016-07-09 11:21:54 +02:00
xml.StartElement("storageModule");
xml.Attribute("moduleId", "org.eclipse.cdt.core.pathentry");
// for each sub project with a linked resource to the source dir:
// - make it type 'src'
// - and exclude it from type 'out'
std::string excludeFromOut;
2016-07-09 11:21:54 +02:00
/* I don't know what the pathentry kind="src" are good for, e.g.
* autocompletion
* works also without them. Done wrong, the indexer complains, see #12417
* and #12213.
* According to #13596, this entry at least limits the directories the
* indexer is searching for files. So now the "src" entry contains only
* the linked resource to CMAKE_SOURCE_DIR.
* The CDT documentation is very terse on that:
* "CDT_SOURCE: Entry kind constant describing a path entry identifying a
* folder containing source code to be compiled."
* Also on the cdt-dev list didn't bring any information:
* http://web.archiveorange.com/archive/v/B4NlJDNIpYoOS1SbxFNy
* Alex */
2017-04-14 19:02:05 +02:00
// include subprojects directory to the src pathentry
// eclipse cdt indexer uses this entries as reference to index source files
if (this->GenerateLinkedResources) {
xml.StartElement("pathentry");
xml.Attribute("kind", "src");
xml.Attribute("path", "[Subprojects]");
xml.EndElement();
}
2016-07-09 11:21:54 +02:00
2018-01-26 17:06:56 +01:00
for (std::string const& p : this->SrcLinkedResources) {
2016-07-09 11:21:54 +02:00
xml.StartElement("pathentry");
xml.Attribute("kind", "src");
2018-01-26 17:06:56 +01:00
xml.Attribute("path", p);
2016-07-09 11:21:54 +02:00
xml.EndElement();
2019-11-11 23:01:05 +01:00
// exclude source directory from output search path
// - only if not named the same as an output directory
if (!cmSystemTools::FileIsDirectory(
2022-08-04 22:12:04 +02:00
cmStrCat(this->HomeOutputDirectory, '/', p))) {
2018-01-26 17:06:56 +01:00
excludeFromOut += p + "/|";
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
excludeFromOut += "**/CMakeFiles/";
2016-07-09 11:21:54 +02:00
xml.StartElement("pathentry");
xml.Attribute("excluding", excludeFromOut);
xml.Attribute("kind", "out");
xml.Attribute("path", "");
xml.EndElement();
// add pre-processor definitions to allow eclipse to gray out sections
2021-09-14 00:13:48 +02:00
emitted.clear();
2020-08-30 11:54:41 +02:00
for (const auto& lgen : this->GlobalGenerator->GetLocalGenerators()) {
2016-07-09 11:21:54 +02:00
2021-11-20 13:41:27 +01:00
if (cmValue cdefs =
2018-01-26 17:06:56 +01:00
lgen->GetMakefile()->GetProperty("COMPILE_DEFINITIONS")) {
// Expand the list.
std::vector<std::string> defs;
2020-08-30 11:54:41 +02:00
cmGeneratorExpression::Split(*cdefs, defs);
2018-01-26 17:06:56 +01:00
for (std::string const& d : defs) {
if (cmGeneratorExpression::Find(d) != std::string::npos) {
2014-08-03 19:52:23 +02:00
continue;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2018-01-26 17:06:56 +01:00
std::string::size_type equals = d.find('=', 0);
std::string::size_type enddef = d.length();
std::string def;
std::string val;
2016-07-09 11:21:54 +02:00
if (equals != std::string::npos && equals < enddef) {
// we have -DFOO=BAR
2018-01-26 17:06:56 +01:00
def = d.substr(0, equals);
val = d.substr(equals + 1, enddef - equals + 1);
2016-07-09 11:21:54 +02:00
} else {
// we have -DFOO
2018-01-26 17:06:56 +01:00
def = d;
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
// insert the definition if not already added.
2021-09-14 00:13:48 +02:00
if (emitted.find(def) == emitted.end()) {
emitted.insert(def);
2016-07-09 11:21:54 +02:00
xml.StartElement("pathentry");
xml.Attribute("kind", "mac");
xml.Attribute("name", def);
xml.Attribute("path", "");
xml.Attribute("value", val);
xml.EndElement();
}
}
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// add system defined c macros
2021-11-20 13:41:27 +01:00
cmValue cDefs =
2016-07-09 11:21:54 +02:00
mf->GetDefinition("CMAKE_EXTRA_GENERATOR_C_SYSTEM_DEFINED_MACROS");
if (this->CEnabled && cDefs) {
2009-10-04 10:30:41 +03:00
// Expand the list.
2023-07-02 19:51:09 +02:00
cmList defs{ *cDefs, cmList::EmptyElements::Yes };
2009-10-04 10:30:41 +03:00
// the list must contain only definition-value pairs:
2016-07-09 11:21:54 +02:00
if ((defs.size() % 2) == 0) {
2020-02-01 23:06:01 +01:00
auto di = defs.begin();
2016-07-09 11:21:54 +02:00
while (di != defs.end()) {
2009-10-04 10:30:41 +03:00
std::string def = *di;
++di;
std::string val;
2016-07-09 11:21:54 +02:00
if (di != defs.end()) {
2009-10-04 10:30:41 +03:00
val = *di;
++di;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// insert the definition if not already added.
2021-09-14 00:13:48 +02:00
if (emitted.find(def) == emitted.end()) {
emitted.insert(def);
2016-07-09 11:21:54 +02:00
xml.StartElement("pathentry");
xml.Attribute("kind", "mac");
xml.Attribute("name", def);
xml.Attribute("path", "");
xml.Attribute("value", val);
xml.EndElement();
2009-10-04 10:30:41 +03:00
}
}
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// add system defined c++ macros
2021-11-20 13:41:27 +01:00
cmValue cxxDefs =
2016-07-09 11:21:54 +02:00
mf->GetDefinition("CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_DEFINED_MACROS");
if (this->CXXEnabled && cxxDefs) {
2009-10-04 10:30:41 +03:00
// Expand the list.
2023-07-02 19:51:09 +02:00
cmList defs{ *cxxDefs, cmList::EmptyElements::Yes };
2009-10-04 10:30:41 +03:00
// the list must contain only definition-value pairs:
2016-07-09 11:21:54 +02:00
if ((defs.size() % 2) == 0) {
2020-02-01 23:06:01 +01:00
auto di = defs.begin();
2016-07-09 11:21:54 +02:00
while (di != defs.end()) {
2009-10-04 10:30:41 +03:00
std::string def = *di;
++di;
std::string val;
2016-07-09 11:21:54 +02:00
if (di != defs.end()) {
2009-10-04 10:30:41 +03:00
val = *di;
++di;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// insert the definition if not already added.
2021-09-14 00:13:48 +02:00
if (emitted.find(def) == emitted.end()) {
emitted.insert(def);
2016-07-09 11:21:54 +02:00
xml.StartElement("pathentry");
xml.Attribute("kind", "mac");
xml.Attribute("name", def);
xml.Attribute("path", "");
xml.Attribute("value", val);
xml.EndElement();
2009-10-04 10:30:41 +03:00
}
}
}
2016-07-09 11:21:54 +02:00
}
// include dirs
2021-09-14 00:13:48 +02:00
emitted.clear();
2020-08-30 11:54:41 +02:00
for (const auto& lgen : this->GlobalGenerator->GetLocalGenerators()) {
const auto& targets = lgen->GetGeneratorTargets();
for (const auto& target : targets) {
2019-11-11 23:01:05 +01:00
if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
continue;
}
2012-04-19 19:04:21 +03:00
std::vector<std::string> includeDirs;
2015-04-27 22:25:09 +02:00
std::string config = mf->GetSafeDefinition("CMAKE_BUILD_TYPE");
2020-08-30 11:54:41 +02:00
lgen->GetIncludeDirectories(includeDirs, target.get(), "C", config);
2021-09-14 00:13:48 +02:00
this->AppendIncludeDirectories(xml, includeDirs, emitted);
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
// now also the system include directories, in case we found them in
// CMakeSystemSpecificInformation.cmake. This makes Eclipse find the
2009-05-01 17:43:35 +03:00
// standard headers.
std::string compiler = mf->GetSafeDefinition("CMAKE_C_COMPILER");
2016-07-09 11:21:54 +02:00
if (this->CEnabled && !compiler.empty()) {
std::string systemIncludeDirs =
mf->GetSafeDefinition("CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
2023-07-02 19:51:09 +02:00
cmList dirs{ systemIncludeDirs };
2021-09-14 00:13:48 +02:00
this->AppendIncludeDirectories(xml, dirs, emitted);
2016-07-09 11:21:54 +02:00
}
2009-05-01 17:43:35 +03:00
compiler = mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
2016-07-09 11:21:54 +02:00
if (this->CXXEnabled && !compiler.empty()) {
std::string systemIncludeDirs =
mf->GetSafeDefinition("CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
2023-07-02 19:51:09 +02:00
cmList dirs{ systemIncludeDirs };
2021-09-14 00:13:48 +02:00
this->AppendIncludeDirectories(xml, dirs, emitted);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
xml.EndElement(); // storageModule
// add build targets
2016-07-09 11:21:54 +02:00
xml.StartElement("storageModule");
xml.Attribute("moduleId", "org.eclipse.cdt.make.core.buildtargets");
xml.StartElement("buildTargets");
2021-09-14 00:13:48 +02:00
emitted.clear();
2019-11-11 23:01:05 +01:00
const std::string& make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
const std::string& makeArgs =
2016-07-09 11:21:54 +02:00
mf->GetSafeDefinition("CMAKE_ECLIPSE_MAKE_ARGUMENTS");
2012-02-18 12:40:36 +02:00
2016-07-09 11:21:54 +02:00
cmGlobalGenerator* generator =
const_cast<cmGlobalGenerator*>(this->GlobalGenerator);
2011-01-16 11:35:12 +01:00
2009-10-04 10:30:41 +03:00
std::string allTarget;
std::string cleanTarget;
2016-07-09 11:21:54 +02:00
if (generator->GetAllTargetName()) {
2009-10-04 10:30:41 +03:00
allTarget = generator->GetAllTargetName();
2016-07-09 11:21:54 +02:00
}
if (generator->GetCleanTargetName()) {
2009-10-04 10:30:41 +03:00
cleanTarget = generator->GetCleanTargetName();
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
// add all executable and library targets and some of the GLOBAL
// and UTILITY targets
2020-08-30 11:54:41 +02:00
for (const auto& lgen : this->GlobalGenerator->GetLocalGenerators()) {
const auto& targets = lgen->GetGeneratorTargets();
2021-09-14 00:13:48 +02:00
std::string subdir =
lgen->MaybeRelativeToTopBinDir(lgen->GetCurrentBinaryDirectory());
2016-07-09 11:21:54 +02:00
if (subdir == ".") {
2018-01-26 17:06:56 +01:00
subdir.clear();
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2020-08-30 11:54:41 +02:00
for (const auto& target : targets) {
2018-01-26 17:06:56 +01:00
std::string targetName = target->GetName();
switch (target->GetType()) {
2017-04-14 19:02:05 +02:00
case cmStateEnums::GLOBAL_TARGET: {
2011-01-16 11:35:12 +01:00
// Only add the global targets from CMAKE_BINARY_DIR,
2009-10-04 10:30:41 +03:00
// not from the subdirs
2016-07-09 11:21:54 +02:00
if (subdir.empty()) {
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
makeArgs, subdir, ": ");
2016-07-09 11:21:54 +02:00
}
} break;
2017-04-14 19:02:05 +02:00
case cmStateEnums::UTILITY:
2016-07-09 11:21:54 +02:00
// Add all utility targets, except the Nightly/Continuous/
// Experimental-"sub"targets as e.g. NightlyStart
2020-08-30 11:54:41 +02:00
if ((cmHasLiteralPrefix(targetName, "Nightly") &&
2016-07-09 11:21:54 +02:00
(targetName != "Nightly")) ||
2020-08-30 11:54:41 +02:00
(cmHasLiteralPrefix(targetName, "Continuous") &&
2016-07-09 11:21:54 +02:00
(targetName != "Continuous")) ||
2020-08-30 11:54:41 +02:00
(cmHasLiteralPrefix(targetName, "Experimental") &&
2016-07-09 11:21:54 +02:00
(targetName != "Experimental"))) {
break;
2012-02-18 12:40:36 +02:00
}
2016-07-09 11:21:54 +02:00
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
makeArgs, subdir, ": ");
2016-07-09 11:21:54 +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: {
2016-07-09 11:21:54 +02:00
const char* prefix =
2018-01-26 17:06:56 +01:00
(target->GetType() == cmStateEnums::EXECUTABLE ? "[exe] "
: "[lib] ");
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendTarget(xml, targetName, make,
makeArgs, subdir, prefix);
2020-02-01 23:06:01 +01:00
std::string fastTarget = cmStrCat(targetName, "/fast");
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendTarget(xml, fastTarget, make,
makeArgs, subdir, prefix);
2016-07-09 11:21:54 +02:00
// Add Build and Clean targets in the virtual folder of targets:
if (this->SupportsVirtualFolders) {
2020-02-01 23:06:01 +01:00
std::string virtDir = cmStrCat("[Targets]/", prefix, targetName);
std::string buildArgs =
cmStrCat("-C \"", lgen->GetBinaryDirectory(), "\" ", makeArgs);
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendTarget(
xml, "Build", make, buildArgs, virtDir, "", targetName.c_str());
2016-07-09 11:21:54 +02:00
2020-02-01 23:06:01 +01:00
std::string cleanArgs =
cmStrCat("-E chdir \"", lgen->GetCurrentBinaryDirectory(),
"\" \"", cmSystemTools::GetCMakeCommand(), "\" -P \"");
2020-08-30 11:54:41 +02:00
cleanArgs += lgen->GetTargetDirectory(target.get());
2016-07-09 11:21:54 +02:00
cleanArgs += "/cmake_clean.cmake\"";
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendTarget(
xml, "Clean", cmSystemTools::GetCMakeCommand(), cleanArgs,
virtDir, "", "");
2016-07-09 11:21:54 +02:00
}
} break;
2019-11-11 23:01:05 +01:00
case cmStateEnums::INTERFACE_LIBRARY:
default:
break;
}
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
2009-10-04 10:30:41 +03:00
// insert the all and clean targets in every subdir
2016-07-09 11:21:54 +02:00
if (!allTarget.empty()) {
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendTarget(xml, allTarget, make, makeArgs,
subdir, ": ");
2016-07-09 11:21:54 +02:00
}
if (!cleanTarget.empty()) {
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendTarget(xml, cleanTarget, make,
makeArgs, subdir, ": ");
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
// insert rules for compiling, preprocessing and assembling individual
// files
2009-10-04 10:30:41 +03:00
std::vector<std::string> objectFileTargets;
2018-01-26 17:06:56 +01:00
lg->GetIndividualFileTargets(objectFileTargets);
for (std::string const& f : objectFileTargets) {
2009-10-04 10:30:41 +03:00
const char* prefix = "[obj] ";
2019-11-11 23:01:05 +01:00
if (f.back() == 's') {
2009-10-04 10:30:41 +03:00
prefix = "[to asm] ";
2019-11-11 23:01:05 +01:00
} else if (f.back() == 'i') {
2009-10-04 10:30:41 +03:00
prefix = "[pre] ";
}
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendTarget(xml, f, make, makeArgs, subdir,
prefix);
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
xml.EndElement(); // buildTargets
xml.EndElement(); // storageModule
2019-11-11 23:01:05 +01:00
cmExtraEclipseCDT4Generator::AppendStorageScanners(xml, *mf);
2016-07-09 11:21:54 +02:00
xml.EndElement(); // cconfiguration
xml.EndElement(); // storageModule
xml.StartElement("storageModule");
xml.Attribute("moduleId", "cdtBuildSystem");
xml.Attribute("version", "4.0.0");
xml.StartElement("project");
xml.Attribute("id", std::string(lg->GetProjectName()) + ".null.1");
xml.Attribute("name", lg->GetProjectName());
xml.EndElement(); // project
xml.EndElement(); // storageModule
2018-08-09 18:06:22 +02:00
// Append additional cproject contents without applying any XML formatting
2021-11-20 13:41:27 +01:00
if (cmValue extraCProjectContents =
2018-08-09 18:06:22 +02:00
mf->GetState()->GetGlobalProperty("ECLIPSE_EXTRA_CPROJECT_CONTENTS")) {
2020-08-30 11:54:41 +02:00
fout << *extraCProjectContents;
2018-08-09 18:06:22 +02:00
}
2016-07-09 11:21:54 +02:00
xml.EndElement(); // cproject
}
2016-07-09 11:21:54 +02:00
std::string cmExtraEclipseCDT4Generator::GetEclipsePath(
const std::string& path)
{
#if defined(__CYGWIN__)
std::string cmd = "cygpath -m " + path;
std::string out;
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::RunSingleCommand(cmd.c_str(), &out, &out)) {
return path;
2016-07-09 11:21:54 +02:00
} else {
out.erase(out.find_last_of('\n'));
return out;
2016-07-09 11:21:54 +02:00
}
#else
return path;
#endif
}
2016-07-09 11:21:54 +02:00
std::string cmExtraEclipseCDT4Generator::GetPathBasename(
const std::string& path)
{
std::string outputBasename = path;
2015-04-27 22:25:09 +02:00
while (!outputBasename.empty() &&
2019-11-11 23:01:05 +01:00
(outputBasename.back() == '/' || outputBasename.back() == '\\')) {
outputBasename.resize(outputBasename.size() - 1);
2016-07-09 11:21:54 +02:00
}
std::string::size_type loc = outputBasename.find_last_of("/\\");
2016-07-09 11:21:54 +02:00
if (loc != std::string::npos) {
outputBasename = outputBasename.substr(loc + 1);
2016-07-09 11:21:54 +02:00
}
return outputBasename;
}
2016-07-09 11:21:54 +02:00
std::string cmExtraEclipseCDT4Generator::GenerateProjectName(
const std::string& name, const std::string& type, const std::string& path)
{
2016-07-09 11:21:54 +02:00
return name + (type.empty() ? "" : "-") + type + "@" + path;
}
// Helper functions
2016-07-09 11:21:54 +02:00
void cmExtraEclipseCDT4Generator::AppendStorageScanners(
cmXMLWriter& xml, const cmMakefile& makefile)
{
// we need the "make" and the C (or C++) compiler which are used, Alex
2019-11-11 23:01:05 +01:00
const std::string& make =
makefile.GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
std::string compiler = makefile.GetSafeDefinition("CMAKE_C_COMPILER");
2012-02-18 12:40:36 +02:00
std::string arg1 = makefile.GetSafeDefinition("CMAKE_C_COMPILER_ARG1");
2016-07-09 11:21:54 +02:00
if (compiler.empty()) {
compiler = makefile.GetSafeDefinition("CMAKE_CXX_COMPILER");
2012-02-18 12:40:36 +02:00
arg1 = makefile.GetSafeDefinition("CMAKE_CXX_COMPILER_ARG1");
2016-07-09 11:21:54 +02:00
}
if (compiler.empty()) // Hmm, what to do now ?
{
compiler = "gcc";
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
// the following right now hardcodes gcc behavior :-/
2012-02-18 12:40:36 +02:00
std::string compilerArgs =
2016-07-09 11:21:54 +02:00
"-E -P -v -dD ${plugin_state_location}/${specs_file}";
if (!arg1.empty()) {
2012-02-18 12:40:36 +02:00
arg1 += " ";
compilerArgs = arg1 + compilerArgs;
2016-07-09 11:21:54 +02:00
}
xml.StartElement("storageModule");
xml.Attribute("moduleId", "scannerConfiguration");
xml.StartElement("autodiscovery");
xml.Attribute("enabled", "true");
xml.Attribute("problemReportingEnabled", "true");
xml.Attribute("selectedProfileId",
"org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile");
xml.EndElement(); // autodiscovery
cmExtraEclipseCDT4Generator::AppendScannerProfile(
xml, "org.eclipse.cdt.make.core.GCCStandardMakePerProjectProfile", true,
"", true, "specsFile", compilerArgs, compiler, true, true);
cmExtraEclipseCDT4Generator::AppendScannerProfile(
xml, "org.eclipse.cdt.make.core.GCCStandardMakePerFileProfile", true, "",
true, "makefileGenerator", "-f ${project_name}_scd.mk", make, true, true);
xml.EndElement(); // storageModule
}
2009-10-04 10:30:41 +03:00
// The prefix is prepended before the actual name of the target. The purpose
// of that is to sort the targets in the view of Eclipse, so that at first
// the global/utility/all/clean targets appear ": ", then the executable
// targets "[exe] ", then the libraries "[lib]", then the rules for the
2011-01-16 11:35:12 +01:00
// object files "[obj]", then for preprocessing only "[pre] " and
// finally the assembly files "[to asm] ". Note the "to" in "to asm",
2009-10-04 10:30:41 +03:00
// without it, "asm" would be the first targets in the list, with the "to"
// they are the last targets, which makes more sense.
2016-07-09 11:21:54 +02:00
void cmExtraEclipseCDT4Generator::AppendTarget(
cmXMLWriter& xml, const std::string& target, const std::string& make,
const std::string& makeArgs, const std::string& path, const char* prefix,
const char* makeTarget)
{
2016-07-09 11:21:54 +02:00
xml.StartElement("target");
xml.Attribute("name", prefix + target);
xml.Attribute("path", path);
xml.Attribute("targetID", "org.eclipse.cdt.make.MakeTargetBuilder");
xml.Element("buildCommand",
cmExtraEclipseCDT4Generator::GetEclipsePath(make));
xml.Element("buildArguments", makeArgs);
xml.Element("buildTarget", makeTarget ? makeTarget : target.c_str());
xml.Element("stopOnError", "true");
xml.Element("useDefaultCommand", "false");
xml.EndElement();
}
2016-07-09 11:21:54 +02:00
void cmExtraEclipseCDT4Generator::AppendScannerProfile(
cmXMLWriter& xml, const std::string& profileID, bool openActionEnabled,
const std::string& openActionFilePath, bool pParserEnabled,
const std::string& scannerInfoProviderID,
const std::string& runActionArguments, const std::string& runActionCommand,
bool runActionUseDefault, bool sipParserEnabled)
{
2016-07-09 11:21:54 +02:00
xml.StartElement("profile");
xml.Attribute("id", profileID);
xml.StartElement("buildOutputProvider");
xml.StartElement("openAction");
xml.Attribute("enabled", openActionEnabled ? "true" : "false");
xml.Attribute("filePath", openActionFilePath);
xml.EndElement(); // openAction
xml.StartElement("parser");
xml.Attribute("enabled", pParserEnabled ? "true" : "false");
xml.EndElement(); // parser
xml.EndElement(); // buildOutputProvider
xml.StartElement("scannerInfoProvider");
xml.Attribute("id", scannerInfoProviderID);
xml.StartElement("runAction");
xml.Attribute("arguments", runActionArguments);
xml.Attribute("command", runActionCommand);
xml.Attribute("useDefault", runActionUseDefault ? "true" : "false");
xml.EndElement(); // runAction
xml.StartElement("parser");
xml.Attribute("enabled", sipParserEnabled ? "true" : "false");
xml.EndElement(); // parser
xml.EndElement(); // scannerInfoProvider
xml.EndElement(); // profile
}
2016-07-09 11:21:54 +02:00
void cmExtraEclipseCDT4Generator::AppendLinkedResource(cmXMLWriter& xml,
const std::string& name,
const std::string& path,
LinkType linkType)
{
2011-06-19 15:41:06 +03:00
const char* locationTag = "location";
2016-07-09 11:21:54 +02:00
int typeTag = 2;
2012-02-18 12:40:36 +02:00
if (linkType == VirtualFolder) // ... and not a linked folder
2016-07-09 11:21:54 +02:00
{
2011-06-19 15:41:06 +03:00
locationTag = "locationURI";
2016-07-09 11:21:54 +02:00
}
if (linkType == LinkToFile) {
typeTag = 1;
}
xml.StartElement("link");
xml.Element("name", name);
xml.Element("type", typeTag);
xml.Element(locationTag, path);
xml.EndElement();
}