summaryrefslogtreecommitdiffstats
path: root/third_party/waf/waflib/extras/haxe.py
blob: 4ff3745794d6d3e34ad22549e7df3ede855b0d6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import re

from waflib import Utils, Task, Errors, Logs
from waflib.Configure import conf
from waflib.TaskGen import extension, taskgen_method

HAXE_COMPILERS = {
    'JS': {'tgt': '--js', 'ext_out': ['.js']},
    'LUA': {'tgt': '--lua', 'ext_out': ['.lua']},
    'SWF': {'tgt': '--swf', 'ext_out': ['.swf']},
    'NEKO': {'tgt': '--neko', 'ext_out': ['.n']},
    'PHP': {'tgt': '--php', 'ext_out': ['.php']},
    'CPP': {'tgt': '--cpp', 'ext_out': ['.h', '.cpp']},
    'CPPIA': {'tgt': '--cppia', 'ext_out': ['.cppia']},
    'CS': {'tgt': '--cs', 'ext_out': ['.cs']},
    'JAVA': {'tgt': '--java', 'ext_out': ['.java']},
    'JVM': {'tgt': '--jvm', 'ext_out': ['.jar']},
    'PYTHON': {'tgt': '--python', 'ext_out': ['.py']},
    'HL': {'tgt': '--hl', 'ext_out': ['.hl']},
    'HLC': {'tgt': '--hl', 'ext_out': ['.h', '.c']},
}

@conf
def check_haxe_pkg(self, **kw):
    self.find_program('haxelib')
    libs = kw.get('libs')
    if not libs or not (type(libs) == str or (type(libs) == list and all(isinstance(s, str) for s in libs))):
        self.fatal('Specify correct libs value in ensure call')
        return
    fetch = kw.get('fetch')
    if not fetch is None and not type(fetch) == bool:
        self.fatal('Specify correct fetch value in ensure call')

    libs = [libs] if type(libs) == str else libs
    halt = False
    for lib in libs:
        try:
            self.start_msg('Checking for library %s' % lib)
            output = self.cmd_and_log(self.env.HAXELIB + ['list', lib])
        except Errors.WafError:
            self.end_msg(False)
            self.fatal('Can\'t run haxelib list, ensuring halted')
            return

        if lib in output:
            self.end_msg(lib in output)
        else:
            if not fetch:
                self.end_msg(False)
                halt = True
                continue
            try:
                status = self.exec_command(self.env.HAXELIB + ['install', lib])
                if status:
                    self.end_msg(False)
                    self.fatal('Can\'t get %s with haxelib, ensuring halted' % lib)
                    return
                else:
                    self.end_msg('downloaded', color='YELLOW')
            except Errors.WafError:
                self.end_msg(False)
                self.fatal('Can\'t run haxelib install, ensuring halted')
                return
        postfix = kw.get('uselib_store') or lib.upper()
        self.env.append_unique('LIB_' + postfix, lib)

    if halt:
        self.fatal('Can\'t find libraries in haxelib list, ensuring halted')
        return

class haxe(Task.Task):
    vars = ['HAXE_VERSION', 'HAXE_FLAGS']
    ext_in = ['.hx']

    def run(self):
        cmd = self.env.HAXE + self.env.HAXE_FLAGS_DEFAULT + self.env.HAXE_FLAGS
        return self.exec_command(cmd)

for COMP in HAXE_COMPILERS:
    # create runners for each compile target
    type("haxe_" + COMP, (haxe,), {'ext_out': HAXE_COMPILERS[COMP]['ext_out']})

@taskgen_method
def init_haxe(self):
    errmsg = '%s not found, specify correct value'
    try:
        compiler = HAXE_COMPILERS[self.compiler]
        comp_tgt = compiler['tgt']
        comp_mod = '/main.c' if self.compiler == 'HLC' else ''
    except (AttributeError, KeyError):
        self.bld.fatal(errmsg % 'COMPILER' + ': ' + ', '.join(HAXE_COMPILERS.keys()))
        return

    self.env.append_value(
        'HAXE_FLAGS',
        [comp_tgt, self.path.get_bld().make_node(self.target + comp_mod).abspath()])
    if hasattr(self, 'use'):
        if not (type(self.use) == str or type(self.use) == list):
            self.bld.fatal(errmsg % 'USE')
            return
        self.use = [self.use] if type(self.use) == str else self.use

        for dep in self.use:
            if self.env['LIB_' + dep]:
                for lib in self.env['LIB_' + dep]:
                    self.env.append_value('HAXE_FLAGS', ['-lib', lib])

    if hasattr(self, 'res'):
        if not type(self.res) == str:
            self.bld.fatal(errmsg % 'RES')
            return
        self.env.append_value('HAXE_FLAGS', ['-D', 'resourcesPath=%s' % self.res])

@extension('.hx')
def haxe_hook(self, node):
    if len(self.source) > 1:
        self.bld.fatal('Use separate task generators for multiple files')
        return

    src = node
    tgt = self.path.get_bld().find_or_declare(self.target)

    self.init_haxe()
    self.create_task('haxe_' + self.compiler, src, tgt)

@conf
def check_haxe(self, mini=None, maxi=None):
    self.start_msg('Checking for haxe version')
    try:
        curr = re.search(
            r'(\d+.?)+',
            self.cmd_and_log(self.env.HAXE + ['-version'])).group()
    except Errors.WafError:
        self.end_msg(False)
        self.fatal('Can\'t get haxe version')
        return

    if mini and Utils.num2ver(curr) < Utils.num2ver(mini):
        self.end_msg('wrong', color='RED')
        self.fatal('%s is too old, need >= %s' % (curr, mini))
        return
    if maxi and Utils.num2ver(curr) > Utils.num2ver(maxi):
        self.end_msg('wrong', color='RED')
        self.fatal('%s is too new, need <= %s' % (curr, maxi))
        return
    self.end_msg(curr, color='GREEN')
    self.env.HAXE_VERSION = curr

def configure(self):
    self.env.append_value(
        'HAXE_FLAGS_DEFAULT',
        ['-D', 'no-compilation', '-cp', self.path.abspath()])
    Logs.warn('Default flags: %s' % ' '.join(self.env.HAXE_FLAGS_DEFAULT))
    self.find_program('haxe')