aboutsummaryrefslogtreecommitdiffstats
path: root/common/pybindings.cc
diff options
context:
space:
mode:
authorMiodrag Milanovic <mmicko@gmail.com>2018-06-05 21:03:06 +0200
committerMiodrag Milanovic <mmicko@gmail.com>2018-06-05 21:03:06 +0200
commit7cb42f4368e873e5930cf84bc2494a8e8172a046 (patch)
treeed773278240546b1b525c066b95a4e69150aabf1 /common/pybindings.cc
parentbd08f9e698269764d9e06052470d4a699f69f951 (diff)
downloadnextpnr-7cb42f4368e873e5930cf84bc2494a8e8172a046.tar.gz
nextpnr-7cb42f4368e873e5930cf84bc2494a8e8172a046.tar.bz2
nextpnr-7cb42f4368e873e5930cf84bc2494a8e8172a046.zip
Initial GUI work
Diffstat (limited to 'common/pybindings.cc')
-rw-r--r--common/pybindings.cc42
1 files changed, 42 insertions, 0 deletions
diff --git a/common/pybindings.cc b/common/pybindings.cc
index 8bae4e51..4986c549 100644
--- a/common/pybindings.cc
+++ b/common/pybindings.cc
@@ -18,6 +18,7 @@
*
*/
+
#include "design.h"
#include "chip.h"
#include "pybindings.h"
@@ -26,6 +27,9 @@
#define PASTER(x, y) x ## _ ## y
#define EVALUATOR(x, y) PASTER(x,y)
#define MODULE_NAME EVALUATOR(nextpnrpy, ARCHNAME)
+#define PYINIT_MODULE_NAME EVALUATOR(&PyInit_nextpnrpy, ARCHNAME)
+#define STRINGIFY(x) #x
+#define TOSTRING(x) STRINGIFY(x)
// Architecture-specific bindings should be created in the below function, which must be implemented in all
// architectures
@@ -39,3 +43,41 @@ BOOST_PYTHON_MODULE (MODULE_NAME) {
arch_wrap_python();
}
+
+void arch_appendinittab()
+{
+ PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME);
+}
+
+void execute_python_file(const char *executable, const char* python_file)
+{
+ wchar_t *program = Py_DecodeLocale(executable, NULL);
+ if (program == NULL) {
+ fprintf(stderr, "Fatal error: cannot decode executable filename\n");
+ exit(1);
+ }
+ try
+ {
+ PyImport_AppendInittab(TOSTRING(MODULE_NAME), PYINIT_MODULE_NAME);
+ Py_SetProgramName(program);
+ Py_Initialize();
+
+ FILE* fp = fopen(python_file, "r");
+ if (fp == NULL) {
+ fprintf(stderr, "Fatal error: file not found %s\n",python_file);
+ exit(1);
+ }
+ PyRun_SimpleFile(fp , python_file);
+ fclose(fp);
+
+ Py_Finalize();
+ PyMem_RawFree(program);
+ }
+ catch(boost::python::error_already_set const &)
+ {
+ // Parse and output the exception
+ std::string perror_str = parse_python_exception();
+ std::cout << "Error in Python: " << perror_str << std::endl;
+ }
+}
+