Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gvasm v3 #44

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
bin2/*
mgba/*
tgt/*
tgt2/*

# random crap that tends to appear over time
Thumbs.db
Expand Down
146 changes: 146 additions & 0 deletions build
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!/bin/bash -e
#
# Usage:
# ./build [zip] [win] [linux] [osx] [osxarm]
#
# No arguments -- build everything and zip it up
# Otherwise, each argument enables a particular action
#
# ./build clean
#
# Deletes the tgt directory
#

ROOT=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd "$ROOT"
SRC=src2
TGT=tgt2
TGT_SRC=tgt2/src2

ZIP=false
WIN=false
LINUX=false
OSX=false
OSXARM=false

if [[ $# -eq 0 ]]; then
ZIP=true
WIN=true
LINUX=true
OSX=true
OSXARM=true
else
for arg in "$@"; do
if [[ "$arg" == clean ]]; then
rm -rf "$TGT"
exit 0
elif [[ "$arg" == zip ]]; then
ZIP=true
elif [[ "$arg" == win ]]; then
WIN=true
elif [[ "$arg" == linux ]]; then
LINUX=true
elif [[ "$arg" == osx ]]; then
OSX=true
elif [[ "$arg" == osxarm ]]; then
OSXARM=true
else
echo "Unknown argument: $arg" >&2
exit 1
fi
done
fi

echo "building tgt2/src2"
mkdir -p "$TGT_SRC"
for file in $(find "$SRC" -type f -name '*.c' -or -name '*.h' -or -name '*.c.js' -or -name '*.h.js'); do
out="${file/src2/tgt2/src2}"
if [[ $file == *.js ]]; then
out="${out%.js}"
cmd=node
else
cmd=cat
fi
if [[ -e "$out" && "$out" -nt "$file" ]]; then
echo " skipping $(basename "$file")"
else
echo " $cmd $(basename "$file")"
"$cmd" "$file" > "$out"
fi
done

compile() {
local TARGET="$1"
local EXE="$2"
local TGT_OBJ="$TGT"/obj/"$TARGET"
local TGT_OUT="$TGT"/out/"$TARGET"
mkdir -p "$TGT_OBJ"
mkdir -p "$TGT_OUT"

echo "target $TARGET"

local OBJ_FILES=()
for file in $(find "$TGT_SRC" -type f -name '*.c'); do
local filed="${file%.c}.d"
local obj="${file/src2/obj/$TARGET}"
obj="${obj%.c}.o"
OBJ_FILES+=("$obj")
local buildo=true
if [[ -e "$filed" ]]; then
buildo=false
for dep in $(node - -- "$filed" <<'EOF'
const filed = process.argv[3];
const fs = require('fs');
const data = fs.readFileSync(filed, 'utf8');
console.log(data
.split('\n')
.map(line => line
.replace(/^target: /, '')
.replace(/^\s+/, '')
.replace(/\\$/, '')
)
.join('\n')
);
EOF
); do
if [[ -e "$obj" && "$obj" -nt "$dep" ]]; then
:
else
buildo=true
echo " $(basename "$obj") dependency change: $(basename "$dep")"
break
fi
done
fi
if [[ "$buildo" == true ]]; then
echo " compiling $(basename "$file")"
zig cc -MM -MT target -MF "${file%.c}.d" "$file"
zig cc -target "$TARGET" -o "$obj" -c "$file" -lm
fi
done
zig cc -o "$TGT_OUT"/"$EXE" -target "$TARGET" -lm "${OBJ_FILES[@]}"
rm -f "$TGT_OUT"/gvasm.pdb

if [[ "$ZIP" == true ]]; then
local zip_file="$ROOT"/tgt2/gvasm-"$TARGET".zip
rm -f "$zip_file"
pushd "$TGT_OUT"
zip -r "$zip_file" *
popd
fi

echo " done"
}

if [[ "$WIN" == true ]]; then
compile x86_64-windows gvasm.exe
fi
if [[ "$OSX" == true ]]; then
compile x86_64-macos gvasm
fi
if [[ "$LINUX" == true ]]; then
compile x86_64-linux gvasm
fi
if [[ "$OSXARM" == true ]]; then
compile aarch64-macos gvasm
fi
8 changes: 8 additions & 0 deletions runtests
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash -e

ROOT=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd "$ROOT"

while IFS= read file; do
./gvasm.ts runtest "$file"
done < <(find tests -type f -name '*.txt' | sort)
8 changes: 8 additions & 0 deletions runtests2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash -e

ROOT=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd "$ROOT"

while IFS= read file; do
./tgt2/out/x86_64-macos/gvasm runtest "$file"
done < <(find tests -type f -name '*.txt' | sort)
44 changes: 44 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IMakeArgs, make } from './make.ts';
import { IRunArgs, run } from './run.ts';
import { dis, IDisArgs } from './dis.ts';
import { IItestArgs, itest } from './itest.ts';
import { IRuntestArgs, runtest } from './runtest.ts';
import { argParse, Path } from './deps.ts';
import { ILexKeyValue, lexKeyValue } from './lexer.ts';

Expand All @@ -35,6 +36,7 @@ Command Summary:
run Run a .gvasm file in debug mode
dis Disassemble a .gba file into a source
itest Run internal tests to verify correct behavior
runtest Run external test .txt file

For more help, try:
gvasm <command> --help`);
Expand Down Expand Up @@ -371,6 +373,42 @@ function parseItestArgs(args: string[]): number | IItestArgs {
return { filters: a._.map((a) => a.toString()) };
}

function printRuntestHelp() {
console.log(`gvasm runtest <file>

<file> The test .txt file`);
}

function parseRuntestArgs(args: string[]): number | IRuntestArgs {
let badArgs = false;
const a = argParse(args, {
stopEarly: true,
boolean: ['help'],
alias: { h: 'help' },
unknown: (_arg: string, key?: string) => {
if (key) {
console.error(`Unknown argument: -${key}`);
badArgs = true;
return false;
}
return true;
},
});
if (badArgs) {
return 1;
}
if (a.help) {
printRuntestHelp();
return 0;
}
const file = a._.map((a) => a.toString())[0];
if (!file) {
printRuntestHelp();
return 1;
}
return { file };
}

export async function main(args: string[]): Promise<number> {
if (args.length <= 0 || args[0] === '-h' || args[0] === '--help') {
printVersion();
Expand Down Expand Up @@ -410,6 +448,12 @@ export async function main(args: string[]): Promise<number> {
return itestArgs;
}
return await itest(itestArgs);
} else if (args[0] === 'runtest') {
const runtestArgs = parseRuntestArgs(args.slice(1));
if (typeof runtestArgs === 'number') {
return runtestArgs;
}
return await runtest(runtestArgs);
}
console.error(`Unknown command: ${args[0]}`);
return 1;
Expand Down
Loading