dol: initial dol commit
[jump.git] / dol / src / dol / visitor / cbe / CbeMakefileVisitor.java
1 /* $Id: CbeMakefileVisitor.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.visitor.cbe;
3
4 import java.io.FileOutputStream;
5 import java.io.OutputStream;
6 import java.io.PrintStream;
7 import java.util.Vector;
8
9 import dol.datamodel.pn.Process;
10 import dol.datamodel.pn.ProcessNetwork;
11 import dol.visitor.PNVisitor;
12
13 /**
14  * This class is a class for a visitor that is used to generate a CBE
15  * package Makefile.
16  */
17 public class CbeMakefileVisitor extends PNVisitor {
18
19     /**
20      * Constructor.
21      *
22      * @param dir path of the Makefile
23      */
24     public CbeMakefileVisitor(String dir) {
25         _dir = dir;
26     }
27
28     /**
29      * Create a Makefile for the given process network.
30      *
31      * @param pn process network
32      */
33     public void visitComponent(ProcessNetwork pn) {
34         try {
35             String filename = _dir + _delimiter + "Makefile";
36             OutputStream file = new FileOutputStream(filename);
37             PrintStream ps = new PrintStream(file);
38
39             ps.println("##############################################");
40             ps.println("# Main Makefile for DOL application on the CBE");
41             ps.println("##############################################");
42             ps.println();
43             ps.println("# Subdirectories");
44
45             String subdirectories = "";
46             Vector<String> pList = new Vector<String>();
47             for (Process process : pn.getProcessList()) {
48                 String basename = process.getBasename();
49                 if (!pList.contains(basename)) {
50                     if (process.getNumOfInports() > 0
51                             && process.getNumOfOutports() > 0) {
52                         subdirectories += "spu_" + basename + " ";
53                     }
54                     pList.add(basename);
55                 }
56             }
57             pList.clear();
58
59             String linkList = "";
60             String srcList = "";
61             for (Process process : pn.getProcessList()) {
62                 String basename = process.getBasename();
63                 if (!pList.contains(basename)) {
64                     if (!(process.getNumOfInports() > 0
65                             && process.getNumOfOutports() > 0)) {
66                         linkList += "ppu_" + basename + "/ppu_" + basename
67                                 + ".o ";
68                         srcList += "$(wildcard ppu_" + basename
69                                 + "/*.c) ";
70
71                     }
72                     pList.add(basename);
73                 }
74             }
75             pList.clear();
76
77             ps.println("DIRS\t := " + subdirectories);
78             ps.println("");
79
80             ps.println("# General definitions:");
81             ps.println("CC = ppu-g++");
82             ps.println("CCFLAGS = -ftree-vectorize -O3 -maltivec "
83                     + "-funroll-loops -mabi=altivec -mcpu=cell");
84             ps.println("COMPILE = $(CC) $(CCFLAGS) -c");
85             ps.println("LINK = $(CC) -lspe2 -lpthread");
86             ps.println("CBE_INCLUDE = /opt/cell/sdk/src/include/ppu");
87             ps.println("LIB_INC = -I lib -I . -I $(CBE_INCLUDE)");
88             ps.println("RM = rm");
89             ps.println("ECHO = echo");
90             ps.println("EXE = ppu_main");
91             ps.println("");
92             ps.println("src := $(wildcard lib/*.c) $(wildcard *.c)");
93             ps.println("srcAll := $(wildcard lib/*.c) "
94                     + "$(wildcard lib/*.c) " + srcList);
95             ps.println("obj = $(src:.c=.o)");
96             ps.println("");
97             ps.println("$(EXE): $(obj) $(srcAll)");
98             ps.println("\tfor d in $(DIRS); do (cd $$d; $(MAKE) ); done");
99
100             for (Process process : pn.getProcessList()) {
101                 String basename = process.getBasename();
102                 if (!pList.contains(basename)) {
103                     if (!(process.getNumOfInports() > 0
104                             && process.getNumOfOutports() > 0)) {
105                         ps.println("\tcd ppu_" + basename
106                                 + "; $(COMPILE) -o ppu_" + basename
107                                 + ".o ppu_" + basename
108                                 + "_wrapper.c -I .. -I ../lib;");
109                     }
110                     pList.add(basename);
111                 }
112             }
113             pList.clear();
114
115             ps.println("\t$(LINK) -o $(EXE) " + linkList + " $(obj)");
116             ps.println("");
117             ps.println("%.o :");
118             ps.println("\t$(COMPILE) -o $(*D)/$(*F).o $(*D)/$(*F).c "
119                     + "$(LIB_INC)");
120             ps.println("");
121             ps.println("clean:");
122             ps.println("\tfor d in $(DIRS); do (cd $$d; $(MAKE) clean );"
123                     + " done");
124             ps.println("\trm $(obj) ");
125
126             for (Process process : pn.getProcessList()) {
127                 String basename = process.getBasename();
128                 if (!pList.contains(basename)) {
129                     if (!(process.getNumOfInports() > 0
130                             && process.getNumOfOutports() > 0))
131                     {
132                         ps.println("\trm ppu_" + basename + "/ppu_"
133                                 + basename + ".o");
134                     }
135                     pList.add(basename);
136                 }
137             }
138             pList.clear();
139
140             ps.println("\trm ppu_main");
141             ps.println("");
142
143         } catch (Exception e) {
144             System.out.println("CbeMakefileVisitor: exception occured: "
145                     + e.getMessage());
146             e.printStackTrace();
147         }
148     }
149
150     protected String _dir = null;
151 }