-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtut00.hancho
169 lines (129 loc) · 4.8 KB
/
tut00.hancho
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
####################################################################################################
hancho(
command = "g++ src/main.cpp src/util.cpp -o build/app",
)
####################################################################################################
hancho(
command = "g++ {in_src} -o {out_bin}",
in_src = ["src/main.cpp", "src/util.cpp"],
out_bin = "app",
)
####################################################################################################
main_o = hancho(
command = "g++ -c {in_src} -o {out_obj}",
in_src = "src/main.cpp",
out_obj = "main.o",
)
util_o = hancho(
command = "g++ -c {in_src} -o {out_obj}",
in_src = "src/util.cpp",
out_obj = "util.o",
)
hancho(
command = "g++ {in_objs} -o {out_bin}",
in_objs = [main_o, util_o],
out_bin = "app",
)
####################################################################################################
main_o = hancho(
command = "g++ -MMD -c {in_src} -o {out_obj}",
in_src = "src/main.cpp",
out_obj = "main.o",
in_depfile = "main.d",
)
util_o = hancho(
command = "g++ -MMD -c {in_src} -o {out_obj}",
in_src = "src/util.cpp",
out_obj = "util.o",
in_depfile = "util.d",
)
hancho(
command = "g++ {in_objs} -o {out_bin}",
in_objs = [main_o, util_o],
out_bin = "app",
)
####################################################################################################
compile_cpp = hancho.Context(
desc = "Compile {in_src} -> {out_obj}",
command = "g++ -MMD -c {in_src} -o {out_obj}",
out_obj = "{ext(in_src, '.o')}",
in_depfile = "{ext(out_obj, '.d')}",
)
link_cpp = hancho.Context(
desc = "Link {in_objs} into {out_bin}",
command = "g++ {in_objs} -o {out_bin}",
)
main_o = hancho(compile_cpp, in_src = "src/main.cpp")
util_o = hancho(compile_cpp, in_src = "src/util.cpp")
app = hancho(link_cpp, in_objs = [main_o, util_o], out_bin = "app")
####################################################################################################
rules = hancho.load("rules.hancho")
main_o = hancho(rules.compile_cpp, in_src = "src/main.cpp")
util_o = hancho(rules.compile_cpp, in_src = "src/util.cpp")
app = hancho(rules.link_cpp, in_objs = [main_o, util_o], out_bin = "app")
#----------------------------------------
compile_cpp = hancho.Context(
desc = "Compile {in_src} -> {out_obj}",
command = "g++ -MMD -c {in_src} -o {out_obj}",
out_obj = "{ext(in_src, '.o')}",
in_depfile = "{ext(out_obj, '.d')}",
)
link_cpp = hancho.Context(
desc = "Link {in_objs} into {out_bin}",
command = "g++ {in_objs} -o {out_bin}",
)
####################################################################################################
rules = hancho.load("rules.hancho")
app = hancho(rules.c_binary, hancho.glob("src/*.cpp"), "app")
#----------------------------------------
compile_cpp = hancho.Context(
desc = "Compile {rel(in_src)} -> {rel(out_obj)}",
command = "g++ -MMD -c {rel(in_src)} -o {rel(out_obj)}",
out_obj = "{ext(in_src, '.o')}",
in_depfile = "{ext(out_obj, '.d')}",
)
link_cpp = hancho.Context(
desc = "Link {rel(in_objs)} -> {rel(out_bin)}",
command = "g++ {rel(in_objs)} -o {rel(out_bin)}",
)
def c_binary(hancho, *, in_srcs, out_bin, **kwargs):
objs = [hancho(compile_cpp, in_src = file, **kwargs) for file in in_srcs]
return hancho(link_cpp, in_objs = objs, out_bin = out_bin, **kwargs)
####################################################################################################
# Async/await and custom commands
import asyncio
# Synchronous functions can be used in place of command strings.
def sync_callback(task):
for abs_file in task.out_txts:
with open(abs_file, 'w', encoding="utf-8") as file:
file.write("hello world")
return task.out_txts
fast_task = hancho.Task(
command = sync_callback,
in_src = ["src/main.cpp"],
out_txts = ["fast1.txt", "fast2.txt", "fast3.txt"],
repo_name = "tut50",
)
# Asynchronous functions can also be used in place of command strings.
async def async_callback(task):
for abs_file in task.out_txts:
with open(abs_file, 'w', encoding="utf-8") as file:
file.write("hello world")
await asyncio.sleep(0.1)
return task.out_txts
slow_task = hancho.Task(
command = async_callback,
in_src = ["src/main.cpp"],
out_txts = ["slow1.txt", "slow2.txt", "slow3.txt"],
repo_name = "tut50",
)
# Promises that resolve to filenames can be used in place of actual filenames in rules.
async def slow_filename_promise():
await asyncio.sleep(0.1)
return ["src/main.cpp"]
echo_task = hancho.Task(
command = "echo {rel(in_src)} > {rel(out_txt)}",
in_src = [slow_filename_promise()],
out_txt = ["promise1.txt"],
repo_name = "tut50",
)