2
2
# vi: set ft=python :
3
3
4
4
"""
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.
7
6
8
7
Example:
9
8
WORKSPACE:
10
9
load("@drake//tools/workspace/numpy:repo.bzl", "numpy_repository")
11
10
numpy_repository(
12
11
name = "foo",
13
- python_version = "2",
14
12
)
15
13
16
14
BUILD:
17
- cc_library (
15
+ py_library (
18
16
name = "foobar",
19
17
deps = ["@foo//:numpy"],
20
- srcs = ["bar.cc "],
18
+ srcs = ["bar.py "],
21
19
)
22
20
23
21
Arguments:
24
22
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.
27
23
"""
28
24
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
+ }
30
40
31
41
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
+ )
54
70
55
71
file_content = """# -*- python -*-
56
72
57
73
# DO NOT EDIT: generated by numpy_repository()
58
74
75
+ load("@drake//tools/install:install.bzl", "install")
76
+
59
77
licenses([
60
78
"notice", # BSD-2-Clause AND BSD-3-Clause AND MIT AND Python-2.0
61
79
"unencumbered", # Public-Domain
62
80
])
63
81
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",
68
100
visibility = ["//visibility:public"],
69
101
)
70
102
"""
@@ -74,6 +106,4 @@ cc_library(
74
106
75
107
numpy_repository = repository_rule (
76
108
_impl ,
77
- attrs = {"python_version" : attr .string (default = "2" )},
78
- local = True ,
79
109
)
0 commit comments