Skip to content

Commit 52814a5

Browse files
mgautierfrrenaud gaudin
authored andcommitted
Simplify setup.py and remove unecessary warning.
Fix #70
1 parent dc743db commit 52814a5

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

setup.py

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,95 +15,99 @@
1515
1616
To compile or run this project, you must first get the libzim headers & binary:
1717
18-
- You can get the headers here and build and install the binary from source:
19-
https://github.com/openzim/libzim
20-
21-
- Or you can download a full prebuilt release (if one exists for your platform):
18+
- Compile and install libzim from source : https://github.com/openzim/libzim
19+
- Download a full prebuilt release (if one exists for your platform):
2220
https://download.openzim.org/release/libzim/
21+
- Installed a packaged version of libzim :
22+
. `apt-get install libzim-devel`
23+
. `dnf install libzim-dev`
2324
2425
Either place the `libzim.so` and `zim/*.h` files in `./lib/` and `./include/`,
2526
or set these environment variables to use custom libzim header and dylib paths:
2627
2728
$ export CFLAGS="-I/tmp/libzim_linux-x86_64-6.1.1/include"
2829
$ export LDFLAGS="-L/tmp/libzim_linux-x86_64-6.1.1/lib/x86_64-linux-gnu"
2930
$ export LD_LIBRARY_PATH+=":/tmp/libzim_linux-x86_64-6.1.1/lib/x86_64-linux-gnu"
31+
32+
If you have installed libzim from the packages, you probably don't have anything to do
33+
on environment variables side.
3034
"""
35+
36+
3137
import os
38+
import sys
3239
import platform
3340
from pathlib import Path
3441
from ctypes.util import find_library
42+
from textwrap import dedent
3543

3644
from setuptools import setup, Extension
3745
from Cython.Build import cythonize
3846
from Cython.Distutils.build_ext import new_build_ext as build_ext
3947

40-
BASE_DIR = Path(__file__).parent
41-
LIBZIM_INCLUDE_DIR = "include" # the libzim C++ header src dir (containing zim/*.h)
42-
LIBZIM_LIBRARY_DIR = "lib" # the libzim .so binary lib dir (containing libzim.so)
43-
LIBZIM_DYLIB = "libzim.{ext}".format(
44-
ext="dylib" if platform.system() == "Darwin" else "so"
45-
)
46-
# set PROFILE env to `1` to enable profile info on build (used for coverage reporting)
47-
PROFILE = os.getenv("PROFILE", "") == "1"
48+
base_dir = Path(__file__).parent
4849

50+
# Check if we need profiling (env var PROFILE set to `1`, used for coverage reporting)
51+
compiler_directives = {"language_level": "3"}
52+
if os.getenv("PROFILE", "") == "1":
53+
define_macros = [("CYTHON_TRACE", "1"), ("CYTHON_TRACE_NOGIL", "1")]
54+
compiler_directives.update(linetrace=True)
55+
else:
56+
define_macros = []
4957

50-
class fixed_build_ext(build_ext):
51-
"""Workaround for rpath bug in distutils for OSX."""
58+
if platform.system() == "Darwin":
59+
class fixed_build_ext(build_ext):
60+
"""Workaround for rpath bug in distutils for OSX."""
5261

53-
def finalize_options(self):
54-
super().finalize_options()
55-
# Special treatment of rpath in case of OSX, to work around python
56-
# distutils bug 36353. This constructs proper rpath arguments for clang.
57-
# See https://bugs.python.org/issue36353
58-
if platform.system() == "Darwin":
62+
def finalize_options(self):
63+
super().finalize_options()
64+
# Special treatment of rpath in case of OSX, to work around python
65+
# distutils bug 36353. This constructs proper rpath arguments for clang.
66+
# See https://bugs.python.org/issue36353
5967
for path in self.rpath:
6068
for ext in self.extensions:
6169
ext.extra_link_args.append("-Wl,-rpath," + path)
6270
self.rpath[:] = []
63-
71+
cmdclass={"build_ext": fixed_build_ext}
72+
dyn_lib_ext = "dylib"
73+
else:
74+
cmdclass={"build_ext": build_ext}
75+
dyn_lib_ext = "so"
6476

6577
include_dirs = ["libzim"]
6678
library_dirs = []
6779
# Check for the CPP Libzim library headers in expected directory
68-
if (BASE_DIR / LIBZIM_INCLUDE_DIR / "zim" / "zim.h").exists() and
69-
(BASE_DIR / LIBZIM_LIB_DIR / LIBZIM_DYLIB).exists():
70-
print(
71-
f"Found lizim library and headers in local directory.\n"
72-
f"We will use them to compile python-libzim.\n"
73-
f"Hint : If you don't want to use them (and use \"system\" installed one), remove them."
74-
)
80+
if (base_dir / "include" / "zim" / "zim.h").exists() and (base_dir / "lib" / f"libzim.{dyn_lib_ext}").exists():
81+
print(dedent("""\
82+
Found lizim library and headers in local directory.
83+
We will use them to compile python-libzim.
84+
Hint : If you don't want to use them (and use "system" installed one), remove them.
85+
"""))
7586
include_dirs.append("include")
7687
library_dirs = ["lib"]
7788
else:
7889
# Check for library.
7990
if not find_library("zim"):
80-
print(
81-
"[!] The libzim library cannot be found.\n"
82-
"Please verify that the library is correctly installed of and can be found."
83-
)
91+
print(dedent("""\
92+
"[!] The libzim library cannot be found.
93+
"Please verify that the library is correctly installed of and can be found.
94+
"""))
8495
sys.exit(1)
8596
print("Using system installed library. We are assuming CFLAGS/LDFLAGS are correctly set.")
8697

87-
8898
wrapper_extension = Extension(
8999
name="libzim",
90100
sources=["libzim/libzim.pyx", "libzim/libwrapper.cpp"],
91-
include_dir=include_dirs,
101+
include_dirs=include_dirs,
92102
libraries=["zim"],
93103
library_dirs=library_dirs,
94104
extra_compile_args=["-std=c++11", "-Wall", "-Wextra"],
95105
language="c++",
96-
define_macros=[("CYTHON_TRACE", "1"), ("CYTHON_TRACE_NOGIL", "1")]
97-
if PROFILE
98-
else [],
106+
define_macros=define_macros,
99107
)
100108

101-
compiler_directives = {"language_level": "3"}
102-
if PROFILE:
103-
compiler_directives.update({"linetrace": "True"})
104-
105109
setup(
106110
# Content
107-
cmdclass={"build_ext": fixed_build_ext},
111+
cmdclass=cmdclass,
108112
ext_modules=cythonize([wrapper_extension], compiler_directives=compiler_directives),
109113
)

0 commit comments

Comments
 (0)