Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
aappleby committed May 8, 2024
1 parent cb6bd08 commit c6edce6
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 135 deletions.
27 changes: 17 additions & 10 deletions build.hancho
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
# matcheroni/build.hancho

hancho.build_tag = "debug"
import hancho

hancho.rules = hancho.load("config/rules.hancho")
hancho.c_lexer = hancho.load("examples/c_lexer/c_lexer.hancho")
hancho.c_parser = hancho.load("examples/c_parser/c_parser.hancho")
imports.config = hancho.Config(build_tag = "debug")
imports.rules = hancho.load("config/rules.hancho", imports)
imports.c_lexer = hancho.load("examples/c_lexer/c_lexer.hancho", imports)
imports.c_parser = hancho.load("examples/c_parser/c_parser.hancho", imports)

hancho.rules.c_library("examples/ini/ini_parser.cpp", "examples/ini/ini_parser.a")
exports.c_lexer = imports.c_lexer
exports.c_parser = imports.c_parser
exports.json = hancho.load("examples/json/json.hancho", imports)
exports.regex = hancho.load("examples/regex/regex.hancho", imports)
exports.toml = hancho.load("examples/toml/toml.hancho", imports)
exports.tests = hancho.load("tests/tests.hancho", imports)
exports.tutorial = hancho.load("examples/tutorial/tutorial.hancho", imports)

hancho.load("examples/json/json.hancho")
hancho.load("examples/regex/regex.hancho")
hancho.load("examples/toml/toml.hancho")
hancho.load("tests/tests.hancho")
hancho.load("examples/tutorial/tutorial.hancho")
exports.ini = imports.rules.c_library(
imports.config,
in_srcs = "examples/ini/ini_parser.cpp",
out_lib = "examples/ini/ini_parser.a",
)
112 changes: 58 additions & 54 deletions config/rules.hancho
Original file line number Diff line number Diff line change
@@ -1,69 +1,73 @@
# matcheroni/config/rules.hancho

#-------------------------------------------------------------------------------
import hancho

compile_cpp = hancho.command(
desc = "Compiling {rel_source_files} -> {rel_build_files} ({build_tag})",
command = "g++ {cpp_std} {gcc_opt} {warnings} {includes} {defines} -c {rel_source_files} -o {rel_build_files}",
cpp_std = "-std=c++20",
gcc_opt = "{'-O3' if build_tag == 'release' else '-g -O0'} -MMD",
warnings = "-Wall -Werror -Wno-unused-variable -Wno-unused-local-typedefs -Wno-unused-but-set-variable",
includes = "-I{repo_path}",
defines = "",
build_files = "{swap_ext(source_files, '.o')}",
build_deps = "{swap_ext(source_files, '.d')}",
)
#-------------------------------------------------------------------------------

link_c_lib = hancho.command(
desc = "Bundling {rel_build_files}",
command = "ar rcs {rel_build_files} {rel_source_files}",
)
def link_c_lib(in_objs, out_lib, *args, **kwargs):
config = hancho.Config(*args, **kwargs)
command = hancho.Config(
desc = "Bundling {rel(out_lib)}",
command = "ar rcs {rel(out_lib)} {rel(in_objs)}",
)
return command(config, in_objs = in_objs, out_lib = out_lib)

link_c_bin = hancho.command(
desc = "Linking {rel_build_files}",
command = "g++ {ld_opt} {warnings} {rel_source_files} {libs} {sys_libs} -o {rel_build_files}",
ld_opt = "{'-O3' if build_tag == 'release' else '-g -O0'}",
libs = "",
sys_libs = "",
warnings = "-Wall",
)

run_c_test = hancho.command(
desc = "Running test {rel_source_files}",
command = "rm -f {rel_build_files} && {rel_source_files} {args} && touch {rel_build_files}",
build_files = "{swap_ext(source_files, '.pass')}",
args = "",
)
def link_c_bin(in_objs, out_bin, *args, **kwargs):
config = hancho.Config(*args, **kwargs)
command = hancho.Config(
desc = "Linking {rel(out_bin)}",
command = "g++ {ld_opt} {warnings} {rel(in_objs)} {libs} {sys_libs} -o {rel(out_bin)}",
ld_opt = "{'-O3' if build_tag == 'release' else '-g -O0'}",
libs = "",
sys_libs = "",
warnings = "-Wall",
)
return command(config, in_objs = in_objs, out_bin = out_bin)

#-------------------------------------------------------------------------------

def compile_srcs(config, source_files):
objs = []
for file in hancho.flatten(source_files):
if isinstance(file, hancho.Task):
objs.append(file)
else:
objs.append(compile_cpp(file, config))
pass
return objs
def compile_srcs(*args, in_srcs, **kwargs):
config = hancho.Config(*args, **kwargs)
command = hancho.Config(
desc = "Compiling {rel(in_src)} -> {rel(out_obj)} ({build_tag})",
command = "g++ {cpp_std} {gcc_opt} {warnings} {includes} {defines} -c {rel(in_src)} -o {rel(out_obj)}",
cpp_std = "-std=c++20",
gcc_opt = "{'-O3' if build_tag == 'release' else '-g -O0'} -MMD",
warnings = "-Wall -Werror -Wno-unused-variable -Wno-unused-local-typedefs -Wno-unused-but-set-variable",
includes = "-I{repo_path}",
defines = "",
out_obj = "{swap_ext(in_src, '.o')}",
dep_gcc = "{swap_ext(in_src, '.d')}",
)
return [command(config, in_src = file) for file in hancho.flatten(in_srcs)]

def c_binary(config):
source_files = config.pop("source_files")
build_files = config.pop("build_files")
objs = compile_srcs(config, source_files)
return link_c_bin(objs, build_files, config)
def c_binary(*args, in_srcs = [], in_objs = [], out_bin, **kwargs):
config = hancho.Config(*args, **kwargs)
objs = compile_srcs(config, in_srcs = in_srcs)
return link_c_bin(objs + in_objs, out_bin, config)

def c_library(config):
source_files = config.pop("source_files")
build_files = config.pop("build_files")
objs = compile_srcs(config, source_files)
return link_c_lib(objs, build_files, config)
def c_library(*args, in_srcs = [], in_objs = [], out_lib, **kwargs):
config = hancho.Config(*args, **kwargs)
objs = compile_srcs(config, in_srcs=in_srcs)
return link_c_lib(objs + in_objs, out_lib, config)

def c_test(config):
return run_c_test(c_binary(config), config)
def c_test(*args, in_srcs = [], in_objs = [], out_bin, **kwargs):
config = hancho.Config(*args, **kwargs)
objs = compile_srcs(config, in_srcs = in_srcs)
bin = link_c_bin(objs + in_objs, out_bin, config)
result = hancho.Task(
config,
desc = "Running test {rel(in_test)}",
command = "rm -f {rel(out_pass)} && {in_test} {args} && touch {rel(out_pass)}",
in_test = bin,
out_pass = "{swap_ext(in_test, '.pass')}",
args = ""
)
return result

#-------------------------------------------------------------------------------

export.c_binary = hancho.callback(c_binary)
export.c_library = hancho.callback(c_library)
export.c_test = hancho.callback(c_test)
exports.c_binary = c_binary
exports.c_library = c_library
exports.c_test = c_test
28 changes: 17 additions & 11 deletions examples/c_lexer/c_lexer.hancho
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
#-------------------------------------------------------------------------------
# Matcheroni C lexer demo

c_lexer_lib = hancho.rules.c_library(
["CLexer.cpp", "CToken.cpp"],
"c_lexer.a",
rules = imports.rules
config = imports.config

c_lexer_lib = rules.c_library(
config,
in_srcs = ["CLexer.cpp", "CToken.cpp"],
out_lib = "c_lexer.a",
)

hancho.rules.c_test(
"c_lexer_test.cpp",
"c_lexer_test",
rules.c_test(
config,
in_srcs = "c_lexer_test.cpp",
out_bin = "c_lexer_test",
libs = c_lexer_lib,
quiet = True,
)

hancho.rules.c_binary(
"c_lexer_benchmark.cpp",
"c_lexer_benchmark",
libs = c_lexer_lib,
rules.c_binary(
config,
in_srcs = "c_lexer_benchmark.cpp",
out_bin = "c_lexer_benchmark",
libs = c_lexer_lib,
)

export.lib = c_lexer_lib
exports.lib = c_lexer_lib
22 changes: 14 additions & 8 deletions examples/c_parser/c_parser.hancho
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#-------------------------------------------------------------------------------
# C parser example (not finished)

c_parser_lib = hancho.rules.c_library(
source_files = ["CContext.cpp", "CNode.cpp", "CScope.cpp"],
build_files = "c_parser.a",
rules = imports.rules
config = imports.config
c_lexer = imports.c_lexer

c_parser_lib = rules.c_library(
config,
in_srcs = ["CContext.cpp", "CNode.cpp", "CScope.cpp"],
out_lib = "c_parser.a",
)

hancho.rules.c_binary(
"c_parser_benchmark.cpp",
"c_parser_benchmark",
libs = [hancho.c_lexer.lib, c_parser_lib],
rules.c_binary(
config,
in_srcs = "c_parser_benchmark.cpp",
out_bin = "c_parser_benchmark",
libs = [c_lexer.lib, c_parser_lib],
)

# Broken?
Expand All @@ -20,4 +26,4 @@ hancho.rules.c_binary(
# quiet = True
#)

export.lib = c_parser_lib
exports.lib = c_parser_lib
40 changes: 24 additions & 16 deletions examples/json/json.hancho
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
#-------------------------------------------------------------------------------
# Matcheroni JSON parser example

json_parser = hancho.rules.c_library(
["json_matcher.cpp", "json_parser.cpp"],
"json_parser.a",
rules = imports.rules
config = imports.config

json_parser = rules.c_library(
config,
in_srcs = ["json_matcher.cpp", "json_parser.cpp"],
out_lib = "json_parser.a",
)

hancho.rules.c_binary(
"json_conformance.cpp",
"json_conformance",
rules.c_binary(
config,
in_srcs = "json_conformance.cpp",
out_bin = "json_conformance",
libs = json_parser
)

hancho.rules.c_binary(
"json_benchmark.cpp",
"json_benchmark",
rules.c_binary(
config,
in_srcs = "json_benchmark.cpp",
out_bin = "json_benchmark",
libs = json_parser,
)

hancho.rules.c_binary(
"json_demo.cpp",
"json_demo",
rules.c_binary(
config,
in_srcs = "json_demo.cpp",
out_bin = "json_demo",
libs = json_parser,
)

hancho.rules.c_test(
"json_test.cpp",
"json_test",
rules.c_test(
config,
in_srcs = "json_test.cpp",
out_bin = "json_test",
libs = json_parser,
quiet = True
)

export.lib = json_parser
exports.lib = json_parser
33 changes: 20 additions & 13 deletions examples/regex/regex.hancho
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,36 @@
#benchmark_defs = ${benchmark_defs} -DSRELL_NO_NAMEDCAPTURE
#benchmark_defs = ${benchmark_defs} -DSRELL_NO_VMODE

regex_parser = hancho.rules.c_library(
"regex_parser.cpp",
"regex_parser.a",
rules = imports.rules
config = imports.config

regex_parser = rules.c_library(
config,
in_srcs = "regex_parser.cpp",
out_lib = "regex_parser.a",
)

hancho.rules.c_binary(
"regex_demo.cpp",
"regex_demo",
rules.c_binary(
config,
in_srcs = "regex_demo.cpp",
out_bin = "regex_demo",
libs = regex_parser,
)

hancho.rules.c_binary(
"regex_benchmark.cpp",
"regex_benchmark",
rules.c_binary(
config,
in_srcs = "regex_benchmark.cpp",
out_bin = "regex_benchmark",
libs = regex_parser,
sys_libs = "-lboost_system -lboost_regex",
)

hancho.rules.c_test(
"regex_test.cpp",
"regex_test",
rules.c_test(
config,
in_srcs = "regex_test.cpp",
out_bin = "regex_test",
libs = regex_parser,
quiet = True
)

hancho.lib = regex_parser
exports.lib = regex_parser
17 changes: 11 additions & 6 deletions examples/toml/toml.hancho
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#-------------------------------------------------------------------------------
# TOML parser example

toml_lib = hancho.rules.c_library(
"toml_parser.cpp",
"toml_lib.a",
rules = imports.rules
config = imports.config

toml_lib = rules.c_library(
config,
in_srcs = "toml_parser.cpp",
out_lib = "toml_lib.a",
)

hancho.rules.c_test(
"toml_test.cpp",
"toml_test",
rules.c_test(
config,
in_srcs = "toml_test.cpp",
out_bin = "toml_test",
libs = toml_lib,
quiet = True
)
Loading

0 comments on commit c6edce6

Please sign in to comment.