diff --git a/platforms/openwrt/LICENSE b/platforms/openwrt/LICENSE new file mode 100644 index 0000000..3c2a07a --- /dev/null +++ b/platforms/openwrt/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2019 Volodymyr Shymanskyy + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/platforms/openwrt/README.md b/platforms/openwrt/README.md new file mode 100644 index 0000000..94bca05 --- /dev/null +++ b/platforms/openwrt/README.md @@ -0,0 +1,36 @@ +# wasm3 for OpenWRT + +This is **wasm3** package for **OpenWRT**. + + wasm3 is an extremely fast WebAssembly interpreter. + +## Build from source + +```bash +echo "src-git wasm3 git://github.com/vshymanskyy/OpenWRT-wasm3-packages.git" >> ./feeds.conf +./scripts/feeds update -a +./scripts/feeds install -p wasm3 -a +make menuconfig +``` +Select ```Languages -> WebAssembly -> wasm3``` +``` +make -j9 +``` + +## Build just wasm3 +``` +make package/wasm3/compile V=s +``` + +## Rebuild wasm3 +``` +make package/wasm3/{clean,compile,install} V=s +``` + +## Install on device + +``` +scp ./bin/ramips/packages/wasm3/wasm3_*.ipk root@DEVICE:/tmp +# Then on device: +opkg install /tmp/wasm3_*.ipk +``` diff --git a/platforms/openwrt/build/Makefile b/platforms/openwrt/build/Makefile new file mode 100644 index 0000000..0bf94e4 --- /dev/null +++ b/platforms/openwrt/build/Makefile @@ -0,0 +1,14 @@ + +M3_SRC_DIR := ../../../source + +SOURCES := main.c \ + $(shell find $(M3_SRC_DIR) -type f -name '*.c') + +all: wasm3 + +override CFLAGS += -std=c99 -O3 -flto -Wfatal-errors -fstrict-aliasing -I$(M3_SRC_DIR) +wasm3: $(SOURCES) + $(CC) $(CFLAGS) $(SOURCES) -o wasm3 -lm + +clean: + $(RM) wasm3 diff --git a/platforms/openwrt/build/main.c b/platforms/openwrt/build/main.c new file mode 100644 index 0000000..59ae4bb --- /dev/null +++ b/platforms/openwrt/build/main.c @@ -0,0 +1,78 @@ +#include +#include +#include + +#include "m3.h" +#include "m3_core.h" + +#define FATAL(msg, ...) { printf("Fatal: " msg "\n", ##__VA_ARGS__); return 1; } + +int main (int i_argc, const char * i_argv []) +{ + M3Result result = c_m3Err_none; + + if (i_argc < 3) FATAL("not enough arguments"); + + u8* wasm = NULL; + u32 fsize = 0; + + //printf("Loading WebAssembly...\n"); + + FILE* f = fopen (i_argv[1], "rb"); + if (f) + { + fseek (f, 0, SEEK_END); + fsize = ftell(f); + fseek (f, 0, SEEK_SET); + + if (fsize > 1000000) FATAL("file is too big"); + + wasm = (u8*) malloc(fsize); + fread (wasm, 1, fsize, f); + fclose (f); + } + + IM3Module module; + result = m3_ParseModule (& module, wasm, fsize); + if (result) FATAL("m3_ParseModule: %s", result); + + IM3Runtime env = m3_NewRuntime (4096); + if (!env) FATAL("m3_NewRuntime"); + + result = m3_LoadModule (env, module); + if (result) FATAL("m3_LoadModule: %s", result); + + IM3Function func; + result = m3_FindFunction (& func, env, "__post_instantiate"); + + if (not result) + { + result = m3_Call (func); + if (result) FATAL("__post_instantiate failed: %s", result); + } + + const char* fname = i_argv[2]; + + result = m3_FindFunction (& func, env, fname); + if (result) FATAL("m3_FindFunction: %s", result); + + //printf("Running...\n"); + + if (!strcmp(fname, "_main") || !strcmp(fname, "main")) { + i_argc -= 2; + i_argv += 2; + result = m3_CallMain (func, i_argc, i_argv); + } else { + i_argc -= 3; + i_argv += 3; + result = m3_CallWithArgs (func, i_argc, i_argv); + } + + free (wasm); + + if (result) FATAL("m3_Call: %s", result); + + printf ("\n"); + + return 0; +} diff --git a/platforms/openwrt/wasm3/wasm3/Makefile b/platforms/openwrt/wasm3/wasm3/Makefile new file mode 100755 index 0000000..a5d74ec --- /dev/null +++ b/platforms/openwrt/wasm3/wasm3/Makefile @@ -0,0 +1,49 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=wasm3 +PKG_VERSION:=0.1.0 +PKG_RELEASE:=1 + +PKG_MAINTAINER:=Volodymyr Shymanskyy +PKG_LICENSE:=MIT + +PKG_SOURCE_URL:=file:///home/vshymanskyy/_Projects/_experiments/Wasm/vsh-m3 +#PKG_SOURCE_PROTO:=git +#PKG_SOURCE_URL:=git://github.com/vshymanskyy/m3.git +#PKG_SOURCE_VERSION:=master +PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION) +PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz + +PKG_BUILD_PARALLEL:=1 + +include $(INCLUDE_DIR)/package.mk + +define Package/wasm3 + SUBMENU:=WebAssembly + SECTION:=lang + CATEGORY:=Languages + TITLE:=Fast WASM interpreter + URL:=https://github.com/vshymanskyy/m3 +# DEPENDS:=+libpthread +endef + +define Package/wasm3/description + wasm3 is an extremely fast WASM interpreter. +endef + +MAKE_PATH := platforms/openwrt/build + +MAKE_FLAGS += LD="$(TARGET_CC)" +MAKE_FLAGS += RELEASE=1 +MAKE_FLAGS += OPENWRT_UCLIBC=1 + +#define Package/hello/Build/Compile +# $(MAKE) -C $(PKG_BUILD_DIR) hello +#endef + +define Package/wasm3/install + $(INSTALL_DIR) $(1)/usr/bin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/$(MAKE_PATH)/wasm3 $(1)/usr/bin/ +endef + +$(eval $(call BuildPackage,wasm3))