MSYS2上でlibgit2をスタティックビルドし、サンプルのgitコマンド(lg2.exe)もスタティックビルドする方法。この手法を使えば One EXE で配布できる git 機能を持ったアプリケーションを作成できます。

[MSYS2](http://www.msys2.org/)上で[libgit2](https://github.com/libgit2/libgit2)をスタティックビルドし、サンプルのgitコマンド(lg2.exe)もスタティックビルドする方法。この手法を使えば One EXE で配布できる git 機能を持ったアプリケーションを作成できます。

# ビルド手順
## (1) ビルド環境の準備

```
$ pacman -S git make mingw-w64-x86_64-gcc mingw-w64-x86_64-cmake mingw-w64-x86_64-zlib mingw-w64-x86_64-openssl mingw-w64-x86_64-libssh2
```

## (2) libgit2のビルド

```
$ git clone https://github.com/libgit2/libgit2
$ cd libgit2
$ mkdir build && cd build
$ cmake .. -G "MSYS Makefiles" -DCMAKE_INSTALL_PREFIX=/mingw64 -DBUILD_SHARED_LIBS=OFF -DREGEX_BACKEND=builtin -DBUILD_CLAR=OFF
$ cmake --build . --target install
Scanning dependencies of target git2
[100%] Linking C static library ../libgit2.a
[100%] Built target git2
Install the project...
-- Install configuration: "Debug"
```

## (3) lg2.exe のビルド

libgit2/examples/CMakeLists.txt の内容を以下で置き換える。

```
project(lg2)

FILE(GLOB LG2_SOURCES *.c *.h)
ADD_EXECUTABLE(lg2 ${LG2_SOURCES})

# Ensure that we do not use deprecated functions internally
ADD_DEFINITIONS(-DGIT_DEPRECATE_HARD)

set(CMAKE_EXE_LINKER_FLAGS "-static")
TARGET_LINK_LIBRARIES(lg2 git2 ws2_32 z ssh2 winhttp crypt32 rpcrt4 crypto ssl ws2_32)
```

libgit2/examples/CMakeLists.txt の内容を書き換えた後に以下を実行して lg2.exe を作成する。

```
$ cd libgit2/examples
$ mkdir build && cd build
$ cmake .. -G "MSYS Makefiles"
$ cmake --build .
Scanning dependencies of target lg2
[  3%] Building C object CMakeFiles/lg2.dir/add.obj
[  7%] Building C object CMakeFiles/lg2.dir/args.obj
[ 10%] Building C object CMakeFiles/lg2.dir/blame.obj
[ 14%] Building C object CMakeFiles/lg2.dir/cat-file.obj
[ 17%] Building C object CMakeFiles/lg2.dir/checkout.obj
[ 21%] Building C object CMakeFiles/lg2.dir/clone.obj
[ 25%] Building C object CMakeFiles/lg2.dir/common.obj
[ 28%] Building C object CMakeFiles/lg2.dir/config.obj
[ 32%] Building C object CMakeFiles/lg2.dir/describe.obj
[ 35%] Building C object CMakeFiles/lg2.dir/diff.obj
[ 39%] Building C object CMakeFiles/lg2.dir/fetch.obj
[ 42%] Building C object CMakeFiles/lg2.dir/for-each-ref.obj
[ 46%] Building C object CMakeFiles/lg2.dir/general.obj
[ 50%] Building C object CMakeFiles/lg2.dir/index-pack.obj
[ 53%] Building C object CMakeFiles/lg2.dir/init.obj
[ 57%] Building C object CMakeFiles/lg2.dir/lg2.obj
[ 60%] Building C object CMakeFiles/lg2.dir/log.obj
[ 64%] Building C object CMakeFiles/lg2.dir/ls-files.obj
[ 67%] Building C object CMakeFiles/lg2.dir/ls-remote.obj
[ 71%] Building C object CMakeFiles/lg2.dir/merge.obj
[ 75%] Building C object CMakeFiles/lg2.dir/remote.obj
[ 78%] Building C object CMakeFiles/lg2.dir/rev-list.obj
[ 82%] Building C object CMakeFiles/lg2.dir/rev-parse.obj
[ 85%] Building C object CMakeFiles/lg2.dir/show-index.obj
[ 89%] Building C object CMakeFiles/lg2.dir/stash.obj
[ 92%] Building C object CMakeFiles/lg2.dir/status.obj
[ 96%] Building C object CMakeFiles/lg2.dir/tag.obj
[100%] Linking C executable lg2.exe
[100%] Built target lg2
```

Cランタイムにダイナミックリンクされてないことを確認。

```
$ cygcheck ./lg2.exe
C:\Users\user\Desktop\libgit2\examples\build\lg2.exe
  C:\Windows\system32\ADVAPI32.dll
    C:\Windows\system32\msvcrt.dll
      C:\Windows\system32\ntdll.dll
      C:\Windows\system32\KERNELBASE.dll
    C:\Windows\system32\SECHOST.dll
      C:\Windows\system32\RPCRT4.dll
    C:\Windows\system32\KERNEL32.dll
  C:\Windows\system32\CRYPT32.dll
    C:\Windows\system32\MSASN1.dll
  C:\Windows\system32\ole32.dll
    C:\Windows\system32\GDI32.dll
      C:\Windows\system32\win32u.dll
    C:\Windows\system32\USER32.dll
    C:\Windows\system32\combase.dll
      C:\Windows\system32\bcryptPrimitives.dll
  C:\Windows\system32\WINHTTP.dll
  C:\Windows\system32\WS2_32.dll
```

# 参考リンク
  * [CMake](https://cmake.org/)
  * [libgit2/libgit2](https://github.com/libgit2/libgit2)
  * [libgit2 API reference](https://libgit2.org/libgit2/#HEAD)