compile with swig files if available

This commit is contained in:
Philippe Teuwen
2020-11-13 22:40:06 +01:00
parent 2f6261badb
commit f37088095b
17 changed files with 198 additions and 593 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ LD = g++
SH = sh
BASH = bash
PERL = perl
CCC =foo
SWIG = swig
CC_VERSION = $(shell $(CC) -dumpversion 2>/dev/null|sed 's/\..*//')
CC_VERSION := $(or $(strip $(CC_VERSION)),0)
+22
View File
@@ -455,6 +455,28 @@ else (SKIPWHEREAMISYSTEM EQUAL 1)
message(STATUS "Whereami library: system library not found, using local library")
endif (WHEREAMI_FOUND)
endif (SKIPWHEREAMISYSTEM EQUAL 1)
# Lua SWIG
if (EXISTS ${PM3_ROOT}/client/src/pm3_luawrap.c)
set (TARGET_SOURCES
${PM3_ROOT}/client/src/pm3_luawrap.c
${TARGET_SOURCES})
add_definitions(-DHAVE_LUA_SWIG)
message(STATUS "Lua SWIG: wrapper found")
endif (EXISTS ${PM3_ROOT}/client/src/pm3_luawrap.c)
# Python SWIG
if (NOT SKIPPYTHON EQUAL 1)
if (PYTHON3EMBED_FOUND OR PYTHON3_FOUND)
if (EXISTS ${PM3_ROOT}/client/src/pm3_pywrap.c)
set (TARGET_SOURCES
${PM3_ROOT}/client/src/pm3_pywrap.c
${TARGET_SOURCES})
add_definitions(-DHAVE_PYTHON_SWIG)
message(STATUS "Python SWIG: wrapper found")
endif (EXISTS ${PM3_ROOT}/client/src/pm3_pywrap.c)
endif (PYTHON3EMBED_FOUND OR PYTHON3_FOUND)
endif (NOT SKIPPYTHON EQUAL 1)
message(STATUS "===================================================================")
add_executable(proxmark3
+55
View File
@@ -285,6 +285,19 @@ ifneq ($(SKIPREADLINE),1)
READLINE_FOUND = 1
endif
########
# SWIG #
########
ifneq ("$(wildcard src/pm3_luawrap.c)","")
SWIG_LUA_FOUND = 1
endif
ifneq ($(SKIPPYTHON),1)
ifneq ("$(wildcard src/pm3_pywrap.c)","")
SWIG_PYTHON_FOUND = 1
endif
endif
#######################################################################################################
CFLAGS ?= $(DEFCFLAGS)
# We cannot just use CFLAGS+=... because it has impact on sub-makes if CFLAGS is defined in env:
@@ -315,6 +328,12 @@ ifeq ($(PYTHON_FOUND),1)
PM3CFLAGS += -DHAVE_PYTHON
endif
ifeq ($(SWIG_LUA_FOUND),1)
PM3CFLAGS += -DHAVE_LUA_SWIG
endif
ifeq ($(SWIG_PYTHON_FOUND),1)
PM3CFLAGS += -DHAVE_PYTHON_SWIG
endif
CXXFLAGS ?= -Wall -Werror -O3
PM3CXXFLAGS = $(CXXFLAGS)
@@ -414,6 +433,13 @@ else
endif
endif
ifeq ($(SWIG_LUA_FOUND),1)
$(info Lua SWIG: wrapper found)
endif
ifeq ($(SWIG_PYTHON_FOUND),1)
$(info Python SWIG: wrapper found)
endif
$(info compiler version: $(shell $(CC) --version|head -n 1))
$(info ===================================================================)
@@ -572,6 +598,15 @@ SRCS += bucketsort.c \
parity.c \
util_posix.c
# swig
ifeq ($(SWIG_LUA_FOUND),1)
SRCS += pm3_luawrap.c
endif
ifeq ($(SWIG_PYTHON_FOUND),1)
SRCS += pm3_pywrap.c
endif
# gui
ifeq ($(QT_FOUND),1)
CXXSRCS = proxgui.cpp proxguiqt.cpp proxguiqt.moc.cpp
@@ -711,6 +746,18 @@ ifneq ($(WHEREAMI_FOUND),1)
$(Q)$(MAKE) --no-print-directory -C $(WHEREAMILIBPATH) all
endif
########
# SWIG #
########
src/pm3_luawrap.c: pm3.i
$(info [=] GEN $@)
$(Q)$(SWIG) -lua -o $@ $<
src/pm3_pywrap.c: pm3.i
$(info [=] GEN $@)
$(Q)$(SWIG) -python -o $@ $<
########
# misc #
########
@@ -725,6 +772,14 @@ src/version.c: default_version.c
# easy printing of MAKE VARIABLES
print-%: ; @echo $* = $($*)
# SWIG files emit a number of warnings, we've to ignore them
%wrap.o: %wrap.c
$(OBJDIR)/%wrap.o : %wrap.c $(OBJDIR)/%wrap.d
$(info [-] CC $<)
$(Q)$(MKDIR) $(dir $@)
$(Q)$(CC) $(DEPFLAGS) $(PM3CFLAGS) -Wno-missing-prototypes -Wno-missing-declarations -Wno-missing-field-initializers -c -o $@ $<
$(Q)$(POSTCOMPILE)
%.o: %.c
$(OBJDIR)/%.o : %.c $(OBJDIR)/%.d
$(info [-] CC $<)
@@ -1 +0,0 @@
build/
@@ -1,4 +1,5 @@
#!/bin/bash
swig -lua -o ../src/pm3_luawrap.c ../src/pm3.i
swig -python -o ../src/pm3_pywrap.c ../src/pm3.i
cd ..
make src/pm3_luawrap.c
make src/pm3_pywrap.c
@@ -1,5 +1,6 @@
#!/bin/bash
cd ..
rm -rf build
mkdir build
(
@@ -7,4 +8,3 @@ mkdir build
cmake ..
make -j
)
@@ -1,3 +0,0 @@
#!/bin/bash
build/proxmark3 /dev/ttyACM0 -c "script run testembedded.lua"
@@ -0,0 +1,3 @@
#!/bin/bash
../build/proxmark3 /dev/ttyACM0 -c "script run testembedded.lua"
@@ -4,4 +4,4 @@
#/usr/lib/python3/dist-packages/pm3.py
# need access to pm3.py
PYTHONPATH=../src build/proxmark3 /dev/ttyACM0 -c "script run testembedded.py"
PYTHONPATH=../src ../build/proxmark3 /dev/ttyACM0 -c "script run testembedded.py"
@@ -0,0 +1,8 @@
#!/bin/bash
#/usr/local/lib/python3/dist-packages/pm3.py
#/usr/lib/python3/dist-packages/pm3.py
# need access to pm3.py
PYTHONPATH=../src ../proxmark3 /dev/ttyACM0 -c "script run testembedded_grab.py" -i
File diff suppressed because it is too large Load Diff
+78
View File
@@ -0,0 +1,78 @@
import os
import sys
import threading
import time
# From https://stackoverflow.com/a/29834357
class OutputGrabber(object):
"""
Class used to grab standard output or another stream.
"""
escape_char = "\b"
def __init__(self, stream=None, threaded=False):
self.origstream = stream
self.threaded = threaded
if self.origstream is None:
self.origstream = sys.stdout
self.origstreamfd = self.origstream.fileno()
self.capturedtext = ""
# Create a pipe so the stream can be captured:
self.pipe_out, self.pipe_in = os.pipe()
def __enter__(self):
self.start()
return self
def __exit__(self, type, value, traceback):
self.stop()
def start(self):
"""
Start capturing the stream data.
"""
self.capturedtext = ""
# Save a copy of the stream:
self.streamfd = os.dup(self.origstreamfd)
# Replace the original stream with our write pipe:
os.dup2(self.pipe_in, self.origstreamfd)
if self.threaded:
# Start thread that will read the stream:
self.workerThread = threading.Thread(target=self.readOutput)
self.workerThread.start()
# Make sure that the thread is running and os.read() has executed:
time.sleep(0.01)
def stop(self):
"""
Stop capturing the stream data and save the text in `capturedtext`.
"""
# Print the escape character to make the readOutput method stop:
self.origstream.write(self.escape_char)
# Flush the stream to make sure all our data goes in before
# the escape character:
self.origstream.flush()
if self.threaded:
# wait until the thread finishes so we are sure that
# we have until the last character:
self.workerThread.join()
else:
self.readOutput()
# Close the pipe:
os.close(self.pipe_in)
os.close(self.pipe_out)
# Restore the original stream:
os.dup2(self.streamfd, self.origstreamfd)
# Close the duplicate stream:
os.close(self.streamfd)
def readOutput(self):
"""
Read the stream data (one byte at a time)
and save the text in `capturedtext`.
"""
while True:
char = os.read(self.pipe_out,1).decode(self.origstream.encoding)
if not char or self.escape_char in char:
break
self.capturedtext += char
+13
View File
@@ -0,0 +1,13 @@
#!/usr/bin/env python3
import pm3
from output_grabber import OutputGrabber
out = OutputGrabber()
p=pm3.pm3()
print("Device:", p.name)
with out:
p.console("hw status")
for line in out.capturedtext.split('\n'):
if "Unique ID" in line:
print(line)
+4 -4
View File
@@ -11,22 +11,22 @@
typedef struct {
%extend {
pm3() {
printf("SWIG pm3 constructor, get current pm3\n");
// printf("SWIG pm3 constructor, get current pm3\n");
pm3_device * p = pm3_get_current_dev();
p->script_embedded = 1;
return p;
}
pm3(char *port) {
printf("SWIG pm3 constructor with port, open pm3\n");
// printf("SWIG pm3 constructor with port, open pm3\n");
pm3_device * p = pm3_open(port);
p->script_embedded = 0;
return p;
}
~pm3() {
if ($self->script_embedded) {
printf("SWIG pm3 destructor, nothing to do\n");
// printf("SWIG pm3 destructor, nothing to do\n");
} else {
printf("SWIG pm3 destructor, close pm3\n");
// printf("SWIG pm3 destructor, close pm3\n");
pm3_close($self);
}
}
+4 -4
View File
@@ -2687,7 +2687,7 @@ static swig_module_info swig_module = {swig_types, 1, 0, 0, 0, 0};
#include "comms.h"
SWIGINTERN pm3 *new_pm3__SWIG_0(void){
printf("SWIG pm3 constructor, get current pm3\n");
// printf("SWIG pm3 constructor, get current pm3\n");
pm3_device * p = pm3_get_current_dev();
p->script_embedded = 1;
return p;
@@ -2701,16 +2701,16 @@ SWIGINTERN int SWIG_lua_isnilstring(lua_State *L, int idx) {
}
SWIGINTERN pm3 *new_pm3__SWIG_1(char *port){
printf("SWIG pm3 constructor with port, open pm3\n");
// printf("SWIG pm3 constructor with port, open pm3\n");
pm3_device * p = pm3_open(port);
p->script_embedded = 0;
return p;
}
SWIGINTERN void delete_pm3(pm3 *self){
if (self->script_embedded) {
printf("SWIG pm3 destructor, nothing to do\n");
// printf("SWIG pm3 destructor, nothing to do\n");
} else {
printf("SWIG pm3 destructor, close pm3\n");
// printf("SWIG pm3 destructor, close pm3\n");
pm3_close(self);
}
}
+4 -4
View File
@@ -2672,7 +2672,7 @@ static swig_module_info swig_module = {swig_types, 2, 0, 0, 0, 0};
#include "comms.h"
SWIGINTERN pm3 *new_pm3__SWIG_0(void){
printf("SWIG pm3 constructor, get current pm3\n");
// printf("SWIG pm3 constructor, get current pm3\n");
pm3_device * p = pm3_get_current_dev();
p->script_embedded = 1;
return p;
@@ -2801,16 +2801,16 @@ SWIG_AsCharPtrAndSize(PyObject *obj, char** cptr, size_t* psize, int *alloc)
SWIGINTERN pm3 *new_pm3__SWIG_1(char *port){
printf("SWIG pm3 constructor with port, open pm3\n");
// printf("SWIG pm3 constructor with port, open pm3\n");
pm3_device * p = pm3_open(port);
p->script_embedded = 0;
return p;
}
SWIGINTERN void delete_pm3(pm3 *self){
if (self->script_embedded) {
printf("SWIG pm3 destructor, nothing to do\n");
// printf("SWIG pm3 destructor, nothing to do\n");
} else {
printf("SWIG pm3 destructor, close pm3\n");
// printf("SWIG pm3 destructor, close pm3\n");
pm3_close(self);
}
}