Skip to content

Commit fe0b69e

Browse files
committed
update libtcod-cffi to use pycparser
1 parent 43e6ae7 commit fe0b69e

103 files changed

Lines changed: 520 additions & 1611 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ target/
5858
# Custom
5959
tcod-*/
6060
Release/
61+
/tcod/

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ prune dependencies/zlib-*/examples
1717
prune dependencies/zlib-*/test
1818
prune dependencies/zlib-*/contrib
1919

20+
recursive-include dependencies/fake_libc_include *.h
21+
2022
recursive-include dependencies/SDL-* *.h *.dll *.lib
2123
include dependencies/SDL-*/*.txt
2224
prune dependencies/SDL-*/src

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ build: off
5858
test_script:
5959
- cmd: "%PYTHON% -m nose2"
6060
deploy_script:
61-
- "%PYTHON% -m twine upload -u %pypi_user% -p %pypi_pass% --skip-existing dist/*"
61+
- "if defined APPVEYOR_REPO_TAG_NAME %PYTHON% -m twine upload -u %pypi_user% -p %pypi_pass% --skip-existing dist/*"

build_libtcod.py

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55

66
import platform
7-
7+
from pycparser import c_parser, c_ast, parse_file, c_generator
88
from cffi import FFI
99

1010
BITSIZE, LINKAGE = platform.architecture()
@@ -83,9 +83,55 @@ def find_sources(directory):
8383
else:
8484
library_dirs += [os.path.realpath('dependencies/SDL-1.2.15/lib/x64')]
8585

86+
def get_cdef():
87+
generator = c_generator.CGenerator()
88+
return generator.visit(get_ast())
89+
90+
def get_ast():
91+
ast = parse_file(filename='src/libtcod_cdef.h', use_cpp=True,
92+
cpp_args=[r'-Idependencies/fake_libc_include',
93+
r'-Idependencies/libtcod-1.5.1/include',
94+
r'-DDECLSPEC=',
95+
r'-DSDLCALL=',
96+
r'-DTCODLIB_API=',
97+
r'-D__inline__=',
98+
r'-U__GNUC__',
99+
])
100+
for node in list(ast.ext):
101+
# resolve binary ops in TCOD_event_t enum
102+
if not isinstance(node, c_ast.Typedef):
103+
continue
104+
if node.name == 'wchar_t':
105+
ast.ext.remove(node) # remove wchar_t placeholder
106+
if node.name != 'TCOD_event_t':
107+
continue
108+
109+
# get to enumerator list node
110+
(type, node), = node.children()
111+
(type, node), = node.children()
112+
(type, node), = node.children()
113+
114+
consts = {}
115+
for type, enum in node.children():
116+
consts[enum.name] = value = resolve_ast(enum.value, consts)
117+
enum.value = c_ast.Constant('int', str(value))
118+
return ast
119+
120+
def resolve_ast(ast, consts):
121+
if isinstance(ast, c_ast.Constant):
122+
return int(ast.value)
123+
elif isinstance(ast, c_ast.ID):
124+
return consts[ast.name]
125+
elif isinstance(ast, c_ast.BinaryOp):
126+
return resolve_ast(ast.left, consts) | resolve_ast(ast.right, consts)
127+
else:
128+
raise RuntimeError('Unexpected ast node: %r' % ast)
129+
130+
86131
ffi = FFI()
87-
with open('src/libtcod_cdef.h', 'r') as file_cdef:
88-
ffi.cdef(file_cdef.read())
132+
ffi.cdef(get_cdef())
133+
#with open('src/libtcod_cdef.h', 'r') as file_cdef:
134+
# ffi.cdef(file_cdef.read())
89135
ffi.set_source(
90136
module_name, source,
91137
include_dirs=include_dirs,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include "_fake_defines.h"
2+
#include "_fake_typedefs.h"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#ifndef _FAKE_DEFINES_H
2+
#define _FAKE_DEFINES_H
3+
4+
#define NULL 0
5+
#define BUFSIZ 1024
6+
#define FOPEN_MAX 20
7+
#define FILENAME_MAX 1024
8+
9+
#ifndef SEEK_SET
10+
#define SEEK_SET 0 /* set file offset to offset */
11+
#endif
12+
#ifndef SEEK_CUR
13+
#define SEEK_CUR 1 /* set file offset to current plus offset */
14+
#endif
15+
#ifndef SEEK_END
16+
#define SEEK_END 2 /* set file offset to EOF plus offset */
17+
#endif
18+
19+
#define __LITTLE_ENDIAN 1234
20+
#define LITTLE_ENDIAN __LITTLE_ENDIAN
21+
#define __BIG_ENDIAN 4321
22+
#define BIG_ENDIAN __BIG_ENDIAN
23+
#define __BYTE_ORDER __LITTLE_ENDIAN
24+
#define BYTE_ORDER __BYTE_ORDER
25+
26+
#define EXIT_FAILURE 1
27+
#define EXIT_SUCCESS 0
28+
29+
#define UCHAR_MAX 255
30+
#define USHRT_MAX 65535
31+
#define UINT_MAX 4294967295U
32+
#define RAND_MAX 32767
33+
#define INT_MAX 32767
34+
35+
/* C99 stdbool.h defines */
36+
//#define __bool_true_false_are_defined 1
37+
//#define false 0
38+
//#define true 1
39+
40+
/* va_arg macros and type*/
41+
typedef int va_list;
42+
#define va_start(_ap, _type) __builtin_va_start((_ap))
43+
#define va_arg(_ap, _type) __builtin_va_arg((_ap))
44+
#define va_end(_list)
45+
46+
#endif
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#ifndef _FAKE_TYPEDEFS_H
2+
#define _FAKE_TYPEDEFS_H
3+
4+
typedef int size_t;
5+
typedef int __builtin_va_list;
6+
typedef int __gnuc_va_list;
7+
typedef int __int8_t;
8+
typedef int __uint8_t;
9+
typedef int __int16_t;
10+
typedef int __uint16_t;
11+
typedef int __int_least16_t;
12+
typedef int __uint_least16_t;
13+
typedef int __int32_t;
14+
typedef int __uint32_t;
15+
typedef int __int64_t;
16+
typedef int __uint64_t;
17+
typedef int __int_least32_t;
18+
typedef int __uint_least32_t;
19+
typedef int __s8;
20+
typedef int __u8;
21+
typedef int __s16;
22+
typedef int __u16;
23+
typedef int __s32;
24+
typedef int __u32;
25+
typedef int __s64;
26+
typedef int __u64;
27+
typedef int _LOCK_T;
28+
typedef int _LOCK_RECURSIVE_T;
29+
typedef int _off_t;
30+
typedef int __dev_t;
31+
typedef int __uid_t;
32+
typedef int __gid_t;
33+
typedef int _off64_t;
34+
typedef int _fpos_t;
35+
typedef int _ssize_t;
36+
typedef int wint_t;
37+
typedef int _mbstate_t;
38+
typedef int _flock_t;
39+
typedef int _iconv_t;
40+
typedef int __ULong;
41+
typedef int __FILE;
42+
typedef int ptrdiff_t;
43+
typedef int wchar_t;
44+
typedef int __off_t;
45+
typedef int __pid_t;
46+
typedef int __loff_t;
47+
typedef int u_char;
48+
typedef int u_short;
49+
typedef int u_int;
50+
typedef int u_long;
51+
typedef int ushort;
52+
typedef int uint;
53+
typedef int clock_t;
54+
typedef int time_t;
55+
typedef int daddr_t;
56+
typedef int caddr_t;
57+
typedef int ino_t;
58+
typedef int off_t;
59+
typedef int dev_t;
60+
typedef int uid_t;
61+
typedef int gid_t;
62+
typedef int pid_t;
63+
typedef int key_t;
64+
typedef int ssize_t;
65+
typedef int mode_t;
66+
typedef int nlink_t;
67+
typedef int fd_mask;
68+
typedef int _types_fd_set;
69+
typedef int clockid_t;
70+
typedef int timer_t;
71+
typedef int useconds_t;
72+
typedef int suseconds_t;
73+
typedef int FILE;
74+
typedef int fpos_t;
75+
typedef int cookie_read_function_t;
76+
typedef int cookie_write_function_t;
77+
typedef int cookie_seek_function_t;
78+
typedef int cookie_close_function_t;
79+
typedef int cookie_io_functions_t;
80+
typedef int div_t;
81+
typedef int ldiv_t;
82+
typedef int lldiv_t;
83+
typedef int sigset_t;
84+
typedef int __sigset_t;
85+
typedef int _sig_func_ptr;
86+
typedef int sig_atomic_t;
87+
typedef int __tzrule_type;
88+
typedef int __tzinfo_type;
89+
typedef int mbstate_t;
90+
typedef int sem_t;
91+
typedef int pthread_t;
92+
typedef int pthread_attr_t;
93+
typedef int pthread_mutex_t;
94+
typedef int pthread_mutexattr_t;
95+
typedef int pthread_cond_t;
96+
typedef int pthread_condattr_t;
97+
typedef int pthread_key_t;
98+
typedef int pthread_once_t;
99+
typedef int pthread_rwlock_t;
100+
typedef int pthread_rwlockattr_t;
101+
typedef int pthread_spinlock_t;
102+
typedef int pthread_barrier_t;
103+
typedef int pthread_barrierattr_t;
104+
typedef int jmp_buf;
105+
typedef int rlim_t;
106+
typedef int sa_family_t;
107+
typedef int sigjmp_buf;
108+
typedef int stack_t;
109+
typedef int siginfo_t;
110+
typedef int z_stream;
111+
112+
/* C99 exact-width integer types */
113+
typedef int int8_t;
114+
typedef int uint8_t;
115+
typedef int int16_t;
116+
typedef int uint16_t;
117+
typedef int int32_t;
118+
typedef int uint32_t;
119+
typedef int int64_t;
120+
typedef int uint64_t;
121+
122+
/* C99 minimum-width integer types */
123+
typedef int int_least8_t;
124+
typedef int uint_least8_t;
125+
typedef int int_least16_t;
126+
typedef int uint_least16_t;
127+
typedef int int_least32_t;
128+
typedef int uint_least32_t;
129+
typedef int int_least64_t;
130+
typedef int uint_least64_t;
131+
132+
/* C99 fastest minimum-width integer types */
133+
typedef int int_fast8_t;
134+
typedef int uint_fast8_t;
135+
typedef int int_fast16_t;
136+
typedef int uint_fast16_t;
137+
typedef int int_fast32_t;
138+
typedef int uint_fast32_t;
139+
typedef int int_fast64_t;
140+
typedef int uint_fast64_t;
141+
142+
/* C99 integer types capable of holding object pointers */
143+
typedef int intptr_t;
144+
typedef int uintptr_t;
145+
146+
/* C99 greatest-width integer types */
147+
typedef int intmax_t;
148+
typedef int uintmax_t;
149+
150+
/* C99 stdbool.h bool type. _Bool is built-in in C99 */
151+
//typedef _Bool bool;
152+
153+
typedef int va_list;
154+
155+
#endif
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include "_fake_defines.h"
2+
#include "_fake_typedefs.h"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include "_fake_defines.h"
2+
#include "_fake_typedefs.h"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include "_fake_defines.h"
2+
#include "_fake_typedefs.h"

0 commit comments

Comments
 (0)