|
1 | 1 | import os
|
2 |
| -from setuptools import setup, Extension |
3 |
| -from setuptools.command.build_ext import build_ext |
| 2 | +import shutil |
| 3 | +import subprocess |
4 | 4 | import sys
|
| 5 | + |
5 | 6 | import setuptools
|
| 7 | +from setuptools import Extension, setup |
| 8 | +from setuptools.command.build_ext import build_ext |
6 | 9 |
|
7 | 10 | __version__ = '1.3.6'
|
8 | 11 |
|
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 |
12 | 43 |
|
13 |
| -libraries = [] |
14 |
| -extra_objects = [] |
| 44 | + raise ValueError("Wrong include dir flag", i) |
15 | 45 |
|
16 |
| -requirements_list = ['pybind11>=2.4', 'numpy'] |
17 | 46 |
|
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 |
21 | 53 |
|
| 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 |
22 | 84 | 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") |
25 | 87 |
|
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")] |
29 | 106 |
|
30 | 107 | ext_modules = [
|
31 | 108 | Extension(
|
32 | 109 | 'pyfastpfor',
|
33 | 110 | source_files,
|
34 |
| - include_dirs=[maindir, os.path.join(maindir, "headers")], |
| 111 | + include_dirs=include_dirs, |
35 | 112 | libraries=libraries,
|
36 | 113 | language='c++',
|
37 | 114 | extra_objects=extra_objects,
|
|
0 commit comments