x86/x64: Add more red zone checks to assembler backend.
[luajit-2.0.git] / doc / install.html
blob218663151cd29a059a27f1000bce046c5bc1323b
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <title>Installation</title>
5 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
6 <meta name="Copyright" content="Copyright (C) 2005-2023">
7 <meta name="Language" content="en">
8 <link rel="stylesheet" type="text/css" href="bluequad.css" media="screen">
9 <link rel="stylesheet" type="text/css" href="bluequad-print.css" media="print">
10 <style type="text/css">
11 table.compat {
12 line-height: 1.2;
13 font-size: 80%;
15 table.compat td {
16 border: 1px solid #bfcfff;
17 height: 1.5em;
19 table.compat tr.compathead td {
20 font-weight: bold;
21 border-bottom: 2px solid #bfcfff;
23 td.compatname {
24 width: 10%;
26 td.compatbits {
27 width: 5%;
29 td.compatx {
30 width: 21%;
32 </style>
33 </head>
34 <body>
35 <div id="site">
36 <a href="https://luajit.org"><span>Lua<span id="logo">JIT</span></span></a>
37 </div>
38 <div id="head">
39 <h1>Installation</h1>
40 </div>
41 <div id="nav">
42 <ul><li>
43 <a href="luajit.html">LuaJIT</a>
44 <ul><li>
45 <a href="https://luajit.org/download.html">Download <span class="ext">&raquo;</span></a>
46 </li><li>
47 <a class="current" href="install.html">Installation</a>
48 </li><li>
49 <a href="running.html">Running</a>
50 </li></ul>
51 </li><li>
52 <a href="extensions.html">Extensions</a>
53 <ul><li>
54 <a href="ext_ffi.html">FFI Library</a>
55 <ul><li>
56 <a href="ext_ffi_tutorial.html">FFI Tutorial</a>
57 </li><li>
58 <a href="ext_ffi_api.html">ffi.* API</a>
59 </li><li>
60 <a href="ext_ffi_semantics.html">FFI Semantics</a>
61 </li></ul>
62 </li><li>
63 <a href="ext_jit.html">jit.* Library</a>
64 </li><li>
65 <a href="ext_c_api.html">Lua/C API</a>
66 </li></ul>
67 </li><li>
68 <a href="https://luajit.org/status.html">Status <span class="ext">&raquo;</span></a>
69 </li><li>
70 <a href="https://luajit.org/faq.html">FAQ <span class="ext">&raquo;</span></a>
71 </li><li>
72 <a href="https://luajit.org/list.html">Mailing List <span class="ext">&raquo;</span></a>
73 </li></ul>
74 </div>
75 <div id="main">
76 <p>
77 LuaJIT is only distributed as source code &mdash; get it from the
78 <a href="https://luajit.org/download.html"><span class="ext">&raquo;</span>&nbsp;git repository</a>. This page explains how to build
79 and install the LuaJIT binary and library for different operating systems.
80 </p>
81 <p>
82 For the impatient (on POSIX systems):
83 </p>
84 <pre class="code">
85 make &amp;&amp; sudo make install
86 </pre>
88 <h2 id="req">Requirements</h2>
89 <p>
90 LuaJIT currently builds out-of-the box on most systems. Please check the
91 supported operating systems and CPU architectures on the
92 <a href="https://luajit.org/status.html"><span class="ext">&raquo;</span>&nbsp;status page</a>.
93 </p>
94 <p>
95 Building LuaJIT requires a recent toolchain based on GCC, Clang/LLVM or
96 MSVC++.
97 </p>
98 <p>
99 The Makefile-based build system requires GNU Make and supports
100 cross-builds.
101 </p>
103 Batch files are provided for MSVC++ builds and console cross-builds.
104 </p>
106 <h2>Configuring LuaJIT</h2>
108 The standard configuration should work fine for most installations.
109 Usually there is no need to tweak the settings. The following files
110 hold all user-configurable settings:
111 </p>
112 <ul>
113 <li><tt>Makefile</tt> has settings for <b>installing</b> LuaJIT (POSIX
114 only).</li>
115 <li><tt>src/Makefile</tt> has settings for <b>compiling</b> LuaJIT
116 under POSIX, MinGW or Cygwin.</li>
117 <li><tt>src/msvcbuild.bat</tt> has settings for compiling LuaJIT with
118 MSVC (Visual Studio).</li>
119 </ul>
121 Please read the instructions given in these files, before changing
122 any settings.
123 </p>
125 <h2 id="posix">POSIX Systems (Linux, macOS, *BSD etc.)</h2>
126 <h3>Prerequisites</h3>
128 Depending on your distribution, you may need to install a package for a
129 compiler (GCC or Clang/LLVM), the development headers and/or a complete SDK.
130 E.g. on a current Debian/Ubuntu, install <tt>build-essential</tt> with the
131 package manager.
132 </p>
133 </pre>
134 <h3>Building LuaJIT</h3>
136 The supplied Makefiles try to auto-detect the settings needed for your
137 operating system and your compiler. They need to be run with GNU Make,
138 which is probably the default on your system, anyway. Simply run:
139 </p>
140 <pre class="code">
141 make
142 </pre>
144 This always builds a native binary, depending on the host OS
145 you're running this command on. Check the section on
146 <a href="#cross">cross-compilation</a> for more options.
147 </p>
149 By default, modules are only searched under the prefix <tt>/usr/local</tt>.
150 You can add an extra prefix to the search paths by appending the
151 <tt>PREFIX</tt> option, e.g.:
152 </p>
153 <pre class="code">
154 make PREFIX=/home/myself/lj2
155 </pre>
157 Please use the LuaJIT 2.1 branch to compile for
158 <b id="osx">macOS (OSX)</b>.
159 </p>
160 <h3>Installing LuaJIT</h3>
162 The top-level Makefile installs LuaJIT by default under
163 <tt>/usr/local</tt>, i.e. the executable ends up in
164 <tt>/usr/local/bin</tt> and so on. You need root privileges
165 to write to this path. So, assuming sudo is installed on your system,
166 run the following command and enter your sudo password:
167 </p>
168 <pre class="code">
169 sudo make install
170 </pre>
172 Otherwise specify the directory prefix as an absolute path, e.g.:
173 </p>
174 <pre class="code">
175 make install PREFIX=/home/myself/lj2
176 </pre>
178 Obviously the prefixes given during build and installation need to be the same.
179 </p>
181 <h2 id="windows">Windows Systems</h2>
182 <h3>Prerequisites</h3>
184 Either install one of the open source SDKs
185 (<a href="http://mingw.org/"><span class="ext">&raquo;</span>&nbsp;MinGW</a> or
186 <a href="https://www.cygwin.com/"><span class="ext">&raquo;</span>&nbsp;Cygwin</a>), which come with a modified
187 GCC plus the required development headers.
188 Or install Microsoft's Visual Studio (MSVC).
189 </p>
190 <h3>Building with MSVC</h3>
192 Open a "Visual Studio Command Prompt" (either x86 or x64), <tt>cd</tt> to the
193 directory with the source code and run these commands:
194 </p>
195 <pre class="code">
196 cd src
197 msvcbuild
198 </pre>
200 Check the <tt>msvcbuild.bat</tt> file for more options.
201 Then follow the installation instructions below.
202 </p>
203 <h3>Building with MinGW or Cygwin</h3>
205 Open a command prompt window and make sure the MinGW or Cygwin programs
206 are in your path. Then <tt>cd</tt> to the directory of the git repository.
207 Then run this command for MinGW:
208 </p>
209 <pre class="code">
210 mingw32-make
211 </pre>
213 Or this command for Cygwin:
214 </p>
215 <pre class="code">
216 make
217 </pre>
219 Then follow the installation instructions below.
220 </p>
221 <h3>Installing LuaJIT</h3>
223 Copy <tt>luajit.exe</tt> and <tt>lua51.dll</tt> (built in the <tt>src</tt>
224 directory) to a newly created directory (any location is ok).
225 Add <tt>lua</tt> and <tt>lua\jit</tt> directories below it and copy
226 all Lua files from the <tt>src\jit</tt> directory of the distribution
227 to the latter directory.
228 </p>
230 There are no hardcoded
231 absolute path names &mdash; all modules are loaded relative to the
232 directory where <tt>luajit.exe</tt> is installed
233 (see <tt>src/luaconf.h</tt>).
234 </p>
236 <h2 id="cross">Cross-compiling LuaJIT</h2>
238 The GNU Makefile-based build system allows cross-compiling on any host
239 for any supported target, as long as both architectures have the same
240 pointer size. If you want to cross-compile to any 32 bit target on an
241 x64 OS, you need to install the multilib development package (e.g.
242 <tt>libc6-dev-i386</tt> on Debian/Ubuntu) and build a 32 bit host part
243 (<tt>HOST_CC="gcc -m32"</tt>).
244 </p>
246 You need to specify <tt>TARGET_SYS</tt> whenever the host OS and the
247 target OS differ, or you'll get assembler or linker errors. E.g. if
248 you're compiling on a Windows or macOS host for embedded Linux or Android,
249 you need to add <tt>TARGET_SYS=Linux</tt> to the examples below. For a
250 minimal target OS, you may need to disable the built-in allocator in
251 <tt>src/Makefile</tt> and use <tt>TARGET_SYS=Other</tt>. Don't forget to
252 specify the same <tt>TARGET_SYS</tt> for the install step, too.
253 </p>
255 The examples below only show some popular targets &mdash; please check
256 the comments in <tt>src/Makefile</tt> for more details.
257 </p>
258 <pre class="code">
259 # Cross-compile to a 32 bit binary on a multilib x64 OS
260 make CC="gcc -m32"
262 # Cross-compile on Debian/Ubuntu for Windows (mingw32 package)
263 make HOST_CC="gcc -m32" CROSS=i586-mingw32msvc- TARGET_SYS=Windows
264 </pre>
265 <p id="cross2">
266 The <tt>CROSS</tt> prefix allows specifying a standard GNU cross-compile
267 toolchain (Binutils, GCC and a matching libc). The prefix may vary
268 depending on the <tt>--target</tt> the toolchain was built for (note the
269 <tt>CROSS</tt> prefix has a trailing <tt>"-"</tt>). The examples below
270 use the canonical toolchain triplets for Linux.
271 </p>
273 Since there's often no easy way to detect CPU features at runtime, it's
274 important to compile with the proper CPU or architecture settings. You
275 can specify these when building the toolchain yourself. Or add
276 <tt>-mcpu=...</tt> or <tt>-march=...</tt> to <tt>TARGET_CFLAGS</tt>. For
277 ARM it's important to have the correct <tt>-mfloat-abi=...</tt> setting,
278 too. Otherwise, LuaJIT may not run at the full performance of your target
279 CPU.
280 </p>
281 <pre class="code">
282 # ARM soft-float
283 make HOST_CC="gcc -m32" CROSS=arm-linux-gnueabi- \
284 TARGET_CFLAGS="-mfloat-abi=soft"
286 # ARM soft-float ABI with VFP (example for Cortex-A8)
287 make HOST_CC="gcc -m32" CROSS=arm-linux-gnueabi- \
288 TARGET_CFLAGS="-mcpu=cortex-a8 -mfloat-abi=softfp"
290 # ARM hard-float ABI with VFP (armhf, requires recent toolchain)
291 make HOST_CC="gcc -m32" CROSS=arm-linux-gnueabihf-
293 # PPC
294 make HOST_CC="gcc -m32" CROSS=powerpc-linux-gnu-
295 # PPC/e500v2 (fast interpreter only)
296 make HOST_CC="gcc -m32" CROSS=powerpc-e500v2-linux-gnuspe-
298 # MIPS big-endian
299 make HOST_CC="gcc -m32" CROSS=mips-linux-
300 # MIPS little-endian
301 make HOST_CC="gcc -m32" CROSS=mipsel-linux-
302 </pre>
304 You can cross-compile for <b id="android">Android</b> using the <a href="https://developer.android.com/ndk/"><span class="ext">&raquo;</span>&nbsp;Android NDK</a>.
305 Please adapt the environment variables to match the install locations and the
306 desired target platform. E.g. Android&nbsp;4.1 corresponds to ABI level&nbsp;16.
307 </p>
308 <pre class="code">
309 # Android/ARM, armeabi-v7a (ARMv7 VFP), Android 4.1+ (JB)
311 NDKDIR=/opt/android/ndk
312 NDKBIN=$NDKDIR/toolchains/llvm/prebuilt/linux-x86_64/bin
313 NDKCROSS=$NDKBIN/arm-linux-androideabi-
314 NDKCC=$NDKBIN/armv7a-linux-androideabi16-clang
315 make HOST_CC="gcc -m32" CROSS=$NDKCROSS \
316 STATIC_CC=$NDKCC DYNAMIC_CC="$NDKCC -fPIC" \
317 TARGET_LD=$NDKCC TARGET_AR="$NDKBIN/llvm-ar rcus" \
318 TARGET_STRIP=$NDKBIN/llvm-strip
319 </pre>
321 Please use the LuaJIT 2.1 branch to compile for
322 <b id="ios">iOS</b> (iPhone/iPad).
323 </p>
325 <h3 id="consoles">Cross-compiling for consoles</h3>
327 Building LuaJIT for consoles requires both a supported host compiler
328 (x86 or x64) and a cross-compiler from the official console SDK.
329 </p>
331 Due to restrictions on consoles, the JIT compiler is disabled and only
332 the fast interpreter is built. This is still faster than plain Lua,
333 but much slower than the JIT compiler. The FFI is disabled, too, since
334 it's not very useful in such an environment.
335 </p>
337 The following commands build a static library <tt>libluajit.a</tt>,
338 which can be linked against your game, just like the Lua library.
339 </p>
341 To cross-compile for <b id="ps3">PS3</b> from a Linux host (requires
342 32&nbsp;bit GCC, i.e. multilib Linux/x64) or a Windows host (requires
343 32&nbsp;bit MinGW), run this command:
344 </p>
345 <pre class="code">
346 make HOST_CC="gcc -m32" CROSS=ppu-lv2-
347 </pre>
349 To cross-compile for the other consoles from a Windows host, open a
350 "Native Tools Command Prompt for VS". You need to choose either the 32
351 or the 64&nbsp;bit version of the host compiler to match the target.
352 Then <tt>cd</tt> to the <tt>src</tt> directory below the source code
353 and run the build command given in the table:
354 </p>
355 <table class="compat">
356 <tr class="compathead">
357 <td class="compatname">Console</td>
358 <td class="compatbits">Bits</td>
359 <td class="compatx">Build Command</td>
360 </tr>
361 <tr class="odd separate">
362 <td class="compatname"><b id="ps4">PS4</b></td>
363 <td class="compatbits">64</td>
364 <td class="compatx"><tt>ps4build</tt></td>
365 </tr>
366 <tr class="even">
367 <td class="compatname"><b id="psvita">PS Vita</b></td>
368 <td class="compatbits">32</td>
369 <td class="compatx"><tt>psvitabuild</tt></td>
370 </tr>
371 <tr class="odd">
372 <td class="compatname"><b id="xbox360">Xbox 360</b></td>
373 <td class="compatbits">32</td>
374 <td class="compatx"><tt>xedkbuild</tt></td>
375 </tr>
376 </table>
378 Please check out the comments in the corresponding <tt>*.bat</tt>
379 file for more options.
380 </p>
382 <h2 id="embed">Embedding LuaJIT</h2>
384 LuaJIT is API-compatible with Lua 5.1. If you've already embedded Lua
385 into your application, you probably don't need to do anything to switch
386 to LuaJIT, except link with a different library:
387 </p>
388 <ul>
389 <li>It's strongly suggested to build LuaJIT separately using the supplied
390 build system. Please do <em>not</em> attempt to integrate the individual
391 source files into your build tree. You'll most likely get the internal build
392 dependencies wrong or mess up the compiler flags. Treat LuaJIT like any
393 other external library and link your application with either the dynamic
394 or static library, depending on your needs.</li>
395 <li>If you want to load C modules compiled for plain Lua
396 with <tt>require()</tt>, you need to make sure the public symbols
397 (e.g. <tt>lua_pushnumber</tt>) are exported, too:
398 <ul><li>On POSIX systems you can either link to the shared library
399 or link the static library into your application. In the latter case
400 you'll need to export all public symbols from your main executable
401 (e.g. <tt>-Wl,-E</tt> on Linux) and add the external dependencies
402 (e.g. <tt>-lm -ldl</tt> on Linux).</li>
403 <li>Since Windows symbols are bound to a specific DLL name, you need to
404 link to the <tt>lua51.dll</tt> created by the LuaJIT build (do not rename
405 the DLL). You may link LuaJIT statically on Windows only if you don't
406 intend to load Lua/C modules at runtime.
407 </li></ul>
408 </li>
409 </ul>
410 <p>Additional hints for initializing LuaJIT using the C API functions:</p>
411 <ul>
412 <li>Here's a
413 <a href="http://lua-users.org/wiki/SimpleLuaApiExample"><span class="ext">&raquo;</span>&nbsp;simple example</a>
414 for embedding Lua or LuaJIT into your application.</li>
415 <li>Make sure you use <tt>luaL_newstate</tt>. Avoid using
416 <tt>lua_newstate</tt>, since this uses the (slower) default memory
417 allocator from your system (no support for this on 64&nbsp;bit architectures).</li>
418 <li>Make sure you use <tt>luaL_openlibs</tt> and not the old Lua 5.0 style
419 of calling <tt>luaopen_base</tt> etc. directly.</li>
420 <li>To change or extend the list of standard libraries to load, copy
421 <tt>src/lib_init.c</tt> to your project and modify it accordingly.
422 Make sure the <tt>jit</tt> library is loaded, or the JIT compiler
423 will not be activated.</li>
424 <li>The <tt>bit.*</tt> module for bitwise operations
425 is already built-in. There's no need to statically link
426 <a href="https://bitop.luajit.org/"><span class="ext">&raquo;</span>&nbsp;Lua BitOp</a> to your application.</li>
427 </ul>
429 <h2 id="distro">Hints for Distribution Maintainers</h2>
431 The LuaJIT build system has extra provisions for the needs of most
432 POSIX-based distributions. If you're a package maintainer for
433 a distribution, <em>please</em> make use of these features and
434 avoid patching, subverting, autotoolizing or messing up the build system
435 in unspeakable ways.
436 </p>
438 There should be absolutely no need to patch <tt>luaconf.h</tt> or any
439 of the Makefiles. And please do not hand-pick files for your packages &mdash;
440 simply use whatever <tt>make install</tt> creates. There's a reason
441 for all the files <em>and</em> directories it creates.
442 </p>
444 The build system uses GNU make and auto-detects most settings based on
445 the host you're building it on. This should work fine for native builds,
446 even when sandboxed. You may need to pass some of the following flags to
447 <em>both</em> the <tt>make</tt> and the <tt>make install</tt> command lines
448 for a regular distribution build:
449 </p>
450 <ul>
451 <li><tt>PREFIX</tt> overrides the installation path and should usually
452 be set to <tt>/usr</tt>. Setting this also changes the module paths and
453 the paths needed to locate the shared library.</li>
454 <li><tt>DESTDIR</tt> is an absolute path which allows you to install
455 to a shadow tree instead of the root tree of the build system.</li>
456 <li><tt>MULTILIB</tt> sets the architecture-specific library path component
457 for multilib systems. The default is <tt>lib</tt>.</li>
458 <li>Have a look at the top-level <tt>Makefile</tt> and <tt>src/Makefile</tt>
459 for additional variables to tweak. The following variables <em>may</em> be
460 overridden, but it's <em>not</em> recommended, except for special needs
461 like cross-builds:
462 <tt>BUILDMODE, CC, HOST_CC, STATIC_CC, DYNAMIC_CC, CFLAGS, HOST_CFLAGS,
463 TARGET_CFLAGS, LDFLAGS, HOST_LDFLAGS, TARGET_LDFLAGS, TARGET_SHLDFLAGS,
464 TARGET_FLAGS, LIBS, HOST_LIBS, TARGET_LIBS, CROSS, HOST_SYS, TARGET_SYS
465 </tt></li>
466 </ul>
468 The build system has a special target for an amalgamated build, i.e.
469 <tt>make amalg</tt>. This compiles the LuaJIT core as one huge C file
470 and allows GCC to generate faster and shorter code. Alas, this requires
471 lots of memory during the build. This may be a problem for some users,
472 that's why it's not enabled by default. But it shouldn't be a problem for
473 most build farms. It's recommended that binary distributions use this
474 target for their LuaJIT builds.
475 </p>
477 The tl;dr version of the above:
478 </p>
479 <pre class="code">
480 make amalg PREFIX=/usr && \
481 make install PREFIX=/usr DESTDIR=/tmp/buildroot
482 </pre>
484 Finally, if you encounter any difficulties, please
485 <a href="contact.html">contact me</a> first, instead of releasing a broken
486 package onto unsuspecting users. Because they'll usually gonna complain
487 to me (the upstream) and not you (the package maintainer), anyway.
488 </p>
489 <br class="flush">
490 </div>
491 <div id="foot">
492 <hr class="hide">
493 Copyright &copy; 2005-2023
494 <span class="noprint">
495 &middot;
496 <a href="contact.html">Contact</a>
497 </span>
498 </div>
499 </body>
500 </html>