1 | #! /usr/bin/env python |
---|
2 | # encoding: utf-8 |
---|
3 | # WARNING! Do not edit! http://waf.googlecode.com/git/docs/wafbook/single.html#_obtaining_the_waf_file |
---|
4 | |
---|
5 | import os,re,tempfile,shutil |
---|
6 | from waflib import TaskGen,Task,Utils,Options,Build,Errors,Node,Logs |
---|
7 | from waflib.Configure import conf |
---|
8 | from waflib.TaskGen import feature,before_method,after_method |
---|
9 | from waflib.Tools import ccroot |
---|
10 | ccroot.USELIB_VARS['javac']=set(['CLASSPATH','JAVACFLAGS']) |
---|
11 | SOURCE_RE='**/*.java' |
---|
12 | JAR_RE='**/*' |
---|
13 | class_check_source=''' |
---|
14 | public class Test { |
---|
15 | public static void main(String[] argv) { |
---|
16 | Class lib; |
---|
17 | if (argv.length < 1) { |
---|
18 | System.err.println("Missing argument"); |
---|
19 | System.exit(77); |
---|
20 | } |
---|
21 | try { |
---|
22 | lib = Class.forName(argv[0]); |
---|
23 | } catch (ClassNotFoundException e) { |
---|
24 | System.err.println("ClassNotFoundException"); |
---|
25 | System.exit(1); |
---|
26 | } |
---|
27 | lib = null; |
---|
28 | System.exit(0); |
---|
29 | } |
---|
30 | } |
---|
31 | ''' |
---|
32 | @feature('javac') |
---|
33 | @before_method('process_source') |
---|
34 | def apply_java(self): |
---|
35 | Utils.def_attrs(self,jarname='',classpath='',sourcepath='.',srcdir='.',jar_mf_attributes={},jar_mf_classpath=[]) |
---|
36 | nodes_lst=[] |
---|
37 | outdir=getattr(self,'outdir',None) |
---|
38 | if outdir: |
---|
39 | if not isinstance(outdir,Node.Node): |
---|
40 | outdir=self.path.get_bld().make_node(self.outdir) |
---|
41 | else: |
---|
42 | outdir=self.path.get_bld() |
---|
43 | outdir.mkdir() |
---|
44 | self.outdir=outdir |
---|
45 | self.env['OUTDIR']=outdir.abspath() |
---|
46 | self.javac_task=tsk=self.create_task('javac') |
---|
47 | tmp=[] |
---|
48 | srcdir=getattr(self,'srcdir','') |
---|
49 | if isinstance(srcdir,Node.Node): |
---|
50 | srcdir=[srcdir] |
---|
51 | for x in Utils.to_list(srcdir): |
---|
52 | if isinstance(x,Node.Node): |
---|
53 | y=x |
---|
54 | else: |
---|
55 | y=self.path.find_dir(x) |
---|
56 | if not y: |
---|
57 | self.bld.fatal('Could not find the folder %s from %s'%(x,self.path)) |
---|
58 | tmp.append(y) |
---|
59 | tsk.srcdir=tmp |
---|
60 | if getattr(self,'compat',None): |
---|
61 | tsk.env.append_value('JAVACFLAGS',['-source',self.compat]) |
---|
62 | if hasattr(self,'sourcepath'): |
---|
63 | fold=[isinstance(x,Node.Node)and x or self.path.find_dir(x)for x in self.to_list(self.sourcepath)] |
---|
64 | names=os.pathsep.join([x.srcpath()for x in fold]) |
---|
65 | else: |
---|
66 | names=[x.srcpath()for x in tsk.srcdir] |
---|
67 | if names: |
---|
68 | tsk.env.append_value('JAVACFLAGS',['-sourcepath',names]) |
---|
69 | @feature('javac') |
---|
70 | @after_method('apply_java') |
---|
71 | def use_javac_files(self): |
---|
72 | lst=[] |
---|
73 | self.uselib=self.to_list(getattr(self,'uselib',[])) |
---|
74 | names=self.to_list(getattr(self,'use',[])) |
---|
75 | get=self.bld.get_tgen_by_name |
---|
76 | for x in names: |
---|
77 | try: |
---|
78 | y=get(x) |
---|
79 | except Exception: |
---|
80 | self.uselib.append(x) |
---|
81 | else: |
---|
82 | y.post() |
---|
83 | lst.append(y.jar_task.outputs[0].abspath()) |
---|
84 | self.javac_task.set_run_after(y.jar_task) |
---|
85 | if lst: |
---|
86 | self.env.append_value('CLASSPATH',lst) |
---|
87 | @feature('javac') |
---|
88 | @after_method('apply_java','propagate_uselib_vars','use_javac_files') |
---|
89 | def set_classpath(self): |
---|
90 | self.env.append_value('CLASSPATH',getattr(self,'classpath',[])) |
---|
91 | for x in self.tasks: |
---|
92 | x.env.CLASSPATH=os.pathsep.join(self.env.CLASSPATH)+os.pathsep |
---|
93 | @feature('jar') |
---|
94 | @after_method('apply_java','use_javac_files') |
---|
95 | @before_method('process_source') |
---|
96 | def jar_files(self): |
---|
97 | destfile=getattr(self,'destfile','test.jar') |
---|
98 | jaropts=getattr(self,'jaropts',[]) |
---|
99 | manifest=getattr(self,'manifest',None) |
---|
100 | basedir=getattr(self,'basedir',None) |
---|
101 | if basedir: |
---|
102 | if not isinstance(self.basedir,Node.Node): |
---|
103 | basedir=self.path.get_bld().make_node(basedir) |
---|
104 | else: |
---|
105 | basedir=self.path.get_bld() |
---|
106 | if not basedir: |
---|
107 | self.bld.fatal('Could not find the basedir %r for %r'%(self.basedir,self)) |
---|
108 | self.jar_task=tsk=self.create_task('jar_create') |
---|
109 | if manifest: |
---|
110 | jarcreate=getattr(self,'jarcreate','cfm') |
---|
111 | node=self.path.find_node(manifest) |
---|
112 | tsk.dep_nodes.append(node) |
---|
113 | jaropts.insert(0,node.abspath()) |
---|
114 | else: |
---|
115 | jarcreate=getattr(self,'jarcreate','cf') |
---|
116 | if not isinstance(destfile,Node.Node): |
---|
117 | destfile=self.path.find_or_declare(destfile) |
---|
118 | if not destfile: |
---|
119 | self.bld.fatal('invalid destfile %r for %r'%(destfile,self)) |
---|
120 | tsk.set_outputs(destfile) |
---|
121 | tsk.basedir=basedir |
---|
122 | jaropts.append('-C') |
---|
123 | jaropts.append(basedir.bldpath()) |
---|
124 | jaropts.append('.') |
---|
125 | tsk.env['JAROPTS']=jaropts |
---|
126 | tsk.env['JARCREATE']=jarcreate |
---|
127 | if getattr(self,'javac_task',None): |
---|
128 | tsk.set_run_after(self.javac_task) |
---|
129 | @feature('jar') |
---|
130 | @after_method('jar_files') |
---|
131 | def use_jar_files(self): |
---|
132 | lst=[] |
---|
133 | self.uselib=self.to_list(getattr(self,'uselib',[])) |
---|
134 | names=self.to_list(getattr(self,'use',[])) |
---|
135 | get=self.bld.get_tgen_by_name |
---|
136 | for x in names: |
---|
137 | try: |
---|
138 | y=get(x) |
---|
139 | except Exception: |
---|
140 | self.uselib.append(x) |
---|
141 | else: |
---|
142 | y.post() |
---|
143 | self.jar_task.run_after.update(y.tasks) |
---|
144 | class jar_create(Task.Task): |
---|
145 | color='GREEN' |
---|
146 | run_str='${JAR} ${JARCREATE} ${TGT} ${JAROPTS}' |
---|
147 | def runnable_status(self): |
---|
148 | for t in self.run_after: |
---|
149 | if not t.hasrun: |
---|
150 | return Task.ASK_LATER |
---|
151 | if not self.inputs: |
---|
152 | global JAR_RE |
---|
153 | try: |
---|
154 | self.inputs=[x for x in self.basedir.ant_glob(JAR_RE,remove=False)if id(x)!=id(self.outputs[0])] |
---|
155 | except Exception: |
---|
156 | raise Errors.WafError('Could not find the basedir %r for %r'%(self.basedir,self)) |
---|
157 | return super(jar_create,self).runnable_status() |
---|
158 | class javac(Task.Task): |
---|
159 | color='BLUE' |
---|
160 | nocache=True |
---|
161 | vars=['CLASSPATH','JAVACFLAGS','JAVAC','OUTDIR'] |
---|
162 | def runnable_status(self): |
---|
163 | for t in self.run_after: |
---|
164 | if not t.hasrun: |
---|
165 | return Task.ASK_LATER |
---|
166 | if not self.inputs: |
---|
167 | global SOURCE_RE |
---|
168 | self.inputs=[] |
---|
169 | for x in self.srcdir: |
---|
170 | self.inputs.extend(x.ant_glob(SOURCE_RE,remove=False)) |
---|
171 | return super(javac,self).runnable_status() |
---|
172 | def run(self): |
---|
173 | env=self.env |
---|
174 | gen=self.generator |
---|
175 | bld=gen.bld |
---|
176 | wd=bld.bldnode.abspath() |
---|
177 | def to_list(xx): |
---|
178 | if isinstance(xx,str):return[xx] |
---|
179 | return xx |
---|
180 | cmd=[] |
---|
181 | cmd.extend(to_list(env['JAVAC'])) |
---|
182 | cmd.extend(['-classpath']) |
---|
183 | cmd.extend(to_list(env['CLASSPATH'])) |
---|
184 | cmd.extend(['-d']) |
---|
185 | cmd.extend(to_list(env['OUTDIR'])) |
---|
186 | cmd.extend(to_list(env['JAVACFLAGS'])) |
---|
187 | files=[a.path_from(bld.bldnode)for a in self.inputs] |
---|
188 | tmp=None |
---|
189 | try: |
---|
190 | if len(str(files))+len(str(cmd))>8192: |
---|
191 | (fd,tmp)=tempfile.mkstemp(dir=bld.bldnode.abspath()) |
---|
192 | try: |
---|
193 | os.write(fd,'\n'.join(files)) |
---|
194 | finally: |
---|
195 | if tmp: |
---|
196 | os.close(fd) |
---|
197 | if Logs.verbose: |
---|
198 | Logs.debug('runner: %r'%(cmd+files)) |
---|
199 | cmd.append('@'+tmp) |
---|
200 | else: |
---|
201 | cmd+=files |
---|
202 | ret=self.exec_command(cmd,cwd=wd,env=env.env or None) |
---|
203 | finally: |
---|
204 | if tmp: |
---|
205 | os.unlink(tmp) |
---|
206 | return ret |
---|
207 | def post_run(self): |
---|
208 | for n in self.generator.outdir.ant_glob('**/*.class'): |
---|
209 | n.sig=Utils.h_file(n.abspath()) |
---|
210 | self.generator.bld.task_sigs[self.uid()]=self.cache_sig |
---|
211 | @feature('javadoc') |
---|
212 | @after_method('process_rule') |
---|
213 | def create_javadoc(self): |
---|
214 | tsk=self.create_task('javadoc') |
---|
215 | tsk.classpath=getattr(self,'classpath',[]) |
---|
216 | self.javadoc_package=Utils.to_list(self.javadoc_package) |
---|
217 | if not isinstance(self.javadoc_output,Node.Node): |
---|
218 | self.javadoc_output=self.bld.path.find_or_declare(self.javadoc_output) |
---|
219 | class javadoc(Task.Task): |
---|
220 | color='BLUE' |
---|
221 | def __str__(self): |
---|
222 | return'%s: %s -> %s\n'%(self.__class__.__name__,self.generator.srcdir,self.generator.javadoc_output) |
---|
223 | def run(self): |
---|
224 | env=self.env |
---|
225 | bld=self.generator.bld |
---|
226 | wd=bld.bldnode.abspath() |
---|
227 | srcpath=self.generator.path.abspath()+os.sep+self.generator.srcdir |
---|
228 | srcpath+=os.pathsep |
---|
229 | srcpath+=self.generator.path.get_bld().abspath()+os.sep+self.generator.srcdir |
---|
230 | classpath=env.CLASSPATH |
---|
231 | classpath+=os.pathsep |
---|
232 | classpath+=os.pathsep.join(self.classpath) |
---|
233 | classpath="".join(classpath) |
---|
234 | self.last_cmd=lst=[] |
---|
235 | lst.extend(Utils.to_list(env['JAVADOC'])) |
---|
236 | lst.extend(['-d',self.generator.javadoc_output.abspath()]) |
---|
237 | lst.extend(['-sourcepath',srcpath]) |
---|
238 | lst.extend(['-classpath',classpath]) |
---|
239 | lst.extend(['-subpackages']) |
---|
240 | lst.extend(self.generator.javadoc_package) |
---|
241 | lst=[x for x in lst if x] |
---|
242 | self.generator.bld.cmd_and_log(lst,cwd=wd,env=env.env or None,quiet=0) |
---|
243 | def post_run(self): |
---|
244 | nodes=self.generator.javadoc_output.ant_glob('**') |
---|
245 | for x in nodes: |
---|
246 | x.sig=Utils.h_file(x.abspath()) |
---|
247 | self.generator.bld.task_sigs[self.uid()]=self.cache_sig |
---|
248 | def configure(self): |
---|
249 | java_path=self.environ['PATH'].split(os.pathsep) |
---|
250 | v=self.env |
---|
251 | if'JAVA_HOME'in self.environ: |
---|
252 | java_path=[os.path.join(self.environ['JAVA_HOME'],'bin')]+java_path |
---|
253 | self.env['JAVA_HOME']=[self.environ['JAVA_HOME']] |
---|
254 | for x in'javac java jar javadoc'.split(): |
---|
255 | self.find_program(x,var=x.upper(),path_list=java_path) |
---|
256 | self.env[x.upper()]=self.cmd_to_list(self.env[x.upper()]) |
---|
257 | if'CLASSPATH'in self.environ: |
---|
258 | v['CLASSPATH']=self.environ['CLASSPATH'] |
---|
259 | if not v['JAR']:self.fatal('jar is required for making java packages') |
---|
260 | if not v['JAVAC']:self.fatal('javac is required for compiling java classes') |
---|
261 | v['JARCREATE']='cf' |
---|
262 | v['JAVACFLAGS']=[] |
---|
263 | @conf |
---|
264 | def check_java_class(self,classname,with_classpath=None): |
---|
265 | javatestdir='.waf-javatest' |
---|
266 | classpath=javatestdir |
---|
267 | if self.env['CLASSPATH']: |
---|
268 | classpath+=os.pathsep+self.env['CLASSPATH'] |
---|
269 | if isinstance(with_classpath,str): |
---|
270 | classpath+=os.pathsep+with_classpath |
---|
271 | shutil.rmtree(javatestdir,True) |
---|
272 | os.mkdir(javatestdir) |
---|
273 | java_file=open(os.path.join(javatestdir,'Test.java'),'w') |
---|
274 | java_file.write(class_check_source) |
---|
275 | java_file.close() |
---|
276 | self.exec_command(self.env['JAVAC']+[os.path.join(javatestdir,'Test.java')],shell=False) |
---|
277 | cmd=self.env['JAVA']+['-cp',classpath,'Test',classname] |
---|
278 | self.to_log("%s\n"%str(cmd)) |
---|
279 | found=self.exec_command(cmd,shell=False) |
---|
280 | self.msg('Checking for java class %s'%classname,not found) |
---|
281 | shutil.rmtree(javatestdir,True) |
---|
282 | return found |
---|
283 | @conf |
---|
284 | def check_jni_headers(conf): |
---|
285 | if not conf.env.CC_NAME and not conf.env.CXX_NAME: |
---|
286 | conf.fatal('load a compiler first (gcc, g++, ..)') |
---|
287 | if not conf.env.JAVA_HOME: |
---|
288 | conf.fatal('set JAVA_HOME in the system environment') |
---|
289 | javaHome=conf.env['JAVA_HOME'][0] |
---|
290 | dir=conf.root.find_dir(conf.env.JAVA_HOME[0]+'/include') |
---|
291 | if dir is None: |
---|
292 | dir=conf.root.find_dir(conf.env.JAVA_HOME[0]+'/../Headers') |
---|
293 | if dir is None: |
---|
294 | conf.fatal('JAVA_HOME does not seem to be set properly') |
---|
295 | f=dir.ant_glob('**/(jni|jni_md).h') |
---|
296 | incDirs=[x.parent.abspath()for x in f] |
---|
297 | dir=conf.root.find_dir(conf.env.JAVA_HOME[0]) |
---|
298 | f=dir.ant_glob('**/*jvm.(so|dll|dylib)') |
---|
299 | libDirs=[x.parent.abspath()for x in f]or[javaHome] |
---|
300 | f=dir.ant_glob('**/*jvm.(lib)') |
---|
301 | if f: |
---|
302 | libDirs=[[x,y.parent.abspath()]for x in libDirs for y in f] |
---|
303 | for d in libDirs: |
---|
304 | try: |
---|
305 | conf.check(header_name='jni.h',define_name='HAVE_JNI_H',lib='jvm',libpath=d,includes=incDirs,uselib_store='JAVA',uselib='JAVA') |
---|
306 | except Exception: |
---|
307 | pass |
---|
308 | else: |
---|
309 | break |
---|
310 | else: |
---|
311 | conf.fatal('could not find lib jvm in %r (see config.log)'%libDirs) |
---|