forked from darvid/python-hyperscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_hyperscan_extension.py
89 lines (82 loc) · 2.67 KB
/
build_hyperscan_extension.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
import glob
import os
import subprocess
from distutils.command.build_ext import build_ext
from distutils.core import Extension
pcre_path = os.getenv("PCRE_PATH", "/opt/pcre/.libs")
# http://code.activestate.com/recipes/502261-python-distutils-pkg-config/
def pkgconfig(libs, optional="", static=False):
flag_map = {
"include_dirs": (["--cflags-only-I"], 2, []),
"library_dirs": (["--libs-only-L"], 2, []),
"libraries": (["--libs-only-l"], 2, []),
"extra_compile_args": (
["--cflags-only-other"],
0,
["-O0", "-DPCRE_STATIC"],
),
}
if static:
flag_map["extra_link_args"] = (
["--libs-only-other"],
0,
[
"-l:libhs.a",
"-l:libchimera.a",
],
)
ext_kwargs = {"extra_compile_args": []}
library_options = set(
subprocess.check_output(
["pkg-config", optional, "--libs-only-l", *libs]
)
.decode()
.split()
)
for lib in libs:
for distutils_kwarg, (
pkg_options,
trim_offset,
default_value,
) in flag_map.items():
_pkg_options = pkg_options[:]
if static:
_pkg_options += [
"--static",
*[_lib for _lib in libs if _lib != lib],
]
options = set(
subprocess.check_output(
["pkg-config", optional, *_pkg_options, lib]
)
.decode()
.split()
)
ext_kwargs.setdefault(distutils_kwarg, default_value).extend(
set([opt[trim_offset:] for opt in options])
)
ext_kwargs["libraries"] = list(set(ext_kwargs["libraries"]))
return ext_kwargs
def build(setup_kwargs):
static = os.getenv("PYTHON_HYPERSCAN_STATIC", 1) not in ("0", "false")
pkg_config_options = pkgconfig(["libhs", "libch"], static=static)
if static:
for lib in ("pcre", "hs", "chimera"):
if lib in pkg_config_options["libraries"]:
pkg_config_options["libraries"].remove(lib)
setup_kwargs.update(
{
"ext_modules": [
Extension(
"hyperscan._hyperscan",
["src/hyperscan/hyperscanmodule.c"],
extra_objects=[
os.path.join(pcre_path, "libpcre.a"),
*glob.glob(os.path.join(pcre_path, '*.o')),
],
**pkg_config_options,
)
],
"cmdclass": {"build_ext": build_ext},
}
)