|
15 | 15 |
|
16 | 16 | To compile or run this project, you must first get the libzim headers & binary: |
17 | 17 |
|
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): |
22 | 20 | https://download.openzim.org/release/libzim/ |
| 21 | + - Installed a packaged version of libzim : |
| 22 | + . `apt-get install libzim-devel` |
| 23 | + . `dnf install libzim-dev` |
23 | 24 |
|
24 | 25 | Either place the `libzim.so` and `zim/*.h` files in `./lib/` and `./include/`, |
25 | 26 | or set these environment variables to use custom libzim header and dylib paths: |
26 | 27 |
|
27 | 28 | $ export CFLAGS="-I/tmp/libzim_linux-x86_64-6.1.1/include" |
28 | 29 | $ export LDFLAGS="-L/tmp/libzim_linux-x86_64-6.1.1/lib/x86_64-linux-gnu" |
29 | 30 | $ 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. |
30 | 34 | """ |
| 35 | + |
| 36 | + |
31 | 37 | import os |
| 38 | +import sys |
32 | 39 | import platform |
33 | 40 | from pathlib import Path |
34 | 41 | from ctypes.util import find_library |
| 42 | +from textwrap import dedent |
35 | 43 |
|
36 | 44 | from setuptools import setup, Extension |
37 | 45 | from Cython.Build import cythonize |
38 | 46 | from Cython.Distutils.build_ext import new_build_ext as build_ext |
39 | 47 |
|
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 |
48 | 49 |
|
| 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 = [] |
49 | 57 |
|
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.""" |
52 | 61 |
|
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 |
59 | 67 | for path in self.rpath: |
60 | 68 | for ext in self.extensions: |
61 | 69 | ext.extra_link_args.append("-Wl,-rpath," + path) |
62 | 70 | 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" |
64 | 76 |
|
65 | 77 | include_dirs = ["libzim"] |
66 | 78 | library_dirs = [] |
67 | 79 | # 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 | + """)) |
75 | 86 | include_dirs.append("include") |
76 | 87 | library_dirs = ["lib"] |
77 | 88 | else: |
78 | 89 | # Check for library. |
79 | 90 | 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 | + """)) |
84 | 95 | sys.exit(1) |
85 | 96 | print("Using system installed library. We are assuming CFLAGS/LDFLAGS are correctly set.") |
86 | 97 |
|
87 | | - |
88 | 98 | wrapper_extension = Extension( |
89 | 99 | name="libzim", |
90 | 100 | sources=["libzim/libzim.pyx", "libzim/libwrapper.cpp"], |
91 | | - include_dir=include_dirs, |
| 101 | + include_dirs=include_dirs, |
92 | 102 | libraries=["zim"], |
93 | 103 | library_dirs=library_dirs, |
94 | 104 | extra_compile_args=["-std=c++11", "-Wall", "-Wextra"], |
95 | 105 | language="c++", |
96 | | - define_macros=[("CYTHON_TRACE", "1"), ("CYTHON_TRACE_NOGIL", "1")] |
97 | | - if PROFILE |
98 | | - else [], |
| 106 | + define_macros=define_macros, |
99 | 107 | ) |
100 | 108 |
|
101 | | -compiler_directives = {"language_level": "3"} |
102 | | -if PROFILE: |
103 | | - compiler_directives.update({"linetrace": "True"}) |
104 | | - |
105 | 109 | setup( |
106 | 110 | # Content |
107 | | - cmdclass={"build_ext": fixed_build_ext}, |
| 111 | + cmdclass=cmdclass, |
108 | 112 | ext_modules=cythonize([wrapper_extension], compiler_directives=compiler_directives), |
109 | 113 | ) |
0 commit comments