From 0bd5a62badf193bbfa695146fc6770219336a56c Mon Sep 17 00:00:00 2001 From: Pierre Pronchery Date: Mon, 11 Feb 2013 02:08:47 +0100 Subject: [PATCH] Early Python binding for libasm (untested) --- Makefile | 4 ++ src/project.conf | 2 +- src/python/Makefile | 52 ++++++++++++++++++++++++++ src/python/libasm.c | 81 +++++++++++++++++++++++++++++++++++++++++ src/python/libasm.py | 25 +++++++++++++ src/python/project.conf | 15 ++++++++ 6 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 src/python/Makefile create mode 100644 src/python/libasm.c create mode 100644 src/python/libasm.py create mode 100644 src/python/project.conf diff --git a/Makefile b/Makefile index 1a5fc52..9f7bb30 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,10 @@ dist: $(PACKAGE)-$(VERSION)/src/format.h \ $(PACKAGE)-$(VERSION)/src/parser.h \ $(PACKAGE)-$(VERSION)/src/token.h \ + $(PACKAGE)-$(VERSION)/src/python/project.conf \ + $(PACKAGE)-$(VERSION)/src/python/Makefile \ + $(PACKAGE)-$(VERSION)/src/python/libasm.c \ + $(PACKAGE)-$(VERSION)/src/python/libasm.py \ $(PACKAGE)-$(VERSION)/src/project.conf \ $(PACKAGE)-$(VERSION)/src/arch/amd64.c \ $(PACKAGE)-$(VERSION)/src/arch/arm.c \ diff --git a/src/project.conf b/src/project.conf index 4dcfbfe..66abe1b 100644 --- a/src/project.conf +++ b/src/project.conf @@ -5,7 +5,7 @@ cppflags= cflags_force=-W `pkg-config --cflags cpp` cflags=-Wall -g -O2 -pedantic ldflags=-ldl -dist=Makefile,arch.h,code.h,common.h,format.h,parser.h,token.h +dist=Makefile,arch.h,code.h,common.h,format.h,parser.h,token.h,python/project.conf,python/Makefile,python/libasm.c,python/libasm.py [libasm] type=library diff --git a/src/python/Makefile b/src/python/Makefile new file mode 100644 index 0000000..da2c661 --- /dev/null +++ b/src/python/Makefile @@ -0,0 +1,52 @@ +MKDIR = mkdir -m 0755 -p +INSTALL = install +RM = rm -f +TARGETS = _libasm.so +PREFIX = /usr/local +DESTDIR = +LIBDIR = $(PREFIX)/lib +CC = cc +CPPFLAGSF= -I ../../include +CPPFLAGS= +CFLAGSF = -W `pkg-config --cflags python-2.7 Asm` -fPIC +CFLAGS = -Wall -g -O2 +LDFLAGSF= `pkg-config --libs python-2.7 Asm` +LDFLAGS = -L.. -L$(LIBDIR) -Wl,-rpath,$(LIBDIR) +AR = ar +RANLIB = ranlib +CCSHARED= $(CC) -shared +RM = rm -f +LN = ln -f +MKDIR = mkdir -m 0755 -p +INSTALL = install + + +all: $(TARGETS) + +_libasm_OBJS = libasm.o +_libasm_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS) +_libasm_LDFLAGS = $(LDFLAGSF) $(LDFLAGS) + +_libasm.so: $(_libasm_OBJS) + $(CCSHARED) -o _libasm.so $(_libasm_OBJS) $(_libasm_LDFLAGS) + +libasm.o: libasm.c + $(CC) $(_libasm_CFLAGS) -c libasm.c + +clean: + $(RM) -- $(_libasm_OBJS) + +distclean: clean + $(RM) -- $(TARGETS) + +install: $(TARGETS) + $(MKDIR) $(DESTDIR)$(PREFIX)/lib/python2.7/site-packages + $(INSTALL) -m 0644 -- _libasm.so $(DESTDIR)$(PREFIX)/lib/python2.7/site-packages/_libasm.so + $(MKDIR) $(DESTDIR)$(PREFIX)/lib/python2.7/site-packages + $(INSTALL) -m 0644 -- libasm.py $(DESTDIR)$(PREFIX)/lib/python2.7/site-packages/libasm.py + +uninstall: + $(RM) -- $(DESTDIR)$(PREFIX)/lib/python2.7/site-packages/_libasm.so + $(RM) -- $(DESTDIR)$(PREFIX)/lib/python2.7/site-packages/libasm.py + +.PHONY: all clean distclean install uninstall diff --git a/src/python/libasm.c b/src/python/libasm.c new file mode 100644 index 0000000..ce42b4c --- /dev/null +++ b/src/python/libasm.c @@ -0,0 +1,81 @@ +/* $Id$ */ +/* Copyright (c) 2013 Pierre Pronchery */ +/* This file is part of DeforaOS Devel asm */ +/* This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . */ + + + +#include +#include "Devel/Asm.h" + + +/* libasm */ +/* private */ +/* constants */ +static char const _libasm_asm_name[] = "libasm::Asm"; + + +/* prototypes */ +/* Asm */ +static PyObject * _libasm_asm_new(PyObject * self, PyObject * args); +static void _libasm_asm_delete(PyObject * self); + + +/* variables */ +static PyMethodDef _libasm_methods[] = +{ + { "asm_new", _libasm_asm_new, METH_VARARGS, + "Instantiates an Asm object." }, + { NULL, NULL, 0, NULL } +}; + + +/* public */ +/* prototypes */ +PyMODINIT_FUNC init_libasm(void); + + +/* functions */ +PyMODINIT_FUNC init_libasm(void) +{ + Py_InitModule("_libasm", _libasm_methods); +} + + +/* private */ +/* functions */ +/* Asm */ +/* libasm_asm_new */ +static PyObject * _libasm_asm_new(PyObject * self, PyObject * args) +{ + Asm * a; + char const * arch; + char const * format; + + if(!PyArg_ParseTuple(args, "ss", &arch, &format)) + return NULL; + if((a = asm_new(arch, format)) == NULL) + return NULL; + return PyCapsule_New(a, _libasm_asm_name, _libasm_asm_delete); +} + + +/* libasm_asm_delete */ +static void _libasm_asm_delete(PyObject * self) +{ + Asm * a; + + if((a = PyCapsule_GetPointer(self, _libasm_asm_name)) == NULL) + return; + asm_delete(a); +} diff --git a/src/python/libasm.py b/src/python/libasm.py new file mode 100644 index 0000000..1af7628 --- /dev/null +++ b/src/python/libasm.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python2.7 +#$Id$ +#Copyright (c) 2013 Pierre Pronchery +#This file is part of DeforaOS Devel asm +#This program is free software: you can redistribute it and/or modify +#it under the terms of the GNU Lesser General Public License as published by +#the Free Software Foundation, version 3 of the License. +# +#This program is distributed in the hope that it will be useful, +#but WITHOUT ANY WARRANTY; without even the implied warranty of +#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +#GNU Lesser General Public License for more details. +# +#You should have received a copy of the GNU Lesser General Public License +#along with this program. If not, see . + + + +import _libasm + + +#Asm +class Asm: + def __init__(self, arch, format): + self.asm = _libasm.asm_new(arch, format) diff --git a/src/python/project.conf b/src/python/project.conf new file mode 100644 index 0000000..bfa181a --- /dev/null +++ b/src/python/project.conf @@ -0,0 +1,15 @@ +targets=_libasm +cppflags_force=-I ../../include +cflags_force=-W `pkg-config --cflags python-2.7 Asm` -fPIC +cflags=-Wall -g -O2 +ldflags_force=`pkg-config --libs python-2.7 Asm` +ldflags=-L.. -L$(LIBDIR) -Wl,-rpath,$(LIBDIR) +dist=Makefile,libasm.py + +[_libasm] +type=plugin +sources=libasm.c +install=$(PREFIX)/lib/python2.7/site-packages + +[libasm.py] +install=$(PREFIX)/lib/python2.7/site-packages