Skip to content

Commit 0161331

Browse files
committed
zig package for libssh2
0 parents  commit 0161331

File tree

7 files changed

+227
-0
lines changed

7 files changed

+227
-0
lines changed

.github/workflows/build.yml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Build and Test
2+
on:
3+
push:
4+
branches: [main]
5+
pull_request:
6+
branches: [main]
7+
workflow_dispatch:
8+
9+
jobs:
10+
build:
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
os: [windows-latest, ubuntu-latest, macos-latest]
15+
target: [
16+
native,
17+
x86_64-linux-gnu,
18+
x86_64-linux-musl,
19+
x86_64-windows,
20+
aarch64-macos,
21+
aarch64-linux-gnu,
22+
aarch64-linux-musl
23+
# TODO: bsd
24+
]
25+
optimize: [
26+
Debug,
27+
ReleaseSafe,
28+
ReleaseFast,
29+
ReleaseSmall
30+
]
31+
runs-on: ${{ matrix.os }}
32+
steps:
33+
- uses: actions/checkout@v2
34+
with:
35+
submodules: recursive
36+
37+
- name: Setup Zig
38+
uses: mlugg/setup-zig@v1
39+
with:
40+
version: 0.13.0
41+
42+
- name: Build
43+
run: zig build -Dtarget=${{ matrix.target }} -Doptimize=${{ matrix.optimize }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.zig-cache
2+
zig-out

build.zig

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
7+
const libssh2_dep = b.dependency("libssh2", .{
8+
.target = target,
9+
.optimize = optimize,
10+
});
11+
12+
const mbedtls_dep = b.dependency("mbedtls", .{
13+
.target = target,
14+
.optimize = optimize,
15+
});
16+
17+
const lib = b.addStaticLibrary(.{
18+
.name = "ssh2",
19+
.target = target,
20+
.optimize = optimize,
21+
.link_libc = true,
22+
});
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);
58+
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");
66+
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);
92+
}
93+
94+
b.installArtifact(lib);
95+
}

build.zig.zon

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
.{
2+
// This is the default name used by packages depending on this one. For
3+
// example, when a user runs `zig fetch --save <url>`, this field is used
4+
// as the key in the `dependencies` table. Although the user can choose a
5+
// different name, most users will stick with this provided value.
6+
//
7+
// It is redundant to include "zig" in this name because it is already
8+
// within the Zig package namespace.
9+
.name = "libssh2",
10+
11+
// This is a [Semantic Version](https://semver.org/).
12+
// In a future version of Zig it will be used for package deduplication.
13+
.version = "1.11.0",
14+
15+
// This field is optional.
16+
// This is currently advisory only; Zig does not yet do anything
17+
// with this value.
18+
//.minimum_zig_version = "0.11.0",
19+
20+
// This field is optional.
21+
// Each dependency must either provide a `url` and `hash`, or a `path`.
22+
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
23+
// Once all dependencies are fetched, `zig build` no longer requires
24+
// internet connectivity.
25+
.dependencies = .{
26+
.libssh2 = .{
27+
.url = "https://github.com/libssh2/libssh2/archive/refs/tags/libssh2-1.11.0.tar.gz",
28+
.hash = "1220a0863be6190270168974107d04653087aacc89e72cd81914789cb7d84b744fda",
29+
},
30+
.mbedtls = .{
31+
.url = "git+https://github.com/allyourcodebase/mbedtls.git#e4da72f6a8bedc883e34953514a3b62cbbd4f251",
32+
.hash = "1220ffeef9740c79b6f62f6bf350f12a7b14768acf548cb47cd56c5ca58a15af7970",
33+
},
34+
},
35+
.paths = .{
36+
"build.zig",
37+
"build.zig.zon",
38+
"src",
39+
// For example...
40+
//"LICENSE",
41+
//"README.md",
42+
},
43+
}

config/libssh2_config.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef LIBSSH2_CONFIG_H
2+
#define LIBSSH2_CONFIG_H
3+
4+
#ifdef WIN32
5+
#include <mswsock.h>
6+
#include <winsock2.h>
7+
#include <ws2tcpip.h>
8+
#endif
9+
10+
#endif

src/main.zig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const std = @import("std");
2+
3+
pub fn main() !void {
4+
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
5+
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
6+
7+
// stdout is for the actual output of your application, for example if you
8+
// are implementing gzip, then only the compressed bytes should be sent to
9+
// stdout, not any debugging messages.
10+
const stdout_file = std.io.getStdOut().writer();
11+
var bw = std.io.bufferedWriter(stdout_file);
12+
const stdout = bw.writer();
13+
14+
try stdout.print("Run `zig build test` to run the tests.\n", .{});
15+
16+
try bw.flush(); // don't forget to flush!
17+
}
18+
19+
test "simple test" {
20+
var list = std.ArrayList(i32).init(std.testing.allocator);
21+
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
22+
try list.append(42);
23+
try std.testing.expectEqual(@as(i32, 42), list.pop());
24+
}

src/root.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const std = @import("std");
2+
const testing = std.testing;
3+
4+
export fn add(a: i32, b: i32) i32 {
5+
return a + b;
6+
}
7+
8+
test "basic add functionality" {
9+
try testing.expect(add(3, 7) == 10);
10+
}

0 commit comments

Comments
 (0)