1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 06:02:11 +02:00

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
This commit is contained in:
John Dallaway 2023-04-14 14:43:49 +01:00
parent a9a7e8b027
commit fe65ab6287
5 changed files with 67 additions and 16 deletions

View file

@ -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 * This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0 * are made available under the terms of the Eclipse Public License 2.0
@ -10,6 +10,7 @@
* *
* Contributors: * Contributors:
* QNX Software Systems - Initial API and implementation * QNX Software Systems - Initial API and implementation
* John Dallaway - Add constructor with environment (#361)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.utils; package org.eclipse.cdt.utils;
@ -26,6 +27,7 @@ import org.eclipse.cdt.utils.spawner.ProcessFactory;
public class Addr2line { public class Addr2line {
private String[] args; private String[] args;
private String[] envp;
private Process addr2line; private Process addr2line;
private BufferedReader stdout; private BufferedReader stdout;
private BufferedWriter stdin; private BufferedWriter stdin;
@ -33,6 +35,12 @@ public class Addr2line {
private static final Pattern OUTPUT_PATTERN = Pattern.compile("(.*)( \\(discriminator.*\\))"); //$NON-NLS-1$ private static final Pattern OUTPUT_PATTERN = Pattern.compile("(.*)( \\(discriminator.*\\))"); //$NON-NLS-1$
//private boolean isDisposed = false; //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 { public Addr2line(String command, String[] params, String file) throws IOException {
init(command, params, file); init(command, params, file);
} }
@ -53,7 +61,7 @@ public class Addr2line {
args[0] = command; args[0] = command;
System.arraycopy(params, 0, args, 1, params.length); 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())); stdin = new BufferedWriter(new OutputStreamWriter(addr2line.getOutputStream()));
stdout = new BufferedReader(new InputStreamReader(addr2line.getInputStream())); stdout = new BufferedReader(new InputStreamReader(addr2line.getInputStream()));
} }

View file

@ -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 * This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0 * are made available under the terms of the Eclipse Public License 2.0
@ -10,6 +10,7 @@
* *
* Contributors: * Contributors:
* QNX Software Systems - Initial API and implementation * QNX Software Systems - Initial API and implementation
* John Dallaway - Add constructor with environment (#361)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.utils; package org.eclipse.cdt.utils;
@ -26,11 +27,18 @@ import org.eclipse.cdt.utils.spawner.ProcessFactory;
*/ */
public class CPPFilt { public class CPPFilt {
private String[] args; private String[] args;
private String[] envp;
private Process cppfilt; private Process cppfilt;
private BufferedReader stdout; private BufferedReader stdout;
private BufferedWriter stdin; private BufferedWriter stdin;
//private boolean isDisposed = false; //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 { public CPPFilt(String command, String[] params) throws IOException {
init(command, params); init(command, params);
} }
@ -51,7 +59,7 @@ public class CPPFilt {
args[0] = command; args[0] = command;
System.arraycopy(params, 0, args, 1, params.length); 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())); stdin = new BufferedWriter(new OutputStreamWriter(cppfilt.getOutputStream()));
stdout = new BufferedReader(new InputStreamReader(cppfilt.getInputStream())); stdout = new BufferedReader(new InputStreamReader(cppfilt.getInputStream()));
} }

View file

@ -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 * This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0 * are made available under the terms of the Eclipse Public License 2.0
@ -10,13 +10,18 @@
* *
* Contributors: * Contributors:
* QNX Software Systems - initial API and implementation * QNX Software Systems - initial API and implementation
* John Dallaway - set environment for spawning GNU tool processes (#361)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.utils; package org.eclipse.cdt.utils;
import java.io.IOException; 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.ICExtension;
import org.eclipse.cdt.core.envvar.IEnvironmentVariable;
import org.eclipse.cdt.core.settings.model.ICConfigExtensionReference; 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.IPath;
import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Path;
@ -30,10 +35,11 @@ public class DefaultGnuToolFactory implements IGnuToolFactory {
@Override @Override
public Addr2line getAddr2line(IPath path) { public Addr2line getAddr2line(IPath path) {
IPath addr2LinePath = getAddr2linePath(); IPath addr2LinePath = getAddr2linePath();
String[] environment = getEnvironment();
Addr2line addr2line = null; Addr2line addr2line = null;
if (addr2LinePath != null && !addr2LinePath.isEmpty()) { if (addr2LinePath != null && !addr2LinePath.isEmpty()) {
try { try {
addr2line = new Addr2line(addr2LinePath.toOSString(), path.toOSString()); addr2line = new Addr2line(addr2LinePath.toOSString(), new String[0], path.toOSString(), environment);
} catch (IOException e1) { } catch (IOException e1) {
} }
} }
@ -43,10 +49,11 @@ public class DefaultGnuToolFactory implements IGnuToolFactory {
@Override @Override
public CPPFilt getCPPFilt() { public CPPFilt getCPPFilt() {
IPath cppFiltPath = getCPPFiltPath(); IPath cppFiltPath = getCPPFiltPath();
String[] environment = getEnvironment();
CPPFilt cppfilt = null; CPPFilt cppfilt = null;
if (cppFiltPath != null && !cppFiltPath.isEmpty()) { if (cppFiltPath != null && !cppFiltPath.isEmpty()) {
try { try {
cppfilt = new CPPFilt(cppFiltPath.toOSString()); cppfilt = new CPPFilt(cppFiltPath.toOSString(), new String[0], environment);
} catch (IOException e2) { } catch (IOException e2) {
} }
} }
@ -57,10 +64,11 @@ public class DefaultGnuToolFactory implements IGnuToolFactory {
public Objdump getObjdump(IPath path) { public Objdump getObjdump(IPath path) {
IPath objdumpPath = getObjdumpPath(); IPath objdumpPath = getObjdumpPath();
String objdumpArgs = getObjdumpArgs(); String objdumpArgs = getObjdumpArgs();
String[] environment = getEnvironment();
Objdump objdump = null; Objdump objdump = null;
if (objdumpPath != null && !objdumpPath.isEmpty()) { if (objdumpPath != null && !objdumpPath.isEmpty()) {
try { try {
objdump = new Objdump(objdumpPath.toOSString(), objdumpArgs, path.toOSString()); objdump = new Objdump(objdumpPath.toOSString(), objdumpArgs, path.toOSString(), environment);
} catch (IOException e1) { } catch (IOException e1) {
} }
} }
@ -71,10 +79,11 @@ public class DefaultGnuToolFactory implements IGnuToolFactory {
public NM getNM(IPath path) { public NM getNM(IPath path) {
IPath nmPath = getNMPath(); IPath nmPath = getNMPath();
String nmArgs = getNMArgs(); String nmArgs = getNMArgs();
String[] environment = getEnvironment();
NM nm = null; NM nm = null;
if (nmPath != null && !nmPath.isEmpty()) { if (nmPath != null && !nmPath.isEmpty()) {
try { try {
nm = new NM(nmPath.toOSString(), nmArgs, path.toOSString()); nm = new NM(nmPath.toOSString(), nmArgs, path.toOSString(), environment);
} catch (IOException e1) { } catch (IOException e1) {
} }
} }
@ -143,4 +152,13 @@ public class DefaultGnuToolFactory implements IGnuToolFactory {
} }
return value; 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);
}
} }

View file

@ -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 * This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0 * are made available under the terms of the Eclipse Public License 2.0
@ -10,6 +10,7 @@
* *
* Contributors: * Contributors:
* QNX Software Systems - Initial API and implementation * QNX Software Systems - Initial API and implementation
* John Dallaway - Add constructor with environment (#361)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.utils; package org.eclipse.cdt.utils;
@ -66,6 +67,8 @@ public class NM {
*/ */
protected List<AddressNamePair> data_symbols; protected List<AddressNamePair> data_symbols;
private String[] envp;
private void parseOutput(InputStream stream) throws IOException { private void parseOutput(InputStream stream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
@ -124,7 +127,9 @@ public class NM {
this(command, (dynamic_only) ? new String[] { "-C", "-D" } : null, file); //$NON-NLS-1$ //$NON-NLS-2$ 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; String[] params;
if (param == null || param.length() == 0) { if (param == null || param.length() == 0) {
params = new String[0]; params = new String[0];
@ -135,6 +140,10 @@ public class NM {
init(command, params, file); 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 { public NM(String command, String[] params, String file) throws IOException {
init(command, params, file); init(command, params, file);
} }
@ -154,7 +163,7 @@ public class NM {
text_symbols = new ArrayList<>(); text_symbols = new ArrayList<>();
data_symbols = new ArrayList<>(); data_symbols = new ArrayList<>();
bss_symbols = new ArrayList<>(); bss_symbols = new ArrayList<>();
Process process = ProcessFactory.getFactory().exec(args); Process process = ProcessFactory.getFactory().exec(args, envp);
parseOutput(process.getInputStream()); parseOutput(process.getInputStream());
process.destroy(); process.destroy();
} }

View file

@ -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 * This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0 * are made available under the terms of the Eclipse Public License 2.0
@ -10,6 +10,7 @@
* *
* Contributors: * Contributors:
* QNX Software Systems - Initial API and implementation * QNX Software Systems - Initial API and implementation
* John Dallaway - Add constructor with environment (#361)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.utils; package org.eclipse.cdt.utils;
@ -28,8 +29,11 @@ import org.eclipse.cdt.utils.spawner.ProcessFactory;
*/ */
public class Objdump { public class Objdump {
String[] args; 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; String[] params;
if (param == null || param.length() == 0) { if (param == null || param.length() == 0) {
params = new String[0]; params = new String[0];
@ -46,6 +50,10 @@ public class Objdump {
init(command, params, file); 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 { public Objdump(String command, String[] params, String file) throws IOException {
init(command, params, file); init(command, params, file);
} }
@ -80,7 +88,7 @@ public class Objdump {
* @since 5.8 * @since 5.8
*/ */
public byte[] getOutput(int limitBytes) throws IOException { public byte[] getOutput(int limitBytes) throws IOException {
Process objdump = ProcessFactory.getFactory().exec(args); Process objdump = ProcessFactory.getFactory().exec(args, envp);
try { try {
StringBuilder buffer = new StringBuilder(); StringBuilder buffer = new StringBuilder();
BufferedReader stdout = new BufferedReader(new InputStreamReader(objdump.getInputStream())); BufferedReader stdout = new BufferedReader(new InputStreamReader(objdump.getInputStream()));
@ -110,7 +118,7 @@ public class Objdump {
/** @since 5.8 */ /** @since 5.8 */
public InputStream getInputStream() throws IOException { public InputStream getInputStream() throws IOException {
Process objdump = ProcessFactory.getFactory().exec(args); Process objdump = ProcessFactory.getFactory().exec(args, envp);
objdump.getOutputStream().close(); objdump.getOutputStream().close();
objdump.getErrorStream().close(); objdump.getErrorStream().close();
return objdump.getInputStream(); return objdump.getInputStream();