···11+# CC Wrapper hygiene
22+#
33+# For at least cross compilation, we need to depend on multiple cc-wrappers at
44+# once---specifically up to one per sort of dependency. This follows from having
55+# different tools targeting different platforms, and different flags for those
66+# tools. For example:
77+#
88+# # Flags for compiling (whether or not linking) C code for the...
99+# NIX_CFLAGS_COMPILE_FOR_BUILD # ...build platform
1010+# NIX_CFLAGS_COMPILE # ...host platform
1111+# NIX_CFLAGS_COMPILE_FOR_TARGET # ...target platform
1212+#
1313+# Notice that these platforms are the 3 *relative* to the package using
1414+# cc-wrapper, not absolute like `x86_64-pc-linux-gnu`.
1515+#
1616+# The simplest solution would be to have separate cc-wrappers per (3 intended
1717+# use-cases * n absolute concrete platforms). For the use-case axis, we would
1818+# @-splice in 'BUILD_' '' 'TARGET_' to use the write environment variables when
1919+# building the cc-wrapper, and likewise prefix the binaries' names so they didn't
2020+# clobber each other on the PATH. But the need for 3x cc-wrappers, along with
2121+# non-standard name prefixes, is annoying and liable to break packages' build
2222+# systems.
2323+#
2424+# Instead, we opt to have just one cc-wrapper per absolute platform. Matching
2525+# convention, the binaries' names can just be prefixed with their target
2626+# platform. On the other hand, that means packages will depend on not just
2727+# multiple cc-wrappers, but the exact same cc-wrapper derivation multiple ways.
2828+# That means the exact same cc-wrapper derivation must be able to avoid
2929+# conflicting with itself, despite the fact that `setup-hook.sh`, the `addCvars`
3030+# function, and `add-flags.sh` are all communicating with each other with
3131+# environment variables. Yuck.
3232+#
3333+# The basic strategy is:
3434+#
3535+# - Everyone exclusively *adds information* to relative-platform-specific
3636+# environment variables, like `NIX_CFLAGS_COMPILE_FOR_TARGET`, to communicate
3737+# with the wrapped binaries.
3838+#
3939+# - The wrapped binaries will exclusively *read* cc-wrapper-derivation-specific
4040+# environment variables distinguished with with `suffixSalt`, like
4141+# `NIX_CFLAGS_COMPILE_@suffixSalt@`.
4242+#
4343+# - `add-flags`, beyond its old task of reading extra flags stuck inside the
4444+# cc-wrapper derivation, will convert the relative-platform-specific
4545+# variables to cc-wrapper-derivation-specific variables. This conversion is
4646+# the only time all but one of the cc-wrapper-derivation-specific variables
4747+# are set.
4848+#
4949+# This ensures the flow of information is exclusive from
5050+# relative-platform-specific variables to cc-wrapper-derivation-specific
5151+# variables. This allows us to support the general case of a many--many relation
5252+# between relative platforms and cc-wrapper derivations.
5353+#
5454+# For more details, read the individual files where the mechanisms used to
5555+# accomplish this will be individually documented.
5656+5757+# Skip setup hook if we're neither a build-time dep, nor, temporarily, doing a
5858+# native compile.
5959+#
6060+# TODO(@Ericson2314): No native exception
6161+[[ -z ${strictDeps-} ]] || (( "$hostOffset" < 0 )) || return 0
6262+6363+# It's fine that any other cc-wrapper will redefine this. Bash functions close
6464+# over no state, and there's no @-substitutions within, so any redefined
6565+# function is guaranteed to be exactly the same.
6666+ccWrapper_addCVars () {
6767+ # See ../setup-hooks/role.bash
6868+ local role_post
6969+ getHostRoleEnvHook
7070+7171+ if [ -d "$1/include" ]; then
7272+ export NIX_CFLAGS_COMPILE${role_post}+=" -isystem $1/include"
7373+ fi
7474+7575+ if [ -d "$1/Library/Frameworks" ]; then
7676+ export NIX_CFLAGS_COMPILE${role_post}+=" -iframework $1/Library/Frameworks"
7777+ fi
7878+}
7979+8080+# See ../setup-hooks/role.bash
8181+getTargetRole
8282+getTargetRoleWrapper
8383+8484+# We use the `targetOffset` to choose the right env hook to accumulate the right
8585+# sort of deps (those with that offset).
8686+addEnvHooks "$targetOffset" ccWrapper_addCVars
8787+8888+# Note 1: these come *after* $out in the PATH (see setup.sh).
8989+# Note 2: phase separation makes this look useless to shellcheck.
9090+9191+# shellcheck disable=SC2157
9292+if [ -n "@cc@" ]; then
9393+ addToSearchPath _PATH @cc@/bin
9494+fi
9595+9696+# shellcheck disable=SC2157
9797+if [ -n "@libc_bin@" ]; then
9898+ addToSearchPath _PATH @libc_bin@/bin
9999+fi
100100+101101+# shellcheck disable=SC2157
102102+if [ -n "@coreutils_bin@" ]; then
103103+ addToSearchPath _PATH @coreutils_bin@/bin
104104+fi
105105+106106+# Export tool environment variables so various build systems use the right ones.
107107+108108+export NIX_CC${role_post}=@out@
109109+110110+export CC${role_post}=@named_cc@
111111+export CXX${role_post}=@named_cxx@
112112+export CC${role_post}=@named_cc@
113113+export CXX${role_post}=@named_cxx@
114114+115115+# If unset, assume the default hardening flags.
116116+: ${NIX_HARDENING_ENABLE="fortify stackprotector pic strictoverflow format relro bindnow"}
117117+export NIX_HARDENING_ENABLE
118118+119119+# No local scope in sourced file
120120+unset -v role_post