forked from libnonius/nonius
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.py
executable file
·174 lines (132 loc) · 4.97 KB
/
bootstrap.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/python
import ninja_syntax
import itertools
import os
import fnmatch
import re
import sys
import argparse
# --- util functions
def flags(*iterables):
return ' '.join(itertools.chain(*iterables))
def include(d):
return '-I' + d;
def warning(d):
return '-W' + d;
def no_warning(d):
return '-Wno-' + d;
def define(d):
return '-D' + d;
def library(l):
return '-l' + l;
def dependency_include(d):
return '-isystem ' + os.path.join('deps', d, 'include');
def get_files(root, pattern):
pattern = fnmatch.translate(pattern)
for dir, dirs, files in os.walk(root):
for f in files:
if re.match(pattern, f):
yield os.path.join(dir, f)
def object_file(fn):
return os.path.join('obj', re.sub(r'\.c\+\+$', '.o', fn))
# --- arguments
parser = argparse.ArgumentParser()
parser.add_argument('--debug', action='store_true', help='compile with debug information')
parser.add_argument('--cxx', default='g++', metavar='executable', help='compiler name to use (default: g++)')
parser.add_argument('--boost-dir', default=None, metavar='path', help='path of boost folder (i.e. the folder with include/ and lib/ subfolders)')
parser.add_argument('--no-lto', action='store_true', help='do not perform link-time optimisation')
args = parser.parse_args()
# --- variables
dependencies = ['catch', 'wheels']
include_flags = flags([include('include')], map(dependency_include, dependencies), ['-isystemdeps/cpptemplate'])
if(args.boost_dir):
include_flags += ' ' + dependency_include(args.boost_dir)
cxx_flags = flags(['-pedantic', '-std=c++11', '-pthread', '-g' if args.debug else '-O3'])
warning_flags = flags(map(warning, ['all', 'extra', 'error']))
ignored_warning_flags = flags(map(no_warning, []))
define_flags = ''
lib_flags = ''
ld_flags = flags(['-pthread'] + [] if args.no_lto or args.debug else ['-flto'])
stringize_tool = 'tools/stringize.py'
single_header_tool = 'tools/single_header.py'
# --- preamble
ninja = ninja_syntax.Writer(open('build.ninja', 'w'))
ninja.variable('ninja_required_version', '1.3')
ninja.variable('builddir', 'obj' + os.sep)
# --- rules
ninja.rule('bootstrap',
command = ' '.join(['python'] + sys.argv),
generator = True,
description = 'BOOTSTRAP')
ninja.rule('cxx',
command = ' '.join([args.cxx, '-MMD', '-MF $out.d', '-c', cxx_flags, warning_flags, include_flags, define_flags, '$in', '-o $out']),
deps = 'gcc',
depfile = '$out.d',
description = 'C++ $in')
ninja.rule('link',
command = ' '.join([args.cxx, cxx_flags, warning_flags, ld_flags, '$in', '-o $out', lib_flags]),
description = 'LINK $in')
ninja.rule('lib',
command = ' '.join(['ar', 'rcs', '$out', '$in']),
description = 'AR $in')
ninja.rule('stringize',
command = ' '.join(['python', stringize_tool, '$in', '$out']),
description = 'STRINGIZE $in')
ninja.rule('header',
command = ' '.join(['python', single_header_tool, '$in', '$out']),
description = 'HEADER $in')
# --- build edges
ninja.build('build.ninja', 'bootstrap',
implicit = sys.argv[0])
hdr_files = list(get_files('include', '*.h++'))
src_files = list(get_files('src', '*.c++'))
obj_files = [object_file(fn) for fn in src_files]
for fn in src_files:
ninja.build(object_file(fn), 'cxx',
inputs = fn)
built_libs = []
if len(obj_files) > 0:
ninja.build('lib', 'phony',
inputs = libnonius)
libnonius = os.path.join('bin', 'libnonius.a')
ninja.build(libnonius, 'lib',
inputs = obj_files)
built_libs = [libnonius]
test_src_files = list(get_files('test', '*.c++'))
test_obj_files = [object_file(fn) for fn in test_src_files]
for fn in test_src_files:
ninja.build(object_file(fn), 'cxx',
inputs = fn)
test_runner = os.path.join('bin', 'test')
ninja.build(test_runner, 'link',
inputs = test_obj_files + built_libs)
ninja.build('test', 'phony',
inputs = test_runner)
header = os.path.join('dist', 'nonius.h++')
ninja.build(header, 'header',
inputs = os.path.join('include', 'nonius', 'nonius_single.h++'),
implicit = single_header_tool)
ninja.build('header', 'phony',
inputs = header)
html_report_template = 'include/nonius/detail/html_report_template.g.h++'
ninja.build(html_report_template, 'stringize',
inputs = 'tpl/html_report.tpl',
implicit = stringize_tool)
ninja.build('templates', 'phony',
inputs = html_report_template)
# --- examples
example_files = list(get_files('examples', '*.c++'))
examples = []
for fn in example_files:
ninja.build(object_file(fn), 'cxx',
inputs = fn)
name = re.sub(r'\.c\+\+$', '', os.path.basename(fn))
example = os.path.join('bin', 'examples', name)
ninja.build(example, 'link',
inputs = [object_file(fn)] + built_libs)
ninja.build(name, 'phony',
inputs = example)
examples += [name]
ninja.build('examples', 'phony',
inputs = examples)
ninja.default('examples')