Skip to content

Commit

Permalink
Compile with WASI
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Oct 24, 2023
1 parent 3c4b3b5 commit d789c88
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/wasi-bindings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: WASI Bindings

on:
push:
paths:
- ".github/workflows/wasi-bindings.yml"
- "include/"
- "src/"
- "*akefile*"
branches:
- main
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: head
bundler-cache: true

- name: rake templates
run: bundle exec rake templates

- name: Set up WASI-SDK
run: |
wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-20.0-linux.tar.gz
tar xvf wasi-sdk-20.0-linux.tar.gz
- name: Build the project
run: make wasm WASI_SDK_PATH=$(pwd)/wasi-sdk-20.0

- uses: actions/upload-artifact@v3
with:
name: librubyparser.wasm
path: build/prism.wasm
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ SOEXT := $(shell ruby -e 'puts RbConfig::CONFIG["SOEXT"]')
CPPFLAGS := -Iinclude
CFLAGS := -g -O2 -std=c99 -Wall -Werror -Wextra -Wpedantic -Wundef -Wconversion -fPIC -fvisibility=hidden
CC := cc
WASI_SDK_PATH := /opt/wasi-sdk

HEADERS := $(shell find include -name '*.h')
SOURCES := $(shell find src -name '*.c')
Expand All @@ -23,6 +24,7 @@ all: shared static

shared: build/librubyparser.$(SOEXT)
static: build/librubyparser.a
wasm: build/prism.wasm

build/librubyparser.$(SOEXT): $(SHARED_OBJECTS)
$(ECHO) "linking $@"
Expand All @@ -32,6 +34,10 @@ build/librubyparser.a: $(STATIC_OBJECTS)
$(ECHO) "building $@"
$(Q) $(AR) $(ARFLAGS) $@ $(STATIC_OBJECTS) $(Q1:0=>/dev/null)

build/prism.wasm: Makefile $(SOURCES) $(HEADERS)
$(ECHO) "building $@"
$(Q) $(WASI_SDK_PATH)/bin/clang --sysroot=$(WASI_SDK_PATH)/share/wasi-sysroot/ $(DEBUG_FLAGS) -DPRISM_EXPORT_SYMBOLS -D_WASI_EMULATED_MMAN -lwasi-emulated-mman -v $(CPPFLAGS) $(CFLAGS) -o $@ $(SOURCES)

build/shared/%.o: src/%.c Makefile $(HEADERS)
$(ECHO) "compiling $@"
$(Q) mkdir -p $(@D)
Expand Down
4 changes: 2 additions & 2 deletions templates/src/serialize.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ static void
pm_serialize_magic_comment(pm_parser_t *parser, pm_magic_comment_t *magic_comment, pm_buffer_t *buffer) {
// serialize key location
pm_buffer_append_u32(buffer, pm_ptrdifft_to_u32(magic_comment->key_start - parser->start));
pm_buffer_append_u32(buffer, pm_ptrdifft_to_u32(magic_comment->key_length));
pm_buffer_append_u32(buffer, pm_sizet_to_u32(magic_comment->key_length));

// serialize value location
pm_buffer_append_u32(buffer, pm_ptrdifft_to_u32(magic_comment->value_start - parser->start));
pm_buffer_append_u32(buffer, pm_ptrdifft_to_u32(magic_comment->value_length));
pm_buffer_append_u32(buffer, pm_sizet_to_u32(magic_comment->value_length));
}

static void
Expand Down

0 comments on commit d789c88

Please sign in to comment.