From b99a252c3503b095b6111b59b11a6659e42205c8 Mon Sep 17 00:00:00 2001
From: Christoph Egger <Christoph.Egger@fau.de>
Date: Wed, 11 Mar 2015 14:31:26 +0100
Subject: [PATCH] Export the Guile interpreter as QtPlugin

The Guile interpreter is now a proper Plugin. Next step is listing +
loading scripting plugins properly

Closes T10
---
 CMakeLists.txt               | 30 +++++++++++++++++++++++++++---
 src/bindings/Guile.hxx       |  5 ++++-
 src/bindings/Interpreter.hxx |  3 +++
 src/gui/Mainwindow.cxx       |  5 ++++-
 src/gui/qt.hxx               |  1 +
 5 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3a4b943..9d27f61 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -39,7 +39,7 @@ SET(CMAKE_SWIG_FLAGS -Linkage passive)
 #INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
 SET_SOURCE_FILES_PROPERTIES(src/bindings/guile/frida.i PROPERTIES CPLUSPLUS ON)
 SWIG_ADD_MODULE(frida-guile guile src/bindings/guile/frida.i)
-SWIG_LINK_LIBRARIES(frida-guile)
+SWIG_LINK_LIBRARIES(frida-guile ${GUILE_LDFLAGS})
 
 
 #################
@@ -47,7 +47,6 @@ SWIG_LINK_LIBRARIES(frida-guile)
 #################
 
 SET(frida_SOURCES
-  src/bindings/Guile.cxx
   src/core/InformationManager.cxx
   src/core/BasicBlock.cxx
   src/core/Function.cxx
@@ -83,7 +82,32 @@ ADD_DEFINITIONS(${QT_DEFINITIONS})
 
 qt5_use_modules(frida Widgets)
 
-TARGET_LINK_LIBRARIES(frida ${QT_LIBRARIES} ${LLVM_LDFLAGS} -lLLVM-3.5 ${LLVM_SYSTEM_LIBS} ${LOG4CXX_LDFLAGS} ${GUILE_LDFLAGS} ${QUAZIP_LIBRARIES})
+TARGET_LINK_LIBRARIES(frida
+  ${QT_LIBRARIES}
+  ${LLVM_LDFLAGS}
+  -lLLVM-3.5
+  ${LLVM_SYSTEM_LIBS}
+  ${LOG4CXX_LDFLAGS}
+  ${QUAZIP_LIBRARIES})
+
+
+###########
+# Plugins #
+###########
+
+# GUILE
+
+set(plugin_guile_SOURCES
+  src/bindings/Interpreter.cxx
+  src/bindings/Guile.cxx)
+
+set(plugin_guile_HEADERS
+  src/bindings/Interpreter.hxx
+  src/bindings/Guile.hxx)
+
+add_library(guilePlugin MODULE ${plugin_guile_SOURCES} ${plugin_guile_HEADERS_MOC})
+qt5_use_modules(guilePlugin Core)
+target_link_libraries(guilePlugin ${QT_LIBRARIES} ${GUILE_LDFLAGS})
 
 ##################
 # TEST Framework #
diff --git a/src/bindings/Guile.hxx b/src/bindings/Guile.hxx
index 7c45a95..d67a0dd 100644
--- a/src/bindings/Guile.hxx
+++ b/src/bindings/Guile.hxx
@@ -5,7 +5,10 @@
 
 #include "Interpreter.hxx"
 
-class GuileInterpreter : public Interpreter {
+class GuileInterpreter : public QObject, public Interpreter {
+    Q_OBJECT
+    Q_PLUGIN_METADATA(IID "xyz.frida.Interpreter")
+    Q_INTERFACES(Interpreter)
 public:
 	GuileInterpreter();
 
diff --git a/src/bindings/Interpreter.hxx b/src/bindings/Interpreter.hxx
index 648301b..898f0e0 100644
--- a/src/bindings/Interpreter.hxx
+++ b/src/bindings/Interpreter.hxx
@@ -3,6 +3,7 @@
 
 #include <string>
 #include <sstream>
+#include <QtPlugin>
 
 class Interpreter {
 public:
@@ -20,4 +21,6 @@ public:
 private:
 };
 
+Q_DECLARE_INTERFACE(Interpreter, "xyz.frida.Interpreter")
+
 #endif /* INCLUDE__Interpreter_hxx_ */
diff --git a/src/gui/Mainwindow.cxx b/src/gui/Mainwindow.cxx
index 47c7885..2036c48 100644
--- a/src/gui/Mainwindow.cxx
+++ b/src/gui/Mainwindow.cxx
@@ -40,7 +40,10 @@ Mainwindow::Mainwindow(InformationManager* mgr)
 
 	QMenu* interpretermenu = menuBar()->addMenu(tr("&Interpreter"));
 
-	interpreter["GUILE"] = new GuileInterpreter;
+	QPluginLoader* loader = new QPluginLoader("libguilePlugin", this);
+	if (!loader->load())
+		LOG4CXX_ERROR(logger, "Loading plugin failed: " << loader->errorString().toStdString());
+	interpreter["GUILE"] = qobject_cast<Interpreter*>(loader->instance());
 	scripting = new ScriptingDock(interpreter["GUILE"], tr("Scripting"), this);
 	scripting->setAllowedAreas(Qt::BottomDockWidgetArea);
 	addDockWidget(Qt::BottomDockWidgetArea, scripting);
diff --git a/src/gui/qt.hxx b/src/gui/qt.hxx
index ac08c0b..e0ff092 100644
--- a/src/gui/qt.hxx
+++ b/src/gui/qt.hxx
@@ -28,5 +28,6 @@
 #include <QXmlStreamWriter>
 #include <QXmlStreamReader>
 #include <QApplication>
+#include <QPluginLoader>
 
 #endif /* INCLUDE__qt_hxx_ */
-- 
2.39.5