Skip to content

Commit 79b6149

Browse files
committed
Added support of building using the deps fetched from pkg-config.
1 parent 48ade52 commit 79b6149

File tree

1 file changed

+94
-17
lines changed

1 file changed

+94
-17
lines changed

python_bindings/setup.py

+94-17
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,114 @@
11
import os
2-
from setuptools import setup, Extension
3-
from setuptools.command.build_ext import build_ext
2+
import shutil
3+
import subprocess
44
import sys
5+
56
import setuptools
7+
from setuptools import Extension, setup
8+
from setuptools.command.build_ext import build_ext
69

710
__version__ = '1.3.6'
811

9-
maindir = os.path.join(".", "fastpfor")
10-
library_file = os.path.join(maindir, "libFastPFor.a")
11-
source_files = ['pyfastpfor.cc']
12+
pcCommand = shutil.which("pkgconf")
13+
if not pcCommand:
14+
pcCommand = shutil.which("pkg-config")
15+
16+
17+
def runPkgConfig(arg, package):
18+
return subprocess.run([pcCommand, arg, package], capture_output=True).stdout.decode(
19+
"utf-8"
20+
)
21+
22+
23+
def getInfoFromPkgConfig(arg, pkgName):
24+
res = str(runPkgConfig(arg, pkgName))[:-1].split(" ")
25+
if len(res) == 1 and not res[0]:
26+
return ()
27+
28+
return res
29+
30+
31+
def processPkgConfigLibDir(l):
32+
if l.startswith("-L"):
33+
l = l[2:]
34+
return l
35+
36+
raise ValueError("Wrong lib dir flag", l)
37+
38+
39+
def processPkgConfigIncludeDir(i):
40+
if i.startswith("-I"):
41+
i = i[2:]
42+
return i
1243

13-
libraries = []
14-
extra_objects = []
44+
raise ValueError("Wrong include dir flag", i)
1545

16-
requirements_list = ['pybind11>=2.4', 'numpy']
1746

18-
if os.path.exists(library_file):
19-
# if we have a prebuilt library file, use that.
20-
extra_objects.append(library_file)
47+
def processPkgConfigLib(l):
48+
if l.startswith("-l"):
49+
l = l[2:]
50+
if l.startswith(":"):
51+
l = l[1:]
52+
return l
2153

54+
raise ValueError("Wrong lib flag", l)
55+
56+
57+
def getPcInfo(pkgName):
58+
if pcCommand:
59+
flags = getInfoFromPkgConfig("-cflags", pkgName)
60+
includes = [
61+
processPkgConfigIncludeDir(l)
62+
for l in getInfoFromPkgConfig("--cflags-only-I", pkgName)
63+
]
64+
libs_dirs = [
65+
processPkgConfigLibDir(l) for l in getInfoFromPkgConfig("--libs-only-L", pkgName)
66+
]
67+
libraries = [processPkgConfigLib(l) for l in getInfoFromPkgConfig("--libs", pkgName)]
68+
extra_objects = getInfoFromPkgConfig("--static", pkgName)
69+
else:
70+
flags = includes = libs_dirs = libraries = extra_objects = ()
71+
72+
return flags, includes, libs_dirs, libraries, extra_objects
73+
74+
75+
flags, include_dirs, libs_dirs, libraries, extra_objects = pkgConfigInfo = getPcInfo(
76+
"fastpfor"
77+
)
78+
79+
source_files = ["pyfastpfor.cc"]
80+
requirements_list = ["pybind11>=2.4", "numpy"]
81+
82+
if any(pkgConfigInfo):
83+
pass
2284
else:
23-
# Otherwise build all the files here directly (excluding test files)
24-
exclude_files = set("""unit.cpp codecs.cpp""".split())
85+
maindir = os.path.join(".", "fastpfor")
86+
library_file = os.path.join(maindir, "libFastPFor.a")
2587

26-
for root, subdirs, files in os.walk(os.path.join(maindir, "src")):
27-
source_files.extend(os.path.join(root, f) for f in files
28-
if (f.endswith(".cc") or f.endswith(".c") or f.endswith(".cpp")) and f not in exclude_files)
88+
extra_objects = []
89+
90+
if os.path.exists(library_file):
91+
# if we have a prebuilt library file, use that.
92+
extra_objects.append(library_file)
93+
94+
else:
95+
# Otherwise build all the files here directly (excluding test files)
96+
exclude_files = set("""unit.cpp codecs.cpp""".split())
97+
98+
for root, subdirs, files in os.walk(os.path.join(maindir, "src")):
99+
source_files.extend(
100+
os.path.join(root, f)
101+
for f in files
102+
if (f.endswith(".cc") or f.endswith(".c") or f.endswith(".cpp"))
103+
and f not in exclude_files
104+
)
105+
include_dirs = [maindir, os.path.join(maindir, "headers")]
29106

30107
ext_modules = [
31108
Extension(
32109
'pyfastpfor',
33110
source_files,
34-
include_dirs=[maindir, os.path.join(maindir, "headers")],
111+
include_dirs=include_dirs,
35112
libraries=libraries,
36113
language='c++',
37114
extra_objects=extra_objects,

0 commit comments

Comments
 (0)