forked from digital-asset/daml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaskell.bzl
300 lines (271 loc) · 8.91 KB
/
haskell.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
load(
"@rules_haskell//haskell:defs.bzl",
"haskell_binary",
"haskell_library",
"haskell_repl",
"haskell_test",
)
load(
"@rules_haskell//haskell:c2hs.bzl",
"c2hs_library",
)
load("//bazel_tools:hlint.bzl", "haskell_hlint")
load("@os_info//:os_info.bzl", "is_windows")
# This file defines common Haskell language extensions and compiler flags used
# throughout this repository. The initial set of flags is taken from the
# daml-foundations project. If you find that additional flags are required for
# another project, consider whether all projects could benefit from these
# changes. If so, add them here.
#
# Use the macros `da_haskell_*` defined in this file, instead of stock rules
# `haskell_*` from `rules_haskell` in order for these default flags to take
# effect.
common_haskell_exts = [
"BangPatterns",
"DeriveDataTypeable",
"DeriveFoldable",
"DeriveFunctor",
"DeriveGeneric",
"DeriveTraversable",
"FlexibleContexts",
"GeneralizedNewtypeDeriving",
"LambdaCase",
"NamedFieldPuns",
"NumericUnderscores",
"OverloadedStrings",
"PackageImports",
"RecordWildCards",
"ScopedTypeVariables",
"StandaloneDeriving",
"TupleSections",
"TypeApplications",
"ViewPatterns",
]
common_haskell_flags = [
"-Wall",
"-Werror",
"-Wincomplete-uni-patterns",
"-Wno-name-shadowing",
"-fno-omit-yields",
"-fno-ignore-asserts",
"-threaded",
"-rtsopts",
# run on two cores, disable idle & parallel GC
"-with-rtsopts=-N2 -qg -I0",
]
def _wrap_rule(rule, name = "", deps = [], hackage_deps = [], compiler_flags = [], **kwargs):
ext_flags = ["-X%s" % ext for ext in common_haskell_exts]
stackage_libs = ["@stackage//:{}".format(dep) for dep in hackage_deps]
rule(
name = name,
compiler_flags = ext_flags + common_haskell_flags + compiler_flags,
deps = stackage_libs + deps,
**kwargs
)
# We don't fetch HLint on Windows
if not is_windows:
haskell_hlint(
name = name + "@hlint",
deps = [name],
testonly = True,
)
# Load Main module on executable rules.
repl_ghci_commands = []
if "main_function" in kwargs:
main_module = ".".join(kwargs["main_function"].split(".")[:-1])
repl_ghci_commands = [
":m " + main_module,
]
da_haskell_repl(
name = name + "@ghci",
deps = [name],
repl_ghci_commands = repl_ghci_commands,
testonly = kwargs.get("testonly", False),
visibility = ["//visibility:public"],
)
def da_haskell_library(**kwargs):
"""
Define a Haskell library.
Allows to define Hackage dependencies using `hackage_deps`,
applies common Haskell options defined in `bazel_tools/haskell.bzl`
and forwards to `haskell_library` from `rules_haskell`.
Refer to the [`rules_haskell` documentation][rules_haskell_docs].
[rules_haskell_docs]: https://api.haskell.build/
Example:
```
da_haskell_library(
name = "example",
src_strip_prefix = "src",
srcs = glob(["src/**/*.hs"]),
hackage_deps = [
"base",
"text",
],
deps = [
"//some/package:target",
],
visibility = ["//visibility:public"],
)
```
"""
_wrap_rule(haskell_library, **kwargs)
def da_haskell_binary(main_function = "Main.main", **kwargs):
"""
Define a Haskell executable.
Allows to define Hackage dependencies using `hackage_deps`,
applies common Haskell options defined in `bazel_tools/haskell.bzl`
and forwards to `haskell_binary` from `rules_haskell`.
Refer to the [`rules_haskell` documentation][rules_haskell_docs].
[rules_haskell_docs]: https://api.haskell.build/
Example:
```
da_haskell_binary(
name = "example",
src_strip_prefix = "src",
srcs = glob(["src/**/*.hs"]),
hackage_deps = [
"base",
"text",
],
deps = [
"//some/package:target",
],
visibility = ["//visibility:public"],
)
```
"""
_wrap_rule(
haskell_binary,
# Make this argument explicit, so we can distinguish library and
# executable rules in _wrap_rule.
main_function = main_function,
**kwargs
)
def da_haskell_test(main_function = "Main.main", testonly = True, **kwargs):
"""
Define a Haskell test suite.
Allows to define Hackage dependencies using `hackage_deps`,
applies common Haskell options defined in `bazel_tools/haskell.bzl`
and forwards to `haskell_test` from `rules_haskell`.
Refer to the [`rules_haskell` documentation][rules_haskell_docs].
[rules_haskell_docs]: https://api.haskell.build/
Example:
```
da_haskell_test(
name = "example",
src_strip_prefix = "src",
srcs = glob(["src/**/*.hs"]),
hackage_deps = [
"base",
"text",
],
deps = [
"//some/package:target",
],
visibility = ["//visibility:public"],
)
```
"""
_wrap_rule(
haskell_test,
# Make this argument explicit, so we can distinguish library and
# executable rules in _wrap_rule.
main_function = main_function,
# Make this argument explicit, so we can distinguish test targets
# in _wrap_rule.
testonly = testonly,
**kwargs
)
def da_haskell_repl(**kwargs):
"""
Define a Haskell repl.
Applies common Haskell options defined in `bazel_tools/haskell.bzl`
and forwards to `haskell_repl` from `rules_haskell`.
Refer to the [`rules_haskell` documentation][rules_haskell_docs].
[rules_haskell_docs]: https://api.haskell.build/
Example:
```
da_haskell_repl(
name = "repl",
# Load these packages and their dependencies by source.
# Will only load packages from the local workspace by source.
deps = [
"//some/package:target_a",
"//some/package:target_b",
],
# (optional) only load targets matching these patterns by source.
experimental_from_source = [
"//some/package/...",
"//some/other/package/...",
],
# (optional) don't load targets matching these patterns by source.
experimental_from_binary = [
"//some/vendored/package/...",
],
# (optional) Pass extra arguments to ghci.
repl_ghci_args = [
"-fexternal-interpreter",
],
# (optional) Make runfiles available to the REPL, or not.
collect_data = True,
)
```
"""
# Set default arguments
with_defaults = dict(
# Whether to make runfiles, such as the daml stdlib, available to the
# REPL. Pass --define ghci_data=True to enable.
collect_data = select({
"//:ghci_data": True,
"//conditions:default": False,
}),
experimental_from_binary = ["//nix/..."],
repl_ghci_args = [
"-fexternal-interpreter",
"-j",
"+RTS",
"-I0",
"-n2m",
"-A128m",
"-qb0",
"-RTS",
],
)
with_defaults.update(kwargs)
# The common_haskell_exts and common_haskell_flags are already passed on
# the library/binary/test rule wrappers. haskell_repl will pick these up
# automatically, if such targets are loaded by source.
# Right now we don't set any default flags.
haskell_repl(**with_defaults)
def _sanitize_string_for_usage(s):
res_array = []
for idx in range(len(s)):
c = s[idx]
if c.isalnum() or c == ".":
res_array.append(c)
else:
res_array.append("_")
return "".join(res_array)
def c2hs_suite(name, hackage_deps, deps = [], srcs = [], c2hs_srcs = [], c2hs_src_strip_prefix = "", **kwargs):
ts = []
for file in c2hs_srcs:
n = _sanitize_string_for_usage(file)
c2hs_library(
name = n,
srcs = [file],
deps = deps + [":" + t for t in ts],
src_strip_prefix = c2hs_src_strip_prefix,
# language-c fails to pass mingw’s intrinsic-impl.h header if
# we do not unset this option.
extra_args = ["-C-U__GCC_ASM_FLAG_OUTPUTS__"] if is_windows else [],
)
ts.append(n)
da_haskell_library(
name = name,
srcs = [":" + t for t in ts] + srcs,
deps = deps,
hackage_deps = hackage_deps,
**kwargs
)