Skip to content

Commit f7bc8a4

Browse files
committed
Statically linking zlib and libpng
1 parent 2bd60de commit f7bc8a4

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

setup.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ def get_extensions():
9393
extra_compile_args = {}
9494
third_party_search_directories = []
9595

96-
97-
runtime_library_dirs = None
96+
extra_objects = []
9897
if sys.platform.startswith('linux'):
9998
sources = sources + source_image_cpu
10099
libraries.append('png')
101100
third_party_search_directories.append(os.path.join(cwd, "third_party/libpng"))
102101
runtime_library_dirs = ['lib']
102+
extra_objects = ['third_party/zlib/libz.a', 'third_party/libpng/libpng.a']
103103

104104
extension = CppExtension
105105

@@ -117,7 +117,6 @@ def get_extensions():
117117

118118
define_macros = []
119119

120-
extra_compile_args = {}
121120
if (torch.cuda.is_available() and CUDA_HOME is not None) or os.getenv('FORCE_CUDA', '0') == '1':
122121
extension = CUDAExtension
123122
sources += source_cuda
@@ -162,7 +161,7 @@ def get_extensions():
162161
include_dirs=include_dirs + third_party_search_directories,
163162
define_macros=define_macros,
164163
extra_compile_args=extra_compile_args,
165-
runtime_library_dirs=runtime_library_dirs
164+
extra_objects=extra_objects
166165
)
167166
]
168167
if compile_cpp_tests:
@@ -231,9 +230,9 @@ def build_deps():
231230
os.chdir(this_dir)
232231

233232
zlib_path = os.path.join(this_dir, "third_party/zlib")
234-
libpng_cmake_options = "-DPNG_BUILD_ZLIB=ON -DPNG_STATIC=OFF -DZLIB_INCLUDE_DIR:PATH={zlib_path} -DZLIB_LIBRARY:FILEPATH={zlib_path}/libz.so".format(zlib_path=zlib_path)
233+
libpng_cmake_options = "-DPNG_BUILD_ZLIB=ON -DPNG_STATIC=ON -DZLIB_INCLUDE_DIR:PATH={zlib_path} -DZLIB_LIBRARY:FILEPATH={zlib_path}/libz.so".format(zlib_path=zlib_path)
235234
os.chdir("third_party/libpng/")
236-
os.system('cmake {} .'.format(libpng_cmake_options))
235+
os.system('cmake -DCMAKE_C_FLAGS="-fPIC" {} .'.format(libpng_cmake_options))
237236
throw_of_failure("cmake --build . -- -j {}".format(cpu_count))
238237
os.chdir(this_dir)
239238

@@ -243,15 +242,6 @@ def build_ext_with_dependencies(self):
243242
return BuildExtension.with_options(no_python_abi_suffix=True)(self)
244243

245244

246-
data_files = []
247-
if sys.platform.startswith('linux'):
248-
data_files = [
249-
('torchvision/lib', [
250-
'third_party/zlib/libz.so',
251-
'third_party/libpng/libpng.so'])
252-
]
253-
254-
255245
setup(
256246
# Metadata
257247
name=package_name,
@@ -276,5 +266,4 @@ def build_ext_with_dependencies(self):
276266
'build_ext': build_ext_with_dependencies,
277267
'clean': clean,
278268
},
279-
data_files=data_files
280269
)

0 commit comments

Comments
 (0)