Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const libssh2_dependency = b.dependency("libssh2", .{
.strip = true, // Strip debug information (default=false)
.linkage = .static, // Whether to link statically or dynamically (default=static)
.@"crypto-backend" = .auto, // auto will to default to wincng on windows, openssl everywhere else. (default=auto)
.@"openssl-linkage" = .static, // each dependency's linkage can be configured to static/dynamic linking
});
```

By default libssh2 will dynamically link against the crypto backend.
This can be disable through the system integration options, i.e. `-fsys` and `-fno-sys`
38 changes: 26 additions & 12 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ pub fn build(b: *std.Build) void {

const crypto_choice = b.option(CryptoBackend, "crypto-backend", "Crypto backend: auto|openssl|mbedtls|libgcrypt|wincng") orelse .auto;
const zlib = b.option(bool, "zlib", "Enable SSH payload compression (links zlib)") orelse false;
const mbedtls_linkage = b.option(std.builtin.LinkMode, "mbedtls-linkage", "static|dynamic") orelse .static;
const openssl_linkage = b.option(std.builtin.LinkMode, "openssl-linkage", "static|dynamic") orelse .static;
const wincng_linkage = b.option(std.builtin.LinkMode, "wincng-linkage", "static|dynamic") orelse .static;
const gcrypt_linkage = b.option(std.builtin.LinkMode, "gcrypt-linkage", "static|dynamic") orelse .static;

const is_windows = target.result.os.tag == .windows;
const mbedtls = crypto_choice == .mbedtls;
Expand Down Expand Up @@ -74,26 +70,44 @@ pub fn build(b: *std.Build) void {

if (mbedtls) {
ssh2_lib.root_module.addCMacro("LIBSSH2_MBEDTLS", "1");
ssh2_lib.linkSystemLibrary2("mbedtls", .{ .preferred_link_mode = mbedtls_linkage });
ssh2_lib.linkSystemLibrary2("mbedcrypto", .{ .preferred_link_mode = mbedtls_linkage });
ssh2_lib.linkSystemLibrary2("mbedx509", .{ .preferred_link_mode = mbedtls_linkage });
if (b.systemIntegrationOption("mbedtls", .{ .default = true })) {
ssh2_lib.linkSystemLibrary("mbedtls");
ssh2_lib.linkSystemLibrary("mbedcrypto");
ssh2_lib.linkSystemLibrary("mbedx509");
} else {
// TODO: Add lazy dependency to build.zig.zon and statically link against library.
// For now it's the users resposibility to compile and link against the library
}
}

if (openssl) {
ssh2_lib.root_module.addCMacro("LIBSSH2_OPENSSL", "1");
ssh2_lib.linkSystemLibrary2("ssl", .{ .preferred_link_mode = openssl_linkage });
ssh2_lib.linkSystemLibrary2("crypto", .{ .preferred_link_mode = openssl_linkage });
if (b.systemIntegrationOption("openssl", .{ .default = true })) {
ssh2_lib.linkSystemLibrary("ssl");
ssh2_lib.linkSystemLibrary("crypto");
} else {
// TODO: Add lazy dependency to build.zig.zon and statically link against library.
// For now it's the users resposibility to compile and link against the library
}
}

if (wincng) {
// There is no need to provide `b.systemIntegrationOption` here,
// because on windows this library MUST be dynamically linked.

ssh2_lib.root_module.addCMacro("LIBSSH2_WINCNG", "1");
ssh2_lib.linkSystemLibrary2("bcrypt", .{ .preferred_link_mode = wincng_linkage });
ssh2_lib.linkSystemLibrary2("ncrypt", .{ .preferred_link_mode = wincng_linkage });
ssh2_lib.linkSystemLibrary2("bcrypt", .{});
ssh2_lib.linkSystemLibrary2("ncrypt", .{});
Comment on lines +99 to +100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well use linkSystemLibrary since we aren't passing any options

}

if (libgcrypt) {
ssh2_lib.root_module.addCMacro("LIBSSH2_LIBGCRYPT", "1");
ssh2_lib.linkSystemLibrary2("gcrypt", .{ .preferred_link_mode = gcrypt_linkage });
if (b.systemIntegrationOption("libgcrypt", .{ .default = true })) {
ssh2_lib.linkSystemLibrary("gcrypt");
} else {
// TODO: Add lazy dependency to build.zig.zon and statically link against library.
// For now it's the users resposibility to compile and link against the library
}
}

if (zlib) {
Expand Down