HGC(Human Genome Center)上にHomebrew(Linuxbrew)を構築する


## Introduction

先日のメンテナンスでLinuxbrew環境が壊れてしまったのでこの機会に整理しておく…

## Summary

- **$HOMEがsymlinkであるため、Homebrew上でリンクを辿らないようにするよう書き換えが必要**

- cacertが古いため(?)設定が必要
- 環境変数が特殊なためHOMEBREW_NO_ENV_FILTERINGが必要、それに伴いxzのCentOS 7用パッチが必要
- gitのmake時にNEEDS_LIBICONV=1が必要

## Prepare cacert

```sh
mkdir -p $HOME/.certs
wget --no-check-certificate -O - https://curl.se/ca/cacert.pem > $HOME/.certs/cacert.pem 
cat >> ~/.curlrc << EOM
cacert = $HOME/.certs/cacert.pem
capath = $HOME/.certs/
EOM
```

## Prepare .bashrc

```sh
export GIT_EXECUTABLE=/usr/local/package/git/2.20.0/bin/git
export HOMEBREW_NO_ENV_FILTERING=1
export HOMEBREW_CURLRC=1
```

## Bootstrap Homebrew

Install using older script which does not check curl version.

```sh
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/fb3acdd4ba286d3b08c980980ec63c8a888d1241/install.sh)"
```

## Patch Homebrew

```patch
diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh
index 6e6693c82..73d256ce3 100644
--- a/Library/Homebrew/cmd/update.sh
+++ b/Library/Homebrew/cmd/update.sh
@@ -728,7 +728,7 @@ EOS
       export HOMEBREW_UPDATE_BEFORE"${TAP_VAR}"="${CURRENT_REVISION}"
       export HOMEBREW_UPDATE_AFTER"${TAP_VAR}"="${CURRENT_REVISION}"
     else
-      merge_or_rebase "${DIR}" "${TAP_VAR}" "${UPSTREAM_BRANCH}"
+      #merge_or_rebase "${DIR}" "${TAP_VAR}" "${UPSTREAM_BRANCH}"
       [[ -n "${HOMEBREW_VERBOSE}" ]] && echo
     fi
   done
diff --git a/Library/Homebrew/extend/ENV/shared.rb b/Library/Homebrew/extend/ENV/shared.rb
index 381ceccfd..c64b7cac6 100644
--- a/Library/Homebrew/extend/ENV/shared.rb
+++ b/Library/Homebrew/extend/ENV/shared.rb
@@ -29,7 +29,7 @@ module SharedEnvExtension
     MACOSX_DEPLOYMENT_TARGET SDKROOT DEVELOPER_DIR
     CMAKE_PREFIX_PATH CMAKE_INCLUDE_PATH CMAKE_FRAMEWORK_PATH
     GOBIN GOPATH GOROOT PERL_MB_OPT PERL_MM_OPT
-    LIBRARY_PATH LD_LIBRARY_PATH LD_PRELOAD LD_RUN_PATH
+    LIBRARY_PATH LD_PRELOAD LD_RUN_PATH
   ].freeze
   private_constant :SANITIZED_VARS
 
diff --git a/Library/Homebrew/shims/super/cc b/Library/Homebrew/shims/super/cc
index c629efa9e..ab06eb15d 100755
--- a/Library/Homebrew/shims/super/cc
+++ b/Library/Homebrew/shims/super/cc
@@ -257,7 +257,7 @@ class Cmd
 
   def keep?(path)
     # Allow references to self
-    if formula_prefix && path.start_with?("#{formula_prefix}/")
+    if formula_prefix && (path.start_with?("#{formula_prefix}/") || path.start_with?("#{Pathname.new(formula_prefix).realpath}/"))
       true
     # first two paths: reject references to Cellar or opt paths
     # for unspecified dependencies
diff --git a/bin/brew b/bin/brew
index a6e4f8865..1f3f5cf7e 100755
--- a/bin/brew
+++ b/bin/brew
@@ -39,7 +39,7 @@ do
 done
 unset cmd
 
-BREW_FILE_DIRECTORY="$(quiet_cd "${0%/*}/" && pwd -P)"
+BREW_FILE_DIRECTORY="$(quiet_cd "${0%/*}/" && pwd)"
 HOMEBREW_BREW_FILE="${BREW_FILE_DIRECTORY%/}/${0##*/}"
 HOMEBREW_PREFIX="${HOMEBREW_BREW_FILE%/*/*}"
 
```

- The LD_LIBRARY_PATH thing is to keep libgcc available in homebrew world.
- Removing pwd -P (and allowing `Pathname.new(formula_prefix).realpath`) is very specific to HGC, **where $HOME is symlink.** Some symlinks must not be resolved or after home-disk migration your homebrew will be messed up.
- By the way in this way you cannot `brew update`. You need to git update .linuxbrew/Homebrew and each Taps.

## Install xz

Due to HOMEBREW_NO_ENV_FILTERING, /usr/bin/xz will load homebrew's liblzma, which is usually incompatible. You need patched xz:

```
brew install cielavenir/ciel/xz
```

## Upgrade git

```
NEEDS_LIBICONV=1 brew install git
```

## My Formula customizations

```patch
diff --git a/Formula/patchelf.rb b/Formula/patchelf.rb
index a7a9f22a260..65e315284b3 100644
--- a/Formula/patchelf.rb
+++ b/Formula/patchelf.rb
@@ -21,7 +21,7 @@ class Patchelf < Formula
   end
 
   on_linux do
-    depends_on "gcc"
+    #depends_on "gcc"
   end
 
   fails_with gcc: "5" # Needs std::optional
diff --git a/Formula/pkg-config.rb b/Formula/pkg-config.rb
index 6b845cdca5f..3cd38c37128 100644
--- a/Formula/pkg-config.rb
+++ b/Formula/pkg-config.rb
@@ -48,6 +48,7 @@ class PkgConfig < Formula
                           "--disable-host-tool",
                           "--with-internal-glib",
                           "--with-pc-path=#{pc_path}",
+                          "--with-libiconv=gnu",
                           "--with-system-include-path=#{MacOS.sdk_path_if_needed}/usr/include"
     system "make"
     system "make", "install"
```