Skip to content

Commit

Permalink
cmake changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fpelliccioni committed Jul 20, 2017
1 parent d992fdf commit e2d3029
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 44 deletions.
58 changes: 14 additions & 44 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,10 @@ set(WITH_FIELD "auto" CACHE STRING "Specify Field Implementation (64bit|32bit|au
set(WITH_SCALAR "auto" CACHE STRING "Specify scalar implementation (64bit|32bit|auto).")
set(WITH_BIGNUM "auto" CACHE STRING "Specify Bignum Implementation (gmp|no|auto).")

function(_check_has_64bit_asm)
if (DEFINED HAS_64BIT_ASM)
return()
endif()

set(_filename "${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/_has_64bit_asm.c")
file(WRITE ${_filename}
"#include <stdint.h>
int main() {
uint64_t a = 11, tmp;
__asm__ __volatile__(\"movq 0x100000000,%1; mulq %%rsi\" : \"+a\"(a) : \"S\"(tmp) : \"cc\", \"%rdx\");
}")
try_compile(HAS_64BIT_ASM "${CMAKE_BINARY_DIR}" ${_filename})
set(HAS_64BIT_ASM ${HAS_64BIT_ASM} PARENT_SCOPE)
file(REMOVE ${_filename})
endfunction()

function(_check_has_int128)
if (DEFINED HAS_INT128)
return()
endif()

set(_filename "${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/_has_int128_test.c")
file(WRITE ${_filename}
"int main() { __int128 x = 0; }")
try_compile(HAS_INT128 "${CMAKE_BINARY_DIR}" ${_filename})
set(HAS_INT128 ${HAS_INT128} PARENT_SCOPE)
file(REMOVE ${_filename})
endfunction()

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
function(_check_has_gmp)
if (DEFINED HAS_GMP)
return()
endif()

find_package(GMP)
set(HAS_GMP ${GMP_FOUND} PARENT_SCOPE)
endfunction()
include(Secp256k1Tools)


# Implement --with-asm
#------------------------------------------------------------------------------
Expand Down Expand Up @@ -211,32 +176,32 @@ set_target_properties(secp256k1 PROPERTIES FOLDER "secp256k1")
#==============================================================================
if (ENABLE_BENCHMARK)
add_executable(bench_verify src/bench_verify.c)
target_link_libraries(bench_verify secp256k1)
target_link_libraries(bench_verify secp256k1::secp256k1)
set_target_properties(bench_verify PROPERTIES FOLDER "secp256k1")

add_executable(bench_sign src/bench_sign.c)
target_link_libraries(bench_sign secp256k1)
target_link_libraries(bench_sign secp256k1::secp256k1)
set_target_properties(bench_sign PROPERTIES FOLDER "secp256k1")

add_executable(bench_internal src/bench_internal.c)
target_link_libraries(bench_internal secp256k1)
target_link_libraries(bench_internal secp256k1::secp256k1)
set_target_properties(bench_internal PROPERTIES FOLDER "secp256k1")

if (ENABLE_MODULE_ECDH)
add_executable(bench_ecdh src/bench_ecdh.c)
target_link_libraries(bench_ecdh secp256k1)
target_link_libraries(bench_ecdh secp256k1::secp256k1)
set_target_properties(bench_ecdh PROPERTIES FOLDER "secp256k1")
endif()

if (ENABLE_MODULE_SCHNORR)
add_executable(bench_schnorr_verify src/bench_schnorr_verify.c)
target_link_libraries(bench_schnorr_verify secp256k1)
target_link_libraries(bench_schnorr_verify secp256k1::secp256k1)
set_target_properties(bench_schnorr_verify PROPERTIES FOLDER "secp256k1")
endif()

if (ENABLE_MODULE_RECOVERY)
add_executable(bench_recover src/bench_recover.c)
target_link_libraries(bench_recover secp256k1)
target_link_libraries(bench_recover secp256k1::secp256k1)
set_target_properties(bench_recover PROPERTIES FOLDER "secp256k1")
endif()
endif()
Expand All @@ -246,10 +211,12 @@ endif()
if (ENABLE_TESTS)
add_executable(tests
src/tests.c)

target_compile_definitions(tests PRIVATE -DVERIFY)
target_include_directories(tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(tests secp256k1)

target_link_libraries(tests secp256k1::secp256k1)

enable_testing()
add_test(
Expand All @@ -265,12 +232,15 @@ install(TARGETS secp256k1
EXPORT secp256k1
ARCHIVE DESTINATION lib)
install(FILES include/secp256k1.h DESTINATION include)

if (ENABLE_MODULE_ECDH)
install(FILES include/secp256k1_ecdh.h DESTINATION include)
endif()

if (ENABLE_MODULE_SCHNORR)
install(FILES include/secp256k1_schnorr.h DESTINATION include)
endif()

if (ENABLE_MODULE_RECOVERY)
install(FILES include/secp256k1_recovery.h DESTINATION include)
endif()
Expand Down
59 changes: 59 additions & 0 deletions cmake/Secp256k1Tools.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#
# Copyright (c) 2017 Bitprim developers (see AUTHORS)
#
# This file is part of Bitprim.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#


function(_check_has_64bit_asm)
if (DEFINED HAS_64BIT_ASM)
return()
endif()

set(_filename "${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/_has_64bit_asm.c")
file(WRITE ${_filename}
"#include <stdint.h>
int main() {
uint64_t a = 11, tmp;
__asm__ __volatile__(\"movq 0x100000000,%1; mulq %%rsi\" : \"+a\"(a) : \"S\"(tmp) : \"cc\", \"%rdx\");
}")
try_compile(HAS_64BIT_ASM "${CMAKE_BINARY_DIR}" ${_filename})
set(HAS_64BIT_ASM ${HAS_64BIT_ASM} PARENT_SCOPE)
file(REMOVE ${_filename})
endfunction()

function(_check_has_int128)
if (DEFINED HAS_INT128)
return()
endif()

set(_filename "${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/_has_int128_test.c")
file(WRITE ${_filename}
"int main() { __int128 x = 0; }")
try_compile(HAS_INT128 "${CMAKE_BINARY_DIR}" ${_filename})
set(HAS_INT128 ${HAS_INT128} PARENT_SCOPE)
file(REMOVE ${_filename})
endfunction()

function(_check_has_gmp)
if (DEFINED HAS_GMP)
return()
endif()

find_package(GMP)
set(HAS_GMP ${GMP_FOUND} PARENT_SCOPE)
endfunction()

0 comments on commit e2d3029

Please sign in to comment.