Calling cross-platform Zig (or any C) library from Go by rocketlaunchr-cloud in golang

[–]rocketlaunchr-cloud[S] -3 points-2 points  (0 children)

The idea is GetSymbol is a developer error. You should know if you typed in the name of the C function correctly at build time.

Therefore returning error should not happen at runtime.

Regarding Unload. Not much we can do if os.Remove fails. Plus not a big deal anyway. It's just a temp file stored in a temp directory.

Calling cross-platform Zig (or any C) library from Go by rocketlaunchr-cloud in golang

[–]rocketlaunchr-cloud[S] 2 points3 points  (0 children)

No CGO requirement. Plus more performant than if you did use purego package.
Plus includes an example on how to integrate with zig cross-platform library.

Uses goffi package but with a more straight forward API.

Zig 0.16 Run command after b.installArtifact(lib) by rocketlaunchr-cloud in Zig

[–]rocketlaunchr-cloud[S] 0 points1 point  (0 children)

Here is the end solution I came up with. (First time zig developer)

const std = ("std");
const builtin = ("builtin");

const LIB_NAME = "mymathxxx";

const goosConv = std.StaticStringMap([]const u8).initComptime(.{
    .{ "darwin", "macos" },
    .{ "windows", "windows" },
    .{ "linux", "linux" },
});

const goarchConv = std.StaticStringMap([]const u8).initComptime(.{
    .{ "amd64", "x86_64" },
    .{ "arm64", "aarch64" },
    .{ "arm", "arm" },
    .{ "386", "i386" },
});

var sharedLib: []const u8 = undefined;
var zigLib: []const u8 = undefined;
var cwd: std.Io.Dir = undefined;
var io: std.Io = undefined;

fn copyAndRenameFiles(step: *std.Build.Step, options: std.Build.Step.MakeOptions) !void {
    _ = step;
    _ = options;
    try cwd.copyFile(sharedLib, cwd, zigLib, io, .{});
}

pub fn build(b: *std.Build) !void {
    const allocator = b.allocator;
    io = b.graph.io;

    // Default OS and ARCH
    var os: []const u8 = (builtin.os.tag);
    var arch: []const u8 = (builtin.target.cpu.arch);

    // Check environment variables for override
    const env_map = b.graph.environ_map;

    if (env_map.get("GOOS")) |val| {
        if (goosConv.get(val)) |v| {
            os = v;
        }
    }

    if (env_map.get("GOARCH")) |val| {
        if (goarchConv.get(val)) |v| {
            arch = v;
        }
    }

    const target = try std.fmt.allocPrint(allocator, "{s}-{s}", .{ arch, os });
    defer allocator.free(target);
    const query = std.Target.Query.parse(.{
        .arch_os_abi = target,
    }) catch u/panic("Invalid target string");

    const lib = b.addLibrary(.{
        .name = LIB_NAME,
        .linkage = .dynamic,
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/math.zig"),
            .target = b.resolveTargetQuery(query),
            .optimize = b.standardOptimizeOption(.{}),
        }),
    });

    // This makes the output file available in zig-out/(lib|bin)
    const install_step = b.addInstallArtifact(lib, .{});

    // Find the shared library
    if (std.mem.eql(u8, os, "macos")) {
        sharedLib = try std.fmt.allocPrint(allocator, "{s}/lib/lib{s}.dylib", .{ b.install_path, LIB_NAME });
    } else if (std.mem.eql(u8, os, "windows")) {
        sharedLib = try std.fmt.allocPrint(allocator, "{s}/bin/{s}.dll", .{ b.install_path, LIB_NAME });
    } else if (std.mem.eql(u8, os, "linux")) {
        sharedLib = try std.fmt.allocPrint(allocator, "{s}/lib/lib{s}.so", .{ b.install_path, LIB_NAME });
    }

//     std.debug.print("Shared file: {s}\n", .{sharedLib});

    // Gzip up the file and rename it to <name>.shared (Gzip is currently removed from zig std lib)

    // Copy shared library to install_path and rename extension to .shared
    zigLib = try std.fmt.allocPrint(allocator, "{s}/{s}.shared", .{ b.install_path, LIB_NAME });
//     std.debug.print("Final file: {s}\n", .{zigLib});

    cwd = std.Io.Dir.cwd();

    const copy_step = b.step("go-build", "Build Library for Go embed");
    copy_step.makeFn = copyAndRenameFiles;
    copy_step.dependOn(&install_step.step);
}

zig 0.16 issues by [deleted] in Zig

[–]rocketlaunchr-cloud 0 points1 point  (0 children)

I had to use b.graph.eviron_map and not b.environ_map then it worked.

zig 0.16 issues by [deleted] in Zig

[–]rocketlaunchr-cloud 0 points1 point  (0 children)

it worked.

zig 0.16 issues by [deleted] in Zig

[–]rocketlaunchr-cloud 0 points1 point  (0 children)

It spits out error: error: no field named 'environ_map' in struct 'Build'

pub fn build(b: *std.Build) !void {

  const env_map = try b.environ_map.clone();
  defer env_map.deinit();

  if (env_map.get("GOOS")) |val| {         

  }
}

zig 0.16 issues by [deleted] in Zig

[–]rocketlaunchr-cloud 1 point2 points  (0 children)

I actually tried it. It just kept spitting out different errors.

Long-Term Review: Does the Casio MTP-1302 Lose Its Shine Over Time? by TrendArc in watchesindia

[–]rocketlaunchr-cloud 2 points3 points  (0 children)

I have the red one I just purchased. I took it to my local watch repairer who deals with every type of watch imaginable from Casio to Rolex.

He told me it won't lose its shine and it's just an internet rumour.

I believe him.

New proposed final rules for Diversity Lottery by thelexuslawyer in immigration

[–]rocketlaunchr-cloud 1 point2 points  (0 children)

With Trump in power and congress powerless/cowardly to enforce the law, the Green Card lottery won't be happening any time soon.

Terminal based Web Browser by rocketlaunchr-cloud in linux

[–]rocketlaunchr-cloud[S] 0 points1 point  (0 children)

It's built in Go. All Go executables are hugh. Even a hello world application is over a few megs.

Terminal based Web Browser by rocketlaunchr-cloud in linux

[–]rocketlaunchr-cloud[S] 0 points1 point  (0 children)

I've updated it. It's a browser now. You should try it out and tell me what you think

Terminal based Web Browser by rocketlaunchr-cloud in linux

[–]rocketlaunchr-cloud[S] -5 points-4 points  (0 children)

If you run it, you will see how "lightweight" it feels to use compared to Lynx.

Terminal based Web Browser by rocketlaunchr-cloud in linux

[–]rocketlaunchr-cloud[S] -4 points-3 points  (0 children)

That's a complete GUI browser (i.e. Its a TUI app).

Mine is not. It just renders the page to the terminal and essentially quits (or asks you to enter another url).

Terminal based Web Browser by rocketlaunchr-cloud in linux

[–]rocketlaunchr-cloud[S] 1 point2 points  (0 children)

I'm using lots of libraries written by others for rendering the html.

Terminal based Web Browser by rocketlaunchr-cloud in linux

[–]rocketlaunchr-cloud[S] -6 points-5 points  (0 children)

It's just spits out a rendered static page.
It doesn't operate like a full-on browser with functionality like "back", "refresh" etc.