ChakraCoreをLinuxでビルドする


[Playing with ChakraCore on Linux](http://ariya.ofilabs.com/2016/01/playing-with-chakracore-on-linux.html) という記事でLinuxでのビルド方法が紹介されています。これをやってみます。

# ChakraCoreのビルドに必要な物

ターゲットプラットフォームは`x64 Ubuntu 15.10`です。
以下の二つがあれば、`Ubuntu 12.04`でもビルド可能です。

- CMake 3.2以上
- clang 3.5以上

# ビルド結果
```
cmake . && make
```
を実行します。30秒ちょいで、ビルドが終わります。びっくり。

なぜかと言うと、今は[Roadmap](https://github.com/Microsoft/ChakraCore/wiki/Roadmap#enhancing-host--platform-support)の

    [...] Get lib\common to build
    
の通り、現在はlib\commonの途中までしかビルドできません。

```
vagrant@precise64:/vagrant/ChakraCore$ make
[  1%] Building CXX object lib/common/codex/CMakeFiles/Chakra.Common.codex.dir/Utf8Codex.cpp.o
Linking CXX static library libChakra.Common.codex.a
[  1%] Built target Chakra.Common.codex
[  2%] Building CXX object lib/common/common/CMakeFiles/Chakra.Common.common.dir/Api.cpp.o
[  3%] Building CXX object lib/common/common/CMakeFiles/Chakra.Common.common.dir/cfglogger.cpp.o
[  5%] Building CXX object lib/common/common/CMakeFiles/Chakra.Common.common.dir/CommonCommonPch.cpp.o
[  6%] Building CXX object lib/common/common/CMakeFiles/Chakra.Common.common.dir/DateUtilities.cpp.o
[  7%] Building CXX object lib/common/common/CMakeFiles/Chakra.Common.common.dir/Event.cpp.o
[  8%] Building CXX object lib/common/common/CMakeFiles/Chakra.Common.common.dir/Int32Math.cpp.o
[ 10%] Building CXX object lib/common/common/CMakeFiles/Chakra.Common.common.dir/Int64Math.cpp.o
[ 11%] Building CXX object lib/common/common/CMakeFiles/Chakra.Common.common.dir/Jobs.cpp.o
[ 12%] Building CXX object lib/common/common/CMakeFiles/Chakra.Common.common.dir/MathUtil.cpp.o
[ 13%] Building CXX object lib/common/common/CMakeFiles/Chakra.Common.common.dir/NumberUtilities.cpp.o
[ 15%] Building CXX object lib/common/common/CMakeFiles/Chakra.Common.common.dir/RejitReason.cpp.o
[ 16%] Building CXX object lib/common/common/CMakeFiles/Chakra.Common.common.dir/SmartFPUControl.cpp.o
[ 17%] Building CXX object lib/common/common/CMakeFiles/Chakra.Common.common.dir/Tick.cpp.o
[ 18%] Building CXX object lib/common/common/CMakeFiles/Chakra.Common.common.dir/vtinfo.cpp.o
Linking CXX static library libChakra.Common.common.a
[ 18%] Built target Chakra.Common.common
[ 20%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/BinaryFeatureControl.cpp.o
[ 21%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/CmdParser.cpp.o
[ 22%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/CodexAssert.cpp.o
[ 23%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/CommonCorePch.cpp.o
[ 25%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/ConfigFlagsTable.cpp.o
[ 26%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/ConfigParser.cpp.o
[ 27%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/DbgHelpSymbolManager.cpp.o
[ 28%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/DelayLoadLibrary.cpp.o
[ 30%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/EtwTraceCore.cpp.o
[ 31%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/FaultInjection.cpp.o
[ 32%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/Output.cpp.o
[ 33%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/PerfCounter.cpp.o
[ 35%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/PerfCounterImpl.cpp.o
[ 36%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/PerfCounterSet.cpp.o
[ 37%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/ProfileInstrument.cpp.o
[ 38%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/ProfileMemory.cpp.o
[ 40%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/StackBackTrace.cpp.o
[ 41%] Building CXX object lib/common/core/CMakeFiles/Chakra.Common.core.dir/SysInfo.cpp.o
Linking CXX static library libChakra.Common.core.a
[ 41%] Built target Chakra.Common.core
[ 42%] Building CXX object lib/common/DataStructures/CMakeFiles/Chakra.Common.DataStructures.dir/BigInt.cpp.o
[ 43%] Building CXX object lib/common/DataStructures/CMakeFiles/Chakra.Common.DataStructures.dir/BufferBuilder.cpp.o
[ 45%] Building CXX object lib/common/DataStructures/CMakeFiles/Chakra.Common.DataStructures.dir/CommonDataStructuresPch.cpp.o
[ 46%] Building CXX object lib/common/DataStructures/CMakeFiles/Chakra.Common.DataStructures.dir/DictionaryStats.cpp.o
[ 47%] Building CXX object lib/common/DataStructures/CMakeFiles/Chakra.Common.DataStructures.dir/FixedBitVector.cpp.o
[ 48%] Building CXX object lib/common/DataStructures/CMakeFiles/Chakra.Common.DataStructures.dir/FixedBitVectorEnumerator.cpp.o
[ 50%] Building CXX object lib/common/DataStructures/CMakeFiles/Chakra.Common.DataStructures.dir/growingArray.cpp.o
[ 51%] Building CXX object lib/common/DataStructures/CMakeFiles/Chakra.Common.DataStructures.dir/ImmutableList.cpp.o
[ 52%] Building CXX object lib/common/DataStructures/CMakeFiles/Chakra.Common.DataStructures.dir/InternalString.cpp.o
[ 53%] Building CXX object lib/common/DataStructures/CMakeFiles/Chakra.Common.DataStructures.dir/InternalStringNoCaseComparer.cpp.o
[ 55%] Building CXX object lib/common/DataStructures/CMakeFiles/Chakra.Common.DataStructures.dir/Interval.cpp.o
[ 56%] Building CXX object lib/common/DataStructures/CMakeFiles/Chakra.Common.DataStructures.dir/SizePolicy.cpp.o
[ 57%] Building CXX object lib/common/DataStructures/CMakeFiles/Chakra.Common.DataStructures.dir/SparseBitVector.cpp.o
Linking CXX static library libChakra.Common.DataStructures.a
[ 57%] Built target Chakra.Common.DataStructures
[ 58%] Building CXX object lib/common/Exceptions/CMakeFiles/Chakra.Common.Exceptions.dir/ExceptionCheck.cpp.o
[ 60%] Building CXX object lib/common/Exceptions/CMakeFiles/Chakra.Common.Exceptions.dir/ExceptionBase.cpp.o
[ 61%] Building CXX object lib/common/Exceptions/CMakeFiles/Chakra.Common.Exceptions.dir/reporterror.cpp.o
[ 62%] Building CXX object lib/common/Exceptions/CMakeFiles/Chakra.Common.Exceptions.dir/Throw.cpp.o
Linking CXX static library libChakra.Common.Exceptions.a
[ 62%] Built target Chakra.Common.Exceptions
[ 63%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/Allocator.cpp.o
[ 65%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/ArenaAllocator.cpp.o
[ 66%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/CommonMemoryPch.cpp.o
[ 67%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/EtwMemoryTracking.cpp.o
[ 68%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/ForcedMemoryConstraints.cpp.o
[ 70%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/HeapAllocator.cpp.o
[ 71%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/HeapAllocatorOperators.cpp.o
[ 72%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/HeapBlockMap.cpp.o
[ 73%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/heapbucket.cpp.o
[ 75%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/IdleDecommitPageAllocator.cpp.o
[ 76%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/LargeHeapBucket.cpp.o
[ 77%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/leakreport.cpp.o
[ 78%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/MarkContext.cpp.o
[ 80%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/MemoryLogger.cpp.o
[ 81%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/MemoryTracking.cpp.o
[ 82%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/PageAllocator.cpp.o
[ 83%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/RecyclerHeuristic.cpp.o
[ 85%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/RecyclerObjectDumper.cpp.o
[ 86%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/RecyclerObjectGraphDumper.cpp.o
[ 87%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/RecyclerPageAllocator.cpp.o
[ 88%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/RecyclerSweep.cpp.o
[ 90%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/SmallFinalizableHeapBlock.cpp.o
[ 91%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/SmallFinalizableHeapBucket.cpp.o
[ 92%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/SmallHeapBlockAllocator.cpp.o
[ 93%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/SmallLeafHeapBlock.cpp.o
[ 95%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/SmallLeafHeapBucket.cpp.o
[ 96%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/SmallNormalHeapBlock.cpp.o
[ 97%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/SmallNormalHeapBucket.cpp.o
[ 98%] Building CXX object lib/common/Memory/CMakeFiles/Chakra.Common.Memory.dir/VirtualAllocWrapper.cpp.o
Linking CXX static library libChakra.Common.Memory.a
[ 98%] Built target Chakra.Common.Memory
[100%] Building CXX object lib/common/util/CMakeFiles/Chakra.Common.util.dir/pinned.cpp.o
Linking CXX static library libChakra.Common.util.a
[100%] Built target Chakra.Common.util
```


# Ubuntu 12.04 (Precise)の環境設定

Ubuntu 15.10 (Wily Werewolf)を使えば、必要なCMakeとclangがすんなりインストールできるようです。Ubuntu 12.04 (Precise)を使うと苦労します。

たまたま手元にあった環境(Precise)を使ったために苦労しました。

## CMake 3.2.3

apt-getで普通に入れると`2.8.7`が入ります。次のエラーが出ます。

```
vagrant@precise64:/vagrant/ChakraCore$ cmake .
CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
  CMake 3.2 or higher is required.  You are running version 2.8.7


-- Configuring incomplete, errors occurred!
```

[12.04 - CMAKE 3 binary package for PRECISE - Ask Ubuntu](http://askubuntu.com/questions/544598/cmake-3-binary-package-for-precise)に従って Personal Package Archivesからインストールします。

```
sudo apt-get install python-software-properties
sudo add-apt-repository ppa:george-edison55/precise-backports
sudo apt-get update
sudo apt-get install cmake
```

これで`cmake .`が通ります。

## clang 3.8

`make`を実行すると次のエラーが出ます。

```
/usr/include/x86_64-linux-gnu/sys/cdefs.h:32:3: error: #error "You need a ISO C conforming compiler to use the glibc headers"
# error "You need a ISO C conforming compiler to use the glibc headers"
  ^
In file included from /vagrant/ChakraCore/lib/common/codex/Utf8Codex.cpp:5:
In file included from /vagrant/ChakraCore/lib/common/codex/Utf8Codex.h:14:
/vagrant/ChakraCore/pal/inc/pal.h:816:13: error: empty parameter list defined with a typedef of 'void' not allowed in C++
        VOID);
            ^
```

このエラーはclangのバージョンが低いため出ます。
glibcヘッダーが求められる理由はさっぱりわかりません。

[Playing with ChakraCore on Linux](http://ariya.ofilabs.com/2016/01/playing-with-chakracore-on-linux.html) を読むと、ちゃんと

    ChakraCore Linux requires Clang version 3.5

と、書いてあります。

[[SOLVED] Problem installing llvm on ubuntu 12.04 LTS](http://ubuntuforums.org/showthread.php?t=2215803)を参考にします。

```
sudo add-apt-repository 'deb http://llvm.org/apt/precise/ llvm-toolchain-precise main'
wget -O - http://llvm.org/apt/llvm-snapshot.gpg.key|sudo apt-key add -
sudo apt-get update
```

`apt-cache search clang`を実行すると

```
vagrant@precise64:/vagrant/ChakraCore$ apt-cache search clang
llvm-3.0 - Low-Level Virtual Machine (LLVM)
clang - Low-Level Virtual Machine (LLVM), C language family frontend
libclang-common-dev - clang library - Common development package
libclang-dev - clang library - Development package
libclang1 - clang library
libsclang1 - SuperCollider language interpreter library
llvm-2.8 - Low-Level Virtual Machine (LLVM)
llvm-2.9 - Low-Level Virtual Machine (LLVM)
clang-3.3-doc - C, C++ and Objective-C compiler (LLVM based) - Documentation
libclang-3.3-dev - clang library - Development package
libclang-common-3.3-dev - clang library - Common development package
libclang1-3.3 - clang library
libclang1-3.3-dbg - clang library
llvm-3.1 - Low-Level Virtual Machine (LLVM)
llvm-3.2 - Low-Level Virtual Machine (LLVM)
llvm-3.3 - Low-Level Virtual Machine (LLVM)
llvm-3.4 - Modular compiler and toolchain technologies
clang-3.3 - C, C++ and Objective-C compiler (LLVM based)
clang-3.4 - C, C++ and Objective-C compiler (LLVM based)
clang-3.4-doc - C, C++ and Objective-C compiler (LLVM based) - Documentation
clang-3.4-examples - Clang examples
clang-format-3.3 - Tool to format C/C++/Obj-C code
clang-format-3.4 - Tool to format C/C++/Obj-C code
clang-modernize-3.4 - Tool to convert C++98 and C++03 code to C++11
cpp11-migrate-3.3 - Tool to convert C++98 and C++03 code to C++11
cpp11-migrate-3.4 - Tool to convert C++98 and C++03 code to C++11
libclang-3.4-dev - clang library - Development package
libclang-common-3.4-dev - clang library - Common development package
libclang1-3.4 - C interface to the clang library
libclang1-3.4-dbg - clang library
lldb-3.3 - Next generation, high-performance debugger
python-clang-3.4 - Clang Python Bindings
clang-3.8 - C, C++ and Objective-C compiler (LLVM based)
clang-3.8-doc - C, C++ and Objective-C compiler (LLVM based) - Documentation
clang-3.8-examples - Clang examples
clang-format-3.8 - Tool to format C/C++/Obj-C code
clang-tidy-3.8 - clang-based C++ linter tool
libclang-3.8-dev - clang library - Development package
libclang-common-3.8-dev - clang library - Common development package
libclang1-3.8 - C interface to the clang library
libclang1-3.8-dbg - clang library
liblldb-3.8 - Next generation, high-performance debugger, library
liblldb-3.8-dbg - Next generation, high-performance debugger, debugging libraries
liblldb-3.8-dev - Next generation, high-performance debugger - Header files
lldb-3.8 - Next generation, high-performance debugger
python-clang-3.8 - Clang Python Bindings
python-lldb-3.8 - Next generation, high-performance debugger, python lib
```

3.5以上のclangは3.8しか提供していないようです。

```
sudo apt-get install clang-3.8
```

でインストールします。
バイナリ名が`clang-3.8`になります。シンボリックリンクを作ります。

```
sudo ln -s /usr/bin/clang-3.8 /usr/bin/clang
```

makeが通ります。

# ファイルの差異
軽い気持ちで・・・

```
~ git diff origin/master --name-only | wc
     159     159    5190
```

Roadmapの13ステップ中の4ステップ目でこれです。なかなか大変そうです・・・

# まとめ
素直に`x64 Ubuntu 15.10`の仮想環境を作りましょう。