1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00
This commit is contained in:
David Inglis 2006-05-15 14:07:07 +00:00
parent a8d10ae99f
commit b2265fc025

View file

@ -49,202 +49,202 @@ import org.eclipse.core.runtime.SubProgressMonitor;
* @author vhirsl * @author vhirsl
*/ */
public class DefaultRunSIProvider implements IExternalScannerInfoProvider { public class DefaultRunSIProvider implements IExternalScannerInfoProvider {
private static final String EXTERNAL_SI_PROVIDER_ERROR = "ExternalScannerInfoProvider.Provider_Error"; //$NON-NLS-1$
private static final String EXTERNAL_SI_PROVIDER_CONSOLE_ID = MakeCorePlugin.getUniqueIdentifier() + ".ExternalScannerInfoProviderConsole"; //$NON-NLS-1$
private static final String LANG_ENV_VAR = "LANG"; //$NON-NLS-1$
protected IResource resource; private static final String EXTERNAL_SI_PROVIDER_ERROR = "ExternalScannerInfoProvider.Provider_Error"; //$NON-NLS-1$
protected String providerId; private static final String EXTERNAL_SI_PROVIDER_CONSOLE_ID = MakeCorePlugin.getUniqueIdentifier() + ".ExternalScannerInfoProviderConsole"; //$NON-NLS-1$
protected IScannerConfigBuilderInfo2 buildInfo; private static final String LANG_ENV_VAR = "LANG"; //$NON-NLS-1$
protected IScannerInfoCollector collector;
// To be initialized by a subclass protected IResource resource;
protected IPath fWorkingDirectory; protected String providerId;
protected IPath fCompileCommand; protected IScannerConfigBuilderInfo2 buildInfo;
protected String[] fCompileArguments; protected IScannerInfoCollector collector;
// To be initialized by a subclass
private SCMarkerGenerator markerGenerator = new SCMarkerGenerator(); protected IPath fWorkingDirectory;
protected IPath fCompileCommand;
protected String[] fCompileArguments;
private SCMarkerGenerator markerGenerator = new SCMarkerGenerator();
/* (non-Javadoc) /* (non-Javadoc)
* @see org.eclipse.cdt.make.core.scannerconfig.IExternalScannerInfoProvider#invokeProvider(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.resources.IResource, java.lang.String, org.eclipse.cdt.make.core.scannerconfig.IScannerConfigBuilderInfo2, org.eclipse.cdt.make.core.scannerconfig.IScannerInfoCollector2) * @see org.eclipse.cdt.make.core.scannerconfig.IExternalScannerInfoProvider#invokeProvider(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.resources.IResource, java.lang.String, org.eclipse.cdt.make.core.scannerconfig.IScannerConfigBuilderInfo2, org.eclipse.cdt.make.core.scannerconfig.IScannerInfoCollector2)
*/ */
public boolean invokeProvider(IProgressMonitor monitor, public boolean invokeProvider(IProgressMonitor monitor,
IResource resource, IResource resource,
String providerId, String providerId,
IScannerConfigBuilderInfo2 buildInfo, IScannerConfigBuilderInfo2 buildInfo,
IScannerInfoCollector collector) { IScannerInfoCollector collector) {
// initialize fields // initialize fields
this.resource = resource; this.resource = resource;
this.providerId = providerId; this.providerId = providerId;
this.buildInfo = buildInfo; this.buildInfo = buildInfo;
this.collector = collector; this.collector = collector;
IProject currentProject = resource.getProject();
// call a subclass to initialize protected fields
if (!initialize()) {
return false;
}
if (monitor == null) {
monitor = new NullProgressMonitor();
}
monitor.beginTask(MakeMessages.getString("ExternalScannerInfoProvider.Reading_Specs"), 100); //$NON-NLS-1$
try {
IConsole console = CCorePlugin.getDefault().getConsole(EXTERNAL_SI_PROVIDER_CONSOLE_ID);
console.start(currentProject);
OutputStream cos = console.getOutputStream();
// Before launching give visual cues via the monitor IProject currentProject = resource.getProject();
monitor.subTask(MakeMessages.getString("ExternalScannerInfoProvider.Reading_Specs")); //$NON-NLS-1$ // call a subclass to initialize protected fields
if (!initialize()) {
String errMsg = null; return false;
CommandLauncher launcher = new CommandLauncher(); }
// Print the command for visual interaction. if (monitor == null) {
launcher.showCommand(true); monitor = new NullProgressMonitor();
}
monitor.beginTask(MakeMessages.getString("ExternalScannerInfoProvider.Reading_Specs"), 100); //$NON-NLS-1$
// add additional arguments try {
// subclass can change default behavior IConsole console = CCorePlugin.getDefault().getConsole(EXTERNAL_SI_PROVIDER_CONSOLE_ID);
console.start(currentProject);
OutputStream cos = console.getOutputStream();
// Before launching give visual cues via the monitor
monitor.subTask(MakeMessages.getString("ExternalScannerInfoProvider.Reading_Specs")); //$NON-NLS-1$
String errMsg = null;
CommandLauncher launcher = new CommandLauncher();
// Print the command for visual interaction.
launcher.showCommand(true);
// add additional arguments
// subclass can change default behavior
String[] compileArguments = prepareArguments( String[] compileArguments = prepareArguments(
buildInfo.isUseDefaultProviderCommand(providerId)); buildInfo.isUseDefaultProviderCommand(providerId));
String ca = coligate(compileArguments); String ca = coligate(compileArguments);
monitor.subTask(MakeMessages.getString("ExternalScannerInfoProvider.Invoking_Command") //$NON-NLS-1$
+ fCompileCommand.toString() + ca);
cos = new StreamMonitor(new SubProgressMonitor(monitor, 70), cos, 100);
monitor.subTask(MakeMessages.getString("ExternalScannerInfoProvider.Invoking_Command") //$NON-NLS-1$
+ fCompileCommand.toString() + ca);
cos = new StreamMonitor(new SubProgressMonitor(monitor, 70), cos, 100);
ConsoleOutputSniffer sniffer = ScannerInfoConsoleParserFactory.getESIProviderOutputSniffer( ConsoleOutputSniffer sniffer = ScannerInfoConsoleParserFactory.getESIProviderOutputSniffer(
cos, cos, currentProject, providerId, buildInfo, collector, markerGenerator); cos, cos, currentProject, providerId, buildInfo, collector, markerGenerator);
OutputStream consoleOut = (sniffer == null ? cos : sniffer.getOutputStream()); OutputStream consoleOut = (sniffer == null ? cos : sniffer.getOutputStream());
OutputStream consoleErr = (sniffer == null ? cos : sniffer.getErrorStream()); OutputStream consoleErr = (sniffer == null ? cos : sniffer.getErrorStream());
TraceUtil.outputTrace("Default provider is executing command:", fCompileCommand.toString() + ca, ""); //$NON-NLS-1$ //$NON-NLS-2$ TraceUtil.outputTrace("Default provider is executing command:", fCompileCommand.toString() + ca, ""); //$NON-NLS-1$ //$NON-NLS-2$
Process p = launcher.execute(fCompileCommand, compileArguments, setEnvironment(launcher), fWorkingDirectory); Process p = launcher.execute(fCompileCommand, compileArguments, setEnvironment(launcher), fWorkingDirectory);
if (p != null) { if (p != null) {
try { try {
// Close the input of the Process explicitely. // Close the input of the Process explicitely.
// We will never write to it. // We will never write to it.
p.getOutputStream().close(); p.getOutputStream().close();
} catch (IOException e) { } catch (IOException e) {
} }
if (launcher.waitAndRead(consoleOut, consoleErr, new SubProgressMonitor(monitor, 0)) != CommandLauncher.OK) { if (launcher.waitAndRead(consoleOut, consoleErr, new SubProgressMonitor(monitor, 0)) != CommandLauncher.OK) {
errMsg = launcher.getErrorMessage(); errMsg = launcher.getErrorMessage();
} }
monitor.subTask(MakeMessages.getString("ExternalScannerInfoProvider.Parsing_Output")); //$NON-NLS-1$ monitor.subTask(MakeMessages.getString("ExternalScannerInfoProvider.Parsing_Output")); //$NON-NLS-1$
} }
else { else {
errMsg = launcher.getErrorMessage(); errMsg = launcher.getErrorMessage();
} }
if (errMsg != null) { if (errMsg != null) {
String errorDesc = MakeMessages.getFormattedString(EXTERNAL_SI_PROVIDER_ERROR, String errorDesc = MakeMessages.getFormattedString(EXTERNAL_SI_PROVIDER_ERROR,
fCompileCommand.toString() + ca); fCompileCommand.toString() + ca);
markerGenerator.addMarker(currentProject, -1, errorDesc, IMarkerGenerator.SEVERITY_WARNING, null); markerGenerator.addMarker(currentProject, -1, errorDesc, IMarkerGenerator.SEVERITY_WARNING, null);
} }
monitor.subTask(MakeMessages.getString("ExternalScannerInfoProvider.Creating_Markers")); //$NON-NLS-1$ monitor.subTask(MakeMessages.getString("ExternalScannerInfoProvider.Creating_Markers")); //$NON-NLS-1$
consoleOut.close(); consoleOut.close();
consoleErr.close(); consoleErr.close();
cos.close(); cos.close();
} }
catch (Exception e) { catch (Exception e) {
CCorePlugin.log(e); CCorePlugin.log(e);
} }
finally { finally {
monitor.done(); monitor.done();
} }
return true; return true;
} }
/** /**
* Initialization of protected fields. * Initialization of protected fields.
* Subclasses are most likely to override default implementation. * Subclasses are most likely to override default implementation.
* *
* @param currentProject * @param currentProject
* @return boolean * @return boolean
*/ */
protected boolean initialize() { protected boolean initialize() {
IProject currProject = resource.getProject(); IProject currProject = resource.getProject();
IMakeBuilderInfo info; IPath workingDirectory = null;
try { try {
info = MakeCorePlugin.createBuildInfo(resource.getProject(), MakeBuilder.BUILDER_ID); IMakeBuilderInfo info = MakeCorePlugin.createBuildInfo(resource.getProject(), MakeBuilder.BUILDER_ID);
} catch (CoreException e) { workingDirectory = info.getBuildLocation();
// TODO Auto-generated catch block if (!workingDirectory.isEmpty()) {
e.printStackTrace(); IResource res = currProject.getParent().findMember(workingDirectory);
throw new RuntimeException(e); if (res instanceof IContainer && res.exists()) {
} workingDirectory = res.getLocation();
IPath workingDirectory = info.getBuildLocation(); }
if (workingDirectory != null && !workingDirectory.isEmpty()) {
IResource res = currProject.getParent().findMember(workingDirectory);
if (res instanceof IContainer && res.exists()) {
workingDirectory = res.getLocation();
} }
} catch (CoreException e) {
// TODO - FIXME
// ignore, we need to change this so that the correct
// working directory can be provided
} }
if (workingDirectory == null) { if (workingDirectory == null || workingDirectory.isEmpty()) {
workingDirectory = currProject.getLocation(); workingDirectory = currProject.getLocation();
} }
//fWorkingDirectory = resource.getProject().getLocation(); // fWorkingDirectory = resource.getProject().getLocation();
fWorkingDirectory = workingDirectory; fWorkingDirectory = workingDirectory;
fCompileCommand = new Path(buildInfo.getProviderRunCommand(providerId)); fCompileCommand = new Path(buildInfo.getProviderRunCommand(providerId));
fCompileArguments = ScannerConfigUtil.tokenizeStringWithQuotes(buildInfo.getProviderRunArguments(providerId), "\"");//$NON-NLS-1$ fCompileArguments = ScannerConfigUtil.tokenizeStringWithQuotes(buildInfo.getProviderRunArguments(providerId), "\"");//$NON-NLS-1$
return (fCompileCommand != null); return (fCompileCommand != null);
} }
/**
/**
* Add additional arguments. For example: tso - target specific options * Add additional arguments. For example: tso - target specific options
* Base class implementation returns compileArguments. * Base class implementation returns compileArguments.
* Subclasses are most likely to override default implementation. * Subclasses are most likely to override default implementation.
* *
* @param isDefaultCommand * @param isDefaultCommand
* @param collector * @param collector
* @return * @return
*/ */
protected String[] prepareArguments(boolean isDefaultCommand) { protected String[] prepareArguments(boolean isDefaultCommand) {
return fCompileArguments; return fCompileArguments;
} }
/** /**
* @param array * @param array
* @return * @return
*/ */
private String coligate(String[] array) { private String coligate(String[] array) {
StringBuffer sb = new StringBuffer(128); StringBuffer sb = new StringBuffer(128);
for (int i = 0; i < array.length; ++i) { for (int i = 0; i < array.length; ++i) {
sb.append(' '); sb.append(' ');
sb.append(array[i]); sb.append(array[i]);
} }
String ca = sb.toString(); String ca = sb.toString();
return ca; return ca;
} }
/** /**
* @param launcher * @param launcher
* @return * @return
*/ */
protected String[] setEnvironment(CommandLauncher launcher) { protected String[] setEnvironment(CommandLauncher launcher) {
// Set the environmennt, some scripts may need the CWD var to be set. // Set the environmennt, some scripts may need the CWD var to be set.
Properties props = launcher.getEnvironment(); Properties props = launcher.getEnvironment();
props.put("CWD", fWorkingDirectory.toOSString()); //$NON-NLS-1$ props.put("CWD", fWorkingDirectory.toOSString()); //$NON-NLS-1$
props.put("PWD", fWorkingDirectory.toOSString()); //$NON-NLS-1$ props.put("PWD", fWorkingDirectory.toOSString()); //$NON-NLS-1$
// On POSIX (Linux, UNIX) systems reset LANG variable to English with UTF-8 encoding // On POSIX (Linux, UNIX) systems reset LANG variable to English with UTF-8 encoding
// since GNU compilers can handle only UTF-8 characters. English language is chosen // since GNU compilers can handle only UTF-8 characters. English language is chosen
// beacuse GNU compilers inconsistently handle different locales when generating // beacuse GNU compilers inconsistently handle different locales when generating
// output of the 'gcc -v' command. Include paths with locale characters will be // output of the 'gcc -v' command. Include paths with locale characters will be
// handled properly regardless of the language as long as the encoding is set to UTF-8. // handled properly regardless of the language as long as the encoding is set to UTF-8.
if (props.containsKey(LANG_ENV_VAR)) { if (props.containsKey(LANG_ENV_VAR)) {
props.put(LANG_ENV_VAR, "en_US.UTF-8"); //$NON-NLS-1$ props.put(LANG_ENV_VAR, "en_US.UTF-8"); //$NON-NLS-1$
} }
String[] env = null; String[] env = null;
ArrayList envList = new ArrayList(); ArrayList envList = new ArrayList();
Enumeration names = props.propertyNames(); Enumeration names = props.propertyNames();
if (names != null) { if (names != null) {
while (names.hasMoreElements()) { while (names.hasMoreElements()) {
String key = (String) names.nextElement(); String key = (String)names.nextElement();
envList.add(key + "=" + props.getProperty(key)); //$NON-NLS-1$ envList.add(key + "=" + props.getProperty(key)); //$NON-NLS-1$
} }
env = (String[]) envList.toArray(new String[envList.size()]); env = (String[])envList.toArray(new String[envList.size()]);
} }
return env; return env;
} }
} }