1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-21 21:52:10 +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
* 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 @@ import org.eclipse.cdt.utils.spawner.ProcessFactory;
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 @@ public class Addr2line {
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()));
}

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
* 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 @@ import org.eclipse.cdt.utils.spawner.ProcessFactory;
*/
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 @@ public class CPPFilt {
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()));
}

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
* 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 class DefaultGnuToolFactory implements IGnuToolFactory {
@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 class DefaultGnuToolFactory implements IGnuToolFactory {
@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 class DefaultGnuToolFactory implements IGnuToolFactory {
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 class DefaultGnuToolFactory implements IGnuToolFactory {
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 @@ public class DefaultGnuToolFactory implements IGnuToolFactory {
}
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
* 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 class NM {
*/
protected List<AddressNamePair> data_symbols;
private String[] envp;
private void parseOutput(InputStream stream) throws IOException {
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$
}
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 class NM {
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 @@ public class NM {
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();
}

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
* 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 @@ import org.eclipse.cdt.utils.spawner.ProcessFactory;
*/
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 class Objdump {
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 class Objdump {
* @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 class Objdump {
/** @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();