You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libshit/ext/wscript_lua

160 lines
5.2 KiB
Python

# -*- python -*-
from waflib import Context
def options(ctx):
ctx.with_opt('lua', ['lua', 'system', 'none'],
default=getattr(Context.g_module, 'DEFAULT_LUA', 'none'))
grp = ctx.get_option_group('configure options')
grp.add_option('--lua-pc-name', action='store', default='lua5.3',
help='Lua pkg-config name (in case of --with-lua=system). '+
'Should be lua 5.3 or compatible with c++ exceptions')
grp.add_option('--lua-dll', action='store_true', default=None,
help='Compile lua as dll (Windows only)')
grp.add_option('--luac-mode', action='store',
help='Luac mode: copy, system-luajit, luac, system-luac, luac-wrapper')
def chk_luac(ctx, modes):
if not ctx.env.LUAC_MODE in modes:
ctx.fatal('Invalid --luac-mode, valid modes: %s' % modes)
if ctx.env.LUAC_MODE in ['system-luajit', 'system-luac', 'luac-wrapper']:
if ctx.env.LUAC == []:
ctx.fatal('Must specify LUAC env var')
def configure(ctx):
from waflib import Logs
ctx.add_os_flags('LUACFLAGS')
ctx.add_os_flags('LUAC')
def no_lua(ctx, _):
ctx.env.append_value('DEFINES_LUA', 'LIBSHIT_WITH_LUA=0')
ctx.env.WITH_LUA = 'none'
ctx.only_host_env(no_lua)
ctx.env.LUAC_MODE = ctx.options.luac_mode
def check_none(ctx):
chk_luac(ctx, [[]])
def check_system(ctx):
ctx.check_cfg(package=ctx.options.lua_pc_name, args='--cflags --libs',
uselib_store='LUA')
if not ctx.env.LUAC_MODE: ctx.env.LUAC_MODE = 'copy'
chk_luac(ctx, ['copy', 'system-luajit', 'system-luac'])
def check_common(ctx):
if ctx.env.DEST_OS == 'win32':
ctx.env.LUA_DLL = ctx.options.lua_dll
if ctx.env['COMPILER_CXX'] != 'msvc':
ctx.env.append_value('CFLAGS_LUA', '-fexceptions')
if ctx.env.LUA_DLL:
ctx.env.append_value('DEFINES_LUA', 'LUA_BUILD_AS_DLL')
if not ctx.options.release:
ctx.env.append_value('DEFINES_LUA', 'LUA_USE_APICHECK')
def check_lua(ctx):
check_common(ctx)
ctx.recurse('lua', name='configure', once=False)
chk_luac(ctx, ['copy', 'luac', 'system-luac', 'luac-wrapper'])
ctx.with_chk(
'lua', {'none': check_none, 'system': check_system,
'lua': check_lua}, define='LIBSHIT_WITH_LUA')
if ctx.env.DEBUG and ctx.env.LUAC_MODE in ['system-luajit']:
ctx.env.append_value('LUACFLAGS', '-g')
elif not ctx.env.DEBUG and \
ctx.env.LUAC_MODE in ['luac', 'system-luac', 'luac-wrapper']:
ctx.env.append_value('LUACFLAGS', '-s')
def build(ctx):
if ctx.env.WITH_LUA == 'lua':
ctx.recurse('lua', name='build', once=False)
from waflib import Task, Utils
from hashlib import sha256
class bin2c(Task.Task):
color = 'BLUE'
map = {
39: "'",
92: "\\",
7: "a",
8: "b",
9: "t",
10: "n",
11: "v",
12: "f",
13: "r",
}
def __init__(self, *k, **kw):
Task.Task.__init__(self, *k, **kw)
if True:
self.name_prefix = 'luaJIT_BC_'
self.quote_name = lambda x: x
else:
self.name_prefix = 'BIN2C'
self.quote_name = Utils.quote_define_name
def run(self):
inp = self.inputs[0]
data = inp.read('rb')
hash = sha256(data).hexdigest()
name = self.name_prefix + self.quote_name(inp.change_ext('').name)
out = """
#ifndef %s_H_INCLUDED_%s
#define %s_H_INCLUDED_%s
#pragma once
static constexpr const size_t %s_SIZE = %d;
static const char %s[] = {
""" % (name, hash, name, hash, name, len(data), name)
i = 0
for c in data:
if i > 16:
out += "\n"
i = 0
i += 1
if c in self.map:
out += "'\\%s'," % self.map[c]
elif c >= 32 and c < 127:
out += "'%s'," % chr(c)
else:
out += "'\%o'," % int(c)
out += "\n};\n\n#endif\n"
self.outputs[0].write(out)
from waflib.TaskGen import extension
@extension('.lua')
def lua_to_h(self, node):
if not self.env.WITH_LUA:
self.bld.fatal('Trying to compile .lua in build with no lua')
out = node.change_ext('.lua.h')
if self.env.LUAC_MODE == 'copy':
self.create_task('bin2c', node, out)
elif self.env.LUAC_MODE == 'system-luajit':
self.create_task('luajit', node, out)
elif self.env.LUAC_MODE == 'system-luac':
self.luac_task(node, out, self.env.LUAC[0])
elif self.env.LUAC_MODE == 'luac':
luac = self.bld.get_tgen_by_name('luac').link_task.outputs[0]
tsk = self.luac_task(node, out, luac.abspath())
tsk.dep_nodes += [luac]
elif self.env.LUAC_MODE == 'luac-wrapper':
luac = self.bld.get_tgen_by_name('luac').link_task.outputs[0]
tsk = self.luac_task(node, out, self.env.LUAC + [luac.abspath()])
tsk.dep_nodes += [luac]
else:
self.bld.fatal('Unknown LUAC_MODE %s' % self.env.LUAC_MODE)
# task .lua->.h
class luajit(Task.Task):
run_str = '${LUAC} -bt h ${LUACFLAGS} ${SRC} ${TGT}'
color = 'BLUE'