<div style="text-align: center; display: block; float: right;margin-left: 20px;margin-right: 20px;margin-bottom: 10px; text-align: justify;"><img width="300px" src="https://dl.dropboxusercontent.com/u/96059047/animation.gif"/>A C++ interpreter compiled for ARM running on an x86 EC2 instance after following the given instructions. As C++ becomes more mature and high-level, an interpreted workflow might lead to mainstream C++ <i>productivity</i> in addition to development!</div>

<p>Development through a C++ interpreter (Cling) as opposed to a standard compiler is an amazing leap in productivity and a window into the newest features of C++. This post tells you how to get your own bleeding-edge C++ interpreter built right on top of the development version of LLVM. We give you a repeatable procedure via Amazon EC2.</p>

<p>With our prescribed steps in place, you can always have an up-to-date development version of Cling. This allows quick testing and investigation of LLVM's newest features. You will also possess a development environment as a path to contributing to Cling or learning how Cling's innards push C++ code along.</p>

<h3 id="background">Background</h3>

<p>Astoundingly, there <strong>is</strong> such a thing as a C++ interpreter! Thus, if you're interested in sticking to C++ for speed and efficiency, you're not confined to the code-compile-run-debug workflow. Living outside such a workflow lends jumps in productivity and a unique angle through which to see C++.</p>

<p>Development of a serious C++ interpreter <a href="http://www.linuxjournal.com/article/2895">was undertaken by the wizards at CERN</a> starting at around 1995. (You might recognize this group as searchers <a href="http://home.web.cern.ch/topics/higgs-boson">for the Higgs Boson</a>). The C++ interpreter was dubbed <a href="https://root.cern.ch/drupal/content/cint">CINT</a> and was designed to support a major data analysis and visualization package known as <a href="https://root.cern.ch/drupal/">ROOT</a> (screenshot below). The system is certainly serious as it is giving researchers a way to investigate the very fabric of the universe!</p>

<p><img src="http://root.cern.ch/drupal/sites/default/files/images/alieve-esd-pbpb.png" alt="A ROOT screenshot being driven by a C++ interpreter (CINT). CINT is not a toy implementation - rather it is used to investigate the fabric of the universe." /></p>

<p>Over the last 20 years, CINT fell behind the evolving C++ language. In response, individuals at CERN began work on a replacement for CINT dubbed Cling with the strategy of leveraging LLVM to stay up to date with the growing C++ specification. Up until recently, the project has been rather nascent, but now it is at the point where its functionality and build system make Cling worthwhile for use and contribution.</p>

<h3 id="build-instructions-for-x86">Build instructions for x86</h3>

<p>We present a build procedure that leverages Amazon EC2. By using the cloud, you are guaranteed to use the exact Linux version and hardware capabilities that we did in testing. We assume that you have an Amazon EC2 account. The expected compute resource cost will be around two dollars.</p>

<ol>
<li><p>Create an <strong>m3.xlarge</strong> instance with the <a href="http://thecloudmarket.com/image/ami-aae18fc2--debian-jessie-amd64-hvm-2014-12-25-13-19-ebs">Debian Jessie AMI (ami-aae18fc2)</a>. Be sure to grab at least <strong>32Gb</strong> of storage.</p></li>
<li><p>Boot the instance and login as <strong>admin</strong>.</p></li>
<li><p>Install required build packages on the instance.</p>

<pre><code>sudo apt-get update
sudo apt-get install build-essential git
</code></pre></li>
<li><p>Use <a href="https://raw.githubusercontent.com/karies/cling-all-in-one/master/clone.sh">the distributed build script</a> from the Cling project to clone required source code and build an x86 version of Cling with all available cores.</p>

<pre><code>wget https://raw.githubusercontent.com/karies/cling-all-in-one/master/clone.sh
chmod +x clone.sh
./clone.sh           # will require a few minutes to finish
</code></pre>

<p>...this might take some time...</p>

<div style="text-align: center;">
  <img width="75%" style="padding: 20px;" src="https://dl.dropboxusercontent.com/u/96059047/cling_build.png" alt="Building cling on an x86 EC2 instance" />
</div></li>
<li><p>Take Cling for a spin!</p></li>
</ol>

<h3 id="build-instructions-for-arm">Build instructions for ARM</h3>

<p>Whereas an x86 build is readily supported by the Cling developers, building for ARM is not a standard build path. Thus, ARM build instructions are a bit more involved.</p>

<ol>
<li><p>Log in as <strong>admin</strong> to the (same x86) instance described.</p></li>
<li><p>First, you'll need the newest LLVM and Clang as they provide utilities for compilation: llvm-tblgen and clang-tblgen. To do so, perform a build of the current llvm git mirror,</p>

<pre><code>
</code></pre>

<li><p>Install ARM hard-float compilation tools.</p>

<pre><code>sudo apt-get update

# add Embedian as an installation source
sudo bash -c 'echo deb http://emdebian.org/tools/debian/ jessie main > \
              /etc/apt/sources.list.d/crosstools.list'
sudo apt-get install -y curl
curl http://emdebian.org/tools/debian/emdebian-toolchain-archive.key | sudo apt-key add -

# add arm hard-float as a supported architecture
sudo dpkg --add-architecture armhf
sudo apt-get update

# install an ARM targeted cross compiler
sudo apt-get install crossbuild-essential-armhf
</code></pre></li>
<li><p>Install QEMU-based ARM emulation. This is a fantastic feature of newer linux kernels: ARM executables are registered with the system so that under a normal invocation, QEMU is invoked under the hood so that it seems as though ARM binaries are native.</p>

<pre><code># install QEMU emulation for ARM executables
sudo apt-get install qemu binfmt-support qemu-user-static
sudo update-binfmts --display
sudo apt-get install libc6:armhf

# compile an ARM executable
cat > hello.c << EOF

#include <stdio.h>
int main(void) { return printf("Hello ARM!\n"); }
EOF
arm-linux-gnueabihf-gcc -static -ohello hello.c

# the following should indicate an ARM exe
file hello

# running the executable with no fanfare should
# transparently invoke QEMU to print "Hello ARM!"
./hello
</code></pre>

<p>Having ARM emulation on our fast build machine makes debugging amazingly faster.</p></li>
<li><p>Install build tools: cmake and ninja.</p>

<pre><code> # install cmake
 sudo apt-get install cmake

 # ninja requires a build
 git clone git://github.com/martine/ninja.git && cd ninja
 git checkout release
 ./configure.py --bootstrap
 chmod +x ninja build.ninja
 sudo cp ninja build.ninja /usr/bin
</code></pre></li>
<li><p>Clone LLVM, Clang, and Cling to the <strong>llvm</strong> directory. This directory differs from the instructions, above, so no worries if you're following these steps in the order of the blog post. Note the specific SHA's we jump to in the repositories. As of this writing, ARM support is in and out, but will hopefully materialize for good!</p>

<pre><code># the following steps assume this is done on a large volume
# mounted at /mnt resulting in /mnt/llvm.

git clone http://root.cern.ch/git/llvm.git
cd llvm
git checkout 88c56cd6d6aec0c44e9ca5aa96b354c1261a95aa
cd tools
git clone http://root.cern.ch/git/cling.git
cd cling
git checkout 899a5dde32cf670605a1478bea0b2d6fd3f95bef
cd ..
git clone http://root.cern.ch/git/clang.git
cd clang 
git checkout d649bf1a04be728fc09222d111e696fce4b55324
</code></pre></li
<li><p>Configure a build in a new <strong>build</strong> directory using cmake. Note the intense and carefully crafted build flags required to do a successful cross compilation. <em>This took quite a bit of debugging to lock down.</em></p>

<p>Note that the following assumes that the prior step was run in <code>/mnt</code> resulting in an <code>/mnt/llvm</code> directory. Indeed, the following block assumes that it is run in the <code>/mnt</code> directory, as well.</p> 

<pre><code>mkdir -p build; cd build
cmake -G Ninja /mnt/llvm -DCMAKE_CROSSCOMPILING=True \
                  -DCMAKE_SYSTEM_NAME=Linux \
                  -DCMAKE_SYSTEM_VERSION=1 \
                  -DCMAKE_SYSTEM_PROCESSOR=armv7 \
                  -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc \
                  -DCMAKE_CXX_COMPILER=arm-linux-gnueabihf-g++ \
                  -DCMAKE_INSTALL_PREFIX=/mnt/build/install \
                  -DLLVM_TABLEGEN=/usr/bin/llvm-tblgen-3.5 \
                  -DCLANG_TABLEGEN=/usr/bin/clang-tblgen-3.5 \
                  -DLLVM_DEFAULT_TARGET_TRIPLE=arm-linux-gnueabihf \
                  -DLLVM_HOST_TRIPLE=arm--linux-gnueabihf \
                  -DLLVM_TARGET_ARCH=ARM \
                  -DLLVM_TARGETS_TO_BUILD=ARM \
                  -DCMAKE_CXX_FLAGS="\
                      -mcpu=cortex-a9 \
                      -I/usr/include/arm-linux-gnueabihf/c++/4.9 \
                      -I/usr/lib/gcc/arm-linux-gnueabihf/4.9/include \
                      -mfloat-abi=hard"
</code></pre></li>
<li><p>Do a build with ninja and install to the <strong>install</strong> subdirectory. This will take a few minutes.</p>

<pre><code># from the /mnt/build directory
mkdir -p install
ninja install
</code></pre></li>
<li><p>Fire up Cling! ou can do this directly on your Amazon EC2 instance through QEMU, but watch out - it is a tad unstable in the case of recovering from syntax errors, etc. However, you can copy what you built to an ARM machine such as a Raspberry Pi or <a href="http://www.solid-run.com">Cubox</a> to take it for a whirl there, too!</p></li>
<pre><code>cd /mnt/build/bin
./cling
</code></pre>
<div style="text-align: center">
<img src="https://dl.dropboxusercontent.com/u/96059047/animation.gif"/></div>
</img>
</ol>

<p></body></html></p>
