Skip to content

Commit

Permalink
Use bintools from pkgs.stdenv.cc instead of pkgs.binutils
Browse files Browse the repository at this point in the history
The `bintools` attribute of a compiler derivation contains the corresponding
binutils or cctools.

Add some documentation and example code to explain how to use
`nixpkgs_cc_configure`.
  • Loading branch information
avdv committed May 18, 2022
1 parent 5aa0b34 commit 4a02190
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 12 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,37 @@ tools. Tools that aren't found are replaced by `${coreutils}/bin/false`.
You can inspect the resulting `@<name>_info//:CC_TOOLCHAIN_INFO` to see
which tools were discovered.

If you specify the `nix_file` or `nix_file_content` argument, the CC
toolchain is discovered by evaluating the corresponding expression. In
addition, you may use the `attribute_path` argument to select an attribute
from the result of the expression to use as the CC toolchain (see example below).

If neither the `nix_file` nor `nix_file_content` argument is used, the
toolchain is discovered from the `stdenv.cc` and the `stdenv.cc.bintools`
attributes of the given `<nixpkgs>` repository.

```
# use GCC 11
nixpkgs_cc_configure(
repository = "@nixpkgs",
nix_file_content = "(import <nixpkgs> {}).gcc11",
)
```
```
# use GCC 11 (same result as above)
nixpkgs_cc_configure(
repository = "@nixpkgs",
attribute_path = "gcc11",
nix_file_content = "import <nixpkgs> {}",
)
```
```
# use the `stdenv.cc` compiler (the default of the given @nixpkgs repository)
nixpkgs_cc_configure(
repository = "@nixpkgs",
)
```

This rule depends on [`rules_cc`](https://github.com/bazelbuild/rules_cc).

**Note:**
Expand Down
31 changes: 31 additions & 0 deletions toolchains/cc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,37 @@ tools. Tools that aren't found are replaced by `${coreutils}/bin/false`.
You can inspect the resulting `@<name>_info//:CC_TOOLCHAIN_INFO` to see
which tools were discovered.

If you specify the `nix_file` or `nix_file_content` argument, the CC
toolchain is discovered by evaluating the corresponding expression. In
addition, you may use the `attribute_path` argument to select an attribute
from the result of the expression to use as the CC toolchain (see example below).

If neither the `nix_file` nor `nix_file_content` argument is used, the
toolchain is discovered from the `stdenv.cc` and the `stdenv.cc.bintools`
attributes of the given `<nixpkgs>` repository.

```
# use GCC 11
nixpkgs_cc_configure(
repository = "@nixpkgs",
nix_file_content = "(import <nixpkgs> {}).gcc11",
)
```
```
# use GCC 11 (same result as above)
nixpkgs_cc_configure(
repository = "@nixpkgs",
attribute_path = "gcc11",
nix_file_content = "import <nixpkgs> {}",
)
```
```
# use the `stdenv.cc` compiler (the default of the given @nixpkgs repository)
nixpkgs_cc_configure(
repository = "@nixpkgs",
)
```

This rule depends on [`rules_cc`](https://github.com/bazelbuild/rules_cc).

**Note:**
Expand Down
31 changes: 31 additions & 0 deletions toolchains/cc/cc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,37 @@ def nixpkgs_cc_configure(
You can inspect the resulting `@<name>_info//:CC_TOOLCHAIN_INFO` to see
which tools were discovered.
If you specify the `nix_file` or `nix_file_content` argument, the CC
toolchain is discovered by evaluating the corresponding expression. In
addition, you may use the `attribute_path` argument to select an attribute
from the result of the expression to use as the CC toolchain (see example below).
If neither the `nix_file` nor `nix_file_content` argument is used, the
toolchain is discovered from the `stdenv.cc` and the `stdenv.cc.bintools`
attributes of the given `<nixpkgs>` repository.
```
# use GCC 11
nixpkgs_cc_configure(
repository = "@nixpkgs",
nix_file_content = "(import <nixpkgs> {}).gcc11",
)
```
```
# use GCC 11 (same result as above)
nixpkgs_cc_configure(
repository = "@nixpkgs",
attribute_path = "gcc11",
nix_file_content = "import <nixpkgs> {}",
)
```
```
# use the `stdenv.cc` compiler (the default of the given @nixpkgs repository)
nixpkgs_cc_configure(
repository = "@nixpkgs",
)
```
This rule depends on [`rules_cc`](https://github.com/bazelbuild/rules_cc).
**Note:**
Expand Down
15 changes: 3 additions & 12 deletions toolchains/cc/cc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,16 @@ let
else
pkgs.buildEnv (
let
paths =
if pkgs.stdenv.isDarwin then
{
inherit (darwinCC) cc bintools;
}
else
{
cc = pkgs.stdenv.cc;
bintools = pkgs.binutils;
};
cc = if pkgs.stdenv.isDarwin then darwinCC else pkgs.stdenv.cc;
in
{
name = "bazel-nixpkgs-cc";
# XXX: `gcov` is missing in `/bin`.
# It exists in `stdenv.cc.cc` but that collides with `stdenv.cc`.
paths = [ paths.cc paths.bintools ];
paths = [ cc cc.bintools ];
pathsToLink = [ "/bin" ];
passthru = {
isClang = paths.cc.isClang;
isClang = cc.isClang;
};
}
)
Expand Down

0 comments on commit 4a02190

Please sign in to comment.