Skip to content

Commit

Permalink
Reverse target architecture mapping logic
Browse files Browse the repository at this point in the history
  • Loading branch information
diemogebhardt committed Nov 30, 2024
1 parent 11b5506 commit 9684c9a
Showing 1 changed file with 11 additions and 39 deletions.
50 changes: 11 additions & 39 deletions bin/verify-binary-architecture.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,32 @@ if [ $# -ne 2 ]; then
echo "Usage: $0 <target-triple> <binary-path>"
exit 1
fi

TARGET_TRIPLE="$1"
BINARY_PATH="$2"

# Extract target OS and architecture
TARGET_OS=$(echo "${TARGET_TRIPLE}" | grep -Eo "darwin|linux|windows" || echo "unknown")
# Extract and validate target architecture
TARGET_ARCHITECTURE=$(echo "${TARGET_TRIPLE}" | grep -Eo "x86_64|aarch64" || echo "unknown")

# Validate inputs
if [ "$TARGET_OS" = "unknown" ]; then
echo "Unknown target OS in '${TARGET_TRIPLE}'"
exit 1
fi
if [ "$TARGET_ARCHITECTURE" = "unknown" ]; then
echo "Unknown target architecture in '${TARGET_TRIPLE}'"
exit 1
fi

# Get expected binary architecture based on target OS and architecture
get_expected_binary_architecture() {
local target_os="$1"
local target_architecture="$2"

case "${target_os}" in
"darwin")
case "${target_architecture}" in
"x86_64") echo "x86_64" ;;
"aarch64") echo "arm64" ;;
esac
;;
"linux")
case "${target_architecture}" in
"x86_64") echo "x86-64" ;;
"aarch64") echo "aarch64" ;;
esac
;;
"windows")
case "${target_architecture}" in
"x86_64") echo "x86-64" ;;
"aarch64") echo "Aarch64" ;;
esac
;;
# Parse and normalize binary architecture
normalize_architecture() {
local architecture="$1"
case "$architechture" in
"x86_64"|"x86-64") echo "x86_64" ;;
"arm64"|"aarch64"|"Aarch64") echo "aarch64" ;;
*) echo "unknown" ;;
esac
}
EXPECTED_BINARY_ARCHITECTURE=$(get_expected_binary_architecture "$TARGET_OS" "$TARGET_ARCHITECTURE")

# Parse binary architecture
file_output=$(file -b "${BINARY_PATH}")
BINARY_ARCHITECTURE=$(echo "${file_output}" | grep -Eo "x86_64|arm64|x86-64|aarch64|x86-64|Aarch64" | head -n1 || echo "unknown")
BINARY_ARCHITECTURE=$(echo "${file_output}" | grep -Eo "x86_64|x86-64|arm64|aarch64|Aarch64" | head -n1 || normalize_architecture "$file_output")

# Verify that binary architecture matches target architecture
if [ "$BINARY_ARCHITECTURE" != "$EXPECTED_BINARY_ARCHITECTURE" ]; then
if [ "$BINARY_ARCHITECTURE" != "$TARGET_ARCHITECTURE" ]; then
echo "Architecture mismatch for '${TARGET_TRIPLE}'"
echo "Expected: '${EXPECTED_BINARY_ARCHITECTURE}'"
echo "Expected: '${TARGET_ARCHITECTURE}'"
echo "Got: '${BINARY_ARCHITECTURE}'"
exit 1
fi

0 comments on commit 9684c9a

Please sign in to comment.