Skip to content

Commit

Permalink
cpp_const: Add ability to impose C++ const semantics in Python.
Browse files Browse the repository at this point in the history
  • Loading branch information
EricCousineau-TRI committed Jan 29, 2018
1 parent 8aa72db commit 5bfad74
Show file tree
Hide file tree
Showing 7 changed files with 556 additions and 0 deletions.
1 change: 1 addition & 0 deletions bindings/pydrake/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ PYBIND_LIBRARIES = adjust_labels_for_drake_hoist([
"//drake/bindings/pydrake/examples",
"//drake/bindings/pydrake/solvers",
"//drake/bindings/pydrake/systems",
"//drake/bindings/pydrake/third_party",
"//drake/bindings/pydrake/util",
])

Expand Down
76 changes: 76 additions & 0 deletions bindings/pydrake/third_party/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
load("@drake//tools/install:install.bzl", "install")
load("//tools/lint:lint.bzl", "add_lint_tests")
load(
"//tools/skylark:drake_py.bzl",
"drake_py_library",
)
load(
"//tools/skylark:pybind.bzl",
"get_pybind_package_info",
)
load(":forward_files.bzl", "forward_files")

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

# This determines how `PYTHONPATH` is configured, and how to install the
# bindings.
PACKAGE_INFO = get_pybind_package_info(
base_package = "//bindings",
)

drake_py_library(
name = "module_py",
srcs = ["__init__.py"],
imports = PACKAGE_INFO.py_imports,
)

# Forward physical files to simplify working with `third_party` files (both
# for Python imports and for installation).
wrapt_prefix = "//third_party:com_github_grahamdumpleton_wrapt/"

forward_files(
srcs = [wrapt_prefix + f for f in [
"__init__.py",
"LICENSE",
"wrappers.py",
]],
dest_prefix = "wrapt/",
strip_prefix = wrapt_prefix,
tags = ["nolint"],
visibility = ["//visibility:private"],
)

# TODO(eric.cousineau): Use Ubuntu packages once we have a later version of
# `python-wrapt` that does not have bugs like a mis-mapped `__mod__` proxy.
drake_py_library(
name = "wrapt_py",
srcs = [
"wrapt/__init__.py",
"wrapt/wrappers.py",
],
imports = PACKAGE_INFO.py_imports,
tags = ["nolint"],
)

PY_LIBRARIES = [
":module_py",
":wrapt_py",
]

drake_py_library(
name = "third_party",
visibility = ["//visibility:public"],
deps = PY_LIBRARIES,
)

install(
name = "install",
targets = PY_LIBRARIES,
py_dest = PACKAGE_INFO.py_dest,
docs = ["wrapt/LICENSE"],
doc_dest = "share/doc",
)

add_lint_tests()
1 change: 1 addition & 0 deletions bindings/pydrake/third_party/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Empty Python module.
35 changes: 35 additions & 0 deletions bindings/pydrake/third_party/forward_files.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- python -*-

def forward_files(
srcs,
strip_prefix,
dest_prefix,
visibility = None,
tags = []):
"""Forwards files in `srcs` to be physically present in the current
packages.
Present implementation simply copies the files.
@param srcs
List of string, pointing *directly* to files. Does not handle filegroup
targets.
@param strip_prefix
String to be stripped from source files. Should include trailing slash.
@param dest_prefix
String to be prepended to target.
"""
outs = []
for src in srcs:
if not src.startswith(strip_prefix):
fail("'{}' not under '{}'".format(src, strip_prefix))
out = dest_prefix + src[len(strip_prefix):]
native.genrule(
name = out + ".forward",
srcs = [src],
outs = [out],
cmd = "cp $< $@",
tags = tags,
visibility = visibility,
)
outs.append(out)
17 changes: 17 additions & 0 deletions bindings/pydrake/util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ drake_cc_library(
],
)

drake_py_library(
name = "cpp_const_py",
srcs = ["cpp_const.py"],
deps = [
":module_py",
"//bindings/pydrake/third_party:wrapt_py",
],
)

drake_cc_library(
name = "drake_optional_pybind",
hdrs = ["drake_optional_pybind.h"],
Expand All @@ -106,6 +115,7 @@ drake_cc_library(
PYBIND_LIBRARIES = []

PY_LIBRARIES = [
":cpp_const_py",
":cpp_param_py",
":cpp_template_py",
":module_py",
Expand Down Expand Up @@ -160,6 +170,13 @@ drake_py_test(
],
)

drake_py_test(
name = "cpp_const_test",
deps = [
":cpp_const_py",
],
)

drake_py_test(
name = "cpp_param_test",
deps = [
Expand Down
Loading

0 comments on commit 5bfad74

Please sign in to comment.