mirror of
https://github.com/fluencelabs/c-template
synced 2025-04-24 14:22:12 +00:00
add docker build
This commit is contained in:
parent
a411ab612d
commit
7e8e556bf0
13
Dockerfile
Normal file
13
Dockerfile
Normal file
@ -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
|
13
Makefile
13
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)
|
||||
|
||||
|
15
README.md
15
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
|
||||
```
|
||||
docker-compose up
|
||||
```
|
||||
|
||||
or by Makefile with [wasi-sdk](https://github.com/CraneStation/wasi-sdk) installed
|
||||
```bash
|
||||
make
|
||||
```
|
||||
|
7
docker-compose.yml
Normal file
7
docker-compose.yml
Normal file
@ -0,0 +1,7 @@
|
||||
version: '3'
|
||||
services:
|
||||
hello_world:
|
||||
build:
|
||||
context: .
|
||||
volumes:
|
||||
- .:/code
|
19
main.c
19
main.c
@ -1,6 +1,25 @@
|
||||
#include "sdk/allocator.h"
|
||||
#include "sdk/logger.h"
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -3,10 +3,25 @@
|
||||
|
||||
#include <stddef.h> // 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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user