Skip to content

Commit

Permalink
Test c4
Browse files Browse the repository at this point in the history
  • Loading branch information
diemogebhardt committed Nov 30, 2024
1 parent cfccfe5 commit b2a9827
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions bin/verify-binary-architecture.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ if [ $# -ne 2 ]; then
echo "Usage: $0 <target-triple> <binary-path>"
exit 1
fi

TARGET_TRIPLE="$1"
BINARY_PATH="$2"
BINARY_FILE_TYPE=$(file -b "$BINARY_PATH")

# Architecture patterns
X86_64_PATTERNS='x86-64|x86_64'
Expand All @@ -17,22 +19,17 @@ parse() { grep -Eo "$X86_64_PATTERNS|$AARCH64_PATTERNS" | head -n1; }
normalize() { sed -E "s/($X86_64_PATTERNS)/x86-64/;s/($AARCH64_PATTERNS)/AArch64/"; }
unknown_architecture_for() { echo "unknown $1 architecture"; }

# Parse target architecture
parse_target_architecture() {
local target_triple="$1"
get() { echo "$target_triple"; }
get | parse | normalize || unknown_architecture_for "target"
}
TARGET_ARCHITECTURE=$(parse_target_architecture "$TARGET_TRIPLE")

# Parse and normalize binary architecture
parse_and_normalize_binary_architecture() {
local binary_path="$1"
debug_pipe() { tee >(cat >&2); }
get() { file -b "$binary_path" | debug_pipe; }
get | parse | normalize || unknown_architecture_for "binary"
}
BINARY_ARCHITECTURE=$(parse_and_normalize_binary_architecture "$BINARY_PATH")
# Parse and normalize architectures
TARGET_ARCHITECTURE=$(
echo "$TARGET_TRIPLE" \
| parse | normalize \
|| unknown_architecture_for "target"
)
BINARY_ARCHITECTURE=$(
echo "$BINARY_FILE_TYPE" \
| parse | normalize \
|| unknown_architecture_for "binary"
)

# Verify that binary architecture matches target architecture
if [ "$BINARY_ARCHITECTURE" != "$TARGET_ARCHITECTURE" ]; then
Expand All @@ -41,5 +38,6 @@ if [ "$BINARY_ARCHITECTURE" != "$TARGET_ARCHITECTURE" ]; then
echo "Got: '$BINARY_ARCHITECTURE'"
exit 1
fi

echo "Architecture match for '$TARGET_TRIPLE'!"
exit 0

0 comments on commit b2a9827

Please sign in to comment.