Skip to content

Commit

Permalink
Merge pull request tweag#227 from tweag/nix-support-and-code-signing
Browse files Browse the repository at this point in the history
Use `nix-support/cc-{c,ld}flags` files to configure compiler flags
  • Loading branch information
mergify[bot] authored May 19, 2022
2 parents 2abe40d + 0c4a632 commit 4ebcf6f
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 38 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
71 changes: 33 additions & 38 deletions toolchains/cc/cc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,40 @@ in
}:

let
# The original `postLinkSignHook` from nixpkgs assumes `codesign_allocate` is
# in the PATH which is not the case when using our cc_wrapper. Set
# `CODESIGN_ALLOCATE` to an absolute path here and override the hook for
# `darwinCC` below.
postLinkSignHook =
with pkgs; writeTextFile {
name = "post-link-sign-hook";
executable = true;

text = ''
CODESIGN_ALLOCATE=${darwin.cctools}/bin/codesign_allocate \
${darwin.sigtool}/bin/codesign -f -s - "$linkerOutput"
'';
};
darwinCC =
# Work around https://github.com/NixOS/nixpkgs/issues/42059.
# See also https://github.com/NixOS/nixpkgs/pull/41589.
pkgs.runCommand "bazel-nixpkgs-cc-wrapper"
{
buildInputs = [ pkgs.makeWrapper ];
passthru = {
isClang = pkgs.stdenv.cc.isClang;
};
}
''
mkdir -p $out/bin
for i in ${pkgs.stdenv.cc}/bin/*; do
ln -sf $i $out/bin
done
# Override cc
rm -f $out/bin/cc $out/bin/clang $out/bin/clang++
makeWrapper ${pkgs.stdenv.cc}/bin/cc $out/bin/cc --add-flags \
"-Wno-unused-command-line-argument \
-isystem ${pkgs.llvmPackages.libcxx}/include/c++/v1 \
-F${pkgs.darwin.apple_sdk.frameworks.CoreFoundation}/Library/Frameworks \
-F${pkgs.darwin.apple_sdk.frameworks.CoreServices}/Library/Frameworks \
-F${pkgs.darwin.apple_sdk.frameworks.Security}/Library/Frameworks \
-F${pkgs.darwin.apple_sdk.frameworks.Foundation}/Library/Frameworks \
-L${pkgs.libiconv}/lib \
-L${pkgs.darwin.libobjc}/lib"
pkgs.wrapCCWith rec {
cc = pkgs.stdenv.cc;
bintools = cc.bintools.override { inherit postLinkSignHook; };
extraBuildCommands = with pkgs.darwin.apple_sdk.frameworks; ''
echo "-Wno-unused-command-line-argument" >> $out/nix-support/cc-cflags
echo "-isystem ${pkgs.llvmPackages.libcxx.dev}/include/c++/v1" >> $out/nix-support/cc-cflags
echo "-isystem ${pkgs.llvmPackages.clang-unwrapped.lib}/lib/clang/${cc.version}/include" >> $out/nix-support/cc-cflags
echo "-F${CoreFoundation}/Library/Frameworks" >> $out/nix-support/cc-cflags
echo "-F${CoreServices}/Library/Frameworks" >> $out/nix-support/cc-cflags
echo "-F${Security}/Library/Frameworks" >> $out/nix-support/cc-cflags
echo "-F${Foundation}/Library/Frameworks" >> $out/nix-support/cc-cflags
echo "-L${pkgs.llvmPackages.libcxx}/lib" >> $out/nix-support/cc-cflags
echo "-L${pkgs.llvmPackages.libcxxabi}/lib" >> $out/nix-support/cc-cflags
echo "-L${pkgs.libiconv}/lib" >> $out/nix-support/cc-cflags
echo "-L${pkgs.darwin.libobjc}/lib" >> $out/nix-support/cc-cflags
'';
};
cc =
if ccType == "ccTypeAttribute" then
pkgs.lib.attrByPath (pkgs.lib.splitString "." ccAttrPath) null ccAttrSet
Expand All @@ -46,26 +51,16 @@ let
else
pkgs.buildEnv (
let
paths =
if pkgs.stdenv.isDarwin then
{
cc = (pkgs.overrideCC pkgs.stdenv darwinCC).cc;
binutils = pkgs.darwin.binutils;
}
else
{
cc = pkgs.stdenv.cc;
binutils = 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.binutils ];
paths = [ cc cc.bintools ];
pathsToLink = [ "/bin" ];
passthru = {
isClang = paths.cc.isClang;
isClang = cc.isClang;
};
}
)
Expand Down

0 comments on commit 4ebcf6f

Please sign in to comment.