platform-detect.sh raw
1 #!/bin/bash
2
3 # Platform detection and binary selection helper
4 # This script detects the current platform and returns the appropriate binary name
5
6 detect_platform() {
7 local os=$(uname -s | tr '[:upper:]' '[:lower:]')
8 local arch=$(uname -m)
9
10 # Normalize OS name
11 case "$os" in
12 linux*)
13 os="linux"
14 ;;
15 darwin*)
16 os="darwin"
17 ;;
18 mingw*|msys*|cygwin*)
19 os="windows"
20 ;;
21 *)
22 echo "unknown"
23 return 1
24 ;;
25 esac
26
27 # Normalize architecture
28 case "$arch" in
29 x86_64|amd64)
30 arch="amd64"
31 ;;
32 aarch64|arm64)
33 arch="arm64"
34 ;;
35 armv7l)
36 arch="arm"
37 ;;
38 *)
39 echo "unknown"
40 return 1
41 ;;
42 esac
43
44 echo "${os}-${arch}"
45 return 0
46 }
47
48 get_binary_name() {
49 local version=$1
50 local platform=$(detect_platform)
51
52 if [ "$platform" = "unknown" ]; then
53 echo "Error: Unsupported platform" >&2
54 return 1
55 fi
56
57 local ext=""
58 if [[ "$platform" == windows-* ]]; then
59 ext=".exe"
60 fi
61
62 echo "orly-${version}-${platform}${ext}"
63 return 0
64 }
65
66 get_library_name() {
67 local platform=$(detect_platform)
68
69 if [ "$platform" = "unknown" ]; then
70 echo "Error: Unsupported platform" >&2
71 return 1
72 fi
73
74 case "$platform" in
75 linux-*)
76 echo "libsecp256k1-${platform}.so"
77 ;;
78 darwin-*)
79 echo "libsecp256k1-${platform}.dylib"
80 ;;
81 windows-*)
82 echo "libsecp256k1-${platform}.dll"
83 ;;
84 *)
85 return 1
86 ;;
87 esac
88
89 return 0
90 }
91
92 # If called directly, run the function based on first argument
93 if [ "${BASH_SOURCE[0]}" -ef "$0" ]; then
94 case "$1" in
95 detect|platform)
96 detect_platform
97 ;;
98 binary)
99 get_binary_name "${2:-v0.25.0}"
100 ;;
101 library|lib)
102 get_library_name
103 ;;
104 *)
105 echo "Usage: $0 {detect|binary <version>|library}"
106 echo ""
107 echo "Commands:"
108 echo " detect - Detect current platform (e.g., linux-amd64)"
109 echo " binary - Get binary name for current platform"
110 echo " library - Get library name for current platform"
111 echo ""
112 echo "Examples:"
113 echo " $0 detect"
114 echo " $0 binary v0.25.0"
115 echo " $0 library"
116 exit 1
117 ;;
118 esac
119 fi
120
121