From 557d956555a790f8158f9f8aab089d96433adad0 Mon Sep 17 00:00:00 2001 From: Farasat Ali <63093876+faraasat@users.noreply.github.com> Date: Sat, 6 Dec 2025 16:24:15 +0500 Subject: [PATCH 1/3] fix: compilation and build for mac and amd architectures --- .gitignore | 1 + cmake/CompilerSetup.cmake | 41 +++++++++++++++++++++++++---------- lib/platform_common/qintrin.h | 13 ++++++++++- src/CMakeLists.txt | 35 ++++++++++++++++++++++-------- test/CMakeLists.txt | 27 ++++++++++++++++++----- 5 files changed, 91 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index bf4f57667..c691607d5 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ x64/ .DS_Store .clang-format tmp +build \ No newline at end of file diff --git a/cmake/CompilerSetup.cmake b/cmake/CompilerSetup.cmake index 4ed5386c3..58495aff0 100644 --- a/cmake/CompilerSetup.cmake +++ b/cmake/CompilerSetup.cmake @@ -189,13 +189,20 @@ if(IS_MSVC) message(STATUS "AVX-512 is disabled. If you would like to activate make sure you set ENABLE_AVX512 to ON while running cmake.") endif() elseif(IS_CLANG OR IS_GCC) - if(ENABLE_AVX512) - set(CPU_INSTRUCTION_FLAGS "-mavx -mavx2 -mavx512f -mavx512cd -mavx512vl -mavx512bw -mavx512dq" CACHE INTERNAL "CPU instruction set flags" FORCE) - message(STATUS "GCC/Clang: Enabling AVX-512 and AVX/AVX2") + # Only enable AVX flags on x86_64 targets. On non-x86 (e.g., arm64) + # these flags are invalid and will cause the compiler to error. + if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64") + if(ENABLE_AVX512) + set(CPU_INSTRUCTION_FLAGS "-mavx -mavx2 -mavx512f -mavx512cd -mavx512vl -mavx512bw -mavx512dq" CACHE INTERNAL "CPU instruction set flags" FORCE) + message(STATUS "GCC/Clang: Enabling AVX-512 and AVX/AVX2") + else() + set(CPU_INSTRUCTION_FLAGS "-mavx -mavx2" CACHE INTERNAL "CPU instruction set flags" FORCE) + message(STATUS "GCC/Clang: Enabling AVX/AVX2") + message(STATUS "AVX-512 is disabled. If you would like to activate make sure you set ENABLE_AVX512 to ON while running cmake.") + endif() else() - set(CPU_INSTRUCTION_FLAGS "-mavx -mavx2" CACHE INTERNAL "CPU instruction set flags" FORCE) - message(STATUS "GCC/Clang: Enabling AVX/AVX2") - message(STATUS "AVX-512 is disabled. If you would like to activate make sure you set ENABLE_AVX512 to ON while running cmake.") + set(CPU_INSTRUCTION_FLAGS "" CACHE INTERNAL "CPU instruction set flags" FORCE) + message(STATUS "Skipping AVX/AVX2/AVX-512 flags for non-x86_64 architecture: ${CMAKE_SYSTEM_PROCESSOR}") endif() endif() @@ -205,12 +212,24 @@ set(TEST_SPECIFIC_FLAGS "" CACHE INTERNAL "Test-specific compiler flags") if(IS_MSVC) set(TEST_SPECIFIC_FLAGS "/WX /EHsc" CACHE INTERNAL "Test-specific compiler flags" FORCE) elseif(IS_CLANG OR IS_GCC) - if(USE_SANITIZER) - set(TEST_SPECIFIC_FLAGS "-Wpedantic -Werror -mrdrnd -Wcast-align -fsanitize=alignment -fno-sanitize-recover=alignment" CACHE INTERNAL "Test-specific compiler flags" FORCE) - set(TEST_SPECIFIC_LINK_FLAGS "-fsanitize=alignment" CACHE INTERNAL "Test-specific linker flags" FORCE) + # Only enable rdrand test flag on x86 targets; rdrand is an x86 + # instruction and -mrdrnd is invalid on arm64. + if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64") + if(USE_SANITIZER) + set(TEST_SPECIFIC_FLAGS "-Wpedantic -Werror -mrdrnd -Wcast-align -fsanitize=alignment -fno-sanitize-recover=alignment" CACHE INTERNAL "Test-specific compiler flags" FORCE) + set(TEST_SPECIFIC_LINK_FLAGS "-fsanitize=alignment" CACHE INTERNAL "Test-specific linker flags" FORCE) + else() + set(TEST_SPECIFIC_FLAGS "-Wpedantic -Werror -mrdrnd -Wcast-align " CACHE INTERNAL "Test-specific compiler flags" FORCE) + set(TEST_SPECIFIC_LINK_FLAGS "" CACHE INTERNAL "Test-specific linker flags" FORCE) + endif() else() - set(TEST_SPECIFIC_FLAGS "-Wpedantic -Werror -mrdrnd -Wcast-align " CACHE INTERNAL "Test-specific compiler flags" FORCE) - set(TEST_SPECIFIC_LINK_FLAGS "" CACHE INTERNAL "Test-specific linker flags" FORCE) + if(USE_SANITIZER) + set(TEST_SPECIFIC_FLAGS "-Wpedantic -Werror -Wcast-align -fsanitize=alignment -fno-sanitize-recover=alignment" CACHE INTERNAL "Test-specific compiler flags" FORCE) + set(TEST_SPECIFIC_LINK_FLAGS "-fsanitize=alignment" CACHE INTERNAL "Test-specific linker flags" FORCE) + else() + set(TEST_SPECIFIC_FLAGS "-Wpedantic -Werror -Wcast-align " CACHE INTERNAL "Test-specific compiler flags" FORCE) + set(TEST_SPECIFIC_LINK_FLAGS "" CACHE INTERNAL "Test-specific linker flags" FORCE) + endif() endif() endif() diff --git a/lib/platform_common/qintrin.h b/lib/platform_common/qintrin.h index a4a18b08f..e38958b0c 100644 --- a/lib/platform_common/qintrin.h +++ b/lib/platform_common/qintrin.h @@ -1,9 +1,20 @@ -#pragma once + #pragma once // Header file for the inclusion of plartform specific x86/x64 intrinsics header files. +// +// Historically the project included x86 intrinsics headers unconditionally. +// That causes hard build failures on non-x86 systems (for example Apple +// Silicon) because and related headers declare x86-only +// intrinsics and inline assembly. To remain backward compatible for x86 +// builds while allowing native builds on other architectures, include +// x86 intrinsic headers only when the compiler target is an x86 family. #if defined(_MSC_VER) && !defined(__clang__) #include #else +#if defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86) #include +#else +// Non-x86 targets: do not include x86 intrinsic headers. +#endif #endif diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ff7ad4190..7007d052e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -24,17 +24,34 @@ set_source_files_properties(platform/custom_stack.asm PROPERTIES # Add additional compiler-specific flags for the main application +# NOTE: The x86-specific flags below (for example `-mrdrnd` and +# `-target x86_64-unknown-windows`) must only be applied when the build +# target is x86_64. On non-x86 platforms (Apple Silicon, ARM), those flags +# and intrinsics are invalid and will break the build. The conditional here +# preserves the original x86 behavior while keeping native non-x86 builds +# functional. if(IS_CLANG) # Clang-specific flags - target_compile_options(Qubic PRIVATE - -D_CONSOLE - $<$:-DNDEBUG> - -mrdrnd - -target x86_64-unknown-windows - $<$:--sysroot="${CLANG_SYSROOT}"> - -Werror - -Wno-unused-parameter - ) + if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64") + # x86_64-only flags (rdrand instruction and cross-target) + target_compile_options(Qubic PRIVATE + -D_CONSOLE + $<$:-DNDEBUG> + -mrdrnd + -target x86_64-unknown-windows + $<$:--sysroot="${CLANG_SYSROOT}"> + -Werror + -Wno-unused-parameter + ) + else() + # Generic clang flags for non-x86 platforms (e.g., arm64 mac) + target_compile_options(Qubic PRIVATE + -D_CONSOLE + $<$:-DNDEBUG> + -Werror + -Wno-unused-parameter + ) + endif() elseif(IS_MSVC) # MSVC-specific flags target_compile_options(Qubic PRIVATE diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 6723ec0a1..75b5e1b71 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -25,8 +25,8 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src) include_directories(${PROJECT_ROOT_DIR}) -add_executable( - qubic_core_tests +# Test sources common to all architectures +set(TEST_SOURCES # assets.cpp # common_def.cpp # contract_core.cpp @@ -34,9 +34,7 @@ add_executable( # contract_qvault.cpp # contract_qx.cpp # kangaroo_twelve.cpp - m256.cpp math_lib.cpp - network_messages.cpp # platform.cpp # qpi_collection.cpp # qpi.cpp @@ -50,12 +48,31 @@ add_executable( # vote_counter.cpp ) +# Certain test sources exercise x86-specific intrinsics and codepaths +# (for example, the AVX/m256 implementation). To keep the test target +# buildable on non-x86 platforms (like arm64), only append those sources +# when the build is targeting x86_64. This preserves the original x86 +# test coverage when building on x86, while allowing native non-x86 +# development without intrusive #ifdefs inside the test files. +if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64") + list(APPEND TEST_SOURCES + m256.cpp + network_messages.cpp + ) +endif() + +add_executable(qubic_core_tests ${TEST_SOURCES}) + # Apply test-specific compiler flags from the centralized detection module apply_test_compiler_flags(qubic_core_tests) # Add additional test-specific flags if needed if(IS_CLANG OR IS_GCC) - target_compile_options(qubic_core_tests PRIVATE -mrdrnd) + if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64") + target_compile_options(qubic_core_tests PRIVATE -mrdrnd) + else() + target_compile_options(qubic_core_tests PRIVATE) + endif() endif() From 6e3fd646d846a6743ed5b4bb6d2fdb7dd9e39c9d Mon Sep 17 00:00:00 2001 From: Farasat Ali <63093876+faraasat@users.noreply.github.com> Date: Sat, 13 Dec 2025 12:58:40 +0500 Subject: [PATCH 2/3] Update test/CMakeLists.txt Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- test/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 75b5e1b71..c9b98df98 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -70,8 +70,6 @@ apply_test_compiler_flags(qubic_core_tests) if(IS_CLANG OR IS_GCC) if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64") target_compile_options(qubic_core_tests PRIVATE -mrdrnd) - else() - target_compile_options(qubic_core_tests PRIVATE) endif() endif() From 8c6b467cf576f59cb4bfa59bb3636ab13ec760b5 Mon Sep 17 00:00:00 2001 From: Farasat Ali <63093876+faraasat@users.noreply.github.com> Date: Sat, 13 Dec 2025 12:59:08 +0500 Subject: [PATCH 3/3] Update lib/platform_common/qintrin.h Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- lib/platform_common/qintrin.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/platform_common/qintrin.h b/lib/platform_common/qintrin.h index e38958b0c..1e1a997cc 100644 --- a/lib/platform_common/qintrin.h +++ b/lib/platform_common/qintrin.h @@ -1,6 +1,6 @@ #pragma once -// Header file for the inclusion of plartform specific x86/x64 intrinsics header files. +// Header file for the inclusion of platform specific x86/x64 intrinsics header files. // // Historically the project included x86 intrinsics headers unconditionally. // That causes hard build failures on non-x86 systems (for example Apple