-
Notifications
You must be signed in to change notification settings - Fork 3
/
configure
executable file
·396 lines (347 loc) · 11.8 KB
/
configure
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env python3
"""Configure the project for building.
Supports Linux, BSD, Mac, Windows and tries to be POSIX compliant (except on
Windows)
"""
import argparse
import collections
import codecs
import fnmatch
import os
import itertools
from os import path
import re
import sys
from tempfile import mkdtemp
import shutil
_project = "libchirp"
_version = "0.1.0"
_base = path.dirname(path.realpath(__file__))
_build = path.realpath(os.curdir)
_baselen = len(_base)
_include = re.compile('^\s*#\s*include\s*"\s*([^"]+\s*).*$')
def match_files(base, pattern):
"""Match files in a base path."""
for root, dirnames, filenames in os.walk(
path.join(_base, base)
):
for filename in fnmatch.filter(filenames, pattern):
yield path.join(root, filename)
def parse_depends(file_, h_files):
"""Find all header depends in this files."""
with codecs.open(file_, 'r', "UTF-8") as f:
for line in f:
match = _include.match(line)
if match:
header = match.group(1)
base = path.dirname(
path.realpath(file_)
)
if _project in header:
depend = path.realpath(
path.join(_base, "include", header)
)
else:
depend = path.realpath(
path.join(base, header)
)
if depend in h_files:
yield depend
def flat_depends(file_, depends, flat):
"""Create a flat depends list of recursive dependencies."""
if file_ not in flat:
dep = depends[file_]
if not dep:
return flat
for rec in dep:
flat_depends(rec, depends, flat)
flat.add(rec)
return flat
def read_depends():
"""Read header dependencies into a dict to generate the makefile."""
depends = collections.defaultdict(set)
c_files = set(match_files("src", "*.c"))
h_files = set()
h_files.update(match_files("src", "*.h"))
h_files.update(match_files("include", "*.h"))
files = list(itertools.chain(c_files, h_files))
for file_ in files:
for depend in parse_depends(file_, h_files):
depends[file_].add(depend)
dependency_list = {file_: flat_depends(
file_,
depends,
set()
) for file_ in c_files}
return dependency_list, files
def replace_base(file_, repl):
"""Replace the base path."""
if file_.startswith(_base):
return "%s%s" % (repl, file_[_baselen:])
else:
return file_
def replace_base_base(file_):
"""Replace the base path with $(BASE)."""
return replace_base(file_, "$(BASE)")
def replace_base_build(file_):
"""Replace the base path with $(BASE)."""
return replace_base(file_, "$(BUILD)")
def parse():
"""Parse the argumentgs."""
parser = argparse.ArgumentParser(
description="Configure %s. Respects CC, CFLAGS , LDFLAGS and "
"ARFLAGS environment variables. If you intend to build bindings, "
"please use the same compiler that was used to build "
"the host language (ie. python, node, php)." % _project
)
parser.add_argument(
'--dev',
action="store_true",
help=(
"Build for development. Enables debug build flags and coverage. "
"Includes all development make targets."
)
)
parser.add_argument(
'--verbose',
action="store_true",
help="Verbose build: display commands. Also: Env-Var VERBOSE=True."
)
parser.add_argument(
'--doc',
action="store_true",
help="Generate documentation. Sphinx has to be installed."
)
parser.add_argument(
'--ignore-coverage',
action="store_true",
help="Ignore the coverage results."
)
parser.add_argument(
'--prefix',
help="Install prefix. Default: /usr/local"
)
return parser.parse_args()
def test_depends(args):
"""Test for dependencies."""
failure = False
sys.stdout.write("Checking for libuv: ")
if os.system("make testlibuv"):
print("""libuv was not found. For details see config.log.
Please install libuv:
Alpine: apk add libuv-dev
Debian-based: apt install libuv1-dev (may require backports)
Redhat-based: yum install libuv-devel
Arch: pacman -S libuv
OSX: brew install libuv
""")
failure = True
else:
print("ok")
sys.stdout.write("Checking for openssl: ")
if os.system("make testopenssl"):
print("""openssl was not found. For details see config.log.
Please install openssl:
Alpine: apk add openssl-dev
Debian-based: apt install libssl-dev
Redhat-based: yum install openssl-devel
Arch: pacman -S openssl
OSX: brew install openssl
""")
failure = True
else:
print("ok")
if failure:
print(
"""\nIf a library is installed in a non-default path please use:
-L in LDFLAGS
and
-I in CFLAGS
""")
if args.doc:
sys.stdout.write("Checking for sphinx: ")
try:
import sphinx # noqa
print("ok")
except ImportError:
print("""sphinx was not found.
Please install sphinx:
Alpine: apk add py-sphinx py-sphinx_rtd_theme graphviz
Debian-based: apt install python-sphinx py-sphinx_rtd_theme graphviz
Redhat-based: yum install python-sphinx python-sphinx_rtd_theme graphviz
Arch: pacman -S python-sphinx python-sphinx_rtd_theme graphviz
OSX: brew install sphinx-doc graphviz
""")
if args.dev:
sys.stdout.write("Checking for cppcheck: ")
if os.system("command -v cppcheck > /dev/null"):
print("""cppcheck was not found.
Please install sphinx:
Alpine: apk add cppcheck
Debian-based: apt install cppcheck
Redhat-based: yum install cppcheck
Arch: pacman -S cppcheck
OSX: brew install cppcheck
""")
else:
print("ok")
def copy_files(args):
"""Copy to build directory."""
if args.dev:
shutil.copy(
path.join(_base, "mk", "dh.pem"),
path.join(_build, "dh.pem")
)
shutil.copy(
path.join(_base, "mk", "cert.pem"),
path.join(_build, "cert.pem")
)
def write_config():
""""Write config.h file with version."""
with codecs.open(
path.join(_base, "mk", "config.defs.h"),
"r",
"UTF-8"
) as infile:
with codecs.open("config.h", "w", "UTF-8") as outfile:
for line in infile:
if line.startswith('#define CH_VERSION "XVERSIONX"'):
outfile.write('#define CH_VERSION "%s"\n' % _version)
else:
outfile.write(line)
print("Created config.h. It contains sensible defaults.")
def write_makefile(args):
"""Parse arguments and write makefile accordingly."""
prefix = "/usr/local"
if args.prefix:
prefix = args.prefix
with codecs.open("Makefile", "w", "UTF-8") as f:
if args.dev:
f.write("all: libraries executables ## Build everything\n\n")
else:
f.write("all: libraries\n\n")
f.write(".PHONY: libraries executables objects\n")
f.write("BASE := %s\n" % _base)
f.write("BUILD := %s\n" % _build)
f.write("PREFIX := %s\n" % prefix)
f.write("DTMP := %s\n\n" % mkdtemp())
f.write("PROJECT := %s\n" % _project)
f.write("VERSION := %s\n" % _version)
f.write("MAJOR := %s\n\n" % _version.split('.')[0])
if args.verbose:
f.write("VERBOSE := True\n")
if not args.dev:
f.write("STRIP := True\n")
if args.doc:
f.write("DOC := True\n")
if args.ignore_coverage:
f.write("IGNORE_COV := True\n")
f.write("\n")
f.write("include $(BASE)/mk/base.mk\n\n")
depends, files = read_depends()
if args.dev:
depends = list(depends.items())
else:
depends = [
(
key, value
) for key, value in depends.items()
if not key.endswith("test.c")
]
f.write(
"LIBRARIES := $(BUILD)/{0}.a $(BUILD)/{0}.so\n\n".format(_project)
)
f.write("libraries: $(LIBRARIES)\n\n")
if args.dev:
f.write("EXECUTABLES := \\\n")
executables = [
(
key, value
) for key, value in depends
if key.endswith("_etest.c")
]
for executable, _ in executables[:-1]:
f.write("\t\t%s \\\n" % replace_base_build(executable[:-2]))
f.write(
"\t\t%s \n\n" % replace_base_build(executables[-1][0][:-2])
)
f.write("executables: $(EXECUTABLES)\n\n")
f.write("COV_FILES := \\\n")
for file_, _ in depends[:-1]:
f.write("\t\t%s.gcov \\\n" % replace_base_build(file_))
f.write("\t\t%s.gcov \n" % replace_base_build(depends[-1][0]))
f.write("""
coverage: clean all etests $(COV_FILES) ## Analyze coverage
ifeq ($(IGNORE_COV),True)
\t!(grep -v "// NOCOV" *.gcov | grep -E "\s+#####:"); true
else
\t!(grep -v "// NOCOV" *.gcov | grep -E "\s+#####:")
endif
\trm -f *.gcov\n\n
""")
f.write("OBJECTS := \\\n")
for file_, _ in depends[:-1]:
f.write("\t\t%so \\\n" % replace_base_build(file_[:-1]))
f.write("\t\t%so \n\n" % replace_base_build(depends[-1][0][:-1]))
f.write("objects: $(OBJECTS) \n\n")
f.write("LIB_OBJECTS := \\\n")
lib_objs = [
file_ for file_, _ in depends
if not file_.endswith("_etest.c")
]
for file_ in lib_objs[:-1]:
f.write("\t\t%so \\\n" % replace_base_build(file_[:-1]))
f.write("\t\t%so \n\n" % replace_base_build(lib_objs[-1][:-1]))
if args.doc:
f.write("DOC_FILES := \\\n")
for file_ in files[:-1]:
f.write("\t\t%s.rst \\\n" % replace_base_build(file_))
f.write("\t\t%s.rst \n\n" % replace_base_build(files[-1]))
f.write("doc_files: $(DOC_FILES) \n\n")
for file_, dep in depends:
if dep:
dep = list(dep)
bfile = "%so" % replace_base_build(file_[:-1])
f.write("%s: \\\n" % bfile)
for depend in dep:
f.write("\t\t%s \\\n" % replace_base_base(depend))
f.write("\t\t$(BUILD)/config.h \\\n")
f.write("\t\t$(BUILD)/Makefile\n")
if args.dev:
f.write("\ninclude $(BASE)/mk/dev.mk")
else:
f.write("\ninclude $(BASE)/mk/rel.mk")
try:
os.unlink(path.join(_base, "config.h"))
except Exception:
pass
print("Created Makefile. It contains sensible defaults.")
def write_ide_files(args):
"""Write ide files like clang_complete."""
if args.dev:
sys.stdout.write("Creating .clang_complete: ")
try:
with codecs.open(path.join(_base, ".clang_complete"), "w") as f:
f.write("-I%s\n-L%s" % (
path.join(_base, "include"),
_build
))
print("ok")
except Exception:
print("failed")
sys.stdout.write("Creating Makefile for syntastic: ")
try:
shutil.copy(
path.join(_base, "mk", "vim.mk"),
path.join(_base, "Makefile"),
)
print("ok")
except Exception:
print("failed")
if __name__ == "__main__":
args = parse()
write_makefile(args)
write_config()
copy_files(args)
test_depends(args)
write_ide_files(args)