|
1 | 1 | # SPDX-License-Identifier: Apache-2.0 OR ISC OR MIT
|
2 | 2 | #
|
3 |
| -# Automatically detect system architecture and set preprocessor etc accordingly |
| 3 | +# Automatically detect system architecture and set preprocessor flags accordingly |
| 4 | +# This file detects host CPU capabilities and combines them with compiler support |
| 5 | +# to enable optimal compilation flags. |
4 | 6 |
|
5 |
| -# Native compilation |
6 |
| -ifeq ($(CROSS_PREFIX),) |
| 7 | +ifndef _AUTO_MK |
| 8 | +_AUTO_MK := |
| 9 | + |
| 10 | +# Helper function to check if host CPU supports a feature |
| 11 | +# Usage: $(call check_host_feature,feature_pattern,source_command) |
| 12 | +define check_host_feature |
| 13 | +$(shell $(2) 2>/dev/null | grep -q "$(1)" && echo 1 || echo 0) |
| 14 | +endef |
| 15 | + |
| 16 | +# x86_64 architecture detection |
| 17 | +ifeq ($(ARCH),x86_64) |
| 18 | + |
| 19 | +# Host CPU feature detection for x86_64 |
7 | 20 | ifeq ($(HOST_PLATFORM),Linux-x86_64)
|
8 |
| - CFLAGS += -mavx2 -mbmi2 -mpopcnt -maes |
9 |
| - CFLAGS += -DMLK_FORCE_X86_64 |
10 |
| -else ifeq ($(HOST_PLATFORM),Linux-aarch64) |
11 |
| - CFLAGS += -DMLK_FORCE_AARCH64 |
| 21 | +# Linux: Use /proc/cpuinfo |
| 22 | +MK_HOST_SUPPORTS_AVX2 := $(call check_host_feature,avx2,cat /proc/cpuinfo) |
| 23 | +MK_HOST_SUPPORTS_SSE2 := $(call check_host_feature,sse2,cat /proc/cpuinfo) |
| 24 | +MK_HOST_SUPPORTS_BMI2 := $(call check_host_feature,bmi2,cat /proc/cpuinfo) |
| 25 | +else ifeq ($(HOST_PLATFORM),Darwin-x86_64) |
| 26 | +# macOS: Use sysctl |
| 27 | +MK_HOST_SUPPORTS_AVX2 := $(call check_host_feature,AVX2,sysctl -n machdep.cpu.leaf7_features) |
| 28 | +MK_HOST_SUPPORTS_SSE2 := $(call check_host_feature,SSE2,sysctl -n machdep.cpu.features) |
| 29 | +MK_HOST_SUPPORTS_BMI2 := $(call check_host_feature,BMI2,sysctl -n machdep.cpu.leaf7_features) |
| 30 | +else ifneq ($(CROSS_PREFIX),) |
| 31 | +# Cross-compilation: assume all features are supported |
| 32 | +MK_HOST_SUPPORTS_AVX2 := 1 |
| 33 | +MK_HOST_SUPPORTS_SSE2 := 1 |
| 34 | +MK_HOST_SUPPORTS_BMI2 := 1 |
| 35 | +else |
| 36 | +# Other platforms: assume no support |
| 37 | +MK_HOST_SUPPORTS_AVX2 := 0 |
| 38 | +MK_HOST_SUPPORTS_SSE2 := 0 |
| 39 | +MK_HOST_SUPPORTS_BMI2 := 0 |
| 40 | +endif # HOST_PLATFORM x86_64 |
| 41 | + |
| 42 | +endif # x86_64 |
| 43 | + |
| 44 | +# AArch64 architecture detection |
| 45 | +ifeq ($(ARCH),aarch64) |
| 46 | + |
| 47 | +# Host CPU feature detection for AArch64 |
| 48 | +ifeq ($(HOST_PLATFORM),Linux-aarch64) |
| 49 | +# Linux: Use /proc/cpuinfo (look for sha3 in Features line) |
| 50 | +MK_HOST_SUPPORTS_SHA3 := $(call check_host_feature,sha3,cat /proc/cpuinfo) |
12 | 51 | else ifeq ($(HOST_PLATFORM),Darwin-arm64)
|
13 |
| - CFLAGS += -DMLK_FORCE_AARCH64 |
| 52 | +# macOS: Use sysctl to check for SHA3 support |
| 53 | +MK_HOST_SUPPORTS_SHA3 := $(call check_host_feature,1,sysctl -n hw.optional.armv8_2_sha3) |
| 54 | +else ifneq ($(CROSS_PREFIX),) |
| 55 | +# Cross-compilation: assume all features are supported |
| 56 | +MK_HOST_SUPPORTS_SHA3 := 1 |
| 57 | +else |
| 58 | +# Other platforms: assume no support |
| 59 | +MK_HOST_SUPPORTS_SHA3 := 0 |
| 60 | +endif # HOST_PLATFORM aarch64 |
| 61 | + |
| 62 | +endif # aarch64 |
| 63 | + |
| 64 | +# Only apply CFLAGS modifications if AUTO=1 |
| 65 | +ifeq ($(AUTO),1) |
| 66 | + |
| 67 | +# x86_64 CFLAGS configuration |
| 68 | +ifeq ($(ARCH),x86_64) |
| 69 | +CFLAGS += -DMLK_FORCE_X86_64 |
| 70 | + |
| 71 | +# Add flags only if both compiler and host support the feature |
| 72 | +ifeq ($(MK_COMPILER_SUPPORTS_AVX2)$(MK_HOST_SUPPORTS_AVX2),11) |
| 73 | +CFLAGS += -mavx2 |
14 | 74 | endif
|
15 |
| -# Cross compilation |
16 |
| -else ifneq ($(findstring x86_64, $(CROSS_PREFIX)),) |
17 |
| - CFLAGS += -mavx2 -mbmi2 -mpopcnt -maes |
18 |
| - CFLAGS += -DMLK_FORCE_X86_64 |
19 |
| -else ifneq ($(findstring aarch64_be, $(CROSS_PREFIX)),) |
20 |
| - CFLAGS += -DMLK_FORCE_AARCH64_EB |
21 |
| -else ifneq ($(findstring aarch64, $(CROSS_PREFIX)),) |
22 |
| - CFLAGS += -DMLK_FORCE_AARCH64 |
23 |
| -else ifneq ($(findstring riscv64, $(CROSS_PREFIX)),) |
24 |
| - CFLAGS += -DMLK_FORCE_RISCV64 |
25 |
| -else ifneq ($(findstring riscv32, $(CROSS_PREFIX)),) |
26 |
| - CFLAGS += -DMLK_FORCE_RISCV32 |
27 |
| -else ifneq ($(findstring powerpc64le, $(CROSS_PREFIX)),) |
28 |
| - CFLAGS += -DMLK_FORCE_PPC64LE |
| 75 | + |
| 76 | +ifeq ($(MK_COMPILER_SUPPORTS_BMI2)$(MK_HOST_SUPPORTS_BMI2),11) |
| 77 | +CFLAGS += -mbmi2 |
29 | 78 | endif
|
| 79 | +endif # x86_64 |
| 80 | + |
| 81 | +# AArch64 CFLAGS configuration |
| 82 | +ifeq ($(ARCH),aarch64) |
| 83 | +CFLAGS += -DMLK_FORCE_AARCH64 |
| 84 | + |
| 85 | +# Add SHA3 flags only if both compiler and host support it |
| 86 | +ifeq ($(MK_COMPILER_SUPPORTS_SHA3)$(MK_HOST_SUPPORTS_SHA3),11) |
| 87 | +CFLAGS += -march=armv8.4-a+sha3 |
| 88 | +endif |
| 89 | +endif # aarch64 |
| 90 | + |
| 91 | +# AArch64 Big Endian CFLAGS configuration |
| 92 | +ifeq ($(ARCH),aarch64_be) |
| 93 | +CFLAGS += -DMLK_FORCE_AARCH64_EB |
| 94 | +endif # aarch64_be |
| 95 | + |
| 96 | +# RISC-V 64-bit CFLAGS configuration |
| 97 | +ifeq ($(ARCH),riscv64) |
| 98 | +CFLAGS += -DMLK_FORCE_RISCV64 |
| 99 | +endif # riscv64 |
| 100 | + |
| 101 | +# RISC-V 32-bit CFLAGS configuration |
| 102 | +ifeq ($(ARCH),riscv32) |
| 103 | +CFLAGS += -DMLK_FORCE_RISCV32 |
| 104 | +endif # riscv32 |
| 105 | + |
| 106 | +# PowerPC 64-bit Little Endian CFLAGS configuration |
| 107 | +ifeq ($(ARCH),powerpc64le) |
| 108 | +CFLAGS += -DMLK_FORCE_PPC64LE |
| 109 | +endif # powerpc64le |
| 110 | + |
| 111 | +endif # AUTO=1 |
| 112 | + |
| 113 | +endif # _AUTO_MK |
0 commit comments