Skip to content

Commit

Permalink
Update toolchain to wrap gcc with new brioche-cc binary
Browse files Browse the repository at this point in the history
  • Loading branch information
kylewlacy committed Nov 23, 2024
1 parent 35e39c6 commit 2bf7bcc
Showing 1 changed file with 2 additions and 72 deletions.
74 changes: 2 additions & 72 deletions packages/std/toolchain/native/gcc.bri
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as std from "/core";
import { wrapWithScript } from "../utils.bri";
import { wrapWithScript, runtimeUtils } from "../utils.bri";
import linuxHeaders from "./linux_headers.bri";
import stage2 from "/toolchain/stage2";
import glibc from "./glibc.bri";
Expand Down Expand Up @@ -99,78 +99,8 @@ export default std.memo((): std.Recipe<std.Directory> => {
"bin/gcc",
],
renameSuffix: "-orig",
script: ccWrapper(),
script: std.castToFile(runtimeUtils().get("bin/brioche-cc")),
});

return gcc;
});

function ccWrapper(): std.Recipe<std.File> {
return std
.file(std.indoc`
#!/usr/bin/env sh
# MIT license: https://stackoverflow.com/a/29835459
rreadlink() ( # Execute the function in a *subshell* to localize variables and the effect of 'cd'.
target=$1 fname= targetDir= CDPATH=
# Try to make the execution environment as predictable as possible:
# All commands below are invoked via 'command', so we must make sure that 'command'
# itself is not redefined as an alias or shell function.
# (Note that command is too inconsistent across shells, so we don't use it.)
# 'command' is a *builtin* in bash, dash, ksh, zsh, and some platforms do not even have
# an external utility version of it (e.g, Ubuntu).
# 'command' bypasses aliases and shell functions and also finds builtins
# in bash, dash, and ksh. In zsh, option POSIX_BUILTINS must be turned on for that
# to happen.
{ \\unalias command; \\unset -f command; } >/dev/null 2>&1
[ -n "$ZSH_VERSION" ] && options[POSIX_BUILTINS]=on # make zsh find *builtins* with 'command' too.
while :; do # Resolve potential symlinks until the ultimate target is found.
[ -L "$target" ] || [ -e "$target" ] || { command printf '%s\\n' "ERROR: '$target' does not exist." >&2; return 1; }
command cd "$(command dirname -- "$target")" # Change to target dir; necessary for correct resolution of target path.
fname=$(command basename -- "$target") # Extract filename.
[ "$fname" = '/' ] && fname='' # !! curiously, 'basename /' returns '/'
if [ -L "$fname" ]; then
# Extract [next] target path, which may be defined
# *relative* to the symlink's own directory.
# Note: We parse 'ls -l' output to find the symlink target
# which is the only POSIX-compliant, albeit somewhat fragile, way.
target=$(command ls -l "$fname")
target=\${target#* -> }
continue # Resolve [next] symlink target.
fi
break # Ultimate target reached.
done
targetDir=$(command pwd -P) # Get canonical dir. path
# Output the ultimate target's canonical path.
# Note that we manually resolve paths ending in /. and /.. to make sure we have a normalized path.
if [ "$fname" = '.' ]; then
command printf '%s\\n' "\${targetDir%/}"
elif [ "$fname" = '..' ]; then
# Caveat: something like /var/.. will resolve to /private (assuming /var@ -> /private/var), i.e. the '..' is applied
# AFTER canonicalization.
command printf '%s\\n' "$(command dirname -- "\${targetDir}")"
else
command printf '%s\\n' "\${targetDir%/}/$fname"
fi
)
add_sysroot=1
for arg in "$@"; do
if [[ $arg == --sysroot* ]]; then
add_sysroot=0
break
fi
done
if [ "$add_sysroot" -eq 1 ]; then
script_dir=$(cd "$(dirname -- "$(rreadlink "$0")")" && pwd -P)
sysroot=$(cd "$script_dir/.." && pwd -P)
set -- --sysroot="$sysroot" "$@"
fi
"$(rreadlink "$0")-orig" "$@"
`)
.withPermissions({ executable: true });
}

0 comments on commit 2bf7bcc

Please sign in to comment.