#!/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))

#!/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 = {}

def lib_compiler_args(lib_dir):
    if lib_dir is None:
        return ''
    else:
        return '-isystem{}'.format(lib_dir)

def boost_compiler_arg():
    return lib_compiler_args(os.environ.get('BOOST_DIR'))

def websocketpp_compiler_arg():
    return lib_compiler_args(os.environ.get('WEBSOCKETPP_DIR'))

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 {boost} {websocketpp} -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'), boost = boost_compiler_arg(), websocketpp = websocketpp_compiler_arg())

(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))

Allow for configuring Boost and websocketpp directories