Building v8 for MIPS using GN
This page describes building V8 for MIPS.
MIPS V8 runs on Linux and Android targets, and is most commonly cross-compiled on Linux x86 hosts. It may also be built natively on MIPS Linux.
Developers commonly build and run on x86 hosts, using the built-in MIPS instruction-set simulator, so that tests can be run quickly. The is described in Simulator build.
For all builds you must first Get-the-code.
This page describes how to build build V8 for mips32 and mips64 (little-endian) target processors from an x86_64 build host using gcc. The same instructions also apply to big-endian, but beforehand you need to configure your system if you wish to compile V8 for big-endian mips with snapshot enabled.
- For big-endian builds please refer to Building v8 for BE with QEMU.
- For simulator builds please refer to Simulator build.
The simplest way to crosscompile is to use builtin CLANG as a compiler and builtin Debian root file system. Both come together as part of V8 build tree, so no additional tools are needed for compilation. Downside to this is that CLANG that is part of V8 is not always stable and the binary which you get with compilation should be used on corresponding version of Debian.
CLANG supports all flavors of MIPS (little endian/big endian, mipsR1, mipsR2, mipsR6 etc.). V8 contains Debian Sid sysroot images for MIPS32ELR2 and MIPS64ELR2. To configure
For Mips32R2
gn gen out.gn/mipsel_clang --args='is_debug=false target_cpu="mipsel" v8_target_cpu="mipsel" mips_arch_variant="r2" is_clang=true target_os="linux" use_debug_fission=false'
For Mips64R2
gn gen out.gn/mips64el_clang --args='is_debug=true target_cpu="mips64el" v8_target_cpu="mips64el" mips_arch_variant="r2" is_clang=true target_os="linux" use_debug_fission=false'
To compile V8
ninja -C out.gn/mipsel_clang -j8
or ninja -C out.gn/mips64el_clang -j8
.
Switch -j8
specifies number of parallel processes for ninja to run, in this case 8. if you have more than one processor, this switch greatly reduces compilation time.
Cross compiling using GCC goes through the similar procedure as compiling using CLANG as in previous step. The only difference is that you need to have GCC toolchain installed and and path exported in PATH environment variable.
If you are using newer version of Ubuntu or Debian, these toolchains are available in their repositories. The toolchains for mips have following prefixed: mips-linux-gnu for mips32be, mipsel-linux-gnu for mips32le, mips64-linux-gnuabi64 for mips64be and mips64el-linux for mips64le. You can install them using apt.
In principle it should be possible to cross compile V8 using any mips GCC toolchain. If the toolchain has a different prefix than above, you can create symbolic links for all the binaries in the toolchain so the tools in the toolchain have correct prefixes. For example, create a symbolic link from mipsel-linux-gnu-g++ to mips-img-linux-g++. Do this for other tools in the toolchain.
When crosscompiling using GCC, set is_clang=false
in GN configuration line. This will force the compilation system to use GCC. When cross compiling, following issues can appear:
- The toolchain can give off warnings. V8 build system is configured to treat warnings as errors. To disable this behavior, specify
treat_warnings_as_errors=false
in GN configuration line - The libraries in the toolchain might be older and not support C++14. To fallback to C++11, specify
use_cxx11=true
in GN configuration line
Official toolchains are those that can be downloaded from mips.com. To download, visit here: https://codescape.mips.com/components/toolchain/2017.10-07/index.html
These toolchain come in two flavors: MTI for releases R2-R5 and IMG for release R6. These toolchains contain both the compiler and all the libraries that are needed to run V8.
Download the toolchain for your architecture and unpack it. You will need to use Linux toolchain, not bare metal toolchains. The tools in the toolchain have different prefixes from those that V8 expects, so you will need to create symbolic links. Execute following list of command in the directory containing tools of the toolchain:
For IMG toolchain
ln -s mips-img-linux-gnu-gcc mipsel-linux-gnu-gcc
ln -s mips-img-linux-gnu-g++ mipsel-linux-gnu-g++
ln -s mips-img-linux-gnu-ar mipsel-linux-gnu-ar
ln -s mips-img-linux-gnu-readelf mipsel-linux-gnu-readelf
ln -s mips-img-linux-gnu-nm mipsel-linux-gnu-nm
ln -s mips-img-linux-gnu-ld mipsel-linux-gnu-ld
ln -s mips-img-linux-gnu-gcc mips-linux-gnu-gcc
ln -s mips-img-linux-gnu-g++ mips-linux-gnu-g++
ln -s mips-img-linux-gnu-ar mips-linux-gnu-ar
ln -s mips-img-linux-gnu-readelf mips-linux-gnu-readelf
ln -s mips-img-linux-gnu-nm mips-linux-gnu-nm
ln -s mips-img-linux-gnu-ld mips-linux-gnu-ld
ln -s mips-img-linux-gnu-gcc mips64el-linux-gnuabi64-gcc
ln -s mips-img-linux-gnu-g++ mips64el-linux-gnuabi64-g++
ln -s mips-img-linux-gnu-ar mips64el-linux-gnuabi64-ar
ln -s mips-img-linux-gnu-readelf mips64el-linux-gnuabi64-readelf
ln -s mips-img-linux-gnu-nm mips64el-linux-gnuabi64-nm
ln -s mips-img-linux-gnu-ld mips64el-linux-gnuabi64-ld
ln -s mips-img-linux-gnu-gcc mips64-linux-gnuabi64-gcc
ln -s mips-img-linux-gnu-g++ mips64-linux-gnuabi64-g++
ln -s mips-img-linux-gnu-ar mips64-linux-gnuabi64-ar
ln -s mips-img-linux-gnu-readelf mips64-linux-gnuabi64-readelf
ln -s mips-img-linux-gnu-nm mips64-linux-gnuabi64-nm
ln -s mips-img-linux-gnu-ld mips64-linux-gnuabi64-ld
For MTI toolchain:
ln -s mips-mti-linux-gnu-g++ mipsel-linux-gnu-g++
ln -s mips-mti-linux-gnu-ar mipsel-linux-gnu-ar
ln -s mips-mti-linux-gnu-readelf mipsel-linux-gnu-readelf
ln -s mips-mti-linux-gnu-nm mipsel-linux-gnu-nm
ln -s mips-mti-linux-gnu-ld mipsel-linux-gnu-ld
ln -s mips-mti-linux-gnu-gcc mips-linux-gnu-gcc
ln -s mips-mti-linux-gnu-g++ mips-linux-gnu-g++
ln -s mips-mti-linux-gnu-ar mips-linux-gnu-ar
ln -s mips-mti-linux-gnu-readelf mips-linux-gnu-readelf
ln -s mips-mti-linux-gnu-nm mips-linux-gnu-nm
ln -s mips-mti-linux-gnu-ld mips-linux-gnu-ld
ln -s mips-mti-linux-gnu-gcc mips64el-linux-gnuabi64-gcc
ln -s mips-mti-linux-gnu-g++ mips64el-linux-gnuabi64-g++
ln -s mips-mti-linux-gnu-ar mips64el-linux-gnuabi64-ar
ln -s mips-mti-linux-gnu-readelf mips64el-linux-gnuabi64-readelf
ln -s mips-mti-linux-gnu-nm mips64el-linux-gnuabi64-nm
ln -s mips-mti-linux-gnu-ld mips64el-linux-gnuabi64-ld
ln -s mips-mti-linux-gnu-gcc mips64-linux-gnuabi64-gcc
ln -s mips-mti-linux-gnu-g++ mips64-linux-gnuabi64-g++
ln -s mips-mti-linux-gnu-ar mips64-linux-gnuabi64-ar
ln -s mips-mti-linux-gnu-readelf mips64-linux-gnuabi64-readelf
ln -s mips-mti-linux-gnu-nm mips64-linux-gnuabi64-nm
ln -s mips-mti-linux-gnu-ld mips64-linux-gnuabi64-ld
After you've setup the toolchain, you need to configure Here are the list of GN configuration options that must be set:
-
is_debug=true
oris_debug=false
-
target_cpu=
can be"mips"
,"mipsel"
,"mips64el"
or"mips64"
-
mips_arch_variant=
can be"r1"
,"r2¨
or"r6"
-
is_clang=false
use GCC, not CLANG target_os="linux"
-
use_sysroot=false
, we want to use libraries from the toolchain
Here is the list of optional GN configuration flags that may be used
-
use_cxx11=true
, if you are using an older version of libraries that do not support C++14, you will get some random linking errors. You can force C++11 compliant compilation using this flag -
treat_warnings_as_errors=false
, some toolchains can give off different warnings. Normally warnings are treated as errors and the compilation will stop. This will disable this kind of behavior -
v8_use_snapshot=false
, disable snapshots. Normally snapshots means faster V8. Unfortunately you cannot compile for BE with enabled snapshots, in order to enable snapshots on BE you need to follow instructions Building v8 for BE with QEMU.
In order to compile, first you need available your toolchain in your path:
export PATH=$PATH:/home/ivica/tools/codescape-2016.05-3-mips-img-linux-gnu/2016.05-03/bin
Next you need to configure V8 for the desired target options. Here are some examples of configuration lines used to configure different versions of V8 for MIPS:
gn gen out.gn/mipselr6_gcc --args='is_debug=true target_cpu="mips64el" mips_arch_variant="r6" is_clang=false target_os="linux" use_sysroot=false treat_warnings_as_errors=false'
gn gen out.gn/mipsr6_gcc --args='is_debug=true target_cpu="mips" v8_target_cpu="mips" mips_arch_variant="r6" is_clang=false target_os="linux" use_sysroot=false treat_warnings_as_errors=false use_cxx11=true'
And lastly, you need to start to compilation
ninja -C /path/to/gn/configuration -j8
GN is a meta build system of sorts, as it generates build files for a number of other build systems. How you build therefore depends on what "back-end" build system and compiler you're using
- Add the patch for fully static builds - or better yet, upstream it!
1: For V8, the important MIPS arch revisions are r1, r2, r6. Revisions r3, r4 (skipped), and r5 did not provide features in the general purpose part of the CPU that are useful for V8, so we do not support those revisions. However, mips r5 adds support for a SIMD unit, called MSA. I suspect that when SIMD support is fully added to V8, there will be MIPS support for r5 in addition to the others.
2: v8 also supports mips32r1, to support Broadcom devices. You can compile with GYPFLAGS="-Dmips_arch_variant=r1". However the Codescape toolchain linked above does not have support for mips32r1 libraries. You will have to find an appropriate toolchain.
3: On a mips64 board with only a mips32 userland, I found it quite useful to put the necessary libs in /lib64 and /usr/lib64 on the particular target board, and set LD_R_PATH
and LDSO_PATH
appropriately.