From daa501036cbb2afd521dc66d54cf63d57cdb3e1e Mon Sep 17 00:00:00 2001 From: paperchalice Date: Wed, 29 Jan 2025 11:43:51 +0800 Subject: [PATCH] [libtommath] Use cmake --- ports/libtommath/bcrypt.patch | 50 +++++++++++++ ports/libtommath/has-set-double.patch | 14 ++++ ports/libtommath/import-lib.patch | 12 ++++ ports/libtommath/msvc-dce.patch | 21 ++++++ ports/libtommath/portfile.cmake | 100 +++++--------------------- ports/libtommath/usage | 9 +++ ports/libtommath/vcpkg.json | 13 +++- 7 files changed, 135 insertions(+), 84 deletions(-) create mode 100644 ports/libtommath/bcrypt.patch create mode 100644 ports/libtommath/has-set-double.patch create mode 100644 ports/libtommath/import-lib.patch create mode 100644 ports/libtommath/msvc-dce.patch create mode 100644 ports/libtommath/usage diff --git a/ports/libtommath/bcrypt.patch b/ports/libtommath/bcrypt.patch new file mode 100644 index 00000000000000..9bbe73b6fc4549 --- /dev/null +++ b/ports/libtommath/bcrypt.patch @@ -0,0 +1,50 @@ +diff --git a/bn_s_mp_rand_platform.c b/bn_s_mp_rand_platform.c +--- a/bn_s_mp_rand_platform.c ++++ b/bn_s_mp_rand_platform.c +@@ -32,20 +32,20 @@ + #include + + static mp_err s_read_wincsp(void *p, size_t n) + { +- static HCRYPTPROV hProv = 0; +- if (hProv == 0) { +- HCRYPTPROV h = 0; +- if (!CryptAcquireContext(&h, NULL, MS_DEF_PROV, PROV_RSA_FULL, +- (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) && +- !CryptAcquireContext(&h, NULL, MS_DEF_PROV, PROV_RSA_FULL, +- CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) { ++ static BCRYPT_ALG_HANDLE hAlg = 0; ++ if (hAlg == 0) { ++ BCRYPT_ALG_HANDLE h = 0; ++ NTSTATUS status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_RSA_ALGORITHM, NULL, ++ BCRYPT_HASH_REUSABLE_FLAG); ++ if(!BCRYPT_SUCCESS(status)) { + return MP_ERR; + } +- hProv = h; ++ hAlg = h; + } +- return CryptGenRandom(hProv, (DWORD)n, (BYTE *)p) == TRUE ? MP_OKAY : MP_ERR; ++ NTSTATUS status = BCryptGenRandom(hAlg, (PUCHAR)p, (ULONG)n, 0); ++ return BCRYPT_SUCCESS(status) ? MP_OKAY : MP_ERR; + } + #endif /* WIN32 */ + + #if !defined(BN_S_READ_WINCSP_C) && defined(__linux__) && defined(__GLIBC_PREREQ) +diff --git a/CMakeLists.txt b/CMakeLists.txt +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -117,8 +117,13 @@ + ) + target_link_options(${PROJECT_NAME} BEFORE PRIVATE + ${LTM_LD_FLAGS} + ) ++if(WIN32) ++ target_link_options(${PROJECT_NAME} PRIVATE ++ bcrypt.lib ++ ) ++endif() + + set(PUBLIC_HEADERS tommath.h) + set(C89 False CACHE BOOL "(Usually maintained automatically) Enable when the library is in c89 mode to package the correct header files on install") + if(C89) diff --git a/ports/libtommath/has-set-double.patch b/ports/libtommath/has-set-double.patch new file mode 100644 index 00000000000000..6e275d75745326 --- /dev/null +++ b/ports/libtommath/has-set-double.patch @@ -0,0 +1,14 @@ +diff --git a/bn_mp_set_double.c b/bn_mp_set_double.c +--- a/bn_mp_set_double.c ++++ b/bn_mp_set_double.c +@@ -2,9 +2,9 @@ + #ifdef BN_MP_SET_DOUBLE_C + /* LibTomMath, multiple-precision integer library -- Tom St Denis */ + /* SPDX-License-Identifier: Unlicense */ + +-#if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) ++#if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) || defined(_MSC_VER) + mp_err mp_set_double(mp_int *a, double b) + { + uint64_t frac; + int exp; diff --git a/ports/libtommath/import-lib.patch b/ports/libtommath/import-lib.patch new file mode 100644 index 00000000000000..ebce1c3890ffac --- /dev/null +++ b/ports/libtommath/import-lib.patch @@ -0,0 +1,12 @@ +diff --git a/sources.cmake b/sources.cmake +--- a/sources.cmake ++++ b/sources.cmake +@@ -171,4 +171,8 @@ + tommath_cutoffs.h + tommath_private.h + tommath_superclass.h + ) ++ ++if(WIN32) ++ list(APPEND SOURCES tommath.def) ++endif() diff --git a/ports/libtommath/msvc-dce.patch b/ports/libtommath/msvc-dce.patch new file mode 100644 index 00000000000000..342c012bb56ebc --- /dev/null +++ b/ports/libtommath/msvc-dce.patch @@ -0,0 +1,21 @@ +diff --git a/bn_s_mp_rand_platform.c b/bn_s_mp_rand_platform.c +--- a/bn_s_mp_rand_platform.c ++++ b/bn_s_mp_rand_platform.c +@@ -136,13 +136,17 @@ + + mp_err s_mp_rand_platform(void *p, size_t n) + { + mp_err err = MP_ERR; ++ #ifndef _MSC_VER + if ((err != MP_OKAY) && MP_HAS(S_READ_ARC4RANDOM)) err = s_read_arc4random(p, n); ++ #endif + if ((err != MP_OKAY) && MP_HAS(S_READ_WINCSP)) err = s_read_wincsp(p, n); ++ #ifndef _MSC_VER + if ((err != MP_OKAY) && MP_HAS(S_READ_GETRANDOM)) err = s_read_getrandom(p, n); + if ((err != MP_OKAY) && MP_HAS(S_READ_URANDOM)) err = s_read_urandom(p, n); + if ((err != MP_OKAY) && MP_HAS(S_READ_LTM_RNG)) err = s_read_ltm_rng(p, n); ++ #endif + return err; + } + + #endif diff --git a/ports/libtommath/portfile.cmake b/ports/libtommath/portfile.cmake index 6448f1e0a6fbde..6237e8a9a897f5 100644 --- a/ports/libtommath/portfile.cmake +++ b/ports/libtommath/portfile.cmake @@ -4,88 +4,22 @@ vcpkg_from_github( REF "v${VERSION}" SHA512 3dbd7053a670afa563a069a9785f1aa4cab14a210bcd05d8fc7db25bd3dcce36b10a3f4f54ca92d75a694f891226f01bdf6ac15bacafeb93a8be6b04c579beb3 HEAD_REF develop + PATCHES + bcrypt.patch + import-lib.patch + has-set-double.patch # Remove in next release. + msvc-dce.patch # This is a won't fix, see https://github.com/libtom/libtommath/blob/develop/s_mp_rand_platform.c#L120-L138 ) -if(VCPKG_TARGET_IS_WINDOWS) - vcpkg_check_linkage(ONLY_STATIC_LIBRARY) - - if(VCPKG_CRT_LINKAGE STREQUAL "dynamic") - set(CRTFLAG "/MD") - else() - set(CRTFLAG "/MT") - endif() - - # Make sure we start from a clean slate - vcpkg_execute_build_process( - COMMAND nmake -f ${SOURCE_PATH}/makefile.msvc clean - WORKING_DIRECTORY ${SOURCE_PATH} - LOGNAME clean-${TARGET_TRIPLET}-dbg - ) - - #Debug Build - vcpkg_execute_build_process( - COMMAND nmake -f ${SOURCE_PATH}/makefile.msvc CFLAGS="${CRTFLAG}d" - WORKING_DIRECTORY ${SOURCE_PATH} - LOGNAME build-${TARGET_TRIPLET}-dbg - ) - - file(INSTALL - ${SOURCE_PATH}/tommath.lib - DESTINATION ${CURRENT_PACKAGES_DIR}/debug/lib - ) - - # Clean up necessary to rebuild without debug symbols - vcpkg_execute_build_process( - COMMAND nmake -f ${SOURCE_PATH}/makefile.msvc clean - WORKING_DIRECTORY ${SOURCE_PATH} - LOGNAME clean-${TARGET_TRIPLET}-rel - ) - - vcpkg_execute_build_process( - COMMAND nmake -f ${SOURCE_PATH}/makefile.msvc CFLAGS="${CRTFLAG}" - WORKING_DIRECTORY ${SOURCE_PATH} - LOGNAME build-${TARGET_TRIPLET}-rel - ) - - file(INSTALL - ${SOURCE_PATH}/tommath.lib - DESTINATION ${CURRENT_PACKAGES_DIR}/lib - ) - - file(INSTALL - ${SOURCE_PATH}/tommath.h - DESTINATION ${CURRENT_PACKAGES_DIR}/include - ) -else() - if(VCPKG_LIBRARY_LINKAGE STREQUAL "dynamic") - set(MAKE_FILE "makefile.shared") - else() - set(MAKE_FILE "makefile") - endif() - - vcpkg_execute_build_process( - COMMAND make -f ${MAKE_FILE} clean - WORKING_DIRECTORY ${SOURCE_PATH} - ) - - vcpkg_execute_build_process( - COMMAND make -j${VCPKG_CONCURRENCY} -f ${MAKE_FILE} PREFIX=${CURRENT_PACKAGES_DIR}/debug COMPILE_DEBUG=1 install - WORKING_DIRECTORY ${SOURCE_PATH} - ) - file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") - - vcpkg_execute_build_process( - COMMAND make -f ${MAKE_FILE} clean - WORKING_DIRECTORY ${SOURCE_PATH} - ) - - vcpkg_execute_build_process( - COMMAND make -j${VCPKG_CONCURRENCY} -f ${MAKE_FILE} PREFIX=${CURRENT_PACKAGES_DIR} install - WORKING_DIRECTORY ${SOURCE_PATH} - ) -endif() - -file(INSTALL - ${SOURCE_PATH}/LICENSE - DESTINATION ${CURRENT_PACKAGES_DIR}/share/${PORT} RENAME copyright -) \ No newline at end of file +vcpkg_cmake_configure( + SOURCE_PATH "${SOURCE_PATH}" +) +vcpkg_cmake_install() +vcpkg_copy_pdbs() +vcpkg_cmake_config_fixup(CONFIG_PATH "lib/cmake/${PORT}") +vcpkg_fixup_pkgconfig() +vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE") + +file(INSTALL "${CMAKE_CURRENT_LIST_DIR}/usage" DESTINATION "${CURRENT_PACKAGES_DIR}/share/${PORT}") +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") +file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/share") diff --git a/ports/libtommath/usage b/ports/libtommath/usage new file mode 100644 index 00000000000000..e2028c1a8e732e --- /dev/null +++ b/ports/libtommath/usage @@ -0,0 +1,9 @@ +libtommath provides CMake targets: + + find_package(libtommath CONFIG REQUIRED) + target_link_libraries(main PRIVATE libtommath) + +libtommath provides pkg-config modules: + + # public domain library for manipulating large integer numbers + libtommath diff --git a/ports/libtommath/vcpkg.json b/ports/libtommath/vcpkg.json index 2dedf585f067c1..2efd1171cafcdf 100644 --- a/ports/libtommath/vcpkg.json +++ b/ports/libtommath/vcpkg.json @@ -1,6 +1,17 @@ { "name": "libtommath", "version": "1.3.0", + "port-version": 1, "description": "LibTomMath is a free open source portable number theoretic multiple-precision integer library written entirely in C.", - "homepage": "https://www.libtom.net/LibTomMath/" + "homepage": "https://www.libtom.net/LibTomMath/", + "dependencies": [ + { + "name": "vcpkg-cmake", + "host": true + }, + { + "name": "vcpkg-cmake-config", + "host": true + } + ] }