stdenv: re-add isMachO helper function (#138334)

authored by Winter and committed by GitHub 8cff7796 c4fd2a8f

+28
+28
pkgs/stdenv/generic/setup.sh
··· 197 if [ "$magic" = $'\177ELF' ]; then return 0; else return 1; fi 198 } 199 200 # Return success if the specified file is a script (i.e. starts with 201 # "#!"). 202 isScript() {
··· 197 if [ "$magic" = $'\177ELF' ]; then return 0; else return 1; fi 198 } 199 200 + # Return success if the specified file is a Mach-O object. 201 + isMachO() { 202 + local fn="$1" 203 + local fd 204 + local magic 205 + exec {fd}< "$fn" 206 + read -r -n 4 -u "$fd" magic 207 + exec {fd}<&- 208 + 209 + # nix uses 'declare -F' in get-env.sh to retrieve the loaded functions. 210 + # If we use the $'string' syntax instead of 'echo -ne' then 'declare' will print the raw characters and break nix. 211 + # See https://github.com/NixOS/nixpkgs/pull/138334 and https://github.com/NixOS/nix/issues/5262. 212 + 213 + # https://opensource.apple.com/source/lldb/lldb-310.2.36/examples/python/mach_o.py.auto.html 214 + if [[ "$magic" = $(echo -ne "\xfe\xed\xfa\xcf") || "$magic" = $(echo -ne "\xcf\xfa\xed\xfe") ]]; then 215 + # MH_MAGIC_64 || MH_CIGAM_64 216 + return 0; 217 + elif [[ "$magic" = $(echo -ne "\xfe\xed\xfa\xce") || "$magic" = $(echo -ne "\xce\xfa\xed\xfe") ]]; then 218 + # MH_MAGIC || MH_CIGAM 219 + return 0; 220 + elif [[ "$magic" = $(echo -ne "\xca\xfe\xba\xbe") || "$magic" = $(echo -ne "\xbe\xba\xfe\xca") ]]; then 221 + # FAT_MAGIC || FAT_CIGAM 222 + return 0; 223 + else 224 + return 1; 225 + fi 226 + } 227 + 228 # Return success if the specified file is a script (i.e. starts with 229 # "#!"). 230 isScript() {