• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1@echo off
2goto :Run
3
4:Usage
5echo.
6echo Before running prepare_libffi.bat
7echo   LIBFFI_SOURCE environment variable must be set to the location of
8echo     of python-source-deps clone of libffi branch
9echo   VCVARSALL must be set to location of vcvarsall.bat
10echo   cygwin must be installed (see below)
11echo   SH environment variable must be set to the location of sh.exe
12echo.
13echo Tested with cygwin-x86 from https://www.cygwin.com/install.html
14echo Select http://mirrors.kernel.org as the download site
15echo Include the following cygwin packages in cygwin configuration:
16echo     make, autoconf, automake, libtool, dejagnu
17echo.
18echo NOTE: dejagnu is only required for running tests.
19echo       set LIBFFI_TEST=1 to run tests (optional)
20echo.
21echo Based on https://github.com/libffi/libffi/blob/master/.appveyor.yml
22echo.
23echo.
24echo.Available flags:
25echo.  -x64    enable x64 build
26echo.  -x86    enable x86 build
27echo.  -arm32  enable arm32 build
28echo.  -arm64  enable arm64 build
29echo.  -?      this help
30echo.  --install-cygwin  install cygwin to c:\cygwin
31exit /b 127
32
33:Run
34
35set BUILD_X64=
36set BUILD_X86=
37set BUILD_ARM32=
38set BUILD_ARM64=
39set BUILD_PDB=
40set BUILD_NOOPT=
41set COPY_LICENSE=
42set INSTALL_CYGWIN=
43
44:CheckOpts
45if "%1"=="" goto :CheckOptsDone
46if /I "%1"=="-x64" (set BUILD_X64=1) & shift & goto :CheckOpts
47if /I "%1"=="-x86" (set BUILD_X86=1) & shift & goto :CheckOpts
48if /I "%1"=="-win32" (set BUILD_X86=1) & shift & goto :CheckOpts
49if /I "%1"=="-arm32" (set BUILD_ARM32=1) & shift & goto :CheckOpts
50if /I "%1"=="-arm64" (set BUILD_ARM64=1) & shift & goto :CheckOpts
51if /I "%1"=="-pdb" (set BUILD_PDB=-g) & shift & goto :CheckOpts
52if /I "%1"=="-noopt" (set BUILD_NOOPT=CFLAGS='-Od -warn all') & shift & goto :CheckOpts
53if /I "%1"=="-license" (set COPY_LICENSE=1) & shift & goto :CheckOpts
54if /I "%1"=="-?" goto :Usage
55if /I "%1"=="--install-cygwin" (set INSTALL_CYGWIN=1) & shift & goto :CheckOpts
56goto :Usage
57
58:CheckOptsDone
59
60if NOT DEFINED BUILD_X64 if NOT DEFINED BUILD_X86 if NOT DEFINED BUILD_ARM32 if NOT DEFINED BUILD_ARM64 (
61    set BUILD_X64=1
62    set BUILD_X86=1
63    set BUILD_ARM32=1
64    set BUILD_ARM64=1
65    set COPY_LICENSE=1
66)
67
68if "%INSTALL_CYGWIN%"=="1" call :InstallCygwin
69
70setlocal
71if NOT DEFINED SH if exist c:\cygwin\bin\sh.exe set SH=c:\cygwin\bin\sh.exe
72
73if NOT DEFINED VCVARSALL (
74    for /F "tokens=*" %%i in ('"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -property installationPath -latest -prerelease -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64') DO @(set VCVARSALL="%%i\VC\Auxiliary\Build\vcvarsall.bat")
75)
76if ^%VCVARSALL:~0,1% NEQ ^" SET VCVARSALL="%VCVARSALL%"
77
78if NOT DEFINED LIBFFI_SOURCE echo.&&echo ERROR LIBFFI_SOURCE environment variable not set && goto :Usage
79if NOT DEFINED SH echo ERROR SH environment variable not set && goto :Usage
80
81if not exist %SH% echo ERROR %SH% does not exist && goto :Usage
82if not exist %LIBFFI_SOURCE% echo ERROR %LIBFFI_SOURCE% does not exist && goto :Usage
83
84set OLDPWD=%LIBFFI_SOURCE%
85pushd %LIBFFI_SOURCE%
86
87%SH% --login -lc "cygcheck -dc cygwin"
88set GET_MSVCC=%SH% -lc "cd $OLDPWD; export MSVCC=`/usr/bin/find $PWD -name msvcc.sh`; echo ${MSVCC};"
89FOR /F "usebackq delims==" %%i IN (`%GET_MSVCC%`) do @set MSVCC=%%i
90
91echo.
92echo VCVARSALL    : %VCVARSALL%
93echo SH           : %SH%
94echo LIBFFI_SOURCE: %LIBFFI_SOURCE%
95echo MSVCC        : %MSVCC%
96echo.
97
98if not exist Makefile.in (
99    %SH% -lc "(cd $LIBFFI_SOURCE; ./autogen.sh;)"
100    if errorlevel 1 exit /B 1
101)
102
103if "%BUILD_X64%"=="1" call :BuildOne x64 x86_64-w64-cygwin x86_64-w64-cygwin
104if errorlevel 1 exit /B %ERRORLEVEL%
105if "%BUILD_X86%"=="1" call :BuildOne x86 i686-pc-cygwin i686-pc-cygwin
106if errorlevel 1 exit /B %ERRORLEVEL%
107if "%BUILD_ARM32%"=="1" call :BuildOne x86_arm i686-pc-cygwin arm-w32-cygwin
108if errorlevel 1 exit /B %ERRORLEVEL%
109if "%BUILD_ARM64%"=="1" call :BuildOne x86_arm64 i686-pc-cygwin aarch64-w64-cygwin
110if errorlevel 1 exit /B %ERRORLEVEL%
111if "%COPY_LICENSE%"=="1" copy /y "%LIBFFI_SOURCE%\LICENSE" "%LIBFFI_OUT%\LICENSE"
112
113popd
114endlocal
115exit /b 0
116REM all done
117
118
119REM this subroutine is called once for each architecture
120:BuildOne
121
122setlocal
123
124REM Initialize variables
125set VCVARS_PLATFORM=%1
126set BUILD=%2
127set HOST=%3
128set ASSEMBLER=
129set SRC_ARCHITECTURE=x86
130
131if NOT DEFINED VCVARS_PLATFORM echo ERROR bad VCVARS_PLATFORM&&exit /b 123
132
133if /I "%VCVARS_PLATFORM%" EQU "x64" (
134    set ARCH=amd64
135    set ARTIFACTS=%LIBFFI_SOURCE%\x86_64-w64-cygwin
136    set ASSEMBLER=-m64
137    set SRC_ARCHITECTURE=x86
138)
139if /I "%VCVARS_PLATFORM%" EQU "x86" (
140    set ARCH=win32
141    set ARTIFACTS=%LIBFFI_SOURCE%\i686-pc-cygwin
142    set ASSEMBLER=
143    set SRC_ARCHITECTURE=x86
144)
145if /I "%VCVARS_PLATFORM%" EQU "x86_arm" (
146    set ARCH=arm32
147    set ARTIFACTS=%LIBFFI_SOURCE%\arm-w32-cygwin
148    set ASSEMBLER=-marm
149    set SRC_ARCHITECTURE=ARM
150)
151if /I "%VCVARS_PLATFORM%" EQU "x86_arm64" (
152    set ARCH=arm64
153    set ARTIFACTS=%LIBFFI_SOURCE%\aarch64-w64-cygwin
154    set ASSEMBLER=-marm64
155    set SRC_ARCHITECTURE=aarch64
156)
157
158if NOT DEFINED LIBFFI_OUT set LIBFFI_OUT=%~dp0\..\externals\libffi
159set _LIBFFI_OUT=%LIBFFI_OUT%\%ARCH%
160
161echo get VS build environment
162call %VCVARSALL% %VCVARS_PLATFORM%
163
164echo clean %_LIBFFI_OUT%
165if exist %_LIBFFI_OUT% (rd %_LIBFFI_OUT% /s/q)
166
167echo ================================================================
168echo Configure the build to generate fficonfig.h and ffi.h
169echo ================================================================
170%SH% -lc "(cd $OLDPWD; ./configure CC='%MSVCC% %ASSEMBLER% %BUILD_PDB%' CXX='%MSVCC% %ASSEMBLER% %BUILD_PDB%' LD='link' CPP='cl -nologo -EP' CXXCPP='cl -nologo -EP' CPPFLAGS='-DFFI_BUILDING_DLL' %BUILD_NOOPT% NM='dumpbin -symbols' STRIP=':' --build=$BUILD --host=$HOST;)"
171if errorlevel 1 exit /B %ERRORLEVEL%
172
173echo ================================================================
174echo Building libffi
175echo ================================================================
176%SH% -lc "(cd $OLDPWD; export PATH=/usr/bin:$PATH; cp src/%SRC_ARCHITECTURE%/ffitarget.h include; make; find .;)"
177if errorlevel 1 exit /B %ERRORLEVEL%
178
179REM Tests are not needed to produce artifacts
180if "%LIBFFI_TEST%" EQU "1" (
181    echo "Running tests..."
182    %SH% -lc "(cd $OLDPWD; export PATH=/usr/bin:$PATH; cp `find $PWD -name 'libffi-?.dll'` $HOST/testsuite/; make check; cat `find ./ -name libffi.log`)"
183) else (
184    echo "Not running tests"
185)
186
187
188echo copying files to %_LIBFFI_OUT%
189if not exist %_LIBFFI_OUT%\include (md %_LIBFFI_OUT%\include)
190copy %ARTIFACTS%\.libs\libffi-*.dll %_LIBFFI_OUT% || exit /B 1
191copy %ARTIFACTS%\.libs\libffi-*.lib %_LIBFFI_OUT% || exit /B 1
192copy %ARTIFACTS%\.libs\libffi-*.pdb %_LIBFFI_OUT%
193copy %ARTIFACTS%\fficonfig.h %_LIBFFI_OUT%\include || exit /B 1
194copy %ARTIFACTS%\include\*.h %_LIBFFI_OUT%\include || exit /B 1
195
196endlocal
197exit /b
198
199:InstallCygwin
200setlocal
201
202if NOT DEFINED CYG_ROOT (set CYG_ROOT=c:/cygwin)
203if NOT DEFINED CYG_CACHE (set CYG_CACHE=C:/cygwin/var/cache/setup)
204if NOT DEFINED CYG_MIRROR (set CYG_MIRROR=http://mirrors.kernel.org/sourceware/cygwin/)
205
206powershell -c "md $env:CYG_ROOT -ErrorAction SilentlyContinue"
207powershell -c "$setup = $env:CYG_ROOT+'/setup.exe'; if (!(Test-Path $setup)){invoke-webrequest https://cygwin.com/setup-x86.exe -outfile $setup}
208%CYG_ROOT%/setup.exe -qnNdO -R "%CYG_ROOT%" -s "%CYG_MIRROR%" -l "%CYG_CACHE%" -P make -P autoconf -P automake -P libtool -P dejagnu
209
210endlocal
211exit /b
212
213