Skip to content

Conversation

RokasPuzonas
Copy link
Contributor

Resolves #7

Here is an example for how the build.zig needs to look if you are using -fno-sys=mbedtls:

const mbedtls_dependency = b.dependency("mbedtls", .{
    .target = target,
    .optimize = optimize,
});

const libssh2_dependency = b.dependency("libssh2", .{
    .target = target,
    .optimize = optimize,
    .strip = optimize != .Debug,
    .@"crypto-backend" = .mbedtls,
});
const libssh2_artifact = libssh2_dependency.artifact("ssh2");
 
// This line is very important, it's important to link mbedtls with libssh2. Not with root_module.
// This is a very easy mistake to make.
libssh2_artifact.linkLibrary(mbedtls_dependency.artifact("mbedtls"));

root_module.linkLibrary(libssh2_artifact);

const exe = b.addExecutable(.{
    .name = "resource_tracker",
    .root_module = root_module
});

README.md Outdated
Comment on lines 42 to 43
By default libssh2 will dynamically link against the crypto backend.
This can be changed through system integration options, i.e. `-fsys` and `-fno-sys`
Copy link
Contributor

@hamptokr hamptokr Oct 1, 2025

Choose a reason for hiding this comment

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

System Integration Options do not control whether something is statically or dynamically linked, it's just controlling whether or not we should use the system library. That said, your changes below do make sense as it allows the user to provide/link their own vendored version if they want while still providing a sane default where it "just works"

build.zig Outdated
Comment on lines 78 to 79
// TODO: Add lazy dependency to build.zig.zon for linking statically.
// For now it's the users resposibility to compile and statically link against library
Copy link
Contributor

Choose a reason for hiding this comment

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

As mentioned, this has nothing to do with static/dynamic linking just means we don't try to link the system library for folks automatically

We can provide an error message here maybe:

Suggested change
// TODO: Add lazy dependency to build.zig.zon for linking statically.
// For now it's the users resposibility to compile and statically link against library
@panic("mbedtls not provided. If you pass --no-sys=mbedtls you must link your own mbedtls to the final app.");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if a nice error message can be provided, because using @panic here will make it always show the message.

Currently the error you will get when it isn't linked is this:

/home/rokas/.cache/zig/p/N-V-__8AAATRLQBu_rNy4X2UK6RtcdYK_yAzkV6OcCqgo5aK/src/mbedtls.h:59:10: error: 'mbedtls/version.h' file not found
#include <mbedtls/version.h>
         ^~~~~~~~~~~~~~~~~~~~
/home/rokas/.cache/zig/p/N-V-__8AAATRLQBu_rNy4X2UK6RtcdYK_yAzkV6OcCqgo5aK/src/agent.c:42:10: note: in file included from /home/rokas/.cache/zig/p/N-V-__8AAATRLQBu_rNy4X2UK6RtcdYK_yAzkV6OcCqgo5aK/src/agent.c:42:
#include "libssh2_priv.h"
         ^
/home/rokas/.cache/zig/p/N-V-__8AAATRLQBu_rNy4X2UK6RtcdYK_yAzkV6OcCqgo5aK/src/libssh2_priv.h:187:10: note: in file included from /home/rokas/.cache/zig/p/N-V-__8AAATRLQBu_rNy4X2UK6RtcdYK_yAzkV6OcCqgo5aK/src/libssh2_priv.h:187:
#include "crypto.h"
         ^
/home/rokas/.cache/zig/p/N-V-__8AAATRLQBu_rNy4X2UK6RtcdYK_yAzkV6OcCqgo5aK/src/crypto.h:49:10: note: in file included from /home/rokas/.cache/zig/p/N-V-__8AAATRLQBu_rNy4X2UK6RtcdYK_yAzkV6OcCqgo5aK/src/crypto.h:49:
#include "mbedtls.h"

// ...

build.zig Outdated
Comment on lines 89 to 90
// TODO: Add lazy dependency to build.zig.zon for linking statically.
// For now it's the users resposibility to compile and statically link against library
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, let's add an error message

Comment on lines +99 to +100
ssh2_lib.linkSystemLibrary2("bcrypt", .{});
ssh2_lib.linkSystemLibrary2("ncrypt", .{});
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

build.zig Outdated
Comment on lines 108 to 109
// TODO: Add lazy dependency to build.zig.zon for linking statically.
// For now it's the users resposibility to compile and statically link against library
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, add an error message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow not linking against crypto system libraries
2 participants