2017-07-06 21:19:53 +00:00
|
|
|
# CC Wrapper hygiene
|
|
|
|
#
|
|
|
|
# For at least cross compilation, we need to depend on multiple cc-wrappers at
|
|
|
|
# once---specifically up to one per sort of dependency. This follows from having
|
|
|
|
# different tools targeting different platforms, and different flags for those
|
|
|
|
# tools. For example:
|
|
|
|
#
|
|
|
|
# # Flags for compiling (whether or not linking) C code for the...
|
|
|
|
# NIX_BUILD_CFLAGS_COMPILE # ...build platform
|
|
|
|
# NIX_CFLAGS_COMPILE # ...host platform
|
|
|
|
# NIX_TARGET_CFLAGS_COMPILE # ...target platform
|
|
|
|
#
|
|
|
|
# Notice that these platforms are the 3 *relative* to the package using
|
|
|
|
# cc-wrapper, not absolute like `x86_64-pc-linux-gnu`.
|
|
|
|
#
|
|
|
|
# The simplest solution would be to have separate cc-wrappers per (3 intended
|
|
|
|
# use-cases * n absolute concrete platforms). For the use-case axis, we would
|
|
|
|
# @-splice in 'BUILD_' '' 'TARGET_' to use the write environment variables when
|
|
|
|
# building the cc-wrapper, and likewise prefix the binaries' names so they didn't
|
|
|
|
# clobber each other on the PATH. But the need for 3x cc-wrappers, along with
|
|
|
|
# non-standard name prefixes, is annoying and liable to break packages' build
|
|
|
|
# systems.
|
|
|
|
#
|
|
|
|
# Instead, we opt to have just one cc-wrapper per absolute platform. Matching
|
|
|
|
# convention, the binaries' names can just be prefixed with their target
|
|
|
|
# platform. On the other hand, that means packages will depend on not just
|
|
|
|
# multiple cc-wrappers, but the exact same cc-wrapper derivation multiple ways.
|
|
|
|
# That means the exact same cc-wrapper derivation must be able to avoid
|
|
|
|
# conflicting with itself, despite the fact that `setup-hook.sh`, the `addCvars`
|
|
|
|
# function, and `add-flags.sh` are all communicating with each other with
|
|
|
|
# environment variables. Yuck.
|
|
|
|
#
|
|
|
|
# The basic strategy is:
|
|
|
|
#
|
|
|
|
# - Everyone exclusively *adds information* to relative-platform-specific
|
|
|
|
# environment variables, like `NIX_TARGET_CFLAGS_COMPILE`, to communicate
|
|
|
|
# with the wrapped binaries.
|
|
|
|
#
|
|
|
|
# - The wrapped binaries will exclusively *read* cc-wrapper-derivation-specific
|
|
|
|
# environment variables distinguished with with `infixSalt`, like
|
|
|
|
# `NIX_@infixSalt@_CFLAGS_COMPILE`.
|
|
|
|
#
|
|
|
|
# - `add-flags`, beyond its old task of reading extra flags stuck inside the
|
|
|
|
# cc-wrapper derivation, will convert the relative-platform-specific
|
|
|
|
# variables to cc-wrapper-derivation-specific variables. This conversion is
|
|
|
|
# the only time all but one of the cc-wrapper-derivation-specific variables
|
|
|
|
# are set.
|
|
|
|
#
|
|
|
|
# This ensures the flow of information is exclusive from
|
|
|
|
# relative-platform-specific variables to cc-wrapper-derivation-specific
|
|
|
|
# variables. This allows us to support the general case of a many--many relation
|
|
|
|
# between relative platforms and cc-wrapper derivations.
|
|
|
|
#
|
|
|
|
# For more details, read the individual files where the mechanisms used to
|
|
|
|
# accomplish this will be individually documented.
|
|
|
|
|
2017-08-03 16:45:06 +00:00
|
|
|
set -u
|
|
|
|
|
2017-08-28 15:33:08 +00:00
|
|
|
# Skip setup hook if we're neither a build-time dep, nor, temporarily, doing a
|
|
|
|
# native compile.
|
|
|
|
#
|
|
|
|
# TODO(@Ericson2314): No native exception
|
2018-05-13 15:31:24 +00:00
|
|
|
[[ -z ${strictDeps-} ]] || (( "$hostOffset" < 0 )) || return 0
|
2017-07-06 21:19:53 +00:00
|
|
|
|
|
|
|
# It's fine that any other cc-wrapper will redefine this. Bash functions close
|
|
|
|
# over no state, and there's no @-substitutions within, so any redefined
|
|
|
|
# function is guaranteed to be exactly the same.
|
2017-06-26 04:43:06 +00:00
|
|
|
ccWrapper_addCVars () {
|
2018-05-07 17:07:19 +00:00
|
|
|
# See ../setup-hooks/role.bash
|
|
|
|
local role_post role_pre
|
2018-06-01 23:42:05 +00:00
|
|
|
getHostRoleEnvHook
|
2017-07-06 21:19:53 +00:00
|
|
|
|
2017-08-02 16:48:51 +00:00
|
|
|
if [[ -d "$1/include" ]]; then
|
2018-05-07 17:07:19 +00:00
|
|
|
export NIX_${role_pre}CFLAGS_COMPILE+=" ${ccIncludeFlag:--isystem} $1/include"
|
2008-06-26 11:07:46 +00:00
|
|
|
fi
|
|
|
|
|
2017-08-02 16:48:51 +00:00
|
|
|
if [[ -d "$1/Library/Frameworks" ]]; then
|
2018-05-07 17:07:19 +00:00
|
|
|
export NIX_${role_pre}CFLAGS_COMPILE+=" -F$1/Library/Frameworks"
|
2015-02-08 18:36:13 +00:00
|
|
|
fi
|
2008-06-26 11:07:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 17:07:19 +00:00
|
|
|
# See ../setup-hooks/role.bash
|
|
|
|
getTargetRole
|
|
|
|
getTargetRoleWrapper
|
2017-07-06 21:19:53 +00:00
|
|
|
|
2017-08-03 16:45:06 +00:00
|
|
|
# We use the `targetOffset` to choose the right env hook to accumulate the right
|
|
|
|
# sort of deps (those with that offset).
|
|
|
|
addEnvHooks "$targetOffset" ccWrapper_addCVars
|
2008-06-26 11:07:46 +00:00
|
|
|
|
2017-08-02 16:48:51 +00:00
|
|
|
# Note 1: these come *after* $out in the PATH (see setup.sh).
|
|
|
|
# Note 2: phase separation makes this look useless to shellcheck.
|
2008-06-26 11:07:46 +00:00
|
|
|
|
2017-08-02 16:48:51 +00:00
|
|
|
# shellcheck disable=SC2157
|
2015-01-09 19:22:12 +00:00
|
|
|
if [ -n "@cc@" ]; then
|
2016-02-28 00:13:15 +00:00
|
|
|
addToSearchPath _PATH @cc@/bin
|
2008-06-26 11:07:46 +00:00
|
|
|
fi
|
|
|
|
|
2017-08-02 16:48:51 +00:00
|
|
|
# shellcheck disable=SC2157
|
2015-04-18 09:00:58 +00:00
|
|
|
if [ -n "@libc_bin@" ]; then
|
2016-04-01 08:06:01 +00:00
|
|
|
addToSearchPath _PATH @libc_bin@/bin
|
2008-06-26 11:07:46 +00:00
|
|
|
fi
|
2009-11-08 00:32:12 +00:00
|
|
|
|
2017-08-02 16:48:51 +00:00
|
|
|
# shellcheck disable=SC2157
|
2015-04-18 09:00:58 +00:00
|
|
|
if [ -n "@coreutils_bin@" ]; then
|
2016-04-01 08:06:01 +00:00
|
|
|
addToSearchPath _PATH @coreutils_bin@/bin
|
2009-11-08 00:32:12 +00:00
|
|
|
fi
|
2014-12-17 18:11:30 +00:00
|
|
|
|
2017-07-06 21:19:53 +00:00
|
|
|
# Export tool environment variables so various build systems use the right ones.
|
2017-06-26 05:17:09 +00:00
|
|
|
|
2017-08-21 17:55:39 +00:00
|
|
|
export NIX_${role_pre}CC=@out@
|
2017-06-26 05:17:09 +00:00
|
|
|
|
2018-08-06 22:37:24 +00:00
|
|
|
export ${role_pre}CC=@out@/bin/@named_cc@
|
|
|
|
export ${role_pre}CXX=@out@/bin/@named_cxx@
|
|
|
|
export CC${role_post}=@out@/bin/@named_cc@
|
|
|
|
export CXX${role_post}=@out@/bin/@named_cxx@
|
2017-06-26 05:17:09 +00:00
|
|
|
|
2018-04-11 18:00:13 +00:00
|
|
|
# If unset, assume the default hardening flags.
|
|
|
|
: ${NIX_HARDENING_ENABLE="fortify stackprotector pic strictoverflow format relro bindnow"}
|
|
|
|
export NIX_HARDENING_ENABLE
|
|
|
|
|
2017-07-06 21:19:53 +00:00
|
|
|
# No local scope in sourced file
|
2017-08-28 18:56:08 +00:00
|
|
|
unset -v role_pre role_post
|
2017-08-03 16:45:06 +00:00
|
|
|
set +u
|