Skip to content

Commit

Permalink
pydrake: Do not import a duplicate of pydrake, but use another module…
Browse files Browse the repository at this point in the history
… to load `libdrake.so`
  • Loading branch information
EricCousineau-TRI committed May 14, 2018
1 parent a68f669 commit bf5dde8
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
30 changes: 30 additions & 0 deletions bindings/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- python -*-

load("//tools/lint:lint.bzl", "add_lint_tests")
load(
"//tools/skylark:pybind.bzl",
"drake_pybind_library",
)
load(
":bazel_workaround_4594_libdrake.bzl",
"get_bazel_workaround_4594_libdrake_package_info",
)

package(default_visibility = [
"//bindings/pydrake:__subpackages__",
])

# This is placed outside of `pydrake` so that we can import this without
# pulling in an extra copy of `pydrake`. For more information, see
# `pydrake/__init__.py`.
drake_pybind_library(
name = "bazel_workaround_4594_libdrake_py",
add_install = False,
cc_so_name = "bazel_workaround_4594_libdrake",
cc_srcs = ["bazel_workaround_4594_libdrake_py.cc"],
# Since `package_info` must be a `struct`, we must define this in a `*.bzl`
# file.
package_info = get_bazel_workaround_4594_libdrake_package_info(),
)

add_lint_tests()
8 changes: 8 additions & 0 deletions bindings/bazel_workaround_4594_libdrake.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- python -*-

def get_bazel_workaround_4594_libdrake_package_info():
"""Only to be used by //bindings:bazel_workaround_4594_libdrake_py.
For more information, see `pydrake/__init__.py`.
"""
return struct(py_imports = ["../.."])
12 changes: 12 additions & 0 deletions bindings/bazel_workaround_4594_libdrake_py.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "pybind11/pybind11.h"

namespace drake {
namespace pydrake {

PYBIND11_MODULE(bazel_workaround_4594_libdrake, m) {
m.doc() = "Consolidated workaround for bazelbuild/bazel#4594 to load "
"libdrake.so, to be used by `pydrake/__init__.py`.";
}

} // namespace pydrake
} // namespace drake
3 changes: 2 additions & 1 deletion bindings/pydrake/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ load(
)

package(default_visibility = [
"//bindings/pydrake:__subpackages__",
"//bindings:__subpackages__",
])

# This determines how `PYTHONPATH` is configured, and how to install the
Expand All @@ -43,6 +43,7 @@ drake_pybind_library(
package_info = PACKAGE_INFO,
py_deps = [
"//bindings/pydrake/util:deprecation_py",
"//bindings:bazel_workaround_4594_libdrake_py",
],
py_srcs = [
"__init__.py",
Expand Down
12 changes: 7 additions & 5 deletions bindings/pydrake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
# When importing `pydrake` as an external under Bazel, Bazel will use a shared
# library whose relative RPATHs are incorrect for `libdrake.so`, and thus will
# fail to load; this issue is captured in bazelbuild/bazel#4594. As a
# workaround, we can use this spelling to load a properly-linked version of
# `libdrake.so`. Once this is loaded, all of the Python C-extension libraries
# workaround, we can use a library that properly links to `libdrake.so`, but in
# `{runfiles}/{workspace}/external/drake` rather than `{runfiles}/drake`.
# Once this is loaded, all of the Python C-extension libraries
# that depend on it (and its dependencies) will load properly.
# Please note that this workaround is only important when running under the
# Bazel runfiles tree. Installed `pydrake` should not have this issue.
# N.B. This will actually import a second copy of `pydrake` in the path below;
# however, we should be able to ignore this as users should not be using it.
# N.B. We do not import `external.drake.bindings.pydrake` as this may cause
# duplicate classes to be loaded (#8810). This will not be a problem once #7912
# is resolved.
try:
import external.drake.bindings.pydrake
import external.drake.bindings.bazel_workaround_4594_libdrake
except ImportError:
pass

Expand Down
2 changes: 1 addition & 1 deletion tools/install/libdrake/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ cc_library(
include_prefix = "drake",
strip_include_prefix = "/",
visibility = [
"//bindings/pydrake:__subpackages__",
"//bindings:__subpackages__",
"//examples:__subpackages__",
],
deps = [
Expand Down

0 comments on commit bf5dde8

Please sign in to comment.