14
14
import shutil
15
15
import subprocess
16
16
import sys
17
+ import tarfile
17
18
import textwrap
18
19
import urllib .request
19
20
import warnings
@@ -1045,6 +1046,8 @@ def add_flags(self, ext):
1045
1046
ext .define_macros .append (('FREETYPE_BUILD_TYPE' , 'system' ))
1046
1047
1047
1048
def do_custom_build (self ):
1049
+ from pathlib import Path
1050
+
1048
1051
# We're using a system freetype
1049
1052
if not options .get ('local_freetype' ):
1050
1053
return
@@ -1058,7 +1061,7 @@ def do_custom_build(self):
1058
1061
else :
1059
1062
libfreetype = 'libfreetype.a'
1060
1063
1061
- if os . path . isfile ( os . path . join ( src_path , ' objs' , ' .libs' , libfreetype )):
1064
+ if Path ( src_path , " objs" , " .libs" , libfreetype ). is_file ( ):
1062
1065
return
1063
1066
1064
1067
tarball = 'freetype-{0}.tar.gz' .format (LOCAL_FREETYPE_VERSION )
@@ -1097,21 +1100,20 @@ def do_custom_build(self):
1097
1100
tarball_url = url_fmt .format (
1098
1101
version = LOCAL_FREETYPE_VERSION , tarball = tarball )
1099
1102
1100
- print ("Downloading {0 }" .format (tarball_url ))
1103
+ print ("Downloading {}" .format (tarball_url ))
1101
1104
try :
1102
1105
urllib .request .urlretrieve (tarball_url , tarball_path )
1103
- except IOError : # URLError (a subclass) on Py3.
1104
- print ("Failed to download {0 }" .format (tarball_url ))
1106
+ except IOError :
1107
+ print ("Failed to download {}" .format (tarball_url ))
1105
1108
else :
1106
1109
if get_file_hash (tarball_path ) != LOCAL_FREETYPE_HASH :
1107
1110
print ("Invalid hash." )
1108
1111
else :
1109
1112
break
1110
1113
else :
1111
- raise IOError ("Failed to download freetype. "
1112
- "You can download the file by "
1113
- "alternative means and copy it "
1114
- " to '{0}'" .format (tarball_path ))
1114
+ raise IOError ("Failed to download freetype; you can "
1115
+ "download the file by alternative means and "
1116
+ "copy it to '{}'" .format (tarball_path ))
1115
1117
os .makedirs (tarball_cache_dir , exist_ok = True )
1116
1118
try :
1117
1119
shutil .copy (tarball_path , tarball_cache_path )
@@ -1122,55 +1124,55 @@ def do_custom_build(self):
1122
1124
1123
1125
if get_file_hash (tarball_path ) != LOCAL_FREETYPE_HASH :
1124
1126
raise IOError (
1125
- "{0} does not match expected hash." .format (tarball ))
1127
+ "{} does not match expected hash" .format (tarball ))
1128
+
1129
+ print ("Building {}" .format (tarball ))
1130
+ with tarfile .open (tarball_path , "r:gz" ) as tgz :
1131
+ tgz .extractall ("build" )
1126
1132
1127
- print ("Building {0}" .format (tarball ))
1128
1133
if sys .platform != 'win32' :
1129
1134
# compilation on all other platforms than windows
1130
- cflags = 'CFLAGS="{0} -fPIC" ' .format (os .environ .get ('CFLAGS' , '' ))
1131
-
1132
- subprocess .check_call (
1133
- ['tar' , 'zxf' , tarball ], cwd = 'build' )
1134
- subprocess .check_call (
1135
- [cflags + './configure --with-zlib=no --with-bzip2=no '
1136
- '--with-png=no --with-harfbuzz=no' ], shell = True , cwd = src_path )
1135
+ env = {** os .environ ,
1136
+ "CFLAGS" : "{} -fPIC" .format (os .environ .get ("CFLAGS" , "" ))}
1137
1137
subprocess .check_call (
1138
- [cflags + 'make' ], shell = True , cwd = src_path )
1138
+ ["./configure" , "--with-zlib=no" , "--with-bzip2=no" ,
1139
+ "--with-png=no" , "--with-harfbuzz=no" ],
1140
+ env = env , cwd = src_path )
1141
+ subprocess .check_call (["make" ], env = env , cwd = src_path )
1139
1142
else :
1140
1143
# compilation on windows
1141
- FREETYPE_BUILD_CMD = """\
1142
- call "%ProgramFiles%\\ Microsoft SDKs\\ Windows\\ v7.0\\ Bin\\ SetEnv.Cmd" /Release /{xXX} /xp
1144
+ FREETYPE_BUILD_CMD = r"""
1145
+ call "%ProgramFiles%\Microsoft SDKs\Windows\v7.0\Bin\SetEnv.Cmd" ^
1146
+ /Release /{xXX} /xp
1143
1147
call "{vcvarsall}" {xXX}
1144
- set MSBUILD=C:\\ Windows\\ Microsoft.NET\\ Framework\\ v4.0.30319\\ MSBuild.exe
1145
- rd /S /Q %FREETYPE%\\ objs
1146
- %MSBUILD% %FREETYPE%\\ builds\\ windows\\ {vc20xx}\\ freetype.sln /t:Clean;Build /p:Configuration="{config}";Platform={WinXX}
1147
- echo Build completed, moving result"
1148
- :: move to the "normal" path for the unix builds...
1149
- mkdir %FREETYPE%\\ objs\\ .libs
1150
- :: REMINDER: fix when changing the version
1151
- copy %FREETYPE%\\ objs\\ {vc20xx}\\ {xXX}\\ freetype261.lib %FREETYPE%\\ objs\\ .libs\\ libfreetype.lib
1152
- if errorlevel 1 (
1153
- rem This is a py27 version, which has a different location for the lib file :-/
1154
- copy %FREETYPE%\\ objs\\ win32\\ {vc20xx}\\ freetype261.lib %FREETYPE%\\ objs\\ .libs\\ libfreetype.lib
1155
- )
1148
+ set MSBUILD=C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
1149
+ %MSBUILD% "builds\windows\{vc20xx}\freetype.sln" ^
1150
+ /t:Clean;Build /p:Configuration="{config}";Platform={WinXX}
1156
1151
"""
1157
- from setup_external_compile import fixproj , prepare_build_cmd , VS2010 , X64 , tar_extract
1158
- # Note: freetype has no build profile for 2014, so we don't bother...
1159
- vc = 'vc2010' if VS2010 else 'vc2008'
1160
- WinXX = 'x64' if X64 else 'Win32'
1161
- tar_extract (tarball_path , "build" )
1162
- # This is only false for py2.7, even on py3.5...
1163
- if not VS2010 :
1164
- fixproj (os .path .join (src_path , 'builds' , 'windows' , vc , 'freetype.sln' ), WinXX )
1165
- fixproj (os .path .join (src_path , 'builds' , 'windows' , vc , 'freetype.vcproj' ), WinXX )
1166
-
1167
- cmdfile = os .path .join ("build" , 'build_freetype.cmd' )
1168
- with open (cmdfile , 'w' ) as cmd :
1169
- cmd .write (prepare_build_cmd (FREETYPE_BUILD_CMD , vc20xx = vc , WinXX = WinXX ,
1170
- config = 'Release' if VS2010 else 'LIB Release' ))
1171
-
1172
- os .environ ['FREETYPE' ] = src_path
1173
- subprocess .check_call ([cmdfile ], shell = True )
1152
+ import distutils .msvc9compiler as msvc
1153
+ # FreeType 2.6.1 has no build profile for 2014.
1154
+ vcvarsall = msvc .find_vcvarsall (10.0 )
1155
+ if vcvarsall is None :
1156
+ raise RuntimeError ("Microsoft VS 2010 required" )
1157
+ X64 = sys .maxsize > 2 ** 32
1158
+ vc20xx = "vc2010"
1159
+ WinXX = "x64" if X64 else "Win32"
1160
+ xXX = "x64" if X64 else "x86"
1161
+ cmd_file = Path ("build" , "build_freetype.cmd" )
1162
+ cmd_file .write_text (FREETYPE_BUILD_CMD .format (
1163
+ vcvarsall = msvc .find_vcvarsall (10.0 ),
1164
+ vc20xx = vc20xx , WinXX = WinXX , xXX = xXX , config = "Release" ))
1165
+ shutil .rmtree (Path (src_path , "objs" ), ignore_errors = True )
1166
+ subprocess .check_call (cmdfile , shell = True , cwd = src_path )
1167
+ # Move to the corresponding Unix build path.
1168
+ Path (src_path , "objs/.libs" ).mkdir ()
1169
+ # Be robust against change of FreeType version.
1170
+ lib_path , = (Path (src_path , "objs" , vc20xx , xXX ).glob ()
1171
+ .glob ("freetype*.lib" ))
1172
+ shutil .copy2 (
1173
+ str (lib_path ),
1174
+ str (Path (src_path , "objs" , ".libs" , "libfreetype.lib" ))
1175
+ )
1174
1176
1175
1177
1176
1178
class FT2Font (SetupPackage ):
0 commit comments