Skip to content
This repository was archived by the owner on Nov 11, 2021. It is now read-only.
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions porth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import subprocess
import shlex
import platform
from os import path
from typing import *
from enum import Enum, auto
Expand Down Expand Up @@ -955,14 +956,23 @@ def usage(compiler_name: str):
basename = basename[:-len(porth_ext)]
basedir = path.dirname(program_path)
basepath = path.join(basedir, basename)

print(f"{basepath=} {basedir=} {basename=}")
print("[INFO] Generating %s" % (basepath + ".asm"))
program = compile_file_to_program(program_path, include_paths);
generate_nasm_linux_x86_64(program, basepath + ".asm")
cmd_call_echoed(["nasm", "-felf64", basepath + ".asm"])
cmd_call_echoed(["ld", "-o", basepath, basepath + ".o"])
if platform.system() == "Windows": # Pseudo Windows 10/11 support, requiring a wsl(version irelevant) installation with nasm and ld (gnu binutils) install on the default Distro
win_basepath = basepath.replace("\\", "/")
cmd_call_echoed(["wsl", "nasm", "-felf64", win_basepath + ".asm"])
cmd_call_echoed(["wsl", "ld", "-o", win_basepath, win_basepath + ".o"])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to prefix the commands with wsl if you are running everything from within the WSL console?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trick I am doing, is that you don't have to run it from wsl, since wsl is a completely contained linux distro, It was able to run porth before this change fine, but for ease of devolopment and tool uage on windows (configuriung wsl for IDE's or toolchains it pretty annoying) It now works if you run it from anywhere, without havin to worry to specifically run it through wsl.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now it autodectects if not run through wsl (since wsl will return "Linux" for platform.system()) and will then switch to run all the external commands through wsl, while still running on the windows host itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand correctly that if you simply run everything from within WSL no changes in the code are required at all?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you understand me correctly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But as I said, configuring tools and other systems to run through wsl is often very hard, annoying or straight up impossible.

else:
cmd_call_echoed(["nasm", "-felf64", basepath + ".asm"])
cmd_call_echoed(["ld", "-o", basepath, basepath + ".o"])
if run:
exit(cmd_call_echoed([basepath] + argv))
if platform.system() == "Windows": # Pseudo Windows 10/11 support, requiring a wsl(version irelevant) installation with nasm and ld (gnu binutils) install on the default Distro
win_basepath = basepath.replace("\\", "/")
exit(cmd_call_echoed(["wsl", win_basepath] + argv))
else:
exit(cmd_call_echoed([basepath] + argv))
elif subcommand == "help":
usage(compiler_name)
exit(0)
Expand Down
20 changes: 15 additions & 5 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import subprocess
import shlex
import platform

def cmd_run_echoed(cmd, **kwargs):
print("[CMD] %s" % " ".join(map(shlex.quote, cmd)))
Expand All @@ -26,8 +27,10 @@ def test(folder):
expected_output = None
with open(txt_path, "rb") as f:
expected_output = f.read()

sim_output = cmd_run_echoed(["./porth.py", "sim", entry.path], capture_output=True).stdout
if platform.system() == "Windows":
sim_output = cmd_run_echoed(["wsl", "python3", "./porth.py", "sim", entry.path], capture_output=True).stdout.replace(b"\r\n", b"\n") # TODO: remove this dirty replace hack, it is needed since windows converts \n to \r\n
else:
sim_output = cmd_run_echoed(["./porth.py", "sim", entry.path], capture_output=True).stdout
if sim_output != expected_output:
sim_failed += 1
print("[ERROR] Unexpected simulation output")
Expand All @@ -37,8 +40,12 @@ def test(folder):
print(" %s" % sim_output)
# exit(1)

cmd_run_echoed(["./porth.py", "com", entry.path])
com_output = cmd_run_echoed([entry.path[:-len(porth_ext)]], capture_output=True).stdout
if platform.system() == "Windows":
cmd_run_echoed(["python", "./porth.py", "com", entry.path])
com_output = cmd_run_echoed(["wsl", entry.path[:-len(porth_ext)]], capture_output=True).stdout.replace(b"\r\n", b"\n") # TODO: remove this dirty replace hack, it is needed since windows converts \n to \r\n
else:
cmd_run_echoed(["./porth.py", "com", entry.path])
com_output = cmd_run_echoed([entry.path[:-len(porth_ext)]], capture_output=True).stdout
if com_output != expected_output:
com_failed += 1
print("[ERROR] Unexpected compilation output")
Expand All @@ -56,7 +63,10 @@ def record(folder):
for entry in os.scandir(folder):
porth_ext = '.porth'
if entry.is_file() and entry.path.endswith(porth_ext):
sim_output = cmd_run_echoed(["./porth.py", "sim", entry.path], capture_output=True).stdout
if platform.system() == "Windows":
sim_output = cmd_run_echoed(["python", "./porth.py", "sim", entry.path], capture_output=True).stdout.replace(b"\r\n", b"\n") # TODO: remove this dirty replace hack, it is needed since windows converts \n to \r\n
else:
sim_output = cmd_run_echoed(["./porth.py", "sim", entry.path], capture_output=True).stdout
txt_path = entry.path[:-len(porth_ext)] + ".txt"
print("[INFO] Saving output to %s" % txt_path)
with open(txt_path, "wb") as txt_file:
Expand Down