-
Notifications
You must be signed in to change notification settings - Fork 12
/
buildtools.bzl
177 lines (153 loc) · 6.21 KB
/
buildtools.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
Setup code for setting up binaries for use
"""
load("@bazel_skylib//lib:new_sets.bzl", "sets")
load("@bazel_skylib//lib:types.bzl", "types")
_TOOL_NAMES = ["buildifier", "buildozer"]
_TYPICAL_PLATFORMS = ["windows", "darwin", "linux"]
_TYPICAL_ARCHES = ["amd64", "arm64"]
_VALID_TOOL_NAMES = sets.make(_TOOL_NAMES)
def _create_asset(name, platform, arch, version, sha256 = None):
"""Create a `struct` representing a buildtools asset.
Args:
name: The name of the asset (e.g. `buildifier`, `buildozer`) as a
`string`.
platform: The platform as a `string`. (e.g. `linux`, `darwin`)
arch: The arch as a `string`. (e.g. `amd64`, `arm64`)
version: The version as a `string`. (e.g. `4.2.3`)
sha256: Optional. The sha256 as a `string`.
Returns:
A `struct` representing the asset to be downloaded.
"""
if name == None:
fail("Expected a name.")
if platform == None:
fail("Expected a platform.")
if arch == None:
fail("Expected an arch.")
if version == None:
fail("Expected a version.")
if arch == "windows" and version == "arm64":
fail("arm64 windows executables are not provided by buildifier/buildozer")
if not sets.contains(_VALID_TOOL_NAMES, name):
fail("Invalid asset name. {name}".format(name = name))
return struct(
name = name,
platform = platform,
arch = arch,
version = version,
sha256 = sha256,
)
def _create_unique_name(asset = None, name = None, platform = None, arch = None):
"""Create a unique name from an asset or from a name/platform/arch.
Args:
asset: An asset `struct` as returned by `buildtools.create_asset`.
name: A tool name (e.g. buildifier) as `string`.
platform: A platform as `string`.
arch: An arch as `string`.
Returns:
A `string` suitable for use as identifying an asset.
"""
if asset != None:
name = asset.name
platform = asset.platform
arch = asset.arch
if name == None or platform == None or arch == None:
fail("An asset or name/platform/arch must be specified.")
return "{name}_{platform}_{arch}".format(
name = name,
platform = platform,
arch = arch,
)
def _asset_to_json(asset):
"""Returns the JSON representation for an asset `struct` or a `list` of asset `struct` values.
Args:
asset: An asset `struct` as returned by `buildtools.create_asset`.
Returns:
Returns a JSON `string` representation of the provided value.
"""
return json.encode(asset)
def _asset_from_json(json_str):
"""Returns an asset `struct` or a `list` of asset `struct` values as represented by the JSON `string`.
Args:
json_str: A JSON `string` representing an asset or a list of assets.
Returns:
An asset `struct` or a `list` of asset `struct` values.
"""
result = json.decode(json_str)
if types.is_list(result):
return [_create_asset(**a) for a in result]
elif types.is_dict(result):
return _create_asset(**result)
fail("Unexpected result type decoding JSON string. %s" % (json_str))
def _create_assets(
version,
names = _TOOL_NAMES,
platforms = _TYPICAL_PLATFORMS,
arches = _TYPICAL_ARCHES,
sha256_values = {}):
"""Create a `list` of asset `struct` values.
Args:
version: The buildtools version string.
names: Optional. A `list` of tools to include.
platforms: Optional. A `list` of platforms to include.
arches: Optional. A `list` of arches to include.
sha256_values: Optional. A `dict` of asset name to sha256.
Returns:
A `list` of buildtools assets.
"""
if version == None:
fail("Expected a version.")
if names == None or names == []:
fail("Expected a non-empty list for names.")
if platforms == None or platforms == []:
fail("Expected a non-empty list for platforms.")
if arches == None or arches == []:
fail("Expected a non-empty list for arches.")
if sha256_values == None:
sha256_values = {}
assets = []
for name in names:
for platform in platforms:
for arch in arches:
if platform == "windows" and arch == "arm64":
continue
uniq_name = _create_unique_name(
name = name,
platform = platform,
arch = arch,
)
assets.append(_create_asset(
name = name,
platform = platform,
arch = arch,
version = version,
sha256 = sha256_values.get(uniq_name),
))
return assets
_DEFAULT_ASSETS = _create_assets(
version = "v7.3.1",
names = ["buildifier", "buildozer"],
platforms = ["darwin", "linux", "windows"],
arches = ["amd64", "arm64"],
sha256_values = {
"buildifier_darwin_amd64": "375f823103d01620aaec20a0c29c6cbca99f4fd0725ae30b93655c6704f44d71",
"buildifier_darwin_arm64": "5a6afc6ac7a09f5455ba0b89bd99d5ae23b4174dc5dc9d6c0ed5ce8caac3f813",
"buildifier_linux_amd64": "5474cc5128a74e806783d54081f581662c4be8ae65022f557e9281ed5dc88009",
"buildifier_linux_arm64": "0bf86c4bfffaf4f08eed77bde5b2082e4ae5039a11e2e8b03984c173c34a561c",
"buildifier_windows_amd64": "370cd576075ad29930a82f5de132f1a1de4084c784a82514bd4da80c85acf4a8",
"buildozer_darwin_amd64": "854c9583efc166602276802658cef3f224d60898cfaa60630b33d328db3b0de2",
"buildozer_darwin_arm64": "31b1bfe20d7d5444be217af78f94c5c43799cdf847c6ce69794b7bf3319c5364",
"buildozer_linux_amd64": "3305e287b3fcc68b9a35fd8515ee617452cd4e018f9e6886b6c7cdbcba8710d4",
"buildozer_linux_arm64": "0b5a2a717ac4fc911e1fec8d92af71dbb4fe95b10e5213da0cc3d56cea64a328",
"buildozer_windows_amd64": "58d41ce53257c5594c9bc86d769f580909269f68de114297f46284fbb9023dcf",
},
)
buildtools = struct(
create_asset = _create_asset,
create_unique_name = _create_unique_name,
asset_to_json = _asset_to_json,
asset_from_json = _asset_from_json,
create_assets = _create_assets,
DEFAULT_ASSETS = _DEFAULT_ASSETS,
)