-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbase_rules.hancho
136 lines (110 loc) · 4.13 KB
/
base_rules.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
import os
####################################################################################################
# C++
default_toolchain = hancho.Config(
arch="x86-64",
compiler="x86_64-linux-gnu-g++",
linker="x86_64-linux-gnu-g++",
)
# ----------------------------------------
check_cpp = hancho.Config(
desc="Checking C++ syntax of {rel(in_src)}",
command="{toolchain.compiler} {flags} {joined_warnings} {joined_defines} {joined_includes} -c {in_src} && touch {out_ok}",
toolchain=default_toolchain,
flags=["-fsyntax-only", "-std={cpp_std}"],
warnings = None,
defines = None,
includes = None,
out_ok="{in_src}.ok",
cpp_std="gnu++2a",
joined_warnings="{join_prefix('-W', warnings)}",
joined_defines="{join_prefix('-D', defines)}",
joined_includes="{join_prefix('-I', includes)}",
)
# ----------------------------------------
compile_cpp = hancho.Config(
desc="Compiling C++ {rel(in_src)} -> {rel(out_obj)} ({build_tag})",
command="{toolchain.compiler} {flags} {joined_warnings} {joined_defines} {joined_includes} -c {in_src} -o {out_obj}",
toolchain=default_toolchain,
flags=[
"-g",
"-O0",
"-MMD",
"-march={toolchain.arch}",
"-std={cpp_std}",
"-fdiagnostics-color=always",
],
cpp_std="gnu++2a",
warnings = None,
defines = None,
includes = None,
out_obj="{swap_ext(in_src, '.o')}",
# Note - this swaps out_obj and not in_src in case the user redirects out_obj
in_depfile="{swap_ext(out_obj, '.d')}",
joined_warnings="{join_prefix('-W', warnings)}",
joined_defines="{join_prefix('-D', defines)}",
joined_includes="{join_prefix('-I', includes)}",
)
if os.name == "nt":
compile_cpp.command = "cl.exe /c {in_src} /sourceDependencies {in_depfile} /Fo:{out_obj}"
compile_cpp.depformat = "msvc"
link_cpp_lib = hancho.Config(
desc="Bundling C++ lib {out_lib}",
command="ar rcs {out_lib} {in_objs}",
)
link_cpp_bin = hancho.Config(
desc = "Linking C++ bin {rel(out_bin)}",
toolchain = default_toolchain,
command = "{toolchain.linker} {flags} -Wl,--start-group {in_objs} {in_libs} {sys_libs} -Wl,--end-group -o {out_bin}",
in_objs = None,
in_libs = None,
flags = None,
sys_libs = None,
)
def cpp_lib(hancho, *, in_srcs=None, in_objs=None, in_libs=None, out_lib, **kwargs):
in_objs = hancho.flatten(in_objs)
for file in hancho.flatten(in_srcs):
obj = hancho(compile_cpp, in_src=file, **kwargs)
in_objs.append(obj)
return hancho(link_cpp_lib, in_objs=[in_objs, in_libs], out_lib=out_lib, **kwargs)
def cpp_bin(hancho, *, in_srcs=None, in_objs=None, in_libs=None, out_bin, **kwargs):
in_objs = hancho.flatten(in_objs)
for file in hancho.flatten(in_srcs):
obj = hancho(compile_cpp, in_src=file, **kwargs)
in_objs.append(obj)
return hancho(
link_cpp_bin,
in_objs=[in_objs, in_libs],
out_bin=out_bin,
**kwargs
)
####################################################################################################
# Makefiles
def make(hancho, *, in_makefile, **kwargs):
cmd = hancho.Config(
desc="Run makefile {rel(in_makefile)}",
command="make -C {make_dir} -f {make_file} {flags}", # > /dev/null
make_dir="{path.dirname(in_makefile)}",
make_file="{path.basename(in_makefile)}",
flags="--quiet",
)
return hancho(cmd, in_makefile=in_makefile, **kwargs)
####################################################################################################
# Tests
run_test = hancho.Config(
desc="Running test {rel(in_test)}",
command="{in_test} {args} && touch {out_pass}",
task_dir="{test_dir}",
test_dir="{mod_dir}",
args="",
out_pass="{in_test}.pass",
)
def cpp_test(hancho, *, in_srcs=None, in_objs=None, in_libs=None, out_bin, **kwargs):
objs = [hancho(compile_cpp, in_src=src, **kwargs) for src in hancho.flatten(in_srcs)]
test_bin = hancho(
link_cpp_bin,
in_objs=[objs, in_objs, in_libs],
out_bin=out_bin,
**kwargs
)
return hancho(run_test, in_test=test_bin, **kwargs)