Changes between Version 2 and Version 3 of PortingRust

Apr 11, 2015, 10:25:40 PM (3 years ago)

Add all the steps for creating the stage 0 compiler


  • PortingRust

    v2 v3  
    7474The next series of steps are about using the freshly baked cross-compiler to create a stage0 compiler that will run on Haiku.
    76 Rust's build system either does not support doing this automatically, or the process is broken. As such what I did is basically looked through the compiler output of what the Rust compiler does, and then to perform these steps manually to create the libraries needed to create the compiler.
     76Rust's build system either does not support doing this automatically, or the process is broken. As such what I did is basically looked through the compiler output of what the Rust compiler does, and then to perform these steps manually to create the libraries needed to create the compiler.
     78=== Step 2.1: setting up the build directory and environment ===
     79I like to work in a clean directory, outside of the build tree. So I do the following. I create a new root folder, in which I build the various modules.
     81Set the following:
     83    export PATH=$PATH:$(HAIKU_BUILD_PATH)/cross-tools-x86/bin/
     84    export  CFG_COMPILER_HOST_TRIPLE=i686-unknown-haiku
     85    mkdir $(RUST_HAIKU_STAGE0)
     88=== Step 2.2: building llvm ===
     91    cd $(RUST_HAIKU_STAGE0)
     92    mkdir llvm
     93    cd llvm
     95    CC=i586-pc-haiku-gcc CPP=i586-pc-haiku-cpp CXX=i586-pc-haiku-g++ AR=i586-pc-haiku-ar RANLIB=i586-pc-haiku-ranlib NM_PATH=i586-pc-haiku-ranlib $(RUST_SOURCE_DIR)/src/llvm/configure --enable-targets=x86,x86_64,arm,aarch64,mips,powerpc --enable-optimized --enable-assertions --disable-docs --enable-bindings=none --disable-terminfo --disable-zlib --disable-libffi --enable-libcpp --build=x86_64-apple-darwin --host=i686-unknown-haiku --target=i686-unknown-haiku --with-python=/usr/bin/python2.7 --disable-libcpp
     96    make ONLY_TOOLS="bugpoint llc llvm-ar llvm-as llvm-dis llvm-mc opt llvm-extract"
     100* In the configure stage we need to set the various haiku build apps, like CC, CPP, CXX, NM, RANLIB and AR. If the last three are not explicitly set, the build system will use the host’s tools, which in my case were incompatible with the output of haiku’s gcc.
     101* —disable-libcpp: it might be because I am building on MacOS X where gcc is an alias for clang, but llvm wants to build with its own standard c library instead of with gcc’s. This becomes an issue during the build, as the build system will try to instruct gcc to use libc++, which is invalid.
     102* The ONLY_TOOLS are from rust’s build system make command
     103* The build process requires some tools that needed to be built and run during the build. In the configure stage you set the proper flags (build is darwin, host is haiku and target is haiku). After issuing make, the build system will invoke another instance of configure to properly build these host tools (where build, host and target are darwin).
     105=== Step 2.2: compiler-rt ===
     108    cd $(RUST_HAIKU_STAGE0)
     109    mkdir -p rt/compiler-rt
     110    make -C "$(RUST_SOURCE_DIR)/src/compiler-rt" ProjSrcRoot="$(RUST_SOURCE_DIR)/src/compiler-rt" ProjObjRoot="$(RUST_HAIKU_STAGE0)/rt/compiler-rt" CC="i586-pc-haiku-gcc" AR="i586-pc-haiku-ar" RANLIB="i586-pc-haiku-ar s" CFLAGS="-Wall -Werror -g -fPIC -m32 " TargetTriple=i686-unknown-haiku triple-builtins
     111    cp rt/compiler-rt/triple/builtins/libcompiler_rt.a rt/libcompiler-rt.a
     114=== Step 2.3: ===
     116The build of the module rustc_llvm depends on a generated file. This file is generated by [] that can be found in the Rust source. This script has to run on the target-platform, and it needs llvm-config compiled for that target platform.
     118For me, running the script outright did not work. So instead I copied by built llvm-config to Haiku, run the command that is used in the script, and manually built up the file. I have attached my file to this page, for your reference. Place this file in a known place, for example in the rt folder of the $(RUST_HAIKU_STAGE0) directory.
     120=== Step 2.4: rustrt  ===
     123    $(RUST_SOURCE_DIR)/x86_64-apple-darwin/llvm/Release+Asserts/bin/llc  -filetype=obj -mtriple=i686-unknown-haiku -relocation-model=pic -o rt/rust_try.o $(RUST_SOURCE_DIR)/src/rt/rust_try.ll
     125    i586-pc-haiku-gcc -E -E -MMD -MP -MT rt/arch/i386/record_sp.o -MF rt/arch/i386/record_sp.d $(RUST_SOURCE_DIR)/src/rt/arch/i386/record_sp.S | $(RUST_SOURCE_DIR)/x86_64-apple-darwin/llvm/Release+Asserts/bin/llvm-mc -assemble -filetype=obj -triple=i686-unknown-haiku -o=rt/arch/i386/record_sp.o
     127    i586-pc-haiku-ar rcs rt/librustrt_native.a rt/rust_try.o rt/arch/i386/record_sp.o
     130    i586-pc-haiku-gcc  -DRUST_DEBUG -O2 -Wall -Werror -g -fPIC -m32  -MMD -MP -MT  rt/rust_builtin.o -MF rt/rust_builtin.d -c -o rt/rust_builtin.o  -I $(RUST_SOURCE_DIR)/src/rt/hoedown/src -I $(RUST_SOURCE_DIR)/src/rt  $(RUST_SOURCE_DIR)/src/rt/rust_builtin.c
     132    i586-pc-haiku-gcc  -DRUST_DEBUG -O2 -Wall -Werror -g -fPIC -m32 -MMD -MP -MT  rt/rust_android_dummy.o -MF rt/rust_android_dummy.d -c -o  rt/rust_android_dummy.o  -I $(RUST_SOURCE_DIR)/src/rt/hoedown/src -I $(RUST_SOURCE_DIR)/src/rt  $(RUST_SOURCE_DIR)/src/rt/rust_android_dummy.c
     134    i586-pc-haiku-ar rcs rt/librust_builtin.a rt/rust_builtin.o rt/rust_android_dummy.o
     137Note about above:
     138* The rust build system generates the rust_android_dummy.o using `touch`. Haiku's ar did not like that as input though, so the file had to be generated with gcc.
     139* Note that this command depends on that a native build for the build platform exists in $(RUST_SOURCE_DIR).
     141=== Step 2.5: libbacktrace  ===
     144    mkdir -p rt/libbacktrace
     146    (cd rt/libbacktrace && CC="i586-pc-haiku-gcc" AR="i586-pc-haiku-ar" RANLIB="i586-pc-haiku-ar s" CFLAGS="-Wall  -g -fPIC -m32 -fno-stack-protector" $(RUST_SOURCE_DIR)/src/libbacktrace/configure --target=i686-unknown-haiku --host=x86_64-apple-darwin)
     148    make -C rt/libbacktrace INCDIR=$(RUST_SOURCE_DIR)/src/libbacktrace
     150    cp rt/libbacktrace/.libs/libbacktrace.a rt/libbacktrace.a
     154=== Step 2.6: copy the existing cross-compiler ===
     156The basis for the new stage 0 compiler will be the existing cross-compiler. This cross-compiler already generates haiku-binaries, and it also contains various dependencies for the compiler, such as libstd. The following command copies the bin/ and lib/ folders with our cross-compiler.
     159    cp -R $(RUST_SOURCE_DIR)/x86_64-apple-darwin/stage2/* .
     162=== Step 2.7 Compiling function ===
     164In the next steps we will use the existing cross-compiler to build our native compiler. In order to get all the commands right, I make a shell function.
     167function compile_rust_lib {
     168      DYLD_LIBRARY_PATH=$(RUST_HAIKU_STAGE0)/lib:$DYLD_LIBRARY_PATH   $(RUST_HAIKU_STAGE0)/bin/rustc --cfg stage0  -O --cfg rtopt --cfg debug  --target=i686-unknown-haiku -C linker=i586-pc-haiku-gcc  -C prefer-dynamic -W warnings -L "rt" -L "$(RUST_HAIKU_STAGE0)/llvm/Release+Asserts/lib" -L ""  --out-dir lib/rustlib/i686-unknown-haiku/lib -C extra-filename=-4e7c5e5c $(RUST_SOURCE_DIR)/src/lib$1/
     172=== Step 2.8: compiling the libs ===
     175    compile_rust_lib fmt_macros
     177    compile_rust_lib syntax
     179    mkdir rustllvm
     181    i586-pc-haiku-c++  -DRUST_DEBUG -O2  -Wall -Werror -g -fPIC -m32  -fno-rtti  -MMD -MP -MT  rustllvm/ExecutionEngineWrapper.o -MF rustllvm/ExecutionEngineWrapper.d -c -o  rustllvm/ExecutionEngineWrapper.o    -iquote $(RUST_SOURCE_DIR)/src/llvm/include  -iquote $(RUST_HAIKU_STAGE0)/llvm/include  -D_DEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -O3  -std=c++11 -fvisibility-inlines-hidden -fno-exceptions -fno-rtti -fno-common -Wcast-qual  -iquote $(RUST_SOURCE_DIR)/src/llvm/include -iquote $(RUST_SOURCE_DIR)/src/rustllvm/include $(RUST_SOURCE_DIR)/src/rustllvm/ExecutionEngineWrapper.cpp
     183    i586-pc-haiku-c++  -DRUST_DEBUG -O2  -Wall -Werror -g -fPIC -m32  -fno-rtti  -MMD -MP -MT  rustllvm/RustWrapper.o -MF rustllvm/RustWrapper.d -c -o  rustllvm/RustWrapper.o    -iquote $(RUST_SOURCE_DIR)/src/llvm/include  -iquote $(RUST_HAIKU_STAGE0)/llvm/include  -D_DEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -O3  -std=c++11 -fvisibility-inlines-hidden -fno-exceptions -fno-rtti -fno-common -Wcast-qual  -iquote $(RUST_SOURCE_DIR)/src/llvm/include -iquote $(RUST_SOURCE_DIR)/src/rustllvm/include $(RUST_SOURCE_DIR)/src/rustllvm/RustWrapper.cpp
     185    i586-pc-haiku-c++  -DRUST_DEBUG -O2  -Wall -Werror -g -fPIC -m32  -fno-rtti  -MMD -MP -MT  rustllvm/PassWrapper.o -MF rustllvm/PassWrapper.d -c -o  rustllvm/PassWrapper.o    -iquote $(RUST_SOURCE_DIR)/src/llvm/include  -iquote $(RUST_HAIKU_STAGE0)/llvm/include  -D_DEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -O3 -std=c++11 -fvisibility-inlines-hidden -fno-exceptions -fno-rtti -fno-common -Wcast-qual  -iquote $(RUST_SOURCE_DIR)/src/llvm/include -iquote $(RUST_SOURCE_DIR)/src/rustllvm/include $(RUST_SOURCE_DIR)/src/rustllvm/PassWrapper.cpp
     187    i586-pc-haiku-ar rcs rt/librustllvm.a rustllvm/RustWrapper.o rustllvm/PassWrapper.o rustllvm/ExecutionEngineWrapper.o
     189    CFG_LLVM_LINKAGE_FILE=$(PATH_TO_LLVMDEPS_DIRECTORY)/ compile_rust_lib rustc_llvm
     191    compile_rust_lib rustc_back
     193    compile_rust_lib rustc
     195    compile_rust_lib rustc_borrowck
     197    compile_rust_lib rustc_typeck
     199    compile_rust_lib rustc_resolve
     201    compile_rust_lib rustc_trans
     203    compile_rust_lib rustc_privacy
     205    compile_rust_lib rustc_driver
     208Note that in a certain step we use the file.
     210=== Step 2.9: creating the binary ===
     213mkdir -p lib/rustlib/i686-unknown-haiku/bin/
     215DYLD_LIBRARY_PATH=$(RUST_HAIKU_STAGE0)/lib:$DYLD_LIBRARY_PATH   $(RUST_HAIKU_STAGE0)/bin/rustc --cfg stage0  -O --cfg rtopt --cfg debug -Z no-landing-pads --target=i686-unknown-haiku -C linker=i586-pc-haiku-gcc -o lib/rustlib/i686-unknown-haiku/bin/rustc $(RUST_SOURCE_DIR)/src/driver/ --cfg rustc
     218=== Step 3: using the binary to compile the source ===