Skip to content

Commit e533cd2

Browse files
numpy: Use custom-packaged version of NumPy
1 parent 84685fa commit e533cd2

File tree

7 files changed

+88
-39
lines changed

7 files changed

+88
-39
lines changed

bindings/pydrake/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ drake_pybind_library(
4242
package_info = PACKAGE_INFO,
4343
py_deps = [
4444
"//bindings/pydrake/util:deprecation_py",
45+
"@numpy_py",
4546
],
4647
py_srcs = [
4748
"__init__.py",

bindings/pydrake/third_party/BUILD.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ install(
7171
py_dest = PACKAGE_INFO.py_dest,
7272
docs = ["wrapt/LICENSE"],
7373
doc_dest = "share/doc",
74+
deps = [
75+
"@numpy_py//:install",
76+
],
7477
)
7578

7679
add_lint_tests()

tools/workspace/default.bzl

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ def add_default_repositories(excludes = [], mirrors = DEFAULT_MIRRORS):
143143
net_sf_jchart2d_repository(name = "net_sf_jchart2d", mirrors = mirrors)
144144
if "nlopt" not in excludes:
145145
nlopt_repository(name = "nlopt")
146-
if "numpy" not in excludes:
147-
numpy_repository(name = "numpy")
146+
if "numpy_py" not in excludes:
147+
numpy_repository(name = "numpy_py")
148148
if "octomap" not in excludes:
149149
octomap_repository(name = "octomap", mirrors = mirrors)
150150
if "optitrack_driver" not in excludes:

tools/workspace/numpy/BUILD.bazel

+7
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,12 @@
44
# neighboring *.bzl file can be loaded elsewhere.
55

66
load("//tools/lint:lint.bzl", "add_lint_tests")
7+
load("//tools/skylark:drake_py.bzl", "drake_py_test")
8+
9+
drake_py_test(
10+
name = "numpy_test",
11+
allow_import_unittest = True,
12+
deps = ["@numpy_py"],
13+
)
714

815
add_lint_tests()

tools/workspace/numpy/repository.bzl

+66-36
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,101 @@
22
# vi: set ft=python :
33

44
"""
5-
Finds local system NumPy headers and makes them available to be used as a
6-
C/C++ dependency.
5+
Provides NumPy from a wheel file.
76
87
Example:
98
WORKSPACE:
109
load("@drake//tools/workspace/numpy:repo.bzl", "numpy_repository")
1110
numpy_repository(
1211
name = "foo",
13-
python_version = "2",
1412
)
1513
1614
BUILD:
17-
cc_library(
15+
py_library(
1816
name = "foobar",
1917
deps = ["@foo//:numpy"],
20-
srcs = ["bar.cc"],
18+
srcs = ["bar.py"],
2119
)
2220
2321
Arguments:
2422
name: A unique name for this rule.
25-
python_version: The major or major.minor version of Python for which NumPy
26-
headers are to be found.
2723
"""
2824

29-
load("@drake//tools/workspace:execute.bzl", "which")
25+
load("@drake//tools/workspace:os.bzl", "determine_os")
26+
27+
# Generated from Git revision 7d247f4 from numpy/numpy#10898.
28+
# See `./packaging/README.md` for instructions to generate these binaries.
29+
# PR DRAFT(eric.cousineau): Upload these to S3 or when they pass review.
30+
wheels = {
31+
"ubuntu_16.04": {
32+
"url": "https://github.com/EricCousineau-TRI/experimental/raw/e84664659a7ceeac016f626b4d77f5a89c4cad62/numpy/numpy-1.15.0.dev0%2B7d247f4-cp27-cp27mu-linux_x86_64.whl", # noqa
33+
"sha256": "643f7e11c5f0213ae171d91e5759da51e4f39a0a9ba912f42189b3004507c21c", # noqa
34+
},
35+
"mac": {
36+
"url": "https://github.com/EricCousineau-TRI/experimental/raw/3211d6dd6aee9e8dd70a19ef86b961a2de3bf821/numpy/numpy-1.15.0.dev0%2B7d247f4-cp27-cp27m-macosx_10_13_x86_64.whl", # noqa
37+
"sha256": "1b3cee66ff92e2e0dc1c97d167b5b13873c3e97a87829a1c7830964ab48b2648", # noqa
38+
},
39+
}
3040

3141
def _impl(repository_ctx):
32-
python = which(repository_ctx, "python{}".format(
33-
repository_ctx.attr.python_version))
34-
35-
if not python:
36-
fail("Could NOT find python{}".format(repository_ctx.attr.version))
37-
38-
result = repository_ctx.execute([
39-
python,
40-
"-c",
41-
"; ".join([
42-
"from __future__ import print_function",
43-
"import numpy",
44-
"print(numpy.get_include())",
45-
]),
46-
])
47-
48-
if result.return_code != 0:
49-
fail("Could NOT determine NumPy include", attr = result.stderr)
50-
51-
source = repository_ctx.path(result.stdout.strip())
52-
destination = repository_ctx.path("include")
53-
repository_ctx.symlink(source, destination)
42+
# Do not name this `numpy`, as Bazel will confuse `PYTHONPATH`:
43+
# If `numpy` is located in the repo root, `random` will be shadowed by
44+
# `numpy.random`, causing everything to fail.
45+
# If installed elsewhere, Bazel will put an autogenerated `__init__.py` at
46+
# external directory's root, leak the wrong path, and shadow the real
47+
# `numpy`. See https://github.com/bazelbuild/bazel/issues/3998
48+
if repository_ctx.name == "numpy":
49+
fail("Do not name this repository `numpy`. Please name it " +
50+
"`numpy_py` or something else.")
51+
52+
os_result = determine_os(repository_ctx)
53+
if os_result.error != None:
54+
fail(os_result.error)
55+
56+
wheel = None
57+
if os_result.is_macos:
58+
wheel = wheels["mac"]
59+
elif os_result.is_ubuntu:
60+
key = "ubuntu_" + os_result.ubuntu_release
61+
wheel = wheels.get(key)
62+
if wheel == None:
63+
fail("Unsupported platform")
64+
65+
repository_ctx.download_and_extract(
66+
url = wheel["url"],
67+
sha256 = wheel["sha256"],
68+
type = "zip",
69+
)
5470

5571
file_content = """# -*- python -*-
5672
5773
# DO NOT EDIT: generated by numpy_repository()
5874
75+
load("@drake//tools/install:install.bzl", "install")
76+
5977
licenses([
6078
"notice", # BSD-2-Clause AND BSD-3-Clause AND MIT AND Python-2.0
6179
"unencumbered", # Public-Domain
6280
])
6381
64-
cc_library(
65-
name = "numpy",
66-
hdrs = glob(["include/**"]),
67-
includes = ["include"],
82+
# Interpret `numpy` sources as data to simplify handling.
83+
filegroup(
84+
name = "data",
85+
srcs = glob(["numpy/**/*"]),
86+
visibility = ["//visibility:private"],
87+
)
88+
89+
py_library(
90+
name = "numpy_py",
91+
imports = ["."],
92+
visibility = ["//visibility:public"],
93+
data = [":data"],
94+
)
95+
96+
install(
97+
name = "install",
98+
data = [":data"],
99+
data_dest = "lib/python2.7/site-packages",
68100
visibility = ["//visibility:public"],
69101
)
70102
"""
@@ -74,6 +106,4 @@ cc_library(
74106

75107
numpy_repository = repository_rule(
76108
_impl,
77-
attrs = {"python_version": attr.string(default = "2")},
78-
local = True,
79109
)
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""
2+
Tests that we can import NumPy, and have the version that we desire.
3+
"""
4+
5+
import numpy as np
6+
7+
assert ".runfiles" in np.__file__, (
8+
"You must use the Bazel-specified `numpy`! Please ensure your WORKSPACE "
9+
"and usage of `numpy_repository` are well-formed.")

tools/workspace/pybind11/package.BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ cc_library(
4444
includes = ["include"],
4545
deps = [
4646
"@eigen",
47-
"@numpy",
4847
"@python",
4948
],
5049
)

0 commit comments

Comments
 (0)