Skip to content

Commit

Permalink
Comment cleanup, add 'depformat' instead of assuming based on platform
Browse files Browse the repository at this point in the history
  • Loading branch information
aappleby committed Mar 12, 2024
1 parent c88f482 commit 00dcf2a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
2 changes: 2 additions & 0 deletions examples/windows/build.hancho
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# hancho/examples/windows/build.hancho - Builds a trivial Win32 app.
from hancho import *

config.depformat = "msvc"

compile = Rule(
desc = "Compile {files_in} -> {files_out}",
command = "cl.exe /nologo /c {files_in} /sourceDependencies {depfile} /Fo:{files_out} > NUL",
Expand Down
35 changes: 17 additions & 18 deletions hancho.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,21 +156,17 @@ def __getattr__(self, key):
return self.__getitem__(key)

def __repr__(self):
"""Turns this config blob into a JSON doc for debugging"""
"""
Turns this config blob into a JSON doc for debugging
"""

class Encoder(json.JSONEncoder):
"""Turns functions and tasks into stub strings for dumping."""
"""
Types the encoder doesn't understand just get stringified.
"""

def default(self, o):
if callable(o):
return f"callable {o}"
if isinstance(o, asyncio.Task):
return f"asyncio.Task {o}"
if isinstance(o, Path):
return f"Path {o}"
if isinstance(o, asyncio.Semaphore):
return f"asyncio.Semaphore {o}"
return super().default(o)
return f"{o}"

return json.dumps(self, indent=2, cls=Encoder)

Expand All @@ -195,6 +191,7 @@ def extend(self, **kwargs):
dryrun = False,
debug = False,
force = False,
depformat = "gcc",

root_dir = Path.cwd(),
task_dir = "{root_dir}",
Expand Down Expand Up @@ -266,7 +263,6 @@ def main():
"""

# pylint: disable=line-too-long

# fmt: off
parser = argparse.ArgumentParser()
parser.add_argument("filename", default="build.hancho", type=str, nargs="?", help="The name of the .hancho file to build")
Expand Down Expand Up @@ -332,7 +328,6 @@ async def async_main():
load_abs(root_filename)

# Top module(s) loaded. Run all tasks in the queue until we run out.

while True:
pending_tasks = asyncio.all_tasks() - {asyncio.current_task()}
if not pending_tasks:
Expand Down Expand Up @@ -573,7 +568,8 @@ def __init__(self, **kwargs):

async def run_async(self):
"""
Entry point for async task stuff.
Entry point for async task stuff, handles exceptions generated during
task execution.
"""

try:
Expand Down Expand Up @@ -725,7 +721,7 @@ async def run_commands(self):

async def run_command(self, command):
"""
Actually runs a command, either by calling it or running it in a subprocess.
Runs a single command, either by calling it or running it in a subprocess.
"""

# Early exit if this is just a dry run
Expand Down Expand Up @@ -812,7 +808,7 @@ async def needs_rerun(self):
f"Rebuilding {self.files_out} because a manual dependency has changed"
)

# Check GCC-format depfile, if present.
# Check depfile, if present.
if self.depfile:
depfile = Path(await expand_async(self, self.depfile))
abs_depfile = abspath(config.root_dir / depfile)
Expand All @@ -821,13 +817,16 @@ async def needs_rerun(self):
log(f"Found depfile {abs_depfile}")
with open(abs_depfile, encoding="utf-8") as depfile:
deplines = None
if os.name == "nt":
if self.depformat == "msvc":
# MSVC /sourceDependencies json depfile
deplines = json.load(depfile)["Data"]["Includes"]
elif os.name == "posix":
elif self.depformat == "gcc":
# GCC .d depfile
deplines = depfile.read().split()
deplines = [d for d in deplines[1:] if d != "\\"]
else:
raise ValueError(f"Invalid depformat {self.depformat}")

if deplines and max(mtime(f) for f in deplines) >= min_out:
return (
f"Rebuilding {self.files_out} because a dependency in "
Expand Down

0 comments on commit 00dcf2a

Please sign in to comment.