Skip to content

Commit

Permalink
ok that's enough for tonight
Browse files Browse the repository at this point in the history
  • Loading branch information
aappleby committed Mar 27, 2024
1 parent 224787d commit e9a993d
Show file tree
Hide file tree
Showing 19 changed files with 291 additions and 50 deletions.
8 changes: 5 additions & 3 deletions hancho.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class Encoder(json.JSONEncoder):

def default(self, o):
if isinstance(o, Task):
return f"task {Expander(o.rule)['desc']}"
return f"task {Expander(o.config)['desc']}"
return str(o)

base = self.__dict__["_base"]
Expand Down Expand Up @@ -662,9 +662,11 @@ async def run_command(self, command):
# Print command output if needed
if not self.config.quiet and (self.stdout or self.stderr):
if self.stderr:
log(f"stderr: {self.stderr}", end="")
log("-----stderr-----")
log(self.stderr, end="")
if self.stdout:
log(f"stdout: {self.stdout}", end="")
log("-----stdout-----")
log(self.stdout, end="")

# Task complete, check the task return code
if self.returncode:
Expand Down
15 changes: 11 additions & 4 deletions tutorial/build.hancho
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
# hancho/tutorial/build.hancho - Tests all the tutorials
from hancho import *
import shutil

shutil.rmtree("build", ignore_errors=True)

global_config.jobs = 1

test_build = build_config.rule(
desc = "{color(200, 200, 100)}Testing tutorial build '{rel_source_files}'{color()}",
command = "rm -rf build && python3 ../hancho.py --verbose {rel_source_files} && echo pass > {rel_build_files} && echo",
command = "python3 ../hancho.py --verbose {rel_source_files} && echo pass > {rel_build_files} && echo",
build_files = "{swap_ext(source_files, '.pass')}",
)

test_build("tut00.hancho")
test_build("tut01.hancho")
test_build("tut02.hancho")
test_build("tut03.hancho")
test_build("tut04.hancho")

test_build("tut10.hancho")
test_build("tut11.hancho")
test_build("tut12.hancho")
test_build("tut13.hancho")
test_build("tut14.hancho")
test_build("tut15.hancho")
test_build("tut16.hancho")

test_build("tut20.hancho")

test_build("tut30.hancho")

test_build("tut40.hancho")

test_build("tut50.hancho")
1 change: 1 addition & 0 deletions tutorial/tut00.hancho
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from hancho import Task
from pathlib import Path

Task(
desc = "Compile main.cpp and util.cpp and link app",
command = "g++ src/main.cpp src/util.cpp -o build/tut00/app",
command_path = Path.cwd(),
command_files = [],
Expand Down
17 changes: 10 additions & 7 deletions tutorial/tut01.hancho
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,32 @@ from hancho import Task
from pathlib import Path

main_o = Task(
command = "g++ -MMD -c src/main.cpp -o build/tut01/main.o",
desc = "Compile main.cpp",
command = "g++ -MMD -c src/main.cpp -o build/tut01/src/main.o",
command_path = Path.cwd(),
command_files = [],
source_path = Path.cwd(),
source_files = ["src/main.cpp"],
build_path = Path.cwd(),
build_files = ["build/tut01/main.o"],
build_deps = ["build/tut01/main.d"],
build_files = ["build/tut01/src/main.o"],
build_deps = ["build/tut01/src/main.d"],
)

util_o = Task(
command = "g++ -MMD -c src/util.cpp -o build/tut01/util.o",
desc = "Compile util.cpp",
command = "g++ -MMD -c src/util.cpp -o build/tut01/src/util.o",
command_path = Path.cwd(),
command_files = [],
source_path = Path.cwd(),
source_files = ["src/util.cpp"],
build_path = Path.cwd(),
build_files = ["build/tut01/util.o"],
build_deps = ["build/tut01/util.d"],
build_files = ["build/tut01/src/util.o"],
build_deps = ["build/tut01/src/util.d"],
)

app = Task(
command = "g++ build/tut01/main.o build/tut01/util.o -o build/tut01/app",
desc = "Link main.o and util.o into app",
command = "g++ build/tut01/src/main.o build/tut01/src/util.o -o build/tut01/app",
command_path = Path.cwd(),
command_files = [],
source_path = Path.cwd(),
Expand Down
19 changes: 11 additions & 8 deletions tutorial/tut02.hancho
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ config = Config(
)

main_o = config.task(
command = "g++ -MMD -c src/main.cpp -o build/tut01/main.o",
desc = "Compile main.cpp",
command = "g++ -MMD -c src/main.cpp -o build/tut02/src/main.o",
source_files = ["src/main.cpp"],
build_files = ["build/tut01/main.o"],
build_deps = ["build/tut01/main.d"],
build_files = ["build/tut02/src/main.o"],
build_deps = ["build/tut02/src/main.d"],
)

util_o = config.task(
command = "g++ -MMD -c src/util.cpp -o build/tut01/util.o",
desc = "Compile util.cpp",
command = "g++ -MMD -c src/util.cpp -o build/tut02/src/util.o",
source_files = ["src/util.cpp"],
build_files = ["build/tut01/util.o"],
build_deps = ["build/tut01/util.d"],
build_files = ["build/tut02/src/util.o"],
build_deps = ["build/tut02/src/util.d"],
)

app = config.task(
command = "g++ build/tut01/main.o build/tut01/util.o -o build/tut01/app",
desc = "Link main.o and util.o into app",
command = "g++ build/tut02/src/main.o build/tut02/src/util.o -o build/tut02/app",
source_files = [main_o, util_o],
build_files = ["build/tut01/app"],
build_files = ["build/tut02/app"],
)
8 changes: 5 additions & 3 deletions tutorial/tut03.hancho
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def compile_cpp(config, source, build_dir):
obj = source.replace('.cpp', '.o')
dep = source.replace('.cpp', '.d')
return config.task(
desc = f"Compile {source}",
command = f"g++ -MMD -c {source} -o {build_dir}/{obj}",
source_files = [source],
build_files = [f"{build_dir}/{obj}"],
Expand All @@ -22,11 +23,12 @@ def compile_cpp(config, source, build_dir):
def link_cpp(config, tasks, build_dir, binary):
objs = [task.config.build_files[0] for task in tasks]
return config.task(
desc = f"Link {' and '.join(objs)} into {build_dir}/{binary}",
command = f"g++ {' '.join(objs)} -o {build_dir}/{binary}",
source_files = tasks,
build_files = [f"{build_dir}/{binary}"],
)

main_o = compile_cpp(config, "src/main.cpp", "build/tut01")
util_o = compile_cpp(config, "src/util.cpp", "build/tut01")
app = link_cpp(config, [main_o, util_o], "build/tut01", "app")
main_o = compile_cpp(config, "src/main.cpp", "build/tut03")
util_o = compile_cpp(config, "src/util.cpp", "build/tut03")
app = link_cpp(config, [main_o, util_o], "build/tut03", "app")
18 changes: 18 additions & 0 deletions tutorial/tut04.hancho
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# tutorial/tut04.hancho
from hancho import Config
from pathlib import Path

import sys
sys.path.append(".")
import tut04_rules

config = Config(
command_path = Path.cwd(),
command_files = [],
source_path = Path.cwd(),
build_path = Path.cwd(),
)

main_o = tut04_rules.compile_cpp(config, "src/main.cpp", "build/tut04")
util_o = tut04_rules.compile_cpp(config, "src/util.cpp", "build/tut04")
app = tut04_rules.link_cpp(config, [main_o, util_o], "build/tut04", "app")
19 changes: 19 additions & 0 deletions tutorial/tut04_rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def compile_cpp(config, source, build_dir):
obj = source.replace('.cpp', '.o')
dep = source.replace('.cpp', '.d')
return config.task(
desc = f"Compile {source}",
command = f"g++ -MMD -c {source} -o {build_dir}/{obj}",
source_files = [source],
build_files = [f"{build_dir}/{obj}"],
build_deps = [f"{build_dir}/{dep}"],
)

def link_cpp(config, tasks, build_dir, binary):
objs = [task.config.build_files[0] for task in tasks]
return config.task(
desc = f"Link {' and '.join(objs)} into {build_dir}/{binary}",
command = f"g++ {' '.join(objs)} -o {build_dir}/{binary}",
source_files = tasks,
build_files = [f"{build_dir}/{binary}"],
)
46 changes: 29 additions & 17 deletions tutorial/tut10.hancho
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
# tutorial/tut1.hancho
from hancho import *
# tutorial/tut10.hancho - branches off from tut02.hancho
from hancho import Config
from pathlib import Path

compile = Rule(
desc = "Compile {source_files} -> {build_files}",
command = "g++ -c {source_files} -o {build_files}",
command_path = Path.cwd(),
source_path = Path.cwd(),
build_path = Path.cwd(),
config = Config(
command_path = Path.cwd(),
command_files = [],
source_path = Path.cwd(),
build_path = Path.cwd(),
build_dir = "build/tut10",
)

link = Rule(
desc = "Link {source_files} -> {build_files}",
command = "g++ {source_files} -o {build_files}",
command_path = Path.cwd(),
source_path = Path.cwd(),
build_path = Path.cwd(),
main_o = config.task(
desc = "Compile {source_files}",
command = "g++ -MMD -c {source_files} -o {build_files}",
source_files = ["src/main.cpp"],
build_files = ["{build_dir}/src/main.o"],
build_deps = ["{build_dir}/src/main.d"],
)

main_o = compile("src/main.cpp", "build/tut1/src/main.o")
util_o = compile("src/util.cpp", "build/tut1/src/util.o")
link([main_o, util_o], "build/tut1/app")
util_o = config.task(
desc = "Compile {source_files}",
command = "g++ -MMD -c {source_files} -o {build_files}",
source_files = ["src/util.cpp"],
build_files = ["{build_dir}/src/util.o"],
build_deps = ["{build_dir}/src/util.d"],
)

app = config.task(
desc = "Link {source_files} into {build_files}",
command = "g++ {joinpath(build_dir, source_files)} -o {build_files}",
source_files = [main_o, util_o],
build_files = ["{build_dir}/app"],
)
38 changes: 38 additions & 0 deletions tutorial/tut11.hancho
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# tutorial/tut11.hancho
from hancho import Config
from pathlib import Path

config = Config(
command_path = Path.cwd(),
command_files = [],
source_path = Path.cwd(),
build_path = Path.cwd(),
build_dir = "build/tut11",
)

compile = config.extend(
desc = "Compile {source_files}",
command = "g++ -MMD -c {source_files} -o {build_files}",
)

main_o = compile.task(
source_files = ["src/main.cpp"],
build_files = ["{build_dir}/src/main.o"],
build_deps = ["{build_dir}/src/main.d"],
)

util_o = compile.task(
source_files = ["src/util.cpp"],
build_files = ["{build_dir}/src/util.o"],
build_deps = ["{build_dir}/src/util.d"],
)

link = config.extend(
desc = "Link {source_files} into {build_files}",
command = "g++ {joinpath(build_dir, source_files)} -o {build_files}",
)

app = link.task(
source_files = [main_o, util_o],
build_files = ["{build_dir}/app"],
)
36 changes: 36 additions & 0 deletions tutorial/tut12.hancho
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# tutorial/tut12.hancho
from hancho import Config
from pathlib import Path

config = Config(
command_path = Path.cwd(),
command_files = [],
source_path = Path.cwd(),
build_path = Path.cwd(),
build_dir = "build/tut12",
)

compile = config.extend(
desc = "Compile {source_files}",
command = "g++ -MMD -c {source_files} -o {build_files}",
build_files = "{joinpath(build_dir, swap_ext(source_files, '.o'))}",
build_deps = "{joinpath(build_dir, swap_ext(source_files, '.d'))}",
)

main_o = compile.task(
source_files = ["src/main.cpp"],
)

util_o = compile.task(
source_files = ["src/util.cpp"],
)

link = config.extend(
desc = "Link {source_files} into {build_files}",
command = "g++ {joinpath(build_dir, source_files)} -o {build_files}",
)

app = link.task(
source_files = [main_o, util_o],
build_files = ["{build_dir}/app"],
)
27 changes: 27 additions & 0 deletions tutorial/tut13.hancho
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# tutorial/tut13.hancho
from hancho import Config
from pathlib import Path

config = Config(
command_path = Path.cwd(),
command_files = [],
source_path = Path.cwd(),
build_path = Path.cwd(),
build_dir = "build/tut13",
)

compile = config.rule(
desc = "Compile {source_files}",
command = "g++ -MMD -c {source_files} -o {build_files}",
build_files = "{joinpath(build_dir, swap_ext(source_files, '.o'))}",
build_deps = "{joinpath(build_dir, swap_ext(source_files, '.d'))}",
)

link = config.rule(
desc = "Link {source_files} into {build_files}",
command = "g++ {joinpath(build_dir, source_files)} -o {build_files}",
)

main_o = compile("src/main.cpp")
util_o = compile("src/util.cpp")
app = link([main_o, util_o], "{build_dir}/app")
27 changes: 27 additions & 0 deletions tutorial/tut14.hancho
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# tutorial/tut14.hancho
from hancho import Config
from pathlib import Path

config = Config(
command_path = Path.cwd(),
command_files = [],
source_path = Path.cwd(),
build_path = Path.cwd(),
build_dir = "build/tut14",
)

compile = config.rule(
desc = "Compile {rel_source_files}",
command = "g++ -MMD -c {rel_source_files} -o {build_files}",
build_files = "{joinpath(build_dir, swap_ext(rel_source_files, '.o'))}",
build_deps = "{joinpath(build_dir, swap_ext(rel_source_files, '.d'))}",
)

link = config.rule(
desc = "Link {rel_source_files} into {build_files}",
command = "g++ {rel_source_files} -o {build_files}",
)

main_o = compile("src/main.cpp")
util_o = compile("src/util.cpp")
app = link([main_o, util_o], "{build_dir}/app")
Loading

0 comments on commit e9a993d

Please sign in to comment.