From 770ee103b77cd0c0f9ebf66092de010d2efbb734 Mon Sep 17 00:00:00 2001 From: Austin Appleby Date: Mon, 25 Mar 2024 16:10:43 -0700 Subject: [PATCH] checkpoint --- hancho.py | 24 ++++++++++++++++++------ tutorial/rules.hancho | 20 +++++++------------- tutorial/src/src.hancho | 16 ++++++++++------ tutorial/tut4.hancho | 8 ++++++-- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/hancho.py b/hancho.py index 5673933..56417b2 100755 --- a/hancho.py +++ b/hancho.py @@ -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) + "/")) @@ -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: @@ -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 @@ -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: @@ -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}") @@ -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 diff --git a/tutorial/rules.hancho b/tutorial/rules.hancho index 3a7db45..9354aeb 100644 --- a/tutorial/rules.hancho +++ b/tutorial/rules.hancho @@ -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): diff --git a/tutorial/src/src.hancho b/tutorial/src/src.hancho index 3ee32c4..055f888 100644 --- a/tutorial/src/src.hancho +++ b/tutorial/src/src.hancho @@ -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") diff --git a/tutorial/tut4.hancho b/tutorial/tut4.hancho index 8d0d7a5..82454ed 100644 --- a/tutorial/tut4.hancho +++ b/tutorial/tut4.hancho @@ -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", +)