about summary refs log tree commit diff
path: root/third_party/bazel/rules_haskell/start
blob: 24f300cb6ec9b7316ddc2f36d6bedb0916b5bc27 (plain) (blame)
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
#!/bin/sh

MIN_BAZEL_MAJOR=0
MIN_BAZEL_MINOR=24

set -e

check_files_dont_exist () {
    if [ -e WORKSPACE ] || [ -e BUILD ] || [ -e BazelExample.hs ]
    then
        echo "Current directory already has WORKSPACE and/or BUILD and/or BazelExample.hs files." >/dev/stderr
        exit 1
    fi
}

check_bazel_version () {
    local actual_raw=$(bazel version | egrep '^Build label:' | egrep -o '[0-9.]+')

    IFS=. read actual_major actual_minor actual_patch <<EOF
$actual_raw
EOF

    local expected=$MIN_BAZEL_MAJOR.$MIN_BAZEL_MINOR.0
    local cmp=$expected'\n'$actual

    if ! ( [ "$actual_major" -gt "$MIN_BAZEL_MAJOR" ] || (
            [ "$actual_major" -eq "$MIN_BAZEL_MAJOR" ] &&
                [ "$actual_minor" -ge "$MIN_BAZEL_MINOR" ] ) )
    then
        echo "Need at least Bazel v${expected}. v${actual_raw} detected." >/dev/stderr
        exit 1
    fi
}

check_files_dont_exist
check_bazel_version

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 `@io_tweag_rules_haskell`.
http_archive(
    name = "io_tweag_rules_haskell",
    strip_prefix = "rules_haskell-0.8",
    urls = ["https://github.com/tweag/rules_haskell/archive/v0.8.tar.gz"],
    sha256 = "431d492a8ee6a110cdf42496181c9d27225dfb997379e64a148eb8e69f272ab7",
)

load(
    "@io_tweag_rules_haskell//haskell:repositories.bzl",
    "haskell_repositories"
)

# `haskell_repositories()` sets up all bazel dependencies
# required by `rules_haskell`.
haskell_repositories()

load(
    "@io_tweag_rules_haskell//haskell:haskell.bzl",
    "haskell_register_ghc_bindists",
)

# Registers a haskell toolchain with a GHC binary
# downloaded from haskell.org.
haskell_register_ghc_bindists(version = "8.6.4")
EOF

cat > BUILD.bazel <<"EOF"
# Set all target’s visibility in this package to "public".
package(default_visibility = ["//visibility:public"])

# Load `rules_haskell` rules.
load(
    "@io_tweag_rules_haskell//haskell:haskell.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, to print the hello world.
haskell_binary(
    name = "example",
    srcs = [":Example.hs"],
    deps = [":base"],
)
EOF

cat > Example.hs <<"EOF"
module Main where

import Prelude (putStrLn)

main = putStrLn "Hello from rules_haskell!"
EOF

cat <<"EOF"
WORKSPACE and initial BUILD files created. To run Bazel and build the example:

    $ bazel run //:example
EOF