Verified Commit ece2ac4a authored by Tom Zander's avatar Tom Zander

[CMake] fix misdetection of CPU features

Upgrade the code to check not just if the compiler supports, but
try the actual system too.

Also add a new cmake option 'reproductable_builds' which, when set
to true, will not include any optimized hashing algorithms.
parent a5dd25db
Pipeline #54883269 passed with stages
in 51 minutes and 46 seconds
......@@ -162,8 +162,8 @@ set (WORDS_BIGENDIAN ${CMAKE_WORDS_BIGENDIAN})
include (TestBigEndian)
test_big_endian(CMAKE_WORDS_BIGENDIAN)
include(CheckCXXSourceCompiles)
check_cxx_source_compiles(
include(CheckCSourceCompiles)
check_c_source_compiles(
"#include <string.h>
int main() {
char buffer[1];
......@@ -177,7 +177,7 @@ check_cxx_source_compiles(
set (_V "CPP11_ARGS")
foreach (ARG "" "-std=gnu++11" "-std=c++11")
set (CMAKE_REQUIRED_FLAGS ${ARG})
check_cxx_source_compiles(
check_c_source_compiles(
"#if __cplusplus < 201103L
# error
#endif
......@@ -191,19 +191,6 @@ foreach (ARG "" "-std=gnu++11" "-std=c++11")
set (_V "${_V}_")
endforeach()
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-msse4 -msse4.1" SSE41)
if (${SSE41})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4 -msse4.1 -DENABLE_SSE41")
endif()
check_cxx_compiler_flag("-msse4 -msha" SHANI)
if (${SHANI})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4 -msha -DENABLE_SHANI")
endif()
check_cxx_compiler_flag("-mavx -mavx2" AVX2)
if (${AVX2})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx -mavx2 -DENABLE_AVX2")
endif()
message ("===== Find optional dependencies")
......
# This file is part of the Flowee project
# Copyright (C) 2018 Tom Zander <tomz@freedommail.ch>
# Copyright (C) 2018-2019 Tom Zander <tomz@freedommail.ch>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -16,15 +16,52 @@
include_directories(${LIBCRYPTO_INCLUDES})
add_library(flowee_crypto STATIC
set (FLOWEE_LIBRARY_SOURCES
aes.cpp
hmac_sha256.cpp
hmac_sha512.cpp
ripemd160.cpp
sha1.cpp
sha256_avx2.cpp
sha256_shani.cpp
sha256_sse41.cpp
sha256.cpp
sha512.cpp
)
option(reproducable_build "Turn off special CPU features even if available" OFF)
if (reproducable_build)
message ("* Reproductable builds turned on, not compiling in fast hashing")
else ()
include(CheckCSourceRuns)
set (CMAKE_REQUIRED_FLAGS "-msse4 -msse4.1")
check_c_source_runs("#include <stdint.h>
#include <immintrin.h>
int main() { __m128i l = _mm_set1_epi32(0); return _mm_extract_epi32(l, 3); }" SSE41)
if (${SSE41})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4 -msse4.1 -DENABLE_SSE41")
list(APPEND FLOWEE_LIBRARY_SOURCES sha256_sse41.cpp)
endif()
set (CMAKE_REQUIRED_FLAGS "-msse4 -msha")
check_c_source_runs("#include <stdint.h>
#include <immintrin.h>
int main() {
__m128i i = _mm_set1_epi32(0);
__m128i j = _mm_set1_epi32(1);
__m128i k = _mm_set1_epi32(2);
return _mm_extract_epi32(_mm_sha256rnds2_epu32(i, i, k), 0);
}" SHANI)
if (${SHANI})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4 -msha -DENABLE_SHANI")
list(APPEND FLOWEE_LIBRARY_SOURCES sha256_shani.cpp)
endif()
set (CMAKE_REQUIRED_FLAGS "-mavx -mavx2")
check_c_source_runs("#include <stdint.h>
#include <immintrin.h>
int main() {__m256i l = _mm256_set1_epi32(0); return _mm256_extract_epi32(l, 7);}" AVX2)
if (${AVX2})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx -mavx2 -DENABLE_AVX2")
list(APPEND FLOWEE_LIBRARY_SOURCES sha256_avx2.cpp)
endif()
endif()
add_library(flowee_crypto STATIC ${FLOWEE_LIBRARY_SOURCES})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment