summaryrefslogtreecommitdiff
path: root/src/main/java/net/sf/antcontrib/cpptasks/CCTask.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/net/sf/antcontrib/cpptasks/CCTask.java')
-rw-r--r--src/main/java/net/sf/antcontrib/cpptasks/CCTask.java250
1 files changed, 246 insertions, 4 deletions
diff --git a/src/main/java/net/sf/antcontrib/cpptasks/CCTask.java b/src/main/java/net/sf/antcontrib/cpptasks/CCTask.java
index 2e6adab..ab40e95 100644
--- a/src/main/java/net/sf/antcontrib/cpptasks/CCTask.java
+++ b/src/main/java/net/sf/antcontrib/cpptasks/CCTask.java
@@ -16,10 +16,15 @@
*/
package net.sf.antcontrib.cpptasks;
import java.io.File;
+import java.io.FileFilter;
import java.io.IOException;
+import java.util.ArrayList;
import java.util.Enumeration;
+import java.util.HashSet;
import java.util.Hashtable;
-import java.util.*;
+import java.util.List;
+import java.util.Set;
+import java.util.Vector;
import net.sf.antcontrib.cpptasks.compiler.CompilerConfiguration;
import net.sf.antcontrib.cpptasks.compiler.LinkType;
@@ -120,6 +125,12 @@ import org.apache.tools.ant.types.Environment;
* @author Curt Arnold
*/
public class CCTask extends Task {
+
+
+
+
+
+
private static class SystemLibraryCollector implements FileVisitor {
private Hashtable libraries;
private Linker linker;
@@ -180,6 +191,8 @@ public class CCTask extends Task {
}
return targetsByConfig;
}
+ // FREEHEP
+ private int maxCores = 0;
/** The compiler definitions. */
private Vector _compilers = new Vector();
/** The output file type. */
@@ -706,6 +719,11 @@ public class CCTask extends Task {
}
}
}
+ // BEGINFREEHEP
+ Progress progress = new Progress(getObjdir(), rebuildCount);
+ progress.start();
+ // ENDFREEHEP
+
for (int i = 0; i < targetVectors.length; i++) {
//
// get the targets for this configuration
@@ -719,6 +737,100 @@ public class CCTask extends Task {
//
// prepare the list of source files
//
+
+ // BEGINFREEHEP
+ int noOfCores = Runtime.getRuntime().availableProcessors();
+ log("Found "+noOfCores+" processors available");
+ if (maxCores > 0) {
+ noOfCores = Math.min(maxCores, noOfCores);
+ log("Limited processors to "+noOfCores);
+ }
+ int noOfFiles = targetsForConfig.size();
+ if (noOfFiles < noOfCores) {
+ noOfCores = noOfFiles;
+ log("Limited used processors to "+noOfCores);
+ }
+
+ Set[] sourceFiles = new HashSet[noOfCores];
+ for (int j = 0; j < sourceFiles.length; j++) {
+ sourceFiles[j] = new HashSet(noOfFiles
+ / sourceFiles.length);
+ }
+ Enumeration targetsEnum = targetsForConfig.elements();
+ index = 0;
+ while (targetsEnum.hasMoreElements()) {
+ TargetInfo targetInfo = ((TargetInfo) targetsEnum
+ .nextElement());
+ sourceFiles[index++].add(targetInfo.getSources()[0]
+ .toString());
+ index %= sourceFiles.length;
+ }
+
+ // setup cores/cpus
+ Core[] cores = new Core[noOfCores];
+ for (int j = 0; j < cores.length; j++) {
+ cores[j] = new Core(this, j, config, _objDir, sourceFiles[j],
+ relentless, monitor);
+ log("\nStarting Core " + j + " with "
+ + sourceFiles[j].size() + " source files...");
+ }
+
+ // starting cores
+ for (int j = 0; j < cores.length; j++) {
+ cores[j].start();
+ }
+
+ // checking cores
+ boolean alive = false;
+ try {
+ do {
+ alive = false;
+ for (int j = 0; j < cores.length; j++) {
+ if (cores[j] != null) {
+ if (cores[j].isAlive()) {
+ alive = true;
+ } else {
+ Exception exception = cores[j].getException();
+ if (exception != null) {
+ if ((compileException == null) && (exception instanceof BuildException)) {
+ compileException = (BuildException)exception;
+ } else {
+ log(cores[j].getName()+" "+exception+" ", Project.MSG_ERR);
+ }
+ if (!relentless) {
+ cores[j] = null;
+ alive = false;
+ break;
+ }
+ }
+ cores[j] = null;
+ }
+ }
+ }
+ if (alive) {
+ // wait for a maximum of 5 seconds or #files*2 seconds.
+ Thread.sleep(Math.min(5000, sourceFiles[0].size()*2000));
+ }
+ } while (alive);
+ } catch (InterruptedException e) {
+ break;
+ }
+
+ // killing leftovers
+ for (int j = 0; j < cores.length; j++) {
+ if (cores[j] != null) {
+ cores[j].interrupt();
+ log(cores[j].getName()+" interrupted ");
+ }
+ }
+
+ if ((!relentless) && (compileException != null)) {
+ break;
+ }
+ // ENDFREEHEP
+
+
+ /* OLD CODE
String[] sourceFiles = new String[targetsForConfig.size()];
Enumeration targetsEnum = targetsForConfig.elements();
index = 0;
@@ -738,7 +850,17 @@ public class CCTask extends Task {
if (!relentless)
break;
}
+ */
}
+
+ // BEGINFREEHEP
+ progress.exit();
+ try {
+ progress.join();
+ } catch (InterruptedException ex) {
+ }
+ // ENDFREEHEP
+
//
// save the details of the object file compilation
// settings to disk for dependency analysis
@@ -796,9 +918,12 @@ public class CCTask extends Task {
//
File output = linkTarget.getOutput();
if (linkTarget.getRebuild()) {
- log("Starting link");
LinkerConfiguration linkConfig = (LinkerConfiguration) linkTarget
.getConfiguration();
+ // BEGINFREEHEP
+ log("Linking...");
+ log("Starting link {" + linkConfig.getIdentifier() + "}");
+ // ENDFREEHEP
if (failOnError) {
linkConfig.link(this, linkTarget);
} else {
@@ -825,6 +950,101 @@ public class CCTask extends Task {
}
}
}
+
+ // BEGINFREEHEP
+ class Core extends Thread {
+ private CCTask task;
+ private CompilerConfiguration config;
+ private File objDir;
+ private Set sourceFiles;
+ private boolean relentless;
+ private CCTaskProgressMonitor monitor;
+ private Exception compileException;
+
+ Core(CCTask task, int coreNo, CompilerConfiguration config, File objDir,
+ Set set, boolean relentless,
+ CCTaskProgressMonitor monitor) {
+ super("Core "+coreNo);
+ this.task = task;
+ this.config = config;
+ this.objDir = objDir;
+ this.sourceFiles = set;
+ this.relentless = relentless;
+ this.monitor = monitor;
+ }
+
+ public Exception getException() {
+ return compileException;
+ }
+
+ public void run() {
+ super.run();
+ try {
+ String[] sources = new String[sourceFiles.size()];
+ sources = (String[]) sourceFiles.toArray(sources);
+ config.compile(task, objDir, sources, relentless, monitor);
+ } catch (Exception ex) {
+ if (compileException == null) {
+ compileException = ex;
+ }
+ }
+ }
+ }
+
+ // ENDFREEHEP
+
+ // BEGINFREEHEP
+ class Progress extends Thread {
+
+ private boolean stop = false;
+ private File objDir;
+ private int rebuildCount;
+
+ public Progress(File objDir, int rebuildCount) {
+ this.objDir = objDir;
+ this.rebuildCount = rebuildCount;
+ }
+
+ public void run() {
+ if (rebuildCount < 10)
+ return;
+ try {
+ FileFilter updatedFiles = new FileFilter() {
+ private long startTime = System.currentTimeMillis();
+
+ public boolean accept(File file) {
+ return file.lastModified() > startTime
+ && !file.getName().endsWith(".xml");
+ }
+ };
+ while (!stop) {
+ System.err.print("\r"
+ + objDir.listFiles(updatedFiles).length + " / "
+ + rebuildCount + " files compiled...");
+ System.err.print("\r");
+ System.err.flush();
+ if (!stop) {
+ Thread.sleep(5000);
+ }
+ }
+ } catch (InterruptedException e) {
+ }
+ System.err
+ .print("\r ");
+ System.err.print("\r");
+ System.err.flush();
+ log(Integer.toString(rebuildCount) + " files were compiled.");
+ }
+
+ public void exit() {
+ stop = true;
+ }
+
+ }
+
+ // ENDFREEHEP
+
+
/**
* Gets the dataset.
*
@@ -1103,6 +1323,18 @@ public class CCTask extends Task {
public void setLink(OutputTypeEnum outputType) {
linkType.setOutputType(outputType);
}
+
+ // BEGINFREEHEP
+ public void setLinkCPP(boolean linkCPP) {
+ linkType.setLinkCPP(linkCPP);
+ }
+
+ public void setLinkFortran(boolean linkFortran) {
+ linkType.setLinkFortran(linkFortran);
+ }
+
+ // ENDFREEHEP
+
/**
* Enables or disables generation of multithreaded code
*
@@ -1372,7 +1604,18 @@ public class CCTask extends Task {
public void setWarnings(WarningLevelEnum level) {
compilerDef.setWarnings(level);
}
-
+
+ // BEGINFREEHEP
+ public void setMaxCores(int maxCores) {
+ this.maxCores = maxCores;
+ }
+
+ public int getMaxCores() {
+ return maxCores;
+ }
+
+ // ENDFREEHEP
+
/**
* Indicates whether the build will continue
* even if there are compilation errors; defaults to true.
@@ -1436,5 +1679,4 @@ public class CCTask extends Task {
newVersionInfo.setProject(this.getProject());
versionInfos.addElement(newVersionInfo);
}
-
}