Skip to content

Commit

Permalink
Merge pull request #7 from hyperledger/feature/cmake-fixes
Browse files Browse the repository at this point in the history
Cmake fixes; fix so library compilation error
  • Loading branch information
Warchant authored Apr 2, 2018
2 parents abe080d + 44934df commit 616d5fd
Show file tree
Hide file tree
Showing 31 changed files with 318 additions and 221 deletions.
133 changes: 58 additions & 75 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.5)
set(ED25519_MAJOR_VERSION 1)
set(ED25519_MINOR_VERSION 2)
set(ED25519_PATCH_VERSION 2)
set(ED25519_MINOR_VERSION 3)
set(ED25519_PATCH_VERSION 0)
set(SOVERSION "${ED25519_MAJOR_VERSION}.${ED25519_MINOR_VERSION}.${ED25519_PATCH_VERSION}")
project(ed25519 VERSION ${SOVERSION} LANGUAGES C CXX)

SET(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall")
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -Wextra -O0 -fdiagnostics-color")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -funroll-loops -fomit-frame-pointer")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -Wextra -O0 -fdiagnostics-color ")
SET(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET(CMAKE_EXPORT_COMPILE_COMMANDS "ON")

set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/bin")
set(LIBRARY_OUTPUT_PATH "${PROJECT_BINARY_DIR}")
set(ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")

set(CMAKE_C_STANDARD 11) # force std=c11
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -DNDEBUG")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -O0 -fdiagnostics-color ")

set(CMAKE_CXX_STANDARD 11) # force std=c++11
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 -fdiagnostics-color")

add_compile_options(-Wall)
add_compile_options(-Wextra)
add_compile_options(-Wpedantic)

option(TESTING "Enable testing" ON)
option(COVERAGE "Enable coverage" OFF)
Expand All @@ -27,10 +40,12 @@ if(COVERAGE)
include(cmake/coverage.cmake)
endif()

include(GenerateExportHeader)
include(GNUInstallDirs)
include(cmake/dependencies.cmake)
include(cmake/functions.cmake)
enable_language(ASM)
test_build_amd64(CAN_BUILD_AMD64)


## DEFAULTS
if(NOT EDIMPL)
Expand All @@ -46,26 +61,40 @@ endif()
if(NOT BUILD)
set(BUILD "SHARED")
endif()
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()

## OPTIONS
ENUM(EDIMPL "${EDIMPL}" "Ed25519 implementation"
ref10
amd64-64-24k
amd64-64-24k-pic
)
ref10
amd64-64-24k
amd64-64-24k-pic
)
ENUM(HASH "${HASH}" "SHA implementation"
sha2_openssl
sha3_brainhub
)
sha2_openssl
sha3_brainhub
)
ENUM(RANDOM "${RANDOM}" "RNG implementation"
rand_openssl
dev_random
dev_urandom
)
rand_openssl
dev_random
dev_urandom
)
ENUM(BUILD "${BUILD}" "library build type"
SHARED
STATIC
)
SHARED
STATIC
)
ENUM(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" "cmake build type"
Release
Debug
)

if(BUILD STREQUAL "SHARED" AND EDIMPL STREQUAL "amd64-64-24k")
message(FATAL_ERROR
"EDIMPL=amd64-64-24k implementation has no PIC fixes, "
"therefore SHARED library can not be built. "
"Use EDIMPL=amd64-64-24k-pic or BUILD=STATIC")
endif()

include_directories(${CMAKE_SOURCE_DIR}/include)

Expand All @@ -74,55 +103,9 @@ if(BENCHMARKING)
add_subdirectory(benchmark)
endif()


set(LIBED25519_VERSION "${SOVERSION}-${EDIMPL}-${HASH}-${RANDOM}")

add_library(ed25519 ${BUILD}
src/ed25519.c
$<TARGET_OBJECTS:${EDIMPL}>
$<TARGET_OBJECTS:${HASH}>
$<TARGET_OBJECTS:${RANDOM}>
)
set_target_properties(ed25519 PROPERTIES
VERSION ${SOVERSION}
SOVERSION ${SOVERSION}
INTERFACE_ed25519_MAJOR_VERSION ${ED25519_MAJOR_VERSION}
PUBLIC_HEADER include/ed25519/ed25519.h
)
set_property(
TARGET ed25519
APPEND PROPERTY
COMPATIBLE_INTERFACE_STRING ed25519_MAJOR_VERSION
)
target_compile_definitions(ed25519 PUBLIC
-DLIBED25519_VERSION=${LIBED25519_VERSION}
)

install(TARGETS ed25519 EXPORT ed25519Config
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}/static
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
FRAMEWORK DESTINATION ${CMAKE_INSTALL_PREFIX}
)
install(
DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(EXPORT ed25519Config DESTINATION share/ed25519/cmake)
export(TARGETS ed25519 FILE ed25519Config.cmake)
target_include_directories(ed25519 PUBLIC
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
add_subdirectory(src)

if(TESTING)
enable_testing()
add_subdirectory(test)
endif()

set_target_properties(${EDIMPL} PROPERTIES EXCLUDE_FROM_ALL FALSE)
set_target_properties(${HASH} PROPERTIES EXCLUDE_FROM_ALL FALSE)
set_target_properties(${RANDOM} PROPERTIES EXCLUDE_FROM_ALL FALSE)

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ During CMake time, users are able to choose any of these implementations using c

- `EDIMPL`
- `ref10` - portable C implementation.
- `amd64-64-24k` - optimized C and ASM implementation, works only on Linux amd64.
- `amd64-64-24k` - optimized C and ASM implementation, works only on Linux amd64. This implementation can be selected only for `BUILD=STATIC`.
- `amd64-64-24k-pic` - same as `amd64-64-24k`, but has fixes in ASM files, to allow *position independent code* (`-fPIC`) builds.
- `HASH`
- `sha2_openssl`
Expand Down
8 changes: 4 additions & 4 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ find_package(PackageHandleStandardArgs)

include(ExternalProject)
set(EP_PREFIX "${PROJECT_SOURCE_DIR}/external")
set_directory_properties(PROPERTIES
EP_PREFIX ${EP_PREFIX}
)
set_directory_properties(PROPERTIES EP_PREFIX ${EP_PREFIX})

if(TESTING)
find_package(gtest)
endif()
if(BENCHMARKING)
find_package(benchmark)
endif()
endif()

find_package(OpenSSL)
68 changes: 62 additions & 6 deletions cmake/functions.cmake
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
function(addtest test_name)
set(SOURCES ${ARGN})
add_executable(${test_name} ${SOURCES})
target_link_libraries(${test_name} gtest gmock)
target_link_libraries(${test_name} PUBLIC gtest gmock)
add_test(
NAME ${test_name}
COMMAND $<TARGET_FILE:${test_name}>
)

set_target_properties(${test_name} PROPERTIES
EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin
ARCHIVE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib/static
LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib
)
endfunction()

# Creates benchmark "bench_name", with "SOURCES" (use string as second argument)
function(addbenchmark bench_name SOURCES)
add_executable(${bench_name} ${SOURCES})
# Creates benchmark "bench_name"
function(addbenchmark bench_name)
add_executable(${bench_name} ${ARGN})
target_link_libraries(${bench_name} PRIVATE benchmark)
strictmode(${bench_name})

set_target_properties(${bench_name} PROPERTIES
EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin
ARCHIVE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib/static
LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib
)
endfunction()


Expand All @@ -32,7 +43,7 @@ function(ENUM variable check description)
list(APPEND opts_pretty "]")
JOIN("${opts_pretty}" "" opts_pretty)

# message(STATUS "${variable}=${opts_pretty}")
set_property (GLOBAL PROPERTY ENUM_${variable}_OPTIONS "${options}")

# get the length of options
list(LENGTH options options_len)
Expand Down Expand Up @@ -86,3 +97,48 @@ function(gethash target out)
endif()
endfunction()

function(ed25519_add_library LIBNAME)
set(options "")
set(oneValueArgs "")
set(multiValueArgs SOURCES INCLUDES LINK_LIBRARIES COMPILE_DEFINITIONS)
cmake_parse_arguments(LIB_${LIBNAME} "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

# for every sourcefile, replace relative path with absolute path to source file
foreach(file ${LIB_${LIBNAME}_SOURCES})
get_filename_component(abspath ${file} REALPATH)
list(APPEND LIB_${LIBNAME}_SOURCES ${abspath})
list(REMOVE_AT LIB_${LIBNAME}_SOURCES 0)
endforeach()

foreach(j ${multiValueArgs})
set_property (GLOBAL PROPERTY LIB_${LIBNAME}_${j} "${LIB_${LIBNAME}_${j}}")
endforeach()

add_library(${LIBNAME} STATIC
${LIB_${LIBNAME}_SOURCES}
)
target_link_libraries(${LIBNAME} PUBLIC
${LIB_${LIBNAME}_LINK_LIBRARIES}
)
target_compile_definitions(${LIBNAME} PUBLIC
"${LIB_${LIBNAME}_COMPILE_DEFINITIONS}"
)
target_include_directories(${LIBNAME} PUBLIC
${LIB_${LIBNAME}_INCLUDES}
)
set_target_properties(${LIBNAME} PROPERTIES
EXCLUDE_FROM_ALL TRUE
EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin
ARCHIVE_OUTPUT_PATH ${CMAKE_BINARY_DIR}
LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}
)
endfunction(ed25519_add_library)


function(test_build_amd64 OUT)
if (UNIX AND CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|AMD64.*|x86_64.*")
set(${OUT} TRUE PARENT_SCOPE)
else()
set(${OUT} FALSE PARENT_SCOPE)
endif()
endfunction()
8 changes: 4 additions & 4 deletions example/CMakeLists.txt → example/sign-verify/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ project(ed25519_example C)
find_package(ed25519 REQUIRED)

add_executable(test1
main.c
)
main.c
)
target_link_libraries(test1
ed25519
)
ed25519
)
2 changes: 1 addition & 1 deletion example/main.c → example/sign-verify/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int main(int arc, char** argv) {
int ret = ed25519_create_keypair(&priv, &pub);
assert(ret != 0); // status code: ok

const int size = 7;
const int size = 7;
unsigned char *buff = "ed25519";

signature_t sig;
Expand Down
24 changes: 24 additions & 0 deletions exportmap
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
global:
ed25519_create_keypair;
ed25519_derive_public_key;
ed25519_sign;
ed25519_verify;

sha512_init;
sha512_final;
sha512_update;
sha512;
SHA_512_CONTEXT_SIZE;

sha256_init;
sha256_final;
sha256_update;
sha256;
SHA_256_CONTEXT_SIZE;

randombytes;


local: *;
};
24 changes: 4 additions & 20 deletions lib/ed25519/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
function(configedimpl target)
set_target_properties(${target} PROPERTIES EXCLUDE_FROM_ALL TRUE)

install(TARGETS ${LIB} EXPORT ed25519Config
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

target_include_directories(${target} PUBLIC
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
endfunction()

if(EDIMPL STREQUAL "amd64-64-24k")
if(CAN_BUILD_AMD64)
add_subdirectory(amd64-64-24k)
elseif(EDIMPL STREQUAL "amd64-64-24k-pic")
add_subdirectory(amd64-64-24k-pic)
elseif(EDIMPL STREQUAL "ref10")
add_subdirectory(ref10)
else()
message(FATAL_ERROR "No such EDIMPL implementation ${EDIMPL}")
message(STATUS "amd64-64-24k targets are not available on your platform")
endif()

add_subdirectory(ref10)
13 changes: 4 additions & 9 deletions lib/ed25519/amd64-64-24k-pic/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
enable_language(ASM)
set(LIB amd64-64-24k-pic)

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
FILE(GLOB sources ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
FILE(GLOB asm ${CMAKE_CURRENT_SOURCE_DIR}/*.s)
add_library(${LIB} OBJECT
${sources}
${asm}
FILE(GLOB sources ${CMAKE_CURRENT_SOURCE_DIR}/*.[cs])

ed25519_add_library(amd64-64-24k-pic
SOURCES ${sources}
)
set_target_properties(${LIB} PROPERTIES EXCLUDE_FROM_ALL TRUE)
Loading

0 comments on commit 616d5fd

Please sign in to comment.