Skip to content

Commit

Permalink
Build fixes for Ubuntu 17.04 (with official pybind11 package)
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jul 21, 2017
1 parent 0808e7b commit f90d8c9
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 15 deletions.
8 changes: 6 additions & 2 deletions configure.ac
Expand Up @@ -173,10 +173,14 @@ then
if test ! -z "$PYTHON_CONFIG"
then
PYTHON_LIBS=`$PYTHON_CONFIG --libs`

BOOST_PYTHON
PYTHON_CPPFLAGS=`$PYTHON_CONFIG --includes`

# pybind11.h is including Python.h, need to help it find the Python.h header
CPPFLAGS="$USER_CPPFLAGS $PYTHON_CPPFLAGS"
AC_CHECK_HEADER([pybind11/pybind11.h], [], [AC_MSG_ERROR([Missing pybind11.h, please install the pybind11 package])])
script_module='script'
AC_SUBST([script_module])
AC_SUBST([PYTHON_CPPFLAGS])
AC_SUBST([PYTHON_LIBS])
fi
fi
Expand Down
3 changes: 1 addition & 2 deletions plugins/script/Makefile.am
@@ -1,6 +1,6 @@
AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/libs \
$(XML_CFLAGS) \
$(BOOST_PYTHON_CPPFLAGS) \
$(PYTHON_CPPFLAGS) \
-DPKGLIBDIR='"$(pkglibdir)"'

modulesdir = $(pkglibdir)/modules
Expand All @@ -9,7 +9,6 @@ modules_LTLIBRARIES = script.la
script_la_LDFLAGS = -module -avoid-version \
$(BOOST_SYSTEM_LIBS) \
$(BOOST_FILESYSTEM_LIBS) \
$(BOOST_PYTHON_LIBS) \
$(PYTHON_LIBS) \
$(WX_LIBS)
script_la_LIBADD = $(top_builddir)/libs/math/libmath.la \
Expand Down
8 changes: 5 additions & 3 deletions plugins/script/PythonModule.cpp
@@ -1,5 +1,7 @@
#include "PythonModule.h"

#include "itextstream.h"

namespace script
{

Expand Down Expand Up @@ -73,16 +75,16 @@ PyObject* PythonModule::InitModuleImpl()
py::object main = py::module::import("__main__");
py::dict globals = main.attr("__dict__").cast<py::dict>();

for (auto& i : globals)
for (auto i = globals.begin(); i != globals.end(); ++i)
{
GetGlobals()[i.first] = i.second;
GetGlobals()[(*i).first] = (*i).second;
}

return _module->ptr();
}
catch (py::error_already_set& e)
{
e.clear();
//e.clear();
PyErr_SetString(PyExc_ImportError, e.what());
return nullptr;
}
Expand Down
16 changes: 13 additions & 3 deletions plugins/script/ScriptingSystem.cpp
@@ -1,9 +1,9 @@
#include "ScriptingSystem.h"

#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <pybind11/attr.h>
#include <pybind11/stl_bind.h>
#include <pybind11/eval.h>

#include "i18n.h"
#include "itextstream.h"
Expand Down Expand Up @@ -146,7 +146,7 @@ ExecutionResultPtr ScriptingSystem::executeString(const std::string& scriptStrin
fullScript.append(scriptString);

// Attempt to run the specified script
py::exec(fullScript, PythonModule::GetGlobals());
py::eval<py::eval_statements>(fullScript, PythonModule::GetGlobals());
}
catch (py::error_already_set& ex)
{
Expand Down Expand Up @@ -185,7 +185,12 @@ void ScriptingSystem::addInterfacesToModule(py::module& mod, py::dict& globals)

void ScriptingSystem::initialise()
{
// The initialize_interpreter() function is available in 2.2.x upwards
#if (PYBIND11_VERSION_MAJOR == 2) && (PYBIND11_VERSION_MINOR <= 1)
Py_Initialize();
#else
py::initialize_interpreter();
#endif

{
try
Expand Down Expand Up @@ -287,7 +292,7 @@ void ScriptingSystem::loadCommandScript(const std::string& scriptFilename)
locals["__executeCommand__"] = false;

// Attempt to run the specified script
py::eval_file(_scriptPath + scriptFilename, py::globals(), locals);
py::eval_file(_scriptPath + scriptFilename, PythonModule::GetGlobals(), locals);

std::string cmdName;
std::string cmdDisplayName;
Expand Down Expand Up @@ -494,7 +499,12 @@ void ScriptingSystem::shutdownModule()

PythonModule::Clear();

// The finalize_interpreter() function is available in 2.2.x upwards
#if (PYBIND11_VERSION_MAJOR == 2) && (PYBIND11_VERSION_MINOR <= 1)
Py_Finalize();
#else
py::finalize_interpreter();
#endif
}

} // namespace script
21 changes: 16 additions & 5 deletions plugins/script/interfaces/EntityInterface.cpp
Expand Up @@ -119,6 +119,22 @@ ScriptSceneNode EntityInterface::createEntity(const std::string& eclassName) {
return ScriptSceneNode(node);
}

struct EntityKeyValuePair :
public std::pair<std::string, std::string>
{
using std::pair<std::string, std::string>::pair;

std::string left()
{
return first;
}

std::string right()
{
return second;
}
};

void EntityInterface::registerInterface(py::module& scope, py::dict& globals)
{
// Add the EntityNode interface
Expand All @@ -133,11 +149,6 @@ void EntityInterface::registerInterface(py::module& scope, py::dict& globals)
entityNode.def("isOfType", &ScriptEntityNode::isOfType);
entityNode.def("getKeyValuePairs", &ScriptEntityNode::getKeyValuePairs);

// Declare a KeyValuePair (pair of strings)
py::class_< std::pair<std::string, std::string> > kvPair(scope, "EntityKeyValuePair");
kvPair.def_readwrite("first", &std::pair<std::string, std::string>::first);
kvPair.def_readwrite("second", &std::pair<std::string, std::string>::second);

// Declare the KeyValuePairs vector
py::bind_vector<Entity::KeyValuePairs>(scope, "EntityKeyValuePairs");

Expand Down
1 change: 1 addition & 0 deletions plugins/script/interfaces/PatchInterface.h
@@ -1,6 +1,7 @@
#pragma once

#include "iscript.h"
#include "ipatch.h"

#include "SceneGraphInterface.h"

Expand Down
1 change: 1 addition & 0 deletions plugins/script/interfaces/SceneGraphInterface.h
Expand Up @@ -3,6 +3,7 @@
#include "inode.h"
#include "iscript.h"
#include "iscenegraph.h"
#include "math/AABB.h"

#include <pybind11/pybind11.h>

Expand Down

0 comments on commit f90d8c9

Please sign in to comment.