|
1 | 1 | const std = @import("std");
|
2 | 2 |
|
| 3 | +const version: std.SemanticVersion = .{ .major = 1, .minor = 11, .patch = 1 }; |
| 4 | + |
| 5 | +const CryptoBackend = enum { |
| 6 | + auto, |
| 7 | + openssl, |
| 8 | + mbedtls, |
| 9 | + libgcrypt, |
| 10 | + wincng, |
| 11 | +}; |
| 12 | + |
3 | 13 | pub fn build(b: *std.Build) void {
|
| 14 | + const upstream = b.dependency("libssh2", .{}); |
4 | 15 | const target = b.standardTargetOptions(.{});
|
5 | 16 | const optimize = b.standardOptimizeOption(.{});
|
6 | 17 |
|
7 |
| - const libssh2_dep = b.dependency("libssh2", .{ |
8 |
| - .target = target, |
9 |
| - .optimize = optimize, |
10 |
| - }); |
| 18 | + const linkage = b.option(std.builtin.LinkMode, "linkage", "Link mode") orelse .static; |
| 19 | + const strip = b.option(bool, "strip", "Omit debug information"); |
| 20 | + const pic = b.option(bool, "pie", "Produce Position Independent Code"); |
| 21 | + |
| 22 | + const crypto_choice = b.option(CryptoBackend, "crypto-backend", "Crypto backend: auto|openssl|mbedtls|libgcrypt|wincng") orelse .auto; |
| 23 | + const zlib = b.option(bool, "zlib", "Enable SSH payload compression (links zlib)") orelse false; |
| 24 | + |
| 25 | + const is_windows = target.result.os.tag == .windows; |
| 26 | + const mbedtls = crypto_choice == .mbedtls; |
| 27 | + const openssl = (crypto_choice == .auto and !is_windows) or crypto_choice == .openssl; |
| 28 | + const wincng = (crypto_choice == .auto and is_windows) or crypto_choice == .wincng; |
| 29 | + const libgcrypt = crypto_choice == .libgcrypt; |
11 | 30 |
|
12 |
| - const mbedtls_dep = b.dependency("mbedtls", .{ |
13 |
| - .target = target, |
14 |
| - .optimize = optimize, |
| 31 | + const config_header = b.addConfigHeader(.{ |
| 32 | + .style = .{ |
| 33 | + .cmake = upstream.path("src/libssh2_config_cmake.h.in"), |
| 34 | + }, |
| 35 | + .include_path = "libssh2_config.h", |
| 36 | + }, .{ |
| 37 | + .LIBSSH2_API = switch (target.result.os.tag) { |
| 38 | + .windows => "__declspec(dllexport)", |
| 39 | + else => "", |
| 40 | + }, |
| 41 | + .LIBSSH2_HAVE_ZLIB = zlib, |
| 42 | + .HAVE_SYS_UIO_H = !is_windows, |
| 43 | + .HAVE_WRITEV = !is_windows, |
| 44 | + .HAVE_SYS_SOCKET_H = !is_windows, |
| 45 | + .HAVE_NETINET_IN_H = !is_windows, |
| 46 | + .HAVE_ARPA_INET_H = !is_windows, |
| 47 | + .HAVE_SYS_TYPES_H = !is_windows, |
| 48 | + .HAVE_INTTYPES_H = true, |
| 49 | + .HAVE_STDINT_H = true, |
15 | 50 | });
|
16 | 51 |
|
17 |
| - const lib = b.addStaticLibrary(.{ |
| 52 | + const ssh2_lib = b.addLibrary(.{ |
| 53 | + .version = version, |
18 | 54 | .name = "ssh2",
|
19 |
| - .target = target, |
20 |
| - .optimize = optimize, |
21 |
| - .link_libc = true, |
| 55 | + .linkage = linkage, |
| 56 | + .root_module = b.createModule(.{ |
| 57 | + .target = target, |
| 58 | + .optimize = optimize, |
| 59 | + .link_libc = true, |
| 60 | + .strip = strip, |
| 61 | + .pic = pic, |
| 62 | + }), |
22 | 63 | });
|
23 |
| - lib.addIncludePath(libssh2_dep.path("include")); |
24 |
| - lib.linkLibrary(mbedtls_dep.artifact("mbedtls")); |
25 |
| - lib.addCSourceFiles(.{ |
26 |
| - .root = libssh2_dep.path("src"), |
27 |
| - .flags = &.{}, |
28 |
| - .files = &.{ |
29 |
| - "channel.c", |
30 |
| - "comp.c", |
31 |
| - "crypt.c", |
32 |
| - "hostkey.c", |
33 |
| - "kex.c", |
34 |
| - "mac.c", |
35 |
| - "misc.c", |
36 |
| - "packet.c", |
37 |
| - "publickey.c", |
38 |
| - "scp.c", |
39 |
| - "session.c", |
40 |
| - "sftp.c", |
41 |
| - "userauth.c", |
42 |
| - "transport.c", |
43 |
| - "version.c", |
44 |
| - "knownhost.c", |
45 |
| - "agent.c", |
46 |
| - "mbedtls.c", |
47 |
| - "pem.c", |
48 |
| - "keepalive.c", |
49 |
| - "global.c", |
50 |
| - "blowfish.c", |
51 |
| - "bcrypt_pbkdf.c", |
52 |
| - "agent_win.c", |
53 |
| - }, |
54 |
| - }); |
55 |
| - lib.installHeader(b.path("config/libssh2_config.h"), "libssh2_config.h"); |
56 |
| - lib.installHeadersDirectory(libssh2_dep.path("include"), ".", .{}); |
57 |
| - lib.defineCMacro("LIBSSH2_MBEDTLS", null); |
| 64 | + b.installArtifact(ssh2_lib); |
| 65 | + ssh2_lib.installHeadersDirectory(upstream.path("include"), "", .{}); |
| 66 | + ssh2_lib.root_module.addConfigHeader(config_header); |
| 67 | + ssh2_lib.root_module.addIncludePath(upstream.path("include")); |
| 68 | + ssh2_lib.root_module.addCMacro("HAVE_CONFIG_H", "1"); |
| 69 | + ssh2_lib.root_module.addCSourceFiles(.{ .files = ssh2_src, .root = upstream.path(""), .flags = ssh2_flags }); |
58 | 70 |
|
59 |
| - if (target.result.os.tag == .windows) { |
60 |
| - lib.defineCMacro("_CRT_SECURE_NO_DEPRECATE", "1"); |
61 |
| - lib.defineCMacro("HAVE_LIBCRYPT32", null); |
62 |
| - lib.defineCMacro("HAVE_WINSOCK2_H", null); |
63 |
| - lib.defineCMacro("HAVE_IOCTLSOCKET", null); |
64 |
| - lib.defineCMacro("HAVE_SELECT", null); |
65 |
| - lib.defineCMacro("LIBSSH2_DH_GEX_NEW", "1"); |
| 71 | + if (mbedtls) { |
| 72 | + ssh2_lib.root_module.addCSourceFile(.{ .file = upstream.path("src/mbedtls.c"), .flags = ssh2_flags }); |
| 73 | + ssh2_lib.root_module.addCMacro("LIBSSH2_MBEDTLS", "1"); |
| 74 | + ssh2_lib.linkSystemLibrary("mbedtls"); |
| 75 | + ssh2_lib.linkSystemLibrary("mbedcrypto"); |
| 76 | + ssh2_lib.linkSystemLibrary("mbedx509"); |
| 77 | + } |
66 | 78 |
|
67 |
| - if (target.result.isGnu()) { |
68 |
| - lib.defineCMacro("HAVE_UNISTD_H", null); |
69 |
| - lib.defineCMacro("HAVE_INTTYPES_H", null); |
70 |
| - lib.defineCMacro("HAVE_SYS_TIME_H", null); |
71 |
| - lib.defineCMacro("HAVE_GETTIMEOFDAY", null); |
72 |
| - } |
73 |
| - } else { |
74 |
| - lib.defineCMacro("HAVE_UNISTD_H", null); |
75 |
| - lib.defineCMacro("HAVE_INTTYPES_H", null); |
76 |
| - lib.defineCMacro("HAVE_STDLIB_H", null); |
77 |
| - lib.defineCMacro("HAVE_SYS_SELECT_H", null); |
78 |
| - lib.defineCMacro("HAVE_SYS_UIO_H", null); |
79 |
| - lib.defineCMacro("HAVE_SYS_SOCKET_H", null); |
80 |
| - lib.defineCMacro("HAVE_SYS_IOCTL_H", null); |
81 |
| - lib.defineCMacro("HAVE_SYS_TIME_H", null); |
82 |
| - lib.defineCMacro("HAVE_SYS_UN_H", null); |
83 |
| - lib.defineCMacro("HAVE_LONGLONG", null); |
84 |
| - lib.defineCMacro("HAVE_GETTIMEOFDAY", null); |
85 |
| - lib.defineCMacro("HAVE_INET_ADDR", null); |
86 |
| - lib.defineCMacro("HAVE_POLL", null); |
87 |
| - lib.defineCMacro("HAVE_SELECT", null); |
88 |
| - lib.defineCMacro("HAVE_SOCKET", null); |
89 |
| - lib.defineCMacro("HAVE_STRTOLL", null); |
90 |
| - lib.defineCMacro("HAVE_SNPRINTF", null); |
91 |
| - lib.defineCMacro("HAVE_O_NONBLOCK", null); |
| 79 | + if (openssl) { |
| 80 | + ssh2_lib.root_module.addCSourceFile(.{ .file = upstream.path("src/openssl.c"), .flags = ssh2_flags }); |
| 81 | + ssh2_lib.root_module.addCMacro("LIBSSH2_OPENSSL", "1"); |
| 82 | + ssh2_lib.linkSystemLibrary("ssl"); |
| 83 | + ssh2_lib.linkSystemLibrary("crypto"); |
| 84 | + } |
| 85 | + |
| 86 | + if (wincng) { |
| 87 | + ssh2_lib.root_module.addCSourceFile(.{ .file = upstream.path("src/wincng.c"), .flags = ssh2_flags }); |
| 88 | + ssh2_lib.root_module.addCMacro("LIBSSH2_WINCNG", "1"); |
| 89 | + // Windows system libs (zig handles names) |
| 90 | + ssh2_lib.linkSystemLibrary("bcrypt"); |
| 91 | + ssh2_lib.linkSystemLibrary("ncrypt"); |
| 92 | + } |
| 93 | + |
| 94 | + if (libgcrypt) { |
| 95 | + ssh2_lib.root_module.addCSourceFile(.{ .file = upstream.path("src/libgcrypt.c"), .flags = ssh2_flags }); |
| 96 | + ssh2_lib.root_module.addCMacro("LIBSSH2_LIBGCRYPT", "1"); |
| 97 | + ssh2_lib.linkSystemLibrary("gcrypt"); |
92 | 98 | }
|
93 | 99 |
|
94 |
| - b.installArtifact(lib); |
| 100 | + if (zlib) { |
| 101 | + if (b.systemIntegrationOption("zlib", .{})) { |
| 102 | + ssh2_lib.root_module.linkSystemLibrary("zlib", .{}); |
| 103 | + } else if (b.lazyDependency("zlib", .{ |
| 104 | + .target = target, |
| 105 | + .optimize = optimize, |
| 106 | + })) |zlib_dependency| { |
| 107 | + ssh2_lib.root_module.linkLibrary(zlib_dependency.artifact("z")); |
| 108 | + } |
| 109 | + } |
95 | 110 | }
|
| 111 | + |
| 112 | +pub const ssh2_src: []const []const u8 = &.{ |
| 113 | + "src/agent.c", |
| 114 | + "src/bcrypt_pbkdf.c", |
| 115 | + "src/blowfish.c", |
| 116 | + "src/chacha.c", |
| 117 | + "src/channel.c", |
| 118 | + "src/cipher-chachapoly.c", |
| 119 | + "src/comp.c", |
| 120 | + "src/crypt.c", |
| 121 | + "src/global.c", |
| 122 | + "src/hostkey.c", |
| 123 | + "src/keepalive.c", |
| 124 | + "src/kex.c", |
| 125 | + "src/knownhost.c", |
| 126 | + "src/mac.c", |
| 127 | + "src/misc.c", |
| 128 | + "src/packet.c", |
| 129 | + "src/pem.c", |
| 130 | + "src/poly1305.c", |
| 131 | + "src/publickey.c", |
| 132 | + "src/scp.c", |
| 133 | + "src/session.c", |
| 134 | + "src/sftp.c", |
| 135 | + "src/transport.c", |
| 136 | + "src/userauth.c", |
| 137 | + "src/userauth_kbd_packet.c", |
| 138 | + "src/version.c", |
| 139 | +}; |
| 140 | + |
| 141 | +pub const ssh2_flags: []const []const u8 = &.{}; |
0 commit comments