From 915c6fdcf8e40dc3b301899dbf2fe4c8bc8c90f2 Mon Sep 17 00:00:00 2001 From: devgianlu Date: Sat, 26 Apr 2025 11:53:09 +0200 Subject: [PATCH] LibCrypto: Add `libtommath` vcpkg dependency and link it An overlay port is required to add the `stdc-iec-559` and `install-pc` patches. The `stdc-iec-559` patch is required because Clang doesn't define `__STDC_IEC_559__`. However, glibc and musl define it if `__GCC_IEC_559` is not defined. The macro is taken from glibc source code. The `install-pc` patch is required because libtommath doesn't install the pkg-config files when building statically compromising our ability to find it during build. Clang: https://clang.llvm.org/c_status.html#:~:text=Yes-, IEC%2060559%20support,-Unknown glibc: https://sourceware.org/git/?p=glibc.git;a=blob; f=include/stdc-predef.h --- Libraries/LibCrypto/CMakeLists.txt | 4 ++ .../overlay-ports/libtommath/bcrypt.patch | 67 +++++++++++++++++++ .../libtommath/has-set-double.patch | 14 ++++ .../overlay-ports/libtommath/import-lib.patch | 12 ++++ .../overlay-ports/libtommath/install-pc.patch | 12 ++++ .../overlay-ports/libtommath/msvc-dce.patch | 21 ++++++ .../overlay-ports/libtommath/portfile.cmake | 27 ++++++++ .../libtommath/stdc-iec-559.patch | 20 ++++++ .../vcpkg/overlay-ports/libtommath/usage | 9 +++ .../vcpkg/overlay-ports/libtommath/vcpkg.json | 17 +++++ vcpkg.json | 5 ++ 11 files changed, 208 insertions(+) create mode 100644 Meta/CMake/vcpkg/overlay-ports/libtommath/bcrypt.patch create mode 100644 Meta/CMake/vcpkg/overlay-ports/libtommath/has-set-double.patch create mode 100644 Meta/CMake/vcpkg/overlay-ports/libtommath/import-lib.patch create mode 100644 Meta/CMake/vcpkg/overlay-ports/libtommath/install-pc.patch create mode 100644 Meta/CMake/vcpkg/overlay-ports/libtommath/msvc-dce.patch create mode 100644 Meta/CMake/vcpkg/overlay-ports/libtommath/portfile.cmake create mode 100644 Meta/CMake/vcpkg/overlay-ports/libtommath/stdc-iec-559.patch create mode 100644 Meta/CMake/vcpkg/overlay-ports/libtommath/usage create mode 100644 Meta/CMake/vcpkg/overlay-ports/libtommath/vcpkg.json diff --git a/Libraries/LibCrypto/CMakeLists.txt b/Libraries/LibCrypto/CMakeLists.txt index 9370208bcd3..fea1daf2e40 100644 --- a/Libraries/LibCrypto/CMakeLists.txt +++ b/Libraries/LibCrypto/CMakeLists.txt @@ -34,5 +34,9 @@ set(SOURCES serenity_lib(LibCrypto crypto) target_link_libraries(LibCrypto PRIVATE LibCore) +find_package(PkgConfig REQUIRED) +pkg_check_modules(libtommath REQUIRED IMPORTED_TARGET libtommath) +target_link_libraries(LibCrypto PRIVATE PkgConfig::libtommath) + find_package(OpenSSL REQUIRED) target_link_libraries(LibCrypto PUBLIC OpenSSL::Crypto) diff --git a/Meta/CMake/vcpkg/overlay-ports/libtommath/bcrypt.patch b/Meta/CMake/vcpkg/overlay-ports/libtommath/bcrypt.patch new file mode 100644 index 00000000000..973c7e17155 --- /dev/null +++ b/Meta/CMake/vcpkg/overlay-ports/libtommath/bcrypt.patch @@ -0,0 +1,67 @@ +diff --git a/CMakeLists.txt b/CMakeLists.txt +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -22,8 +22,10 @@ + #----------------------------------------------------------------------------- + include(GNUInstallDirs) + include(CheckIPOSupported) + include(CMakePackageConfigHelpers) ++include(CMakePushCheckState) ++include(CheckSymbolExists) + # default is "No tests" + option(BUILD_TESTING "" OFF) + include(CTest) + include(sources.cmake) +@@ -118,8 +120,19 @@ + target_link_options(${PROJECT_NAME} BEFORE PRIVATE + ${LTM_LD_FLAGS} + ) + ++if(MSVC) ++ cmake_push_check_state() ++ set(CMAKE_REQUIRED_LIBRARIES bcrypt) ++ check_symbol_exists(BCryptGenRandom "Windows.h;bcrypt.h" BCRYPT_AVAILABLE) ++ cmake_pop_check_state() ++ if(BCRYPT_AVAILABLE) ++ target_compile_definitions(${PROJECT_NAME} PRIVATE LTM_WIN32_BCRYPT) ++ target_link_libraries(${PROJECT_NAME} PRIVATE bcrypt) ++ endif() ++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) + list(APPEND PUBLIC_HEADERS tommath_c89.h) +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 +@@ -28,8 +28,19 @@ + #endif + + #define WIN32_LEAN_AND_MEAN + #include ++ ++#ifdef LTM_WIN32_BCRYPT ++#include ++#pragma comment(lib, "bcrypt") ++ ++static mp_err s_read_wincsp(void *p, size_t n) ++{ ++ return BCRYPT_SUCCESS(BCryptGenRandom(NULL, (PUCHAR)p, (ULONG)n, ++ BCRYPT_USE_SYSTEM_PREFERRED_RNG)) ? MP_OKAY : MP_ERR; ++} ++#else + #include + + static mp_err s_read_wincsp(void *p, size_t n) + { +@@ -45,8 +56,9 @@ + hProv = h; + } + return CryptGenRandom(hProv, (DWORD)n, (BYTE *)p) == TRUE ? MP_OKAY : MP_ERR; + } ++#endif + #endif /* WIN32 */ + + #if !defined(BN_S_READ_WINCSP_C) && defined(__linux__) && defined(__GLIBC_PREREQ) + #if __GLIBC_PREREQ(2, 25) diff --git a/Meta/CMake/vcpkg/overlay-ports/libtommath/has-set-double.patch b/Meta/CMake/vcpkg/overlay-ports/libtommath/has-set-double.patch new file mode 100644 index 00000000000..795aace0ba8 --- /dev/null +++ b/Meta/CMake/vcpkg/overlay-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/Meta/CMake/vcpkg/overlay-ports/libtommath/import-lib.patch b/Meta/CMake/vcpkg/overlay-ports/libtommath/import-lib.patch new file mode 100644 index 00000000000..e767fbaddd4 --- /dev/null +++ b/Meta/CMake/vcpkg/overlay-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/Meta/CMake/vcpkg/overlay-ports/libtommath/install-pc.patch b/Meta/CMake/vcpkg/overlay-ports/libtommath/install-pc.patch new file mode 100644 index 00000000000..864e1c16a6d --- /dev/null +++ b/Meta/CMake/vcpkg/overlay-ports/libtommath/install-pc.patch @@ -0,0 +1,12 @@ +diff --git a/CMakeLists.txt b/CMakeLists.txt +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -168,7 +168,7 @@ install(TARGETS ${PROJECT_NAME} + ) + + # Install libtommath.pc for pkg-config if we build a shared library +-if(BUILD_SHARED_LIBS) ++if(BUILD_SHARED_LIBS OR TRUE) + # Let the user override the default directory of the pkg-config file (usually this shouldn't be required to be changed) + set(CMAKE_INSTALL_PKGCONFIGDIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig" CACHE PATH "Folder where to install .pc files") + diff --git a/Meta/CMake/vcpkg/overlay-ports/libtommath/msvc-dce.patch b/Meta/CMake/vcpkg/overlay-ports/libtommath/msvc-dce.patch new file mode 100644 index 00000000000..ccce1143add --- /dev/null +++ b/Meta/CMake/vcpkg/overlay-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/Meta/CMake/vcpkg/overlay-ports/libtommath/portfile.cmake b/Meta/CMake/vcpkg/overlay-ports/libtommath/portfile.cmake new file mode 100644 index 00000000000..82962613549 --- /dev/null +++ b/Meta/CMake/vcpkg/overlay-ports/libtommath/portfile.cmake @@ -0,0 +1,27 @@ +vcpkg_from_github( + OUT_SOURCE_PATH SOURCE_PATH + REPO libtom/libtommath + 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 + stdc-iec-559.patch # Define __STDC_IEC_559__ like glibc and musl do. + install-pc.patch # Always install pkgconfig file, even when building statically. +) + +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/Meta/CMake/vcpkg/overlay-ports/libtommath/stdc-iec-559.patch b/Meta/CMake/vcpkg/overlay-ports/libtommath/stdc-iec-559.patch new file mode 100644 index 00000000000..4441cc90fcf --- /dev/null +++ b/Meta/CMake/vcpkg/overlay-ports/libtommath/stdc-iec-559.patch @@ -0,0 +1,20 @@ +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 +@@ -3,6 +3,16 @@ + /* LibTomMath, multiple-precision integer library -- Tom St Denis */ + /* SPDX-License-Identifier: Unlicense */ + ++#ifdef __GCC_IEC_559 ++# if __GCC_IEC_559 > 0 ++# define __STDC_IEC_559__ 1 ++# define __STDC_IEC_60559_BFP__ 201404L ++# endif ++#else ++# define __STDC_IEC_559__ 1 ++# define __STDC_IEC_60559_BFP__ 201404L ++#endif ++ + #if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559) || defined(_MSC_VER) + mp_err mp_set_double(mp_int *a, double b) + { diff --git a/Meta/CMake/vcpkg/overlay-ports/libtommath/usage b/Meta/CMake/vcpkg/overlay-ports/libtommath/usage new file mode 100644 index 00000000000..657d5e0c71f --- /dev/null +++ b/Meta/CMake/vcpkg/overlay-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/Meta/CMake/vcpkg/overlay-ports/libtommath/vcpkg.json b/Meta/CMake/vcpkg/overlay-ports/libtommath/vcpkg.json new file mode 100644 index 00000000000..f0ab5327925 --- /dev/null +++ b/Meta/CMake/vcpkg/overlay-ports/libtommath/vcpkg.json @@ -0,0 +1,17 @@ +{ + "name": "libtommath", + "version": "1.3.0", + "port-version": 2, + "description": "LibTomMath is a free open source portable number theoretic multiple-precision integer library written entirely in C.", + "homepage": "https://www.libtom.net/LibTomMath/", + "dependencies": [ + { + "name": "vcpkg-cmake", + "host": true + }, + { + "name": "vcpkg-cmake-config", + "host": true + } + ] +} diff --git a/vcpkg.json b/vcpkg.json index f69b75139c5..f29664765c1 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -70,6 +70,7 @@ "dav1d" ] }, + "libtommath", { "name": "libwebp", "features": [ @@ -172,6 +173,10 @@ "name": "libproxy", "version": "0.4.18#3" }, + { + "name": "libtommath", + "version": "1.3.0#2" + }, { "name": "libavif", "version": "1.0.4#1"