Skip to content

Commit 97d0cca

Browse files
committed
Update for windows
1 parent ca0d225 commit 97d0cca

File tree

13 files changed

+20249
-14460
lines changed

13 files changed

+20249
-14460
lines changed

build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def download():
128128
Path(archive).unlink()
129129

130130
def build(extra):
131-
args = [exe_file("bin/princess"), "-d", "-Isrc", "--buildfolder=build", "--outfile", exe_file("bin/princess2"), "src/main.pr"]
131+
args = [exe_file("bin/princess"), "-d", "--no-incremental", "-Isrc", "--buildfolder=build", "--outfile", exe_file("bin/princess2"), "src/main.pr"]
132132
if sys.platform == "win32":
133133
args += WIN_ARGS
134134
else:

include/cstd.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,36 @@
44
%EXCLUDE __builtin_fabsf __builtin_inff __builtin_fabs __builtin_inf __builtin_fabsl __builtin_infl longjmperror
55
%EXCLUDE __va_start _sopen_s_nolock
66
%EXCLUDE __builtin_bswap32 __builtin_bswap64
7+
8+
Windows specific
9+
%EXCLUDE vfwprintf _vfwprintf_s_l vfwprintf_s _vfwprintf_p_l _vfwprintf_p _vwprintf_l vwprintf _vwprintf_s_l vwprintf_s
10+
%EXCLUDE _vwprintf_p_l _vwprintf_p _fwprintf_l _fwprintf_s_l fwprintf_s _fwprintf_p_l _fwprintf_p _wprintf_l wprintf
11+
%EXCLUDE _wprintf_s_l wprintf_s _wprintf_p_l _wprintf_p _vfwscanf_l vfwscanf _vfwscanf_s_l vfwscanf_s _vwscanf_l vwscanf
12+
%EXCLUDE _vwscanf_s_l vwscanf_s _fwscanf_l fwscanf _fwscanf_s_l fwscanf_s _wscanf_l wscanf _wscanf_s_l wscanf_s _vsnwprintf_l
13+
%EXCLUDE _vsnwprintf_s_l _vsnwprintf_s _snwprintf _vsnwprintf _vswprintf_c _vswprintf_l __vswprintf_l _vswprintf _vswprintf
14+
%EXCLUDE vswprintf vswprintf _vswprintf_s_l vswprintf_s _vswprintf_p_l _vswprintf_p _vscwprintf_l _vscwprintf _vscwprintf_p_l
15+
%EXCLUDE _vscwprintf_p _swprintf __swprintf_l _swprintf_l _swprintf _swprintf _swprintf_s_l swprintf_s _swprintf_p_l _swprintf_p
16+
%EXCLUDE _swprintf_c_l _swprintf_c _snwprintf_l _snwprintf_s_l _snwprintf_s _scwprintf_l _scwprintf _scwprintf_p_l
17+
%EXCLUDE _scwprintf_p _vswscanf_l vswscanf _vswscanf_s_l vswscanf_s _vsnwscanf_l _vsnwscanf_s_l _swscanf_l swscanf
18+
%EXCLUDE _swscanf_s_l swscanf_s _snwscanf_l _snwscanf _snwscanf_s_l _snwscanf_s _vfprintf_s_l vfprintf_s _vfprintf_p_l
19+
%EXCLUDE _vfprintf_p _vprintf_l _vprintf_s_l vprintf_s _vprintf_p_l _vprintf_p _fprintf_l _fprintf_s_l fprintf_s
20+
%EXCLUDE _fprintf_p_l _fprintf_p _printf_l _printf_s_l printf_s _printf_p_l _printf_p _vfscanf_s_l vfscanf_s _vscanf_l
21+
%EXCLUDE _vscanf_s_l vscanf_s _fscanf_l _fscanf_s_l fscanf_s _scanf_l _scanf_s_l scanf_s vsprintf_s _vsprintf_p_l _vsprintf_p
22+
%EXCLUDE _vsnprintf_s_l _vsnprintf_s _vsnprintf_s vsnprintf_s vsnprintf_s _vscprintf_l _vscprintf _vscprintf_p_l _vscprintf_p
23+
%EXCLUDE _vsnprintf_c_l _vsnprintf_c _sprintf_l _sprintf_s_l _sprintf_p_l _sprintf_p _snprintf_l _snprintf_c_l _snprintf_c
24+
%EXCLUDE _snprintf_s_l _snprintf_s _scprintf_l _scprintf _scprintf_p_l _scprintf_p _vsscanf_s_l vsscanf_s _sscanf_l
25+
%EXCLUDE _sscanf_s_l sscanf_s _snscanf_l _snscanf _snscanf_s_l _snscanf_s wcsnlen_s _wcstok strnlen_s _wctime_s timespec_get
26+
%EXCLUDE ctime_s gmtime_s localtime_s _chgsignl _copysignl _hypotl __ascii_iswdigit _vcwprintf_l _vcwprintf _vcwprintf_s_l
27+
%EXCLUDE _vcwprintf_s _vcwprintf_p_l _vcwprintf_p _cwprintf_l _cwprintf _cwprintf_s_l _cwprintf_s _cwprintf_p_l _cwprintf_p
28+
%EXCLUDE _vcwscanf_l _vcwscanf _vcwscanf_s_l _vcwscanf_s _cwscanf_l _cwscanf _cwscanf_s_l _cwscanf_s _vcprintf_l _vcprintf
29+
%EXCLUDE _vcprintf_s_l _vcprintf_s _vcprintf_p_l _vcprintf_p _cprintf_l _cprintf _cprintf _cprintf_s_l _cprintf_s _cprintf_p_l
30+
%EXCLUDE _cprintf_p _vcscanf_l _vcscanf _vcscanf_s_l _vcscanf_s _cscanf_l _cscanf _cscanf _cscanf_s_l _cscanf_s cprintf cprintf cscanf cscanf
31+
32+
Inline functions
33+
%EXCLUDE __acrt_locale_get_ctype_array_value __ascii_iswalpha __ascii_toupper time __ascii_towlower difftime sprintf_s
34+
%EXCLUDE _vsscanf_l localtime _chvalidchk_l mktime _mkgmtime memcpy_s _vfscanf_l _vsprintf_l __acrt_get_locale_data_prefix
35+
%EXCLUDE _vsprintf_s_l fwprintf __ascii_tolower _vfprintf_l __ascii_towupper __threadid _ischartype_l _wctime gmtime
36+
%EXCLUDE __threadhandle _vsnprintf_l ctime __local_stdio_printf_options _vfwprintf_l swprintf memmove_s __local_stdio_scanf_options _vswprintf_c_l
737
*/
838

939
#include <stdlib.h>

include/gencstd.py

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ def print_references(self, file: File):
8888
if self.displayname in file.has_declared or self.displayname in file.has_printed: return
8989
else: file.has_declared.add(self.displayname)
9090

91-
print(f"export type {self.displayname}", file = file.fp)
91+
if self.displayname and "()" not in self.displayname:
92+
print(f"export type {self.displayname}", file = file.fp)
9293

9394
class IncompleteTypedef(Type):
9495
def __init__(self, name: str):
@@ -192,14 +193,15 @@ def print_references(self, file: File):
192193
self = file.TAGGED[self.name]
193194

194195
for f in self.fields:
195-
f.type.print_references(file)
196+
if f.type:
197+
f.type.print_references(file)
196198

197199
if not self.fields: return
198200
if not self.displayname in file.has_printed:
199201
file.has_printed.add(self.displayname)
200202
else: return
201203

202-
if self.name:
204+
if self.displayname:
203205
print(f"export type {self.displayname}", file = file.fp, end = "")
204206
if self.fields:
205207
print(f" = {self.to_definition(file)}", file = file.fp)
@@ -215,7 +217,7 @@ def to_definition(self, file: File) -> str:
215217
if self.fields:
216218
res = "struct { "
217219
for field in self.fields:
218-
res += field.to_definition(file) + "; "
220+
if field.type: res += field.to_definition(file) + "; "
219221
res += "}"
220222
return res
221223

@@ -229,7 +231,7 @@ def to_definition(self, file: File) -> str:
229231
if self.fields:
230232
res = "struct #union { "
231233
for field in self.fields:
232-
res += field.to_definition(file) + "; "
234+
if field.type: res += field.to_definition(file) + "; "
233235
res += "}"
234236
return res
235237

@@ -265,7 +267,7 @@ def print_references(self, file: File):
265267
file.has_printed.add(self.displayname)
266268
else: return
267269

268-
if self.name and not is_anonymous(self.name):
270+
if self.displayname and not is_anonymous(self.name):
269271
print(f"export type {self.displayname}", file = file.fp, end = "")
270272
if self.fields:
271273
print(f" = {self.to_definition(file)}", file = file.fp)
@@ -418,10 +420,22 @@ def parse_struct(name: str, inner: clang.Type, file: File, lookup: bool = False)
418420

419421
fields = []
420422
field: clang.Cursor
423+
index = 0
421424
for field in children:
422425
if field.kind == clang.CursorKind.STRUCT_DECL:
423426
if len(list(field.get_children())) == 0: continue
424-
fields.append(Field(parse_type(field.type, file, is_in_struct = True), field.spelling, field.is_bitfield(), field.get_bitfield_width()))
427+
field_type = parse_type(field.type, file, is_in_struct = True)
428+
if not field_type: return None
429+
430+
exists = True
431+
spelling = field.spelling
432+
if not spelling:
433+
spelling = "_" + str(index)
434+
if type(field_type) == IncompleteType:
435+
field_type = None
436+
437+
fields.append(Field(field_type, spelling, field.is_bitfield(), field.get_bitfield_width()))
438+
index += 1
425439

426440
res = None
427441
if declaration.kind == clang.CursorKind.STRUCT_DECL:
@@ -486,9 +500,24 @@ def parse_type(type: clang.Type, file: File, lookup: bool = False, is_in_struct:
486500
elif type.kind == clang.TypeKind.INCOMPLETEARRAY:
487501
return Pointer(parse_type(type.get_array_element_type(), file, lookup, is_in_struct))
488502
elif type.kind == clang.TypeKind.TYPEDEF:
489-
return file.TYPEDEFS[type.spelling]
503+
spelling = type.spelling
504+
# TODO This doesn't work properly
505+
if spelling.startswith("const"):
506+
spelling = spelling.replace("const", "", 1)
507+
spelling = spelling.strip()
508+
if spelling.startswith("__unaligned"):
509+
spelling = spelling.replace("__unaligned", "", 1)
510+
spelling = spelling.strip()
511+
if spelling.startswith("volatile"):
512+
spelling = spelling.replace("volatile", "", 1)
513+
spelling = spelling.strip()
514+
515+
spelling = spelling.strip()
516+
return file.TYPEDEFS[spelling]
517+
518+
return IncompleteType(type.spelling)
490519

491-
assert False, f"Invalid type! {type.kind}"
520+
ALL_DEFINITIONS = {}
492521

493522
def process_module(name: str, *libs):
494523
included = []
@@ -516,39 +545,45 @@ def process_module(name: str, *libs):
516545
def extract(node: clang.Cursor):
517546
if node.kind == clang.CursorKind.FUNCTION_DECL:
518547
if node.is_static_method(): return
519-
520-
for token in node.get_tokens():
521-
if token.spelling == "__inline" or token.spelling == "inline":
522-
return
523548

524-
dllimport = False #TODO
549+
dllimport = False
550+
for child in node.get_children():
551+
if child.kind == clang.CursorKind.DLLIMPORT_ATTR:
552+
dllimport = True
553+
525554
name = node.spelling
526555
args = []
527556

528557
for child in node.get_arguments():
529558
if child.kind == clang.CursorKind.PARM_DECL:
530559
tokens = list(child.get_tokens())
531-
if len(tokens) == 2 and tokens[0].spelling == "size_t": #FIXME This is a hack, see https://github.com/sighingnow/libclang/issues/53
532-
args.append((child.spelling, PRIMITIVES[clang.TypeKind.ULONG]))
560+
spelling = escape_name(child.spelling)
561+
if sys.platform == "linux" and len(tokens) == 2 and tokens[0].spelling == "size_t": #FIXME This is a hack, see https://github.com/sighingnow/libclang/issues/53
562+
args.append((spelling, PRIMITIVES[clang.TypeKind.ULONG]))
533563
else:
534-
args.append((child.spelling, parse_type(child.type, file)))
564+
args.append((spelling, parse_type(child.type, file)))
535565

536566
is_size_t = False #FIXME Same hack
537567
for token in node.get_tokens():
538568
if token.kind == clang.TokenKind.KEYWORD and token.spelling == "extern": continue
539569
if token.spelling == "size_t": is_size_t = True
540570
if token.spelling == "(": break
541571

542-
if is_size_t:
572+
if is_size_t and sys.platform == "linux":
543573
ret = PRIMITIVES[clang.TypeKind.ULONG]
544574
else:
545575
ret = parse_type(node.result_type, file)
546576

547577
file.GLOBALS[name] = FunctionDecl(name, ret, args, node.type.is_function_variadic(), dllimport)
548578
elif node.kind == clang.CursorKind.VAR_DECL:
549579
if node.storage_class == clang.StorageClass.EXTERN:
580+
dllimport = False
581+
for child in node.get_children():
582+
if child.kind == clang.CursorKind.DLLIMPORT_ATTR:
583+
dllimport = True
584+
550585
type = parse_type(node.type, file)
551-
file.GLOBALS[node.spelling] = VarDecl(node.spelling, type, False)
586+
file.GLOBALS[node.spelling] = VarDecl(node.spelling, type, dllimport)
552587
elif node.kind == clang.CursorKind.TYPEDEF_DECL:
553588
name = node.spelling
554589
underlying = node.underlying_typedef_type
@@ -580,18 +615,19 @@ def extract(node: clang.Cursor):
580615
elif node.kind == clang.CursorKind.MACRO_DEFINITION:
581616
tokens = list(node.get_tokens())
582617
if len(tokens) == 2 and tokens[1].kind == clang.TokenKind.LITERAL:
583-
token = tokens[1]
584-
if token.spelling.startswith('"') and token.spelling.endswith('"'):
585-
s = token.spelling
586-
s = re.sub(r"(?<!\\)\\(\d{1,3})", lambda o: f"\\x{ + int(o.group(1), base = 8):02x}", s)
587-
file.GLOBALS[node.spelling] = ConstDecl(node.spelling, string, s)
588-
elif token.spelling.isdigit():
589-
file.GLOBALS[node.spelling] = ConstDecl(node.spelling, PRIMITIVES[clang.TypeKind.INT], token.spelling)
590-
else:
591-
try:
592-
float(token.spelling)
593-
file.GLOBALS[node.spelling] = ConstDecl(node.spelling, PRIMITIVES[clang.TypeKind.DOUBLE], token.spelling)
594-
except ValueError: pass
618+
if node.spelling != "true" and node.spelling != "false":
619+
token = tokens[1]
620+
if token.spelling.startswith('"') and token.spelling.endswith('"'):
621+
s = token.spelling
622+
s = re.sub(r"(?<!\\)\\(\d{1,3})", lambda o: f"\\x{ + int(o.group(1), base = 8):02x}", s)
623+
file.GLOBALS[node.spelling] = ConstDecl(node.spelling, string, s)
624+
elif token.spelling.isdigit():
625+
file.GLOBALS[node.spelling] = ConstDecl(node.spelling, PRIMITIVES[clang.TypeKind.INT], token.spelling)
626+
else:
627+
try:
628+
float(token.spelling)
629+
file.GLOBALS[node.spelling] = ConstDecl(node.spelling, PRIMITIVES[clang.TypeKind.DOUBLE], token.spelling)
630+
except ValueError: pass
595631

596632
index = clang.Index.create()
597633
tu = index.parse(folder / f"{name}.h", options =
@@ -603,7 +639,8 @@ def extract(node: clang.Cursor):
603639
extract(node)
604640

605641

606-
file.GLOBALS = {k:v for k,v in file.GLOBALS.items() if k not in excluded}
642+
file.GLOBALS = {k:v for k,v in file.GLOBALS.items() if k not in excluded and k not in ALL_DEFINITIONS }
643+
ALL_DEFINITIONS.update(file.GLOBALS)
607644

608645
for type in file.TYPEDEFS.values():
609646
type.print_references(file)
@@ -625,6 +662,7 @@ def extract(node: clang.Cursor):
625662
print(f"export var __SYMBOLS: [{num_decls}; symbol::Symbol]", file = fp2)
626663

627664
def main():
665+
clang.Config.set_library_file(r"C:\Users\Vic\Programming\llvm-project\build\Release\bin\libclang.dll")
628666
if sys.platform != "win32":
629667
process_module("linux")
630668
process_module("bfd")

include/windows.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
/*
22
%EXCLUDE WinMain wWinMain
3+
4+
Inline functions
5+
%EXCLUDE GetProcessDefaultCpuSetMasks SymGetSourceFileTokenByTokenName SymGetSourceFileFromTokenByTokenName
6+
%EXCLUDE GetNumaNodeProcessorMask2 SetThreadSelectedCpuSetMasks RegisterForTooltipDismissNotification QueueUserAPC2
7+
%EXCLUDE EnableProcessOptionalXStateFeatures GetThreadSelectedCpuSetMasks GetMachineTypeAttributes SymGetSourceFileTokenByTokenNameW
8+
%EXCLUDE SetProcessDefaultCpuSetMasks SetAdditionalForegroundBoostProcesses AreShortNamesEnabled GetTempPath2A GetTempPath2W
9+
%EXCLUDE GetThreadEnabledXStateFeatures SymGetSourceFileFromTokenByTokenNameW StackWalk2
310
*/
411
#ifdef _WIN32
512
#include <Windows.h>

0 commit comments

Comments
 (0)