Skip to content

Commit 4bcb2bc

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

File tree

2 files changed

+98
-19
lines changed

2 files changed

+98
-19
lines changed

python_bindings/pyfastpfor.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
#include <pybind11/pybind11.h>
1616
#include <pybind11/numpy.h>
1717

18-
#include "headers/codecfactory.h"
19-
#include "headers/deltautil.h"
18+
#include <fastpfor/codecfactory.h>
19+
#include <fastpfor/deltautil.h>
2020

2121
namespace py = pybind11;
2222

python_bindings/setup.py

+96-17
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,119 @@
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+
pcCommand = None
16+
17+
18+
def runPkgConfig(arg, package):
19+
return subprocess.run([pcCommand, arg, package], capture_output=True).stdout.decode(
20+
"utf-8"
21+
)
22+
23+
24+
def getInfoFromPkgConfig(arg, pkgName):
25+
res = str(runPkgConfig(arg, pkgName))[:-1].split(" ")
26+
if len(res) == 1 and not res[0]:
27+
return ()
28+
29+
return res
30+
31+
32+
def processPkgConfigLibDir(l):
33+
if l.startswith("-L"):
34+
l = l[2:]
35+
return l
36+
37+
raise ValueError("Wrong lib dir flag", l)
38+
39+
40+
def processPkgConfigIncludeDir(i):
41+
if i.startswith("-I"):
42+
i = i[2:]
43+
return i
1244

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

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

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

55+
raise ValueError("Wrong lib flag", l)
56+
57+
58+
def getPcInfo(pkgName):
59+
if pcCommand:
60+
extra_compile_args = getInfoFromPkgConfig("-cflags", pkgName)
61+
include_dirs = [
62+
processPkgConfigIncludeDir(l)
63+
for l in getInfoFromPkgConfig("--cflags-only-I", pkgName)
64+
]
65+
libs_dirs = [
66+
processPkgConfigLibDir(l) for l in getInfoFromPkgConfig("--libs-only-L", pkgName)
67+
]
68+
libraries = [processPkgConfigLib(l) for l in getInfoFromPkgConfig("--libs", pkgName)]
69+
extra_objects = getInfoFromPkgConfig("--static", pkgName)
70+
else:
71+
extra_compile_args = include_dirs = libs_dirs = libraries = extra_objects = ()
72+
73+
return extra_compile_args, include_dirs, libs_dirs, libraries, extra_objects
74+
75+
76+
extra_compile_args, include_dirs, libs_dirs, libraries, extra_objects = pkgConfigInfo = getPcInfo("fastpfor")
77+
78+
source_files = ["pyfastpfor.cc"]
79+
requirements_list = ["pybind11>=2.4", "numpy"]
80+
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")
87+
extra_compile_args = ()
2588

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

30108
ext_modules = [
31109
Extension(
32110
'pyfastpfor',
33111
source_files,
34-
include_dirs=[maindir, os.path.join(maindir, "headers")],
112+
include_dirs=include_dirs,
35113
libraries=libraries,
36114
language='c++',
37115
extra_objects=extra_objects,
116+
extra_compile_args = extra_compile_args,
38117
),
39118
]
40119

0 commit comments

Comments
 (0)