From 7988ccd28f57c7292b091edb60e1f11207862596 Mon Sep 17 00:00:00 2001 From: Chris Kuethe Date: Thu, 11 May 2017 14:42:55 -0700 Subject: [PATCH] python bindings --- .gitignore | 4 ++++ Makefile | 36 +++++++++++++++++++++++++++++------- csdrpy.i.in | 21 +++++++++++++++++++++ csdrpy_test.py | 14 ++++++++++++++ 4 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 csdrpy.i.in create mode 100644 csdrpy_test.py diff --git a/.gitignore b/.gitignore index 73f43452..2bb0aa9d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,11 @@ nmux ddcd *.o *.so +*.so.* tags dumpvect.*.vect grc_tests/top_block.py *.swp +csdrpy.i +csdrpy.py* +csdrpy_wrap.* diff --git a/Makefile b/Makefile index a029a5b3..0e5c3019 100644 --- a/Makefile +++ b/Makefile @@ -43,16 +43,18 @@ PARAMS_MISC = -Wno-unused-result #PARAMS_DEBUG = $(if $(DEBUG_ON),-g,) FFTW_PACKAGE = fftw-3.3.3 PREFIX ?= /usr +PYVERSION ?= 2.7 SOVERSION = 0.15 -.PHONY: clean-vect clean +.PHONY: clean-vect clean-python clean all: csdr nmux libcsdr.so: fft_fftw.c fft_rpi.c libcsdr_wrapper.c libcsdr.c libcsdr_gpl.c fastddc.c fastddc.h fft_fftw.h fft_rpi.h ima_adpcm.h libcsdr_gpl.h libcsdr.h predefined.h @echo NOTE: you may have to manually edit Makefile to optimize for your CPU \(especially if you compile on ARM, please edit PARAMS_NEON\). @echo Auto-detected optimization parameters: $(PARAMS_SIMD) @echo rm -f dumpvect*.vect - gcc -std=gnu99 $(PARAMS_LOOPVECT) $(PARAMS_SIMD) $(LIBSOURCES) $(PARAMS_LIBS) $(PARAMS_MISC) -fpic -shared -Wl,-soname,libcsdr.so.$(SOVERSION) -o libcsdr.so + gcc -std=gnu99 $(PARAMS_LOOPVECT) $(PARAMS_SIMD) $(LIBSOURCES) $(PARAMS_LIBS) $(PARAMS_MISC) -fpic -shared -Wl,-soname,libcsdr.so.$(SOVERSION) -o libcsdr.so.$(SOVERSION) + @ln -fs libcsdr.so.$(SOVERSION) libcsdr.so -./parsevect dumpvect*.vect csdr: csdr.c libcsdr.so gcc -std=gnu99 $(PARAMS_LOOPVECT) $(PARAMS_SIMD) csdr.c $(PARAMS_LIBS) -L. -lcsdr $(PARAMS_MISC) -o csdr @@ -65,20 +67,20 @@ arm-cross: clean-vect arm-linux-gnueabihf-gcc -std=gnu99 -O3 -fshort-double -ffast-math -dumpbase dumpvect-arm -fdump-tree-vect-details -mfloat-abi=softfp -march=armv7-a -mtune=cortex-a9 -mfpu=neon -mvectorize-with-neon-quad -Wno-unused-result -Wformat=0 $(SOURCES) -lm -o ./csdr clean-vect: rm -f dumpvect*.vect -clean: clean-vect - rm -f libcsdr.so csdr ddcd nmux +clean: clean-vect clean-python + rm -f libcsdr.so.$(SOVERSION) csdr ddcd nmux *.o *.so install: all - install -m 0755 libcsdr.so $(PREFIX)/lib + install -m 0755 libcsdr.so.$(SOVERSION) $(PREFIX)/lib install -m 0755 csdr $(PREFIX)/bin install -m 0755 csdr-fm $(PREFIX)/bin install -m 0755 nmux $(PREFIX)/bin #-install -m 0755 ddcd $(PREFIX)/bin @ldconfig || echo please run ldconfig uninstall: - rm $(PREFIX)/lib/libcsdr.so $(PREFIX)/bin/csdr $(PREFIX)/bin/csdr-fm + rm $(PREFIX)/lib/libcsdr.so.$(SOVERSION) $(PREFIX)/bin/csdr $(PREFIX)/bin/csdr-fm ldconfig disasm: - objdump -S libcsdr.so > libcsdr.disasm + objdump -S libcsdr.so.$(SOVERSION) > libcsdr.disasm emcc-clean: -rm sdr.js/sdr.js -rm sdr.js/sdrjs-compiled.js @@ -98,3 +100,23 @@ emcc: cat sdr.js/sdrjs-header.js sdr.js/sdrjs-compiled.js sdr.js/sdrjs-footer.js > sdr.js/sdr.js emcc-beautify: bash -c 'type js-beautify >/dev/null 2>&1; if [ $$? -eq 0 ]; then js-beautify sdr.js/sdr.js >sdr.js/sdr.js.beautiful; mv sdr.js/sdr.js.beautiful sdr.js/sdr.js; fi' + +python: libcsdr.so clean-python + @echo "/* This file is autogenerated. Any changes should be made to csdrpy.i.in */" > csdrpy.i + @echo 'generating "csdrpy.i"' + @cat csdrpy.i.in >> csdrpy.i + @cat fastddc.h >> csdrpy.i + @cat fft_fftw.h >> csdrpy.i + @cat fft_rpi.h >> csdrpy.i + @cat ima_adpcm.h >> csdrpy.i + @cat libcsdr_gpl.h >> csdrpy.i + @cat libcsdr.h >> csdrpy.i + swig -python csdrpy.i + gcc -c -fPIC csdrpy_wrap.c -I/usr/include/python$(PYVERSION) -I. + gcc -shared csdrpy_wrap.o -L. -lcsdr -lfftw3f -lm -o _csdrpy.so + +clean-python: + rm -f csdrpy_wrap.c csdrpy_wrap.o _csdrpy.so csdrpy.py* csdrpy.i + +python-install: libcsdr.so + install -m 0755 csdrpy.py _csdrpy.so $(PREFIX)/lib/python$(PYVERSION)/ diff --git a/csdrpy.i.in b/csdrpy.i.in new file mode 100644 index 00000000..f2a09231 --- /dev/null +++ b/csdrpy.i.in @@ -0,0 +1,21 @@ +%module csdrpy + +%{ +#define SWIG_FILE_WITH_INIT +#define LIBCSDR_GPL +#define USE_FFTW +#define USE_IMA_ADPCM + +#include "predefined.h" +#include "fastddc.h" +#include "libcsdr.h" +#include "libcsdr_gpl.h" +#include "fft_fftw.h" +#include "fft_rpi.h" +#include "ima_adpcm.h" +%} + +%feature("autodoc", "2"); + +/* contents of libcsdr.h will be appended below for processing by swig */ + diff --git a/csdrpy_test.py b/csdrpy_test.py new file mode 100644 index 00000000..88828b85 --- /dev/null +++ b/csdrpy_test.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python + +import csdrpy + +csdrpy_syms = filter(lambda x: x.startswith('_') == False, dir(csdrpy)) +csdrpy_syms = set(csdrpy_syms) + +assert( csdrpy.is_nan(1.0) == False) +assert( csdrpy.is_nan(float('NaN')) == True) +assert( csdrpy.log2n(1) == 0 ) +assert( csdrpy.log2n(256) == 8) + +print 'Loaded {0} symbols'.format(len(csdrpy_syms)) +print 'csdr python binding works! :)'