-
Notifications
You must be signed in to change notification settings - Fork 81
/
start
executable file
·575 lines (459 loc) · 15.4 KB
/
start
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
#!/usr/bin/env sh
#
# Checks the version of Bazel found in the PATH, and then initializes
# a new Bazel workspace with dummy Haskell build targets.
set -eu
# If the environment variable `GHC_VERSION` is not set yet,
# we use the default version (currently "9.4.6").
GHC_VERSION=${GHC_VERSION:="9.4.6"}
NIXPKGS_REVISION=${NIXPKGS_REVISION:-nixos-24.05}
readonly MIN_BAZEL_MAJOR=6
readonly MIN_BAZEL_MINOR=0
readonly MAX_BAZEL_MAJOR=6
readonly MAX_BAZEL_MINOR=5
stderr () {
>&2 echo "$*"
}
usage () {
exit_code="$1"
cat >&2 <<-"EOF"
start [--use-bindists|--use-nix|--help|--with-bzlmod=true|false]
Set up a minimal rules_haskell bazel configuration.
--use-bindists: The project is set up to provision GHC from binary distributions. This does not require nix to build.
--use-nix: The project is set up to provision GHC from nixpkgs. This requires nix to build.
--with-bzlmod=true|false:
If enabled, use a MODULE.bazel file and enable bzlmod to fetch rules_haskell.
(only supported in bindist mode)
If no argument is given, `--use-bindists` is assumed
and a helpful message is printed that `--use-nix` also exists.
For more information visit https://haskell.build/
EOF
exit "${exit_code}"
}
# either bindists or nix
MODE="bindists"
PRINT_NIX_USAGE=
BZLMOD=false
parse_args () {
# Defaults, if no arguments provided
if [ "$#" -eq 0 ]; then
PRINT_NIX_USAGE=1
return
fi
for arg; do
case "$arg" in
"--help") usage 0 ;;
"--use-bindists") MODE="bindists" ;;
"--use-nix") MODE="nix" ;;
"--with-bzlmod=true") BZLMOD=true ;;
"--with-bzlmod=false") BZLMOD=false ;;
*) usage 1 ;;
esac
done
if $BZLMOD && [ $MODE != bindists ]; then
stderr "error: --with-bzlmod is only supported with --use-bindists"
exit 1
fi
}
check_dir () {
path="$1"
# we can't write a file when a directory of that name exists
if [ -d "${path}" ]; then
stderr "STOP: There's a directory named ${path} but we want to write a file with the same name. Please delete or rename the ${path} directory."
exit 1
fi
}
check_alt () {
path="$1"
alternative="$2"
check_dir "${alternative}"
if [ -f "${path}" ]; then
stderr "STOP: There's a ${path} file but we intend to write a ${alternative} file. When both exist, bazel will pick the one with the .bazel extension, ignoring the other one. Please delete the ${path} file."
exit 1
fi
}
check_clash () {
path="$1"
check_dir "${path}"
if [ -e "${path}" ]; then
stderr "STOP: The current directory already has a ${path} file and we don't want to overwrite it."
exit 1
fi
}
check_files_dont_exist () {
# A BUILD.bazel file takes precedence over a BUILD file and likewise
# a WORKSPACE.bazel file takes precedence over a WORKSPACE file. We write
# BUILD.bazel and WORKSPACE files.
# Some Bazel tooling may fail on WORKSPACE.bazel files,
# e.g. https://github.com/bazelbuild/bazel-gazelle/issues/678
check_alt WORKSPACE.bazel WORKSPACE
check_alt BUILD BUILD.bazel
for clash in .bazelrc WORKSPACE BUILD.bazel MODULE.bazel zlib.BUILD.bazel Example.hs non_module_deps.bzl; do
check_clash "${clash}"
done
}
have () {
command -v "$1" > /dev/null 2>&1
}
check_bazel_version () {
if ! have bazel; then
# shellcheck disable=SC2016
stderr 'Warning: cannot find `bazel` executable in $PATH (skipping version check)'
return
fi
if ! actual_raw=$(bazel version | grep -E '^Build label:' | grep -Eo '[0-9.]+'); then
stderr 'Warning: cannot determine bazel version (skipping version check)'
return
fi
# shellcheck disable=SC2034
if ! IFS=. read -r actual_major actual_minor actual_patch <<-EOF
${actual_raw}
EOF
then
stderr "Warning: cannot parse version from bazel output (skipping version check)"
return
fi
expected_min="${MIN_BAZEL_MAJOR}.${MIN_BAZEL_MINOR}.0"
expected_max="${MAX_BAZEL_MAJOR}.${MAX_BAZEL_MINOR}.x"
if [ "${actual_major}" -gt "${MAX_BAZEL_MAJOR}" ] || \
{ [ "${actual_major}" -eq "${MAX_BAZEL_MAJOR}" ] && [ "${actual_minor}" -gt "${MAX_BAZEL_MINOR}" ]; }
then
stderr "Warning: a too new version of Bazel detected: v${actual_raw}."
stderr " Recommended versions are from v${expected_min} to v${expected_max}."
elif [ "${actual_major}" -lt "${MIN_BAZEL_MAJOR}" ] || \
{ [ "${actual_major}" -eq "${MIN_BAZEL_MAJOR}" ] && [ "${actual_minor}" -lt "${MIN_BAZEL_MINOR}" ]; }
then
stderr "Error: Need at least Bazel v${expected_min} but v${actual_raw} detected."
exit 1
fi
}
## Parse Arguments and Preflight Checks ################################
parse_args "$@"
if [ "${PRINT_NIX_USAGE}" ]; then
# shellcheck disable=SC2016
stderr 'INFO: Creating a WORKSPACE file based on GHC bindists. If you want to use a nix-based setup (e.g. on NixOS), call with `--use-nix`. See `--help` for more info.'
fi
check_files_dont_exist
check_bazel_version
## Write zlib Build File ###############################################
readonly ZLIB_BUILD_FILE="zlib.BUILD.bazel"
stderr "Creating ${ZLIB_BUILD_FILE}"
case "${MODE}" in
"bindists") cat <<-EOF
load("@rules_cc//cc:defs.bzl", "cc_library")
cc_library(
name = "zlib",
# Import ':z' as 'srcs' to enforce the library name 'libz.so'. Otherwise,
# Bazel would mangle the library name and e.g. Cabal wouldn't recognize it.
srcs = [":z"],
hdrs = glob(["*.h"]),
includes = ["."],
visibility = ["//visibility:public"],
)
cc_library(
name = "z",
srcs = glob(["*.c"]),
hdrs = glob(["*.h"]),
copts = ["-Wno-implicit-function-declaration"],
)
EOF
;;
"nix") cat <<-EOF
load("@rules_cc//cc:defs.bzl", "cc_library")
filegroup(
name = "include",
srcs = glob(["include/*.h"]),
visibility = ["//visibility:public"],
)
cc_library(
name = "zlib",
srcs = ["@nixpkgs_zlib//:lib"],
hdrs = [":include"],
strip_include_prefix = "include",
visibility = ["//visibility:public"],
)
EOF
;;
esac > "${ZLIB_BUILD_FILE}"
WITH_HADDOCK=True
case "$GHC_VERSION" in
8.10.*)
SNAPSHOT=lts-18.28
;;
9.0.*)
SNAPSHOT=lts-19.33
;;
9.2.*)
SNAPSHOT=lts-20.26
;;
9.4.*)
SNAPSHOT=lts-21.5
;;
9.6.*)
SNAPSHOT=lts-22.22
;;
*)
# zlib >= 0.7.1.0 depends on zlib-clib on Windows (unless using pkg-config), since zlib-clib is
# a C only cabal library that does not produce any haddock this results in an error.
# See https://github.com/tweag/rules_haskell/issues/2200
case "$( uname )" in
WindowsNT | MINGW* | MSYS* ) WITH_HADDOCK=False ;;
esac
SNAPSHOT=nightly-2024-05-24
stderr "warning: unsupported GHC version: ${GHC_VERSION}, using stack resolver ${SNAPSHOT}"
esac
## Write WORKSPACE File ################################################
stderr "Creating WORKSPACE"
if $BZLMOD; then
touch WORKSPACE
else
cat > WORKSPACE <<EOF
# Give your project a name. :)
workspace(name = "YOUR_PROJECT_NAME_HERE")
# Load the repository rule to download an http archive.
load(
"@bazel_tools//tools/build_defs/repo:http.bzl",
"http_archive"
)
# Download rules_haskell and make it accessible as "@rules_haskell".
http_archive(
name = "rules_haskell",
sha256 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
strip_prefix = "rules_haskell-M.NN",
url = "https://github.com/tweag/rules_haskell/releases/download/vM.NN/rules_haskell-M.NN.tar.gz",
)
load(
"@rules_haskell//haskell:repositories.bzl",
"rules_haskell_dependencies",
)
# Setup all Bazel dependencies required by rules_haskell.
rules_haskell_dependencies()
load(
"@rules_haskell//haskell:toolchain.bzl",
"rules_haskell_toolchains",
)
load(
"@rules_haskell//haskell:cabal.bzl",
"stack_snapshot"
)
stack_snapshot(
name = "stackage",
extra_deps = {"zlib": ["@zlib.dev//:zlib"]},
packages = ["zlib"],
# disable calling pkg-config
flags = {"zlib": ["-pkg-config"]},
haddock = $WITH_HADDOCK,
# LTS snapshot published for ghc-${GHC_VERSION} (default version used by rules_haskell)
snapshot = "$SNAPSHOT",
# This uses an unpinned version of stack_snapshot, meaning that stack is invoked on every build.
# To switch to pinned stackage dependencies, run \`bazel run @stackage-unpinned//:pin\` and
# uncomment the following line.
# stack_snapshot_json = "//:stackage_snapshot.json",
)
EOF
# Append toolchain and zlib rules
case "${MODE}" in
"bindists") cat <<-EOF
# Download a GHC binary distribution from haskell.org and register it as a toolchain.
rules_haskell_toolchains(
version = "${GHC_VERSION}",
)
http_archive(
name = "zlib.dev",
build_file = "//:${ZLIB_BUILD_FILE}",
sha256 = "9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23",
strip_prefix = "zlib-1.3.1",
urls = ["https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz"],
)
EOF
;;
"nix") cat <<-EOF
# Load nixpkgs_git_repository from rules_nixpkgs,
# which was already initialized by rules_haskell_dependencies above.
load(
"@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl",
"nixpkgs_cc_configure",
"nixpkgs_git_repository",
"nixpkgs_package",
"nixpkgs_python_configure",
)
# Fetch a version of nixpkgs from GitHub.
# For more information see the documentation of rules_nixpkgs at
# https://github.com/tweag/rules_nixpkgs/blob/master/README.md
nixpkgs_git_repository(
name = "nixpkgs",
revision = "${NIXPKGS_REVISION}",${NIXPKGS_HASH:+
integrity = \"$NIXPKGS_HASH\",}
)
nixpkgs_cc_configure(
repository = "@nixpkgs",
)
nixpkgs_python_configure(
repository = "@nixpkgs",
)
load(
"@rules_haskell//haskell:nixpkgs.bzl",
"haskell_register_ghc_nixpkgs",
)
# Fetch a GHC binary distribution from nixpkgs and register it as a toolchain.
# For more information:
# https://api.haskell.build/haskell/nixpkgs.html#haskell_register_ghc_nixpkgs
haskell_register_ghc_nixpkgs(
repository = "@nixpkgs",
attribute_path = "haskell.compiler.ghc$( echo "$GHC_VERSION" | tr -d . )",
version = "${GHC_VERSION}",
)
# For ${ZLIB_BUILD_FILE}
nixpkgs_package(
name = "nixpkgs_zlib",
attribute_path = "zlib",
repository = "@nixpkgs",
)
nixpkgs_package(
name = "zlib.dev",
build_file = "//:${ZLIB_BUILD_FILE}",
repository = "@nixpkgs",
)
EOF
;;
esac >> WORKSPACE
fi
if $BZLMOD; then
stderr "Creating MODULE.bazel"
cat >MODULE.bazel <<EOF
module(name = "your_project_name_here", version = "0.1")
bazel_dep(name = "rules_haskell", version = "0.0")
bazel_dep(name = "rules_cc", version = "0.0.9")
haskell_toolchains = use_extension(
"@rules_haskell//extensions:haskell_toolchains.bzl",
"haskell_toolchains",
)
haskell_toolchains.bindists(version = "$GHC_VERSION")
non_module_deps = use_extension(
"//:non_module_deps.bzl",
"non_module_deps",
)
use_repo(
non_module_deps,
"zlib.dev",
)
stack = use_extension(
"@rules_haskell//extensions:stack_snapshot.bzl",
"stack_snapshot",
)
use_repo(
stack,
"stackage",
"stackage-exe",
"stackage-unpinned",
)
stack.package(
name = "zlib",
extra_deps = ["@zlib.dev//:zlib"],
)
# LTS snapshot published for ghc-${GHC_VERSION} (default version used by rules_haskell)
stack.snapshot(name = "$SNAPSHOT")
# This uses an unpinned version of stack_snapshot, meaning that stack is invoked on every build.
# To switch to pinned stackage dependencies, run \`bazel run @stackage-unpinned//:pin\` and
# uncomment the following line.
#stack.stack_snapshot_json(label = "//:stackage_snapshot.json")
EOF
stderr "Creating non_module_deps.bzl"
cat >non_module_deps.bzl <<-EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
def _non_module_deps_impl(_mctx):
http_archive(
name = "zlib.dev",
build_file = "//:${ZLIB_BUILD_FILE}",
sha256 = "b5b06d60ce49c8ba700e0ba517fa07de80b5d4628a037f4be8ad16955be7a7c0",
strip_prefix = "zlib-1.3",
urls = ["https://github.com/madler/zlib/archive/v1.3.tar.gz"],
)
non_module_deps = module_extension(implementation = _non_module_deps_impl)
EOF
fi
## Write .bazelrc File #################################################
stderr "Creating .bazelrc"
cat > .bazelrc <<EOF
build:ci --loading_phase_threads=1
build:ci --jobs=2
build:ci --verbose_failures
common:ci --color=no
test:ci --test_output=errors
common --enable_bzlmod=$BZLMOD
# Should become the default in bazel 7
build --incompatible_enable_cc_toolchain_resolution
# Enable automatic OS-specific config (e.g. build:linux, build:macos, build:windows).
common --enable_platform_specific_config
# MacOS Configuration
# -------------------
# do not use Xcode on macOS
common:macos --repo_env=BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
# Windows Configuration
# ---------------------
# prevent auto-detection of system compilers on Windows
common:windows --repo_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1
EOF
if [ "${MODE}" = "nix" ]; then
cat >> .bazelrc <<-EOF
# This project uses a GHC provisioned via nix.
# We need to use the rules_haskell nix toolchain accordingly:
build --host_platform=@rules_nixpkgs_core//platforms:host
run --host_platform=@rules_nixpkgs_core//platforms:host
EOF
fi
cat >> .bazelrc <<EOF
# test environment does not propagate locales by default
# some tests reads files written in UTF8, we need to propagate the correct
# environment variables, such as LOCALE_ARCHIVE
# We also need to setup an utf8 locale
test --test_env=LANG=en_US.utf8 --test_env=LOCALE_ARCHIVE
try-import .bazelrc.local
EOF
## Write BUILD.bazel File ##############################################
stderr "Creating BUILD.bazel"
cat > BUILD.bazel <<EOF
# Set all target’s visibility in this package to "public".
package(default_visibility = ["//visibility:public"])
# Load rules_haskell rules.
load(
"@rules_haskell//haskell:defs.bzl",
"haskell_toolchain_library",
"haskell_library",
"haskell_binary",
)
# haskell_toolchain_library can access builtin GHC packages
# and assign them a bazel target name, so that they
# can be referenced as dependencies.
haskell_toolchain_library(name = "base")
# You can add your own libraries with haskell_library.
# haskell_library(
# name = "MY_LIBRARY_NAME",
# src_strip_prefix = "src",
# srcs = glob(['src/**/*.hs']),
# deps = [
# "base_pkg"
# ],
# )
# An example binary using the Prelude module from the
# GHC base package, and zlib from stackage, to print the hello world.
haskell_binary(
name = "example",
srcs = [":Example.hs"],
deps = [":base", "@stackage//:zlib"],
)
EOF
## Write Example.hs File ###############################################
stderr "Creating Example.hs"
cat > Example.hs <<EOF
module Main where
import Codec.Compression.Zlib (compress, decompress)
import Prelude ((.), putStrLn)
main = putStrLn "Hello from rules_haskell!"
slowId = decompress . compress
EOF
## Finish Up ###########################################################
cat >&2 <<EOF
WORKSPACE and initial BUILD files created. To run Bazel and build the example:
$ bazel run //:example
EOF