Skip to content

Commit

Permalink
Add a CLI for creating a debug build (#7)
Browse files Browse the repository at this point in the history
This is useful for hooking up as a VSCode launch configuration alongside
an LLDB debugger.
  • Loading branch information
gatesn authored Aug 31, 2023
1 parent 9fa7c58 commit 572f048
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 18 deletions.
8 changes: 4 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"type": "lldb",
"request": "launch",
"name": "Debug Example",
"preLaunchTask": "zig-test-bin-example",
"program": "${workspaceFolder}/example/zig-out/bin/test.bin",
"args": [],
"cwd": "${workspaceFolder}/example",
"initCommands": [
"shell poetry run pydust debug ${file}"
],
"program": "zig-out/debug.bin",
},
]
}
18 changes: 8 additions & 10 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
{
"label": "zig-test-bin",
"command": "zig",
"args": ["build", "-Dtest-debug-root=${file}"],
"options": { "cwd": "${workspaceFolder}" },
"type": "shell"
},
{
"label": "zig-test-bin-example",
"command": "zig",
"args": ["build", "-Dtest-debug-root=${file}"],
"options": { "cwd": "${workspaceFolder}/example" },
"args": [
"build",
"-Dtest-debug-root=${file}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"type": "shell"
},
]
}
}
44 changes: 44 additions & 0 deletions pydust/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import argparse
import os
import shutil
import subprocess

from pydust import config, zigexe

parser = argparse.ArgumentParser()
sub = parser.add_subparsers(dest="command", required=True)

debug = sub.add_parser("debug", help="Compile a Zig file with debug symbols. Useful for running from an IDE.")
debug.add_argument("entrypoint")


def main():
args = parser.parse_args()

if args.command == "debug":
debug(args)


def debug(args):
"""Given an entrypoint file, compile it for test debugging. Placing it in a well-known location."""
entrypoint = args.entrypoint

filename = os.path.basename(entrypoint)
name, _ext = os.path.splitext(filename)

ext_module = config.ExtModule(
name=name,
root=entrypoint,
# Not sure how else we could guess this?
limited_api=False,
)

with zigexe.build_argv("test", ext_module, optimize="Debug") as argv:
subprocess.run(argv, check=True)

os.makedirs("zig-out/", exist_ok=True)
shutil.move(ext_module.test_bin, "zig-out/debug.bin")


if __name__ == "__main__":
main()
3 changes: 2 additions & 1 deletion pydust/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def build():
pydust_conf = config.load()

for ext_module in pydust_conf.ext_modules:
with zigexe.build_argv("build-lib", ext_module) as argv:
# TODO(ngates): figure out if we're running as part of a dev install, or an sdist install?
with zigexe.build_argv("build-lib", ext_module, optimize="ReleaseSafe") as argv:
retcode = subprocess.call(argv)
if retcode != 0:
raise ValueError(f"Failed to compile Zig: {' '.join(shlex.quote(arg) for arg in argv)}")
5 changes: 5 additions & 0 deletions pydust/src/types/str.zig
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ pub const PyString = extern struct {
return self.appendObj(other.obj);
}

pub fn asOwnedSlice(self: PyString) ![:0]const u8 {
defer self.decref();
return try self.asSlice();
}

pub fn asSlice(self: PyString) ![:0]const u8 {
var size: i64 = 0;
const buffer: [*]const u8 = ffi.PyUnicode_AsUTF8AndSize(self.obj.py, &size) orelse return PyError.Propagate;
Expand Down
4 changes: 2 additions & 2 deletions pydust/zigexe.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@


@contextlib.contextmanager
def build_argv(command: Command, ext_module: config.ExtModule):
def build_argv(command: Command, ext_module: config.ExtModule, optimize: str = "Debug"):
"""The main entry point from Poetry's build script."""
argv = [sys.executable, "-m", "ziglang", command]
argv = [sys.executable, "-m", "ziglang", command, "-O", optimize]
if command == "build-lib":
argv += ["-dynamic"]
# TODO(ngates): create the correct .so filename based on arch
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description = ""
authors = ["Nicholas Gates <[email protected]>"]
license = "Apache 2.0"
readme = "README.md"
packages = [{include = "pydust"}]
packages = [{ include = "pydust" }]
include = ["src"]
exclude = ["example"]

Expand All @@ -19,6 +19,9 @@ pytest = "^7.4.0"
ruff = "^0.0.286"
black = "^23.7.0"

[tool.poetry.scripts]
pydust = "pydust.__main__:main"

[tool.black]
line-length = 120

Expand Down

0 comments on commit 572f048

Please sign in to comment.