|
| 1 | + |
| 2 | +import re |
| 3 | +import sys |
| 4 | +from collections import OrderedDict |
| 5 | + |
| 6 | +defines = OrderedDict() |
| 7 | +cdefs = [] |
| 8 | + |
| 9 | +location_re = re.compile(r'^# \d+ "([^"]*)"') |
| 10 | +glpath_re = re.compile(r'^(?:.*[\\/]|\A)(gl|glu|glfw3|glext)\.h$') |
| 11 | +define_re = re.compile(r"^#define\s+([^\s]+)\s+([^\s]+)$") |
| 12 | + |
| 13 | +number_re = re.compile(r"^-?[0-9]+$") |
| 14 | +hex_re = re.compile(r"0x[0-9a-fA-F]+$") |
| 15 | + |
| 16 | +# Set of known #defines that we don't need to include |
| 17 | +INVALID_DEFINES = set(["GLAPI", "APIENTRY", "GLU_TESS_MAX_COORD", "gluErrorStringWIN", "WINGDIAPI", "CALLBACK"]) |
| 18 | + |
| 19 | +if __name__ == "__main__": |
| 20 | + in_gl = False |
| 21 | + for line in sys.stdin: |
| 22 | + # Ignore blank lines |
| 23 | + line = line.strip() |
| 24 | + if not line: |
| 25 | + continue |
| 26 | + |
| 27 | + # Is this a preprocessor statement? |
| 28 | + if line.startswith("#"): |
| 29 | + |
| 30 | + # Is this a location pragma? |
| 31 | + location_match = location_re.match(line) |
| 32 | + if location_match: |
| 33 | + # If we are transitioning to a header we need to parse, set the flag |
| 34 | + glpath_match = glpath_re.match(location_match.group(1)) |
| 35 | + in_gl = bool(glpath_match) |
| 36 | + continue |
| 37 | + |
| 38 | + if in_gl: |
| 39 | + # Is it a define? |
| 40 | + define_match = define_re.match(line) |
| 41 | + if define_match: |
| 42 | + name, val = define_match.groups() |
| 43 | + |
| 44 | + if val in defines: |
| 45 | + # Is this an alias of something we have already defined? |
| 46 | + val = defines[val] |
| 47 | + elif number_re.match(val) or hex_re.match(val): |
| 48 | + # Is this a number? |
| 49 | + # Store the define |
| 50 | + defines[name] = val |
| 51 | + elif val == "0xFFFFFFFFFFFFFFFFull": |
| 52 | + # Fix for GL_TIMEOUT_IGNORED |
| 53 | + defines[name] = val |
| 54 | + elif val == "0xFFFFFFFFu": |
| 55 | + # Fix for GL_INVALID_INDEX |
| 56 | + defines[name] = "0xFFFFFFFF" |
| 57 | + elif name not in INVALID_DEFINES: |
| 58 | + # Incompatible define |
| 59 | + print("Invalid define:", name, file=sys.stderr) |
| 60 | + |
| 61 | + continue |
| 62 | + |
| 63 | + # Otherwise just include it in the cdef |
| 64 | + elif in_gl: |
| 65 | + # Windows likes to add __stdcall__ to everything, but it isn't needed and is actually harmful when using under linux. |
| 66 | + cdefs.append(line.replace(r'__attribute__((__stdcall__)) ', '')) |
| 67 | + |
| 68 | + # Output the file |
| 69 | + #print("--[[ AUTOGENERATED FILE, DO NOT MODIFY ]]") |
| 70 | + print("local glc; do require('ffi').cdef [[") |
| 71 | + for line in cdefs: |
| 72 | + print(line) |
| 73 | + print("]]; glc = {") |
| 74 | + |
| 75 | + for k in defines.keys(): |
| 76 | + print("%s = %s," % ("['"+k+"']", defines[k])) |
| 77 | + |
| 78 | + print("} end") |
0 commit comments