<em>What was necessary to make cross-compiling work for newer WriteFreely versions with SQLite support.</em>
<br/>
Last updated: <timedatetime="Thu, 10 Jan 2019 00:00:00 +0000">January 10, 2019</time>
</header>
<div><hr><p>This is a follow-up on <ahref="https://write.as/buttpicker/the-expected-tutorial-how-to-install-writefreely-on-a-raspberry-pi-3-in-10">The expected tutorial: How to install WriteFreely on a Raspberry pi 3 in 10 steps</a>.</p><hr><p>I did it! I finally got WriteFreely to run on my Arm server (check out <ahref="https://www.scaleway.com/baremetal-cloud-servers/">Scaleways baremetal cloud servers</a>).</p><p>It wasn't so easy because with 512MB of RAM I couldn't simply download and build the source on my webserver. Only solution: Cross compiling. Easy especially in Go, right?</p><p>If you read the article linked in the beginning you know how easy it could be. But as the article already mentions in an update, since Version 0.6 it is not working anymore because of the new SQLite dependency (newest version as of writing this article is 0.7).</p><p>With a bit of research I figured out what to do to make it work anyhow. There are two solutions. A quick (and slightly dirty) one for people who don't need SQLite support and a correct solution that needs a tad more effort.</p><h2>Quick solution: remove SQLite support</h2><p>SQLite support makes problems with the cross compiling because it needs some C code to be compiled. Before figuring out how to make this working with the otherwise super easy Go cross compiling, removing the feature might be a viable quick fix. For this, simply change or remove all occurences of sqlite in the Makefile:</p><pre><codeclass="diff"><spanclass="variable builtin">diff --git a/Makefile b/Makefile</span>
index <spanclass="constant">5950dfd</span>..<spanclass="constant">032fd0c</span> 100644
@hash go-bindata > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
</code></pre>
<p>Now just go on as described in the original article and it should work:</p><pre><codeclass="bash"><spanclass="constant">env</span><spanclass="constant">GOARCH=arm</span><spanclass="constant">GOARM=7</span><spanclass="constant">go</span><spanclass="constant">get</span><spanclass="constant">github.com/writeas/writefreely/cmd/writefreely</span>
</code></pre>
<h2>The correct solution</h2><p>To get WriteFreely cross compiled with SQLite support, a C cross compiler is needed. Void Linux, the distribution of my choice, offers a bunch of packages for all kind of architectures. They are called for example <code>cross-armv7l-linux-gnueabihf</code> (ARMv7), <code>cross-arm-linux-gnueabihf</code> (ARMv6) or <code>cross-arm-linux-gnueabi</code> (ARMv5). I found similar packages in AUR (for Arch Linux).</p><p>As soon as the corresponding cross compiler is found, go can be told to use it:</p><pre><codeclass="sh">env CGO_ENABLED=1 CC=armv7l-linux-gnueabihf-gcc GOOS=linux GOARCH=arm GOARM=7 make
</code></pre>
<p>The environment variables used are:</p><p><code>CGO_ENABLED=1</code> should be obvious. It tells Go to enable the C compilation.</p><p><code>CC=armv...</code> tells Go which C compiler to use. Usually this would be just <code>gcc</code>. In this case it is the name of the cross compiler. Please set it to the compiler for your target platform. I'm going to use ARMv7 examples here. It is the name of a directory found in <code>/usr/</code>, eg <code>/usr/armv7l-linux-gnueabihf</code>. Initially that failed for me though because it expected to find a file <code>./lib/libc.so</code> which ended up in another subfolder <code>/usr/</code>. So I cheated a bit and did:</p><pre><codeclass="bash"><spanclass="comment"># You might not need to do this on your platform.</span>
<p><code>GOOS=linux GOARCH=arm</code> are the same as in the original article.</p><p><code>GOARM=7</code> is optional, even on an actual ARMv7. It enables some register optimizations that only work on ARMv7.</p><p>And finally <code>make</code> is called. This is short for <code>make all</code> which should do everything necessary.</p><p>Not all files are necessary to be transferred to the Server or RaspberryPi. What I did after some experimentation was:</p><pre><codeclass="bash"><spanclass="comment"># after building everything create a package</span>
<spanclass="constant">mkdir</span><spanclass="constant">writefreely-arm/keys</span><spanclass="comment"># fun fact: key generation crashes without this</span>