]> git.sesse.net Git - stockfish/blob - scripts/get_native_properties.sh
Fix compilation after recent merge.
[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 # 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
18 get_flags() {
19   flags=$(awk '/^flags[ \t]*:|^Features[ \t]*:/{gsub(/^flags[ \t]*:[ \t]*|^Features[ \t]*:[ \t]*|[_.]/, ""); line=$0} END{print line}' /proc/cpuinfo)
20 }
21
22 # Check for gcc march "znver1" or "znver2" https://en.wikichip.org/wiki/amd/cpuid
23 check_znver_1_2() {
24   vendor_id=$(awk '/^vendor_id/{print $3; exit}' /proc/cpuinfo)
25   cpu_family=$(awk '/^cpu family/{print $4; exit}' /proc/cpuinfo)
26   [ "$vendor_id" = "AuthenticAMD" ] && [ "$cpu_family" = "23" ] && znver_1_2=true
27 }
28
29 # Set the file CPU x86_64 architecture
30 set_arch_x86_64() {
31   if check_flags 'avx512vnni' 'avx512dq' 'avx512f' 'avx512bw' 'avx512vl'; then
32     true_arch='x86-64-vnni256'
33   elif check_flags 'avx512f' 'avx512bw'; then
34     true_arch='x86-64-avx512'
35   elif [ -z "${znver_1_2+1}" ] && check_flags 'bmi2'; then
36     true_arch='x86-64-bmi2'
37   elif check_flags 'avx2'; then
38     true_arch='x86-64-avx2'
39   elif check_flags 'sse41' && check_flags 'popcnt'; then
40     true_arch='x86-64-sse41-popcnt'
41   else
42     true_arch='x86-64'
43   fi
44 }
45
46 # Check the system type
47 uname_s=$(uname -s)
48 uname_m=$(uname -m)
49 case $uname_s in
50   'Darwin') # Mac OSX system
51     case $uname_m in
52       'arm64')
53         true_arch='apple-silicon'
54         file_arch='x86-64-sse41-popcnt' # Supported by Rosetta 2
55         ;;
56       'x86_64')
57         flags=$(sysctl -n machdep.cpu.features machdep.cpu.leaf7_features | tr '\n' ' ' | tr '[:upper:]' '[:lower:]' | tr -d '_.')
58         set_arch_x86_64
59         if [ "$true_arch" = 'x86-64-vnni256' ] || [ "$true_arch" = 'x86-64-avx512' ]; then
60            file_arch='x86-64-bmi2'
61         fi
62         ;;
63     esac
64     file_os='macos'
65     file_ext='tar'
66     ;;
67   'Linux') # Linux system
68     get_flags
69     case $uname_m in
70       'x86_64')
71         file_os='ubuntu'
72         check_znver_1_2
73         set_arch_x86_64
74         ;;
75       'i686')
76         file_os='ubuntu'
77         true_arch='x86-32'
78         ;;
79       'aarch64')
80         file_os='android'
81         true_arch='armv8'
82         if check_flags 'dotprod'; then
83           true_arch="$true_arch-dotprod"
84         fi
85         ;;
86       'armv7'*)
87         file_os='android'
88         true_arch='armv7'
89         if check_flags 'neon'; then
90           true_arch="$true_arch-neon"
91         fi
92         ;;
93       *) # Unsupported machine type, exit with error
94         printf 'Unsupported machine type: %s\n' "$uname_m"
95         exit 1
96         ;;
97     esac
98     file_ext='tar'
99     ;;
100   'CYGWIN'*|'MINGW'*|'MSYS'*) # Windows system with POSIX compatibility layer
101     get_flags
102     check_znver_1_2
103     set_arch_x86_64
104     file_os='windows'
105     file_ext='zip'
106     ;;
107   *)
108     # Unknown system type, exit with error
109     printf 'Unsupported system type: %s\n' "$uname_s"
110     exit 1
111     ;;
112 esac
113
114 if [ -z "$file_arch" ]; then
115   file_arch=$true_arch
116 fi
117
118 file_name="stockfish-$file_os-$file_arch.$file_ext"
119
120 printf '%s %s\n' "$true_arch" "$file_name"