Package wafadmin :: Package Tools :: Module java
[hide private]
[frames] | no frames]

Source Code for Module wafadmin.Tools.java

  1  #!/usr/bin/env python 
  2  # encoding: utf-8 
  3  # Thomas Nagy, 2006-2008 (ita) 
  4   
  5  """ 
  6  Java support 
  7   
  8  Javac is one of the few compilers that behaves very badly: 
  9  * it outputs files where it wants to (-d is only for the package root) 
 10  * it recompiles files silently behind your back 
 11  * it outputs an undefined amount of files (inner classes) 
 12   
 13  Fortunately, the convention makes it possible to use th build dir without 
 14  too many problems for the moment 
 15   
 16  Inner classes must be located and cleaned when a problem arise, 
 17  for the moment waf does not track the production of inner classes. 
 18   
 19  Adding all the files to a task and executing it if any of the input files 
 20  change is only annoying for the compilation times 
 21  """ 
 22   
 23  import os, re 
 24  from Configure import conf 
 25  import TaskGen, Task, Utils 
26 27 -class java_taskgen(TaskGen.task_gen):
28 - def __init__(self, *k):
29 TaskGen.task_gen.__init__(self, *k) 30 31 self.jarname = '' 32 self.jaropts = '' 33 self.classpath = '' 34 self.source_root = '.' 35 36 # Jar manifest attributes 37 # TODO: Add manifest creation 38 self.jar_mf_attributes = {} 39 self.jar_mf_classpath = []
40
41 - def apply(self):
42 nodes_lst = [] 43 44 if not self.classpath: 45 if not self.env['CLASSPATH']: 46 self.env['CLASSPATH'] = '..' + os.pathsep + '.' 47 else: 48 self.env['CLASSPATH'] = self.classpath 49 50 re_foo = re.compile(self.source) 51 52 source_root_node = self.path.find_dir(self.source_root) 53 54 src_nodes = [] 55 bld_nodes = [] 56 57 prefix_path = source_root_node.abspath() 58 for (root, dirs, filenames) in os.walk(source_root_node.abspath()): 59 for x in filenames: 60 file = root + '/' + x 61 file = file.replace(prefix_path, '') 62 if file.startswith('/'): 63 file = file[1:] 64 65 if re_foo.search(file) > -1: 66 node = source_root_node.find_resource(file) 67 src_nodes.append(node) 68 69 node2 = node.change_ext(".class") 70 bld_nodes.append(node2) 71 72 self.env['OUTDIR'] = source_root_node.abspath(self.env) 73 74 tsk = self.create_task('javac') 75 tsk.set_inputs(src_nodes) 76 tsk.set_outputs(bld_nodes) 77 78 if self.jarname: 79 tsk = self.create_task('jar_create') 80 tsk.set_inputs(bld_nodes) 81 tsk.set_outputs(self.path.find_or_declare(self.jarname)) 82 83 if not self.env['JAROPTS']: 84 if self.jaropts: 85 self.env['JAROPTS'] = self.jaropts 86 else: 87 dirs = '/' 88 self.env['JAROPTS'] = '-C %s %s' % (self.env['OUTDIR'], dirs)
89 90 Task.simple_task_type('javac', '${JAVAC} -classpath ${CLASSPATH} -d ${OUTDIR} ${SRC}', color='BLUE', before="jar_create") 91 Task.simple_task_type('jar_create', '${JAR} ${JARCREATE} ${TGT} ${JAROPTS}', color='GREEN')
92 93 -def detect(conf):
94 # If JAVA_PATH is set, we prepend it to the path list 95 java_path = os.environ['PATH'].split(os.pathsep) 96 v = conf.env 97 98 if os.environ.has_key('JAVA_HOME'): 99 java_path = [os.path.join(os.environ['JAVA_HOME'], 'bin')] + java_path 100 conf.env['JAVA_HOME'] = os.environ['JAVA_HOME'] 101 102 conf.find_program('javac', var='JAVAC', path_list=java_path) 103 conf.find_program('java', var='JAVA', path_list=java_path) 104 conf.find_program('jar', var='JAR', path_list=java_path) 105 v['JAVA_EXT'] = ['.java'] 106 107 if os.environ.has_key('CLASSPATH'): 108 v['CLASSPATH'] = os.environ['CLASSPATH'] 109 110 if not v['JAR']: conf.fatal('jar is required for making java packages') 111 if not v['JAVAC']: conf.fatal('javac is required for compiling java classes') 112 v['JARCREATE'] = 'cf' # can use cvf
113
114 @conf 115 -def check_java_class(conf, classname, with_classpath=None):
116 """ 117 Check if specified java class is installed. 118 """ 119 120 class_check_source = """ 121 public class Test { 122 public static void main(String[] argv) { 123 Class lib; 124 if (argv.length < 1) { 125 System.err.println("Missing argument"); 126 System.exit(77); 127 } 128 try { 129 lib = Class.forName(argv[0]); 130 } catch (ClassNotFoundException e) { 131 System.err.println("ClassNotFoundException"); 132 System.exit(1); 133 } 134 lib = null; 135 System.exit(0); 136 } 137 } 138 """ 139 import shutil 140 141 javatestdir = '.waf-javatest' 142 143 classpath = javatestdir 144 if conf.env['CLASSPATH']: 145 classpath += os.pathsep + conf.env['CLASSPATH'] 146 if isinstance(with_classpath, str): 147 classpath += os.pathsep + with_classpath 148 149 shutil.rmtree(javatestdir, True) 150 os.mkdir(javatestdir) 151 152 java_file = open(os.path.join(javatestdir, 'Test.java'), 'w') 153 java_file.write(class_check_source) 154 java_file.close() 155 156 # Compile the source 157 os.popen(conf.env['JAVAC'] + ' ' + os.path.join(javatestdir, 'Test.java')) 158 159 (jstdin, jstdout, jstderr) = os.popen3(conf.env['JAVA'] + ' -cp ' + classpath + ' Test ' + classname) 160 161 found = not bool(jstderr.read()) 162 conf.check_message('Java class %s' % classname, "", found) 163 164 shutil.rmtree(javatestdir, True) 165 166 return found
167