Gentoo ccache

ccache 是非常重要的的编译加速工具(毕竟Gentoo就是依赖源代码编译不断滚动升级),通过从缓存目录获取结果来帮助避免对相同的 C 和 C++ 对象文件进行重复重新编译。

编译器缓存通常用于:

  • 多次重建相同/相似代码库并使用 /etc/portage/patches 来测试补丁的开发人员

  • 经常更改 USE 标志并最终多次重建相同包的用户

  • 广泛使用 live ebuild 的用户

  • 安装非常大的 ebuild ,例如 ChromiumLibreOffice ,无需担心因失败而丢失多个小时的代码编译

备注

对于我们这样不断折腾系统的人来说, ccache 简直是 居家旅行 杀人灭口 必备良药

安装和配置

  • 安装 dev-util/ccache :

安装 ccache
emerge --ask dev-util/ccache
  • 激活 ccache 非常简单,主要就是在 /etc/portage/make.conf 激活,例如在 在MacBook Pro上安装Gentoo Linux 过程中,先安装部署 ccache 并启用配置:

启用 ccache
# These settings were set by the catalyst build script that automatically
# built this stage.
# Please consult /usr/share/portage/config/make.conf.example for a more
# detailed example.
COMMON_FLAGS="-march=native -O2 -pipe"
CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}"

# If left undefined, Portage's default behavior is to set the MAKEOPTS value to the same number of threads returned by `nproc` 
MAKEOPTS="-j8"

# NOTE: This stage was built with the bindist Use flag enabled

# This sets the language of build output to English.
# Please keep this setting intact when reporting bugs.
LC_MESSAGES=C

GENTOO_MIRRORS="http://mirrors.aliyun.com/gentoo http://distfiles.gentoo.org"
ACCEPT_LICENSE="-* @FREE @BINARY-REDISTRIBUTABLE"

USE="wayland man dbus elogind alsa pulseaudio ogg gtk -systemd -X -mesa -gnome -qt5 -qt6 -kde -fortran"

VIDEO_CARDS="intel"

ACCEPT_KEYWORDS="~amd64"

FEATURES="ccache"
CCACHE_DIR="/var/cache/ccache"
  • 此外 /etc/ccache.conf 提供了一些控制参数,例如限制缓存磁盘大小:

调整ccache的缓存大小
# Maximum cache size to maintain
max_size = 20.0G

# Allow others to run 'ebuild' and share the cache.
umask = 002

# Don't include the current directory when calculating
# hashes for the cache. This allows re-use of the cache
# across different package versions, at the cost of
# slightly incorrect paths in debugging info.
# https://ccache.dev/manual/4.4.html#_performance
hash_dir = false

# Preserve cache across GCC rebuilds and
# introspect GCC changes through GCC wrapper.
#
# We use -dumpversion here instead of -v,
# see https://bugs.gentoo.org/872971.
compiler_check = %compiler% -dumpversion

# I expect 1.5M files. 300 files per directory.
cache_dir_levels = 3

# Logging setup is optional
# Portage runs various phases as different users
# so beware of setting a log_file path here: the file
# should already exist and be writable by at least
# root and portage. If a log_file path is set, don't
# forget to set up log rotation!
# log_file = /var/log/ccache.log
# Alternatively, log to syslog
# log_file = syslog

ccache.conf 还支持压缩功能,且可以设置压缩级别:

ccache 支持内容压缩
compression = true
compression_level = 1

参考