From 614e972b950c79d2772e6483b9edf51bfaf81919 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Thu, 12 Jan 2023 17:31:51 +0100 Subject: [PATCH] An example of working function exports. --- zig-impl/drop/Makefile | 32 ++++++++++++++++++++++++++++++++ zig-impl/drop/build.zig | 23 +++++++++++++++++++++++ zig-impl/drop/src/main.zig | 28 ++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 zig-impl/drop/Makefile create mode 100644 zig-impl/drop/build.zig create mode 100644 zig-impl/drop/src/main.zig diff --git a/zig-impl/drop/Makefile b/zig-impl/drop/Makefile new file mode 100644 index 0000000..6b68f35 --- /dev/null +++ b/zig-impl/drop/Makefile @@ -0,0 +1,32 @@ +ZIGC=zig + +CC=gcc +CFLAGS=-Wall -Wextra +LDFLAGS=-I build/ -L build/ -lipc + +all: zigcompilation compilation + +ifeq ($(SRC),) +test-src: + @echo SRC must be set via command line. + @exit 1 +else +test-src: +endif + +list-obj-files: test-src + @# List all .o included in a .a archive. + ar t $(SRC) +list-symbols: test-src + @# List all symbols in a .so. + nm -D $(SRC) +list-symbols-alt: test-src + @# Alternative: grep .text section in an objdump output. + objdump -T $(SRC) | grep text + +zigcompilation: build.zig src/*.zig + $(ZIGC) build + +compilation: src/main.c + @echo the following compilation will produce errors despite actually working + $(CC) -o app -o main build/libipc.so $(CFLAGS) $^ $(LDFLAGS) diff --git a/zig-impl/drop/build.zig b/zig-impl/drop/build.zig new file mode 100644 index 0000000..7e83769 --- /dev/null +++ b/zig-impl/drop/build.zig @@ -0,0 +1,23 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const lib = b.addStaticLibrary("ipc", "src/main.zig"); + lib.setOutputDir("build"); + lib.setBuildMode(mode); + lib.install(); + + const solib = b.addSharedLibrary("ipc", "src/main.zig", b.version(0, 0, 1)); + solib.setOutputDir("build"); + solib.setBuildMode(mode); + solib.install(); + + const main_tests = b.addTest("src/main.zig"); + main_tests.setBuildMode(mode); + + const test_step = b.step("test", "Run library tests"); + test_step.dependOn(&main_tests.step); +} diff --git a/zig-impl/drop/src/main.zig b/zig-impl/drop/src/main.zig new file mode 100644 index 0000000..7c98d29 --- /dev/null +++ b/zig-impl/drop/src/main.zig @@ -0,0 +1,28 @@ +const std = @import("std"); +const testing = std.testing; + +const SOMESTRUCT = packed struct { + somevalue: i32, + const Self = @This(); + + fn update(self: *Self) callconv(.C) i32 { + self.somevalue += 1; + return self.somevalue; + } +}; + +export fn some_struct_bidouillage_init() callconv(.C) SOMESTRUCT { + return SOMESTRUCT {.somevalue = 2}; +} + +export fn some_struct_bidouillage_update(s: *SOMESTRUCT) callconv(.C) i32 { + return s.update(); +} + +export fn someipc(a: i32, b: i32) callconv(.C) i32 { + return a + b; +} + +test "basic add functionality" { + try testing.expect(someipc(3, 7) == 10); +}