From 229bf15ab260a7c672f4786c9e83d1028f2d7455 Mon Sep 17 00:00:00 2001 From: Federico Pellegrin Date: Tue, 31 Aug 2021 10:40:10 +0200 Subject: [PATCH] eclipse: add generation of editor language settings Add automatic generation of editor language settings for C and C++, so the automatic code correction uses the correct compiler and compiler flags, including for example the correct C/C++ standard so construct from such standards are correctly managed by the IDE. Correct compiler and flags are automatically generated using the build environment data gathered during configure phase. The playground example has been modified to contain some code that is standard specific to demonstrate the new feature when run under Eclipse. --- playground/eclipse/c/exLibC/src/exLibC.cpp | 4 ++ playground/eclipse/c/wscript | 11 ++-- waflib/extras/eclipse.py | 63 ++++++++++++++++++++++ 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/playground/eclipse/c/exLibC/src/exLibC.cpp b/playground/eclipse/c/exLibC/src/exLibC.cpp index b8720ebe..79877a3d 100644 --- a/playground/eclipse/c/exLibC/src/exLibC.cpp +++ b/playground/eclipse/c/exLibC/src/exLibC.cpp @@ -1,9 +1,13 @@ #include #include +#include #include int check_smaller(int value) { + const char* foo = u8"bar"; // u8 is C++17 only + std::cout << __cplusplus << std::endl; // Check version of C++ standard + if (value < HELLO_LIMIT) { return 0; } else { diff --git a/playground/eclipse/c/wscript b/playground/eclipse/c/wscript index dbdce20b..7093bcd8 100644 --- a/playground/eclipse/c/wscript +++ b/playground/eclipse/c/wscript @@ -2,12 +2,15 @@ # encoding: utf-8 def options(opt): - # We are using C++ - opt.load('compiler_cxx') + # We are using C and C++ + opt.load('compiler_c compiler_cxx') def configure(conf): - # We are using C++ - conf.load('compiler_cxx') + # We are using C and C++ + conf.load('compiler_c compiler_cxx') + # Force some standards to see that IDE will follow them + conf.env.CXXFLAGS=['-std=c++17'] + conf.env.CFLAGS=['-std=c17'] def build(bld): bld.shlib(source='exLibC/src/exLibC.cpp', includes='exLibC/src/include', target='exampleLibC', export_includes='exLibC/src/include/') diff --git a/waflib/extras/eclipse.py b/waflib/extras/eclipse.py index bb787416..e123fc16 100644 --- a/waflib/extras/eclipse.py +++ b/waflib/extras/eclipse.py @@ -25,6 +25,8 @@ cdt_core = oe_cdt + '.core' cdt_bld = oe_cdt + '.build.core' extbuilder_dir = '.externalToolBuilders' extbuilder_name = 'Waf_Builder.launch' +settings_dir = '.settings' +settings_name = 'language.settings.xml' class eclipse(Build.BuildContext): cmd = 'eclipse' @@ -156,6 +158,61 @@ class eclipse(Build.BuildContext): project = self.impl_create_javaproject(javasrcpath, javalibpath) self.write_conf_to_xml('.classpath', project) + # Create editor language settings to have correct standards applied in IDE, as per project configuration + try: + os.mkdir(settings_dir) + except OSError: + pass # Ignore if dir already exists + + lang_settings = Document() + project = lang_settings.createElement('project') + + # Language configurations for C and C++ via cdt + if hasc: + configuration = self.add(lang_settings, project, 'configuration', + {'id' : 'org.eclipse.cdt.core.default.config.1', 'name': 'Default'}) + + extension = self.add(lang_settings, configuration, 'extension', {'point': 'org.eclipse.cdt.core.LanguageSettingsProvider'}) + + provider = self.add(lang_settings, extension, 'provider', + { 'copy-of': 'extension', + 'id': 'org.eclipse.cdt.ui.UserLanguageSettingsProvider'}) + + provider = self.add(lang_settings, extension, 'provider-reference', + { 'id': 'org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider', + 'ref': 'shared-provider'}) + + provider = self.add(lang_settings, extension, 'provider-reference', + { 'id': 'org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider', + 'ref': 'shared-provider'}) + + # C and C++ are kept as separated providers so appropriate flags are used also in mixed projects + if self.env.CC: + provider = self.add(lang_settings, extension, 'provider', + { 'class': 'org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector', + 'console': 'false', + 'id': 'org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector.1', + 'keep-relative-paths' : 'false', + 'name': 'CDT GCC Built-in Compiler Settings', + 'parameter': '%s %s ${FLAGS} -E -P -v -dD "${INPUTS}"'%(self.env.CC[0],' '.join(self.env['CFLAGS'])), + 'prefer-non-shared': 'true' }) + + self.add(lang_settings, provider, 'language-scope', { 'id': 'org.eclipse.cdt.core.gcc'}) + + if self.env.CXX: + provider = self.add(lang_settings, extension, 'provider', + { 'class': 'org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector', + 'console': 'false', + 'id': 'org.eclipse.cdt.managedbuilder.language.settings.providers.GCCBuiltinSpecsDetector.2', + 'keep-relative-paths' : 'false', + 'name': 'CDT GCC Built-in Compiler Settings', + 'parameter': '%s %s ${FLAGS} -E -P -v -dD "${INPUTS}"'%(self.env.CXX[0],' '.join(self.env['CXXFLAGS'])), + 'prefer-non-shared': 'true' }) + self.add(lang_settings, provider, 'language-scope', { 'id': 'org.eclipse.cdt.core.g++'}) + + lang_settings.appendChild(project) + self.write_conf_to_xml('%s%s%s'%(settings_dir, os.path.sep, settings_name), lang_settings) + def impl_create_project(self, executable, appname, hasc, hasjava, haspython, waf_executable): doc = Document() projectDescription = doc.createElement('projectDescription') @@ -348,6 +405,12 @@ class eclipse(Build.BuildContext): self.add(doc, storageModule, 'project', {'id': '%s.null.1'%appname, 'name': appname}) + storageModule = self.add(doc, cproject, 'storageModule', + {'moduleId': 'org.eclipse.cdt.core.LanguageSettingsProviders'}) + + storageModule = self.add(doc, cproject, 'storageModule', + {'moduleId': 'scannerConfiguration'}) + doc.appendChild(cproject) return doc