swift: expose the static-only API in the libzstd module A module map can't define macros, so the Swift package couldn't reach the static-only API of zstd.h and zdict.h (#4581). zstd.h and zdict.h now define ZSTD_STATIC_LINKING_ONLY and ZDICT_STATIC_LINKING_ONLY while clang builds the libzstd module. Other builds are unchanged. The new dev-short-tests job swift-package builds and runs tests/swift, a Swift package that uses this API.
diff --git a/.github/workflows/dev-short-tests.yml b/.github/workflows/dev-short-tests.yml index af5eda9..5a61c55 100644 --- a/.github/workflows/dev-short-tests.yml +++ b/.github/workflows/dev-short-tests.yml
@@ -612,6 +612,15 @@ cc -Wall -Wextra -Wpedantic -Werror -o simple examples/simple_compression.c $(pkg-config --cflags --libs libzstd) ./simple LICENSE + swift-package: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # tag=v7.0.1 + - name: Swift package, static-only API + run: | + swift --version + swift run --package-path tests/swift + versions-compatibility: runs-on: ubuntu-latest steps:
diff --git a/lib/zdict.h b/lib/zdict.h index 599b793..9bfeb86 100644 --- a/lib/zdict.h +++ b/lib/zdict.h
@@ -8,6 +8,14 @@ * You may select, at your option, one of the above-listed licenses. */ +/* The clang module libzstd (lib/module.modulemap, used by the Swift package) + * always exposes the static-only API: a module map can't define macros. */ +#if defined(__building_module) +# if __building_module(libzstd) && !defined(ZDICT_STATIC_LINKING_ONLY) +# define ZDICT_STATIC_LINKING_ONLY +# endif +#endif + #ifndef ZSTD_ZDICT_H #define ZSTD_ZDICT_H
diff --git a/lib/zstd.h b/lib/zstd.h index 702374e..4408175 100644 --- a/lib/zstd.h +++ b/lib/zstd.h
@@ -8,6 +8,14 @@ * You may select, at your option, one of the above-listed licenses. */ +/* The clang module libzstd (lib/module.modulemap, used by the Swift package) + * always exposes the static-only API: a module map can't define macros. */ +#if defined(__building_module) +# if __building_module(libzstd) && !defined(ZSTD_STATIC_LINKING_ONLY) +# define ZSTD_STATIC_LINKING_ONLY +# endif +#endif + #ifndef ZSTD_H_235446 #define ZSTD_H_235446
diff --git a/tests/.gitignore b/tests/.gitignore index 0f15576..3adefc3 100644 --- a/tests/.gitignore +++ b/tests/.gitignore
@@ -52,6 +52,9 @@ namespaceTest dirTest* +# Swift package test +swift/.build/ + # fuzzer afl
diff --git a/tests/swift/Package.swift b/tests/swift/Package.swift new file mode 100644 index 0000000..0c02b4d --- /dev/null +++ b/tests/swift/Package.swift
@@ -0,0 +1,16 @@ +// swift-tools-version:5.5 +// A Swift package that depends on zstd and uses its static-only API. +// Run it with: swift run --package-path tests/swift +import PackageDescription + +let package = Package( + name: "StaticAPI", + dependencies: [ .package(name: "zstd", path: "../..") ], + targets: [ + .executableTarget( + name: "StaticAPI", + dependencies: [ .product(name: "libzstd", package: "zstd") ], + path: ".", + sources: [ "main.swift" ]) + ] +)
diff --git a/tests/swift/main.swift b/tests/swift/main.swift new file mode 100644 index 0000000..5a6792e --- /dev/null +++ b/tests/swift/main.swift
@@ -0,0 +1,21 @@ +// Uses the static-only API of zstd.h and zdict.h through the libzstd module. +import libzstd + +let src = Array(repeating: UInt8(ascii: "z"), count: 1000) +var dst = [UInt8](repeating: 0, count: ZSTD_compressBound(src.count)) +let cSize = ZSTD_compress(&dst, dst.count, src, src.count, 1) +precondition(ZSTD_isError(cSize) == 0) + +// zstd.h +precondition(ZSTD_isFrame(dst, cSize) == 1) +precondition(ZSTD_findDecompressedSize(dst, cSize) == UInt64(src.count)) + +// zdict.h: a single sample is not enough to train +var params = ZDICT_cover_params_t() +params.k = 64 +params.d = 8 +var dict = [UInt8](repeating: 0, count: 1024) +let dictSize = ZDICT_trainFromBuffer_cover(&dict, dict.count, src, [src.count], 1, params) +precondition(ZDICT_isError(dictSize) != 0) + +print("static-only API: ok")