Merge pull request #309460 from r-ryantm/auto-update/home-manager
[NixPkgs.git] / pkgs / build-support / cc-wrapper / gnat-wrapper.sh
blobe75eb3eb1ebf6a4a60ab171174164fe5fdcc48c8
1 #! @shell@
2 set -eu -o pipefail +o posix
3 shopt -s nullglob
5 if (( "${NIX_DEBUG:-0}" >= 7 )); then
6 set -x
7 fi
9 path_backup="$PATH"
11 # That @-vars are substituted separately from bash evaluation makes
12 # shellcheck think this, and others like it, are useless conditionals.
13 # shellcheck disable=SC2157
14 if [[ -n "@coreutils_bin@" && -n "@gnugrep_bin@" ]]; then
15 PATH="@coreutils_bin@/bin:@gnugrep_bin@/bin"
18 cInclude=0
20 source @out@/nix-support/utils.bash
22 # Flirting with a layer violation here.
23 if [ -z "${NIX_BINTOOLS_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then
24 source @bintools@/nix-support/add-flags.sh
27 # Put this one second so libc ldflags take priority.
28 if [ -z "${NIX_CC_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then
29 source @out@/nix-support/add-flags.sh
32 if [ -z "${NIX_GNAT_WRAPPER_EXTRA_FLAGS_SET_@suffixSalt@:-}" ]; then
33 source @out@/nix-support/add-gnat-extra-flags.sh
36 # Parse command line options and set several variables.
37 # For instance, figure out if linker flags should be passed.
38 # GCC prints annoying warnings when they are not needed.
39 dontLink=0
40 nonFlagArgs=0
41 # shellcheck disable=SC2193
43 expandResponseParams "$@"
44 declare -i n=0
45 nParams=${#params[@]}
46 while (( "$n" < "$nParams" )); do
47 p=${params[n]}
48 p2=${params[n+1]:-} # handle `p` being last one
49 if [ "$p" = -c ]; then
50 dontLink=1
51 elif [ "$p" = -S ]; then
52 dontLink=1
53 elif [ "$p" = -E ]; then
54 dontLink=1
55 elif [ "$p" = -E ]; then
56 dontLink=1
57 elif [ "$p" = -M ]; then
58 dontLink=1
59 elif [ "$p" = -MM ]; then
60 dontLink=1
61 elif [[ "$p" = -x && "$p2" = *-header ]]; then
62 dontLink=1
63 elif [[ "$p" != -?* ]]; then
64 # A dash alone signifies standard input; it is not a flag
65 nonFlagArgs=1
67 n+=1
68 done
70 # If we pass a flag like -Wl, then gcc will call the linker unless it
71 # can figure out that it has to do something else (e.g., because of a
72 # "-c" flag). So if no non-flag arguments are given, don't pass any
73 # linker flags. This catches cases like "gcc" (should just print
74 # "gcc: no input files") and "gcc -v" (should print the version).
75 if [ "$nonFlagArgs" = 0 ]; then
76 dontLink=1
79 # Optionally filter out paths not refering to the store.
80 if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "$NIX_STORE" ]]; then
81 rest=()
82 nParams=${#params[@]}
83 declare -i n=0
84 while (( "$n" < "$nParams" )); do
85 p=${params[n]}
86 p2=${params[n+1]:-} # handle `p` being last one
87 if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then
88 skip "${p:2}"
89 elif [ "$p" = -L ] && badPath "$p2"; then
90 n+=1; skip "$p2"
91 elif [ "${p:0:3}" = -I/ ] && badPath "${p:2}"; then
92 skip "${p:2}"
93 elif [ "$p" = -I ] && badPath "$p2"; then
94 n+=1; skip "$p2"
95 elif [ "${p:0:4}" = -aI/ ] && badPath "${p:3}"; then
96 skip "${p:3}"
97 elif [ "$p" = -aI ] && badPath "$p2"; then
98 n+=1; skip "$p2"
99 elif [ "${p:0:4}" = -aO/ ] && badPath "${p:3}"; then
100 skip "${p:3}"
101 elif [ "$p" = -aO ] && badPath "$p2"; then
102 n+=1; skip "$p2"
103 elif [ "$p" = -isystem ] && badPath "$p2"; then
104 n+=1; skip "$p2"
105 else
106 rest+=("$p")
108 n+=1
109 done
110 # Old bash empty array hack
111 params=(${rest+"${rest[@]}"})
115 # Clear march/mtune=native -- they bring impurity.
116 if [ "$NIX_ENFORCE_NO_NATIVE_@suffixSalt@" = 1 ]; then
117 rest=()
118 # Old bash empty array hack
119 for p in ${params+"${params[@]}"}; do
120 if [[ "$p" = -m*=native ]]; then
121 skip "$p"
122 else
123 rest+=("$p")
125 done
126 # Old bash empty array hack
127 params=(${rest+"${rest[@]}"})
130 case "$(basename $0)x" in
131 "gnatbindx")
132 extraBefore=()
133 extraAfter=($NIX_GNATFLAGS_COMPILE_@suffixSalt@)
135 "gnatchopx")
136 extraBefore=("--GCC=@out@/bin/gcc")
137 extraAfter=()
139 "gnatcleanx")
140 extraBefore=($NIX_GNATFLAGS_COMPILE_@suffixSalt@)
141 extraAfter=()
143 "gnatlinkx")
144 extraBefore=()
145 extraAfter=("--GCC=@out@/bin/gcc")
147 "gnatlsx")
148 extraBefore=()
149 extraAfter=($NIX_GNATFLAGS_COMPILE_@suffixSalt@)
151 "gnatmakex")
152 extraBefore=("--GNATBIND=@out@/bin/gnatbind" "--GNATLINK=@out@/bin/gnatlink")
153 extraAfter=($NIX_GNATFLAGS_COMPILE_@suffixSalt@ -cargs $NIX_GNATMAKE_CARGS_@suffixSalt@)
155 esac
157 # As a very special hack, if the arguments are just `-v', then don't
158 # add anything. This is to prevent `gcc -v' (which normally prints
159 # out the version number and returns exit code 0) from printing out
160 # `No input files specified' and returning exit code 1.
161 if [ "$*" = -v ]; then
162 extraAfter=()
163 extraBefore=()
166 # Optionally print debug info.
167 if (( "${NIX_DEBUG:-0}" >= 1 )); then
168 # Old bash workaround, see ld-wrapper for explanation.
169 echo "extra flags before to @prog@:" >&2
170 printf " %q\n" ${extraBefore+"${extraBefore[@]}"} >&2
171 echo "original flags to @prog@:" >&2
172 printf " %q\n" ${params+"${params[@]}"} >&2
173 echo "extra flags after to @prog@:" >&2
174 printf " %q\n" ${extraAfter+"${extraAfter[@]}"} >&2
177 PATH="$path_backup"
178 # Old bash workaround, see above.
179 exec @prog@ \
180 ${extraBefore+"${extraBefore[@]}"} \
181 ${params+"${params[@]}"} \
182 ${extraAfter+"${extraAfter[@]}"}