Skip to content

Commit

Permalink
cli: Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
remko committed Nov 5, 2023
1 parent e31b763 commit c973477
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 76 deletions.
40 changes: 32 additions & 8 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,40 @@ runs:
with:
node-version: 17
cache: 'yarn'
- run: |
curl -L -s https://github.com/WebAssembly/wabt/releases/download/1.0.31/wabt-1.0.31-ubuntu.tar.gz | tar xvz -C /tmp
sudo mv /tmp/wabt-1.0.31/bin/* /usr/local/bin
sudo mv /tmp/wabt-1.0.31/include/* /usr/local/include
sudo mv /tmp/wabt-1.0.31/lib/*.a /usr/local/lib
sudo mv /tmp/wabt-1.0.31/share/wabt /usr/local/share
- if: runner.os == 'Linux'
run: |
curl -L -s https://github.com/WebAssembly/wabt/releases/download/1.0.33/wabt-1.0.33-ubuntu.tar.gz | tar xvz -C /tmp
sudo mv /tmp/wabt-1.0.33/bin/* /usr/local/bin
sudo mv /tmp/wabt-1.0.33/include/* /usr/local/include
sudo mv /tmp/wabt-1.0.33/lib/*.a /usr/local/lib
sudo mv /tmp/wabt-1.0.33/share/wabt /usr/local/share
echo "WABT_INCLUDE_DIR=/usr/local/include" >> $GITHUB_ENV
echo "WABT_LIB_DIR=/usr/local/lib" >> $GITHUB_ENV
echo "WABT_DATA_DIR=/usr/local/share/wabt" >> $GITHUB_ENV
shell: bash
- run: |
- if: runner.os == 'macOS'
run: |
curl -L -s https://github.com/WebAssembly/wabt/releases/download/1.0.33/wabt-1.0.33-macos-12.tar.gz | tar xvz -C /tmp
sudo mv /tmp/wabt-1.0.33/bin/* /usr/local/bin
sudo mv /tmp/wabt-1.0.33/include/* /usr/local/include
sudo mv /tmp/wabt-1.0.33/lib/*.a /usr/local/lib
sudo mv /tmp/wabt-1.0.33/share/wabt /usr/local/share
shell: bash
- if: runner.os == 'Windows'
run: |
curl -L -s https://github.com/WebAssembly/wabt/releases/download/1.0.33/wabt-1.0.33-windows.tar.gz | tar xvz -C /tmp
mv /tmp/wabt-1.0.33 /c/tools/wabt
cp /c/tools/wabt/bin/* /c/Windows/system32
shell: bash
- if: runner.os == 'Linux'
run: |
curl -L -s https://rabbits.srht.site/uxn/uxn-essentials-lin64.tar.gz | tar xvz
echo "$GITHUB_WORKSPACE/uxn" >> $GITHUB_PATH
shell: bash
- if: runner.os == 'macOS'
run: |
curl -L -s https://rabbits.srht.site/uxn/uxn-essentials-mac64.tar.gz | tar xvz
echo "$GITHUB_WORKSPACE/uxn" >> $GITHUB_PATH
shell: bash
- run: yarnpkg --pure-lockfile
shell: bash
shell: bash
19 changes: 19 additions & 0 deletions .github/workflows/build-cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Build CLI

on:
push:
pull_request:
workflow_dispatch:
workflow_call:

jobs:
build:
strategy:
matrix:
os: [macos-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/setup
- run: make -C src/cli package
- run: make -C src/cli test
23 changes: 23 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ jobs:
build:
uses: ./.github/workflows/build.yml

build-cli:
uses: ./.github/workflows/build-cli.yml

publish:
needs: build
runs-on: ubuntu-latest
Expand All @@ -29,3 +32,23 @@ jobs:
RELEASE_ASSETS_UPLOAD_URL: https://uploads.github.com/repos/${{ github.event.repository.full_name}}/releases/${{ github.event.release.id }}/assets
shell: bash

publish-cli:
needs: build-cli
runs-on: [macos-latest, ubuntu-latest]
steps:
- uses: actions/checkout@v3
- uses: ./.github/actions/setup
- run: make -C src/cli package
- name: "Upload artifacts"
run: |
for f in `find dist -name 'uxn-wasm-cli.*'`; do
curl --fail \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: $(file -b --mime-type $f)" \
--data-binary @$f \
"$RELEASE_ASSETS_UPLOAD_URL?name=$(basename $f)"
done
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RELEASE_ASSETS_UPLOAD_URL: https://uploads.github.com/repos/${{ github.event.repository.full_name}}/releases/${{ github.event.release.id }}/assets
shell: bash
6 changes: 6 additions & 0 deletions src/cli/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
BasedOnStyle: LLVM
ColumnLimit: 150
---
Language: Cpp
AllowShortFunctionsOnASingleLine: Empty
5 changes: 3 additions & 2 deletions src/cli/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*.sym
*.profraw
*.profdata
*.log
/coverage
/uxnwasm
uxnwasm_core.*
/uxn-wasm-cli
cli_core.*
78 changes: 62 additions & 16 deletions src/cli/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
.DEFAULT_GOAL := all

VERSION?=$(shell cat ../../package.json | grep '"version"' | sed -e 's/.*:.*"\(.*\)".*/\1/')

ifeq ($(OS),Windows_NT)
CXX = g++
WABT_DIR = /c/tools/wabt
WABT_INCLUDE_DIR ?= $(WABT_DIR)/include
WABT_DATA_DIR ?= $(WABT_DIR)/share/wabt
package: uxn-wasm-cli
7z a -tzip uxn-wasm-cli-v$(VERSION)-windows.zip uxn-wasm-cli.exe
else # $(OS)

ifeq (, $(shell which gtar))
TAR := tar
else
# bsd-tar corrupts files on GitHub: https://github.com/actions/virtual-environments/issues/2619
TAR := gtar
endif

UNAME_S=$(shell uname -s)
LLVM_PROFDATA ?= llvm-profdata
UNAME_S=$(shell uname -s)
UNAME_P=$(shell uname -p)
ifeq ($(UNAME_S),Darwin)
ifeq ($(UNAME_P),arm)
PACKAGE_SUFFIX=arm64-macos
else
PACKAGE_SUFFIX=x86_64-macos
endif
WABT_DIR = $(shell brew --prefix)
WABT_INCLUDE_DIR = $(WABT_DIR)/include
WABT_DATA_DIR = $(WABT_DIR)/share/wabt
Expand All @@ -10,38 +35,59 @@ LLVM_COV = $(WABT_DIR)/opt/llvm/bin/llvm-cov
else # $(UNAME_S)
WABT_INCLUDE_DIR ?= /usr/lib/include
WABT_DATA_DIR ?= /usr/share/wabt
PACKAGE_SUFFIX=x86_64-linux
endif # $(UNAME_S)
CPPFLAGS = -I$(WABT_INCLUDE_DIR)

# Add code coverage
CFLAGS := -DVERSION='"$(VERSION)"' -Wall -Wno-unused-function -Wno-unused-value -I$(WABT_INCLUDE_DIR)

ifeq ($(COVERAGE),1)
CFLAGS := $(CFLAGS) -fprofile-instr-generate -fcoverage-mapping
else ifeq ($(DEBUG), 1)
CFLAGS := $(CFLAGS) -g
else
CFLAGS := $(CFLAGS) -O2
endif

package: uxn-wasm-cli
mkdir -p ../../dist
$(TAR) czf ../../dist/uxn-wasm-cli-v$(VERSION)-$(PACKAGE_SUFFIX).tgz uxn-wasm-cli

endif # $(OS)

.PHONY: all
all: uxnwasm opctest.rom
all: uxn-wasm-cli

uxnwasm: uxnwasm_core.o uxnwasm.o wasm-rt-impl.o
uxn-wasm-cli: cli_core.o cli.o wasm-rt-impl.o
$(CC) $(CFLAGS) $(LINKFLAGS) -o $@ $^

uxnwasm_core.c uxnwasm_core.h: ../../build/uxn.wasm
wasm2c $< -o uxnwasm_core.c
cli_core.c cli_core.h: ../../build/uxn.wasm
wasm2c $< -o cli_core.c

wasm-rt-impl.o: $(WABT_DATA_DIR)/wasm2c/wasm-rt-impl.c
$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<

%.o: %.cpp
$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<
$(CC) -c $(CFLAGS) -o $@ $<

../../build/uxn.wasm:
../../build/uxn.wasm: ../../src/uxn.wat
$(MAKE) -C ../.. build/uxn.wasm

opctest.rom: ../tests/opctest.tal
uxnasm $< $@

mandelbrot.rom: ../benchmarks/mandelbrot.tal
uxnasm $< $@

primes32.rom: ../benchmarks/primes32.tal
cd ../benchmarks && uxnasm $(PWD)/$< $(PWD)/$@

test: uxn-wasm-cli opctest.rom
./uxn-wasm-cli opctest.rom | tee opctest.log
@grep failed opctest.log; if [ $$? -eq 0 ]; then echo "Test failed"; exit -1; fi

.PHONY: coverage
coverage: uxnwasm opctest.rom
env LLVM_PROFILE_FILE="opctest.profraw" ./uxnwasm opctest.rom
coverage: opctest.rom
$(MAKE) COVERAGE=1 uxn-wasm-cli
env LLVM_PROFILE_FILE="opctest.profraw" ./uxn-wasm-cli opctest.rom
$(LLVM_PROFDATA) merge -sparse opctest.profraw -o opctest.profdata
$(LLVM_COV) show uxnwasm -instr-profile=opctest.profdata -format=html -output-dir=coverage -sources uxnwasm_core.c
$(LLVM_COV) show uxn-wasm-cli -instr-profile=opctest.profdata -format=html -output-dir=coverage -sources cli_core.c

clean:
-rm -rf *.o uxnwasm_core.* uxnwasm *.rom *.rom.sym *.profraw *.profdata coverage
-rm -rf *.o cli_core.* uxn-wasm-cli *.rom *.rom.sym *.profraw *.profdata coverage *.log
58 changes: 58 additions & 0 deletions src/cli/cli.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdlib.h>

#include "cli_core.h"

typedef struct w2c_system {
wasm_rt_memory_t *mem;
} w2c_system;

void w2c_system_deo(struct w2c_system *sys, u32 port, u32 value) {
// Console
if (port == 0x18) {
fputc(value, stdout);
} else if (port == 0x18) {
fputc(value, stderr);
}
}

u32 w2c_system_dei(struct w2c_system *sys, u32 port) {
return (u32)sys->mem->data[0x10200 + port];
}

int main(int argc, char **argv) {
if (argc < 2) {
printf("missing rom\n");
return 1;
}
FILE *file = fopen(argv[1], "rb");
if (file == NULL) {
printf("error opening file\n");
return 1;
}
fseek(file, 0, SEEK_END);
long filesize = ftell(file);
fseek(file, 0, SEEK_SET);

wasm_rt_init();
w2c_uxn uxn;
w2c_system sys;
wasm2c_uxn_instantiate(&uxn, &sys);
sys.mem = w2c_uxn_memory(&uxn);

// Read ROM
(void)!fread(sys.mem->data + 0x100, filesize, 1, file);
if (ferror(file)) {
printf("error reading file\n");
return 1;
}

// Run
w2c_uxn_eval(&uxn, 0x100);

// Cleanup
wasm2c_uxn_free(&uxn);
wasm_rt_free();
fclose(file);
return 0;
}
50 changes: 0 additions & 50 deletions src/cli/uxnwasm.c

This file was deleted.

0 comments on commit c973477

Please sign in to comment.