Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set environment for spawning GNU tool processes #367

Merged
merged 1 commit into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,6 +10,7 @@
*
* Contributors:
* QNX Software Systems - Initial API and implementation
* John Dallaway - Add constructor with environment (#361)
*******************************************************************************/
package org.eclipse.cdt.utils;

Expand All @@ -26,13 +27,20 @@

public class Addr2line {
private String[] args;
private String[] envp;
private Process addr2line;
private BufferedReader stdout;
private BufferedWriter stdin;
private String lastaddr, lastsymbol, lastline;
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);
}
Expand All @@ -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()));
}
Expand Down
12 changes: 10 additions & 2 deletions core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/CPPFilt.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,6 +10,7 @@
*
* Contributors:
* QNX Software Systems - Initial API and implementation
* John Dallaway - Add constructor with environment (#361)
*******************************************************************************/
package org.eclipse.cdt.utils;

Expand All @@ -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);
}
Expand All @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

Expand All @@ -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) {
}
}
Expand All @@ -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) {
}
}
Expand All @@ -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) {
}
}
Expand All @@ -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) {
}
}
Expand Down Expand Up @@ -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);
}
}
15 changes: 12 additions & 3 deletions core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/NM.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,6 +10,7 @@
*
* Contributors:
* QNX Software Systems - Initial API and implementation
* John Dallaway - Add constructor with environment (#361)
*******************************************************************************/

package org.eclipse.cdt.utils;
Expand Down Expand Up @@ -66,6 +67,8 @@ public String toString() {
*/
protected List<AddressNamePair> data_symbols;

private String[] envp;

private void parseOutput(InputStream stream) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
Expand Down Expand Up @@ -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];
Expand All @@ -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);
}
Expand All @@ -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();
}
Expand Down
16 changes: 12 additions & 4 deletions core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/Objdump.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,6 +10,7 @@
*
* Contributors:
* QNX Software Systems - Initial API and implementation
* John Dallaway - Add constructor with environment (#361)
*******************************************************************************/
package org.eclipse.cdt.utils;

Expand All @@ -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];
Expand All @@ -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);
}
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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();
Expand Down