8 import os |
8 import os |
9 |
9 |
10 from distutils.extension import Extension |
10 from distutils.extension import Extension |
11 |
11 |
12 |
12 |
13 zstd_sources = ['zstd/%s' % p for p in ( |
13 zstd_sources = [ |
14 'common/debug.c', |
14 "zstd/%s" % p |
15 'common/entropy_common.c', |
15 for p in ( |
16 'common/error_private.c', |
16 "common/debug.c", |
17 'common/fse_decompress.c', |
17 "common/entropy_common.c", |
18 'common/pool.c', |
18 "common/error_private.c", |
19 'common/threading.c', |
19 "common/fse_decompress.c", |
20 'common/xxhash.c', |
20 "common/pool.c", |
21 'common/zstd_common.c', |
21 "common/threading.c", |
22 'compress/fse_compress.c', |
22 "common/xxhash.c", |
23 'compress/hist.c', |
23 "common/zstd_common.c", |
24 'compress/huf_compress.c', |
24 "compress/fse_compress.c", |
25 'compress/zstd_compress_literals.c', |
25 "compress/hist.c", |
26 'compress/zstd_compress_sequences.c', |
26 "compress/huf_compress.c", |
27 'compress/zstd_compress.c', |
27 "compress/zstd_compress_literals.c", |
28 'compress/zstd_double_fast.c', |
28 "compress/zstd_compress_sequences.c", |
29 'compress/zstd_fast.c', |
29 "compress/zstd_compress.c", |
30 'compress/zstd_lazy.c', |
30 "compress/zstd_double_fast.c", |
31 'compress/zstd_ldm.c', |
31 "compress/zstd_fast.c", |
32 'compress/zstd_opt.c', |
32 "compress/zstd_lazy.c", |
33 'compress/zstdmt_compress.c', |
33 "compress/zstd_ldm.c", |
34 'decompress/huf_decompress.c', |
34 "compress/zstd_opt.c", |
35 'decompress/zstd_ddict.c', |
35 "compress/zstdmt_compress.c", |
36 'decompress/zstd_decompress.c', |
36 "decompress/huf_decompress.c", |
37 'decompress/zstd_decompress_block.c', |
37 "decompress/zstd_ddict.c", |
38 'dictBuilder/cover.c', |
38 "decompress/zstd_decompress.c", |
39 'dictBuilder/divsufsort.c', |
39 "decompress/zstd_decompress_block.c", |
40 'dictBuilder/fastcover.c', |
40 "dictBuilder/cover.c", |
41 'dictBuilder/zdict.c', |
41 "dictBuilder/divsufsort.c", |
42 )] |
42 "dictBuilder/fastcover.c", |
43 |
43 "dictBuilder/zdict.c", |
44 zstd_sources_legacy = ['zstd/%s' % p for p in ( |
44 ) |
45 'deprecated/zbuff_common.c', |
45 ] |
46 'deprecated/zbuff_compress.c', |
46 |
47 'deprecated/zbuff_decompress.c', |
47 zstd_sources_legacy = [ |
48 'legacy/zstd_v01.c', |
48 "zstd/%s" % p |
49 'legacy/zstd_v02.c', |
49 for p in ( |
50 'legacy/zstd_v03.c', |
50 "deprecated/zbuff_common.c", |
51 'legacy/zstd_v04.c', |
51 "deprecated/zbuff_compress.c", |
52 'legacy/zstd_v05.c', |
52 "deprecated/zbuff_decompress.c", |
53 'legacy/zstd_v06.c', |
53 "legacy/zstd_v01.c", |
54 'legacy/zstd_v07.c' |
54 "legacy/zstd_v02.c", |
55 )] |
55 "legacy/zstd_v03.c", |
|
56 "legacy/zstd_v04.c", |
|
57 "legacy/zstd_v05.c", |
|
58 "legacy/zstd_v06.c", |
|
59 "legacy/zstd_v07.c", |
|
60 ) |
|
61 ] |
56 |
62 |
57 zstd_includes = [ |
63 zstd_includes = [ |
58 'zstd', |
64 "zstd", |
59 'zstd/common', |
65 "zstd/common", |
60 'zstd/compress', |
66 "zstd/compress", |
61 'zstd/decompress', |
67 "zstd/decompress", |
62 'zstd/dictBuilder', |
68 "zstd/dictBuilder", |
63 ] |
69 ] |
64 |
70 |
65 zstd_includes_legacy = [ |
71 zstd_includes_legacy = [ |
66 'zstd/deprecated', |
72 "zstd/deprecated", |
67 'zstd/legacy', |
73 "zstd/legacy", |
68 ] |
74 ] |
69 |
75 |
70 ext_includes = [ |
76 ext_includes = [ |
71 'c-ext', |
77 "c-ext", |
72 'zstd/common', |
78 "zstd/common", |
73 ] |
79 ] |
74 |
80 |
75 ext_sources = [ |
81 ext_sources = [ |
76 'zstd/common/pool.c', |
82 "zstd/common/error_private.c", |
77 'zstd/common/threading.c', |
83 "zstd/common/pool.c", |
78 'zstd.c', |
84 "zstd/common/threading.c", |
79 'c-ext/bufferutil.c', |
85 "zstd/common/zstd_common.c", |
80 'c-ext/compressiondict.c', |
86 "zstd.c", |
81 'c-ext/compressobj.c', |
87 "c-ext/bufferutil.c", |
82 'c-ext/compressor.c', |
88 "c-ext/compressiondict.c", |
83 'c-ext/compressoriterator.c', |
89 "c-ext/compressobj.c", |
84 'c-ext/compressionchunker.c', |
90 "c-ext/compressor.c", |
85 'c-ext/compressionparams.c', |
91 "c-ext/compressoriterator.c", |
86 'c-ext/compressionreader.c', |
92 "c-ext/compressionchunker.c", |
87 'c-ext/compressionwriter.c', |
93 "c-ext/compressionparams.c", |
88 'c-ext/constants.c', |
94 "c-ext/compressionreader.c", |
89 'c-ext/decompressobj.c', |
95 "c-ext/compressionwriter.c", |
90 'c-ext/decompressor.c', |
96 "c-ext/constants.c", |
91 'c-ext/decompressoriterator.c', |
97 "c-ext/decompressobj.c", |
92 'c-ext/decompressionreader.c', |
98 "c-ext/decompressor.c", |
93 'c-ext/decompressionwriter.c', |
99 "c-ext/decompressoriterator.c", |
94 'c-ext/frameparams.c', |
100 "c-ext/decompressionreader.c", |
|
101 "c-ext/decompressionwriter.c", |
|
102 "c-ext/frameparams.c", |
95 ] |
103 ] |
96 |
104 |
97 zstd_depends = [ |
105 zstd_depends = [ |
98 'c-ext/python-zstandard.h', |
106 "c-ext/python-zstandard.h", |
99 ] |
107 ] |
100 |
108 |
101 |
109 |
102 def get_c_extension(support_legacy=False, system_zstd=False, name='zstd', |
110 def get_c_extension( |
103 warnings_as_errors=False, root=None): |
111 support_legacy=False, |
|
112 system_zstd=False, |
|
113 name="zstd", |
|
114 warnings_as_errors=False, |
|
115 root=None, |
|
116 ): |
104 """Obtain a distutils.extension.Extension for the C extension. |
117 """Obtain a distutils.extension.Extension for the C extension. |
105 |
118 |
106 ``support_legacy`` controls whether to compile in legacy zstd format support. |
119 ``support_legacy`` controls whether to compile in legacy zstd format support. |
107 |
120 |
108 ``system_zstd`` controls whether to compile against the system zstd library. |
121 ``system_zstd`` controls whether to compile against the system zstd library. |
123 |
136 |
124 sources = set([os.path.join(actual_root, p) for p in ext_sources]) |
137 sources = set([os.path.join(actual_root, p) for p in ext_sources]) |
125 if not system_zstd: |
138 if not system_zstd: |
126 sources.update([os.path.join(actual_root, p) for p in zstd_sources]) |
139 sources.update([os.path.join(actual_root, p) for p in zstd_sources]) |
127 if support_legacy: |
140 if support_legacy: |
128 sources.update([os.path.join(actual_root, p) |
141 sources.update([os.path.join(actual_root, p) for p in zstd_sources_legacy]) |
129 for p in zstd_sources_legacy]) |
|
130 sources = list(sources) |
142 sources = list(sources) |
131 |
143 |
132 include_dirs = set([os.path.join(actual_root, d) for d in ext_includes]) |
144 include_dirs = set([os.path.join(actual_root, d) for d in ext_includes]) |
133 if not system_zstd: |
145 if not system_zstd: |
134 include_dirs.update([os.path.join(actual_root, d) |
146 include_dirs.update([os.path.join(actual_root, d) for d in zstd_includes]) |
135 for d in zstd_includes]) |
|
136 if support_legacy: |
147 if support_legacy: |
137 include_dirs.update([os.path.join(actual_root, d) |
148 include_dirs.update( |
138 for d in zstd_includes_legacy]) |
149 [os.path.join(actual_root, d) for d in zstd_includes_legacy] |
|
150 ) |
139 include_dirs = list(include_dirs) |
151 include_dirs = list(include_dirs) |
140 |
152 |
141 depends = [os.path.join(actual_root, p) for p in zstd_depends] |
153 depends = [os.path.join(actual_root, p) for p in zstd_depends] |
142 |
154 |
143 compiler = distutils.ccompiler.new_compiler() |
155 compiler = distutils.ccompiler.new_compiler() |
144 |
156 |
145 # Needed for MSVC. |
157 # Needed for MSVC. |
146 if hasattr(compiler, 'initialize'): |
158 if hasattr(compiler, "initialize"): |
147 compiler.initialize() |
159 compiler.initialize() |
148 |
160 |
149 if compiler.compiler_type == 'unix': |
161 if compiler.compiler_type == "unix": |
150 compiler_type = 'unix' |
162 compiler_type = "unix" |
151 elif compiler.compiler_type == 'msvc': |
163 elif compiler.compiler_type == "msvc": |
152 compiler_type = 'msvc' |
164 compiler_type = "msvc" |
153 elif compiler.compiler_type == 'mingw32': |
165 elif compiler.compiler_type == "mingw32": |
154 compiler_type = 'mingw32' |
166 compiler_type = "mingw32" |
155 else: |
167 else: |
156 raise Exception('unhandled compiler type: %s' % |
168 raise Exception("unhandled compiler type: %s" % compiler.compiler_type) |
157 compiler.compiler_type) |
169 |
158 |
170 extra_args = ["-DZSTD_MULTITHREAD"] |
159 extra_args = ['-DZSTD_MULTITHREAD'] |
|
160 |
171 |
161 if not system_zstd: |
172 if not system_zstd: |
162 extra_args.append('-DZSTDLIB_VISIBILITY=') |
173 extra_args.append("-DZSTDLIB_VISIBILITY=") |
163 extra_args.append('-DZDICTLIB_VISIBILITY=') |
174 extra_args.append("-DZDICTLIB_VISIBILITY=") |
164 extra_args.append('-DZSTDERRORLIB_VISIBILITY=') |
175 extra_args.append("-DZSTDERRORLIB_VISIBILITY=") |
165 |
176 |
166 if compiler_type == 'unix': |
177 if compiler_type == "unix": |
167 extra_args.append('-fvisibility=hidden') |
178 extra_args.append("-fvisibility=hidden") |
168 |
179 |
169 if not system_zstd and support_legacy: |
180 if not system_zstd and support_legacy: |
170 extra_args.append('-DZSTD_LEGACY_SUPPORT=1') |
181 extra_args.append("-DZSTD_LEGACY_SUPPORT=1") |
171 |
182 |
172 if warnings_as_errors: |
183 if warnings_as_errors: |
173 if compiler_type in ('unix', 'mingw32'): |
184 if compiler_type in ("unix", "mingw32"): |
174 extra_args.append('-Werror') |
185 extra_args.append("-Werror") |
175 elif compiler_type == 'msvc': |
186 elif compiler_type == "msvc": |
176 extra_args.append('/WX') |
187 extra_args.append("/WX") |
177 else: |
188 else: |
178 assert False |
189 assert False |
179 |
190 |
180 libraries = ['zstd'] if system_zstd else [] |
191 libraries = ["zstd"] if system_zstd else [] |
181 |
192 |
182 # Python 3.7 doesn't like absolute paths. So normalize to relative. |
193 # Python 3.7 doesn't like absolute paths. So normalize to relative. |
183 sources = [os.path.relpath(p, root) for p in sources] |
194 sources = [os.path.relpath(p, root) for p in sources] |
184 include_dirs = [os.path.relpath(p, root) for p in include_dirs] |
195 include_dirs = [os.path.relpath(p, root) for p in include_dirs] |
185 depends = [os.path.relpath(p, root) for p in depends] |
196 depends = [os.path.relpath(p, root) for p in depends] |
186 |
197 |
187 # TODO compile with optimizations. |
198 # TODO compile with optimizations. |
188 return Extension(name, sources, |
199 return Extension( |
189 include_dirs=include_dirs, |
200 name, |
190 depends=depends, |
201 sources, |
191 extra_compile_args=extra_args, |
202 include_dirs=include_dirs, |
192 libraries=libraries) |
203 depends=depends, |
|
204 extra_compile_args=extra_args, |
|
205 libraries=libraries, |
|
206 ) |