From 943f1dbc940a8d63297f5cad077366efe9d42559 Mon Sep 17 00:00:00 2001 From: David Inglis Date: Wed, 8 Oct 2003 18:25:46 +0000 Subject: [PATCH] added a stream monitor so that we can fuge that we are getting progress --- .../eclipse/cdt/make/core/MakeBuilder.java | 37 ++-- .../cdt/make/internal/core/MakeRecon.java | 183 ++++++++++++++++++ .../cdt/make/internal/core/StreamMonitor.java | 94 +++++++++ 3 files changed, 299 insertions(+), 15 deletions(-) create mode 100644 build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/MakeRecon.java create mode 100644 build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/StreamMonitor.java diff --git a/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/core/MakeBuilder.java b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/core/MakeBuilder.java index 4f4af2179d9..76308b5846a 100644 --- a/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/core/MakeBuilder.java +++ b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/core/MakeBuilder.java @@ -19,11 +19,11 @@ import java.util.Properties; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CommandLauncher; -import org.eclipse.cdt.core.ConsoleOutputStream; import org.eclipse.cdt.core.ErrorParserManager; import org.eclipse.cdt.core.model.ICModelMarker; import org.eclipse.cdt.core.resources.ACBuilder; import org.eclipse.cdt.core.resources.IConsole; +import org.eclipse.cdt.make.internal.core.StreamMonitor; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IProject; @@ -99,7 +99,7 @@ public class MakeBuilder extends ACBuilder { if (monitor == null) { monitor = new NullProgressMonitor(); } - monitor.beginTask(MakeCorePlugin.getResourceString("MakeBuilder.Invoking_Make_Builder") + currProject.getName(), IProgressMonitor.UNKNOWN); //$NON-NLS-1$ + monitor.beginTask(MakeCorePlugin.getResourceString("MakeBuilder.Invoking_Make_Builder") + currProject.getName(), 100); //$NON-NLS-1$ try { IPath buildCommand = info.getBuildCommand(); @@ -107,7 +107,7 @@ public class MakeBuilder extends ACBuilder { IConsole console = CCorePlugin.getDefault().getConsole(); console.start(currProject); - ConsoleOutputStream cos = console.getOutputStream(); + OutputStream cos = console.getOutputStream(); // remove all markers for this project removeAllMarkers(currProject); @@ -115,11 +115,11 @@ public class MakeBuilder extends ACBuilder { IPath workingDirectory = null; if (!info.getBuildLocation().isEmpty()) { IResource res = currProject.getParent().findMember(info.getBuildLocation()); - if ( res instanceof IContainer && res.exists()) { + if (res instanceof IContainer && res.exists()) { workingDirectory = res.getLocation(); } } - if ( workingDirectory == null) { + if (workingDirectory == null) { workingDirectory = currProject.getLocation(); } String[] targets = getTargets(kind, info); @@ -148,27 +148,32 @@ public class MakeBuilder extends ACBuilder { } env = (String[]) envList.toArray(new String[envList.size()]); } - ErrorParserManager epm = new ErrorParserManager(getProject(), this, info.getErrorParsers()); - epm.setOutputStream(cos); - OutputStream stdout = epm.getOutputStream(); - OutputStream stderr = epm.getOutputStream(); - String[] buildArguments = targets; if (info.isDefaultBuildCmd()) { - if ( !info.isStopOnError()) { + if (!info.isStopOnError()) { buildArguments = new String[targets.length + 1]; buildArguments[0] = "-k"; //$NON-NLS-1$ System.arraycopy(targets, 0, buildArguments, 1, targets.length); } } else { String args = info.getBuildArguments(); - if ( args != null && !args.equals("")) { //$NON-NLS-1$ - String[] newArgs = makeArray(args); + if (args != null && !args.equals("")) { //$NON-NLS-1$ + String[] newArgs = makeArray(args); buildArguments = new String[targets.length + newArgs.length]; System.arraycopy(newArgs, 0, buildArguments, 0, newArgs.length); System.arraycopy(targets, 0, buildArguments, newArgs.length, targets.length); } } + if (true) { +// MakeRecon recon = new MakeRecon(buildCommand, buildArguments, env, workingDirectory, makeMonitor, cos); +// recon.invokeMakeRecon(); +// cos = recon; + cos = new StreamMonitor(new SubProgressMonitor(monitor, 100), cos); + } + ErrorParserManager epm = new ErrorParserManager(getProject(), this, info.getErrorParsers()); + epm.setOutputStream(cos); + OutputStream stdout = epm.getOutputStream(); + OutputStream stderr = epm.getOutputStream(); Process p = launcher.execute(buildCommand, buildArguments, env, workingDirectory); if (p != null) { try { @@ -177,9 +182,9 @@ public class MakeBuilder extends ACBuilder { p.getOutputStream().close(); } catch (IOException e) { } - if (launcher.waitAndRead(stdout, stderr, new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN)) != CommandLauncher.OK) + if (launcher.waitAndRead(stdout, stderr, new SubProgressMonitor(monitor, 0)) + != CommandLauncher.OK) errMsg = launcher.getErrorMessage(); - monitor.subTask(MakeCorePlugin.getResourceString("MakeBuilder.Updating_project")); //$NON-NLS-1$ try { @@ -192,6 +197,7 @@ public class MakeBuilder extends ACBuilder { } else { errMsg = launcher.getErrorMessage(); } +// makeMonitor.done(); if (errMsg != null) { StringBuffer buf = new StringBuffer(buildCommand.toString() + " "); //$NON-NLS-1$ @@ -213,6 +219,7 @@ public class MakeBuilder extends ACBuilder { monitor.subTask(MakeCorePlugin.getResourceString("MakeBuilder.Creating_Markers")); //$NON-NLS-1$ epm.reportProblems(); + cos.close(); } } catch (Exception e) { CCorePlugin.log(e); diff --git a/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/MakeRecon.java b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/MakeRecon.java new file mode 100644 index 00000000000..2ae764748f4 --- /dev/null +++ b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/MakeRecon.java @@ -0,0 +1,183 @@ +/********************************************************************** + * Copyright (c) 2002,2003 QNX Software Systems and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * QNX Software Systems - Initial API and implementation +***********************************************************************/ +package org.eclipse.cdt.make.internal.core; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.StringReader; + +import org.eclipse.cdt.utils.spawner.ProcessFactory; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; + +public class MakeRecon extends OutputStream { + + IPath make; + String[] args; + String[] environ; + IPath directory; + IProgressMonitor monitor; + OutputStream console; + BufferedReader log; + StringBuffer currentLine; + String expectation; + + public MakeRecon( + IPath buildCommand, + String[] buildArguments, + String[] env, + IPath workingDirectory, + IProgressMonitor mon, + OutputStream cos) { + make = buildCommand; + if (buildArguments != null) { + args = new String[buildArguments.length + 1]; + args[0] = "-n"; + System.arraycopy(buildArguments, 0, args, 1, buildArguments.length); + } else { + args = new String[] { "-n" }; + } + environ = env; + directory = workingDirectory; + monitor = mon; + console = cos; + currentLine = new StringBuffer(); + } + + public void invokeMakeRecon() { + int i = 0; + String[] array = new String[args.length + 1]; + array[0] = make.toOSString(); + System.arraycopy(args, 0, array, 1, args.length); + Process p; + try { + p = ProcessFactory.getFactory().exec(array, environ, directory.toFile()); + InputStream in = p.getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + // Swallow the output + String line; + StringBuffer sb = new StringBuffer(); + try { + while ((line = reader.readLine()) != null) { + sb.append(line).append('\n'); + i++; + } + } catch (IOException e) { + } + try { + in.close(); + } catch (IOException e) { + } + p.destroy(); + log = new BufferedReader(new StringReader(sb.toString())); + } catch (IOException e1) { + i = IProgressMonitor.UNKNOWN; + } + if (monitor != null) { + monitor.beginTask("", i); + } + } + + /** + * @see java.io.OutputStream#close() + */ + public void close() throws IOException { + if (console != null) { + console.close(); + } + } + + /** + * @see java.io.OutputStream#flush() + */ + public void flush() throws IOException { + if (console != null) { + console.flush(); + } + } + + /** + * @see java.io.OutputStream#write(int) + */ + public synchronized void write(int b) throws IOException { + currentLine.append((char) b); + checkProgress(false); + if (console != null) { + console.write(b); + } + } + + /** + * @see java.io.OutputStream#write(...) + */ + public synchronized void write(byte[] b, int off, int len) throws IOException { + if (b == null) { + throw new NullPointerException(); + } else if (off != 0 || (len < 0) || (len > b.length)) { + throw new IndexOutOfBoundsException(); + } else if (len == 0) { + return; + } + currentLine.append(new String(b, 0, len)); + checkProgress(false); + if (console != null) { + console.write(b, off, len); + } + } + + private void checkProgress(boolean flush) { + String buffer = currentLine.toString(); + int i = 0; + while ((i = buffer.indexOf("\n")) != -1) { + String line = buffer.substring(0, i).trim(); // get rid of any trailing \r + processLine(line); + buffer = buffer.substring(i + 1); // skip the \n and advance + } + currentLine.setLength(0); + if (flush) { + if (buffer.length() > 0) { + processLine(buffer); + } + } else { + currentLine.append(buffer); + } + } + + private void processLine(String line) { + if (expectation == null) { + try { + expectation = log.readLine(); + if (expectation != null) { + String show; + if (expectation.length() > 150) { + show = expectation.substring(0, 150); + } else { + show = expectation; + } + monitor.subTask(show); + } + } catch (IOException e) { + } + if (expectation == null) { + expectation = ""; + } + } + if (expectation.startsWith(line)) { + expectation = null; + if (monitor != null) { + monitor.worked(1); + } + } + } +} diff --git a/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/StreamMonitor.java b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/StreamMonitor.java new file mode 100644 index 00000000000..a08b3809edd --- /dev/null +++ b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/StreamMonitor.java @@ -0,0 +1,94 @@ +/********************************************************************** + * Copyright (c) 2002,2003 QNX Software Systems and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * QNX Software Systems - Initial API and implementation +***********************************************************************/ +package org.eclipse.cdt.make.internal.core; + +import java.io.IOException; +import java.io.OutputStream; + +import org.eclipse.core.runtime.IProgressMonitor; + +public class StreamMonitor extends OutputStream { + + IProgressMonitor monitor; + OutputStream console; + public static final int TOTAL_WORK = 10; + private int halfWay = TOTAL_WORK / 2; + private int currentIncrement = 1; + private int nextProgress = currentIncrement; + private int worked = 0; + + public StreamMonitor(IProgressMonitor mon, OutputStream cos) { + monitor = mon; + console = cos; + monitor.beginTask("", TOTAL_WORK); + } + + private void progressUpdate() { + if (--nextProgress <= 0) { + //we have exhausted the current increment, so report progress + monitor.worked(1); + worked++; + if (worked >= halfWay) { + //we have passed the current halfway point, so double the + //increment and reset the halfway point. + currentIncrement *= 2; + halfWay += (TOTAL_WORK - halfWay) / 2; + } + //reset the progress counter to another full increment + nextProgress = currentIncrement; + } + } + /** + * @see java.io.OutputStream#close() + */ + public void close() throws IOException { + if (console != null) { + console.close(); + } + monitor.done(); + } + + /** + * @see java.io.OutputStream#flush() + */ + public void flush() throws IOException { + if (console != null) { + console.flush(); + } + } + + /** + * @see java.io.OutputStream#write(int) + */ + public synchronized void write(int b) throws IOException { + if (console != null) { + console.write(b); + } + progressUpdate(); + } + + /** + * @see java.io.OutputStream#write(...) + */ + public synchronized void write(byte[] b, int off, int len) throws IOException { + if (b == null) { + throw new NullPointerException(); + } else if (off != 0 || (len < 0) || (len > b.length)) { + throw new IndexOutOfBoundsException(); + } else if (len == 0) { + return; + } + if (console != null) { + console.write(b, off, len); + } + progressUpdate(); + } +}