Skip to content

Commit

Permalink
Add CMake build files
Browse files Browse the repository at this point in the history
This commit adds the files necessary to build this project with CMake.
The CMake build automatically detects x86 extensions and necessary
compiler flags.
  • Loading branch information
WOnder93 committed Apr 19, 2017
1 parent 3f1fbf5 commit 47a3763
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@

# Vim
*.swp

# CMake
CMakeFiles/
*.cmake
CMakeCache.txt
Makefile

120 changes: 120 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)

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<feature> 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})
8 changes: 8 additions & 0 deletions arch/x86_64/src/test-feature-avx2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <x86intrin.h>

void function_avx2(__m256i *dst, const __m256i *a, const __m256i *b)
{
*dst = _mm256_xor_si256(*a, *b);
}

int main(void) { return 0; }
8 changes: 8 additions & 0 deletions arch/x86_64/src/test-feature-avx512f.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <x86intrin.h>

void function_avx512f(__m512i *dst, const __m512i *a)
{
*dst = _mm512_ror_epi64(*a, 57);
}

int main(void) { return 0; }
8 changes: 8 additions & 0 deletions arch/x86_64/src/test-feature-sse2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <x86intrin.h>

void function_sse2(__m128i *dst, const __m128i *a, const __m128i *b)
{
*dst = _mm_xor_si128(*a, *b);
}

int main(void) { return 0; }
8 changes: 8 additions & 0 deletions arch/x86_64/src/test-feature-ssse3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <x86intrin.h>

void function_ssse3(__m128i *dst, const __m128i *a, const __m128i *b)
{
*dst = _mm_shuffle_epi8(*a, *b);
}

int main(void) { return 0; }
8 changes: 8 additions & 0 deletions arch/x86_64/src/test-feature-xop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <x86intrin.h>

void function_xop(__m128i *dst, const __m128i *a, int b)
{
*dst = _mm_roti_epi64(*a, b);
}

int main(void) { return 0; }

0 comments on commit 47a3763

Please sign in to comment.