Changes between Version 2 and Version 3 of PortingRust


Ignore:
Timestamp:
Apr 11, 2015, 8:25:40 PM (4 years ago)
Author:
nielx
Comment:

Add all the steps for creating the stage 0 compiler

Legend:

Unmodified
Added
Removed
Modified
  • 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.
    7575
    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.
     77
     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.
     80
     81Set the following:
     82{{{
     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)
     86}}}
     87
     88=== Step 2.2: building llvm ===
     89
     90{{{
     91    cd $(RUST_HAIKU_STAGE0)
     92    mkdir llvm
     93    cd llvm
     94
     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"
     97}}}
     98
     99Notes:
     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).
     104
     105=== Step 2.2: compiler-rt ===
     106
     107{{{
     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
     112}}}
     113
     114=== Step 2.3: llvmdeps.rs ===
     115
     116The build of the module rustc_llvm depends on a generated file. This file is generated by [https://github.com/rust-lang/rust/blob/master/src/etc/mklldeps.py mklldeps.py] 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.
     117
     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 llvmdeps.rs 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.
     119
     120=== Step 2.4: rustrt  ===
     121
     122{{{
     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
     124
     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
     126
     127    i586-pc-haiku-ar rcs rt/librustrt_native.a rt/rust_try.o rt/arch/i386/record_sp.o
     128
     129
     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
     131
     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
     133
     134    i586-pc-haiku-ar rcs rt/librust_builtin.a rt/rust_builtin.o rt/rust_android_dummy.o
     135}}}
     136
     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).
     140
     141=== Step 2.5: libbacktrace  ===
     142
     143{{{
     144    mkdir -p rt/libbacktrace
     145
     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)
     147
     148    make -C rt/libbacktrace INCDIR=$(RUST_SOURCE_DIR)/src/libbacktrace
     149
     150    cp rt/libbacktrace/.libs/libbacktrace.a rt/libbacktrace.a
     151}}}
     152
     153
     154=== Step 2.6: copy the existing cross-compiler ===
     155
     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.
     157
     158{{{
     159    cp -R $(RUST_SOURCE_DIR)/x86_64-apple-darwin/stage2/* .
     160}}}
     161
     162=== Step 2.7 Compiling function ===
     163
     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.
     165
     166{{{
     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/lib.rs
     169}
     170}}}
     171
     172=== Step 2.8: compiling the libs ===
     173
     174{{{
     175    compile_rust_lib fmt_macros
     176
     177    compile_rust_lib syntax
     178
     179    mkdir rustllvm
     180
     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
     182
     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
     184
     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
     186
     187    i586-pc-haiku-ar rcs rt/librustllvm.a rustllvm/RustWrapper.o rustllvm/PassWrapper.o rustllvm/ExecutionEngineWrapper.o
     188
     189    CFG_LLVM_LINKAGE_FILE=$(PATH_TO_LLVMDEPS_DIRECTORY)/llvmdeps.rs compile_rust_lib rustc_llvm
     190
     191    compile_rust_lib rustc_back
     192
     193    compile_rust_lib rustc
     194
     195    compile_rust_lib rustc_borrowck
     196
     197    compile_rust_lib rustc_typeck
     198
     199    compile_rust_lib rustc_resolve
     200
     201    compile_rust_lib rustc_trans
     202
     203    compile_rust_lib rustc_privacy
     204
     205    compile_rust_lib rustc_driver
     206}}}
     207
     208Note that in a certain step we use the llvmdeps.rs file.
     209
     210=== Step 2.9: creating the binary ===
     211
     212{{{
     213mkdir -p lib/rustlib/i686-unknown-haiku/bin/
     214
     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/driver.rs --cfg rustc
     216}}}
     217
     218=== Step 3: using the binary to compile the source ===
     219...