<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>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>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" target="_blank">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>

<p>sudo apt-get update
sudo apt-get install build-essential git</p></li>
<li><p>Use <a href="https://raw.githubusercontent.com/karies/cling-all-in-one/master/clone.sh" target="_blank">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>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
</pre>

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

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

<h3>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>Install ARM hard-float compilation tools.</p>

<p>&lt;</p>

<p>pre># 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 -</p></li>
</ol>

<h1>add arm hard-float as a supported architecture</h1>

<p>sudo dpkg --add-architecture armhf sudo apt-get update</p>

<h1>install an ARM targeted cross compiler</h1>

<p>sudo apt-get install crossbuild-essential-armhf </pre></p>

<ol>
<li>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.</li>
</ol>

<pre># 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 &lt;&lt; EOF

    # include &lt;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

    # invoke QEMU transparently to print "Hello ARM!"

    ./hello
    </pre>

<pre><code>Having ARM emulation on our fast build machine makes debugging amazingly faster.
</code></pre>

<ol>
<li>Install build tools: cmake and ninja.</li>
</ol>

<pre># 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
    </pre>

<ol>
<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.</p>

<p>&lt;</p>

<p>pre># the following steps aasume this is done in the home directory</p></li>
</ol>

<h1>and therefore yielding ~/llvm</h1>

<p>git clone http://root.cern.ch/git/llvm.git src cd src git checkout cling-patches cd tools git clone http://root.cern.ch/git/cling.git git clone http://root.cern.ch/git/clang.git cd clang git checkout cling-patches </pre></p>

<ol>
<li><p>Configure a build in the 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>

<pre>mkdir -p build && cd build
cmake -G Ninja ~/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=/home/admin/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'
</pre></li>
<li><p>Do a build with ninja and install to the <strong>install</strong> subdirectory.</p>

<pre># ninja here
</pre></li>
<li><p>Fire up Cling!</p></li>
</ol>
