From ead76e71ef7cd055b43e2f4846ed9f8acdbc5122 Mon Sep 17 00:00:00 2001 From: Michael Chiu Date: Mon, 16 Dec 2024 00:19:20 -0800 Subject: [PATCH 1/2] fix FreeBSD build --- Package.swift | 1 + Sources/CCryptoBoringSSL/CMakeLists.txt | 4 ++-- Sources/Crypto/CMakeLists.txt | 2 +- Sources/CryptoBoringWrapper/Util/RandomBytes.swift | 2 +- Tests/CryptoTests/Encodings/DERTests.swift | 2 +- cmake/modules/SwiftSupport.cmake | 2 ++ 6 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Package.swift b/Package.swift index 04f0df2e..76a87b9d 100644 --- a/Package.swift +++ b/Package.swift @@ -45,6 +45,7 @@ if development { Platform.android, Platform.windows, Platform.wasi, + Platform.freebsd ] swiftSettings = [ .define("CRYPTO_IN_SWIFTPM"), diff --git a/Sources/CCryptoBoringSSL/CMakeLists.txt b/Sources/CCryptoBoringSSL/CMakeLists.txt index 5b11945c..84ac892f 100644 --- a/Sources/CCryptoBoringSSL/CMakeLists.txt +++ b/Sources/CCryptoBoringSSL/CMakeLists.txt @@ -276,7 +276,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL Darwin AND CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x gen/crypto/chacha-x86_64-apple.S gen/crypto/chacha20_poly1305_x86_64-apple.S gen/crypto/md5-x86_64-apple.S) -elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|Android" AND CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64") +elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|Android|FreeBSD" AND CMAKE_SYSTEM_PROCESSOR MATCHES "amd64|x86_64") target_sources(CCryptoBoringSSL PRIVATE gen/bcm/aes-gcm-avx10-x86_64-linux.S gen/bcm/aesni-gcm-x86_64-linux.S @@ -313,7 +313,7 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm gen/bcm/vpaes-armv8-apple.S gen/crypto/chacha-armv8-apple.S gen/crypto/chacha20_poly1305_armv8-apple.S) -elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|Android" AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64") +elseif(CMAKE_SYSTEM_NAME MATCHES "Linux|Android|FreeBSD" AND CMAKE_SYSTEM_PROCESSOR MATCHES "arm64|aarch64") target_sources(CCryptoBoringSSL PRIVATE gen/bcm/aesv8-armv8-linux.S gen/bcm/aesv8-gcm-armv8-linux.S diff --git a/Sources/Crypto/CMakeLists.txt b/Sources/Crypto/CMakeLists.txt index 95a1c437..3bc8e1bb 100644 --- a/Sources/Crypto/CMakeLists.txt +++ b/Sources/Crypto/CMakeLists.txt @@ -96,7 +96,7 @@ add_library(Crypto target_compile_definitions(Crypto PRIVATE "$<$:CRYPTO_IN_SWIFTPM>") -if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Windows" OR CMAKE_SYSTEM_NAME STREQUAL "Android" OR CMAKE_SYSTEM_NAME STREQUAL "WASI") +if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Windows" OR CMAKE_SYSTEM_NAME STREQUAL "Android" OR CMAKE_SYSTEM_NAME STREQUAL "WASI" OR CMAKE_SYSTEM_NAME STREQUAL FreeBSD) target_compile_definitions(Crypto PRIVATE "$<$:CRYPTO_IN_SWIFTPM_FORCE_BUILD_API>") endif() diff --git a/Sources/CryptoBoringWrapper/Util/RandomBytes.swift b/Sources/CryptoBoringWrapper/Util/RandomBytes.swift index fc3970d0..0511c5fb 100644 --- a/Sources/CryptoBoringWrapper/Util/RandomBytes.swift +++ b/Sources/CryptoBoringWrapper/Util/RandomBytes.swift @@ -19,7 +19,7 @@ extension UnsafeMutableRawBufferPointer { return } - #if canImport(Darwin) || os(Linux) || os(Android) || os(Windows) + #if canImport(Darwin) || os(Linux) || os(Android) || os(Windows) || os(FreeBSD) var rng = SystemRandomNumberGenerator() precondition(count <= self.count) diff --git a/Tests/CryptoTests/Encodings/DERTests.swift b/Tests/CryptoTests/Encodings/DERTests.swift index 38bcfbca..02ce6bbd 100644 --- a/Tests/CryptoTests/Encodings/DERTests.swift +++ b/Tests/CryptoTests/Encodings/DERTests.swift @@ -49,7 +49,7 @@ class DERTests: XCTestCase { } func randomBytes(count: Int) -> [UInt8] { - #if canImport(Darwin) || os(Linux) || os(Android) || os(Windows) + #if canImport(Darwin) || os(Linux) || os(Android) || os(Windows) || os(FreeBSD) var rng = SystemRandomNumberGenerator() return (0.. Date: Mon, 16 Dec 2024 01:00:59 -0800 Subject: [PATCH 2/2] Guard swift settings and dependencies for FreeBSD behind a `isFreeBSD` boolean `Platform.freebsd` is not yet available on swift-pm. Therefore we guard the settings and dependencies behind a `isFreeBSD` boolean instead. --- Package.swift | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Package.swift b/Package.swift index 76a87b9d..61d98e23 100644 --- a/Package.swift +++ b/Package.swift @@ -27,9 +27,18 @@ import PackageDescription // To develop this on Apple platforms, set this to true let development = false +// Ideally, we should use `.when(platforms:)` to set `swiftSettings` and +// `dependencies` like on other platforms. However, `Platform.freebsd` is not +// yet available, and therefore we guard the settings behind this boolean. +#if os(FreeBSD) +let isFreeBSD = true +#else +let isFreeBSD = false +#endif + let swiftSettings: [SwiftSetting] let dependencies: [Target.Dependency] -if development { +if development || isFreeBSD { swiftSettings = [ .define("CRYPTO_IN_SWIFTPM"), .define("CRYPTO_IN_SWIFTPM_FORCE_BUILD_API"), @@ -44,8 +53,7 @@ if development { Platform.linux, Platform.android, Platform.windows, - Platform.wasi, - Platform.freebsd + Platform.wasi ] swiftSettings = [ .define("CRYPTO_IN_SWIFTPM"),