Skip to content

Commit 2582aa2

Browse files
committed
Implement nixpkgs_cc_configure.
This macro calls cc_autoconf_impl like before, but ahead of then also defines a nixpkgs_package with all the tools we want for the CC toolchain.
1 parent a456a2e commit 2582aa2

File tree

6 files changed

+129
-65
lines changed

6 files changed

+129
-65
lines changed

README.md

+55
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,61 @@ filegroup(
226226
</tbody>
227227
</table>
228228

229+
### nixpkgs_cc_configure
230+
231+
Tells Bazel to use compilers and linkers from Nixpkgs for the CC
232+
toolchain. By default, Bazel autodetects a toolchain on the current
233+
`PATH`. Overriding this autodetection makes builds more hermetic and
234+
is considered a best practice.
235+
236+
Example:
237+
238+
```bzl
239+
nixpkgs_cc_configure(repository = "@nixpkgs//:default.nix")
240+
```
241+
242+
<table class="table table-condensed table-bordered table-params">
243+
<colgroup>
244+
<col class="col-param" />
245+
<col class="param-description" />
246+
</colgroup>
247+
<thead>
248+
<tr>
249+
<th colspan="2">Attributes</th>
250+
</tr>
251+
</thead>
252+
<tbody>
253+
<tr>
254+
<td><code>nix_file_content</code></td>
255+
<td>
256+
<p><code>String; optional</code></p>
257+
<p>An expression for a Nix environment derivation.</p>
258+
</td>
259+
</tr>
260+
<tr>
261+
<td><code>repository</code></td>
262+
<td>
263+
<p><code>Label; optional</code></p>
264+
<p>A repository label identifying which Nixpkgs to use.</p>
265+
</td>
266+
</tr>
267+
<tr>
268+
<td><code>repositories</code></td>
269+
<td>
270+
<p><code>String-keyed label dict; optional</code></p>
271+
<p>A dictionary mapping `NIX_PATH` entries to repository labels.</p>
272+
<p>Setting it to
273+
<pre><code>repositories = { "myrepo" : "//:myrepo" }</code></pre>
274+
for example would replace all instances
275+
of <code>&lt;myrepo&gt;</code> in the called nix code by the
276+
path to the target <code>"//:myrepo"</code>. See the
277+
<a href="https://nixos.org/nix/manual/#env-NIX_PATH">relevant
278+
section in the nix manual</a> for more information.</p>
279+
<p>Specify one of `path` or `repositories`.</p>
280+
</td>
281+
</tr>
282+
</tbody>
283+
</table>
229284

230285
## Migration
231286

WORKSPACE

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
workspace(name = "io_tweag_rules_nixpkgs")
22

3-
load("//nixpkgs:nixpkgs.bzl", "nixpkgs_git_repository", "nixpkgs_package")
3+
load(
4+
"//nixpkgs:nixpkgs.bzl",
5+
"nixpkgs_cc_configure",
6+
"nixpkgs_git_repository",
7+
"nixpkgs_package"
8+
)
49

510
# For tests
611

@@ -74,3 +79,5 @@ filegroup(
7479
)
7580
""",
7681
)
82+
83+
nixpkgs_cc_configure(repository = "@remote_nixpkgs//:default.nix")

nixpkgs/BUILD.pkg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package(default_visibility = [ "//visibility:public" ])
1+
package(default_visibility = ["//visibility:public"])
22

33
filegroup(
44
name = "bin",

nixpkgs/nixpkgs.bzl

+58-63
Original file line numberDiff line numberDiff line change
@@ -187,67 +187,62 @@ stderr:
187187
stdout=exec_result.stdout,
188188
stderr=exec_result.stderr))
189189

190-
def _cc_configure_custom(ctx):
191-
overriden_tools = {
192-
"gcc": ctx.path(ctx.attr.gcc),
193-
"ld": ctx.path(ctx.attr.ld),
194-
}
195-
return cc_autoconf_impl(ctx, overriden_tools)
196-
197-
cc_configure_custom = repository_rule(
198-
implementation = _cc_configure_custom,
199-
attrs = {
200-
"gcc": attr.label(
201-
executable=True,
202-
cfg="host",
203-
allow_single_file=True,
204-
doc="`gcc` to use in cc toolchain",
205-
),
206-
"ld": attr.label(
207-
executable=True,
208-
cfg="host",
209-
allow_single_file=True,
210-
doc="`ld` to use in cc toolchain",
211-
),
212-
},
213-
local = True,
214-
environ = [
215-
"ABI_LIBC_VERSION",
216-
"ABI_VERSION",
217-
"BAZEL_COMPILER",
218-
"BAZEL_HOST_SYSTEM",
219-
"BAZEL_LINKOPTS",
220-
"BAZEL_PYTHON",
221-
"BAZEL_SH",
222-
"BAZEL_TARGET_CPU",
223-
"BAZEL_TARGET_LIBC",
224-
"BAZEL_TARGET_SYSTEM",
225-
"BAZEL_USE_CPP_ONLY_TOOLCHAIN",
226-
"BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN",
227-
"BAZEL_USE_LLVM_NATIVE_COVERAGE",
228-
"BAZEL_VC",
229-
"BAZEL_VS",
230-
"CC",
231-
"CC_CONFIGURE_DEBUG",
232-
"CC_TOOLCHAIN_NAME",
233-
"CPLUS_INCLUDE_PATH",
234-
"CUDA_COMPUTE_CAPABILITIES",
235-
"CUDA_PATH",
236-
"GCOV",
237-
"HOMEBREW_RUBY_PATH",
238-
"NO_WHOLE_ARCHIVE_OPTION",
239-
"SYSTEMROOT",
240-
"USE_DYNAMIC_CRT",
241-
"USE_MSVC_WRAPPER",
242-
"VS90COMNTOOLS",
243-
"VS100COMNTOOLS",
244-
"VS110COMNTOOLS",
245-
"VS120COMNTOOLS",
246-
"VS140COMNTOOLS",
247-
],
190+
def _nixpkgs_cc_autoconf_impl(repository_ctx):
191+
# Calling repository_ctx.path() on anything but a regular file
192+
# fails. So the roundabout way to do the same thing is to find
193+
# a regular file we know is in the workspace (i.e. the WORKSPACE
194+
# file itself) and then use dirname to get the path of the workspace
195+
# root.
196+
workspace_file_path = repository_ctx.path(
197+
Label("@nixpkgs_cc_toolchain//:WORKSPACE")
198+
)
199+
workspace_root = repository_ctx.execute(
200+
["dirname", workspace_file_path]
201+
).stdout.rstrip()
202+
203+
# Make a list of all available tools in the Nix derivation. Override
204+
# the Bazel autoconfiguration with the tools we found.
205+
res = repository_ctx.execute(["ls", workspace_root + "/bin"])
206+
if res.return_code == 0:
207+
overriden_tools = {
208+
tool: repository_ctx.path(Label("@nixpkgs_cc_toolchain//:bin/" + tool))
209+
for tool in res.stdout.splitlines()
210+
}
211+
cc_autoconf_impl(repository_ctx, overriden_tools = overriden_tools)
212+
else:
213+
fail("Cannot list content of @nixpkgs_cc_toolchain:" + res.stderr)
214+
215+
_nixpkgs_cc_autoconf = repository_rule(
216+
implementation = _nixpkgs_cc_autoconf_impl
248217
)
249-
"""Overwrite cc toolchain by supplying custom `gcc` and `ld` (e.g. from
250-
Nix). This allows to fix mismatch of `gcc` versions between what is used by
251-
packages that come from Nix (e.g. `ghc`) and what Bazel detects
252-
automatically (i.e. system-level `gcc`).
253-
"""
218+
219+
def nixpkgs_cc_configure(
220+
repository = None,
221+
repositories = None,
222+
nix_file_content = """
223+
with import <nixpkgs> {}; buildEnv {
224+
name = "bazel-cc-toolchain";
225+
paths = [ gcc binutils ];
226+
}
227+
"""):
228+
"""Use a CC toolchain from Nixpkgs.
229+
230+
By default, Bazel auto-configures a CC toolchain from commands (e.g.
231+
`gcc`) available in the environment. To make builds more hermetic, use
232+
this rule to specific explicitly which commands the toolchain should
233+
use.
234+
"""
235+
if repository and repositories or not repository and not repositories:
236+
fail("Specify one of repository or repositories (but not both).")
237+
elif repository:
238+
repositories = {"nixpkgs": repository}
239+
240+
nixpkgs_package(
241+
name = "nixpkgs_cc_toolchain",
242+
repositories = repositories,
243+
nix_file_content = nix_file_content,
244+
build_file_content = """exports_files(glob(["bin/*"]))""",
245+
)
246+
_nixpkgs_cc_autoconf(name = "local_config_cc")
247+
native.bind(name = "cc_toolchain", actual = "@local_config_cc//:toolchain")
248+
native.register_toolchains("@local_config_cc//:all")

tests/BUILD

+6
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,9 @@ package(default_testonly = 1)
4747
timeout = "short",
4848
)
4949
]
50+
51+
# Test nixpkgs_cc_configure() by building some CC code.
52+
cc_binary(
53+
name = "cc-test",
54+
srcs = ["cc-test.cc"],
55+
)

tests/cc-test.cc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int main() { return 0; }

0 commit comments

Comments
 (0)