Implement dual compilation with custom defines

This commit is contained in:
Kovid Goyal 2016-11-12 11:41:40 +05:30
parent c8a71ef5f8
commit 939ffe191f
3 changed files with 39 additions and 1 deletions

View File

@ -13,10 +13,14 @@ extern int init_ColorProfile(PyObject *);
extern int init_SpriteMap(PyObject *);
extern int init_ChangeTracker(PyObject *);
extern PyObject* create_256_color_table();
extern PyObject* parse_bytes_dump(PyObject UNUSED *self, PyObject *val);
extern PyObject* parse_bytes(PyObject UNUSED *self, PyObject *val);
#include "gl.h"
static PyMethodDef module_methods[] = {
GL_METHODS
{"parse_bytes", (PyCFunction)parse_bytes, METH_VARARGS, ""},
{"parse_bytes_dump", (PyCFunction)parse_bytes_dump, METH_VARARGS, ""},
{NULL, NULL, 0, NULL} /* Sentinel */
};

20
kitty/parser.c Normal file
View File

@ -0,0 +1,20 @@
/*
* parser.c
* Copyright (C) 2016 Kovid Goyal <kovid at kovidgoyal.net>
*
* Distributed under terms of the GPL3 license.
*/
#include "data-types.h"
PyObject*
#ifdef DUMP_COMMANDS
parse_bytes_dump(PyObject UNUSED *self, PyObject *args) {
#else
parse_bytes(PyObject UNUSED *self, PyObject *args) {
#endif
Py_buffer pybuf;
if (!PyArg_ParseTuple(args, "y*", &pybuf)) return NULL;
Py_RETURN_NONE;
}

View File

@ -71,6 +71,10 @@ def init_env(debug=False):
pass
def define(x):
return '-D' + x
def run_tool(cmd):
if isinstance(cmd, str):
cmd = shlex.split(cmd[0])
@ -80,13 +84,22 @@ def run_tool(cmd):
if ret != 0:
raise SystemExit(ret)
SPECIAL_SOURCES = {
'kitty/parser_dump.c': ('kitty/parser.c', ['DUMP_COMMANDS']),
}
def compile_c_extension(module, *sources):
prefix = os.path.basename(module)
objects = [os.path.join(build_dir, prefix + '-' + os.path.basename(src) + '.o') for src in sources]
for src, dest in zip(sources, objects):
cflgs = cflags[:]
if src in SPECIAL_SOURCES:
src, defines = SPECIAL_SOURCES[src]
cflgs.extend(map(define, defines))
src = os.path.join(base, src)
run_tool([cc] + cflags + ['-c', src] + ['-o', dest])
run_tool([cc] + cflgs + ['-c', src] + ['-o', dest])
run_tool([cc] + ldflags + objects + ldpaths + ['-o', os.path.join(base, module + '.so')])
@ -103,6 +116,7 @@ def find_c_files():
for x in os.listdir(d):
if x.endswith('.c'):
yield os.path.join('kitty', x)
yield 'kitty/parser_dump.c'
def main():