Skip to content

Commit

Permalink
treewide: Use pkgs/build-support/roles.bash to remove copy pasta
Browse files Browse the repository at this point in the history
Also fix some setup hooks that unnecessarily used environment hooks,
which revolted in the same variable being modified too many times.
  • Loading branch information
Ericson2314 committed May 7, 2018
1 parent 34a3233 commit 2110c0b
Show file tree
Hide file tree
Showing 29 changed files with 203 additions and 261 deletions.
7 changes: 6 additions & 1 deletion pkgs/build-support/bintools-wrapper/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,12 @@ stdenv.mkDerivation {

depsTargetTargetPropagated = extraPackages;

setupHook = ./setup-hook.sh;
wrapperName = "BINTOOLS_WRAPPER";

setupHooks = [
../setup-hooks/role.bash
./setup-hook.sh
];

postFixup =
''
Expand Down
38 changes: 8 additions & 30 deletions pkgs/build-support/bintools-wrapper/setup-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,22 @@ set -u
[[ -z ${crossConfig-} ]] || (( "$hostOffset" < 0 )) || return 0

bintoolsWrapper_addLDVars () {
case $depHostOffset in
-1) local role='BUILD_' ;;
0) local role='' ;;
1) local role='TARGET_' ;;
*) echo "bintools-wrapper: Error: Cannot be used with $depHostOffset-offset deps" >2;
return 1 ;;
esac
# See ../setup-hooks/role.bash
local role_post role_pre
getTargetRoleEnvHook

if [[ -d "$1/lib64" && ! -L "$1/lib64" ]]; then
export NIX_${role}LDFLAGS+=" -L$1/lib64"
export NIX_${role_pre}LDFLAGS+=" -L$1/lib64"
fi

if [[ -d "$1/lib" ]]; then
export NIX_${role}LDFLAGS+=" -L$1/lib"
export NIX_${role_pre}LDFLAGS+=" -L$1/lib"
fi
}

case $targetOffset in
-1)
export NIX_BINTOOLS_WRAPPER_@infixSalt@_TARGET_BUILD=1
role_pre='BUILD_'
role_post='_FOR_BUILD'
;;
0)
export NIX_BINTOOLS_WRAPPER_@infixSalt@_TARGET_HOST=1
role_pre=''
role_post=''
;;
1)
export NIX_BINTOOLS_WRAPPER_@infixSalt@_TARGET_TARGET=1
role_pre='TARGET_'
role_post='_FOR_TARGET'
;;
*)
echo "cc-wrapper: used as improper sort of dependency" >2;
return 1
;;
esac
# See ../setup-hooks/role.bash
getTargetRole
getTargetRoleWrapper

addEnvHooks "$targetOffset" bintoolsWrapper_addLDVars

Expand Down
7 changes: 6 additions & 1 deletion pkgs/build-support/cc-wrapper/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,12 @@ stdenv.mkDerivation {
propagatedBuildInputs = [ bintools ];
depsTargetTargetPropagated = extraPackages;

setupHook = ./setup-hook.sh;
wrapperName = "CC_WRAPPER";

setupHooks = [
../setup-hooks/role.bash
./setup-hook.sh
];

postFixup =
''
Expand Down
49 changes: 8 additions & 41 deletions pkgs/build-support/cc-wrapper/setup-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -66,55 +66,22 @@ set -u
# over no state, and there's no @-substitutions within, so any redefined
# function is guaranteed to be exactly the same.
ccWrapper_addCVars () {
# The `depHostOffset` describes how the host platform of the dependencies
# are slid relative to the depending package. It is brought into scope of
# the environment hook defined as the role of the dependency being applied.
case $depHostOffset in
-1) local role='BUILD_' ;;
0) local role='' ;;
1) local role='TARGET_' ;;
*) echo "cc-wrapper: Error: Cannot be used with $depHostOffset-offset deps" >2;
return 1 ;;
esac
# See ../setup-hooks/role.bash
local role_post role_pre
getTargetRoleEnvHook

This comment has been minimized.

Copy link
@ElvishJerricco

ElvishJerricco Jun 10, 2018

Contributor

@Ericson2314 I'm not sure what's correct, but this is definitely a change in behavior. Previously, we checked depHostOffset, and now we check depTargetOffset. The symptom I'm seeing of this is that a library depended on in buildInputs is not having its include directory added to the C flags.

This comment has been minimized.

Copy link
@ElvishJerricco

ElvishJerricco Jun 10, 2018

Contributor

Ah, I see this is changed on staging. In the mean time, master is largely unusable for cross compilation. Does that mean we should merge staging soon?


if [[ -d "$1/include" ]]; then
export NIX_${role}CFLAGS_COMPILE+=" ${ccIncludeFlag:--isystem} $1/include"
export NIX_${role_pre}CFLAGS_COMPILE+=" ${ccIncludeFlag:--isystem} $1/include"
fi

if [[ -d "$1/Library/Frameworks" ]]; then
export NIX_${role}CFLAGS_COMPILE+=" -F$1/Library/Frameworks"
export NIX_${role_pre}CFLAGS_COMPILE+=" -F$1/Library/Frameworks"
fi
}

# Since the same cc-wrapper derivation can be depend on in multiple ways, we
# need to accumulate *each* role (i.e. target platform relative the depending
# derivation) in which the cc-wrapper derivation is used.
# `NIX_CC_WRAPPER_@infixSalt@_TARGET_*` tracks this (needs to be an exported env
# var so can't use fancier data structures).
#
# We also need to worry about what role is being added on *this* invocation of
# setup-hook, which `role` tracks.
case $targetOffset in
-1)
export NIX_CC_WRAPPER_@infixSalt@_TARGET_BUILD=1
role_pre='BUILD_'
role_post='_FOR_BUILD'
;;
0)
export NIX_CC_WRAPPER_@infixSalt@_TARGET_HOST=1
role_pre=''
role_post=''
;;
1)
export NIX_CC_WRAPPER_@infixSalt@_TARGET_TARGET=1
role_pre='TARGET_'
role_post='_FOR_TARGET'
;;
*)
echo "cc-wrapper: used as improper sort of dependency" >2;
return 1
;;
esac
# See ../setup-hooks/role.bash
getTargetRole
getTargetRoleWrapper

# We use the `targetOffset` to choose the right env hook to accumulate the right
# sort of deps (those with that offset).
Expand Down
75 changes: 75 additions & 0 deletions pkgs/build-support/setup-hooks/role.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Since the same derivation can be depend on in multiple ways, we need to
# accumulate *each* role (i.e. host and target platforms relative the depending
# derivation) in which the derivation is used.
#
# The role is intened to be use as part of other variables names like
# - $NIX_${role_pre}_SOMETHING
# - $NIX_SOMETHING_${role_post}

function getRole() {
case $1 in
-1)
role_pre='BUILD_'
role_post='_FOR_BUILD'
;;
0)
role_pre=''
role_post=''
;;
1)
role_pre='TARGET_'
role_post='_FOR_TARGET'
;;
*)
echo "@name@: used as improper sort of dependency" >2
return 1
;;
esac
}

# `hostOffset` describes how the host platform of the package is slid relative
# to the depending package. `targetOffset` likewise describes the target
# platform of the package. Both are brought into scope of the setup hook defined
# for dependency whose setup hook is being processed relative to the package
# being built.

function getHostRole() {
getRole "$hostOffset"
}
function getTargetRole() {
getRole "$targetOffset"
}

# `depHostOffset` describes how the host platform of the dependencies are slid
# relative to the depending package. `depTargetOffset` likewise describes the
# target platform of dependenices. Both are brought into scope of the
# environment hook defined for the dependency being applied relative to the
# package being built.

function getHostRoleEnvHook() {
getRole "$depHostOffset"
}
function getTargetRoleEnvHook() {
getRole "$depTargetOffset"
}

# This variant is inteneded specifically for code-prodocing tool wrapper scripts
# `NIX_@wrapperName@_@infixSalt@_TARGET_*` tracks this (needs to be an exported
# env var so can't use fancier data structures).
function getTargetRoleWrapper() {
case $targetOffset in
-1)
export NIX_@wrapperName@_@infixSalt@_TARGET_BUILD=1
;;
0)
export NIX_@wrapperName@_@infixSalt@_TARGET_HOST=1
;;
1)
export NIX_@wrapperName@_@infixSalt@_TARGET_TARGET=1
;;
*)
echo "@name@: used as improper sort of dependency" >2
return 1
;;
esac
}
16 changes: 4 additions & 12 deletions pkgs/development/compilers/gcc/libstdc++-hook.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
# The `hostOffset` describes how the host platform of the dependencies are slid
# relative to the depending package. It is brought into scope of the setup hook
# defined as the role of the dependency whose hooks is being run.
case $hostOffset in
-1) local role='BUILD_' ;;
0) local role='' ;;
1) local role='TARGET_' ;;
*) echo "cc-wrapper: Error: Cannot be used with $hostOffset-offset deps" >2;
return 1 ;;
esac
# See pkgs/build-support/setup-hooks/role.bash
getHostRole

export NIX_${role}CXXSTDLIB_COMPILE+=" -isystem $(echo -n @gcc@/include/c++/*) -isystem $(echo -n @gcc@/include/c++/*)/$(@gcc@/bin/gcc -dumpmachine)"
export NIX_${role}CXXSTDLIB_LINK=" -stdlib=libstdc++"
export NIX_${role_pre}CXXSTDLIB_COMPILE+=" -isystem $(echo -n @gcc@/include/c++/*) -isystem $(echo -n @gcc@/include/c++/*)/$(@gcc@/bin/gcc -dumpmachine)"
export NIX_${role_pre}CXXSTDLIB_LINK=" -stdlib=libstdc++"
5 changes: 4 additions & 1 deletion pkgs/development/compilers/llvm/3.5/libc++/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ stdenv.mkDerivation rec {

linkCxxAbi = stdenv.isLinux;

setupHook = ./setup-hook.sh;
setupHooks = [
../../../../../build-support/setup-hooks/role.bash
./setup-hook.sh
];

meta = {
homepage = http://libcxx.llvm.org/;
Expand Down
16 changes: 4 additions & 12 deletions pkgs/development/compilers/llvm/3.5/libc++/setup-hook.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
# The `hostOffset` describes how the host platform of the dependencies are slid
# relative to the depending package. It is brought into scope of the setup hook
# defined as the role of the dependency whose hooks is being run.
case $hostOffset in
-1) local role='BUILD_' ;;
0) local role='' ;;
1) local role='TARGET_' ;;
*) echo "cc-wrapper: Error: Cannot be used with $hostOffset-offset deps" >2;
return 1 ;;
esac
# See pkgs/build-support/setup-hooks/role.bash
getHostRole

linkCxxAbi="@linkCxxAbi@"
export NIX_${role}CXXSTDLIB_COMPILE+=" -isystem @out@/include/c++/v1"
export NIX_${role}CXXSTDLIB_LINK=" -stdlib=libc++${linkCxxAbi:+" -lc++abi"}"
export NIX_${role_pre}CXXSTDLIB_COMPILE+=" -isystem @out@/include/c++/v1"
export NIX_${role_pre}CXXSTDLIB_LINK=" -stdlib=libc++${linkCxxAbi:+" -lc++abi"}"
5 changes: 4 additions & 1 deletion pkgs/development/compilers/llvm/3.7/libc++/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ stdenv.mkDerivation rec {

linkCxxAbi = stdenv.isLinux;

setupHook = ./setup-hook.sh;
setupHooks = [
../../../../../build-support/setup-hooks/role.bash
./setup-hook.sh
];

meta = {
homepage = http://libcxx.llvm.org/;
Expand Down
16 changes: 4 additions & 12 deletions pkgs/development/compilers/llvm/3.7/libc++/setup-hook.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
# The `hostOffset` describes how the host platform of the dependencies are slid
# relative to the depending package. It is brought into scope of the setup hook
# defined as the role of the dependency whose hooks is being run.
case $hostOffset in
-1) local role='BUILD_' ;;
0) local role='' ;;
1) local role='TARGET_' ;;
*) echo "cc-wrapper: Error: Cannot be used with $hostOffset-offset deps" >2;
return 1 ;;
esac
# See pkgs/build-support/setup-hooks/role.bash
getHostRole

linkCxxAbi="@linkCxxAbi@"
export NIX_${role}CXXSTDLIB_COMPILE+=" -isystem @out@/include/c++/v1"
export NIX_${role}CXXSTDLIB_LINK=" -stdlib=libc++${linkCxxAbi:+" -lc++abi"}"
export NIX_${role_pre}CXXSTDLIB_COMPILE+=" -isystem @out@/include/c++/v1"
export NIX_${role_pre}CXXSTDLIB_LINK=" -stdlib=libc++${linkCxxAbi:+" -lc++abi"}"
5 changes: 4 additions & 1 deletion pkgs/development/compilers/llvm/3.8/libc++/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ stdenv.mkDerivation rec {

linkCxxAbi = stdenv.isLinux;

setupHook = ./setup-hook.sh;
setupHooks = [
../../../../../build-support/setup-hooks/role.bash
./setup-hook.sh
];

meta = {
homepage = http://libcxx.llvm.org/;
Expand Down
16 changes: 4 additions & 12 deletions pkgs/development/compilers/llvm/3.8/libc++/setup-hook.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
# The `hostOffset` describes how the host platform of the dependencies
# relative to the depending package. It is brought into scope of the setup hook
# defined as the role of the dependency whose hooks is being run.
case $hostOffset in
-1) local role='BUILD_' ;;
0) local role='' ;;
1) local role='TARGET_' ;;
*) echo "cc-wrapper: Error: Cannot be used with $hostOffset-offset deps" >2;
return 1 ;;
esac
# See pkgs/build-support/setup-hooks/role.bash
getHostRole

linkCxxAbi="@linkCxxAbi@"
export NIX_${role}CXXSTDLIB_COMPILE+=" -isystem @out@/include/c++/v1"
export NIX_${role}CXXSTDLIB_LINK=" -stdlib=libc++${linkCxxAbi:+" -lc++abi"}"
export NIX_${role_pre}CXXSTDLIB_COMPILE+=" -isystem @out@/include/c++/v1"
export NIX_${role_pre}CXXSTDLIB_LINK=" -stdlib=libc++${linkCxxAbi:+" -lc++abi"}"
5 changes: 4 additions & 1 deletion pkgs/development/compilers/llvm/3.9/libc++/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ stdenv.mkDerivation rec {

linkCxxAbi = stdenv.isLinux;

setupHook = ./setup-hook.sh;
setupHooks = [
../../../../../build-support/setup-hooks/role.bash
./setup-hook.sh
];

meta = {
homepage = http://libcxx.llvm.org/;
Expand Down
16 changes: 4 additions & 12 deletions pkgs/development/compilers/llvm/3.9/libc++/setup-hook.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
# The `hostOffset` describes how the host platform of the dependencies
# relative to the depending package. It is brought into scope of the setup hook
# defined as the role of the dependency whose hooks is being run.
case $hostOffset in
-1) local role='BUILD_' ;;
0) local role='' ;;
1) local role='TARGET_' ;;
*) echo "cc-wrapper: Error: Cannot be used with $hostOffset-offset deps" >2;
return 1 ;;
esac
# See pkgs/build-support/setup-hooks/role.bash
getHostRole

linkCxxAbi="@linkCxxAbi@"
export NIX_${role}CXXSTDLIB_COMPILE+=" -isystem @out@/include/c++/v1"
export NIX_${role}CXXSTDLIB_LINK=" -stdlib=libc++${linkCxxAbi:+" -lc++abi"}"
export NIX_${role_pre}CXXSTDLIB_COMPILE+=" -isystem @out@/include/c++/v1"
export NIX_${role_pre}CXXSTDLIB_LINK=" -stdlib=libc++${linkCxxAbi:+" -lc++abi"}"
5 changes: 4 additions & 1 deletion pkgs/development/compilers/llvm/4/libc++/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ stdenv.mkDerivation rec {

linkCxxAbi = stdenv.isLinux;

setupHook = ./setup-hook.sh;
setupHooks = [
../../../../../build-support/setup-hooks/role.bash
./setup-hook.sh
];

meta = {
homepage = http://libcxx.llvm.org/;
Expand Down
16 changes: 4 additions & 12 deletions pkgs/development/compilers/llvm/4/libc++/setup-hook.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
# The `hostOffset` describes how the host platform of the dependencies
# relative to the depending package. It is brought into scope of the setup hook
# defined as the role of the dependency whose hooks is being run.
case $hostOffset in
-1) local role='BUILD_' ;;
0) local role='' ;;
1) local role='TARGET_' ;;
*) echo "cc-wrapper: Error: Cannot be used with $hostOffset-offset deps" >2;
return 1 ;;
esac
# See pkgs/build-support/setup-hooks/role.bash
getHostRole

linkCxxAbi="@linkCxxAbi@"
export NIX_${role}CXXSTDLIB_COMPILE+=" -isystem @out@/include/c++/v1"
export NIX_${role}CXXSTDLIB_LINK=" -stdlib=libc++${linkCxxAbi:+" -lc++abi"}"
export NIX_${role_pre}CXXSTDLIB_COMPILE+=" -isystem @out@/include/c++/v1"
export NIX_${role_pre}CXXSTDLIB_LINK=" -stdlib=libc++${linkCxxAbi:+" -lc++abi"}"
Loading

2 comments on commit 2110c0b

@ElvishJerricco
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit broke cross compiling acl, which breaks cross compiling NixOS. Investigating...

@Ericson2314
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did the wrappers wrong. There is a staging commit fixing it.

Please sign in to comment.