1+ """
2+ This file extracts and builds library dependencies libpng, zlib, & freetype2 on
3+ MS Windows. It also extract tcl/tk for the header files.
4+
5+ There are four possible build targets -- one for each permutation of VS 2008,
6+ 2010 and 32/64 bit. This builds the configuration that matches the Python
7+ install that is executing.
8+
9+ For Python 2.6, 2.7, 3.2:
10+
11+ - VS 2008, 32 bit -- Windows SDK v7.0
12+ - VS 2008, 64 bit -- Windows SDK v7.0
13+
14+ For Python 3.3, 3.4:
15+
16+ - VS 2010, 32 bit -- Windows SDK v7.1
17+ - VS 2010, 64 bit -- Windows SDK v7.1
18+ """
19+
20+ from __future__ import print_function , absolute_import
21+ import sys
22+ import platform
23+ import os
24+ import glob
25+ import shutil
26+ import zipfile
27+ import tarfile
28+ import distutils .msvc9compiler as msvc
29+
30+ def fixproj (project_file , bit_target ):
31+ """
32+ :param bit_target: one of 'Win32' or 'x64'
33+ """
34+ with open (project_file , 'r' ) as fd :
35+ content = '\n ' .join (line .strip () for line in fd if line .strip ())
36+ content = content .replace ('Win32' , bit_target ).replace ('x64' , bit_target )
37+ with open (project_file , 'w' ) as fd :
38+ fd .write (content )
39+
40+ def tar_extract (tar_file , target ):
41+ with tarfile .open (tar_file , 'r:gz' ) as tgz :
42+ tgz .extractall (target )
43+
44+ def zip_extract (zip_file , target ):
45+ with zipfile .ZipFile (zip_file ) as zf :
46+ zf .extractall (target )
47+
48+ # Configuration selection & declaration:
49+ DEPSSRC = os .path .join (os .path .dirname (os .path .normpath (__file__ )), 'deps_source' )
50+ DEPSBUILD = os .path .join (os .path .dirname (os .path .normpath (__file__ )), 'build' )
51+ X64 = platform .architecture ()[0 ] == '64bit'
52+ PYVER = sys .version_info [:2 ]
53+ VS2010 = PYVER >= (3 , 3 )
54+ # If not VS2010, then use VS2008
55+
56+ VCVARSALL = None
57+
58+ def prepare_build_cmd (build_cmd , ** kwargs ):
59+ global VCVARSALL
60+ if VCVARSALL == None :
61+ candidate = msvc .find_vcvarsall (10.0 if VS2010 else 9.0 )
62+ if candidate == None :
63+ raise RuntimeError ('Microsoft VS {} required' .format ('2010' if VS2010 else '2008' ))
64+ else :
65+ VCVARSALL = candidate
66+
67+ return build_cmd .format (vcvarsall = VCVARSALL , xXX = 'x64' if X64 else 'x86' , ** kwargs )
68+
69+ def config_dir ():
70+ segment = 'msvcr{}-x{}' .format ('100' if VS2010 else '90' , '64' if X64 else '32' )
71+ return os .path .join (DEPSBUILD , segment )
72+
73+ def tcl_config_dir ():
74+ return os .path .join (config_dir (), 'tcl85' , 'include' )
75+
76+ def build_tcl ():
77+ inclib = config_dir ()
78+ tcl_inclib = tcl_config_dir ()
79+ if not os .path .exists (tcl_inclib ):
80+ os .makedirs (tcl_inclib )
81+ tcl_inclib_x11 = os .path .join (tcl_inclib , 'X11' )
82+ if not os .path .exists (tcl_inclib_x11 ):
83+ os .makedirs (tcl_inclib_x11 )
84+
85+ distfile = os .path .join (DEPSSRC , 'tcl8513-src.zip' )
86+ compfile = os .path .join (tcl_inclib , 'tcl.h' )
87+ if not os .path .exists (compfile ) or os .path .getmtime (distfile ) > os .path .getmtime (compfile ):
88+ zip_extract (distfile , DEPSBUILD )
89+ targetdir = os .path .join (DEPSBUILD , 'tcl8.5.13' )
90+ headers = glob .glob (os .path .join (targetdir , 'generic' , '*.h' ))
91+ for filename in headers :
92+ shutil .copy (filename , tcl_inclib )
93+
94+ distfile = os .path .join (DEPSSRC , 'tk8513-src.zip' )
95+ compfile = os .path .join (tcl_inclib , 'tk.h' )
96+ if not os .path .exists (compfile ) or os .path .getmtime (distfile ) > os .path .getmtime (compfile ):
97+ zip_extract (distfile , DEPSBUILD )
98+ targetdir = os .path .join (DEPSBUILD , 'tk8.5.13' )
99+ headers = glob .glob (os .path .join (targetdir , 'generic' , '*.h' ))
100+ for filename in headers :
101+ shutil .copy (filename , tcl_inclib )
102+ headers = glob .glob (os .path .join (targetdir , 'xlib' , 'X11' , '*.*' ))
103+ for filename in headers :
104+ shutil .copy (filename , tcl_inclib_x11 )
105+
106+ ZLIB_BUILD_CMD = """\
107+ @ECHO OFF
108+ REM call "%ProgramFiles%\\ Microsoft SDKs\\ Windows\\ v7.0\\ Bin\\ SetEnv.Cmd" /Release /{xXX} /xp
109+ call "{vcvarsall}" {xXX}
110+
111+ cd /D %ZLIB%
112+ nmake -f win32\\ Makefile.msc clean
113+ nmake -f win32\\ Makefile.msc
114+ copy /Y /B *.dll %INCLIB%
115+ copy /Y /B *.lib %INCLIB%
116+ copy /Y /B zlib.lib %INCLIB%\\ z.lib
117+ copy /Y /B zlib.h %INCLIB%
118+ copy /Y /B zconf.h %INCLIB%
119+ """
120+
121+ def build_zlib ():
122+ inclib = config_dir ()
123+ if not os .path .exists (inclib ):
124+ os .makedirs (inclib )
125+
126+ distfile = os .path .join (DEPSSRC , 'zlib128.zip' )
127+ compfile = os .path .join (inclib , 'z.lib' )
128+ if os .path .exists (compfile ) and os .path .getmtime (distfile ) < os .path .getmtime (compfile ):
129+ # already built
130+ return
131+
132+ zip_extract (distfile , DEPSBUILD )
133+
134+ cmdfile = os .path .join (DEPSBUILD , 'build_zlib.cmd' )
135+ with open (cmdfile , 'w' ) as cmd :
136+ cmd .write (prepare_build_cmd (ZLIB_BUILD_CMD ))
137+
138+ os .environ ['INCLIB' ] = inclib
139+ os .environ ['ZLIB' ] = os .path .join (DEPSBUILD , 'zlib-1.2.8' )
140+ os .system (cmdfile )
141+
142+ LIBPNG_BUILD_CMD = """\
143+ @ECHO OFF
144+ REM call "%ProgramFiles%\\ Microsoft SDKs\\ Windows\\ v7.0\\ Bin\\ SetEnv.Cmd" /Release /{xXX} /xp
145+ call "{vcvarsall}" {xXX}
146+ set CMAKE="cmake.exe"
147+
148+ set BUILDDIR=%LIBPNG%-build
149+ rd /S /Q %BUILDDIR%
150+ %CMAKE% -G"NMake Makefiles" -H%LIBPNG% -B%BUILDDIR% ^
151+ -DCMAKE_BUILD_TYPE=Release ^
152+ -DZLIB_INCLUDE_DIR=%INCLIB% ^
153+ -DZLIB_LIBRARY:FILEPATH=%INCLIB%\\ zlib.lib ^
154+ -DPNG_STATIC=ON ^
155+ -DPNG_SHARED=OFF
156+ copy /Y /B %BUILDDIR%\\ pnglibconf.h %INCLIB%
157+ copy /Y /B %LIBPNG%\\ png.h %INCLIB%
158+ copy /Y /B %LIBPNG%\\ pngconf.h %INCLIB%
159+ cd %BUILDDIR%
160+ nmake -f Makefile
161+ REM It's a static lib -- no *.dll in sight!
162+ REM copy /Y /B *.dll %INCLIB%
163+ copy /Y /B *.lib %INCLIB%
164+ copy /Y /B libpng16_static.lib %INCLIB%\\ png.lib
165+ """
166+
167+ def build_libpng ():
168+ inclib = config_dir ()
169+ if not os .path .exists (inclib ):
170+ os .mkdir (inclib )
171+
172+ distfile = os .path .join (DEPSSRC , 'libpng-1.6.7.tar.gz' )
173+ compfile = os .path .join (inclib , 'png.lib' )
174+ if os .path .exists (compfile ) and os .path .getmtime (distfile ) < os .path .getmtime (compfile ):
175+ # already built
176+ return
177+
178+ tar_extract (distfile , DEPSBUILD )
179+
180+ cmdfile = os .path .join (DEPSBUILD , 'build_libpng.cmd' )
181+ with open (cmdfile , 'w' ) as cmd :
182+ cmd .write (prepare_build_cmd (LIBPNG_BUILD_CMD ))
183+
184+ os .environ ['INCLIB' ] = inclib
185+ os .environ ['LIBPNG' ] = os .path .join (DEPSBUILD , 'libpng-1.6.7' )
186+ os .system (cmdfile )
187+
188+ FREETYPE_VERSION = '2.4.11'
189+
190+ FREETYPE_BUILD_CMD = """\
191+ @ECHO OFF
192+ REM call "%ProgramFiles%\\ Microsoft SDKs\\ Windows\\ v7.0\\ Bin\\ SetEnv.Cmd" /Release /{xXX} /xp
193+ call "{vcvarsall}" {xXX}
194+ set MSBUILD=C:\\ Windows\\ Microsoft.NET\\ Framework\\ v4.0.30319\\ MSBuild.exe
195+
196+ rd /S /Q %FREETYPE%\\ objs
197+ %MSBUILD% %FREETYPE%\\ builds\\ win32\\ {vc20xx}\\ freetype.sln /t:Clean;Build /p:Configuration="{config}";Platform={WinXX}
198+ xcopy /Y /E /Q %FREETYPE%\\ include %INCLIB%
199+ xcopy /Y /E /Q %FREETYPE%\\ objs\\ win32\\ {vc20xx} %INCLIB%
200+ copy /Y /B %FREETYPE%\\ objs\\ win32\\ {vc20xx}\\ *.lib %INCLIB%\\ freetype.lib
201+ """
202+
203+ def build_freetype ():
204+ inclib = config_dir ()
205+ if not os .path .exists (inclib ):
206+ os .mkdir (inclib )
207+
208+ distfile = os .path .join (DEPSSRC , 'ft2411.zip' )
209+ compfile = os .path .join (inclib , 'freetype.lib' )
210+ if os .path .exists (compfile ) and os .path .getmtime (distfile ) < os .path .getmtime (compfile ):
211+ # already built
212+ return
213+
214+ vc = 'vc2010' if VS2010 else 'vc2008'
215+ WinXX = 'x64' if X64 else 'Win32'
216+
217+ zip_extract (distfile , DEPSBUILD )
218+ ft_dir = os .path .join (DEPSBUILD , 'freetype-2.4.11' )
219+ fixproj (os .path .join (ft_dir , 'builds' , 'win32' , vc , 'freetype.sln' ), WinXX )
220+ fixproj (os .path .join (ft_dir , 'builds' , 'win32' , vc , 'freetype.{}' .format ('vcxproj' if VS2010 else 'vcproj' )), WinXX )
221+
222+ cmdfile = os .path .join (DEPSBUILD , 'build_freetype.cmd' )
223+ with open (cmdfile , 'w' ) as cmd :
224+ cmd .write (prepare_build_cmd (FREETYPE_BUILD_CMD , vc20xx = vc , WinXX = WinXX , config = 'Release' if VS2010 else 'LIB Release' ))
225+
226+ os .environ ['INCLIB' ] = inclib
227+ os .environ ['FREETYPE' ] = ft_dir
228+ os .system (cmdfile )
0 commit comments