From 47a3763bcd9f6d3f9d073f6728971d27686de96f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ondrej=20Mosn=C3=A1=C4=8Dek?= Date: Wed, 19 Apr 2017 11:22:26 +0200 Subject: [PATCH] Add CMake build files This commit adds the files necessary to build this project with CMake. The CMake build automatically detects x86 extensions and necessary compiler flags. --- .gitignore | 7 ++ CMakeLists.txt | 120 +++++++++++++++++++++++++ arch/x86_64/src/test-feature-avx2.c | 8 ++ arch/x86_64/src/test-feature-avx512f.c | 8 ++ arch/x86_64/src/test-feature-sse2.c | 8 ++ arch/x86_64/src/test-feature-ssse3.c | 8 ++ arch/x86_64/src/test-feature-xop.c | 8 ++ 7 files changed, 167 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 arch/x86_64/src/test-feature-avx2.c create mode 100644 arch/x86_64/src/test-feature-avx512f.c create mode 100644 arch/x86_64/src/test-feature-sse2.c create mode 100644 arch/x86_64/src/test-feature-ssse3.c create mode 100644 arch/x86_64/src/test-feature-xop.c diff --git a/.gitignore b/.gitignore index a302fb4..5bff3d0 100644 --- a/.gitignore +++ b/.gitignore @@ -61,3 +61,10 @@ # Vim *.swp + +# CMake +CMakeFiles/ +*.cmake +CMakeCache.txt +Makefile + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4ea8f8a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,120 @@ +cmake_minimum_required(VERSION 2.6) + +project(Argon2 C) +set(ARGON2_VERSION 1.0) +set(BINARY_INSTALL_DIR /usr/local/bin) +set(LIBRARY_INSTALL_DIR /usr/local/lib) +set(INCLUDE_INSTALL_DIR /usr/local/include) +set(CMAKE_C_STANDARD 90) +set(CMAKE_C_STANDARD_REQUIRED ON) + +include(CheckCSourceCompiles) +find_package(Threads REQUIRED) + +add_library(argon2-interface INTERFACE) +target_include_directories(argon2-interface INTERFACE + $ + $ +) + +add_library(argon2-internal INTERFACE) +target_include_directories(argon2-internal INTERFACE lib lib/blake2) +target_link_libraries(argon2-internal INTERFACE argon2-interface) + +add_library(argon2 SHARED + lib/argon2.c + lib/core.c + lib/encoding.c + lib/genkat.c + lib/impl-select.c + lib/thread.c + lib/blake2/blake2.c +) +target_link_libraries(argon2 + PUBLIC argon2-interface ${CMAKE_THREAD_LIBS_INIT} + PRIVATE argon2-internal +) + +set_property(TARGET argon2 PROPERTY VERSION ${Upstream_VERSION}) +set_property(TARGET argon2 PROPERTY SOVERSION 1) +set_property(TARGET argon2 PROPERTY INTERFACE_ARGON2_MAJOR_VERSION 1) +set_property(TARGET argon2 APPEND PROPERTY + COMPATIBLE_INTERFACE_STRING ARGON2_MAJOR_VERSION +) + +if(CMAKE_SYSTEM_PROCESSOR STREQUAL x86_64) + function(add_feature_impl FEATURE GCC_FLAG DEF) + add_library(argon2-${FEATURE} STATIC + arch/x86_64/lib/argon2-${FEATURE}.c + ) + target_link_libraries(argon2-${FEATURE} PRIVATE argon2-internal) + + message("-- Detecting feature '${FEATURE}'...") + file(READ arch/x86_64/src/test-feature-${FEATURE}.c SOURCE_CODE) + + # try without flag: + check_c_source_compiles("${SOURCE_CODE}" FEATURE_${FEATURE}_NOFLAG) + set(HAS_FEATURE ${FEATURE_${FEATURE}_NOFLAG}) + if(NOT "${HAS_FEATURE}") + # try with -m flag: + set(CMAKE_REQUIRED_FLAGS ${GCC_FLAG}) + check_c_source_compiles("${SOURCE_CODE}" FEATURE_${FEATURE}_FLAG) + set(CMAKE_REQUIRED_FLAGS "") + + set(HAS_FEATURE ${FEATURE_${FEATURE}_FLAG}) + if(${HAS_FEATURE}) + target_compile_options(argon2-${FEATURE} PRIVATE ${GCC_FLAG}) + endif() + endif() + + if(${HAS_FEATURE}) + message("-- Feature '${FEATURE}' detected!") + target_compile_definitions(argon2-${FEATURE} PRIVATE ${DEF}) + endif() + target_link_libraries(argon2 PUBLIC argon2-${FEATURE}) + endfunction() + + target_include_directories(argon2-internal INTERFACE arch/x86_64/lib) + + add_feature_impl(sse2 -msse2 HAVE_SSE2) + add_feature_impl(ssse3 -mssse3 HAVE_SSSE3) + add_feature_impl(xop -mxop HAVE_XOP) + add_feature_impl(avx2 -mavx2 HAVE_AVX2) + add_feature_impl(avx512f -mavx512f HAVE_AVX512F) + + target_sources(argon2 PRIVATE + arch/x86_64/lib/argon2-arch.c + arch/x86_64/lib/cpu-flags.c + ) +else() + target_sources(argon2 PRIVATE + arch/generic/lib/argon2-arch.c + ) +endif() + +add_executable(argon2-exec src/run.c) +target_link_libraries(argon2-exec argon2 argon2-internal) +target_include_directories(argon2-exec PRIVATE src) +set_target_properties(argon2-exec PROPERTIES OUTPUT_NAME argon2) + +add_executable(argon2-genkat src/genkat.c) +target_include_directories(argon2-genkat PRIVATE src) +target_link_libraries(argon2-genkat argon2) + +add_executable(argon2-bench2 src/bench2.c) +target_include_directories(argon2-bench2 PRIVATE src) +target_link_libraries(argon2-bench2 argon2) + +add_executable(argon2-test tests/test.c) +target_include_directories(argon2-test PRIVATE tests) +target_link_libraries(argon2-test argon2) + +add_test(test argon2-test) + +install(TARGETS argon2 DESTINATION ${LIBRARY_INSTALL_DIR}) +install(FILES + include/argon2.h + include/argon2-opt.h + DESTINATION ${INCLUDE_INSTALL_DIR} +) +install(TARGETS argon2-exec DESTINATION ${BINARY_INSTALL_DIR}) diff --git a/arch/x86_64/src/test-feature-avx2.c b/arch/x86_64/src/test-feature-avx2.c new file mode 100644 index 0000000..01e5b15 --- /dev/null +++ b/arch/x86_64/src/test-feature-avx2.c @@ -0,0 +1,8 @@ +#include + +void function_avx2(__m256i *dst, const __m256i *a, const __m256i *b) +{ + *dst = _mm256_xor_si256(*a, *b); +} + +int main(void) { return 0; } diff --git a/arch/x86_64/src/test-feature-avx512f.c b/arch/x86_64/src/test-feature-avx512f.c new file mode 100644 index 0000000..372eb25 --- /dev/null +++ b/arch/x86_64/src/test-feature-avx512f.c @@ -0,0 +1,8 @@ +#include + +void function_avx512f(__m512i *dst, const __m512i *a) +{ + *dst = _mm512_ror_epi64(*a, 57); +} + +int main(void) { return 0; } diff --git a/arch/x86_64/src/test-feature-sse2.c b/arch/x86_64/src/test-feature-sse2.c new file mode 100644 index 0000000..4a3b70f --- /dev/null +++ b/arch/x86_64/src/test-feature-sse2.c @@ -0,0 +1,8 @@ +#include + +void function_sse2(__m128i *dst, const __m128i *a, const __m128i *b) +{ + *dst = _mm_xor_si128(*a, *b); +} + +int main(void) { return 0; } diff --git a/arch/x86_64/src/test-feature-ssse3.c b/arch/x86_64/src/test-feature-ssse3.c new file mode 100644 index 0000000..db2b355 --- /dev/null +++ b/arch/x86_64/src/test-feature-ssse3.c @@ -0,0 +1,8 @@ +#include + +void function_ssse3(__m128i *dst, const __m128i *a, const __m128i *b) +{ + *dst = _mm_shuffle_epi8(*a, *b); +} + +int main(void) { return 0; } diff --git a/arch/x86_64/src/test-feature-xop.c b/arch/x86_64/src/test-feature-xop.c new file mode 100644 index 0000000..5897fe1 --- /dev/null +++ b/arch/x86_64/src/test-feature-xop.c @@ -0,0 +1,8 @@ +#include + +void function_xop(__m128i *dst, const __m128i *a, int b) +{ + *dst = _mm_roti_epi64(*a, b); +} + +int main(void) { return 0; }