forked from google-coral/libedgetpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkspace.bzl
160 lines (139 loc) · 4.7 KB
/
workspace.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
This module contains workspace definitions for building and using libedgetpu.
"""
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
# TF release 2.5.0 as of 05/17/2021.
TENSORFLOW_COMMIT = "a4dfb8d1a71385bd6d122e4f27f86dcebb96712d"
TENSORFLOW_SHA256 = "cb99f136dc5c89143669888a44bfdd134c086e1e2d9e36278c1eb0f03fe62d76"
CORAL_CROSSTOOL_COMMIT = "6bcc2261d9fc60dff386b557428d98917f0af491"
CORAL_CROSSTOOL_SHA256 = "38cb4da13009d07ebc2fed4a9d055b0f914191b344dd2d1ca5803096343958b4"
def libedgetpu_dependencies(
tensorflow_commit = TENSORFLOW_COMMIT,
tensorflow_sha256 = TENSORFLOW_SHA256,
coral_crosstool_commit = CORAL_CROSSTOOL_COMMIT,
coral_crosstool_sha256 = CORAL_CROSSTOOL_SHA256):
"""Sets up libedgetpu dependencies.
Args:
tensorflow_commit: https://github.com/tensorflow/tensorflow commit ID
tensorflow_sha256: corresponding sha256 of the source archive
coral_crosstool_commit: https://github.com/google-coral/crosstool commit ID
coral_crosstool_sha256: corresponding sha256 of the source archive
"""
maybe(
http_archive,
name = "org_tensorflow",
urls = [
"https://github.com/tensorflow/tensorflow/archive/" + tensorflow_commit + ".tar.gz",
],
sha256 = tensorflow_sha256,
strip_prefix = "tensorflow-" + tensorflow_commit,
)
maybe(
http_archive,
name = "coral_crosstool",
urls = [
"https://github.com/google-coral/crosstool/archive/" + coral_crosstool_commit + ".tar.gz",
],
sha256 = coral_crosstool_sha256,
strip_prefix = "crosstool-" + coral_crosstool_commit,
)
maybe(
libusb_repository,
name = "libusb",
)
# Use bazel to query values defined here, e.g.:
# bazel query "@libedgetpu_properties//..." | grep tensorflow_commit | cut -d\# -f2
_properties_repository(
name = "libedgetpu_properties",
properties = {
"tensorflow_commit": tensorflow_commit,
"coral_crosstool_commit": coral_crosstool_commit,
},
)
def _brew_libusb_path(ctx):
res = ctx.execute(["/bin/bash", "-c", "command -v brew"])
if res.return_code != 0:
return None
brew = ctx.path(res.stdout.strip())
res = ctx.execute([brew, "ls", "--versions", "libusb"])
if res.return_code != 0:
return None
res = ctx.execute([brew, "--prefix", "libusb"])
if res.return_code != 0:
return None
return res.stdout.strip()
def _port_libusb_path(ctx):
res = ctx.execute(["/bin/bash", "-c", "command -v port"])
if res.return_code != 0:
return None
port = ctx.path(res.stdout.strip())
res = ctx.execute([port, "info", "libusb"])
if res.return_code != 0:
return None
return port.dirname.dirname
def _libusb_impl(ctx):
lower_name = ctx.os.name.lower()
if lower_name.startswith("linux"):
path = None
build_file_content = """
cc_library(
name = "headers",
linkopts = ["-l:libusb-1.0.so"],
visibility = ["//visibility:public"],
)
"""
elif lower_name.startswith("windows"):
path = str(ctx.path(Label("@//:WORKSPACE"))) + "/../../libusb"
build_file_content = """
cc_library(
name = "headers",
includes = ["root/include"],
hdrs = ["root/include/libusb-1.0/libusb.h"],
visibility = ["//visibility:public"],
)
cc_import(
name = "shared",
interface_library = "root/VS2019/MS64/dll/libusb-1.0.lib",
shared_library = "root/VS2019/MS64/dll/libusb-1.0.dll",
visibility = ["//visibility:public"],
)
"""
elif lower_name.startswith("mac os x"):
path = _brew_libusb_path(ctx)
if not path:
path = _port_libusb_path(ctx)
if not path:
fail("Install libusb using MacPorts or Homebrew.")
build_file_content = """
cc_library(
name = "headers",
includes = ["root/include"],
hdrs = ["root/include/libusb-1.0/libusb.h"],
srcs = ["root/lib/libusb-1.0.dylib"],
visibility = ["//visibility:public"],
)
"""
else:
fail("Unsupported operating system.")
if path:
ctx.symlink(path, "root")
ctx.file(
"BUILD",
content = build_file_content,
executable = False,
)
libusb_repository = repository_rule(
implementation = _libusb_impl,
)
def _properties_impl(ctx):
content = ""
for name, value in ctx.attr.properties.items():
content += "filegroup(name='" + name + "#" + value + "')\n"
ctx.file("BUILD", content = content, executable = False)
_properties_repository = repository_rule(
implementation = _properties_impl,
attrs = {
"properties": attr.string_dict(),
},
)