Fixed identation
[yosys.git] / kernel / yosys.cc
index 450e4e4cfba7be82e1d8987279fd50f1fbdb8811..a12355f1d509453bf54bc6b4414f019e90fcee73 100644 (file)
 #  include <sys/sysctl.h>
 #endif
 
+#ifdef WITH_PYTHON
+#if PY_MAJOR_VERSION >= 3
+#   define INIT_MODULE PyInit_libyosys
+    extern "C" PyObject* INIT_MODULE();
+#else
+#   define INIT_MODULE initlibyosys
+       extern "C" void INIT_MODULE();
+#endif
+#endif
+
 #include <limits.h>
 #include <errno.h>
 
@@ -477,21 +487,42 @@ int GetSize(RTLIL::Wire *wire)
        return wire->width;
 }
 
+bool already_setup = false;
+
 void yosys_setup()
 {
+       if(already_setup)
+               return;
+       already_setup = true;
        // if there are already IdString objects then we have a global initialization order bug
        IdString empty_id;
        log_assert(empty_id.index_ == 0);
        IdString::get_reference(empty_id.index_);
 
+       #ifdef WITH_PYTHON
+               PyImport_AppendInittab((char*)"libyosys", INIT_MODULE);
+               Py_Initialize();
+               PyRun_SimpleString("import sys");
+       #endif
+
        Pass::init_register();
        yosys_design = new RTLIL::Design;
        yosys_celltypes.setup();
        log_push();
 }
 
+bool yosys_already_setup()
+{
+       return already_setup;
+}
+
+bool already_shutdown = false;
+
 void yosys_shutdown()
 {
+       if(already_shutdown)
+               return;
+       already_shutdown = true;
        log_pop();
 
        delete yosys_design;
@@ -519,9 +550,16 @@ void yosys_shutdown()
                dlclose(it.second);
 
        loaded_plugins.clear();
+#ifdef WITH_PYTHON
+       loaded_python_plugins.clear();
+#endif
        loaded_plugin_aliases.clear();
 #endif
 
+#ifdef WITH_PYTHON
+       Py_Finalize();
+#endif
+
        IdString empty_id;
        IdString::put_reference(empty_id.index_);
 }