68
|
1 /*
|
|
2 Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com>
|
|
3
|
|
4 Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5 of this software and associated documentation files (the "Software"), to deal
|
|
6 in the Software without restriction, including without limitation the rights
|
|
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8 copies of the Software, and to permit persons to whom the Software is
|
|
9 furnished to do so, subject to the following conditions:
|
|
10
|
|
11 The above copyright notice and this permission notice shall be included in
|
|
12 all copies or substantial portions of the Software.
|
|
13
|
|
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20 THE SOFTWARE.
|
|
21 */
|
|
22
|
|
23 package fschmidt.tools;
|
|
24
|
|
25 import java.io.IOException;
|
|
26 import java.io.PrintWriter;
|
|
27 import java.io.File;
|
|
28 import java.io.BufferedOutputStream;
|
|
29 import java.io.FileOutputStream;
|
|
30 import java.util.Set;
|
|
31 import java.util.HashSet;
|
|
32 import java.util.List;
|
|
33 import java.util.ArrayList;
|
|
34 import java.util.Date;
|
|
35 import java.util.regex.Pattern;
|
|
36
|
|
37
|
|
38 /**
|
|
39 * Doing "java fschmidt.tools.Mmake" on the command line will build
|
|
40 * make files in the current directory and all sub-directories.
|
|
41 *
|
|
42 * @author frank
|
|
43 */
|
|
44 public final class Mmake {
|
|
45 private static String compiler = "javac -g -encoding UTF8";
|
|
46 private static String toolsPath;
|
|
47 private static String[] classpath = System.getProperty("java.class.path").split(Pattern.quote(File.pathSeparator),-1);
|
|
48
|
|
49 static String findPathTo(String file) {
|
|
50 for( String s : classpath ) {
|
|
51 File path = new File(s);
|
|
52 if( path.isAbsolute() && new File(path,file).exists() )
|
|
53 return path.getPath() + File.separator;
|
|
54 }
|
|
55 return null;
|
|
56 }
|
|
57
|
|
58 public static void main(String args[]) throws Exception {
|
|
59 for( int i=0; i<args.length; i++ ) {
|
|
60 compiler += " " + args[i];
|
|
61 }
|
|
62 String subpath = "fschmidt"+File.separator+"tools";
|
|
63 String path = findPathTo(subpath+File.separator+"Mmake.java");
|
|
64 if( path != null ) {
|
|
65 toolsPath = (path+subpath).replace('\\','/');
|
|
66 }
|
|
67 mmake(".");
|
|
68 }
|
|
69
|
|
70 private static class R {
|
|
71 boolean java = false;
|
|
72 boolean j_p = false;
|
|
73 boolean rmi = false;
|
|
74 boolean jjt = false;
|
|
75 }
|
|
76
|
|
77 static R mmake(String dir)
|
|
78 throws IOException
|
|
79 {
|
|
80 R rtn = new R();
|
|
81 PrintWriter out = null;
|
|
82 File dirF = new File(dir);
|
|
83 String[] ls = dirF.list();
|
|
84 if( ls == null )
|
|
85 return rtn;
|
|
86 Set<String> java = new HashSet<String>();
|
|
87 Set<String> jtp = new HashSet<String>();
|
|
88 Set<String> jmp = new HashSet<String>();
|
|
89 Set<String> rmi = new HashSet<String>();
|
|
90 Set<String> jjt = new HashSet<String>();
|
|
91 List<String> dirs = new ArrayList<String>();
|
|
92 List<String> dirs2 = new ArrayList<String>();
|
|
93 List<String> dirsRmi = new ArrayList<String>();
|
|
94 List<String> dirsJjt = new ArrayList<String>();
|
|
95 for( int i=0; i<ls.length; i++ ) {
|
|
96 String file = ls[i];
|
|
97 if( file.endsWith(".java") ) {
|
|
98 String s = file.substring(0,file.length()-5);
|
|
99 java.add(s);
|
|
100 }
|
|
101 if( file.endsWith(".jtp") ) {
|
|
102 String s = file.substring(0,file.length()-4);
|
|
103 java.add(s);
|
|
104 jtp.add(s);
|
|
105 }
|
|
106 if( file.endsWith(".jmp") ) {
|
|
107 String s = file.substring(0,file.length()-4);
|
|
108 java.add(s);
|
|
109 jmp.add(s);
|
|
110 }
|
|
111 if( file.endsWith(".rmi") ) {
|
|
112 String s = file.substring(0,file.length()-4);
|
|
113 rmi.add(s);
|
|
114 }
|
|
115 if( file.endsWith(".jjt") ) {
|
|
116 String s = file.substring(0,file.length()-4);
|
|
117 jjt.add(s);
|
|
118 }
|
|
119 String d = dir + "/" + file;
|
|
120 if( new File(d).isDirectory() ) {
|
|
121 R r = mmake(d);
|
|
122 if( r.java ) {
|
|
123 dirs.add(file);
|
|
124 if( r.j_p )
|
|
125 dirs2.add(file);
|
|
126 if( r.rmi )
|
|
127 dirsRmi.add(file);
|
|
128 if( r.jjt )
|
|
129 dirsJjt.add(file);
|
|
130 }
|
|
131 }
|
|
132 }
|
|
133 if( java.isEmpty() && dirs.isEmpty() )
|
|
134 return rtn;
|
|
135 rtn.java = true;
|
|
136 String[] a;
|
|
137 out = init(dir);
|
|
138 if( !java.isEmpty() ) {
|
|
139 a = (String[])java.toArray(new String[0]);
|
|
140 for( int i=0; i<a.length; i++ ) {
|
|
141 out.println("\\");
|
|
142 out.print("\t\t"+a[i]+".class ");
|
|
143 }
|
|
144 }
|
|
145 out.println();
|
|
146 String[] subs = (String[])dirs.toArray(new String[0]);
|
|
147 int n = subs.length;
|
|
148 for( int i=0; i<n; i++ )
|
|
149 out.println("\tcd `pwd`/"+subs[i]+"; make _src");
|
|
150
|
|
151 rtn.j_p = !jtp.isEmpty() || !jmp.isEmpty() || !dirs2.isEmpty();
|
|
152 out.println();
|
|
153 if( !rtn.j_p ) {
|
|
154 out.println("_java:");
|
|
155 } else {
|
|
156 if( toolsPath==null ) {
|
|
157 out.print("_java:\t");
|
|
158 } else {
|
|
159 boolean hasJtp = !jtp.isEmpty();
|
|
160 boolean hasJmp = !jmp.isEmpty();
|
|
161 if( hasJtp ) {
|
|
162 out.println(toolsPath+"/Jtp.class: "+toolsPath+"/Jtp.java");
|
|
163 out.println("\tjavac "+toolsPath+"/Jtp.java");
|
|
164 out.println();
|
|
165 }
|
|
166 if( hasJmp ) {
|
|
167 out.println(toolsPath+"/Jmp.class: "+toolsPath+"/Jmp.java");
|
|
168 out.println("\tjavac "+toolsPath+"/Jmp.java");
|
|
169 out.println();
|
|
170 }
|
|
171 out.print("_java:\t");
|
|
172 if( hasJtp )
|
|
173 out.print(toolsPath+"/Jtp.class ");
|
|
174 if( hasJmp )
|
|
175 out.print(toolsPath+"/Jmp.class ");
|
|
176 }
|
|
177 a = (String[])jtp.toArray(new String[0]);
|
|
178 for( int i=0; i<a.length; i++ ) {
|
|
179 out.println("\\");
|
|
180 out.print("\t\t"+a[i]+".java ");
|
|
181 }
|
|
182 a = (String[])jmp.toArray(new String[0]);
|
|
183 for( int i=0; i<a.length; i++ ) {
|
|
184 out.println("\\");
|
|
185 out.print("\t\t"+a[i]+".java ");
|
|
186 }
|
|
187 out.println();
|
|
188 String[] subs2 = (String[])dirs2.toArray(new String[0]);
|
|
189 int n2 = subs2.length;
|
|
190 for( int i=0; i<n2; i++ )
|
|
191 out.println("\tcd `pwd`/"+subs2[i]+"; make _java");
|
|
192 }
|
|
193
|
|
194 rtn.rmi = !rmi.isEmpty() || !dirsRmi.isEmpty();
|
|
195 out.println();
|
|
196 out.print("_rmi: ");
|
|
197 if( !rtn.rmi ) {
|
|
198 out.println();
|
|
199 } else {
|
|
200 a = (String[])rmi.toArray(new String[0]);
|
|
201 for( int i=0; i<a.length; i++ ) {
|
|
202 String s = a[i];
|
|
203 out.println("\\");
|
|
204 out.print("\t\t"+s+"_Stub.class ");
|
|
205 }
|
|
206 out.println();
|
|
207 String absPath = dirF.getCanonicalPath();
|
|
208 File cPath = null;
|
|
209 for( String path : classpath ) {
|
|
210 File f = new File(path);
|
|
211 if( !f.isAbsolute() )
|
|
212 continue;
|
|
213 if( absPath.startsWith(f.getCanonicalPath()) ) {
|
|
214 cPath = f.getCanonicalFile();
|
|
215 break;
|
|
216 }
|
|
217 }
|
|
218 if( cPath==null )
|
|
219 throw new RuntimeException(dir+" not in classpath");
|
|
220 StringBuilder clsPathBuf = new StringBuilder();
|
|
221 StringBuilder topBuf = new StringBuilder( "." );
|
|
222 for( File f=dirF.getCanonicalFile(); !f.equals(cPath); f=f.getParentFile() ) {
|
|
223 clsPathBuf.insert(0, f.getName() + '.' );
|
|
224 topBuf.append( File.separator + ".." );
|
|
225 }
|
|
226 String clsPath = clsPathBuf.toString();
|
|
227 String top = topBuf.toString();
|
|
228 for( int i=0; i<a.length; i++ ) {
|
|
229 String s = a[i];
|
|
230 out.println();
|
|
231 out.println(s+"_Stub.class: "+s+".class");
|
|
232 out.println("\trmic -d '"+top+"' "+clsPath+s);
|
|
233 }
|
|
234 String[] subs2 = (String[])dirsRmi.toArray(new String[0]);
|
|
235 int n2 = subs2.length;
|
|
236 for( int i=0; i<n2; i++ )
|
|
237 out.println("\tcd `pwd`/"+subs2[i]+"; make _rmi");
|
|
238 }
|
|
239
|
|
240 rtn.jjt = !jjt.isEmpty() || !dirsJjt.isEmpty();
|
|
241 out.println();
|
|
242 out.print("_jjt: ");
|
|
243 if( rtn.jjt ) {
|
|
244 a = (String[])jjt.toArray(new String[0]);
|
|
245 for( int i=0; i<a.length; i++ ) {
|
|
246 out.println("\\");
|
|
247 out.print("\t\t"+a[i]+".jjt_done ");
|
|
248 }
|
|
249 out.println();
|
|
250 String[] subs2 = (String[])dirsJjt.toArray(new String[0]);
|
|
251 int n2 = subs2.length;
|
|
252 for( int i=0; i<n2; i++ )
|
|
253 out.println("\tcd `pwd`/"+subs2[i]+"; make _jjt");
|
|
254 }
|
|
255
|
|
256 out.println();
|
|
257 out.println("clean:");
|
|
258 out.print("\trm -f *.class *.jjt_done");
|
|
259 a = (String[])jtp.toArray(new String[0]);
|
|
260 for( int i=0; i<a.length; i++ ) {
|
|
261 out.print(" "+a[i]+".java");
|
|
262 }
|
|
263 a = (String[])jmp.toArray(new String[0]);
|
|
264 for( int i=0; i<a.length; i++ ) {
|
|
265 out.print(" "+a[i]+".java");
|
|
266 }
|
|
267 out.println();
|
|
268 for( int i=0; i<n; i++ )
|
|
269 out.println("\tcd `pwd`/"+subs[i]+"; make clean");
|
|
270 out.println();
|
|
271 out.close();
|
|
272 System.out.println(dir);
|
|
273
|
|
274 return rtn;
|
|
275 }
|
|
276
|
|
277 static PrintWriter init(String dir)
|
|
278 throws IOException
|
|
279 {
|
|
280 PrintWriter out = new PrintWriter(new BufferedOutputStream(
|
|
281 new FileOutputStream(dir+"/Makefile")
|
|
282 ));
|
|
283 out.println("# Makefile created on "+new Date()+" by Mmake");
|
|
284 out.println();
|
|
285 out.println(".SUFFIXES: .jtp .jmp .java .class .jjt .jjt_done");
|
|
286 out.println();
|
|
287 out.println(".java.class:");
|
|
288 out.println("\t"+compiler+" $<");
|
|
289 out.println();
|
|
290 out.println(".jtp.java:");
|
|
291 out.println("\tjava fschmidt.tools.Jtp $<");
|
|
292 out.println();
|
|
293 out.println(".jmp.java:");
|
|
294 out.println("\tjava fschmidt.tools.Jmp $<");
|
|
295 out.println();
|
|
296 out.println(".jjt.jjt_done:");
|
|
297 out.println("\tjava jjtree $<");
|
|
298 out.println("\tjava javacc $*.jj");
|
|
299 out.println("\ttouch $@");
|
|
300 out.println();
|
|
301 out.println("all: _jjt _java _src _rmi");
|
|
302 out.println();
|
|
303 out.println("hlp:");
|
|
304 out.println("\t@echo \"Usage: make [all|clean]\"");
|
|
305 out.println();
|
|
306 out.print("_src: ");
|
|
307 return out;
|
|
308 }
|
|
309 }
|