Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpp_const: Add ability to impose C++ const semantics in Python. #7884

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions bindings/pydrake/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ load(
load(
"//tools/skylark:pybind.bzl",
"drake_pybind_library",
"get_drake_pybind_installs",
"get_drake_py_installs",
"get_pybind_package_info",
)
load(
Expand Down Expand Up @@ -126,7 +126,7 @@ alias(
actual = "//bindings/pydrake/util",
)

PYBIND_LIBRARIES = adjust_labels_for_drake_hoist([
PY_LIBRARIES_WITH_INSTALL = adjust_labels_for_drake_hoist([
":autodiffutils_py",
":common_py",
":parsers_py",
Expand All @@ -136,6 +136,7 @@ PYBIND_LIBRARIES = adjust_labels_for_drake_hoist([
"//drake/bindings/pydrake/multibody",
"//drake/bindings/pydrake/solvers",
"//drake/bindings/pydrake/systems",
"//drake/bindings/pydrake/third_party",
"//drake/bindings/pydrake/util",
])

Expand All @@ -146,13 +147,13 @@ install(
targets = PY_LIBRARIES,
py_dest = PACKAGE_INFO.py_dest,
visibility = ["//visibility:public"],
deps = get_drake_pybind_installs(PYBIND_LIBRARIES),
deps = get_drake_py_installs(PY_LIBRARIES_WITH_INSTALL),
)

drake_py_library(
name = "pydrake",
visibility = ["//visibility:public"],
deps = PYBIND_LIBRARIES + PY_LIBRARIES,
deps = PY_LIBRARIES_WITH_INSTALL + PY_LIBRARIES,
)

# Test ODR (One Definition Rule).
Expand Down
8 changes: 4 additions & 4 deletions bindings/pydrake/examples/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load("//tools/lint:lint.bzl", "add_lint_tests")
load(
"//tools/skylark:pybind.bzl",
"drake_pybind_library",
"get_drake_pybind_installs",
"get_drake_py_installs",
"get_pybind_package_info",
)
load(
Expand Down Expand Up @@ -56,7 +56,7 @@ drake_pybind_library(
],
)

PYBIND_LIBRARIES = [
PY_LIBRARIES_WITH_INSTALL = [
":pendulum_py",
]

Expand All @@ -67,14 +67,14 @@ PY_LIBRARIES = [
drake_py_library(
name = "examples",
imports = PACKAGE_INFO.py_imports,
deps = PYBIND_LIBRARIES + PY_LIBRARIES,
deps = PY_LIBRARIES_WITH_INSTALL + PY_LIBRARIES,
)

install(
name = "install",
targets = PY_LIBRARIES,
py_dest = PACKAGE_INFO.py_dest,
deps = get_drake_pybind_installs(PYBIND_LIBRARIES),
deps = get_drake_py_installs(PY_LIBRARIES_WITH_INSTALL),
)

drake_py_test(
Expand Down
8 changes: 4 additions & 4 deletions bindings/pydrake/multibody/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load("//tools/lint:lint.bzl", "add_lint_tests")
load(
"//tools/skylark:pybind.bzl",
"drake_pybind_library",
"get_drake_pybind_installs",
"get_drake_py_installs",
"get_pybind_package_info",
)
load(
Expand Down Expand Up @@ -56,7 +56,7 @@ drake_py_library(
],
)

PYBIND_LIBRARIES = [
PY_LIBRARIES_WITH_INSTALL = [
":rigid_body_plant_py",
]

Expand All @@ -68,14 +68,14 @@ PY_LIBRARIES = [
drake_py_library(
name = "multibody",
imports = PACKAGE_INFO.py_imports,
deps = PYBIND_LIBRARIES + PY_LIBRARIES,
deps = PY_LIBRARIES_WITH_INSTALL + PY_LIBRARIES,
)

install(
name = "install",
targets = PY_LIBRARIES,
py_dest = PACKAGE_INFO.py_dest,
deps = get_drake_pybind_installs(PYBIND_LIBRARIES),
deps = get_drake_py_installs(PY_LIBRARIES_WITH_INSTALL),
)

drake_py_test(
Expand Down
8 changes: 4 additions & 4 deletions bindings/pydrake/solvers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ load("//tools/skylark:test_tags.bzl", "gurobi_test_tags", "mosek_test_tags")
load(
"//tools/skylark:pybind.bzl",
"drake_pybind_library",
"get_drake_pybind_installs",
"get_drake_py_installs",
"get_pybind_package_info",
)
load(
Expand Down Expand Up @@ -89,7 +89,7 @@ drake_pybind_library(
py_deps = [":mathematicalprogram_py"],
)

PYBIND_LIBRARIES = [
PY_LIBRARIES_WITH_INSTALL = [
":gurobi_py",
":ik_py",
":ipopt_py",
Expand All @@ -103,14 +103,14 @@ PY_LIBRARIES = [

drake_py_library(
name = "solvers",
deps = PYBIND_LIBRARIES + PY_LIBRARIES,
deps = PY_LIBRARIES_WITH_INSTALL + PY_LIBRARIES,
)

install(
name = "install",
targets = PY_LIBRARIES,
py_dest = PACKAGE_INFO.py_dest,
deps = get_drake_pybind_installs(PYBIND_LIBRARIES),
deps = get_drake_py_installs(PY_LIBRARIES_WITH_INSTALL),
)

drake_py_test(
Expand Down
8 changes: 4 additions & 4 deletions bindings/pydrake/systems/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load("//tools/lint:lint.bzl", "add_lint_tests")
load(
"//tools/skylark:pybind.bzl",
"drake_pybind_library",
"get_drake_pybind_installs",
"get_drake_py_installs",
"get_pybind_package_info",
)
load(
Expand Down Expand Up @@ -111,7 +111,7 @@ drake_py_library(
],
)

PYBIND_LIBRARIES = [
PY_LIBRARIES_WITH_INSTALL = [
":analysis_py",
":framework_py",
":primitives_py",
Expand All @@ -127,14 +127,14 @@ PY_LIBRARIES = [
drake_py_library(
name = "systems",
imports = PACKAGE_INFO.py_imports,
deps = PYBIND_LIBRARIES + PY_LIBRARIES,
deps = PY_LIBRARIES_WITH_INSTALL + PY_LIBRARIES,
)

install(
name = "install",
targets = PY_LIBRARIES,
py_dest = PACKAGE_INFO.py_dest,
deps = get_drake_pybind_installs(PYBIND_LIBRARIES),
deps = get_drake_py_installs(PY_LIBRARIES_WITH_INSTALL),
)

drake_py_test(
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.
38 changes: 38 additions & 0 deletions bindings/pydrake/third_party/forward_files.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- python -*-

def forward_files(
srcs = [],
strip_prefix = None,
dest_prefix = None,
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 as absolute Bazel target
paths. This does NOT resolve relative targets, nor does it unpack
filegroup targets `$(locations ...)`.
@param strip_prefix
String to be stripped from source files. Should include trailing slash.
@param dest_prefix
String to be prepended to target.
"""
if strip_prefix == None or dest_prefix == None:
fail("Must supply `strip_prefix` and `dest_prefix`.")
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)
25 changes: 21 additions & 4 deletions bindings/pydrake/util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ load(
load(
"//tools/skylark:pybind.bzl",
"drake_pybind_cc_googletest",
"get_drake_pybind_installs",
"get_drake_py_installs",
"get_pybind_package_info",
)
load(
Expand Down Expand Up @@ -98,14 +98,24 @@ 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"],
)

PYBIND_LIBRARIES = []
PY_LIBRARIES_WITH_INSTALL = []

PY_LIBRARIES = [
":cpp_const_py",
":cpp_param_py",
":cpp_template_py",
":module_py",
Expand All @@ -116,14 +126,14 @@ PY_LIBRARIES = [
drake_py_library(
name = "util",
imports = PACKAGE_INFO.py_imports,
deps = PYBIND_LIBRARIES + PY_LIBRARIES,
deps = PY_LIBRARIES_WITH_INSTALL + PY_LIBRARIES,
)

install(
name = "install",
targets = PY_LIBRARIES,
py_dest = PACKAGE_INFO.py_dest,
deps = get_drake_pybind_installs(PYBIND_LIBRARIES),
deps = get_drake_py_installs(PY_LIBRARIES_WITH_INSTALL),
)

drake_cc_googletest(
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