@@ -110,6 +110,7 @@ nix_toolchain=$(cat <<EOF
110110load(
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(
142143EOF
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+
145220get_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+
152234echo " Creating WORKSPACE" >&2
153235cat > 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+ )
185306EOF
186307
187308echo " 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.
241362haskell_binary(
242363 name = "example",
243364 srcs = [":Example.hs"],
244- deps = [":base"],
365+ deps = [":base", "@stackage//:zlib" ],
245366)
246367EOF
247368
248369echo " Creating Example.hs" >&2
249370cat > Example.hs << "EOF "
250371module Main where
251372
252- import Prelude (putStrLn)
373+ import Codec.Compression.Zlib (compress, decompress)
374+ import Prelude ((.), putStrLn)
253375
254376main = putStrLn "Hello from rules_haskell!"
377+
378+ slowId = decompress . compress
255379EOF
256380
257381cat >&2 << "EOF "
0 commit comments