add stuff
This commit is contained in:
parent
91140888db
commit
1cd4394841
12 changed files with 1702 additions and 18 deletions
132
configure.py
Normal file
132
configure.py
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
#!/usr/bin/env python3
|
||||
"""generates a Ninja build file
|
||||
|
||||
usage:
|
||||
python3 configure.py
|
||||
python3 configure.py --asan # enable address/undefined sanitizers
|
||||
python3 configure.py --debug # -O0 instead of -O2
|
||||
"""
|
||||
import argparse
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent
|
||||
SRC = ROOT / "src"
|
||||
BUILD = ROOT / "build"
|
||||
|
||||
CXX = os.environ.get("CXX", "g++")
|
||||
CXXFLAGS = ["-std=c++20", "-O2", "-g"]
|
||||
WARNINGS = ["-Wall", "-Wextra", "-Wpedantic", "-Wshadow", "-Wconversion", "-Wnull-dereference"]
|
||||
linkflags = []
|
||||
|
||||
def discover_sources():
|
||||
return sorted(
|
||||
p.name for p in SRC.glob("*.cpp")
|
||||
if not p.name.endswith("_main.cpp") and not p.name.startswith(".")
|
||||
)
|
||||
|
||||
def write_makefile_convenience():
|
||||
make = ROOT / "Makefile"
|
||||
make.write_text(
|
||||
"""# convenience wrapper around configure.py + ninja.
|
||||
#
|
||||
# make -> configure + build
|
||||
# make configure -> regenerate build.ninja
|
||||
# make clean -> remove build dir
|
||||
# make format -> clang-format all sources (if available)
|
||||
#
|
||||
.PHONY: all configure clean format
|
||||
all: configure
|
||||
\t@ninja -C build
|
||||
|
||||
configure:
|
||||
\t@python3 configure.py
|
||||
|
||||
clean:
|
||||
\t@rm -rf build
|
||||
|
||||
format:
|
||||
\t@command -v clang-format >/dev/null && clang-format -i src/*.cpp src/*.hpp || true
|
||||
""",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
|
||||
def emit_ninja(cxx, cxxflags, dst):
|
||||
sources = discover_sources()
|
||||
objs = ["obj/" + Path(s).stem + ".o" for s in sources]
|
||||
target = "bajia"
|
||||
|
||||
rule_cxx = (
|
||||
"rule cxx\n"
|
||||
" command = {cxx} {cxxflags} -MMD -MF $out.d -c $in -o $out\n"
|
||||
" depfile = $out.d\n"
|
||||
" deps = gcc\n"
|
||||
" description = CXX $out\n"
|
||||
).format(cxx=cxx, cxxflags=" ".join(cxxflags))
|
||||
|
||||
rule_link = (
|
||||
"rule link\n"
|
||||
" command = {cxx} {ldflags} $in -o $out -lpthread\n"
|
||||
" description = LINK $out\n"
|
||||
).format(cxx=cxx, ldflags=" ".join(linkflags))
|
||||
|
||||
lines = ["# generated by configure.py -- do not edit", "ninja_required_version = 1.8", ""]
|
||||
lines.append(rule_cxx)
|
||||
lines.append(rule_link)
|
||||
lines.append('build {target}: link {objs}'.format(target=target, objs=" ".join(objs)))
|
||||
lines.append("")
|
||||
for o, s in zip(objs, sources):
|
||||
lines.append('build {o}: cxx {src}/{s}'.format(o=o, src=SRC, s=s))
|
||||
lines.append("")
|
||||
lines.append('build all: phony {target}'.format(target=target))
|
||||
lines.append("default all")
|
||||
lines.append("")
|
||||
|
||||
(BUILD / "obj").mkdir(exist_ok=True)
|
||||
dst.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
||||
|
||||
def parse_args():
|
||||
p = argparse.ArgumentParser(description="Configure bajia build (generates build.ninja)")
|
||||
p.add_argument("--asan", action="store_true", help="enable address + UB sanitizers")
|
||||
p.add_argument("--debug", action="store_true", help="disable -O2, enable -O0")
|
||||
p.add_argument("--clean", action="store_true", help="remove build dir")
|
||||
return p.parse_args()
|
||||
|
||||
def main():
|
||||
args = parse_args()
|
||||
if args.clean:
|
||||
shutil.rmtree(BUILD, ignore_errors=True)
|
||||
print("removed build/")
|
||||
return 0
|
||||
|
||||
if not shutil.which("ninja"):
|
||||
print("error: ninja not found in PATH", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
BUILD.mkdir(exist_ok=True)
|
||||
|
||||
cxxflags = list(CXXFLAGS)
|
||||
global linkflags
|
||||
linkflags = []
|
||||
if args.debug:
|
||||
cxxflags = [f for f in cxxflags if f != "-O2"]
|
||||
cxxflags += ["-O0"]
|
||||
if args.asan:
|
||||
cxxflags += ["-fsanitize=address,undefined", "-fno-omit-frame-pointer"]
|
||||
linkflags += ["-fsanitize=address,undefined"]
|
||||
|
||||
cxxflags = [f for f in cxxflags if f not in WARNINGS]
|
||||
cxxflags = WARNINGS + cxxflags
|
||||
|
||||
emit_ninja(CXX, cxxflags, BUILD / "build.ninja")
|
||||
write_makefile_convenience()
|
||||
|
||||
print(f"configured {len(discover_sources())} sources -> build/build.ninja")
|
||||
print("run: ninja -C build")
|
||||
return 0
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Loading…
Add table
Add a link
Reference in a new issue