4343from Cython .Distutils .build_ext import new_build_ext as build_ext
4444from setuptools import Extension , setup
4545
46+ # Avoid running cythonize on `setup.py clean` and similar
47+ SKIP_BUILD_CMDS = ["clean" , "--help" , "egg_info" , "--version" ]
48+
4649base_dir = Path (__file__ ).parent
4750
4851# Check if we need profiling (env var PROFILE set to `1`, used for coverage reporting)
@@ -69,51 +72,58 @@ def finalize_options(self):
6972 self .rpath [:] = []
7073
7174 cmdclass = {"build_ext" : fixed_build_ext }
72- dyn_lib_ext = "dylib"
7375else :
7476 cmdclass = {"build_ext" : build_ext }
75- dyn_lib_ext = "so"
76-
77- include_dirs = ["libzim" ]
78- library_dirs = []
79- # Check for the CPP Libzim library headers in expected directory
80- header_file = base_dir / "include" / "zim" / "zim.h"
81- lib_file = base_dir / "lib" / f"libzim.{ dyn_lib_ext } "
82- if header_file .exists () and lib_file .exists ():
83- print (
84- "Found lizim library and headers in local directory. "
85- "We will use them to compile python-libzim.\n "
86- "Hint : If you don't want to use them "
87- "(and use “system” installed one), remove them."
88- )
89- include_dirs .append ("include" )
90- library_dirs = ["lib" ]
91- else :
92- # Check for library.
93- if not find_library ("zim" ):
77+
78+
79+ def cython_ext_module ():
80+ dyn_lib_ext = "dylib" if platform .system () == "Darwin" else "so"
81+ include_dirs = ["libzim" ]
82+ library_dirs = []
83+ # Check for the CPP Libzim library headers in expected directory
84+ header_file = base_dir / "include" / "zim" / "zim.h"
85+ lib_file = base_dir / "lib" / f"libzim.{ dyn_lib_ext } "
86+ if header_file .exists () and lib_file .exists ():
9487 print (
95- "[!] The libzim library cannot be found.\n "
96- "Please verify that the library is correctly installed and can be found."
88+ "Found lizim library and headers in local directory. "
89+ "Will use them to compile python-libzim.\n "
90+ "Hint : If you don't want to use them "
91+ "(and use “system” installed one), remove them."
9792 )
98- sys .exit (1 )
99- print (
100- "Using system installed library. "
101- "We are assuming CFLAGS/LDFLAGS are correctly set."
93+ include_dirs .append ("include" )
94+ library_dirs = ["lib" ]
95+ elif "clean" not in sys .argv :
96+ # Check for library.
97+ if not find_library ("zim" ):
98+ print (
99+ "[!] The libzim library cannot be found.\n "
100+ "Please verify it is correctly installed and can be found."
101+ )
102+ sys .exit (1 )
103+ print (
104+ "Using system installed library; Assuming CFLAGS/LDFLAGS are correctly set."
105+ )
106+
107+ wrapper_extension = Extension (
108+ name = "libzim" ,
109+ sources = ["libzim/libzim.pyx" , "libzim/libwrapper.cpp" ],
110+ include_dirs = include_dirs ,
111+ libraries = ["zim" ],
112+ library_dirs = library_dirs ,
113+ extra_compile_args = ["-std=c++11" , "-Wall" , "-Wextra" ],
114+ language = "c++" ,
115+ define_macros = define_macros ,
102116 )
117+ return cythonize ([wrapper_extension ], compiler_directives = compiler_directives )
103118
104- wrapper_extension = Extension (
105- name = "libzim" ,
106- sources = ["libzim/libzim.pyx" , "libzim/libwrapper.cpp" ],
107- include_dirs = include_dirs ,
108- libraries = ["zim" ],
109- library_dirs = library_dirs ,
110- extra_compile_args = ["-std=c++11" , "-Wall" , "-Wextra" ],
111- language = "c++" ,
112- define_macros = define_macros ,
113- )
119+
120+ if len (sys .argv ) == 2 and sys .argv [1 ] in SKIP_BUILD_CMDS :
121+ ext_modules = None
122+ else :
123+ ext_modules = cython_ext_module ()
114124
115125setup (
116126 # Content
117127 cmdclass = cmdclass ,
118- ext_modules = cythonize ([ wrapper_extension ], compiler_directives = compiler_directives ) ,
128+ ext_modules = ext_modules ,
119129)
0 commit comments