Skip to content

Commit

Permalink
feat: add wasm interface and compilation mode (tested it works separa…
Browse files Browse the repository at this point in the history
…tely)
  • Loading branch information
freedmand committed Nov 26, 2021
1 parent d8c13a1 commit 842b89c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Expand Up @@ -24,7 +24,7 @@ jobs:
- run: mv zig-out/lib/libfastfec.so libfastfec-linux-x86_64-${{ steps.get_version.outputs.VERSION }}.so
- run: zig build -Dlib-only=true -Dtarget=aarch64-linux
- run: mv zig-out/lib/libfastfec.so libfastfec-linux-aarch64-${{ steps.get_version.outputs.VERSION }}.so
- run: zig build -Dlib-only=true -Dtarget=wasm32-freestanding
- run: zig build -Dwasm
- run: mv zig-out/lib/fastfec.wasm libfastfec-${{ steps.get_version.outputs.VERSION }}.wasm
- name: Release
uses: softprops/action-gh-release@v1
Expand Down
43 changes: 30 additions & 13 deletions build.zig
@@ -1,23 +1,24 @@
const std = @import("std");
const builtin = @import("builtin");
const CrossTarget = std.zig.CrossTarget;

pub fn build(b: *std.build.Builder) !void {
b.setPreferredReleaseMode(.ReleaseFast);
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();

const lib_only: bool = b.option(bool, "lib-only", "Only compile the library") orelse false;
const wasm: bool = b.option(bool, "wasm", "Compile the wasm library") orelse false;

// Compile pcre
const pcre = b.addStaticLibrary("pcre", null);
pcre.setBuildMode(.ReleaseFast);
pcre.setTarget(target);
pcre.linkLibC();
pcre.addIncludeDir(pcreIncludeDir);
pcre.addCSourceFiles(&pcreSources, &buildOptions);

// Main build step
if (!lib_only) {
if (!lib_only and !wasm) {
const fastfec_cli = b.addExecutable("fastfec", null);
fastfec_cli.setTarget(target);
fastfec_cli.setBuildMode(mode);
Expand Down Expand Up @@ -45,18 +46,34 @@ pub fn build(b: *std.build.Builder) !void {
}, &buildOptions);
}

// Library build step
const fastfec_lib = b.addSharedLibrary("fastfec", null, .unversioned);
fastfec_lib.setTarget(target);
fastfec_lib.setBuildMode(mode);
fastfec_lib.install();
fastfec_lib.linkLibC();
fastfec_lib.addCSourceFiles(&libSources, &buildOptions);
if (builtin.os.tag == .windows) {
fastfec_lib.linkSystemLibrary("pcre");
if (!wasm) {
// Library build step
const fastfec_lib = b.addSharedLibrary("fastfec", null, .unversioned);
fastfec_lib.setTarget(target);
fastfec_lib.setBuildMode(mode);
fastfec_lib.install();
fastfec_lib.linkLibC();
if (builtin.os.tag == .windows) {
fastfec_lib.linkSystemLibrary("pcre");
} else {
fastfec_lib.addIncludeDir(pcreIncludeDir);
fastfec_lib.linkLibrary(pcre);
}
fastfec_lib.addCSourceFiles(&libSources, &buildOptions);
} else {
fastfec_lib.addIncludeDir(pcreIncludeDir);
fastfec_lib.linkLibrary(pcre);
// Wasm library build step
const fastfec_wasm = b.addSharedLibrary("fastfec", null, .unversioned);
const wasm_target = CrossTarget{ .cpu_arch = .wasm32, .os_tag = .wasi };
fastfec_wasm.setTarget(wasm_target);
// Update pcre target for wasm
pcre.setTarget(wasm_target);
fastfec_wasm.setBuildMode(mode);
fastfec_wasm.install();
fastfec_wasm.linkLibC();
fastfec_wasm.addIncludeDir(pcreIncludeDir);
fastfec_wasm.linkLibrary(pcre);
fastfec_wasm.addCSourceFiles(&libSources, &buildOptions);
fastfec_wasm.addCSourceFile("src/wasm.c", &buildOptions);
}

// Test step
Expand Down
2 changes: 1 addition & 1 deletion python/fastfec.py
Expand Up @@ -49,7 +49,7 @@ def init_lib(self):
self.libfastfec = CDLL(os.path.join(SCRIPT_DIR, "../zig-out/lib/libfastfec.dylib"))

# Lay out arg/res types
self.libfastfec.newPersistentMemoryContext.argtypes = [c_int]
self.libfastfec.newPersistentMemoryContext.argtypes = []
self.libfastfec.newPersistentMemoryContext.restype = c_void_p

self.libfastfec.newFecContext.argtypes = [
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Expand Up @@ -279,7 +279,7 @@ int main(int argc, char *argv[])
}

// Initialize persistent memory context
PERSISTENT_MEMORY_CONTEXT *persistentMemory = newPersistentMemoryContext(0);
PERSISTENT_MEMORY_CONTEXT *persistentMemory = newPersistentMemoryContext();
// Initialize FEC context
FEC_CONTEXT *fec = newFecContext(persistentMemory, ((BufferRead)(&url_readBuffer)), BUFFERSIZE, NULL, BUFFERSIZE, handle, fecId, outputDirectory, includeFilingId, silent, warn);

Expand Down
2 changes: 1 addition & 1 deletion src/memory.c
Expand Up @@ -68,7 +68,7 @@ void copyString(STRING *src, STRING *dst)
strcpy(dst->str, src->str);
}

PERSISTENT_MEMORY_CONTEXT *newPersistentMemoryContext(int bufferSize)
PERSISTENT_MEMORY_CONTEXT *newPersistentMemoryContext()
{
PERSISTENT_MEMORY_CONTEXT *ctx = malloc(sizeof(PERSISTENT_MEMORY_CONTEXT));
ctx->rawLine = newString(DEFAULT_STRING_SIZE);
Expand Down
12 changes: 12 additions & 0 deletions src/wasm.c
@@ -0,0 +1,12 @@
#include "fec.h"

extern size_t wasmBufferRead(char *buffer, int want, void *data);
extern void wasmBufferWrite(char* filename, char* extension, char* contents, int numBytes);

void wasmFec(int bufferSize) {
PERSISTENT_MEMORY_CONTEXT *persistentMemory = newPersistentMemoryContext();
FEC_CONTEXT *fec = newFecContext(persistentMemory, ((BufferRead)(&wasmBufferRead)), bufferSize, ((CustomWriteFunction)(&wasmBufferWrite)), bufferSize, NULL, NULL, NULL, 0, 1, 0);
int fecParseResult = parseFec(fec);
freeFecContext(fec);
freePersistentMemoryContext(persistentMemory);
}

0 comments on commit 842b89c

Please sign in to comment.