|
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 |
| - load("@drake//tools/workspace/numpy:repo.bzl", "numpy_repository") |
11 |
| - numpy_repository( |
| 9 | + load("@drake//tools/workspace/numpy:repo.bzl", "numpy_py_repository") |
| 10 | + numpy_py_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 |
| - deps = ["@foo//:numpy"], |
20 |
| - srcs = ["bar.cc"], |
| 17 | + deps = ["@foo//:numpy_py"], |
| 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") |
30 | 26 |
|
31 |
| -def _impl(repository_ctx): |
32 |
| - python = which(repository_ctx, "python{}".format( |
33 |
| - repository_ctx.attr.python_version, |
34 |
| - )) |
35 |
| - |
36 |
| - if not python: |
37 |
| - fail("Could NOT find python{}".format(repository_ctx.attr.version)) |
38 |
| - |
39 |
| - result = repository_ctx.execute([ |
40 |
| - python, |
41 |
| - "-c", |
42 |
| - "; ".join([ |
43 |
| - "from __future__ import print_function", |
44 |
| - "import numpy", |
45 |
| - "print(numpy.get_include())", |
46 |
| - ]), |
47 |
| - ]) |
48 |
| - |
49 |
| - if result.return_code != 0: |
50 |
| - fail("Could NOT determine NumPy include", attr = result.stderr) |
51 |
| - |
52 |
| - source = repository_ctx.path(result.stdout.strip()) |
53 |
| - destination = repository_ctx.path("include") |
54 |
| - repository_ctx.symlink(source, destination) |
55 |
| - |
56 |
| - file_content = """# -*- python -*- |
| 27 | +# See: https://pypi.org/project/numpy/#files |
| 28 | +wheels = { |
| 29 | + "ubuntu_16.04": { |
| 30 | + "url": "https://files.pythonhosted.org/packages/40/c5/f1ed15dd931d6667b40f1ab1c2fe1f26805fc2b6c3e25e45664f838de9d0/numpy-1.15.2-cp27-cp27mu-manylinux1_x86_64.whl", # noqa |
| 31 | + "sha256": "82f00a1e2695a0e5b89879aa25ea614530b8ebdca6d49d4834843d498e8a5e92", # noqa |
| 32 | + }, |
| 33 | + "mac": { |
| 34 | + "url": "https://files.pythonhosted.org/packages/d6/47/447d4e08e18c4f0e7f935db24d8afcfc9026a84002c0e5d85103c14baaf1/numpy-1.15.2-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl", # noqa |
| 35 | + "sha256": "b5ff7dae352fd9e1edddad1348698e9fea14064460a7e39121ef9526745802e6", # noqa |
| 36 | + }, |
| 37 | +} |
57 | 38 |
|
58 |
| -# DO NOT EDIT: generated by numpy_repository() |
59 |
| -
|
60 |
| -licenses([ |
61 |
| - "notice", # BSD-2-Clause AND BSD-3-Clause AND MIT AND Python-2.0 |
62 |
| - "unencumbered", # Public-Domain |
63 |
| -]) |
64 |
| -
|
65 |
| -cc_library( |
66 |
| - name = "numpy", |
67 |
| - hdrs = glob(["include/**"]), |
68 |
| - includes = ["include"], |
69 |
| - visibility = ["//visibility:public"], |
70 |
| -) |
71 |
| - """ |
72 |
| - |
73 |
| - repository_ctx.file( |
| 39 | +def _impl(repository_ctx): |
| 40 | + # Do not name this `numpy`, as Bazel will confuse `PYTHONPATH`: |
| 41 | + # If `numpy` is located in the repo root, `random` will be shadowed by |
| 42 | + # `numpy.random`, causing everything to fail. |
| 43 | + # If installed elsewhere, Bazel will put an autogenerated `__init__.py` at |
| 44 | + # external directory's root, leak the wrong path, and shadow the real |
| 45 | + # `numpy`. See https://github.com/bazelbuild/bazel/issues/3998 |
| 46 | + if repository_ctx.name == "numpy": |
| 47 | + fail("Do not name this repository `numpy`. Please name it " + |
| 48 | + "`numpy_py` or something else.") |
| 49 | + |
| 50 | + os_result = determine_os(repository_ctx) |
| 51 | + if os_result.error != None: |
| 52 | + fail(os_result.error) |
| 53 | + |
| 54 | + wheel = None |
| 55 | + if os_result.is_macos: |
| 56 | + wheel = wheels["mac"] |
| 57 | + elif os_result.is_ubuntu: |
| 58 | + key = "ubuntu_" + os_result.ubuntu_release |
| 59 | + wheel = wheels.get(key) |
| 60 | + if wheel == None: |
| 61 | + fail("Unsupported platform") |
| 62 | + |
| 63 | + repository_ctx.download_and_extract( |
| 64 | + url = wheel["url"], |
| 65 | + sha256 = wheel["sha256"], |
| 66 | + type = "zip", |
| 67 | + ) |
| 68 | + repository_ctx.symlink( |
| 69 | + Label("@drake//tools/workspace/numpy:package.BUILD.bazel"), |
74 | 70 | "BUILD.bazel",
|
75 |
| - content = file_content, |
76 |
| - executable = False, |
77 | 71 | )
|
78 | 72 |
|
79 |
| -numpy_repository = repository_rule( |
| 73 | +numpy_py_repository = repository_rule( |
80 | 74 | _impl,
|
81 |
| - attrs = {"python_version": attr.string(default = "2")}, |
82 |
| - local = True, |
83 | 75 | )
|
0 commit comments