diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index bfa87e6..35b997d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/build.zig b/build.zig index 01caa2b..a90718b 100644 --- a/build.zig +++ b/build.zig @@ -1,5 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); +const CrossTarget = std.zig.CrossTarget; pub fn build(b: *std.build.Builder) !void { b.setPreferredReleaseMode(.ReleaseFast); @@ -7,17 +8,17 @@ pub fn build(b: *std.build.Builder) !void { 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); @@ -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 diff --git a/python/fastfec.py b/python/fastfec.py index 5678bac..dabe403 100644 --- a/python/fastfec.py +++ b/python/fastfec.py @@ -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 = [ diff --git a/src/main.c b/src/main.c index 28c8c26..e772dc4 100644 --- a/src/main.c +++ b/src/main.c @@ -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); diff --git a/src/memory.c b/src/memory.c index 2b85359..519cb6f 100644 --- a/src/memory.c +++ b/src/memory.c @@ -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); diff --git a/src/wasm.c b/src/wasm.c new file mode 100644 index 0000000..4100933 --- /dev/null +++ b/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); +}