blob: 3df85130f48adb530b5846f6cc6de79456d920ba [file] [log] [blame]
Paul Monson32119e12019-03-29 16:30:10 -07001@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 build for x64
26echo. -x86 build for x86
27echo. -? this help
28echo. --install-cygwin install cygwin to c:\cygwin
29exit /b 127
30
31:Run
32
33set BUILD_X64=
34set BUILD_X86=
35set INSTALL_CYGWIN=
36
37:CheckOpts
38if "%1"=="" goto :CheckOptsDone
39if /I "%1"=="-x64" (set BUILD_X64=1) & shift & goto :CheckOpts
40if /I "%1"=="-x86" (set BUILD_X86=1) & shift & goto :CheckOpts
41if /I "%1"=="-?" goto :Usage
42if /I "%1"=="--install-cygwin" (set INSTALL_CYGWIN=1) & shift & goto :CheckOpts
43goto :Usage
44
45:CheckOptsDone
46
47if NOT DEFINED BUILD_X64 if NOT DEFINED BUILD_X86 if NOT DEFINED BUILD_ARM32 (
48 set BUILD_X64=1
49 set BUILD_X86=1
50)
51
52if "%INSTALL_CYGWIN%"=="1" call :InstallCygwin
53
54setlocal
55if NOT DEFINED SH if exist c:\cygwin\bin\sh.exe set SH=c:\cygwin\bin\sh.exe
56
57if NOT DEFINED VCVARSALL (
58 if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" (
59 set VCVARSALL="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat"
60 )
61)
62if ^%VCVARSALL:~0,1% NEQ ^" SET VCVARSALL="%VCVARSALL%"
63
64if NOT DEFINED LIBFFI_SOURCE echo.&&echo ERROR LIBFFI_SOURCE environment variable not set && goto :Usage
65if NOT DEFINED SH echo ERROR SH environment variable not set && goto :Usage
66
67if not exist %SH% echo ERROR %SH% does not exist && goto :Usage
68if not exist %LIBFFI_SOURCE% echo ERROR %LIBFFI_SOURCE% does not exist && goto :Usage
69
70set OLDPWD=%LIBFFI_SOURCE%
71pushd %LIBFFI_SOURCE%
72
73%SH% --login -lc "cygcheck -dc cygwin"
74set GET_MSVCC=%SH% -lc "cd $OLDPWD; export MSVCC=`/usr/bin/find $PWD -name msvcc.sh`; echo ${MSVCC};"
75FOR /F "usebackq delims==" %%i IN (`%GET_MSVCC%`) do @set MSVCC=%%i
76
77echo.
78echo VCVARSALL : %VCVARSALL%
79echo SH : %SH%
80echo LIBFFI_SOURCE: %LIBFFI_SOURCE%
81echo MSVCC : %MSVCC%
82echo.
83
84if not exist Makefile.in (%SH% -lc "(cd $LIBFFI_SOURCE; ./autogen.sh;)")
85
86call :BuildOne x86 i686-pc-cygwin i686-pc-cygwin
87call :BuildOne x64 x86_64-w64-cygwin x86_64-w64-cygwin
88
89popd
90endlocal
91exit /b 0
92REM all done
93
94
95REM this subroutine is called once for each architecture
96:BuildOne
97
98setlocal
99
100REM Initialize variables
101set VCVARS_PLATFORM=%1
102set BUILD=%2
103set HOST=%3
104set ASSEMBLER=
105set SRC_ARCHITECTURE=x86
106
107if NOT DEFINED VCVARS_PLATFORM echo ERROR bad VCVARS_PLATFORM&&exit /b 123
108
109if /I "%VCVARS_PLATFORM%" EQU "x64" (
110 set ARCH=amd64
111 set ARTIFACTS=%LIBFFI_SOURCE%\x86_64-w64-cygwin
112 set ASSEMBLER=-m64
113 set SRC_ARCHITECTURE=x86
114)
115if /I "%VCVARS_PLATFORM%" EQU "x86" (
116 set ARCH=win32
117 set ARTIFACTS=%LIBFFI_SOURCE%\i686-pc-cygwin
118 set ASSEMBLER=
119 set SRC_ARCHITECTURE=x86
120)
121
122if NOT DEFINED LIBFFI_OUT set LIBFFI_OUT=%~dp0\..\externals\libffi
123set _LIBFFI_OUT=%LIBFFI_OUT%\%ARCH%
124
125echo get VS build environment
126call %VCVARSALL% %VCVARS_PLATFORM%
127
128echo clean %_LIBFFI_OUT%
129if exist %_LIBFFI_OUT% (rd %_LIBFFI_OUT% /s/q)
130
131echo Configure the build to generate fficonfig.h and ffi.h
132%SH% -lc "(cd $OLDPWD; ./configure CC='%MSVCC% %ASSEMBLER%' CXX='%MSVCC% %ASSEMBLER%' LD='link' CPP='cl -nologo -EP' CXXCPP='cl -nologo -EP' CPPFLAGS='-DFFI_BUILDING_DLL' NM='dumpbin -symbols' STRIP=':' --build=$BUILD --host=$HOST;)"
133
134echo Building libffi
135%SH% -lc "(cd $OLDPWD; export PATH=/usr/bin:$PATH; cp src/%SRC_ARCHITECTURE%/ffitarget.h include; make; find .;)"
136
137REM Tests are not needed to produce artifacts
138if "%LIBFFI_TEST%" EQU "1" (
139 echo "Running tests..."
140 %SH% -lc "(cd $OLDPWD; export PATH=/usr/bin:$PATH; cp `find $PWD -name 'libffi-?.dll'` $HOST/testsuite/; make check; cat `find ./ -name libffi.log`)"
141) else (
142 echo "Not running tests"
143)
144
145
146echo copying files to %_LIBFFI_OUT%
147if not exist %_LIBFFI_OUT%\include (md %_LIBFFI_OUT%\include)
148copy %ARTIFACTS%\.libs\libffi-7.dll %_LIBFFI_OUT%
149copy %ARTIFACTS%\.libs\libffi-7.lib %_LIBFFI_OUT%
150copy %ARTIFACTS%\fficonfig.h %_LIBFFI_OUT%\include
151copy %ARTIFACTS%\include\*.h %_LIBFFI_OUT%\include
152
153endlocal
154exit /b
155
156:InstallCygwin
157setlocal
158
159if NOT DEFINED CYG_ROOT (set CYG_ROOT=c:/cygwin)
160if NOT DEFINED CYG_CACHE (set CYG_CACHE=C:/cygwin/var/cache/setup)
161if NOT DEFINED CYG_MIRROR (set CYG_MIRROR=http://mirrors.kernel.org/sourceware/cygwin/)
162
163powershell -c "md $env:CYG_ROOT -ErrorAction SilentlyContinue"
164powershell -c "$setup = $env:CYG_ROOT+'/setup.exe'; if (!(Test-Path $setup)){invoke-webrequest https://cygwin.com/setup-x86.exe -outfile $setup}
165%CYG_ROOT%/setup.exe -qnNdO -R "%CYG_ROOT%" -s "%CYG_MIRROR%" -l "%CYG_CACHE%" -P make -P autoconf -P automake -P libtool -P dejagnu
166
167endlocal
168exit /b
169