Skip to content

Commit 8d0c622

Browse files
committed
Refactor --config setup
Define top-level configurations for the user to specify, e.g. `--config linux-nixpkgs`. These will then define the required flags for that platform/toolchain combination. Configure CI by writing `--config` flags to `.bazelrc.local` instead of passing them on the command-line. This makes sure that the same configuration is used even if Bazel is called indirectly through a script. This is useful for tools like `ghcide` and `hie-bios`.
1 parent cf6beb4 commit 8d0c622

File tree

8 files changed

+156
-81
lines changed

8 files changed

+156
-81
lines changed

.bazelrc

+57-13
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,74 @@
11
# See https://docs.bazel.build/versions/master/user-manual.html#bazelrc.
22

3+
# Global Configuration
4+
# --------------------
5+
6+
# test environment does not propagate locales by default some tests reads files
7+
# written in UTF8, we need to propagate the correct environment variables, such
8+
# as LOCALE_ARCHIVE We also need to setup an utf8 locale
9+
test --test_env=LANG=C.UTF-8 --test_env=LOCALE_ARCHIVE
10+
11+
# Platform / Toolchain Selection
12+
# ------------------------------
13+
build:linux-nixpkgs --host_platform=@rules_haskell//haskell/platforms:linux_x86_64_nixpkgs
14+
build:macos-nixpkgs --host_platform=@rules_haskell//haskell/platforms:darwin_x86_64_nixpkgs
315
# Use this configuration when targeting Windows. Eventually this will
416
# no longer be required:
517
# https://bazel.build/roadmaps/platforms.html#replace---cpu-and---host_cpu-flags.
6-
build:windows --crosstool_top=@rules_haskell_ghc_windows_amd64//:cc_toolchain -s --verbose_failures --sandbox_debug
18+
build:windows-bindist --crosstool_top=@rules_haskell_ghc_windows_amd64//:cc_toolchain
19+
20+
# Build and Test Filters
21+
# ----------------------
22+
# Note tag filters do not compose. If you wish to augment these filters on the
23+
# command-line you have to repeat the full list.
24+
build:linux-bindist --build_tag_filters -requires_lz4,-requires_proto,-requires_zlib,-requires_doctest,-requires_c2hs,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist
25+
build:linux-bindist --test_tag_filters -requires_lz4,-requires_proto,-requires_zlib,-requires_doctest,-requires_c2hs,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist
26+
27+
build:macos-nixpkgs --build_tag_filters -dont_test_on_darwin
28+
build:macos-nixpkgs --test_tag_filters -dont_test_on_darwin
29+
30+
build:macos-bindist --build_tag_filters -dont_test_on_darwin,-dont_test_on_darwin_with_bindist,-requires_lz4,-requires_proto,-requires_zlib,-requires_doctest,-requires_c2hs,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist
31+
build:macos-bindist --test_tag_filters -dont_test_on_darwin,-dont_test_on_darwin_with_bindist,-requires_lz4,-requires_proto,-requires_zlib,-requires_doctest,-requires_c2hs,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist
32+
33+
# CI Configuration
34+
# ----------------
35+
common:ci --color=no
736

8-
build:ci_linux --remote_http_cache=https://storage.googleapis.com/tweag-bazel-cache-buildkite --remote_upload_local_results=true --google_default_credentials
937
build:ci --loading_phase_threads=1
1038
build:ci --verbose_failures
1139
# Make sure we don't rely on the names of convenience symlinks because those
1240
# can be changed by user.
1341
build:ci --symlink_prefix=bazel-ci-
14-
common:ci --color=no
42+
1543
test:ci --test_output=errors
1644

45+
build:ci-linux-bindist --remote_http_cache=https://storage.googleapis.com/tweag-bazel-cache-buildkite --remote_upload_local_results=true --google_default_credentials
46+
build:ci-linux-nixpkgs --remote_http_cache=https://storage.googleapis.com/tweag-bazel-cache-buildkite --remote_upload_local_results=true --google_default_credentials
47+
48+
build:ci-linux-bindist --repository_cache ~/repo_cache
49+
# XXX: @com_google_protobuf sets `use_default_shell_env = True`, so we enable
50+
# strict action env to avoid changes in `PATH` invalidating the cache.
51+
build:ci-linux-bindist --experimental_strict_action_env
52+
53+
build:ci-macos-bindist --disk_cache=~/.cache/bazel/
54+
build:ci-macos-nixpkgs --disk_cache=~/.cache/bazel/
55+
56+
build:ci-windows-bindist --subcommands --sandbox_debug
57+
1758
# Note [backward compatible options]
18-
common:ci --incompatible_enable_cc_toolchain_resolution
19-
common:ci --incompatible_load_cc_rules_from_bzl
59+
build:ci-linux-bindist --incompatible_enable_cc_toolchain_resolution
60+
build:ci-macos-bindist --incompatible_enable_cc_toolchain_resolution
61+
# rules_nixpkgs is not yet compatible with cc toolchain resolution, see
62+
# https://github.com/tweag/rules_nixpkgs/issues/88
63+
#build:ci-linux-nixpkgs --incompatible_enable_cc_toolchain_resolution
64+
#build:ci-macos-nixpkgs --incompatible_enable_cc_toolchain_resolution
65+
# Windows still uses --crosstool_top.
66+
#build:ci-windows-bindist --incompatible_enable_cc_toolchain_resolution
67+
build:ci --incompatible_load_cc_rules_from_bzl
2068
# Blocked by https://github.com/bazelbuild/buildtools/issues/757
21-
#common:ci --incompatible_load_proto_rules_from_bzl
22-
common:ci --incompatible_load_python_rules_from_bzl
23-
24-
# test environment does not propagate locales by default
25-
# some tests reads files written in UTF8, we need to propagate the correct
26-
# environment variables, such as LOCALE_ARCHIVE
27-
# We also need to setup an utf8 locale
28-
test --test_env=LANG=C.UTF-8 --test_env=LOCALE_ARCHIVE
69+
#build:ci --incompatible_load_proto_rules_from_bzl
70+
build:ci --incompatible_load_python_rules_from_bzl
2971

72+
# User Configuration
73+
# ------------------
3074
try-import %workspace%/.bazelrc.local

.buildkite/bindists-pipeline

+6-11
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@
66
set -euo pipefail
77

88
BAZEL_DIR="$(.ci/fetch-bazel-bindist)"
9-
REPO_CACHE="$HOME/repo_cache"
109

1110
trap "rm -rf '$BAZEL_DIR'" EXIT
1211
export PATH="$BAZEL_DIR:$PATH"
13-
EXCLUDED="-requires_lz4,-requires_proto,-requires_zlib,-requires_doctest,-requires_c2hs,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist"
14-
echo "common:ci --build_tag_filters $EXCLUDED" > .bazelrc.local
15-
echo "common:ci --test_tag_filters $EXCLUDED" >> .bazelrc.local
16-
echo "build:ci --config=ci_linux" >> .bazelrc.local
17-
echo "common:ci --repository_cache $REPO_CACHE" >> .bazelrc.local
18-
# XXX: @com_google_protobuf sets `use_default_shell_env = True`, so we enable
19-
# strict action env to avoid changes in `PATH` invalidating the cache.
20-
echo "build:ci --experimental_strict_action_env" >> .bazelrc.local
12+
cat >.bazelrc.local <<EOF
13+
common --config=ci
14+
build --config=linux-bindist --config=ci-linux-bindist
15+
EOF
2116
./tests/run-start-script.sh --use-bindists
22-
bazel build --config ci //tests/...
23-
bazel test --config ci //tests/...
17+
bazel build //tests/...
18+
bazel test //tests/...

.buildkite/pipeline.yml

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
steps:
22
- label: "Run tests (Nixpkgs)"
33
command: |
4-
echo "build:ci --host_platform=@rules_haskell//haskell/platforms:linux_x86_64_nixpkgs" > .bazelrc.local
5-
echo "build:ci --config=ci_linux" >> .bazelrc.local
4+
cat >.bazelrc.local <<EOF
5+
common --config=ci
6+
build --config=linux-nixpkgs --config=ci-linux-nixpkgs
7+
EOF
68
nix-shell --arg docTools false --pure --run '
79
set -e
810
# Ensure that the Nixpkgs bazel version matches the one specified in
911
# `.bazelversion` and fetched in
1012
# `.buildkite/bindists-pipeline` for the bindists version.
1113
.ci/check-bazel-version
1214
./tests/run-start-script.sh --use-nix
13-
bazel build --config ci //tests:run-tests
15+
bazel build //tests:run-tests
1416
./bazel-ci-bin/tests/run-tests
15-
bazel coverage //tests/... --config ci --build_tag_filters "coverage-compatible" --test_tag_filters "coverage-compatible" --test_output=all
17+
bazel coverage //tests/... --build_tag_filters "coverage-compatible" --test_tag_filters "coverage-compatible" --test_output=all
1618
'
1719
timeout: 100
1820

.circleci/config.yml

+13-12
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ jobs:
4747
command: |
4848
mkdir -p ~/.cache/bazel/
4949
50-
echo "build:ci --host_platform=@rules_haskell//haskell/platforms:darwin_x86_64_nixpkgs" >> .bazelrc.local
51-
echo "build:ci --disk_cache=~/.cache/bazel/" >> .bazelrc.local
52-
echo "common:ci --test_tag_filters -dont_test_on_darwin" >> .bazelrc.local
50+
cat >.bazelrc.local <<EOF
51+
common --config=ci
52+
build --config=macos-nixpkgs --config=ci-macos-nixpkgs
53+
EOF
5354
5455
- restore_cache:
5556
keys: # see note about 'Disk cache'
@@ -75,7 +76,7 @@ jobs:
7576
shell: /bin/bash -eilo pipefail
7677
command: |
7778
nix-shell --arg docTools false --pure --run \
78-
'bazel build --config ci //tests/...'
79+
'bazel build //tests/...'
7980
- run:
8081
name: Run tests
8182
shell: /bin/bash -eilo pipefail
@@ -90,9 +91,9 @@ jobs:
9091
# './tests/run-start-script.sh --use-nix'
9192
nix-shell --arg docTools false --pure --run '
9293
set -euo pipefail
93-
bazel build --config ci //tests:run-tests
94+
bazel build //tests:run-tests
9495
./bazel-ci-bin/tests/run-tests
95-
bazel coverage //tests/... --config ci --build_tag_filters "coverage-compatible" --test_tag_filters "coverage-compatible" --test_output=all
96+
bazel coverage //tests/... --build_tag_filters "coverage-compatible" --test_tag_filters "coverage-compatible" --test_output=all
9697
'
9798
9899
# see note about 'Disk cache'
@@ -137,10 +138,10 @@ jobs:
137138
export BAZEL_USE_CPP_ONLY_TOOLCHAIN=1
138139
EOF
139140
140-
echo "build:ci --disk_cache=~/.cache/bazel/" >> .bazelrc.local
141-
EXCLUDED="-dont_test_on_darwin,-dont_test_on_darwin_with_bindist,-requires_lz4,-requires_proto,-requires_zlib,-requires_doctest,-requires_c2hs,-requires_shellcheck,-requires_threaded_rts,-dont_test_with_bindist"
142-
echo "common:ci --build_tag_filters $EXCLUDED" >> .bazelrc.local
143-
echo "common:ci --test_tag_filters $EXCLUDED" >> .bazelrc.local
141+
cat >.bazelrc.local <<EOF
142+
common --config=ci
143+
build --config=macos-bindist --config=ci-macos-bindist
144+
EOF
144145
145146
- restore_cache:
146147
keys: # see note about 'Disk cache'
@@ -160,7 +161,7 @@ jobs:
160161
shell: /bin/bash -eilo pipefail
161162
command: |
162163
. $HOME/.bashrc_rules_haskell
163-
bazel build --config ci //tests/...
164+
bazel build //tests/...
164165
- run:
165166
name: Run tests
166167
shell: /bin/bash -eilo pipefail
@@ -173,7 +174,7 @@ jobs:
173174
# XXX 2019-01-22 Disable start script checking on Darwin
174175
# due to a clash between binutils and clang.
175176
# ./tests/run-start-script.sh --use-bindists
176-
bazel test --config ci //tests/...
177+
bazel test //tests/...
177178
178179
# see note about 'Disk cache'
179180
- save_cache:

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,31 @@ This chooses the `cc_toolchain` bundled with GHC.
252252

253253
## For `rules_haskell` developers
254254

255+
### Configuring your platform
256+
257+
`rules_haskell` can be built and tested on Linux, MacOS, and Windows. Depending
258+
on the platform GHC can be provisioned using nixpkgs or by downloading a binary
259+
distribution. In case of nixpkgs other toolchains (C compiler, Python, shell
260+
tools) will also be provided by nixpkgs, in case of bindist they will be taken
261+
from the environment (`$PATH`). The following `--config` options select the
262+
corresponding combination of operating system and GHC distribution:
263+
264+
| | Linux | MacOS | Windows |
265+
| ------------------- | --------------- | --------------- | ----------------- |
266+
| nixpkgs | `linux-nixpkgs` | `macos-nixpkgs` | |
267+
| binary distribution | `linux-bindist` | `macos-bindist` | `windows-bindist` |
268+
269+
Hint: You can use Bazel's `--announce_rc` flag to see what options are being
270+
used for a command in a specific configuration. E.g.
271+
```
272+
$ bazel build //tests:run-tests --config linux-nixpkgs --nobuild --announce_rc
273+
```
274+
275+
Hint: To avoid repetition you can add your configuration to `.bazelrc.local`.
276+
```
277+
echo "build --config=linux-nixpkgs" >>.bazelrc.local
278+
```
279+
255280
### Saving common command-line flags to a file
256281

257282
If you find yourself constantly passing the same flags on the

azure-pipelines.yml

+43-36
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ jobs:
2222
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' -Name 'LongPathsEnabled' -Value 1
2323
displayName: "Enable da long paths"
2424
25+
- bash: |
26+
cat >.bazelrc.local <<EOF
27+
common --config=ci
28+
build --config=windows-bindist --config=ci-windows-bindist
29+
EOF
30+
displayName: "Configure Bazel"
31+
2532
- bash: |
2633
set -e
2734
export MSYS2_ARG_CONV_EXCL="*"
@@ -32,49 +39,49 @@ jobs:
3239
echo "PATH='$PATH'"
3340
3441
# Tests that build but don't run
35-
/c/bazel/bazel.exe build --config windows "//tests/c-compiles-still/..."
36-
/c/bazel/bazel.exe build --config windows "//tests/binary-with-data/..."
37-
/c/bazel/bazel.exe build --config windows "//tests/binary-indirect-cbits"
42+
/c/bazel/bazel.exe build "//tests/c-compiles-still/..."
43+
/c/bazel/bazel.exe build "//tests/binary-with-data/..."
44+
/c/bazel/bazel.exe build "//tests/binary-indirect-cbits"
3845
3946
# Tests that only require building
4047
# (when using 'test' CI fails with:
4148
# ERROR: No test targets were found, yet testing was requested
4249
# )
4350
# See https://github.com/bazelbuild/bazel/issues/7291
44-
/c/bazel/bazel.exe build --config windows "//tests/data/..."
45-
/c/bazel/bazel.exe build --config windows "//tests/failures/..."
46-
/c/bazel/bazel.exe build --config windows "//tests/hidden-modules/..."
47-
/c/bazel/bazel.exe build --config windows "//tests/package-id-clash/..."
51+
/c/bazel/bazel.exe build "//tests/data/..."
52+
/c/bazel/bazel.exe build "//tests/failures/..."
53+
/c/bazel/bazel.exe build "//tests/hidden-modules/..."
54+
/c/bazel/bazel.exe build "//tests/package-id-clash/..."
4855
4956
# Tests that succeed
50-
/c/bazel/bazel.exe test --config windows "//tests:test-binary-simple"
51-
/c/bazel/bazel.exe test --config windows "//tests:test-binary-custom-main"
52-
/c/bazel/bazel.exe test --config windows "//tests/binary-custom-main/..."
53-
/c/bazel/bazel.exe test --config windows "//tests/binary-exe-path/..."
54-
/c/bazel/bazel.exe test --config windows "//tests/binary-with-data/..."
55-
/c/bazel/bazel.exe test --config windows "//tests/binary-with-lib/..."
56-
/c/bazel/bazel.exe test --config windows "//tests/binary-with-main/..."
57-
/c/bazel/bazel.exe test --config windows "//tests/binary-simple/..."
58-
/c/bazel/bazel.exe test --config windows "//tests/binary-with-compiler-flags/..."
59-
/c/bazel/bazel.exe test --config windows "//tests/binary-with-import/..."
60-
/c/bazel/bazel.exe test --config windows "//tests/binary-with-link-flags/..."
61-
/c/bazel/bazel.exe test --config windows "//tests/cpp_macro_conflict/..."
62-
/c/bazel/bazel.exe test --config windows "//tests/extra-source-files/..."
63-
/c/bazel/bazel.exe test --config windows "//tests/java_classpath/..."
64-
/c/bazel/bazel.exe test --config windows "//tests/generated-modules/..."
65-
/c/bazel/bazel.exe test --config windows "//tests/haskell_test/..."
66-
/c/bazel/bazel.exe test --config windows "//tests/hs-boot/..."
67-
/c/bazel/bazel.exe test --config windows "//tests/indirect-link/..."
68-
/c/bazel/bazel.exe test --config windows "//tests/library-deps/..."
69-
/c/bazel/bazel.exe test --config windows "//tests/library-exports/..."
70-
/c/bazel/bazel.exe test --config windows "//tests/library-linkstatic-flag/..."
71-
/c/bazel/bazel.exe test --config windows "//tests/lhs/..."
72-
/c/bazel/bazel.exe test --config windows "//tests/package-id-clash-binary/..."
73-
/c/bazel/bazel.exe test --config windows "//tests/package-name/..."
74-
/c/bazel/bazel.exe test --config windows "//tests/textual-hdrs/..."
75-
/c/bazel/bazel.exe test --config windows "//tests/two-libs/..."
76-
/c/bazel/bazel.exe test --config windows "//tests/encoding/..."
77-
/c/bazel/bazel.exe test --config windows "//tests/c-compiles/..."
78-
/c/bazel/bazel.exe test --config windows "//tests/stack-snapshot-deps/..."
57+
/c/bazel/bazel.exe test "//tests:test-binary-simple"
58+
/c/bazel/bazel.exe test "//tests:test-binary-custom-main"
59+
/c/bazel/bazel.exe test "//tests/binary-custom-main/..."
60+
/c/bazel/bazel.exe test "//tests/binary-exe-path/..."
61+
/c/bazel/bazel.exe test "//tests/binary-with-data/..."
62+
/c/bazel/bazel.exe test "//tests/binary-with-lib/..."
63+
/c/bazel/bazel.exe test "//tests/binary-with-main/..."
64+
/c/bazel/bazel.exe test "//tests/binary-simple/..."
65+
/c/bazel/bazel.exe test "//tests/binary-with-compiler-flags/..."
66+
/c/bazel/bazel.exe test "//tests/binary-with-import/..."
67+
/c/bazel/bazel.exe test "//tests/binary-with-link-flags/..."
68+
/c/bazel/bazel.exe test "//tests/cpp_macro_conflict/..."
69+
/c/bazel/bazel.exe test "//tests/extra-source-files/..."
70+
/c/bazel/bazel.exe test "//tests/java_classpath/..."
71+
/c/bazel/bazel.exe test "//tests/generated-modules/..."
72+
/c/bazel/bazel.exe test "//tests/haskell_test/..."
73+
/c/bazel/bazel.exe test "//tests/hs-boot/..."
74+
/c/bazel/bazel.exe test "//tests/indirect-link/..."
75+
/c/bazel/bazel.exe test "//tests/library-deps/..."
76+
/c/bazel/bazel.exe test "//tests/library-exports/..."
77+
/c/bazel/bazel.exe test "//tests/library-linkstatic-flag/..."
78+
/c/bazel/bazel.exe test "//tests/lhs/..."
79+
/c/bazel/bazel.exe test "//tests/package-id-clash-binary/..."
80+
/c/bazel/bazel.exe test "//tests/package-name/..."
81+
/c/bazel/bazel.exe test "//tests/textual-hdrs/..."
82+
/c/bazel/bazel.exe test "//tests/two-libs/..."
83+
/c/bazel/bazel.exe test "//tests/encoding/..."
84+
/c/bazel/bazel.exe test "//tests/c-compiles/..."
85+
/c/bazel/bazel.exe test "//tests/stack-snapshot-deps/..."
7986
8087
displayName: 'Run Bazel'

haskell/ghc.BUILD.tpl

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
load(
2+
"@rules_cc//cc:defs.bzl",
3+
"cc_toolchain",
4+
"cc_toolchain_suite",
5+
)
16
load(
27
"@rules_haskell//haskell:cc_toolchain_config.bzl",
38
"cc_toolchain_config",

tests/RunTests.hs

+1-5
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,7 @@ main = hspec $ do
9090
-- | Returns a bazel command line suitable for CI
9191
-- This should be called with the action as first item of the list. e.g 'bazel ["build", "//..."]'.
9292
bazel :: [String] -> Process.CreateProcess
93-
-- Note: --config=ci is intercalated between the action and the list of
94-
-- arguments. It should appear after the action, but before any @--@
95-
-- following argument.
96-
bazel (command:args) = Process.proc "bazel" (command:"--config=ci":args)
97-
bazel [] = Process.proc "bazel" []
93+
bazel args = Process.proc "bazel" args
9894

9995
-- | Runs a bazel query and return the list of matching targets
10096
bazelQuery :: String -> SpecM a [String]

0 commit comments

Comments
 (0)