Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
aappleby committed Mar 25, 2024
1 parent 87bc5a5 commit 770ee10
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
24 changes: 18 additions & 6 deletions hancho.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def relpath(path1, path2):
"""We don't want to generate paths with '..' in them, so we just try and remove the prefix.
If we can't remove the prefix we'll still have an absolute path."""
if isinstance(path1, list):
return [relpath(p, prefix) for p in path1]
return [relpath(p, path2) for p in path1]
if str(path1) == str(path2):
return Path("")
return Path(str(path1).removeprefix(str(path2) + "/"))
Expand Down Expand Up @@ -163,9 +163,9 @@ async def await_variant(variant):
return variant


def load(hancho_file=None, **kwargs):
def load(hancho_file=None, build_config=None, **kwargs):
"""Module loader entry point for .hancho files."""
return app.load_module(hancho_file, kwargs)
return app.load_module(hancho_file, build_config, kwargs)


class Chdir:
Expand Down Expand Up @@ -675,6 +675,12 @@ def __init__(self):
run_cmd=run_cmd,
swap_ext=swap_ext,
join=join,
abs_source_files = "{join(source_path, source_files)}",
abs_build_files = "{join(build_path, build_files)}",
abs_command_files = "{join(command_path, command_files)}",
rel_source_files = "{relpath(abs_source_files, command_path)}",
rel_build_files = "{relpath(abs_build_files, command_path)}",
rel_command_files = "{relpath(abs_command_files, command_path)}",
base=None,
)
# fmt: on
Expand Down Expand Up @@ -732,7 +738,7 @@ async def async_main(self):
abs_file = global_config.start_path / file
if not abs_file.exists():
raise FileNotFoundError(f"Could not find {abs_file}")
self.load_module(abs_file)
self.load_module(abs_file, None)

# Root module(s) loaded. Run all tasks in the queue until we run out.
while True:
Expand All @@ -759,9 +765,12 @@ async def async_main(self):

return -1 if self.tasks_fail else 0

def load_module(self, mod_filename, kwargs={}):
def load_module(self, mod_filename, build_config=None, kwargs={}):
"""Loads a Hancho module ***while chdir'd into its directory***"""

print(f"mod_filename {mod_filename}")
print(f"build_config {build_config}")

mod_path = abspath(mod_filename)
if not mod_path.exists():
raise FileNotFoundError(f"Could not load module {file}")
Expand All @@ -780,7 +789,10 @@ def load_module(self, mod_filename, kwargs={}):
module = type(sys)(mod_path.stem)
module.__file__ = mod_path
module.__builtins__ = builtins
module.build_config = Config(**kwargs)
if build_config is not None:
module.build_config = build_config.extend(**kwargs)
else:
module.build_config = Config(**kwargs)
self.hancho_mods[module_key] = module

# We must chdir()s into the .hancho file directory before running it so that
Expand Down
20 changes: 7 additions & 13 deletions tutorial/rules.hancho
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
# tutorial/rules.hancho
from hancho import *

build_config.set_defaults(
source_dir = "{relpath(source_path, command_path)}",
build_dir = "{relpath(build_path, command_path)}",
source_dir_files = "{join(source_dir, source_files)}",
build_dir_files = "{join(build_dir, build_files)}",
)
import os

compile = build_config.rule(
desc = "Compile {source_dir_files} -> {build_dir_files}",
command = "g++ -MMD -c {source_dir_files} -o {build_dir_files}",
build_files = "{swap_ext(source_dir_files, '.o')}",
build_deps = "{swap_ext(source_dir_files, '.d')}",
desc = "Compile {rel_source_files} -> {rel_build_files}",
command = "g++ -MMD -c {rel_source_files} -o {rel_build_files}",
build_files = "{swap_ext(rel_source_files, '.o')}",
build_deps = "{swap_ext(rel_source_files, '.d')}",
)

link = build_config.rule(
desc = "Link {source_dir_files} -> {build_dir_files}",
command = "g++ {source_dir_files} -o {build_dir_files}",
desc = "Link {rel_source_files} -> {rel_build_files}",
command = "g++ {rel_source_files} -o {rel_build_files}",
)

def c_binary(source_files, build_files, **kwargs):
Expand Down
16 changes: 10 additions & 6 deletions tutorial/src/src.hancho
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# tutorial/src/src.hancho
from hancho import *
import glob
import os

rules = load("../rules.hancho")
this_path = Path(__file__).parent

if os.name == 'nt':
rules.c_binary(glob.glob("*.cpp"), "app.exe")
else:
rules.c_binary(glob.glob("*.cpp"), "app")
build_config.set_defaults(
command_path = this_path,
source_path = this_path,
build_path = this_path / "build",
)

rules = load("../rules.hancho", build_config)

rules.c_binary(glob.glob("*.cpp"), "app")
8 changes: 6 additions & 2 deletions tutorial/tut4.hancho
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# tutorial/tut3.hancho
from hancho import *

config.build_dir = "build/tut3"
this_path = Path(__file__).parent

load("src/src.hancho")
load(
"src/src.hancho",
command_path = this_path,
build_path = this_path / "build/tut4",
)

0 comments on commit 770ee10

Please sign in to comment.