Skip to content

Commit

Permalink
libgrass-interface: Upgrade to ctypesgen v1.1.1 (OSGeo#2598)
Browse files Browse the repository at this point in the history
Notable changes are
- Added support for sized integer types on Windows
- Added support to handle function specifier keywords: inline and _Noreturn
- Fixed mapping of 'short int' to c_short
- Improved parsing of pragma directives
  • Loading branch information
nilason authored Oct 28, 2022
1 parent 8c2239e commit 7ccc717
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 314 deletions.
5 changes: 3 additions & 2 deletions python/libgrass_interface_generator/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# librass interface generator
# libgrass interface generator

## Notes on ctypesgen

Currently installed version:
<https://github.com/ctypesgen/ctypesgen/commit/fd495e5edb2f69e5407774f8937a1d62cd33fe55> (17 February 2022)
[**v1.1.1**](https://github.com/ctypesgen/ctypesgen/releases/tag/1.1.1)
(19 October 2022)


### How to update ctypesgen version
Expand Down
24 changes: 19 additions & 5 deletions python/libgrass_interface_generator/ctypesgen/ctypedescs.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,41 @@
("double", True, 0): "c_double",
("double", True, 1): "c_longdouble",
("int8_t", True, 0): "c_int8",
("__int8_t", True, 0): "c_int8",
("__int8", True, 0): "c_int8",
("int16_t", True, 0): "c_int16",
("__int16_t", True, 0): "c_int16",
("__int16", True, 0): "c_int16",
("int32_t", True, 0): "c_int32",
("__int32_t", True, 0): "c_int32",
("__int32", True, 0): "c_int32",
("int64_t", True, 0): "c_int64",
("__int64", True, 0): "c_int64",
("uint8_t", True, 0): "c_uint8",
("uint16_t", True, 0): "c_uint16",
("uint32_t", True, 0): "c_uint32",
("uint64_t", True, 0): "c_uint64",
("__int64_t", True, 0): "c_int64",
("uint8_t", False, 0): "c_uint8",
("__uint8", False, 0): "c_uint8",
("__uint8_t", False, 0): "c_uint8",
("uint16_t", False, 0): "c_uint16",
("__uint16", False, 0): "c_uint16",
("__uint16_t", False, 0): "c_uint16",
("uint32_t", False, 0): "c_uint32",
("__uint32", False, 0): "c_uint32",
("__uint32_t", False, 0): "c_uint32",
("uint64_t", False, 0): "c_uint64",
("__uint64", False, 0): "c_uint64",
("__uint64_t", False, 0): "c_uint64",
("_Bool", True, 0): "c_bool",
}

ctypes_type_map_python_builtin = {
("int", True, -1): "c_short",
("int", False, -1): "c_ushort",
("int", True, 2): "c_longlong",
("int", False, 2): "c_ulonglong",
("size_t", True, 0): "c_size_t",
("apr_int64_t", True, 0): "c_int64",
("off64_t", True, 0): "c_int64",
("apr_uint64_t", True, 0): "c_uint64",
("apr_uint64_t", False, 0): "c_uint64",
("wchar_t", True, 0): "c_wchar",
("ptrdiff_t", True, 0): "c_ptrdiff_t", # Requires definition in preamble
("ssize_t", True, 0): "c_ptrdiff_t", # Requires definition in preamble
Expand Down
33 changes: 30 additions & 3 deletions python/libgrass_interface_generator/ctypesgen/parser/cgrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@


reserved_keyword_tokens = (
"SIZEOF", "TYPEDEF", "EXTERN", "STATIC", "AUTO", "REGISTER", # "INLINE",
"SIZEOF", "TYPEDEF", "EXTERN", "STATIC", "AUTO", "REGISTER", "INLINE",
"CONST", "RESTRICT", "VOLATILE",
"CHAR", "SHORT", "INT", "LONG", "SIGNED", "UNSIGNED", "FLOAT", "DOUBLE",
"VOID", "STRUCT", "UNION", "ENUM",
Expand All @@ -51,16 +51,18 @@
)

reserved_keyword_tokens_new = (
"_BOOL",
"_BOOL", "_NORETURN",
# "_ALIGNAS", "_ALIGNOF", "_ATOMIC", "_COMPLEX",
# "_DECIMAL128", "_DECIMAL32", "_DECIMAL64",
# "_GENERIC", "_IMAGINARY", "_NORETURN", "_STATIC_ASSERT", "_THREAD_LOCAL",
# "_GENERIC", "_IMAGINARY", "_STATIC_ASSERT", "_THREAD_LOCAL",
)

extra_keywords_with_alias = {
"__asm__": "__ASM__",
"__attribute__": "__ATTRIBUTE__",
"__restrict": "RESTRICT",
"__inline__": "INLINE",
"__inline": "INLINE",
}

keyword_map = {}
Expand Down Expand Up @@ -650,6 +652,7 @@ def p_declaration_specifier(p):
""" declaration_specifier : storage_class_specifier
| type_specifier
| type_qualifier
| function_specifier
"""
p[0] = p[1]

Expand Down Expand Up @@ -916,6 +919,12 @@ def p_type_qualifier(p):
p[0] = cdeclarations.TypeQualifier(p[1])


def p_function_specifier(p):
""" function_specifier : INLINE
| _NORETURN
"""


def p_declarator(p):
""" declarator : pointer direct_declarator
| direct_declarator
Expand Down Expand Up @@ -1367,6 +1376,7 @@ def p_error(t):

def p_pragma(p):
""" pragma : pragma_pack
| PRAGMA pragma_directive_list PRAGMA_END
"""


Expand Down Expand Up @@ -1420,5 +1430,22 @@ def p_pragma_pack_stack_args(p):
p[0] = (op, id, n)


def p_pragma_directive_list(p):
""" pragma_directive_list : pragma_directive
| pragma_directive_list pragma_directive
"""
if len(p) == 3:
p[0] = p[1] + (p[2],)
else:
p[0] = (p[1],)


def p_pragma_directive(p):
""" pragma_directive : IDENTIFIER
| string_literal
"""
p[0] = p[1]


def main():
yacc.yacc(tabmodule="new_parsetab")
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def get_ctypes_type(self, typ, declarator, check_qualifiers=False):
signed = False
elif specifier == "long":
longs += 1
elif specifier == "short":
longs = -1
else:
typename = str(specifier)

Expand Down
596 changes: 302 additions & 294 deletions python/libgrass_interface_generator/ctypesgen/parser/parsetab.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ def token(self):
class PreprocessorParser(object):
def __init__(self, options, cparser):
self.defines = [
"inline=",
"__inline__=",
"__extension__=",
"__const=const",
"__asm__(x)=",
Expand All @@ -70,9 +68,6 @@ def __init__(self, options, cparser):
# errors in the macOS standard headers.
if IS_MAC:
self.defines += [
"__uint16_t=uint16_t",
"__uint32_t=uint32_t",
"__uint64_t=uint64_t",
"_Nullable=",
"_Nonnull=",
]
Expand Down
11 changes: 6 additions & 5 deletions python/libgrass_interface_generator/ctypesgen/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ def version():
out, err = p.communicate()
if p.returncode:
raise RuntimeError("no version defined?")
return out.strip().decode()
git_tag = out.strip().decode()
return f"{DEFAULT_PREFIX}-{git_tag}"
except Exception:
# failover is to try VERSION_FILE instead
try:
return read_file_version()
return f"{DEFAULT_PREFIX}-{read_file_version()}"
except Exception:
return DEFAULT_PREFIX + "-0.0.0"
return f"{DEFAULT_PREFIX}-0.0.0"


def version_number():
Expand Down Expand Up @@ -72,11 +73,11 @@ def write_version_file(v=None):
import argparse

p = argparse.ArgumentParser()
p.add_argument("--save", action="store_true", help="Store version to " + VERSION_FILE)
p.add_argument("--save", action="store_true", help=f"Store version to {VERSION_FILE}")
p.add_argument(
"--read-file-version",
action="store_true",
help="Read the version stored in " + VERSION_FILE,
help=f"Read the version stored in {VERSION_FILE}",
)
args = p.parse_args()

Expand Down

0 comments on commit 7ccc717

Please sign in to comment.