1@rem Script to build LuaJIT with the PS4 SDK.
2@rem Donated to the public domain.
3@rem
4@rem Open a "Visual Studio .NET Command Prompt" (64 bit host compiler)
5@rem or "VS2015 x64 Native Tools Command Prompt".
6@rem
7@rem Then cd to this directory and run this script.
8@rem
9@rem Recommended invocation:
10@rem
11@rem ps4build        release build, amalgamated, 64-bit GC
12@rem ps4build debug    debug build, amalgamated, 64-bit GC
13@rem
14@rem Additional command-line options (not generally recommended):
15@rem
16@rem gc32 (before debug)    32-bit GC
17@rem noamalg (after debug)  non-amalgamated build
18
19@if not defined INCLUDE goto :FAIL
20@if not defined SCE_ORBIS_SDK_DIR goto :FAIL
21
22@setlocal
23@rem ---- Host compiler ----
24@set LJCOMPILE=cl /nologo /c /MD /O2 /W3 /D_CRT_SECURE_NO_DEPRECATE
25@set LJLINK=link /nologo
26@set LJMT=mt /nologo
27@set DASMDIR=..\dynasm
28@set DASM=%DASMDIR%\dynasm.lua
29@set ALL_LIB=lib_base.c lib_math.c lib_bit.c lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c lib_jit.c lib_ffi.c lib_buffer.c
30@set GC64=
31@set DASC=vm_x64.dasc
32
33@if "%1" neq "gc32" goto :NOGC32
34@shift
35@set GC64=-DLUAJIT_DISABLE_GC64
36@set DASC=vm_x86.dasc
37:NOGC32
38
39%LJCOMPILE% host\minilua.c
40@if errorlevel 1 goto :BAD
41%LJLINK% /out:minilua.exe minilua.obj
42@if errorlevel 1 goto :BAD
43if exist minilua.exe.manifest^
44  %LJMT% -manifest minilua.exe.manifest -outputresource:minilua.exe
45
46@rem Check for 64 bit host compiler.
47@minilua
48@if not errorlevel 8 goto :FAIL
49
50@set DASMFLAGS=-D P64 -D NO_UNWIND
51minilua %DASM% -LN %DASMFLAGS% -o host\buildvm_arch.h %DASC%
52@if errorlevel 1 goto :BAD
53
54%LJCOMPILE% /I "." /I %DASMDIR% %GC64% -DLUAJIT_TARGET=LUAJIT_ARCH_X64 -DLUAJIT_OS=LUAJIT_OS_OTHER -DLUAJIT_DISABLE_JIT -DLUAJIT_DISABLE_FFI -DLUAJIT_NO_UNWIND host\buildvm*.c
55@if errorlevel 1 goto :BAD
56%LJLINK% /out:buildvm.exe buildvm*.obj
57@if errorlevel 1 goto :BAD
58if exist buildvm.exe.manifest^
59  %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe
60
61buildvm -m elfasm -o lj_vm.s
62@if errorlevel 1 goto :BAD
63buildvm -m bcdef -o lj_bcdef.h %ALL_LIB%
64@if errorlevel 1 goto :BAD
65buildvm -m ffdef -o lj_ffdef.h %ALL_LIB%
66@if errorlevel 1 goto :BAD
67buildvm -m libdef -o lj_libdef.h %ALL_LIB%
68@if errorlevel 1 goto :BAD
69buildvm -m recdef -o lj_recdef.h %ALL_LIB%
70@if errorlevel 1 goto :BAD
71buildvm -m vmdef -o jit\vmdef.lua %ALL_LIB%
72@if errorlevel 1 goto :BAD
73buildvm -m folddef -o lj_folddef.h lj_opt_fold.c
74@if errorlevel 1 goto :BAD
75
76@rem ---- Cross compiler ----
77@set LJCOMPILE="%SCE_ORBIS_SDK_DIR%\host_tools\bin\orbis-clang" -c -Wall -DLUAJIT_DISABLE_FFI %GC64%
78@set LJLIB="%SCE_ORBIS_SDK_DIR%\host_tools\bin\orbis-ar" rcus
79@set INCLUDE=""
80
81orbis-as -o lj_vm.o lj_vm.s
82
83@if "%1" neq "debug" goto :NODEBUG
84@shift
85@set LJCOMPILE=%LJCOMPILE% -g -O0
86@set TARGETLIB=libluajitD_ps4.a
87goto :BUILD
88:NODEBUG
89@set LJCOMPILE=%LJCOMPILE% -O2
90@set TARGETLIB=libluajit_ps4.a
91:BUILD
92del %TARGETLIB%
93@if "%1" neq "noamalg" goto :AMALG
94for %%f in (lj_*.c lib_*.c) do (
95  %LJCOMPILE% %%f
96  @if errorlevel 1 goto :BAD
97)
98
99%LJLIB% %TARGETLIB% lj_*.o lib_*.o
100@if errorlevel 1 goto :BAD
101@goto :NOAMALG
102:AMALG
103%LJCOMPILE% ljamalg.c
104@if errorlevel 1 goto :BAD
105%LJLIB% %TARGETLIB% ljamalg.o lj_vm.o
106@if errorlevel 1 goto :BAD
107:NOAMALG
108
109@del *.o *.obj *.manifest minilua.exe buildvm.exe
110@echo.
111@echo === Successfully built LuaJIT for PS4 ===
112
113@goto :END
114:BAD
115@echo.
116@echo *******************************************************
117@echo *** Build FAILED -- Please check the error messages ***
118@echo *******************************************************
119@goto :END
120:FAIL
121@echo To run this script you must open a "Visual Studio .NET Command Prompt"
122@echo (64 bit host compiler). The PS4 Orbis SDK must be installed, too.
123:END
124