From 7e8e556bf0bcd03cae32e3015f0c24f9fca9e47b Mon Sep 17 00:00:00 2001 From: vms Date: Sun, 21 Apr 2019 22:23:36 +0300 Subject: [PATCH] add docker build --- Dockerfile | 13 +++++++++++++ Makefile | 13 +++++++------ README.md | 15 +++++++++------ docker-compose.yml | 7 +++++++ main.c | 19 +++++++++++++++++++ sdk/allocator.h | 19 +++++++++++++++++-- sdk/logger.h | 6 +++++- 7 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..748a27c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM ubuntu:19.04 + +RUN apt-get update \ + && apt-get install -y ca-certificates \ + curl \ + git \ + make + +RUN curl -L https://github.com/CraneStation/wasi-sdk/releases/download/wasi-sdk-4/wasi-sdk-4.0-linux-amd64.tar.gz | tar xz --strip-components=1 -C / + +VOLUME /code +WORKDIR /code +CMD make diff --git a/Makefile b/Makefile index 45b2211..63f99d6 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,10 @@ TARGET = hello_world -CC = clang -SYSROOT = --sysroot=/tmp/wasi-sdk/ -TARGET_TRIPLE = --target=wasm32-unknown-wasi -CFLAGS = -nostartfiles -fvisibility=hidden -Wl,--no-entry,--demangle,--allow-undefined -EXPORT_FUNCS = -Wl,--export=allocate,--export=deallocate,--export=invoke +CC = /opt/wasi-sdk/bin/clang +SYSROOT = /opt/wasi-sdk/share/sysroot +TARGET_TRIPLE = wasm32-unknown-wasi +CFLAGS = -nostartfiles -fvisibility=hidden +LDFLAGS = -Wl,--no-entry,--demangle,--allow-undefined +EXPORT_FUNCS = --export=allocate,--export=deallocate,--export=invoke SDK = sdk/allocator.c sdk/logger.c .PHONY: default all clean @@ -12,7 +13,7 @@ default: $(TARGET) all: default $(TARGET): main.c $(SDK) - $(CC) $(SYSROOT) $(TARGET_TRIPLE) $(CFLAGS) $(EXPORT_FUNCS) $^ -o $@.wasm + $(CC) --sysroot=$(SYSROOT) --target=$(TARGET_TRIPLE) $(CFLAGS) $(LDFLAGS) -Wl,$(EXPORT_FUNCS) $^ -o $@.wasm .PRECIOUS: $(TARGET) diff --git a/README.md b/README.md index 839eca2..8e27609 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ -# C template +# How to build -Template backend project for Rust - -## How to compile +This app could be built either with docker ```bash -~$ make -``` \ No newline at end of file +docker-compose up +``` + +or by Makefile with [wasi-sdk](https://github.com/CraneStation/wasi-sdk) installed +```bash +make +``` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..42ef770 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,7 @@ +version: '3' +services: + hello_world: + build: + context: . + volumes: + - .:/code diff --git a/main.c b/main.c index ab2d82b..52bdbb0 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,25 @@ #include "sdk/allocator.h" #include "sdk/logger.h" +#include + +const char *const greeting = "Hello world! From "; +const int RESPONSE_SIZE_BYTES = 4; char *invoke(const char *str, int length) { + const size_t greeting_length = strlen(greeting); + // TODO: add check for overflow + const size_t response_length = length + RESPONSE_SIZE_BYTES + greeting_length; + char *response = (char *)allocate(response_length); + + wasm_log(str, length); + + for(int i = 0; i < RESPONSE_SIZE_BYTES; ++i) { + response[i] = (response_length >> 8*i) & 0xFF; + } + + memcpy(response + RESPONSE_SIZE_BYTES, greeting, greeting_length); + memcpy(response + RESPONSE_SIZE_BYTES + greeting_length, str, length); + + return response; } diff --git a/sdk/allocator.h b/sdk/allocator.h index fcb9121..4fd3347 100644 --- a/sdk/allocator.h +++ b/sdk/allocator.h @@ -3,10 +3,25 @@ #include // for size_t -// used by Wasm VM for byte array passing (should be exported from module) +/** + * Allocates a memory region of given size. + * + * Used by Wasm VM for byte array passing. Should be exported from module. + * + * @param size a size of needed memory region. + * @return a pointer to allocated memory region. + */ void *allocate(size_t size); -// used by Wasm VM for freeing previous memory allocated by allocate function +/** + * Frees a memory region. + * + * Used by Wasm VM for freeing previous memory allocated by `allocate` function. + * Should be exported from module. + * + * @param ptr the pointer to the previously allocated memory region. + * @param size the size of the previously allocated memory region. + */ void deallocate(void *ptr, size_t size); #endif //FLUENCE_C_SDK_ALLOCATOR_H diff --git a/sdk/logger.h b/sdk/logger.h index 0421077..eba6076 100644 --- a/sdk/logger.h +++ b/sdk/logger.h @@ -1,7 +1,11 @@ #ifndef FLUENCE_C_SDK_LOGGER_H #define FLUENCE_C_SDK_LOGGER_H -// writes provided string to Wasm VM logger +/** + * Writes provided string to Wasm VM logger. + * + * @param log_message a message that should be logged. + */ void wasm_log(const char *str, int len); #endif //FLUENCE_C_SDK_LOGGER_H