blob: c65a5f7010bd0674acd20da1007a700977895dcf [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
Paul Monson11efd792019-04-17 18:09:16 -070027echo. -arm32 build for arm32
Paul Monsonf96e7fd2019-05-17 10:07:24 -070028echo. -arm64 build for arm64
Paul Monson32119e12019-03-29 16:30:10 -070029echo. -? this help
30echo. --install-cygwin install cygwin to c:\cygwin
31exit /b 127
32
33:Run
34
35set BUILD_X64=
36set BUILD_X86=
Paul Monson11efd792019-04-17 18:09:16 -070037set BUILD_ARM32=
Paul Monsonf96e7fd2019-05-17 10:07:24 -070038set BUILD_ARM64=
39set BUILD_PDB=
40set BUILD_NOOPT=
Paul Monson32119e12019-03-29 16:30:10 -070041set INSTALL_CYGWIN=
42
43:CheckOpts
44if "%1"=="" goto :CheckOptsDone
45if /I "%1"=="-x64" (set BUILD_X64=1) & shift & goto :CheckOpts
46if /I "%1"=="-x86" (set BUILD_X86=1) & shift & goto :CheckOpts
Paul Monson11efd792019-04-17 18:09:16 -070047if /I "%1"=="-arm32" (set BUILD_ARM32=1) & shift & goto :CheckOpts
Paul Monsonf96e7fd2019-05-17 10:07:24 -070048if /I "%1"=="-arm64" (set BUILD_ARM64=1) & shift & goto :CheckOpts
49if /I "%1"=="-pdb" (set BUILD_PDB=-g) & shift & goto :CheckOpts
50if /I "%1"=="-noopt" (set BUILD_NOOPT=CFLAGS='-Od -warn all') & shift & goto :CheckOpts
Paul Monson32119e12019-03-29 16:30:10 -070051if /I "%1"=="-?" goto :Usage
52if /I "%1"=="--install-cygwin" (set INSTALL_CYGWIN=1) & shift & goto :CheckOpts
53goto :Usage
54
55:CheckOptsDone
56
Paul Monsonf96e7fd2019-05-17 10:07:24 -070057if NOT DEFINED BUILD_X64 if NOT DEFINED BUILD_X86 if NOT DEFINED BUILD_ARM32 if NOT DEFINED BUILD_ARM64 (
Paul Monson32119e12019-03-29 16:30:10 -070058 set BUILD_X64=1
59 set BUILD_X86=1
Paul Monson11efd792019-04-17 18:09:16 -070060 set BUILD_ARM32=1
Paul Monsonf96e7fd2019-05-17 10:07:24 -070061 set BUILD_ARM64=1
Paul Monson32119e12019-03-29 16:30:10 -070062)
63
64if "%INSTALL_CYGWIN%"=="1" call :InstallCygwin
65
66setlocal
67if NOT DEFINED SH if exist c:\cygwin\bin\sh.exe set SH=c:\cygwin\bin\sh.exe
68
69if NOT DEFINED VCVARSALL (
70 if exist "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" (
71 set VCVARSALL="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvarsall.bat"
72 )
73)
74if ^%VCVARSALL:~0,1% NEQ ^" SET VCVARSALL="%VCVARSALL%"
75
76if NOT DEFINED LIBFFI_SOURCE echo.&&echo ERROR LIBFFI_SOURCE environment variable not set && goto :Usage
77if NOT DEFINED SH echo ERROR SH environment variable not set && goto :Usage
78
79if not exist %SH% echo ERROR %SH% does not exist && goto :Usage
80if not exist %LIBFFI_SOURCE% echo ERROR %LIBFFI_SOURCE% does not exist && goto :Usage
81
82set OLDPWD=%LIBFFI_SOURCE%
83pushd %LIBFFI_SOURCE%
84
85%SH% --login -lc "cygcheck -dc cygwin"
86set GET_MSVCC=%SH% -lc "cd $OLDPWD; export MSVCC=`/usr/bin/find $PWD -name msvcc.sh`; echo ${MSVCC};"
87FOR /F "usebackq delims==" %%i IN (`%GET_MSVCC%`) do @set MSVCC=%%i
88
89echo.
90echo VCVARSALL : %VCVARSALL%
91echo SH : %SH%
92echo LIBFFI_SOURCE: %LIBFFI_SOURCE%
93echo MSVCC : %MSVCC%
94echo.
95
96if not exist Makefile.in (%SH% -lc "(cd $LIBFFI_SOURCE; ./autogen.sh;)")
97
Paul Monson11efd792019-04-17 18:09:16 -070098if "%BUILD_X64%"=="1" call :BuildOne x64 x86_64-w64-cygwin x86_64-w64-cygwin
99if "%BUILD_X86%"=="1" call :BuildOne x86 i686-pc-cygwin i686-pc-cygwin
100if "%BUILD_ARM32%"=="1" call :BuildOne x86_arm i686-pc-cygwin arm-w32-cygwin
Paul Monsonf96e7fd2019-05-17 10:07:24 -0700101if "%BUILD_ARM64%"=="1" call :BuildOne x86_arm64 i686-pc-cygwin aarch64-w64-cygwin
Paul Monson32119e12019-03-29 16:30:10 -0700102
103popd
104endlocal
105exit /b 0
106REM all done
107
108
109REM this subroutine is called once for each architecture
110:BuildOne
111
112setlocal
113
114REM Initialize variables
115set VCVARS_PLATFORM=%1
116set BUILD=%2
117set HOST=%3
118set ASSEMBLER=
119set SRC_ARCHITECTURE=x86
120
121if NOT DEFINED VCVARS_PLATFORM echo ERROR bad VCVARS_PLATFORM&&exit /b 123
122
123if /I "%VCVARS_PLATFORM%" EQU "x64" (
124 set ARCH=amd64
125 set ARTIFACTS=%LIBFFI_SOURCE%\x86_64-w64-cygwin
126 set ASSEMBLER=-m64
127 set SRC_ARCHITECTURE=x86
128)
129if /I "%VCVARS_PLATFORM%" EQU "x86" (
130 set ARCH=win32
131 set ARTIFACTS=%LIBFFI_SOURCE%\i686-pc-cygwin
132 set ASSEMBLER=
133 set SRC_ARCHITECTURE=x86
134)
Paul Monson11efd792019-04-17 18:09:16 -0700135if /I "%VCVARS_PLATFORM%" EQU "x86_arm" (
136 set ARCH=arm32
137 set ARTIFACTS=%LIBFFI_SOURCE%\arm-w32-cygwin
138 set ASSEMBLER=-marm
139 set SRC_ARCHITECTURE=ARM
140)
Paul Monsonf96e7fd2019-05-17 10:07:24 -0700141if /I "%VCVARS_PLATFORM%" EQU "x86_arm64" (
142 set ARCH=arm64
143 set ARTIFACTS=%LIBFFI_SOURCE%\aarch64-w64-cygwin
144 set ASSEMBLER=-marm64
145 set SRC_ARCHITECTURE=aarch64
146)
Paul Monson32119e12019-03-29 16:30:10 -0700147
148if NOT DEFINED LIBFFI_OUT set LIBFFI_OUT=%~dp0\..\externals\libffi
149set _LIBFFI_OUT=%LIBFFI_OUT%\%ARCH%
150
151echo get VS build environment
152call %VCVARSALL% %VCVARS_PLATFORM%
153
154echo clean %_LIBFFI_OUT%
155if exist %_LIBFFI_OUT% (rd %_LIBFFI_OUT% /s/q)
156
Paul Monsonf96e7fd2019-05-17 10:07:24 -0700157echo ================================================================
Paul Monson32119e12019-03-29 16:30:10 -0700158echo Configure the build to generate fficonfig.h and ffi.h
Paul Monsonf96e7fd2019-05-17 10:07:24 -0700159echo ================================================================
160%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;)"
Paul Monson32119e12019-03-29 16:30:10 -0700161
Paul Monsonf96e7fd2019-05-17 10:07:24 -0700162echo ================================================================
Paul Monson32119e12019-03-29 16:30:10 -0700163echo Building libffi
Paul Monsonf96e7fd2019-05-17 10:07:24 -0700164echo ================================================================
Paul Monson32119e12019-03-29 16:30:10 -0700165%SH% -lc "(cd $OLDPWD; export PATH=/usr/bin:$PATH; cp src/%SRC_ARCHITECTURE%/ffitarget.h include; make; find .;)"
166
167REM Tests are not needed to produce artifacts
168if "%LIBFFI_TEST%" EQU "1" (
169 echo "Running tests..."
170 %SH% -lc "(cd $OLDPWD; export PATH=/usr/bin:$PATH; cp `find $PWD -name 'libffi-?.dll'` $HOST/testsuite/; make check; cat `find ./ -name libffi.log`)"
171) else (
172 echo "Not running tests"
173)
174
175
176echo copying files to %_LIBFFI_OUT%
177if not exist %_LIBFFI_OUT%\include (md %_LIBFFI_OUT%\include)
178copy %ARTIFACTS%\.libs\libffi-7.dll %_LIBFFI_OUT%
179copy %ARTIFACTS%\.libs\libffi-7.lib %_LIBFFI_OUT%
Paul Monsonf96e7fd2019-05-17 10:07:24 -0700180copy %ARTIFACTS%\.libs\libffi-7.pdb %_LIBFFI_OUT%
Paul Monson32119e12019-03-29 16:30:10 -0700181copy %ARTIFACTS%\fficonfig.h %_LIBFFI_OUT%\include
182copy %ARTIFACTS%\include\*.h %_LIBFFI_OUT%\include
183
184endlocal
185exit /b
186
187:InstallCygwin
188setlocal
189
190if NOT DEFINED CYG_ROOT (set CYG_ROOT=c:/cygwin)
191if NOT DEFINED CYG_CACHE (set CYG_CACHE=C:/cygwin/var/cache/setup)
192if NOT DEFINED CYG_MIRROR (set CYG_MIRROR=http://mirrors.kernel.org/sourceware/cygwin/)
193
194powershell -c "md $env:CYG_ROOT -ErrorAction SilentlyContinue"
195powershell -c "$setup = $env:CYG_ROOT+'/setup.exe'; if (!(Test-Path $setup)){invoke-webrequest https://cygwin.com/setup-x86.exe -outfile $setup}
196%CYG_ROOT%/setup.exe -qnNdO -R "%CYG_ROOT%" -s "%CYG_MIRROR%" -l "%CYG_CACHE%" -P make -P autoconf -P automake -P libtool -P dejagnu
197
198endlocal
199exit /b
200