#!/usr/bin/env python3
import os
from pathlib import Path

here        = Path(__file__).parent.resolve()
source_path = Path('src')
temp_path   = Path('tmp')
out_path    = Path('bin')

sources = {}

for root, dirs, files in os.walk(str(source_path)):
    for fn in files:
        if fn.endswith('.cpp'):
            path        = Path(root) / fn
            object_path = temp_path / path.relative_to(source_path).with_suffix('.o')

            sources[path] = object_path

prefix = '''
# this file is generated by configure.py
cxx          = {cxx}
common_flags = -ggdb -fPIC
cxx_flags    = $common_flags -Wall -Werror -pedantic-errors -std=c++14 -MMD -c
exe_flags    = $common_flags -fno-omit-frame-pointer

rule cxx
    depfile = $out.d
    command = $cxx $cxx_flags -MF $out.d -o $out $in

rule exe
    command = $cxx $exe_flags -o $out $in
'''.format(cxx = os.environ.get('CXX', 'clang++-3.8'))

(here / temp_path).mkdir(parents = True, exist_ok = True)
(here / out_path).mkdir(parents = True, exist_ok = True)

with (here / 'build.ninja').open('w', encoding = 'utf-8', newline = '\n') as fp:
    def out(*args, **kwargs):
        kwargs['file'] = fp
        print(*args, **kwargs)

    out(prefix.strip('\n'))

    # exe rule: all objects into single exec
    # also make it default
    exe_deps = ' '.join(str(x) for x in sources.values())
    exe_path = (out_path / 'tatsoryk-server')
    out('build {}: exe {}'.format(exe_path, exe_deps))
    out('default {}'.format(exe_path))

    # object rules
    for src, obj in sources.items():
        out('build {}: cxx {}'.format(obj, src))

@@ -191,16 +191,327 @@
 s = %7B%7D%0A%0A
+def lib_compiler_args(lib_dir):%0A    if lib_dir is None:%0A        return ''%0A    else:%0A        return '-isystem%7B%7D'.format(lib_dir)%0A%0Adef boost_compiler_arg():%0A    return lib_compiler_args(os.environ.get('BOOST_DIR'))%0A%0Adef websocketpp_compiler_arg():%0A    return lib_compiler_args(os.environ.get('WEBSOCKETPP_DIR'))%0A%0A
 for root
@@ -949,16 +949,38 @@
 td=c++14
+ %7Bboost%7D %7Bwebsocketpp%7D
  -MMD -c
@@ -1169,16 +1169,16 @@
 out $in%0A
-
 '''.form
@@ -1222,16 +1222,88 @@
 ++-3.8')
+, boost = boost_compiler_arg(), websocketpp = websocketpp_compiler_arg()
 )%0A%0A(here

Allow for configuring Boost and websocketpp directories