目錄

用 ccache 來加速建置 edk2 吧

無意間查到也可以用 ccache 來加速建置 edk2,想說就來照著文章試試看吧。

先從 ccache 官網看一下介紹吧

About

Ccache (or “ccache”) is a compiler cache. It speeds up recompilation by caching previous compilations and detecting when the same compilation is being done again. Ccache is free software, released under the GNU General Public License version 3 or later. See also the license page.

Latest release: version 3.7.9

Features

  • Supports GCC, Clang and other similar compilers[1]. (details)
  • Works on Linux, macOS, other Unix-like operating systems[1] and Windows[1]. (details)
  • Understands C, C++, assembler, CUDA, Objective-C[1] and Objective-C++[1]. (details)
  • Supports fast “direct” and “depend” modes that don’t rely on using the preprocessor. (details)
  • Keeps statistics on hits/misses. (details)
  • Automatic cache size management. (details)
  • Easy installation. (details)
  • Low overhead. (details)
  • Support for rewriting absolute paths to relative in order to increase the cache hit ratio. (details)
  • Optionally uses hard links where possible to avoid copies.
  • [1] Might have limited support — see supported platforms, compilers and languages.

簡單說就是他能將 complier 的東西 cache 起來以達到加速的效果。

安裝 ccache

1
2
3
4
sudo apt update
sudo apt install -y ccache
# Update symbol links
sudo /usr/sbin/update-ccache-symlinks

先來檢查一下 gccg++ 的路徑

1
which gcc g++

正常來說預設的路徑應該會是在

1
2
/usr/bin/gcc
/usr/bin/g++

讓我們將 gccg++ 換成 ccache

1
export PATH="/usr/lib/ccache:$PATH"

接著我們再輸入 which gcc g++ 試試看

1
2
/usr/lib/ccache/gcc
/usr/lib/ccache/g++

這樣就修改成功了

修改 gcc/g++ 預設路徑

上面的修改方式只能用一次,logout 再進來就會發現又變回去原本的 gccg++ 了。

我們可以透過直接修改 /etc/environment 來永久修改這個 PATH

1
2
# PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
PATH="/usr/lib/ccache:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
若我用的是 fish shell 怎麼辦?

在 fish shell 下輸入下面這行即可

1
set -Ua fish_user_paths /usr/lib/ccache

接著按照原本的方式去建置 edk2 即可,若想要看到 cache 的狀態可以執行 ccache -s

$ ccache -s
cache directory                     /home/gitlab-runner/.ccache
primary config                      /home/gitlab-runner/.ccache/ccache.conf
secondary config      (readonly)    /etc/ccache.conf
stats zero time                     Sat May 23 15:36:59 2020
cache hit (direct)                  3904
cache hit (preprocessed)               0
cache miss                           488
cache hit rate                     88.89 %
called for preprocessing             252
no input file                        504
cleanups performed                     0
files in cache                       976
cache size                          16.0 MB
max cache size                       5.0 GB

可以看到重複建置同樣的 code 之後可以看到 88.89% 的 hit rate, 但實際上建置的時間卻沒有縮短多少 …. 就先到這邊吧。

參照

ccache — a fast C/C++ compiler cache

Edk2 compiler cache enabling steps on Linux

Edk2 compiler cache enabling steps on Windows

How to add a directory to the PATH?

How do I set up ccache?