Skip to content

Commit 0a89734

Browse files
committed
BLD: Simplify library linking on Windows
1 parent 92044bb commit 0a89734

File tree

5 files changed

+32
-75
lines changed

5 files changed

+32
-75
lines changed

.appveyor.yml

+6-17
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ environment:
4343
CONDA_NPY: "111"
4444
CONDA_INSTALL_LOCN: "C:\\Miniconda36-x64"
4545
TEST_ALL: "no"
46+
MPLSTATICBUILD: True
4647

4748
# We always use a 64-bit machine, but can build x86 distributions
4849
# with the PYTHON_ARCH variable (which is used by CMD_IN_ENV).
@@ -99,17 +100,6 @@ install:
99100
curl -sL https://github.com/python/cpython/pull/1224.patch |
100101
patch -fsup 1 -d %CONDA_PREFIX% ) || ( set errorlevel= )
101102

102-
# Let the install prefer the static builds of the libs
103-
- set LIBRARY_LIB=%CONDA_PREFIX%\Library\lib
104-
- mkdir lib || cmd /c "exit /b 0"
105-
- copy /y %LIBRARY_LIB%\zlibstatic.lib lib\z.lib
106-
- copy /y %LIBRARY_LIB%\libpng_static.lib lib\png.lib
107-
# These z.lib / png.lib are not static versions but files which end up as
108-
# dependencies to the dll file. This is fine for the conda build, but not here
109-
# and for the wheels
110-
- del %LIBRARY_LIB%\png.lib
111-
- del %LIBRARY_LIB%\z.lib
112-
- set MPLBASEDIRLIST=%CONDA_PREFIX%\Library\;.
113103
# enables the local freetype build
114104
- copy ci\travis\setup.cfg .
115105
# Show the installed packages + versions
@@ -120,9 +110,9 @@ test_script:
120110
- '%CMD_IN_ENV% pip install -ve .'
121111
# these should show no z, png, or freetype dll...
122112
- set "DUMPBIN=%VS140COMNTOOLS%\..\..\VC\bin\dumpbin.exe"
123-
- '"%DUMPBIN%" /DEPENDENTS lib\matplotlib\ft2font*.pyd | findstr freetype.*.dll && exit /b 1 || exit /b 0'
124-
- '"%DUMPBIN%" /DEPENDENTS lib\matplotlib\_png*.pyd | findstr z.*.dll && exit /b 1 || exit /b 0'
125-
- '"%DUMPBIN%" /DEPENDENTS lib\matplotlib\_png*.pyd | findstr png.*.dll && exit /b 1 || exit /b 0'
113+
- 'if x%MPLSTATICBUILD% == xTrue "%DUMPBIN%" /DEPENDENTS lib\matplotlib\ft2font*.pyd | findstr freetype.*.dll && exit /b 1 || exit /b 0'
114+
- 'if x%MPLSTATICBUILD% == xTrue "%DUMPBIN%" /DEPENDENTS lib\matplotlib\_png*.pyd | findstr z.*.dll && exit /b 1 || exit /b 0'
115+
- 'if x%MPLSTATICBUILD% == xTrue "%DUMPBIN%" /DEPENDENTS lib\matplotlib\_png*.pyd | findstr png.*.dll && exit /b 1 || exit /b 0'
126116

127117
# this are optional dependencies so that we don't skip so many tests...
128118
- if x%TEST_ALL% == xyes conda install -q ffmpeg inkscape miktex pillow
@@ -144,15 +134,14 @@ after_test:
144134
# After the tests were a success, build packages (wheels and conda)
145135

146136
# Build the wheel with the static libs
137+
- set MPLSTATICBUILD=True
147138
# Hide the output, the copied files really clutter the build log...
148139
- '%CMD_IN_ENV% python setup.py bdist_wheel > NUL:'
149140

150141
# And now the conda build after a cleanup...
151142
# cleanup build files so that they don't pollute the conda build but keep the wheel in dist...
143+
- set MPLSTATICBUILD=
152144
- git clean -xdfq -e dist/
153-
# cleanup the environment so that the test-environment does not leak into the conda build...
154-
- set MPLBASEDIRLIST=
155-
- set LIBRARY_LIB=
156145
- deactivate
157146
- path
158147
- where python

INSTALL.rst

+2-12
Original file line numberDiff line numberDiff line change
@@ -332,22 +332,12 @@ without fiddling with environment variables::
332332
# for Python 2.7
333333
conda install -c conda-forge backports.functools_lru_cache
334334

335-
# copy the libs which have "wrong" names
336-
set LIBRARY_LIB=%CONDA_DEFAULT_ENV%\Library\lib
337-
mkdir lib || cmd /c "exit /b 0"
338-
copy %LIBRARY_LIB%\zlibstatic.lib lib\z.lib
339-
copy %LIBRARY_LIB%\libpng_static.lib lib\png.lib
340-
341-
# Make the header files and the rest of the static libs available during the build
342-
# CONDA_DEFAULT_ENV is a env variable which is set to the currently active environment path
343-
set MPLBASEDIRLIST=%CONDA_DEFAULT_ENV%\Library\;.
335+
# force the build against static libpng and zlib libraries
336+
set MPLSTATICBUILD=True
344337

345338
# build the wheel
346339
python setup.py bdist_wheel
347340

348-
The `build_alllocal.cmd` script in the root folder automates these steps if
349-
you have already created and activated the conda environment.
350-
351341

352342
Conda packages
353343
^^^^^^^^^^^^^^

build_alllocal.cmd

-36
This file was deleted.

ci/conda_recipe/bld.bat

-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
set LIBPATH=%LIBRARY_LIB%;
2-
set INCLUDE=%INCLUDE%;%PREFIX%\Library\include\freetype2
3-
41
ECHO [directories] > setup.cfg
52
ECHO basedirlist = %LIBRARY_PREFIX% >> setup.cfg
63
ECHO [packages] >> setup.cfg
74
ECHO tests = False >> setup.cfg
85
ECHO sample_data = False >> setup.cfg
96
ECHO toolkits_tests = False >> setup.cfg
107

11-
@rem workaround for https://github.com/matplotlib/matplotlib/issues/6460
12-
@rem see also https://github.com/conda-forge/libpng-feedstock/pull/4
13-
copy /y %LIBRARY_LIB%\libpng16.lib %LIBRARY_LIB%\png.lib
14-
158
%PYTHON% setup.py install --single-version-externally-managed --record=record.txt
169
if errorlevel 1 exit 1

setupext.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ def _get_xdg_cache_dir():
8888
'display_status': True,
8989
'verbose': False,
9090
'backend': None,
91-
'basedirlist': None
91+
'basedirlist': None,
92+
'staticbuild': False,
9293
}
9394

9495

@@ -119,6 +120,9 @@ def _get_xdg_cache_dir():
119120
lft = bool(os.environ.get('MPLLOCALFREETYPE', False))
120121
options['local_freetype'] = lft or options.get('local_freetype', False)
121122

123+
staticbuild = bool(os.environ.get('MPLSTATICBUILD', True))
124+
options['staticbuild'] = staticbuild or options.get('staticbuild', False)
125+
122126

123127
def get_win32_compiler():
124128
"""
@@ -307,6 +311,23 @@ def get_file_hash(filename):
307311
return hasher.hexdigest()
308312

309313

314+
def deplib(libname):
315+
if sys.platform != 'win32':
316+
return libname
317+
318+
known_libs = {
319+
# TODO: support versioned libpng on build system revrite
320+
'png': ('libpng', '_static'),
321+
'z': ('zlib', 'static'),
322+
}
323+
324+
libname, static_postfix = known_libs[libname]
325+
if options['staticbuild']:
326+
libname += static_postfix
327+
328+
return libname
329+
330+
310331
class PkgConfig(object):
311332
"""
312333
This is a class for communicating with pkg-config.
@@ -1122,7 +1143,7 @@ def add_flags(self, ext):
11221143
'lib/freetype2/include/freetype2'],
11231144
default_library_dirs=[
11241145
'freetype2/lib'],
1125-
default_libraries=['freetype', 'z'])
1146+
default_libraries=['freetype', deplib('z')])
11261147
ext.define_macros.append(('FREETYPE_BUILD_TYPE', 'system'))
11271148

11281149
def do_custom_build(self):
@@ -1320,7 +1341,7 @@ def get_extension(self):
13201341
]
13211342
ext = make_extension('matplotlib._png', sources)
13221343
pkg_config.setup_extension(
1323-
ext, 'libpng', default_libraries=['png', 'z'],
1344+
ext, 'libpng', default_libraries=map(deplib, ['png', 'z']),
13241345
alt_exec='libpng-config --ldflags')
13251346
Numpy().add_flags(ext)
13261347
return ext

0 commit comments

Comments
 (0)