Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.zig text eol=lf
*.zon text eol=lf
43 changes: 0 additions & 43 deletions .github/workflows/build.yml

This file was deleted.

36 changes: 36 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:

jobs:
build:
strategy:
fail-fast: false
matrix:
zig-version: [master]
os: [ubuntu-latest, macos-latest, windows-latest]
include:
- zig-version: "0.15.1"
os: ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Zig
uses: mlugg/setup-zig@v2
with:
version: ${{ matrix.zig-version }}

- name: Check Formatting
run: zig fmt --ast-check --check .

- name: Build
run: zig build -Dstrip=true -Dzlib=true --summary all
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.zig-cache
zig-cache
zig-out
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
[![CI](https://github.com/hamptokr/zig-libssh2/actions/workflows/ci.yaml/badge.svg)](https://github.com/hamptokr/zig-libssh2/actions)

# libssh2

libssh2 packaged with Zig
This is [libssh2](https://github.com/libssh2/libssh2), packaged for [Zig](https://ziglang.org/).

## Installation

First, update your `build.zig.zon`:

```
# Initialize a `zig build` project if you haven't already
zig init
zig fetch --save git+https://github.com/allyourcodebase/libssh2.git#libssh2-1.11.1
```

You can then import `libssh2` in your `build.zig` with:

```zig
const libssh2_dependency = b.dependency("libssh2", .{
.target = target,
.optimize = optimize,
});
your_exe.linkLibrary(libssh2_dependency.artifact("ssh2"));
```

## Build Options

`libssh2` offers a few options which you can control like so:

```zig
const libssh2_dependency = b.dependency("libssh2", .{
.target = target,
.optimize = optimize,
.zlib = true, // links to zlib for payload compression if enabled (default=true)
.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)
});
```

```

```
204 changes: 125 additions & 79 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,95 +1,141 @@
const std = @import("std");

const version: std.SemanticVersion = .{ .major = 1, .minor = 11, .patch = 1 };

const CryptoBackend = enum {
auto,
openssl,
mbedtls,
libgcrypt,
wincng,
};

pub fn build(b: *std.Build) void {
const upstream = b.dependency("libssh2", .{});
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const libssh2_dep = b.dependency("libssh2", .{
.target = target,
.optimize = optimize,
});
const linkage = b.option(std.builtin.LinkMode, "linkage", "Link mode") orelse .static;
const strip = b.option(bool, "strip", "Omit debug information");
const pic = b.option(bool, "pie", "Produce Position Independent Code");

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 is_windows = target.result.os.tag == .windows;
const mbedtls = crypto_choice == .mbedtls;
const openssl = (crypto_choice == .auto and !is_windows) or crypto_choice == .openssl;
const wincng = (crypto_choice == .auto and is_windows) or crypto_choice == .wincng;
const libgcrypt = crypto_choice == .libgcrypt;

const mbedtls_dep = b.dependency("mbedtls", .{
.target = target,
.optimize = optimize,
const config_header = b.addConfigHeader(.{
.style = .{
.cmake = upstream.path("src/libssh2_config_cmake.h.in"),
},
.include_path = "libssh2_config.h",
}, .{
.LIBSSH2_API = switch (target.result.os.tag) {
.windows => "__declspec(dllexport)",
else => "",
},
.LIBSSH2_HAVE_ZLIB = zlib,
.HAVE_SYS_UIO_H = !is_windows,
.HAVE_WRITEV = !is_windows,
.HAVE_SYS_SOCKET_H = !is_windows,
.HAVE_NETINET_IN_H = !is_windows,
.HAVE_ARPA_INET_H = !is_windows,
.HAVE_SYS_TYPES_H = !is_windows,
.HAVE_INTTYPES_H = true,
.HAVE_STDINT_H = true,
});

const lib = b.addStaticLibrary(.{
const ssh2_lib = b.addLibrary(.{
.version = version,
.name = "ssh2",
.target = target,
.optimize = optimize,
.link_libc = true,
.linkage = linkage,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.strip = strip,
.pic = pic,
}),
});
lib.addIncludePath(libssh2_dep.path("include"));
lib.linkLibrary(mbedtls_dep.artifact("mbedtls"));
lib.addCSourceFiles(.{
.root = libssh2_dep.path("src"),
.flags = &.{},
.files = &.{
"channel.c",
"comp.c",
"crypt.c",
"hostkey.c",
"kex.c",
"mac.c",
"misc.c",
"packet.c",
"publickey.c",
"scp.c",
"session.c",
"sftp.c",
"userauth.c",
"transport.c",
"version.c",
"knownhost.c",
"agent.c",
"mbedtls.c",
"pem.c",
"keepalive.c",
"global.c",
"blowfish.c",
"bcrypt_pbkdf.c",
"agent_win.c",
},
});
lib.installHeader(b.path("config/libssh2_config.h"), "libssh2_config.h");
lib.installHeadersDirectory(libssh2_dep.path("include"), ".", .{});
lib.defineCMacro("LIBSSH2_MBEDTLS", null);
b.installArtifact(ssh2_lib);
ssh2_lib.installHeadersDirectory(upstream.path("include"), "", .{});
ssh2_lib.root_module.addConfigHeader(config_header);
ssh2_lib.root_module.addIncludePath(upstream.path("include"));
ssh2_lib.root_module.addCMacro("HAVE_CONFIG_H", "1");
ssh2_lib.root_module.addCSourceFiles(.{ .files = ssh2_src, .root = upstream.path(""), .flags = ssh2_flags });

if (target.result.os.tag == .windows) {
lib.defineCMacro("_CRT_SECURE_NO_DEPRECATE", "1");
lib.defineCMacro("HAVE_LIBCRYPT32", null);
lib.defineCMacro("HAVE_WINSOCK2_H", null);
lib.defineCMacro("HAVE_IOCTLSOCKET", null);
lib.defineCMacro("HAVE_SELECT", null);
lib.defineCMacro("LIBSSH2_DH_GEX_NEW", "1");
if (mbedtls) {
ssh2_lib.root_module.addCSourceFile(.{ .file = upstream.path("src/mbedtls.c"), .flags = ssh2_flags });
ssh2_lib.root_module.addCMacro("LIBSSH2_MBEDTLS", "1");
ssh2_lib.linkSystemLibrary("mbedtls");
ssh2_lib.linkSystemLibrary("mbedcrypto");
ssh2_lib.linkSystemLibrary("mbedx509");
}

if (target.result.isGnu()) {
lib.defineCMacro("HAVE_UNISTD_H", null);
lib.defineCMacro("HAVE_INTTYPES_H", null);
lib.defineCMacro("HAVE_SYS_TIME_H", null);
lib.defineCMacro("HAVE_GETTIMEOFDAY", null);
}
} else {
lib.defineCMacro("HAVE_UNISTD_H", null);
lib.defineCMacro("HAVE_INTTYPES_H", null);
lib.defineCMacro("HAVE_STDLIB_H", null);
lib.defineCMacro("HAVE_SYS_SELECT_H", null);
lib.defineCMacro("HAVE_SYS_UIO_H", null);
lib.defineCMacro("HAVE_SYS_SOCKET_H", null);
lib.defineCMacro("HAVE_SYS_IOCTL_H", null);
lib.defineCMacro("HAVE_SYS_TIME_H", null);
lib.defineCMacro("HAVE_SYS_UN_H", null);
lib.defineCMacro("HAVE_LONGLONG", null);
lib.defineCMacro("HAVE_GETTIMEOFDAY", null);
lib.defineCMacro("HAVE_INET_ADDR", null);
lib.defineCMacro("HAVE_POLL", null);
lib.defineCMacro("HAVE_SELECT", null);
lib.defineCMacro("HAVE_SOCKET", null);
lib.defineCMacro("HAVE_STRTOLL", null);
lib.defineCMacro("HAVE_SNPRINTF", null);
lib.defineCMacro("HAVE_O_NONBLOCK", null);
if (openssl) {
ssh2_lib.root_module.addCSourceFile(.{ .file = upstream.path("src/openssl.c"), .flags = ssh2_flags });
ssh2_lib.root_module.addCMacro("LIBSSH2_OPENSSL", "1");
ssh2_lib.linkSystemLibrary("ssl");
ssh2_lib.linkSystemLibrary("crypto");
}

if (wincng) {
ssh2_lib.root_module.addCSourceFile(.{ .file = upstream.path("src/wincng.c"), .flags = ssh2_flags });
ssh2_lib.root_module.addCMacro("LIBSSH2_WINCNG", "1");
// Windows system libs (zig handles names)
ssh2_lib.linkSystemLibrary("bcrypt");
ssh2_lib.linkSystemLibrary("ncrypt");
}

if (libgcrypt) {
ssh2_lib.root_module.addCSourceFile(.{ .file = upstream.path("src/libgcrypt.c"), .flags = ssh2_flags });
ssh2_lib.root_module.addCMacro("LIBSSH2_LIBGCRYPT", "1");
ssh2_lib.linkSystemLibrary("gcrypt");
}

b.installArtifact(lib);
if (zlib) {
if (b.systemIntegrationOption("zlib", .{})) {
ssh2_lib.root_module.linkSystemLibrary("zlib", .{});
} else if (b.lazyDependency("zlib", .{
.target = target,
.optimize = optimize,
})) |zlib_dependency| {
ssh2_lib.root_module.linkLibrary(zlib_dependency.artifact("z"));
}
}
}

pub const ssh2_src: []const []const u8 = &.{
"src/agent.c",
"src/bcrypt_pbkdf.c",
"src/blowfish.c",
"src/chacha.c",
"src/channel.c",
"src/cipher-chachapoly.c",
"src/comp.c",
"src/crypt.c",
"src/global.c",
"src/hostkey.c",
"src/keepalive.c",
"src/kex.c",
"src/knownhost.c",
"src/mac.c",
"src/misc.c",
"src/packet.c",
"src/pem.c",
"src/poly1305.c",
"src/publickey.c",
"src/scp.c",
"src/session.c",
"src/sftp.c",
"src/transport.c",
"src/userauth.c",
"src/userauth_kbd_packet.c",
"src/version.c",
};

pub const ssh2_flags: []const []const u8 = &.{};
Loading
Loading