#!/usr/local/bin/perl -w require 5.003; use strict; use vars qw($javac @alljava $code %code $path $class $java); # Copyright (c) 1997 Gabor Egressy gabor@vmunix.com # All rights reserved. All wrongs reversed. This program is free # software; you can redistribute it and/or modify it under the same # terms as Perl itself. # May 23, 1997, minor updates June 18, July 16 # # The idea for this perl script is based on Samuel Oosterhuis' jmake, # which was written as a Bourne shell script. # If your Java code is intermixing C and C++ style comments then there # might be some problems with removing the comments before looking for # the dependencies # // /* # * a C style comment that has been turned off by the C++ style comment. # * Normally there is code here that was commented out and is now being # * used. # // */ # The above will cause problems. # jmake creates a Makefile which can be used for compiling Java class # files without having to do the compilation by hand. It simplifies and # automates the creation of the Makefile. # The way the script works is - it removes all backslashed quotes, then # it removes all quoted strings, then all C style comments, and then all # C++ style comments. It then searches for references (dependencies) to # other Java files in all Java files. # # Limits ? # May require a lot of memory if there are a lot of .java files and/or # they are large. Requires Perl5. The shebang line might need to be # changed to something else if Perl5 resides somewhere else on your # system, or has a name like 'perl5.003' # Bugs ? # Let me know if you find any. # # Feel free to redistribute this to all interested parties. # You can find a copy of this at # http://www.vmunix.com/~gabor/perl/jmake # CONSTANTS # should change this line to wherever javac resides $javac = '/usr/local/java/bin/javac'; # find all java files in this directory and save them in alljava; Java # files are recognized by the .java extension which must be in lowercase opendir DIR, "." or die "serious problem : $!"; @alljava = sort grep /\.java$/, readdir DIR; closedir DIR; if($#alljava < 0) { print "No JAVA files in directory\nExiting\n"; exit 0; } undef $/; # so we can read an entire file into a single scalar # read in the entire file (each one) into a scalar, remove comments so # they do not interfere with the dependency search, remove the .java # extension from the class file names, put each file into a hash foreach (@alljava) { open FILE, "<$_" or die "serious problem : $!"; $code = ; close FILE; $code =~ s/\\"//mg; # remove escaped double quotes $code =~ s/"[^"]*"//mg; # remove quoted strings $code =~ s(/\*.*?\*/)()sg; # remove C style comments $code =~ s(//.*$)()mg; # remove C++ style comments $_ =~ s/\.java$//; # remove the .java extension $code{$_} = $code; # put code into hash to save it } # save the old makefile just in case it was useful, of course this does # replace makefile.old with the current makefile so save it under a different # name if you wish to keep makefile.old if(-e "makefile") { rename "makefile", "makefile.old"; } # set up the makefile open FILE, ">makefile" or die "serious problem : $!"; print FILE "# Makefile created by jmake at ", `date +"%H:%M on %A %B %d, %Y"`; print FILE "# jmake was written by Gabor Egressy\n"; print FILE "# gabor\@vmunix.com\n"; print FILE "# Idea based on Samuel Oosterhuis' jmake Bourne shell script\n\n"; print FILE "JC = $javac -O"; if($#ARGV == 0) { print FILE " -d $ARGV[0]"; $path = $ARGV[0]; if(!($path =~ m/\/$/)) { $path =~ s/$/\//; } } else { $path = ""; } print FILE "\n\nall :"; foreach (@alljava) { print FILE " \\\n $path$_.class"; # add class file names, one per line } print FILE "\n\n\n"; # look for all the dependencies in all .java files, of course there is at # least a dependency on itself which must always be added foreach $class (@alljava) { print FILE "$path$class.class : $class.java"; # dependency on itself JAVA : foreach $java (@alljava) { # skip file if we are already processing it next JAVA if $java eq $class; # search for names of other classes referred to in this file if($code{$class} =~ m!\b$java\b(\s+\w+\s*(,|;|\)|\[)|\s*\[|\.\w+|\s*\()!mx) { print FILE " \\\n $java.java"; # add dependency } } print FILE "\n\t\$(JC) $class.java\n\n"; # add compilation line } # people seem to like this in their makefile print FILE "\nclean :\n\trm -f $path*.class\n"; # this is nice if you have your umask set to something like 66 print FILE "\nchmod :\n\tchmod 644 $path*.class\n"; close FILE; exit;