]> git.sesse.net Git - stockfish/blob - scripts/get_native_properties.sh
cffb0ce2731cd082222a3eb9a77702a2a9e9fd1f
[stockfish] / scripts / get_native_properties.sh
1 #!/bin/sh
2
3 #
4 # Returns properties of the native system.
5 # best architecture as supported by the CPU
6 # filename of the best binary uploaded as an artifact during CI
7 #
8
9 # Check if all the given flags are present in the CPU flags list
10 check_flags() {
11   for flag; do
12     printf '%s\n' "$flags" | grep -q -w "$flag" || return 1
13   done
14 }
15
16 # Set the CPU flags list
17 get_flags() {
18   flags="$(awk '/^flags[ \t]*:/{gsub(/^flags[ \t]*:[ \t]*/, ""); line=$0} END{print line}' /proc/cpuinfo) $(awk '/^Features[ \t]*:/{gsub(/^Features[ \t]*:[ \t]*/, ""); line=$0} END{print line}' /proc/cpuinfo)"
19   # remove underscores and points from flags, e.g. gcc uses avx512vnni, while some cpuinfo can have avx512_vnni, some systems use sse4_1 others sse4.1
20   flags=$(printf '%s' "$flags" | sed "s/[_.]//g")
21 }
22
23 # Check for gcc march "znver1" or "znver2" https://en.wikichip.org/wiki/amd/cpuid
24 check_znver_1_2() {
25   vendor_id=$(awk '/^vendor_id/{print $3; exit}' /proc/cpuinfo)
26   cpu_family=$(awk '/^cpu family/{print $4; exit}' /proc/cpuinfo)
27   [ "$vendor_id" = "AuthenticAMD" ] && [ "$cpu_family" = "23" ] && znver_1_2=true
28 }
29
30 # Set the file CPU x86_64 architecture
31 set_arch_x86_64() {
32   if check_flags 'avx512vnni' 'avx512dq' 'avx512f' 'avx512bw' 'avx512vl'; then
33     true_arch='x86-64-vnni256'
34   elif check_flags 'avx512f' 'avx512bw'; then
35     true_arch='x86-64-avx512'
36   elif [ -z "${znver_1_2+1}" ] && check_flags 'bmi2'; then
37     true_arch='x86-64-bmi2'
38   elif check_flags 'avx2'; then
39     true_arch='x86-64-avx2'
40   elif check_flags 'sse41' && check_flags 'popcnt'; then
41     true_arch='x86-64-sse41-popcnt'
42   else
43     true_arch='x86-64'
44   fi
45 }
46
47 # Check the system type
48 uname_s=$(uname -s)
49 uname_m=$(uname -m)
50 case $uname_s in
51   'Darwin') # Mac OSX system
52     case $uname_m in
53       'arm64')
54         true_arch='apple-silicon'
55         file_arch='x86-64-sse41-popcnt' # Supported by Rosetta 2
56         ;;
57       'x86_64')
58         flags=$(sysctl -n machdep.cpu.features machdep.cpu.leaf7_features | tr '\n' ' ' | tr '[:upper:]' '[:lower:]' | sed "s/[_.]//g")
59         set_arch_x86_64
60         if [ "$true_arch" = 'x86-64-vnni256' ] || [ "$true_arch" = 'x86-64-avx512' ]; then
61            file_arch='x86-64-bmi2'
62         fi
63         ;;
64     esac
65     file_os='macos'
66     file_ext='tar'
67     ;;
68   'Linux') # Linux system
69     get_flags
70     case $uname_m in
71       'x86_64')
72         file_os='ubuntu'
73         check_znver_1_2
74         set_arch_x86_64
75         ;;
76       'i686')
77         file_os='ubuntu'
78         true_arch='x86-32'
79         ;;
80       'aarch64')
81         file_os='android'
82         true_arch='armv8'
83         if check_flags 'dotprod'; then
84           true_arch="$true_arch-dotprod"
85         fi
86         ;;
87       'armv7'*)
88         file_os='android'
89         true_arch='armv7'
90         if check_flags 'neon'; then
91           true_arch="$true_arch-neon"
92         fi
93         ;;
94       *) # Unsupported machine type, exit with error
95         printf 'Unsupported machine type: %s\n' "$uname_m"
96         exit 1
97         ;;
98     esac
99     file_ext='tar'
100     ;;
101   'CYGWIN'*|'MINGW'*|'MSYS'*) # Windows system with POSIX compatibility layer
102     get_flags
103     check_znver_1_2
104     set_arch_x86_64
105     file_os='windows'
106     file_ext='zip'
107     ;;
108   *)
109     # Unknown system type, exit with error
110     printf 'Unsupported system type: %s\n' "$uname_s"
111     exit 1
112     ;;
113 esac
114
115 if [ -z "$file_arch" ]; then
116   file_arch=$true_arch
117 fi
118
119 file_name="stockfish-$file_os-$file_arch.$file_ext"
120
121 printf '%s %s\n' "$true_arch" "$file_name"