-
Notifications
You must be signed in to change notification settings - Fork 16
/
setup.py
191 lines (156 loc) · 5.47 KB
/
setup.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# -*- coding: utf-8 -*-
import os
import sys
import glob
import shutil
import pybind11
from setuptools.command.build_ext import build_ext
from setuptools import setup, Extension, find_packages
here = os.path.abspath(os.path.dirname(__file__))
about = {}
with open(os.path.join(here, "src", "pdftopng", "__version__.py"), "r") as f:
exec(f.read(), about)
with open("README.md", "r") as f:
readme = f.read()
requires = [
"Click>=7.0",
]
dev_requires = ["Pillow>=8.2.0", "pytest>=6.2.3", "pytest-cov>=2.11.1"]
dev_requires = dev_requires + requires
poppler_dir = os.path.join(os.getcwd(), "lib", "poppler")
build_dir = os.path.join(poppler_dir, "build")
library_dirs = []
libraries = []
if sys.platform in ["linux", "darwin"]:
library_dirs.extend([build_dir])
libraries.extend(["poppler"])
if sys.platform == "win32":
# https://docs.python.org/3/library/platform.html#platform.architecture
x = "x64" if sys.maxsize > 2 ** 32 else "x86"
poppler_dir = os.path.join(os.getcwd(), "lib", "poppler")
build_dir = os.path.join(poppler_dir, f"build_win_{x}")
# set VCPKG_INSTALLATION_ROOT=C:\dev\vcpkg
vcpkg_lib_dir = os.path.join(
os.environ["VCPKG_INSTALLATION_ROOT"], "installed", f"{x}-windows", "lib"
)
poppler_lib_dir = os.path.join(build_dir, "Release")
library_dirs.extend([vcpkg_lib_dir, poppler_lib_dir])
libraries.extend(
["freetype", "fontconfig", "libpng16", "jpeg", "advapi32", "poppler"]
)
include_dirs = [
poppler_dir,
os.path.join(poppler_dir, "fofi"),
os.path.join(poppler_dir, "goo"),
os.path.join(poppler_dir, "utils"),
os.path.join(poppler_dir, "poppler"),
build_dir,
os.path.join(build_dir, "utils"),
os.path.join(build_dir, "poppler"),
pybind11.get_include(),
]
ext_modules = [
Extension(
"pdftopng.pdftopng",
# Sort input source files to ensure bit-for-bit reproducible builds
# (https://github.com/pybind/python_example/pull/53)
sorted([os.path.join("src", "pdftopng", "pdftopng.cpp")]),
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
language="c++",
),
]
# cf http://bugs.python.org/issue26689
def has_flag(compiler, flagname):
"""Return a boolean indicating whether a flag name is supported on
the specified compiler.
"""
import tempfile
import os
with tempfile.NamedTemporaryFile("w", suffix=".cpp", delete=False) as f:
f.write("int main (int argc, char **argv) { return 0; }")
fname = f.name
try:
compiler.compile([fname], extra_postargs=[flagname])
except setuptools.distutils.errors.CompileError:
return False
finally:
try:
os.remove(fname)
except OSError:
pass
return True
def cpp_flag(compiler):
"""Return the -std=c++[11/14/17] compiler flag.
The newer version is prefered over c++11 (when it is available).
"""
flags = ["-std=c++14", "-std=c++11"]
for flag in flags:
if has_flag(compiler, flag):
return flag
raise RuntimeError("Unsupported compiler -- at least C++11 support " "is needed!")
class BuildExt(build_ext):
"""A custom build extension for adding compiler-specific options."""
c_opts = {
"msvc": ["/MD", "/EHsc", "/std:c++14"],
"unix": ["-O3", "-Wall", "-shared", "-fPIC"],
}
l_opts = {
"msvc": [],
"unix": [],
}
if sys.platform == "darwin":
darwin_opts = ["-stdlib=libc++", "-mmacosx-version-min=10.7"]
c_opts["unix"] += darwin_opts
l_opts["unix"] += darwin_opts
def build_extensions(self):
ct = self.compiler.compiler_type
opts = self.c_opts.get(ct, [])
link_opts = self.l_opts.get(ct, [])
if ct == "unix":
opts.append(cpp_flag(self.compiler))
if has_flag(self.compiler, "-fvisibility=hidden"):
opts.append("-fvisibility=hidden")
for ext in self.extensions:
ext.define_macros = [
("VERSION_INFO", '"{}"'.format(self.distribution.get_version()))
]
ext.extra_compile_args = opts
ext.extra_link_args = link_opts
build_ext.build_extensions(self)
def setup_package():
metadata = dict(
name=about["__title__"],
version=about["__version__"],
description=about["__description__"],
long_description=readme,
long_description_content_type="text/markdown",
url=about["__url__"],
author=about["__author__"],
author_email=about["__author_email__"],
license=about["__license__"],
packages=find_packages(where="src", exclude=("tests",)),
package_dir={"": "src"},
ext_modules=ext_modules,
include_package_data=True,
install_requires=requires,
extras_require={"dev": dev_requires},
entry_points={
"console_scripts": [
"pdftopng = pdftopng.cli:cli",
],
},
classifiers=[
# Trove classifiers
# Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers
"License :: OSI Approved :: GNU General Public License v2 (GPLv2)",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
],
cmdclass={"build_ext": BuildExt},
)
setup(**metadata)
if __name__ == "__main__":
setup_package()