dol: initial dol commit
[jump.git] / dol / src / dol / visitor / cbe / CbeMakefileVisitor.java
diff --git a/dol/src/dol/visitor/cbe/CbeMakefileVisitor.java b/dol/src/dol/visitor/cbe/CbeMakefileVisitor.java
new file mode 100644 (file)
index 0000000..45a4f2a
--- /dev/null
@@ -0,0 +1,151 @@
+/* $Id: CbeMakefileVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
+package dol.visitor.cbe;
+
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.util.Vector;
+
+import dol.datamodel.pn.Process;
+import dol.datamodel.pn.ProcessNetwork;
+import dol.visitor.PNVisitor;
+
+/**
+ * This class is a class for a visitor that is used to generate a CBE
+ * package Makefile.
+ */
+public class CbeMakefileVisitor extends PNVisitor {
+
+    /**
+     * Constructor.
+     *
+     * @param dir path of the Makefile
+     */
+    public CbeMakefileVisitor(String dir) {
+        _dir = dir;
+    }
+
+    /**
+     * Create a Makefile for the given process network.
+     *
+     * @param pn process network
+     */
+    public void visitComponent(ProcessNetwork pn) {
+        try {
+            String filename = _dir + _delimiter + "Makefile";
+            OutputStream file = new FileOutputStream(filename);
+            PrintStream ps = new PrintStream(file);
+
+            ps.println("##############################################");
+            ps.println("# Main Makefile for DOL application on the CBE");
+            ps.println("##############################################");
+            ps.println();
+            ps.println("# Subdirectories");
+
+            String subdirectories = "";
+            Vector<String> pList = new Vector<String>();
+            for (Process process : pn.getProcessList()) {
+                String basename = process.getBasename();
+                if (!pList.contains(basename)) {
+                    if (process.getNumOfInports() > 0
+                            && process.getNumOfOutports() > 0) {
+                        subdirectories += "spu_" + basename + " ";
+                    }
+                    pList.add(basename);
+                }
+            }
+            pList.clear();
+
+            String linkList = "";
+            String srcList = "";
+            for (Process process : pn.getProcessList()) {
+                String basename = process.getBasename();
+                if (!pList.contains(basename)) {
+                    if (!(process.getNumOfInports() > 0
+                            && process.getNumOfOutports() > 0)) {
+                        linkList += "ppu_" + basename + "/ppu_" + basename
+                                + ".o ";
+                        srcList += "$(wildcard ppu_" + basename
+                                + "/*.c) ";
+
+                    }
+                    pList.add(basename);
+                }
+            }
+            pList.clear();
+
+            ps.println("DIRS\t := " + subdirectories);
+            ps.println("");
+
+            ps.println("# General definitions:");
+            ps.println("CC = ppu-g++");
+            ps.println("CCFLAGS = -ftree-vectorize -O3 -maltivec "
+                    + "-funroll-loops -mabi=altivec -mcpu=cell");
+            ps.println("COMPILE = $(CC) $(CCFLAGS) -c");
+            ps.println("LINK = $(CC) -lspe2 -lpthread");
+            ps.println("CBE_INCLUDE = /opt/cell/sdk/src/include/ppu");
+            ps.println("LIB_INC = -I lib -I . -I $(CBE_INCLUDE)");
+            ps.println("RM = rm");
+            ps.println("ECHO = echo");
+            ps.println("EXE = ppu_main");
+            ps.println("");
+            ps.println("src := $(wildcard lib/*.c) $(wildcard *.c)");
+            ps.println("srcAll := $(wildcard lib/*.c) "
+                    + "$(wildcard lib/*.c) " + srcList);
+            ps.println("obj = $(src:.c=.o)");
+            ps.println("");
+            ps.println("$(EXE): $(obj) $(srcAll)");
+            ps.println("\tfor d in $(DIRS); do (cd $$d; $(MAKE) ); done");
+
+            for (Process process : pn.getProcessList()) {
+                String basename = process.getBasename();
+                if (!pList.contains(basename)) {
+                    if (!(process.getNumOfInports() > 0
+                            && process.getNumOfOutports() > 0)) {
+                        ps.println("\tcd ppu_" + basename
+                                + "; $(COMPILE) -o ppu_" + basename
+                                + ".o ppu_" + basename
+                                + "_wrapper.c -I .. -I ../lib;");
+                    }
+                    pList.add(basename);
+                }
+            }
+            pList.clear();
+
+            ps.println("\t$(LINK) -o $(EXE) " + linkList + " $(obj)");
+            ps.println("");
+            ps.println("%.o :");
+            ps.println("\t$(COMPILE) -o $(*D)/$(*F).o $(*D)/$(*F).c "
+                    + "$(LIB_INC)");
+            ps.println("");
+            ps.println("clean:");
+            ps.println("\tfor d in $(DIRS); do (cd $$d; $(MAKE) clean );"
+                    + " done");
+            ps.println("\trm $(obj) ");
+
+            for (Process process : pn.getProcessList()) {
+                String basename = process.getBasename();
+                if (!pList.contains(basename)) {
+                    if (!(process.getNumOfInports() > 0
+                            && process.getNumOfOutports() > 0))
+                    {
+                        ps.println("\trm ppu_" + basename + "/ppu_"
+                                + basename + ".o");
+                    }
+                    pList.add(basename);
+                }
+            }
+            pList.clear();
+
+            ps.println("\trm ppu_main");
+            ps.println("");
+
+        } catch (Exception e) {
+            System.out.println("CbeMakefileVisitor: exception occured: "
+                    + e.getMessage());
+            e.printStackTrace();
+        }
+    }
+
+    protected String _dir = null;
+}