diff --git a/headers/fastpfor b/headers/fastpfor new file mode 120000 index 0000000..945c9b4 --- /dev/null +++ b/headers/fastpfor @@ -0,0 +1 @@ +. \ No newline at end of file diff --git a/python_bindings/pyfastpfor.cc b/python_bindings/pyfastpfor.cc index 1d5e4e3..f10bce3 100644 --- a/python_bindings/pyfastpfor.cc +++ b/python_bindings/pyfastpfor.cc @@ -15,8 +15,8 @@ #include #include -#include "headers/codecfactory.h" -#include "headers/deltautil.h" +#include +#include namespace py = pybind11; diff --git a/python_bindings/setup.py b/python_bindings/setup.py index 56a0db3..ca734a0 100755 --- a/python_bindings/setup.py +++ b/python_bindings/setup.py @@ -1,40 +1,119 @@ import os -from setuptools import setup, Extension -from setuptools.command.build_ext import build_ext +import shutil +import subprocess import sys + import setuptools +from setuptools import Extension, setup +from setuptools.command.build_ext import build_ext __version__ = '1.3.6' -maindir = os.path.join(".", "fastpfor") -library_file = os.path.join(maindir, "libFastPFor.a") -source_files = ['pyfastpfor.cc'] +pcCommand = shutil.which("pkgconf") +if not pcCommand: + pcCommand = shutil.which("pkg-config") +pcCommand = None + + +def runPkgConfig(arg, package): + return subprocess.run([pcCommand, arg, package], capture_output=True).stdout.decode( + "utf-8" + ) + + +def getInfoFromPkgConfig(arg, pkgName): + res = str(runPkgConfig(arg, pkgName))[:-1].split(" ") + if len(res) == 1 and not res[0]: + return () + + return res + + +def processPkgConfigLibDir(l): + if l.startswith("-L"): + l = l[2:] + return l + + raise ValueError("Wrong lib dir flag", l) + + +def processPkgConfigIncludeDir(i): + if i.startswith("-I"): + i = i[2:] + return i -libraries = [] -extra_objects = [] + raise ValueError("Wrong include dir flag", i) -requirements_list = ['pybind11>=2.4', 'numpy'] -if os.path.exists(library_file): - # if we have a prebuilt library file, use that. - extra_objects.append(library_file) +def processPkgConfigLib(l): + if l.startswith("-l"): + l = l[2:] + if l.startswith(":"): + l = l[1:] + return l + raise ValueError("Wrong lib flag", l) + + +def getPcInfo(pkgName): + if pcCommand: + extra_compile_args = getInfoFromPkgConfig("-cflags", pkgName) + include_dirs = [ + processPkgConfigIncludeDir(l) + for l in getInfoFromPkgConfig("--cflags-only-I", pkgName) + ] + libs_dirs = [ + processPkgConfigLibDir(l) for l in getInfoFromPkgConfig("--libs-only-L", pkgName) + ] + libraries = [processPkgConfigLib(l) for l in getInfoFromPkgConfig("--libs", pkgName)] + extra_objects = getInfoFromPkgConfig("--static", pkgName) + else: + extra_compile_args = include_dirs = libs_dirs = libraries = extra_objects = () + + return extra_compile_args, include_dirs, libs_dirs, libraries, extra_objects + + +extra_compile_args, include_dirs, libs_dirs, libraries, extra_objects = pkgConfigInfo = getPcInfo("fastpfor") + +source_files = ["pyfastpfor.cc"] +requirements_list = ["pybind11>=2.4", "numpy"] + + +if any(pkgConfigInfo): + pass else: - # Otherwise build all the files here directly (excluding test files) - exclude_files = set("""unit.cpp codecs.cpp""".split()) + maindir = os.path.join(".", "fastpfor") + library_file = os.path.join(maindir, "libFastPFor.a") + extra_compile_args = () - for root, subdirs, files in os.walk(os.path.join(maindir, "src")): - source_files.extend(os.path.join(root, f) for f in files - if (f.endswith(".cc") or f.endswith(".c") or f.endswith(".cpp")) and f not in exclude_files) + extra_objects = [] + + if os.path.exists(library_file): + # if we have a prebuilt library file, use that. + extra_objects.append(library_file) + + else: + # Otherwise build all the files here directly (excluding test files) + exclude_files = set("""unit.cpp codecs.cpp""".split()) + + for root, subdirs, files in os.walk(os.path.join(maindir, "src")): + source_files.extend( + os.path.join(root, f) + for f in files + if (f.endswith(".cc") or f.endswith(".c") or f.endswith(".cpp")) + and f not in exclude_files + ) + include_dirs = [maindir, os.path.join(maindir, "headers")] ext_modules = [ Extension( 'pyfastpfor', source_files, - include_dirs=[maindir, os.path.join(maindir, "headers")], + include_dirs=include_dirs, libraries=libraries, language='c++', extra_objects=extra_objects, + extra_compile_args = extra_compile_args, ), ]