rustcをビルドする

プログラミング言語の勉強だから、コンパイラをビルドしてみるのも良いかと思う。

# ビルド
[rust-lang/rust](https://github.com/rust-lang/rust)に書いてある通り。Rustはこういう泥臭い系の作業が楽になるように作られている気がする。素晴らしい。

Ubuntu20.04にインストールする。ビルドに必要なパッケージをインストールする。

```shell
$ sudo apt install clang git python cmake ninja-build libssl-dev pkg-config
```

公式通りにconfig.tomlを作成する。

```shell
$ git clone https://github.com/rust-lang/rust.git
$ cd rust
$ cp config.toml.example config.toml
```

まずはビルドするところまで。

```shell
$ ./x.py build 
```

# ビルド中にでてきたメッセージ

```shell
/home/ken/rust/src/llvm-project/llvm/lib/IR/Core.cpp: In function ‘void LLVMContextSetDiagnosticHandler(LLVMContextRef, LLVMDiagnosticHandler, void*)’:
/home/ken/rust/src/llvm-project/llvm/lib/IR/Core.cpp:89:18: warning: cast between incompatible function types from ‘LLVMDiagnosticHandler’ {aka ‘void (*)(LLVMOpaqueDiagnosticInfo*, void*)’} to ‘llvm::DiagnosticHandler::DiagnosticHandlerTy’ {aka ‘void (*)(const llvm::DiagnosticInfo&, void*)’} [-Wcast-function-type]
   89 |           Handler),
      |                  ^
/home/ken/rust/src/llvm-project/llvm/lib/IR/Core.cpp: In function ‘void (* LLVMContextGetDiagnosticHandler(LLVMContextRef))(LLVMDiagnosticInfoRef, void*)’:
/home/ken/rust/src/llvm-project/llvm/lib/IR/Core.cpp:95:48: warning: cast between incompatible function types from ‘llvm::DiagnosticHandler::DiagnosticHandlerTy’ {aka ‘void (*)(const llvm::DiagnosticInfo&, void*)’} to ‘LLVMDiagnosticHandler’ {aka ‘void (*)(LLVMOpaqueDiagnosticInfo*, void*)’} [-Wcast-function-type]
   95 |       unwrap(C)->getDiagnosticHandlerCallBack());
      |                                                ^
[452/2616] Building CXX object lib/CodeGen/CMakeFiles/LLVMCodeGen.dir/ExecutionDomainFix.cpp.o
In file included from /home/ken/rust/src/llvm-project/llvm/include/llvm/CodeGen/ExecutionDomainFix.h:25,
                 from /home/ken/rust/src/llvm-project/llvm/lib/CodeGen/ExecutionDomainFix.cpp:9:
/home/ken/rust/src/llvm-project/llvm/include/llvm/ADT/SmallVector.h: In member function ‘void llvm::ExecutionDomainFix::visitSoftInstr(llvm::MachineInstr*, unsigned int)’:
/home/ken/rust/src/llvm-project/llvm/include/llvm/ADT/SmallVector.h:566:7: warning: array subscript 1 is outside array bounds of ‘int [1]’ [-Warray-bounds]
  566 |       ++EltPtr;
      |       ^~
/home/ken/rust/src/llvm-project/llvm/lib/CodeGen/ExecutionDomainFix.cpp:330:12: note: while referencing ‘rx’
  330 |   for (int rx : used) {
      |            ^~
```

操作ミスで途中でビルドを止めてしまったので、再度実行。

```shell
[331/1920] Building CXX object lib/MC/CMakeFiles/LLVMMC.dir/XCOFFObjectWriter.cpp.o
In file included from /usr/include/string.h:495,
                 from /usr/include/c++/9/cstring:42,
                 from /home/ken/rust/src/llvm-project/llvm/include/llvm/ADT/Hashing.h:53,
                 from /home/ken/rust/src/llvm-project/llvm/include/llvm/ADT/ArrayRef.h:12,
                 from /home/ken/rust/src/llvm-project/llvm/include/llvm/MC/MCAsmBackend.h:12,
                 from /home/ken/rust/src/llvm-project/llvm/lib/MC/XCOFFObjectWriter.cpp:14:
In function ‘char* strncpy(char*, const char*, size_t)’,
    inlined from ‘{anonymous}::Section::Section(const char*, llvm::XCOFF::SectionTypeFlags, bool, {anonymous}::CsectGroups)’ at /home/ken/rust/src/llvm-project/llvm/lib/MC/XCOFFObjectWriter.cpp:146:12:
/usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:34: warning: ‘char* __builtin_strncpy(char*, const char*, long unsigned int)’ specified bound 8 equals destination size [-Wstringop-truncation]
  106 |   return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
      |          ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[349/1920] Building CXX object lib/MC/MCParser/CMakeFiles/LLVMMCParser.dir/MasmParser.cpp.o
/home/ken/rust/src/llvm-project/llvm/lib/MC/MCParser/MasmParser.cpp:3833:6: warning: ‘bool {anonymous}::MasmParser::emitStructValue(const {anonymous}::StructInfo&)’ defined but not used [-Wunused-function]
 3833 | bool MasmParser::emitStructValue(const StructInfo &Structure) {
      |      ^~~~~~~~~~
[824/1920] Building CXX object lib/Target/AArch64/CMakeFiles/LLVMAArch64CodeGen.dir/GISel/AArch64InstructionSelector.cpp.o
/home/ken/rust/src/llvm-project/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp: In function ‘bool selectCopy(llvm::MachineInstr&, const llvm::TargetInstrInfo&, llvm::MachineRegisterInfo&, const llvm::TargetRegisterInfo&, const llvm::RegisterBankInfo&)’:
/home/ken/rust/src/llvm-project/llvm/lib/Target/AArch64/GISel/AArch64InstructionSelector.cpp:773:8: warning: variable ‘KnownValid’ set but not used [-Wunused-but-set-variable]
  773 |   bool KnownValid = false;
      |        ^~~~~~~~~~

```

cppのコード中にいくつかwarningが出ている。
ビルドが終わるまで、手元のマシンではしばらくかかる(執筆時点でまだビルド中)。
ビルドが終わったら、コードの構造を眺めていこう。

```shell
   Compiling smallvec v1.4.2
   Compiling crossbeam-utils v0.7.2
   Compiling memoffset v0.5.5
   Compiling crossbeam-epoch v0.8.2
   Compiling crossbeam-utils v0.6.6
   Compiling unicase v2.6.0
   Compiling itertools v0.8.2
   Compiling minifier v0.0.33
   Compiling crossbeam-queue v0.1.2
   Compiling num_cpus v1.13.0
   Compiling quote v1.0.7
   Compiling rand_core v0.5.1
   Compiling rand_chacha v0.2.2
   Compiling crossbeam-deque v0.7.3
   Compiling rand v0.7.3
   Compiling tempfile v3.1.0
   Compiling rustc-rayon v0.3.0
   Compiling rustdoc v0.0.0 (/home/ken/rust/src/librustdoc)
   Compiling rustdoc-tool v0.0.0 (/home/ken/rust/src/tools/rustdoc)
    Finished release [optimized] target(s) in 4m 40s
Build completed successfully in 3:12:37
```

無事ビルド終了。

# 参考資料
1. [Guide to Rustc Development](https://rustc-dev-guide.rust-lang.org/)