Debian Codegraph @

others/mbedtls/tf-psa-crypto/drivers/pqcp/mldsa-native/examples/basic_deterministic/auto.mk

raw
1 # Copyright (c) The mlkem-native project authors
2 # Copyright (c) The mldsa-native project authors
3 # SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
4
5 # Automatically detect system architecture and set preprocessor flags accordingly
6 # This file detects host CPU capabilities and combines them with compiler support
7 # to enable optimal compilation flags.
8 #
9 # Compiler feature detection can be overridden by setting the corresponding variable
10 # on the command line:
11 # make MK_COMPILER_SUPPORTS_SHA3=0 # Disable SHA3 detection
12 # make MK_COMPILER_SUPPORTS_AVX2=0 # Disable AVX2 detection
13 # make MK_COMPILER_SUPPORTS_BMI2=0 # Disable BMI2 detection
14 # make MK_COMPILER_SUPPORTS_SSE2=0 # Disable SSE2 detection
15
16 ifndef _AUTO_MK
17 _AUTO_MK :=
18
19 # Override ARCH for cross-compilation based on CROSS_PREFIX
20 ifeq ($(CROSS_PREFIX),)
21 ARCH := $(shell uname -m)
22 # Normalize architecture names
23 ifeq ($(ARCH),arm64)
24 ARCH := aarch64
25 endif
26 else # CROSS_PREFIX
27 ifneq ($(findstring x86_64, $(CROSS_PREFIX)),)
28 ARCH := x86_64
29 else ifneq ($(findstring aarch64_be, $(CROSS_PREFIX)),)
30 ARCH := aarch64_be
31 else ifneq ($(findstring aarch64, $(CROSS_PREFIX)),)
32 ARCH := aarch64
33 else ifneq ($(findstring riscv64, $(CROSS_PREFIX)),)
34 ARCH := riscv64
35 else ifneq ($(findstring riscv32, $(CROSS_PREFIX)),)
36 ARCH := riscv32
37 else ifneq ($(findstring powerpc64le, $(CROSS_PREFIX)),)
38 ARCH := powerpc64le
39 else ifneq ($(findstring arm-none-eabi-, $(CROSS_PREFIX)),)
40 ARCH := arm
41 else
42 ifeq ($(AUTO),1)
43 $(warning Unknown cross-compilation prefix $(CROSS_PREFIX), no automatic detection of CFLAGS.)
44 ARCH := unknown
45 endif
46 endif
47 endif # CROSS_PREFIX
48
49 # x86_64 compiler feature detection
50 ifeq ($(ARCH),x86_64)
51
52 # Test AVX2 support using C with inline assembly
53 MK_COMPILER_SUPPORTS_AVX2 ?= $(shell echo 'int main() { __asm__("vpxor %%ymm0, %%ymm1, %%ymm2" ::: "ymm0", "ymm1", "ymm2"); return 0; }' | $(CC) -mavx2 -x c - -c -o /dev/null 2>/dev/null && echo 1 || echo 0)
54
55 # Test SSE2 support using C with inline assembly
56 MK_COMPILER_SUPPORTS_SSE2 ?= $(shell echo 'int main() { __asm__("pxor %%xmm0, %%xmm1" ::: "xmm0", "xmm1"); return 0; }' | $(CC) -msse2 -x c - -c -o /dev/null 2>/dev/null && echo 1 || echo 0)
57
58 # Test BMI2 support using C with inline assembly
59 MK_COMPILER_SUPPORTS_BMI2 ?= $(shell echo 'int main() { __asm__("pdep %%eax, %%ebx, %%ecx" ::: "eax", "ebx", "ecx"); return 0; }' | $(CC) -mbmi2 -x c - -c -o /dev/null 2>/dev/null && echo 1 || echo 0)
60
61 endif # x86_64 compiler detection
62
63 # AArch64 compiler feature detection
64 ifeq ($(ARCH),aarch64)
65
66 # Test SHA3 support (Armv8.4-a+SHA3) using C with inline assembly
67 MK_COMPILER_SUPPORTS_SHA3 ?= $(shell echo 'int main() { __asm__("eor3 v0.16b, v1.16b, v2.16b, v3.16b" ::: "v0", "v1", "v2", "v3"); return 0; }' | $(CC) -march=armv8.4-a+sha3 -x c - -c -o /dev/null 2>/dev/null && echo 1 || echo 0)
68
69 endif # aarch64 compiler detection
70
71 # Define HOST_PLATFORM if not already defined
72 HOST_PLATFORM ?= $(shell uname -s)-$(shell uname -m)
73
74 # Helper function to check if host CPU supports a feature
75 # Usage: $(call check_host_feature,feature_pattern,source_command)
76 define check_host_feature
77 $(shell $(2) 2>/dev/null | grep -q "$(1)" && echo 1 || echo 0)
78 endef
79
80 # x86_64 architecture detection
81 ifeq ($(ARCH),x86_64)
82
83 # Host CPU feature detection for x86_64
84 ifeq ($(HOST_PLATFORM),Linux-x86_64)
85 # Linux: Use /proc/cpuinfo
86 MK_HOST_SUPPORTS_AVX2 := $(call check_host_feature,avx2,cat /proc/cpuinfo)
87 MK_HOST_SUPPORTS_SSE2 := $(call check_host_feature,sse2,cat /proc/cpuinfo)
88 MK_HOST_SUPPORTS_BMI2 := $(call check_host_feature,bmi2,cat /proc/cpuinfo)
89 else ifeq ($(HOST_PLATFORM),Darwin-x86_64)
90 # macOS: Use sysctl
91 MK_HOST_SUPPORTS_AVX2 := $(call check_host_feature,AVX2,sysctl -n machdep.cpu.leaf7_features)
92 MK_HOST_SUPPORTS_SSE2 := $(call check_host_feature,SSE2,sysctl -n machdep.cpu.features)
93 MK_HOST_SUPPORTS_BMI2 := $(call check_host_feature,BMI2,sysctl -n machdep.cpu.leaf7_features)
94 else ifneq ($(CROSS_PREFIX),)
95 # Cross-compilation: assume all features are supported
96 MK_HOST_SUPPORTS_AVX2 := 1
97 MK_HOST_SUPPORTS_SSE2 := 1
98 MK_HOST_SUPPORTS_BMI2 := 1
99 else
100 # Other platforms: assume no support
101 MK_HOST_SUPPORTS_AVX2 := 0
102 MK_HOST_SUPPORTS_SSE2 := 0
103 MK_HOST_SUPPORTS_BMI2 := 0
104 endif # HOST_PLATFORM x86_64
105
106 endif # x86_64
107
108 # AArch64 architecture detection
109 ifeq ($(ARCH),aarch64)
110
111 # Host CPU feature detection for AArch64
112 ifeq ($(HOST_PLATFORM),Linux-aarch64)
113 # Linux: Use /proc/cpuinfo (look for sha3 in Features line)
114 MK_HOST_SUPPORTS_SHA3 := $(call check_host_feature,sha3,cat /proc/cpuinfo)
115 else ifeq ($(HOST_PLATFORM),Darwin-arm64)
116 # macOS: Use sysctl to check for SHA3 support
117 MK_HOST_SUPPORTS_SHA3 := $(call check_host_feature,1,sysctl -n hw.optional.armv8_2_sha3)
118 else ifneq ($(CROSS_PREFIX),)
119 # Cross-compilation: assume all features are supported
120 MK_HOST_SUPPORTS_SHA3 := 1
121 else
122 # Other platforms: assume no support
123 MK_HOST_SUPPORTS_SHA3 := 0
124 endif # HOST_PLATFORM aarch64
125
126 endif # aarch64
127
128 # Only apply CFLAGS modifications if AUTO=1
129 ifeq ($(AUTO),1)
130
131 # x86_64 CFLAGS configuration
132 ifeq ($(ARCH),x86_64)
133 CFLAGS += -DMLD_FORCE_X86_64
134
135 # Add flags only if both compiler and host support the feature
136 ifeq ($(MK_COMPILER_SUPPORTS_AVX2)$(MK_HOST_SUPPORTS_AVX2),11)
137 CFLAGS += -mavx2
138 endif
139
140 ifeq ($(MK_COMPILER_SUPPORTS_BMI2)$(MK_HOST_SUPPORTS_BMI2),11)
141 CFLAGS += -mbmi2
142 endif
143 endif # x86_64
144
145 # AArch64 CFLAGS configuration
146 ifeq ($(ARCH),aarch64)
147 CFLAGS += -DMLD_FORCE_AARCH64
148
149 # Add SHA3 flags only if both compiler and host support it
150 ifeq ($(MK_COMPILER_SUPPORTS_SHA3)$(MK_HOST_SUPPORTS_SHA3),11)
151 CFLAGS += -march=armv8.4-a+sha3
152 endif
153 endif # aarch64
154
155 # AArch64 Big Endian CFLAGS configuration
156 ifeq ($(ARCH),aarch64_be)
157 CFLAGS += -DMLD_FORCE_AARCH64_EB
158 endif # aarch64_be
159
160 # RISC-V 64-bit CFLAGS configuration
161 ifeq ($(ARCH),riscv64)
162 CFLAGS += -DMLD_FORCE_RISCV64
163 CFLAGS += -march=rv64gcv
164 endif # riscv64
165
166 # RISC-V 32-bit CFLAGS configuration
167 ifeq ($(ARCH),riscv32)
168 CFLAGS += -DMLD_FORCE_RISCV32
169 endif # riscv32
170
171 # PowerPC 64-bit Little Endian CFLAGS configuration
172 ifeq ($(ARCH),powerpc64le)
173 CFLAGS += -DMLD_FORCE_PPC64LE
174 endif # powerpc64le
175
176 endif # AUTO=1
177
178 endif # _AUTO_MK