mirror of
https://github.com/fluencelabs/c-template
synced 2025-04-25 14:42:13 +00:00
add the initial version
This commit is contained in:
parent
f2dd1f89bb
commit
0907da6246
13
.gitignore
vendored
Normal file
13
.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# IntelliJ
|
||||||
|
.idea
|
||||||
|
.vscode
|
||||||
|
*.iml
|
||||||
|
|
||||||
|
# MacOS folder metadata
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
# Text writing
|
||||||
|
*.scriv
|
||||||
|
|
||||||
|
# Webassembly generated files
|
||||||
|
*.wasm
|
20
Makefile
Normal file
20
Makefile
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
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
|
||||||
|
SDK = sdk/allocator.c sdk/logger.c
|
||||||
|
|
||||||
|
.PHONY: default all clean
|
||||||
|
|
||||||
|
default: $(TARGET)
|
||||||
|
all: default
|
||||||
|
|
||||||
|
$(TARGET): main.c $(SDK)
|
||||||
|
$(CC) $(SYSROOT) $(TARGET_TRIPLE) $(CFLAGS) $(EXPORT_FUNCS) $^ -o $@.wasm
|
||||||
|
|
||||||
|
.PRECIOUS: $(TARGET)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-rm -f $(TARGET).wasm
|
6
main.c
Normal file
6
main.c
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#include "sdk/allocator.h"
|
||||||
|
#include "sdk/logger.h"
|
||||||
|
|
||||||
|
char *invoke(const char *str, int length) {
|
||||||
|
|
||||||
|
}
|
13
sdk/allocator.c
Normal file
13
sdk/allocator.c
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include "allocator.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define UNUSED(x) (void)(x)
|
||||||
|
|
||||||
|
void *allocate(size_t size) {
|
||||||
|
return malloc(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void deallocate(void *ptr, size_t size) {
|
||||||
|
UNUSED(size);
|
||||||
|
free(ptr);
|
||||||
|
}
|
12
sdk/allocator.h
Normal file
12
sdk/allocator.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#ifndef FLUENCE_C_SDK_ALLOCATOR_H
|
||||||
|
#define FLUENCE_C_SDK_ALLOCATOR_H
|
||||||
|
|
||||||
|
#include <stddef.h> // for size_t
|
||||||
|
|
||||||
|
// used by Wasm VM for byte array passing (should be exported from module)
|
||||||
|
void *allocate(size_t size);
|
||||||
|
|
||||||
|
// used by Wasm VM for freeing previous memory allocated by allocate function
|
||||||
|
void deallocate(void *ptr, size_t size);
|
||||||
|
|
||||||
|
#endif //FLUENCE_C_SDK_ALLOCATOR_H
|
15
sdk/logger.c
Normal file
15
sdk/logger.c
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#include "logger.h"
|
||||||
|
|
||||||
|
#define __LOGGER_IMPORT(name) \
|
||||||
|
__attribute__((__import_module__("logger"), __import_name__(#name)))
|
||||||
|
|
||||||
|
void __write(char ch) __LOGGER_IMPORT(write);
|
||||||
|
void __flush() __LOGGER_IMPORT(flush);
|
||||||
|
|
||||||
|
void wasm_log(const char *str, int len) {
|
||||||
|
for(int byteId = 0; byteId < len; ++byteId) {
|
||||||
|
__write(str[byteId]);
|
||||||
|
}
|
||||||
|
|
||||||
|
__flush();
|
||||||
|
}
|
7
sdk/logger.h
Normal file
7
sdk/logger.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#ifndef FLUENCE_C_SDK_LOGGER_H
|
||||||
|
#define FLUENCE_C_SDK_LOGGER_H
|
||||||
|
|
||||||
|
// writes provided string to Wasm VM logger
|
||||||
|
void wasm_log(const char *str, int len);
|
||||||
|
|
||||||
|
#endif //FLUENCE_C_SDK_LOGGER_H
|
Loading…
x
Reference in New Issue
Block a user