Skip to content

Commit 98faf6f

Browse files
authored
Merge pull request #1286 from tweag/ch/augment-start-script
start script: add stack_snapshot example
2 parents dd7f33b + ea538f1 commit 98faf6f

File tree

2 files changed

+129
-4
lines changed

2 files changed

+129
-4
lines changed

haskell/ghc_bindist.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value")
55
load("@rules_sh//sh:posix.bzl", "sh_posix_configure")
66
load(":private/workspace_utils.bzl", "execute_or_fail_loudly")
77

8-
_GHC_DEFAULT_VERSION = "8.6.5"
8+
_GHC_DEFAULT_VERSION = "8.6.5" # If you change this, change stackage's version
9+
# in the start script (see stackage.org)
910

1011
# Generated with `bazel run @rules_haskell//haskell:gen-ghc-bindist`
1112
# To add a version or architecture, edit the constants in haskell/gen_ghc_bindist.py,

start

Lines changed: 127 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ nix_toolchain=$(cat <<EOF
110110
load(
111111
"@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl",
112112
"nixpkgs_git_repository",
113+
"nixpkgs_package",
113114
"nixpkgs_python_configure",
114115
)
115116
@@ -142,13 +143,94 @@ haskell_register_ghc_nixpkgs(
142143
EOF
143144
)
144145

146+
declare -r ALEX_BUILD_FILE="alex.BUILD.bazel"
147+
declare -r ZLIB_BUILD_FILE="zlib.BUILD.bazel"
148+
149+
echo "Creating $ZLIB_BUILD_FILE" >&2
150+
case "$mode" in
151+
"bindists")
152+
153+
cat > "$ZLIB_BUILD_FILE" <<"EOF"
154+
load("@rules_cc//cc:defs.bzl", "cc_library")
155+
156+
cc_library(
157+
name = "zlib",
158+
# Import ':z' as 'srcs' to enforce the library name 'libz.so'. Otherwise,
159+
# Bazel would mangle the library name and e.g. Cabal wouldn't recognize it.
160+
srcs = [":z"],
161+
hdrs = glob(["*.h"]),
162+
includes = ["."],
163+
visibility = ["//visibility:public"],
164+
)
165+
cc_library(name = "z", srcs = glob(["*.c"]), hdrs = glob(["*.h"]))
166+
EOF
167+
;;
168+
169+
"nix")
170+
171+
cat > "$ZLIB_BUILD_FILE" <<"EOF"
172+
load("@rules_cc//cc:defs.bzl", "cc_library")
173+
174+
filegroup(
175+
name = "include",
176+
srcs = glob(["include/*.h"]),
177+
visibility = ["//visibility:public"],
178+
)
179+
cc_library(
180+
name = "zlib",
181+
srcs = ["@nixpkgs_zlib//:lib"],
182+
hdrs = [":include"],
183+
strip_include_prefix = "include",
184+
visibility = ["//visibility:public"],
185+
)
186+
EOF
187+
esac
188+
189+
bindist_zlib=$(cat <<EOF
190+
http_archive(
191+
name = "zlib.dev",
192+
build_file = "//:$ZLIB_BUILD_FILE",
193+
sha256 = "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1",
194+
strip_prefix = "zlib-1.2.11",
195+
urls = [
196+
"https://mirror.bazel.build/zlib.net/zlib-1.2.11.tar.gz",
197+
"http://zlib.net/zlib-1.2.11.tar.gz",
198+
],
199+
)
200+
EOF
201+
)
202+
203+
nix_zlib=$(cat <<EOF
204+
# For $ZLIB_BUILD_FILE
205+
nixpkgs_package(
206+
name = "nixpkgs_zlib",
207+
attribute_path = "zlib",
208+
repository = "@nixpkgs",
209+
)
210+
211+
nixpkgs_package(
212+
name = "zlib.dev",
213+
build_file = "//:$ZLIB_BUILD_FILE",
214+
repository = "@nixpkgs",
215+
)
216+
EOF
217+
)
218+
219+
145220
get_toolchain () {
146221
case $mode in
147222
bindists) printf '%s' "$bindist_toolchain" ;;
148223
nix) printf '%s' "$nix_toolchain" ;;
149224
esac
150225
}
151226

227+
get_zlib () {
228+
case $mode in
229+
bindists) printf '%s' "$bindist_zlib" ;;
230+
nix) printf '%s' "$nix_zlib" ;;
231+
esac
232+
}
233+
152234
echo "Creating WORKSPACE" >&2
153235
cat > WORKSPACE <<EOF
154236
# Give your project a name. :)
@@ -181,7 +263,46 @@ load(
181263
"rules_haskell_toolchains",
182264
)
183265
266+
# Download alex and make it accessible as @alex.
267+
# Delete this block, alex.BUILD.bazel, and the reference in
268+
# stack_snapshot's tools if you don't use alex.
269+
http_archive(
270+
name = "alex",
271+
build_file = "//:$ALEX_BUILD_FILE",
272+
sha256 = "d58e4d708b14ff332a8a8edad4fa8989cb6a9f518a7c6834e96281ac5f8ff232",
273+
strip_prefix = "alex-3.2.4",
274+
urls = ["http://hackage.haskell.org/package/alex-3.2.4/alex-3.2.4.tar.gz"],
275+
)
276+
277+
load(
278+
"@rules_haskell//haskell:cabal.bzl",
279+
"stack_snapshot"
280+
)
281+
282+
stack_snapshot(
283+
name = "stackage",
284+
extra_deps = {"zlib": ["@zlib.dev//:zlib"]},
285+
packages = ["zlib"],
286+
tools = [ "@alex" ],
287+
snapshot = "lts-14.27", # Last snapshot published for ghc-8.6.5
288+
# the default version picked up by rules_haskell
289+
)
290+
184291
$(get_toolchain)
292+
293+
$(get_zlib)
294+
EOF
295+
296+
echo "Creating $ALEX_BUILD_FILE" >&2
297+
cat > "$ALEX_BUILD_FILE" <<EOF
298+
load("@rules_haskell//haskell:cabal.bzl", "haskell_cabal_binary")
299+
300+
haskell_cabal_binary(
301+
name = "alex",
302+
srcs = glob(["**"]),
303+
verbose = False,
304+
visibility = ["//visibility:public"],
305+
)
185306
EOF
186307

187308
echo "Creating .bazelrc" >&2
@@ -237,21 +358,24 @@ haskell_toolchain_library(name = "base")
237358
# )
238359
239360
# An example binary using the Prelude module from the
240-
# GHC base package, to print the hello world.
361+
# GHC base package, and zlib from stackage, to print the hello world.
241362
haskell_binary(
242363
name = "example",
243364
srcs = [":Example.hs"],
244-
deps = [":base"],
365+
deps = [":base", "@stackage//:zlib"],
245366
)
246367
EOF
247368

248369
echo "Creating Example.hs" >&2
249370
cat > Example.hs <<"EOF"
250371
module Main where
251372
252-
import Prelude (putStrLn)
373+
import Codec.Compression.Zlib (compress, decompress)
374+
import Prelude ((.), putStrLn)
253375
254376
main = putStrLn "Hello from rules_haskell!"
377+
378+
slowId = decompress . compress
255379
EOF
256380

257381
cat >&2 <<"EOF"

0 commit comments

Comments
 (0)