2009-10-27 06:12:24 +00:00
|
|
|
/*******************************************************************************
|
2012-10-05 04:26:25 +00:00
|
|
|
* Copyright (c) 2009-2012 Luaj.org. All rights reserved.
|
2009-10-27 06:12:24 +00:00
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
* THE SOFTWARE.
|
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
2010-07-30 00:16:12 +00:00
|
|
|
import java.io.File;
|
2009-10-27 06:12:24 +00:00
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
2013-09-18 05:32:30 +00:00
|
|
|
import java.io.InputStreamReader;
|
2010-07-30 00:16:12 +00:00
|
|
|
import java.util.ArrayList;
|
2009-11-03 02:13:45 +00:00
|
|
|
import java.util.Enumeration;
|
|
|
|
|
import java.util.Hashtable;
|
2010-07-30 00:16:12 +00:00
|
|
|
import java.util.List;
|
2009-10-27 06:12:24 +00:00
|
|
|
|
2013-09-18 05:32:30 +00:00
|
|
|
import org.luaj.vm2.Globals;
|
2009-10-27 06:12:24 +00:00
|
|
|
import org.luaj.vm2.Lua;
|
2010-07-30 18:09:31 +00:00
|
|
|
import org.luaj.vm2.lib.jse.JsePlatform;
|
2010-04-13 14:31:40 +00:00
|
|
|
import org.luaj.vm2.luajc.LuaJC;
|
2009-10-27 06:12:24 +00:00
|
|
|
|
|
|
|
|
/**
|
2010-04-25 05:35:24 +00:00
|
|
|
* Compiler for lua files to compile lua sources or lua binaries into java classes.
|
2009-10-27 06:12:24 +00:00
|
|
|
*/
|
|
|
|
|
public class luajc {
|
2012-10-02 07:00:17 +00:00
|
|
|
private static final String version = Lua._VERSION + " Copyright (C) 2012 luaj.org";
|
2009-10-27 06:12:24 +00:00
|
|
|
|
|
|
|
|
private static final String usage =
|
2010-07-30 00:16:12 +00:00
|
|
|
"usage: java -cp luaj-jse.jar,bcel-5.2.jar luajc [options] fileordir [, fileordir ...]\n" +
|
2009-10-27 06:12:24 +00:00
|
|
|
"Available options are:\n" +
|
|
|
|
|
" - process stdin\n" +
|
2010-07-30 00:16:12 +00:00
|
|
|
" -s src source directory\n" +
|
|
|
|
|
" -d dir destination directory\n" +
|
|
|
|
|
" -p pkg package prefix to apply to all classes\n" +
|
2012-10-02 07:00:17 +00:00
|
|
|
" -m generate main(String[]) function for JSE\n" +
|
2010-07-30 00:16:12 +00:00
|
|
|
" -r recursively compile all\n" +
|
2010-07-30 16:44:42 +00:00
|
|
|
" -l load classes to verify generated bytecode\n" +
|
2013-09-18 05:32:30 +00:00
|
|
|
" -c enc use the supplied encoding 'enc' for input files\n" +
|
2010-07-30 00:16:12 +00:00
|
|
|
" -v verbose\n";
|
2009-10-27 06:12:24 +00:00
|
|
|
|
|
|
|
|
private static void usageExit() {
|
|
|
|
|
System.out.println(usage);
|
|
|
|
|
System.exit(-1);
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-05 04:26:25 +00:00
|
|
|
private String srcdir = ".";
|
|
|
|
|
private String destdir = ".";
|
2012-10-02 07:00:17 +00:00
|
|
|
private boolean genmain = false;
|
2010-07-30 00:16:12 +00:00
|
|
|
private boolean recurse = false;
|
2010-04-25 05:35:24 +00:00
|
|
|
private boolean verbose = false;
|
2010-07-30 16:44:42 +00:00
|
|
|
private boolean loadclasses = false;
|
2013-09-18 05:32:30 +00:00
|
|
|
private String encoding = null;
|
2010-07-30 00:16:12 +00:00
|
|
|
private String pkgprefix = null;
|
2010-07-30 23:47:52 +00:00
|
|
|
private List files = new ArrayList();
|
2013-09-18 05:32:30 +00:00
|
|
|
private Globals globals;
|
2009-10-27 06:12:24 +00:00
|
|
|
|
|
|
|
|
public static void main( String[] args ) throws IOException {
|
|
|
|
|
new luajc( args );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private luajc( String[] args ) throws IOException {
|
|
|
|
|
|
|
|
|
|
// process args
|
2010-07-30 23:47:52 +00:00
|
|
|
List seeds = new ArrayList ();
|
2010-07-30 00:16:12 +00:00
|
|
|
|
|
|
|
|
// get stateful args
|
|
|
|
|
for ( int i=0; i<args.length; i++ ) {
|
|
|
|
|
if ( ! args[i].startsWith("-") ) {
|
|
|
|
|
seeds.add(args[i]);
|
|
|
|
|
} else {
|
|
|
|
|
switch ( args[i].charAt(1) ) {
|
|
|
|
|
case 's':
|
|
|
|
|
if ( ++i >= args.length )
|
|
|
|
|
usageExit();
|
|
|
|
|
srcdir = args[i];
|
|
|
|
|
break;
|
|
|
|
|
case 'd':
|
|
|
|
|
if ( ++i >= args.length )
|
|
|
|
|
usageExit();
|
|
|
|
|
destdir = args[i];
|
|
|
|
|
break;
|
2010-07-30 16:44:42 +00:00
|
|
|
case 'l':
|
|
|
|
|
loadclasses = true;
|
2010-07-30 00:16:12 +00:00
|
|
|
break;
|
|
|
|
|
case 'p':
|
|
|
|
|
if ( ++i >= args.length )
|
|
|
|
|
usageExit();
|
|
|
|
|
pkgprefix = args[i];
|
|
|
|
|
break;
|
2012-10-02 07:00:17 +00:00
|
|
|
case 'm':
|
|
|
|
|
genmain = true;
|
|
|
|
|
break;
|
2010-07-30 00:16:12 +00:00
|
|
|
case 'r':
|
|
|
|
|
recurse = true;
|
|
|
|
|
break;
|
2013-09-18 05:32:30 +00:00
|
|
|
case 'c':
|
|
|
|
|
if ( ++i >= args.length )
|
|
|
|
|
usageExit();
|
|
|
|
|
encoding = args[i];
|
|
|
|
|
break;
|
2010-07-30 00:16:12 +00:00
|
|
|
case 'v':
|
|
|
|
|
verbose = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
usageExit();
|
|
|
|
|
break;
|
2009-10-27 06:12:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-07-30 00:16:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// echo version
|
|
|
|
|
if ( verbose ) {
|
|
|
|
|
System.out.println(version);
|
|
|
|
|
System.out.println("srcdir: "+srcdir);
|
2012-10-02 07:00:17 +00:00
|
|
|
System.out.println("destdir: "+destdir);
|
2010-07-30 00:16:12 +00:00
|
|
|
System.out.println("files: "+seeds);
|
|
|
|
|
System.out.println("recurse: "+recurse);
|
|
|
|
|
}
|
2009-10-27 06:12:24 +00:00
|
|
|
|
2010-07-30 00:16:12 +00:00
|
|
|
// need at least one seed
|
|
|
|
|
if ( seeds.size() <= 0 ) {
|
|
|
|
|
System.err.println(usage);
|
|
|
|
|
System.exit(-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// collect up files to process
|
|
|
|
|
for ( int i=0; i<seeds.size(); i++ )
|
|
|
|
|
collectFiles( srcdir+"/"+seeds.get(i) );
|
|
|
|
|
|
|
|
|
|
// check for at least one file
|
|
|
|
|
if ( files.size() <= 0 ) {
|
|
|
|
|
System.err.println("no files found in "+seeds);
|
|
|
|
|
System.exit(-1);
|
2009-10-27 06:12:24 +00:00
|
|
|
}
|
2010-07-30 00:16:12 +00:00
|
|
|
|
|
|
|
|
// process input files
|
2013-09-18 05:32:30 +00:00
|
|
|
globals = JsePlatform.standardGlobals();
|
2010-07-30 23:47:52 +00:00
|
|
|
for ( int i=0,n=files.size(); i<n; i++ )
|
|
|
|
|
processFile( (InputFile) files.get(i) );
|
2009-10-27 06:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
2010-07-30 00:16:12 +00:00
|
|
|
private void collectFiles(String path) {
|
|
|
|
|
File f = new File(path);
|
|
|
|
|
if ( f.isDirectory() && recurse )
|
|
|
|
|
scandir(f,pkgprefix);
|
|
|
|
|
else if ( f.isFile() ) {
|
|
|
|
|
File dir = f.getAbsoluteFile().getParentFile();
|
|
|
|
|
if ( dir != null )
|
|
|
|
|
scanfile( dir, f, pkgprefix );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void scandir(File dir, String javapackage) {
|
|
|
|
|
File[] f = dir.listFiles();
|
|
|
|
|
for ( int i=0; i<f.length; i++ )
|
|
|
|
|
scanfile( dir, f[i], javapackage );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void scanfile(File dir, File f, String javapackage) {
|
|
|
|
|
if ( f.exists() ) {
|
|
|
|
|
if ( f.isDirectory() && recurse )
|
|
|
|
|
scandir( f, (javapackage!=null? javapackage+"."+f.getName(): f.getName()) );
|
|
|
|
|
else if ( f.isFile() && f.getName().endsWith(".lua") )
|
|
|
|
|
files.add( new InputFile(dir,f,javapackage) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class InputFile {
|
|
|
|
|
public String luachunkname;
|
|
|
|
|
public String srcfilename;
|
|
|
|
|
public File infile;
|
|
|
|
|
public File outdir;
|
|
|
|
|
public String javapackage;
|
|
|
|
|
|
|
|
|
|
public InputFile(File dir, File f, String javapackage) {
|
|
|
|
|
this.infile = f;
|
|
|
|
|
String subdir = javapackage!=null? javapackage.replace('.', '/'): null;
|
|
|
|
|
String outdirpath = subdir!=null? destdir+"/"+subdir: destdir;
|
|
|
|
|
this.javapackage = javapackage;
|
|
|
|
|
this.srcfilename = (subdir!=null? subdir+"/": "")+infile.getName();
|
|
|
|
|
this.luachunkname = (subdir!=null? subdir+"/": "")+infile.getName().substring( 0, infile.getName().lastIndexOf('.') );
|
|
|
|
|
this.infile = f;
|
|
|
|
|
this.outdir = new File(outdirpath);
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-07-30 16:44:42 +00:00
|
|
|
|
2010-07-30 00:16:12 +00:00
|
|
|
private void processFile( InputFile inf ) {
|
|
|
|
|
inf.outdir.mkdirs();
|
2009-10-27 06:12:24 +00:00
|
|
|
try {
|
2010-07-30 00:16:12 +00:00
|
|
|
if ( verbose )
|
|
|
|
|
System.out.println("chunk="+inf.luachunkname+" srcfile="+inf.srcfilename);
|
2009-10-27 06:12:24 +00:00
|
|
|
|
2010-07-30 00:16:12 +00:00
|
|
|
// create the chunk
|
|
|
|
|
FileInputStream fis = new FileInputStream( inf.infile );
|
2013-09-18 05:32:30 +00:00
|
|
|
final Hashtable t = encoding != null?
|
|
|
|
|
LuaJC.instance.compileAll( new InputStreamReader(fis, encoding), inf.luachunkname, inf.srcfilename, globals, genmain):
|
|
|
|
|
LuaJC.instance.compileAll( fis, inf.luachunkname, inf.srcfilename, globals, genmain);
|
2010-07-30 00:16:12 +00:00
|
|
|
fis.close();
|
|
|
|
|
|
2009-10-27 06:12:24 +00:00
|
|
|
// write out the chunk
|
2010-04-25 05:35:24 +00:00
|
|
|
for ( Enumeration e = t.keys(); e.hasMoreElements(); ) {
|
|
|
|
|
String key = (String) e.nextElement();
|
|
|
|
|
byte[] bytes = (byte[]) t.get(key);
|
2010-07-30 00:16:12 +00:00
|
|
|
if ( key.indexOf('/')>=0 ) {
|
|
|
|
|
String d = (destdir!=null? destdir+"/": "")+key.substring(0,key.lastIndexOf('/'));
|
|
|
|
|
new File(d).mkdirs();
|
|
|
|
|
}
|
2010-04-25 05:35:24 +00:00
|
|
|
String destpath = (destdir!=null? destdir+"/": "") + key + ".class";
|
|
|
|
|
if ( verbose )
|
2010-07-30 00:16:12 +00:00
|
|
|
System.out.println( " "+destpath +" ("+bytes.length+" bytes)");
|
2010-04-25 05:35:24 +00:00
|
|
|
FileOutputStream fos = new FileOutputStream( destpath );
|
|
|
|
|
fos.write( bytes );
|
|
|
|
|
fos.close();
|
2009-10-27 06:12:24 +00:00
|
|
|
}
|
2010-07-30 16:44:42 +00:00
|
|
|
|
|
|
|
|
// try to load the files
|
|
|
|
|
if ( loadclasses ) {
|
|
|
|
|
ClassLoader loader = new ClassLoader() {
|
|
|
|
|
public Class findClass(String classname) throws ClassNotFoundException {
|
2010-08-13 00:23:53 +00:00
|
|
|
byte[] bytes = (byte[]) t.get(classname);
|
2010-07-30 16:44:42 +00:00
|
|
|
if ( bytes != null )
|
|
|
|
|
return defineClass(classname, bytes, 0, bytes.length);
|
|
|
|
|
return super.findClass(classname);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
for ( Enumeration e = t.keys(); e.hasMoreElements(); ) {
|
2010-08-13 00:23:53 +00:00
|
|
|
String classname = (String) e.nextElement();
|
2010-07-30 16:44:42 +00:00
|
|
|
try {
|
|
|
|
|
Class c = loader.loadClass(classname);
|
2010-08-05 21:38:49 +00:00
|
|
|
Object o = c.newInstance();
|
2010-07-30 16:44:42 +00:00
|
|
|
if ( verbose )
|
2010-08-05 21:38:49 +00:00
|
|
|
System.out.println(" loaded "+classname+" as "+o );
|
2012-01-25 03:58:33 +00:00
|
|
|
} catch ( Exception ex ) {
|
2010-07-30 16:44:42 +00:00
|
|
|
System.out.flush();
|
2012-01-25 03:58:33 +00:00
|
|
|
System.err.println(" failed to load "+classname+": "+ex );
|
2010-07-30 16:44:42 +00:00
|
|
|
System.err.flush();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-25 03:58:33 +00:00
|
|
|
} catch ( Exception e ) {
|
|
|
|
|
System.err.println(" failed to load "+inf.srcfilename+": "+e );
|
|
|
|
|
e.printStackTrace( System.err );
|
2010-08-11 18:21:20 +00:00
|
|
|
System.err.flush();
|
2009-10-27 06:12:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|