<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>TJSomething&#39;s Something Blog</title>
    <description>My reference for how to do things I&#39;ve done.
</description>
    <link>http://tjsomething.github.io/</link>
    <atom:link href="http://tjsomething.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Fri, 17 Apr 2015 08:58:16 +0000</pubDate>
    <lastBuildDate>Fri, 17 Apr 2015 08:58:16 +0000</lastBuildDate>
    <generator>Jekyll v2.4.0</generator>
    
      <item>
        <title>Linking with the Rust Cross-Compiler</title>
        <description>&lt;p&gt;I’d like to start this blog by saying that it’s really easy to build a
Rust cross-compiler for Windows and Android on Linux. It’s just:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; /tmp/
&lt;span class=&quot;nv&quot;&gt;$ANDROID_NDK&lt;/span&gt;/build/tools/make-standalone-toolchain.sh &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    --platform&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;android-14 &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    --arch&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;arm &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    --install-dir&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$NDK_TOOLCHAIN&lt;/span&gt;
git clone https://github.com/rust-lang/rust
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;rust
mkdir build
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;build
../configure --target&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;arm-linux-androideabi,x86_64-unknown-linux-gnu,x86_64-pc-windows-gnu &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
    --android-cross-path&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$NDK_TOOLCHAIN&lt;/span&gt;
make
sudo make install
wget https://static.rust-lang.org/cargo-dist/cargo-nightly-x86_64-unknown-linux-gnu.tar.gz
tar xf cargo-nightly-x86_64-unknown-linux-gnu.tar.gz
./cargo-nightly-x86_64-unknown-linux-gnu/install.sh&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;where $ANDROID_NDK is where you put the &lt;a href=&quot;https://developer.android.com/tools/sdk/ndk/index.html&quot;&gt;Android NDK&lt;/a&gt;
and $NDK_TOOLCHAIN is a place you can keep a ~300MB compiler toolchain.&lt;/p&gt;

&lt;p&gt;However, there is a slight trick to getting Cargo to link code. If you get:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;/usr/bin/ld: /home/tommy/workspace/xlang-test/hello_world/target/arm-linux-androideabi/debug/hello_world.o: Relocations in generic ELF (EM: 40)
/home/tommy/workspace/xlang-test/hello_world/target/arm-linux-androideabi/debug/hello_world.o: error adding symbols: File in wrong format
collect2: error: ld returned 1 exit status&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;when you compile for Android or&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;note: /usr/bin/ld: unrecognized option &amp;#39;--enable-long-section-names&amp;#39;
/usr/bin/ld: use the --help option for usage information
collect2: error: ld returned 1 exit status&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;then you need to set your linkers. The first step is wrapping the Android linker, as all 
Android executables need to be compiled as position independent executables and
there’s no way to add arguments to linkers in the config. I’d recommend putting the
wrapper in the &lt;code&gt;~/.cargo/&lt;/code&gt; directory, in &lt;code&gt;android-linker&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;c&quot;&gt;#!/bin/bash&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;set&lt;/span&gt; -e
&lt;span class=&quot;nv&quot;&gt;$NDK_TOOLCHAIN&lt;/span&gt;/arm-linux-androideabi/bin/gcc -pie &lt;span class=&quot;s2&quot;&gt;&amp;quot;$@&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;except with &lt;code&gt;$NDK_TOOLCHAIN&lt;/code&gt; replaced with the absolute path to the toolchain, unless
you also decide to set that in your shell profile. Then, you can setup your Cargo configuration.&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arm-linux-androideabi&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;linker&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;$HOME/.cargo/android-linker&amp;quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x86_64-pc-windows-gnu&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;linker&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;x86_64-w64-mingw32-gcc&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;$HOME&lt;/code&gt; needs to be replaced with the actual absolute path of your home directory. Alternately,
you could put &lt;code&gt;android-linker&lt;/code&gt; in your path, but this avoids polluting your path with something
that will probably be rendered obsolete in the next few months.&lt;/p&gt;

&lt;p&gt;If you actually want to run your application in the context of Android, there are two ways to
do it. If you want to run a NativeActivity, you apparently need to extern your main function
and add support for an Android app struct. &lt;a href=&quot;https://github.com/tomaka/android-rs-glue&quot;&gt;android-rs-glue&lt;/a&gt;
seems to have this down, but it wasn’t working with my current nightlies and I wasn’t interested
in that use case. I wanted to run the Rust program as an executable from an app. So, I put the
Rust program in the &lt;code&gt;jniLibs&lt;/code&gt; directory in the app directory and renamed it so that it started
with &lt;code&gt;lib&lt;/code&gt; and ended with &lt;code&gt;.so&lt;/code&gt;. If you called it &lt;code&gt;libprogram.so&lt;/code&gt;, then it can be started like
this:&lt;/p&gt;

&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fileName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getApplicationContext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getFilesDir&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getParentFile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getPath&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&amp;quot;/lib/libprogram.so&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Process&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ProcessBuilder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fileName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;redirectErrorStream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

</description>
        <pubDate>Fri, 17 Apr 2015 00:00:00 +0000</pubDate>
        <link>http://tjsomething.github.io/rust/2015/04/17/rust-linker-cross-compiling.html</link>
        <guid isPermaLink="true">http://tjsomething.github.io/rust/2015/04/17/rust-linker-cross-compiling.html</guid>
        
        
        <category>rust</category>
        
      </item>
    
  </channel>
</rss>
