From fe65ab6287f39b9ca4ee6e8d7efb91d7b03123b6 Mon Sep 17 00:00:00 2001 From: John Dallaway Date: Fri, 14 Apr 2023 14:43:49 +0100 Subject: [PATCH] Set environment for spawning GNU tool processes Allows GNU tools to be found on the PATH defined by the build configuration of the containing project. Part of #361 --- .../org/eclipse/cdt/utils/Addr2line.java | 12 ++++++-- .../utils/org/eclipse/cdt/utils/CPPFilt.java | 12 ++++++-- .../cdt/utils/DefaultGnuToolFactory.java | 28 +++++++++++++++---- .../utils/org/eclipse/cdt/utils/NM.java | 15 ++++++++-- .../utils/org/eclipse/cdt/utils/Objdump.java | 16 ++++++++--- 5 files changed, 67 insertions(+), 16 deletions(-) diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/Addr2line.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/Addr2line.java index 7bfc0ad4983..31124935486 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/Addr2line.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/Addr2line.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2014 QNX Software Systems and others. + * Copyright (c) 2000, 2023 QNX Software Systems and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -10,6 +10,7 @@ * * Contributors: * QNX Software Systems - Initial API and implementation + * John Dallaway - Add constructor with environment (#361) *******************************************************************************/ package org.eclipse.cdt.utils; @@ -26,6 +27,7 @@ public class Addr2line { private String[] args; + private String[] envp; private Process addr2line; private BufferedReader stdout; private BufferedWriter stdin; @@ -33,6 +35,12 @@ public class Addr2line { private static final Pattern OUTPUT_PATTERN = Pattern.compile("(.*)( \\(discriminator.*\\))"); //$NON-NLS-1$ //private boolean isDisposed = false; + /** @since 8.2 */ + public Addr2line(String command, String[] params, String file, String[] envp) throws IOException { + this.envp = envp; + init(command, params, file); + } + public Addr2line(String command, String[] params, String file) throws IOException { init(command, params, file); } @@ -53,7 +61,7 @@ protected void init(String command, String[] params, String file) throws IOExcep args[0] = command; System.arraycopy(params, 0, args, 1, params.length); } - addr2line = ProcessFactory.getFactory().exec(args); + addr2line = ProcessFactory.getFactory().exec(args, envp); stdin = new BufferedWriter(new OutputStreamWriter(addr2line.getOutputStream())); stdout = new BufferedReader(new InputStreamReader(addr2line.getInputStream())); } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/CPPFilt.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/CPPFilt.java index fae12d68c52..e3884b6d855 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/CPPFilt.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/CPPFilt.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2009 QNX Software Systems and others. + * Copyright (c) 2000, 2023 QNX Software Systems and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -10,6 +10,7 @@ * * Contributors: * QNX Software Systems - Initial API and implementation + * John Dallaway - Add constructor with environment (#361) *******************************************************************************/ package org.eclipse.cdt.utils; @@ -26,11 +27,18 @@ */ public class CPPFilt { private String[] args; + private String[] envp; private Process cppfilt; private BufferedReader stdout; private BufferedWriter stdin; //private boolean isDisposed = false; + /** @since 8.2 */ + public CPPFilt(String command, String[] params, String[] envp) throws IOException { + this.envp = envp; + init(command, params); + } + public CPPFilt(String command, String[] params) throws IOException { init(command, params); } @@ -51,7 +59,7 @@ protected void init(String command, String[] params) throws IOException { args[0] = command; System.arraycopy(params, 0, args, 1, params.length); } - cppfilt = ProcessFactory.getFactory().exec(args); + cppfilt = ProcessFactory.getFactory().exec(args, envp); stdin = new BufferedWriter(new OutputStreamWriter(cppfilt.getOutputStream())); stdout = new BufferedReader(new InputStreamReader(cppfilt.getInputStream())); } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/DefaultGnuToolFactory.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/DefaultGnuToolFactory.java index ff46be844b3..00729b18c45 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/DefaultGnuToolFactory.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/DefaultGnuToolFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2004, 2012 QNX Software Systems and others. + * Copyright (c) 2004, 2023 QNX Software Systems and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -10,13 +10,18 @@ * * Contributors: * QNX Software Systems - initial API and implementation + * John Dallaway - set environment for spawning GNU tool processes (#361) *******************************************************************************/ package org.eclipse.cdt.utils; import java.io.IOException; +import java.util.Arrays; +import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.ICExtension; +import org.eclipse.cdt.core.envvar.IEnvironmentVariable; import org.eclipse.cdt.core.settings.model.ICConfigExtensionReference; +import org.eclipse.cdt.core.settings.model.ICConfigurationDescription; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; @@ -30,10 +35,11 @@ public DefaultGnuToolFactory(ICExtension ext) { @Override public Addr2line getAddr2line(IPath path) { IPath addr2LinePath = getAddr2linePath(); + String[] environment = getEnvironment(); Addr2line addr2line = null; if (addr2LinePath != null && !addr2LinePath.isEmpty()) { try { - addr2line = new Addr2line(addr2LinePath.toOSString(), path.toOSString()); + addr2line = new Addr2line(addr2LinePath.toOSString(), new String[0], path.toOSString(), environment); } catch (IOException e1) { } } @@ -43,10 +49,11 @@ public Addr2line getAddr2line(IPath path) { @Override public CPPFilt getCPPFilt() { IPath cppFiltPath = getCPPFiltPath(); + String[] environment = getEnvironment(); CPPFilt cppfilt = null; if (cppFiltPath != null && !cppFiltPath.isEmpty()) { try { - cppfilt = new CPPFilt(cppFiltPath.toOSString()); + cppfilt = new CPPFilt(cppFiltPath.toOSString(), new String[0], environment); } catch (IOException e2) { } } @@ -57,10 +64,11 @@ public CPPFilt getCPPFilt() { public Objdump getObjdump(IPath path) { IPath objdumpPath = getObjdumpPath(); String objdumpArgs = getObjdumpArgs(); + String[] environment = getEnvironment(); Objdump objdump = null; if (objdumpPath != null && !objdumpPath.isEmpty()) { try { - objdump = new Objdump(objdumpPath.toOSString(), objdumpArgs, path.toOSString()); + objdump = new Objdump(objdumpPath.toOSString(), objdumpArgs, path.toOSString(), environment); } catch (IOException e1) { } } @@ -71,10 +79,11 @@ public Objdump getObjdump(IPath path) { public NM getNM(IPath path) { IPath nmPath = getNMPath(); String nmArgs = getNMArgs(); + String[] environment = getEnvironment(); NM nm = null; if (nmPath != null && !nmPath.isEmpty()) { try { - nm = new NM(nmPath.toOSString(), nmArgs, path.toOSString()); + nm = new NM(nmPath.toOSString(), nmArgs, path.toOSString(), environment); } catch (IOException e1) { } } @@ -143,4 +152,13 @@ protected String getNMArgs() { } return value; } + + /** @since 8.2 */ + protected String[] getEnvironment() { + ICConfigExtensionReference ref = fExtension.getConfigExtensionReference(); + ICConfigurationDescription cfg = ref.getConfiguration(); + IEnvironmentVariable[] vars = CCorePlugin.getDefault().getBuildEnvironmentManager().getVariables(cfg, true); + return Arrays.stream(vars).map(v -> String.format("%s=%s", v.getName(), v.getValue())) //$NON-NLS-1$ + .toArray(String[]::new); + } } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/NM.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/NM.java index 0dc2b6bd7c1..c548605af53 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/NM.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/NM.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2009 QNX Software Systems and others. + * Copyright (c) 2000, 2023 QNX Software Systems and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -10,6 +10,7 @@ * * Contributors: * QNX Software Systems - Initial API and implementation + * John Dallaway - Add constructor with environment (#361) *******************************************************************************/ package org.eclipse.cdt.utils; @@ -66,6 +67,8 @@ public String toString() { */ protected List data_symbols; + private String[] envp; + private void parseOutput(InputStream stream) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); @@ -124,7 +127,9 @@ public NM(String command, String file, boolean dynamic_only) throws IOException this(command, (dynamic_only) ? new String[] { "-C", "-D" } : null, file); //$NON-NLS-1$ //$NON-NLS-2$ } - public NM(String command, String param, String file) throws IOException { + /** @since 8.2 */ + public NM(String command, String param, String file, String[] envp) throws IOException { + this.envp = envp; String[] params; if (param == null || param.length() == 0) { params = new String[0]; @@ -135,6 +140,10 @@ public NM(String command, String param, String file) throws IOException { init(command, params, file); } + public NM(String command, String param, String file) throws IOException { + this(command, param, file, null); + } + public NM(String command, String[] params, String file) throws IOException { init(command, params, file); } @@ -154,7 +163,7 @@ protected void init(String command, String[] params, String file) throws IOExcep text_symbols = new ArrayList<>(); data_symbols = new ArrayList<>(); bss_symbols = new ArrayList<>(); - Process process = ProcessFactory.getFactory().exec(args); + Process process = ProcessFactory.getFactory().exec(args, envp); parseOutput(process.getInputStream()); process.destroy(); } diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/Objdump.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/Objdump.java index 16339e22706..53bc86c6f90 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/Objdump.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/Objdump.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2016 QNX Software Systems and others. + * Copyright (c) 2000, 2023 QNX Software Systems and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -10,6 +10,7 @@ * * Contributors: * QNX Software Systems - Initial API and implementation + * John Dallaway - Add constructor with environment (#361) *******************************************************************************/ package org.eclipse.cdt.utils; @@ -28,8 +29,11 @@ */ public class Objdump { String[] args; + String[] envp; - public Objdump(String command, String param, String file) throws IOException { + /** @since 8.2 */ + public Objdump(String command, String param, String file, String[] envp) throws IOException { + this.envp = envp; String[] params; if (param == null || param.length() == 0) { params = new String[0]; @@ -46,6 +50,10 @@ public Objdump(String command, String param, String file) throws IOException { init(command, params, file); } + public Objdump(String command, String param, String file) throws IOException { + this(command, param, file, null); + } + public Objdump(String command, String[] params, String file) throws IOException { init(command, params, file); } @@ -80,7 +88,7 @@ public String toString() { * @since 5.8 */ public byte[] getOutput(int limitBytes) throws IOException { - Process objdump = ProcessFactory.getFactory().exec(args); + Process objdump = ProcessFactory.getFactory().exec(args, envp); try { StringBuilder buffer = new StringBuilder(); BufferedReader stdout = new BufferedReader(new InputStreamReader(objdump.getInputStream())); @@ -110,7 +118,7 @@ public byte[] getOutput() throws IOException { /** @since 5.8 */ public InputStream getInputStream() throws IOException { - Process objdump = ProcessFactory.getFactory().exec(args); + Process objdump = ProcessFactory.getFactory().exec(args, envp); objdump.getOutputStream().close(); objdump.getErrorStream().close(); return objdump.getInputStream();