All Projects → trolldbois → ctypeslib

trolldbois / ctypeslib

Licence: MIT License
Generate python ctypes classes from C headers. Requires LLVM clang

Programming Languages

python
139335 projects - #7 most used programming language
c
50402 projects - #5 most used programming language
Smarty
1635 projects
Makefile
30231 projects
C++
36643 projects - #6 most used programming language
shell
77523 projects

Labels

Projects that are alternatives of or similar to ctypeslib

Jucipp
A lightweight & cross-platform IDE supporting the most recent C++ standards. This project has moved to https://gitlab.com/cppit/jucipp.
Stars: ✭ 887 (+577.1%)
Mutual labels:  libclang
Deoplete Clang
deoplete.nvim source for C/C++/Obj-C/Obj-C++ with clang-python3
Stars: ✭ 186 (+41.98%)
Mutual labels:  libclang
pywrap
C++ binding generator based on libclang and pybind11
Stars: ✭ 17 (-87.02%)
Mutual labels:  libclang
Irony Mode
A C/C++ minor mode for Emacs powered by libclang
Stars: ✭ 851 (+549.62%)
Mutual labels:  libclang
Siplasplas
A library for C++ reflection and introspection
Stars: ✭ 167 (+27.48%)
Mutual labels:  libclang
Cppast.net
CppAst is a .NET library providing a C/C++ parser for header files powered by Clang/libclang with access to the full AST, comments and macros
Stars: ✭ 228 (+74.05%)
Mutual labels:  libclang
Easyclangcomplete
💥 Robust C/C++ code completion for Sublime Text 3
Stars: ✭ 537 (+309.92%)
Mutual labels:  libclang
cide
A fast, lightweight C/C++ IDE for Linux and Windows
Stars: ✭ 33 (-74.81%)
Mutual labels:  libclang
Dstep
A tool for converting C and Objective-C headers to D modules
Stars: ✭ 177 (+35.11%)
Mutual labels:  libclang
cxxd
C/C++ language server implemented on top of Clang frontend.
Stars: ✭ 145 (+10.69%)
Mutual labels:  libclang
Gmock.py
'Google Mock' mocks generator based on libclang
Stars: ✭ 35 (-73.28%)
Mutual labels:  libclang
Clang.jl
Julia interface to libclang and C wrapper generator
Stars: ✭ 142 (+8.4%)
Mutual labels:  libclang
hcparse
High-level nim bindings for parsing C/C++ code
Stars: ✭ 37 (-71.76%)
Mutual labels:  libclang
Color coded
A vim plugin for libclang-based highlighting of C, C++, ObjC
Stars: ✭ 841 (+541.98%)
Mutual labels:  libclang
bootstrap
Bootstrap Go bindings for Clang's C API
Stars: ✭ 18 (-86.26%)
Mutual labels:  libclang
Cmake Ide
Use Emacs as a C/C++ IDE
Stars: ✭ 661 (+404.58%)
Mutual labels:  libclang
Dpp
Directly include C headers in D source code
Stars: ✭ 189 (+44.27%)
Mutual labels:  libclang
layout
Determine the layout of C and C++ types, including their size, and the size, offset, and padding of each field in the type.
Stars: ✭ 21 (-83.97%)
Mutual labels:  libclang
ffi-clang
Ruby FFI bindings for libclang 3.4+.
Stars: ✭ 41 (-68.7%)
Mutual labels:  libclang
regen
Easy C++ reflection and code generation
Stars: ✭ 29 (-77.86%)
Mutual labels:  libclang

ctypeslib with libclang

Build Status

Coverage Status

Latest release Supported versions

PyPI Python

Quick usage guide in the docs/ folder.

Status update

  • 2021-02:
    • Thanks for the pull requests
    • Note: libclang-xx-dev must be installed for stddef and other reasons.
    • bump to libclang-11
  • 2018-01-03: master branch works with libclang-5.0 HEAD, python clang from pypi, python3
  • 2017-05-01: master branch works with libclang-4.0 HEAD

Installation

On Ubuntu, libclang libraries are installed with versions. This library tries to load a few different versions to help you out. (__init__.py) But if you encounter a version compatibility issue, you might have to fix the problem using one of the following solutions:

  1. Install libclang-11-dev to get libclang.so (maybe)
  2. OR create a link to libclang-11.so.1 named libclang.so
  3. OR hardcode a call to clang.cindex.Config.load_library_file('libclang-10.so.1') in your code

Pypi

Stable Distribution is available through PyPi at https://pypi.python.org/pypi/ctypeslib2/

sudo pip install ctypeslib2

Setting up clang >= 3.7 dependency

See the LLVM Clang instructions at http://apt.llvm.org/ or use your distribution's packages.

Examples - Library usage

import ctypeslib
py_module = ctypeslib.translate('''int i = 12;''')
print(py_module.i)  # Prints 12

py_module2 = ctypeslib.translate('''struct coordinates { int i ; int y; };''')
print(py_module.struct_coordinates)  # <class 'struct_coordinates'>
print(py_module.struct_coordinates(1,2))  # <struct_coordinates object at 0xabcde12345>

Look at test/test_example_script.py for more advanced Library usage

Other example - CLI

Source file:

$ cat t.c 
struct my_bitfield {
long a:3;
long b:4;
unsigned long long c:3;
unsigned long long d:3;
long f:2;
};

Run c-to-python script:

clang2py t.c

Output:

# -*- coding: utf-8 -*-
#
# TARGET arch is: []
# WORD_SIZE is: 8
# POINTER_SIZE is: 8
# LONGDOUBLE_SIZE is: 16
#
import ctypes

class struct_my_bitfield(ctypes.Structure):
    _pack_ = True # source:False
    _fields_ = [
    ('a', ctypes.c_int64, 3),
    ('b', ctypes.c_int64, 4),
    ('c', ctypes.c_int64, 3),
    ('d', ctypes.c_int64, 3),
    ('f', ctypes.c_int64, 2),
    ('PADDING_0', ctypes.c_int64, 49)]

__all__ = \
    ['struct_my_bitfield']

Other example with headers:

Source file:

$ cat test-stdbool.c 
#include <stdbool.h>

typedef struct s_foo {
bool bar1;
bool bar2;
bool bar3;
} foo;

Run c-to-python script (with any relevant include folder):

clang2py --clang-args="-I/usr/include/clang/4.0/include" test-stdbool.c

Output:

# -*- coding: utf-8 -*-
#
# TARGET arch is: ['-I/usr/include/clang/4.0/include']
# WORD_SIZE is: 8
# POINTER_SIZE is: 8
# LONGDOUBLE_SIZE is: 16
#
import ctypes

class struct_s_foo(ctypes.Structure):
    _pack_ = True # source:False
    _fields_ = [
    ('bar1', ctypes.c_bool),
    ('bar2', ctypes.c_bool),
    ('bar3', ctypes.c_bool),]

foo = struct_s_foo
__all__ = ['struct_s_foo', 'foo']

pack and PADDING explanation

clang2py test/data/test-record.c

This outputs:

# ...

class struct_Node2(Structure):
    _pack_ = True # source:False
    _fields_ = [ 
    ('m1', ctypes.c_ubyte),
    ('PADDING_0', ctypes.c_ubyte * 7),
    ('m2', POINTER_T(struct_Node)),]

# ...

The PADDING_0 field is added to force the ctypes memory Structure to align fields offset with the definition given by the clang compiler.

The pack attribute forces the alignment on 0 bytes, to ensure all fields are as defined by this library, and not per the compiler used by the host python binary

The objective of this, is to be able to produce cross-architecture python code, that can read memory structures from a different architecture (like reading a memory dump from a different architecture)

Usage

usage: clang2py [-h] [-c] [-d] [--debug] [-e] [-k TYPEKIND] [-i] [-l DLL]
                [-m module] [--nm NM] [-o OUTPUT] [-p DLL] [-q]
                [-r EXPRESSION] [-s SYMBOL] [-t TARGET] [-v] [-V] [-w W] [-x]
                [--show-ids SHOWIDS] [--max-depth N] [--clang-args CLANG_ARGS]
                files [files ...]

Version 2.3.0. Generate python code from C headers

positional arguments:
  files                 source filenames. stdin is not supported

optional arguments:
  -h, --help            show this help message and exit
  -c, --comments        include source doxygen-style comments
  -d, --doc             include docstrings containing C prototype and source
                        file location
  --debug               setLevel to DEBUG
  -e, --show-definition-location
                        include source file location in comments
  -k TYPEKIND, --kind TYPEKIND
                        kind of type descriptions to include: a = Alias, c =
                        Class, d = Variable, e = Enumeration, f = Function, m
                        = Macro, #define s = Structure, t = Typedef, u = Union
                        default = 'cdefstu'
  -i, --includes        include declaration defined outside of the sourcefiles
  -l DLL, --include-library DLL
                        library to search for exported functions. Add multiple
                        times if required
  -m module, --module module
                        Python module(s) containing symbols which will be
                        imported instead of generated
  --nm NM               nm program to use to extract symbols from libraries
  -o OUTPUT, --output OUTPUT
                        output filename (if not specified, standard output
                        will be used)
  -p DLL, --preload DLL
                        dll to be loaded before all others (to resolve
                        symbols)
  -q, --quiet           Shut down warnings and below
  -r EXPRESSION, --regex EXPRESSION
                        regular expression for symbols to include (if neither
                        symbols nor expressions are specified,everything will
                        be included)
  -s SYMBOL, --symbol SYMBOL
                        symbol to include (if neither symbols nor expressions
                        are specified,everything will be included)
  -t TARGET, --target TARGET
                        target architecture (default: x86_64-Linux)
  -v, --verbose         verbose output
  -V, --version         show program's version number and exit
  -w W                  add all standard windows dlls to the searched dlls
                        list
  -x, --exclude-includes
                        Parse object in sources files only. Ignore includes
  --show-ids SHOWIDS    Don't compute cursor IDs (very slow)
  --max-depth N         Limit cursor expansion to depth N
  --clang-args CLANG_ARGS
                        clang options, in quotes: --clang-args="-std=c99
                        -Wall"

Cross-architecture: You can pass target modifiers to clang. For example, try
--clang-args="-target x86_64" or "-target i386-linux" to change the target CPU
arch.

Inner workings for memo

  • clang2py is a script that calls ctypeslib/ctypeslib/clang2py.py
  • clang2py.py is mostly the old xml2py.py module forked to use libclang.
  • clang2py.py calls ctypeslib/ctypeslib/codegen/codegenerator.py
  • codegenerator.py calls ctypeslib/ctypeslib/codegen/clangparser.py
  • clangparser.py uses libclang's python binding to access the clang internal representation of the C source code.
    • It then translate each child of the AST tree to python objects as listed in typedesc.
  • codegenerator.py then uses these python object to generate ctypes-based python source code.

Because clang is capable to handle different target architecture, this fork {is/should be} able to produce cross-platform memory representation if needed.

Credits

This fork of ctypeslib is mainly about using the libclang1>=3.7 python bindings to generate python code from C source code, instead of gccxml.

the original ctypeslib contains these packages:

  • ctypeslib.codegen - a code generator
  • ctypeslib.contrib - various contributed modules
  • ctypeslib.util - assorted small helper functions
  • ctypeslib.test - unittests

This fork of ctypeslib is heavily patched for clang.

The original ctypeslib is written by

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].