Skip to content

Commit

Permalink
[eclipse-cdt#407] fix CompilerBuiltinsDetector for msys2 on Windows (e…
Browse files Browse the repository at this point in the history
…clipse-cdt#648)

[eclipse-cdt#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 eclipse-cdt#407
  • Loading branch information
ghentschke authored Dec 18, 2023
1 parent 9aa6fb1 commit 36110e1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion jsoncdb/org.eclipse.cdt.jsoncdb.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -182,20 +184,47 @@ private List<String> 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<String, String>();
// 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 36110e1

Please sign in to comment.