From 36110e125115da12b79eedec52e1dca154026f3f Mon Sep 17 00:00:00 2001 From: Gesa Hentschke <123444711+ghentschke@users.noreply.github.com> Date: Mon, 18 Dec 2023 16:42:17 +0100 Subject: [PATCH] [#407] fix CompilerBuiltinsDetector for msys2 on Windows (#648) [#407] fix CompilerBuiltinsDetector for msys2 on Windows The cc.exe from mingw64 (part of Msys2) on Windows needs the bin folder to be on the PATH to be executed. e.g. 'C:\msys64\mingw64\bin' must be part of the PATH environment variable fixes #407 --- .../META-INF/MANIFEST.MF | 2 +- .../builtins/CompilerBuiltinsDetector.java | 47 +++++++++++++++---- .../core/internal/builtins/Messages.java | 1 + .../internal/builtins/messages.properties | 1 + 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/jsoncdb/org.eclipse.cdt.jsoncdb.core/META-INF/MANIFEST.MF b/jsoncdb/org.eclipse.cdt.jsoncdb.core/META-INF/MANIFEST.MF index 34c0cc30944..a455c8a3f96 100644 --- a/jsoncdb/org.eclipse.cdt.jsoncdb.core/META-INF/MANIFEST.MF +++ b/jsoncdb/org.eclipse.cdt.jsoncdb.core/META-INF/MANIFEST.MF @@ -4,7 +4,7 @@ Bundle-Name: %bundleName Bundle-Description: %bundleDescription Bundle-Copyright: %Bundle-Copyright Bundle-SymbolicName: org.eclipse.cdt.jsoncdb.core;singleton:=true -Bundle-Version: 1.4.200.qualifier +Bundle-Version: 1.4.300.qualifier Bundle-Vendor: %Bundle-Vendor Bundle-Localization: plugin Bundle-RequiredExecutionEnvironment: JavaSE-17 diff --git a/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/internal/builtins/CompilerBuiltinsDetector.java b/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/internal/builtins/CompilerBuiltinsDetector.java index db4e691b050..9dcac6373ff 100644 --- a/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/internal/builtins/CompilerBuiltinsDetector.java +++ b/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/internal/builtins/CompilerBuiltinsDetector.java @@ -14,9 +14,11 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Objects; +import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.ConsoleOutputStream; import org.eclipse.cdt.core.ICommandLauncher; import org.eclipse.cdt.core.resources.IConsole; @@ -104,8 +106,8 @@ public IRawSourceFileInfo detectBuiltins(IProject project, java.nio.file.Path bu launcher.setProject(project); launcher.showCommand(console != null); - final Process proc = launcher.execute(new Path(command), argList.toArray(new String[argList.size()]), getEnvp(), - new Path(this.buildDirectory.toString()), monitor); + final Process proc = launcher.execute(new Path(command), argList.toArray(new String[argList.size()]), + getEnvp(project), new Path(this.buildDirectory.toString()), monitor); if (proc != null) { try { // Close the input of the process since we will never write to it @@ -182,20 +184,47 @@ private List getCompilerArguments() { * @return String array of environment variables in format "var=value". Does not * return {@code null}. */ - private String[] getEnvp() { + private String[] getEnvp(IProject project) { + var map = new HashMap(); + // The cc.exe from mingw64 (part of Msys2) on Windows needs the bin folder to be on the PATH to be executed. + // e.g. 'C:\msys64\mingw64\bin' must be part of the PATH environment variable. That's why we need PATH here: + // Fixes CDT #407 + try { + final String path = "PATH"; //$NON-NLS-1$ + var variables = CCorePlugin.getDefault().getBuildEnvironmentManager() + .getVariables(project.getActiveBuildConfig(), true); + for (var variable : variables) { + if (path.equalsIgnoreCase(variable.getName())) { + map.put(path, variable.getValue()); + break; + } + } + } catch (CoreException e) { + Plugin.getDefault().getLog().error( + String.format(Messages.CompilerBuiltinsDetector_errmsg_determine_PATH_failed, project.getName()), + e); + } // On POSIX (Linux, UNIX) systems reset language variables to default (English) // with UTF-8 encoding since GNU compilers can handle only UTF-8 characters. // Include paths with locale characters will be handled properly regardless // of the language as long as the encoding is set to UTF-8. // English language is set for parser because it relies on English messages // in the output of the 'gcc -v'. - String[] strings = { - // override for GNU gettext - "LANGUAGE" + "=en", //$NON-NLS-1$//$NON-NLS-2$ - // for other parts of the system libraries - "LC_ALL" + "=C.UTF-8" }; //$NON-NLS-1$ //$NON-NLS-2$ - return strings; + // override for GNU gettext + map.put("LANGUAGE", "en"); //$NON-NLS-1$ //$NON-NLS-2$ + // for other parts of the system libraries + map.put("LC_ALL", "C.UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$ + + var envArray = map.entrySet().stream().map(entry -> { + var result = entry.getKey() + '='; + if (entry.getValue() != null) { + result = result + entry.getValue(); + } + return result; + }).toArray(String[]::new); + + return envArray; } /** diff --git a/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/internal/builtins/Messages.java b/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/internal/builtins/Messages.java index 25b703e3ef2..fc94f58c679 100644 --- a/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/internal/builtins/Messages.java +++ b/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/internal/builtins/Messages.java @@ -23,6 +23,7 @@ class Messages extends NLS { public static String CompilerBuiltinsDetector_msg_detection_finished; public static String CompilerBuiltinsDetector_msg_detection_start; public static String CompilerBuiltinsDetector_msg_unexpectedly_still_running; + public static String CompilerBuiltinsDetector_errmsg_determine_PATH_failed; public static String DetectorConsole_title; static { // initialize resource bundle diff --git a/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/internal/builtins/messages.properties b/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/internal/builtins/messages.properties index 09a209b9b21..fdb9275f030 100644 --- a/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/internal/builtins/messages.properties +++ b/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/internal/builtins/messages.properties @@ -15,4 +15,5 @@ CompilerBuiltinsDetector_errmsg_command_failed=%1$s exited with status %2$d. CompilerBuiltinsDetector_msg_detection_finished=Detecting compiler built-ins took %d ms. CompilerBuiltinsDetector_msg_detection_start=%1$s Detecting compiler built-ins for project '%2$s' CompilerBuiltinsDetector_msg_unexpectedly_still_running=%1$s is unexpectedly still running. Some built-ins might not have been captured by the compiler built-ins detector. +CompilerBuiltinsDetector_errmsg_determine_PATH_failed=Cannot determine PATH variable for project '%1$s' DetectorConsole_title=Compiler Built-ins Detection Console