rust icon indicating copy to clipboard operation
rust copied to clipboard

Implement cross-compilation to Android

Open brson opened this issue 13 years ago • 44 comments

We have Android code in tree but no way to build it. It should not be all that difficult to integrate the NDK since we have some minimal cross-compile support already.

brson avatar Jan 17 '13 01:01 brson

the libraries to be cross compiled are

  1. libuv (for librustrt.so as dependency ), Its already here https://github.com/ILyoan/libuv
  2. libcore.so
  3. librustrt .so
  4. libstd.so

Other tasks:

  1. setup standalone ndk toolchain http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html
  2. specifying NDK tool chain possibly in configure phase

amuxtux avatar Jan 17 '13 02:01 amuxtux

As mentioned previously, this is my branch where I have been hacking on Rust's cross-compile: https://github.com/brson/rust/commits/cross2

All it does so far is add CFG_HOST_TRIPLES and CFG_BUILD_TRIPLE. Under this scheme, the target triples would be systems that we build core and std, and for the host triples we would build the entire compiler. The build triple is the architecture we bootstrap from.

brson avatar Jan 31 '13 01:01 brson

How can one specify multiple host-triples or target-triples? Separating with a space doesn't seem to work.

yichoi avatar Jan 31 '13 10:01 yichoi

It looks to me like that "for loop" of target-triples make each target directory. Assuming arm-unknown-android as target triples and x86_64-apple-darwin as host-triples, One possible way to make build directory is to adding host-triples value to target-triples, the other possible way is to modify configure script to handle host-triples and target-triples separately.

yichoi avatar Jan 31 '13 11:01 yichoi

@yichoi on the command line they are specified like ../configure --target-triples=x86_64-unknown-linux-gnu,i686-unknown-linux-gnu

brson avatar Jan 31 '13 19:01 brson

@yichoi yeah, the host and target triples could be disjoint, though I've been assuming that target triples are a superset of host triples.

brson avatar Jan 31 '13 19:01 brson

@yichoi I pushed a cleanup commit to my cross2 branch that deletes a bunch of code to make further changes easier: https://github.com/brson/rust/commit/abb5c65aa035301b24af56256f70f636a7974152

I'm going to do a little more cleanup to my cross2 branch to make sure it works like I think it does (I'm testing e.g. configure --host-triples=x86_64-unknown-linux-gnu --target-triples=x86_64-unknown-linux-gnu,i686-unknown-linuxgnu with the intent that we build cross-compiled libraries, but not a cross-compiled compiler, for the non-host-triple target-triples. I'd like to know what you think about this approach. It might be best if you take over this work since you are more familiar with what exactly we need to do for Android.

I have some examples below of how I imagine the configuration should work and the directory structure it should produce.

In the makefiles on cross2 ''host' platforms are those for which we build ompilers; 'target' platforms are the platforms are the ones the 'host' compilers target.

As an example, in a scenario where CFG_HOST_TRIPLES=x86_64-unknown-linux-gnu and CFG_TARGET_TRIPLES=x86_64-unknown-linux-gnu arm-unknown-android I would expect to see final directory output like:

x86_64-unknown-linux-gnu/
  bin/
    - x86_64 compiler, etc.
  lib/
    - x86_64 libs
    rustc/
      x86_64-unknown-linux-gnu/
      arm-unknown-android/
        bin/
          - no contents here since all our binaries depend on librustc and we won't be building a rustc to run on arm
        lib/
          librt
          libcore
          libstd      

In an even more complex scenario where we are building the entire compiler for multiple architectures (as indicated by multiple host triples) we would expect the following output (and the current build does this sort of thing already).

x86_64-unknown-linux-gnu/
  - as above
i686-unknown-linux-gnu/
  bin/
  lib/
    rustc/
      x86_64-unknown-linux-gnu/
      arm-unknown-android/
        bin/
          - no contents here since all our binaries depend on librustc and we won't be building a rustc to run on arm
        lib/
          librt
          libcore
          libstd

Some next steps (I'll leave these to you if you like):

  • Each target triple needs to have it's own variables for controlling the C++ toolchain. A lot of this is defined in platform.mk
  • The configure script needs a flag like --enable-android-ndk=${path} to configure the above
  • rustc's linking logic in rustc::back::link will need to be modified. This code tends to be fairly platform-specific, but since the NDK uses gcc we may be able to get away with a simple change to specify which instance of gcc rustc should call to do the linking, maybe with a flag like --cc-name. If that isn't sufficient we may need to teach rustc about the android NDK itself, adding an --android-ndk-path= or similar. Perhaps this has been added already and just needs to be wired up to the makefiles.

brson avatar Jan 31 '13 20:01 brson

@graydon @nikomatsakis you may be interested in this subject

brson avatar Jan 31 '13 20:01 brson

I did some tweaks and testing to my branch and it gets through the full build under several cross-compile scenarios (just using x86 and i686), but tests.mk isn't quite right. I'm going to leave it as is until I get some feedback from @yichoi.

brson avatar Feb 01 '13 03:02 brson

@nikomatsakis asked on IRC why we don't just build the compiler for ARM. That would allow us to leave the build as it is. I had a strong belief that one wouldn't usually want a compiler to run on ARM, but that may not be true. It could be simpler to build compilers everywhere.

As an alternative to making this distinction between 'host' and 'target', which effectively just changes which binaries are built, we could teach the configure script to disable components or groups of components per target, and disable the ARM compiler by default.

brson avatar Feb 01 '13 07:02 brson

In case of x86_64-unknown-linux-gnu as host-triples, arm-unknown-android as target-triples. I've copied host-triples value to target-triples then made almost works except libstd of Rust building at stage2. I am investigating about that.

x86_64-unknown-linux-gnu/stage2/bin/rustc --cfg stage2 -O --target=arm-unknown-android -o x86_64-unknown-linux-gnu/stage2/lib/rustc/arm-unknown-android/lib/libstd.so /home/yichoi/rust_latest/src/libstd/std.rc && touch x86_64-unknown-linux-gnu/stage2/lib/rustc/arm-unknown-android/lib/libstd.so Call result #4 has unhandled type i32 UNREACHABLE executed at /home/yichoi/rust_latest/src/llvm/lib/CodeGen/CallingConvLower.cpp:165! Stack dump: 0. Running pass 'Function Pass Manager' on module 'std.rc'.

  1.  Running pass 'ARM Instruction Selection' on function '@rust_uv_ip4_addr__c_stack_shim'
    

In case of x86_64-apple-darwin as host-triples, arm-unknown-android as target-triples, I am thinking a way to specify CFG_LIB_NAME=lib$(1).so or CFG_LIB_NAME=lib$(1).dylib for each target-triples. It might need to expand configuration method in platform.mk

yichoi avatar Feb 03 '13 11:02 yichoi

you may check https://github.com/yichoi/rust/commits/cross3. However libstd.so cannot built yet.

yichoi avatar Feb 04 '13 06:02 yichoi

I believe this is because of foreign function abi. It used to use x86 cabi to register foreign function when target was given as arm-android, but now it is default (cabi::llvm_abi_info). I don't know the difference between two exactly, but after reverted to old setting it compiles well. And probably we might need to implement cabi_arm.rs ?

ILyoan avatar Feb 04 '13 09:02 ILyoan

https://github.com/yichoi/rust/commits/cross3 works with cabi_arm.rs regression patch. @ILyoan will send pull request of cabi_arm.rs regression patch.

yichoi avatar Feb 04 '13 10:02 yichoi

@yichoi Configuring with --target-triples=x86_64-unknown-linux-gnu,i686-unknon-linux-gnu I see a bunch of lines like:

/home/brian/dev/rust3/mk/target.mk:84: warning: overriding commands for target `x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib/libmorestack.a'
/home/brian/dev/rust3/mk/target.mk:84: warning: ignoring old commands for target `x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib/libmorestack.a'
/home/brian/dev/rust3/mk/target.mk:84: warning: overriding commands for target `x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib/librustrt.so'
/home/brian/dev/rust3/mk/target.mk:84: warning: ignoring old commands for target `x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib/librustrt.so'
/home/brian/dev/rust3/mk/target.mk:84: warning: overriding commands for target `x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib/libcore.so'
/home/brian/dev/rust3/mk/target.mk:84: warning: ignoring old commands for target `x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib/libcore.so'
/home/brian/dev/rust3/mk/target.mk:84: warning: overriding commands for target `x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib/libstd.so'
/home/brian/dev/rust3/mk/target.mk:84: warning: ignoring old commands for target `x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib/libstd.so'

Probably has to do with this line at the beginning of the output indicating I have duplicate target triples: cfg: target triples x86_64-unknown-linux-gnu x86_64-unknown-linux-gnu i686-unknown-linux-gnu

Then later the build fails with:

x86_64-unknown-linux-gnu/stage0/bin/rustc --cfg stage0  -O  --target=x86_64-unknown-linux-gnu -o x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib/librustc.so /home/brian/dev/rust3/src/librustc/rustc.rc && touch x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib/librustc.so
error: linking with `cc` failed with code 1
note: cc arguments: -L/home/brian/dev/rust3/build/x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib -m64 -o x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib/librustc-c84825241471686d-0.6.so x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib/librustc.o -L/home/brian/dev/rust3/build/x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib -lcore-c3ca5d77d81b46c1-0.6 -L/home/brian/dev/rust3/build/x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib -lstd-4782a756585a81-0.6 -L/home/brian/dev/rust3/build/x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib -lsyntax-84efebcb12c867a2-0.6 -Lrustllvm -lrustllvm -shared -lrustrt -lrt -ldl -lm -lmorestack -Wl,-rpath,$ORIGIN/. -Wl,-rpath,/home/brian/dev/rust3/build/x86_64-unknown-linux-gnu/stage0/lib/rustc/x86_64-unknown-linux-gnu/lib -Wl,-rpath,/usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib
note: /usr/bin/ld: cannot find -lrustllvm
collect2: error: ld returned 1 exit status

Which is fixed by this patch: https://github.com/brson/rust/commit/762502215ecebe8d301293145cdf27451a190714

The modifications to platform.mk look like they don't go far enough to me, requiring special cases in rt.mk, etc. to use the _arm compilers. Instead I think it makes sense to parametrize more of the compiler variables over the target triples (like much of the other code in the makefiles). Essentially, the MAKE_CC macro should be more like your MAKE_CC2 macro, allowing each target to have its own compiler. I realize this is a big change.

brson avatar Feb 05 '13 02:02 brson

@yichoi Nice job, and thank you for working on this.

brson avatar Feb 05 '13 02:02 brson

hm. perhaps you were expecting me to configure like ../configure --host-triples=x86_64-unknown-linux-gnu --target-triples=i686-unknown-linux-gnu, as the configure script copies triples from CFG_HOST_TRIPLES to CFG_TARGET_TRIPLES. That's a fair way to do it.

brson avatar Feb 05 '13 03:02 brson

Under x86_64-unknown-linux-gnu, ../configure --target-triples=arm-unknown-android works. Under x86_64-apple-darwin build, ../configure --target-triples=arm-unknown-android works too. currently I've modified for target-specific library extension (*.so, *.dylib).

yichoi avatar Feb 18 '13 09:02 yichoi

current arm latest working branch along with latest libuv https://github.com/yichoi/rust/commits/arm_latest

yichoi avatar Feb 22 '13 00:02 yichoi

@yichoi thanks. giving it a look now

brson avatar Feb 22 '13 00:02 brson