haxe.py (3806B)
1 import os, re 2 from waflib import Utils, Task, Errors 3 from waflib.TaskGen import extension, taskgen_method, feature 4 from waflib.Configure import conf 5 6 @conf 7 def libname_haxe(self, libname): 8 return libname 9 10 @conf 11 def check_lib_haxe(self, libname, uselib_store=None): 12 haxe_libs = [node.name for node in self.root.find_node('haxe_libraries').ant_glob()] 13 changed = False 14 self.start_msg('Checking for library %s' % libname) 15 if libname + '.hxml' in haxe_libs: 16 self.end_msg('yes') 17 else: 18 changed = True 19 try: 20 cmd = self.env.LIX + ['+lib', libname] 21 res = self.cmd_and_log(cmd) 22 if (res): 23 raise Errors.WafError(res) 24 else: 25 self.end_msg('downloaded', color = 'YELLOW') 26 except Errors.WafError as e: 27 self.end_msg('no', color = 'RED') 28 self.fatal('Getting %s has failed' % libname) 29 30 postfix = uselib_store if uselib_store else libname.upper() 31 self.env['LIB_' + postfix] += [self.libname_haxe(libname)] 32 return changed 33 34 @conf 35 def check_libs_haxe(self, libnames, uselib_store=None): 36 changed = False 37 for libname in Utils.to_list(libnames): 38 if self.check_lib_haxe(libname, uselib_store): 39 changed = True 40 return changed 41 42 @conf 43 def ensure_lix_pkg(self, *k, **kw): 44 if kw.get('compiler') == 'hx': 45 if isinstance(kw.get('libs'), list) and len(kw.get('libs')): 46 changed = self.check_libs_haxe(kw.get('libs'), kw.get('uselib_store')) 47 if changed: 48 try: 49 cmd = self.env.LIX + ['download'] 50 res = self.cmd_and_log(cmd) 51 if (res): 52 raise Errors.WafError(res) 53 except Errors.WafError as e: 54 self.fatal('lix download has failed') 55 else: 56 self.check_lib_haxe(kw.get('lib'), kw.get('uselib_store')) 57 58 @conf 59 def haxe(bld, *k, **kw): 60 task_gen = bld(*k, **kw) 61 62 class haxe(Task.Task): 63 vars = ['HAXE', 'HAXE_VERSION', 'HAXEFLAGS'] 64 ext_out = ['.hl', '.c', '.h'] 65 66 def run(self): 67 cmd = self.env.HAXE + self.env.HAXEFLAGS 68 return self.exec_command(cmd, stdout = open(os.devnull, 'w')) 69 70 @taskgen_method 71 def init_haxe_task(self, node): 72 def addflags(flags): 73 self.env.append_value('HAXEFLAGS', flags) 74 75 if node.suffix() == '.hxml': 76 addflags(self.path.abspath() + '/' + node.name) 77 else: 78 addflags(['-main', node.name]) 79 addflags(['-hl', self.path.get_bld().make_node(self.target).abspath()]) 80 addflags(['-cp', self.path.abspath()]) 81 addflags(['-D', 'resourcesPath=%s' % getattr(self, 'res', '')]) 82 if hasattr(self, 'use'): 83 for dep in self.use: 84 if self.env['LIB_' + dep]: 85 for lib in self.env['LIB_' + dep]: addflags(['-lib', lib]) 86 87 @extension('.hx', '.hxml') 88 def haxe_file(self, node): 89 if len(self.source) > 1: 90 self.bld.fatal('Use separate task generators for multiple files') 91 92 try: 93 haxetask = self.haxetask 94 except AttributeError: 95 haxetask = self.haxetask = self.create_task('haxe') 96 self.init_haxe_task(node) 97 98 haxetask.inputs.append(node) 99 haxetask.outputs.append(self.path.get_bld().make_node(self.target)) 100 101 @conf 102 def find_haxe(self, min_version): 103 npx = self.env.NPX = self.find_program('npx') 104 self.env.LIX = npx + ['lix'] 105 npx_haxe = self.env.HAXE = npx + ['haxe'] 106 try: 107 output = self.cmd_and_log(npx_haxe + ['-version']) 108 except Errors.WafError: 109 haxe_version = None 110 else: 111 ver = re.search(r'\d+.\d+.\d+', output).group().split('.') 112 haxe_version = tuple([int(x) for x in ver]) 113 114 self.msg('Checking for haxe version', 115 haxe_version, haxe_version and haxe_version >= min_version) 116 if npx_haxe and haxe_version < min_version: 117 self.fatal('haxe version %r is too old, need >= %r' % (haxe_version, min_version)) 118 119 self.env.HAXE_VERSION = haxe_version 120 return npx_haxe 121 122 @conf 123 def check_haxe(self, min_version=(4,1,4)): 124 if self.env.HAXE_MINVER: 125 min_version = self.env.HAXE_MINVER 126 find_haxe(self, min_version) 127 128 def configure(self): 129 self.env.HAXEFLAGS = [] 130 self.check_haxe() 131 self.add_os_flags('HAXEFLAGS', dup = False)