Skip to content

Commit

Permalink
feat: make sources compatible with WASI SDK
Browse files Browse the repository at this point in the history
WASI has no support for <filesystem> or <termios.h>
  • Loading branch information
edubart committed Sep 7, 2023
1 parent e6ef978 commit ccfcb33
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
10 changes: 7 additions & 3 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ EMPTY:=
SPACE:=$(EMPTY) $(EMPTY)
CLANG_TIDY_HEADER_FILTER=$(PWD)/($(subst $(SPACE),|,$(LINTER_HEADERS)))

CXXFLAGS+=$(OPTFLAGS) -std=c++17 -fvisibility=hidden -fPIC -MMD $(CC_MARCH) $(INCS) $(GCFLAGS) $(UBFLAGS) $(DEFS) $(WARNS) $(MYCFLAGS)
CFLAGS+=$(OPTFLAGS) -std=c99 -fvisibility=hidden -fPIC -MMD $(CC_MARCH) $(INCS) $(GCFLAGS) $(UBFLAGS) $(DEFS) $(WARNS) $(MYCFLAGS)
LDFLAGS+=$(UBFLAGS) $(MYLDFLAGS)
CXXFLAGS+=$(OPTFLAGS) -std=c++17 -fvisibility=hidden -fPIC -MMD $(CC_MARCH) $(INCS) $(GCFLAGS) $(UBFLAGS) $(DEFS) $(WARNS)
CFLAGS+=$(OPTFLAGS) -std=c99 -fvisibility=hidden -fPIC -MMD $(CC_MARCH) $(INCS) $(GCFLAGS) $(UBFLAGS) $(DEFS) $(WARNS)
LDFLAGS+=$(UBFLAGS)

COVERAGE_WORKLOAD=\
dhrystone 100; \
Expand All @@ -289,6 +289,10 @@ $(error invalid value for COVERAGE_TOOLCHAIN: $(COVERAGE_TOOLCHAIN))
endif
endif

CXXFLAGS+=$(MYCXXFLAGS)
CFLAGS+=$(MYCFLAGS)
LDFLAGS+=$(MYLDFLAGS)

all: luacartesi grpc hash c-api jsonrpc-remote-cartesi-machine

.PHONY: all generate use clean test lint format format-lua check-format check-format-lua luacartesi libcartesi grpc hash c-api compile_flags.txt
Expand Down
3 changes: 0 additions & 3 deletions src/machine-c-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <any>
#include <cstring>
#include <exception>
#include <filesystem>
#include <future>
#include <ios>
#include <optional>
Expand Down Expand Up @@ -84,8 +83,6 @@ int cm_result_failure(char **err_msg) try { throw; } catch (std::exception &e) {
return CM_ERROR_REGEX_ERROR;
} catch (std::ios_base::failure &ex) {
return CM_ERROR_SYSTEM_IOS_BASE_FAILURE;
} catch (std::filesystem::filesystem_error &ex) {
return CM_ERROR_FILESYSTEM_ERROR;
} catch (std::runtime_error &ex) {
return CM_ERROR_RUNTIME_ERROR;
} catch (std::bad_typeid &ex) {
Expand Down
15 changes: 10 additions & 5 deletions src/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <cinttypes>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <future>
#include <iomanip>
#include <iostream>
Expand Down Expand Up @@ -364,13 +363,19 @@ machine::machine(const machine_config &c, const machine_runtime_config &r) :
const std::string flash_description = "flash drive "s + std::to_string(i++);
// Auto detect flash drive image length
if (f.length == UINT64_C(-1)) {
std::error_code ec;
f.length = std::filesystem::file_size(f.image_filename, ec);
if (ec) {
throw std::system_error{ec.value(), ec.category(),
auto fp = unique_fopen(f.image_filename.c_str(), "rb");
if (fseek(fp.get(), 0, SEEK_END) != 0) {
throw std::system_error{errno, std::generic_category(),
"unable to obtain length of image file '"s + f.image_filename + "' when initializing "s +
flash_description};
}
const auto length = ftell(fp.get());
if (length < 0) {
throw std::system_error{errno, std::generic_category(),
"unable to obtain length of image file '"s + f.image_filename + "' when initializing "s +
flash_description};
}
f.length = length;
}
register_pma_entry(make_flash_drive_pma_entry(flash_description, f));
}
Expand Down
17 changes: 14 additions & 3 deletions src/tty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>

#ifndef NO_TERMIOS
#include <termios.h>
#endif

#include "tty.h"

namespace cartesi {
Expand All @@ -34,13 +37,16 @@ static const int CONSOLE_BUF_SIZE = 1024; ///< Number of characters in console i
/// \brief TTY global state
struct tty_state {
bool initialized{false};
int ttyfd{-1};
termios oldtty{};
std::array<char, CONSOLE_BUF_SIZE> buf{};
ssize_t buf_pos{};
ssize_t buf_len{};
#ifndef NO_TERMIOS
int ttyfd{-1};
termios oldtty{};
#endif
};

#ifndef NO_TERMIOS
static int new_ttyfd(const char *path) {
int fd{};
do {
Expand All @@ -66,6 +72,7 @@ static int get_ttyfd(void) {
// NOLINTEND(bugprone-assignment-in-if-condition)
return -1;
}
#endif

static bool try_read_chars_from_stdin(uint64_t wait, char *data, size_t max_len, long *actual_len) {
const int fd_max{0};
Expand Down Expand Up @@ -98,6 +105,7 @@ void tty_initialize(void) {
throw std::runtime_error("TTY already initialized.");
}
s->initialized = true;
#ifndef NO_TERMIOS
// NOLINTNEXTLINE(bugprone-assignment-in-if-condition)
if ((s->ttyfd = get_ttyfd()) >= 0) {
struct termios tty {};
Expand Down Expand Up @@ -129,6 +137,7 @@ void tty_initialize(void) {
tcsetattr(s->ttyfd, TCSANOW, &tty);
//??D Should we check to see if changes stuck?
}
#endif
}

void tty_finalize(void) {
Expand All @@ -137,11 +146,13 @@ void tty_finalize(void) {
throw std::runtime_error("TTY not initialized");
}
s->initialized = false;
#ifndef NO_TERMIOS
if (s->ttyfd >= 0) {
tcsetattr(s->ttyfd, TCSANOW, &s->oldtty);
close(s->ttyfd);
s->ttyfd = -1;
}
#endif
}

void tty_poll_console(uint64_t wait) {
Expand Down
8 changes: 4 additions & 4 deletions third-party/xkcp/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
LIB_CFLAGS+=-O2 -g0 -fvisibility=hidden -fPIC
LIB_CFLAGS+=-fno-stack-protector -D_FORTIFY_SOURCE=0
CFLAGS=-O2 -g0
LIB_CFLAGS=-fvisibility=hidden -fPIC -fno-stack-protector -D_FORTIFY_SOURCE=0
XKCP_DIR=XKCP-f7fe32a80f0c6600d1c5db50392a43265d3bba9a
XKCP_LIBS=$(XKCP_DIR)/bin/libXKCP.a

Expand All @@ -20,7 +20,7 @@ $(XKCP_DIR)/bin:
$(MAKE) -C $(XKCP_DIR)

$(XKCP_DIR)/bin/generic64/libXKCP.a: $(XKCP_DIR)/bin
$(MAKE) -C $(XKCP_DIR) generic64/libXKCP.a CC=$(CC) AR=$(AR) CFLAGS="$(CFLAGS) $(LIB_CFLAGS)"
$(MAKE) -C $(XKCP_DIR) generic64/libXKCP.a CC=$(CC) AR=$(AR) CFLAGS="$(CFLAGS) $(LIB_CFLAGS) $(MYCFLAGS)"

$(XKCP_DIR)/bin/libXKCP.a: $(XKCP_DIR)/bin/generic64/libXKCP.a
cp $< $@
Expand All @@ -30,7 +30,7 @@ $(XKCP_DIR)/bin/libXKCP_AVX.a: ARCHFLAGS=-mavx
$(XKCP_DIR)/bin/libXKCP_AVX2.a: ARCHFLAGS=-mavx2
$(XKCP_DIR)/bin/libXKCP_AVX512.a: ARCHFLAGS=-march=skylake-avx512
$(XKCP_DIR)/bin/libXKCP_%.a: $(XKCP_DIR)/bin/libXKCP.a
$(MAKE) -C $(XKCP_DIR) $*/libXKCP.a CC=$(CC) AR=$(AR) CFLAGS="$(CFLAGS) $(ARCHFLAGS) $(LIB_CFLAGS)"
$(MAKE) -C $(XKCP_DIR) $*/libXKCP.a CC=$(CC) AR=$(AR) CFLAGS="$(ARCHFLAGS) $(CFLAGS) $(LIB_CFLAGS) $(MYCFLAGS)"
# prefix all library symbols with the target extension
objcopy --prefix-symbols $*_ \
$(XKCP_DIR)/bin/$*/libXKCP.a $@
Expand Down

0 comments on commit ccfcb33

Please sign in to comment.