Skip to content

Commit 6dd6a7c

Browse files
committed
refactor(compilation): migrate ccache to extension; remove legacy ccache.sh
Wires the new ccache extension (extensions/ccache.sh) into the compilation pipeline, replacing the in-core lib/functions/compilation/ ccache.sh module that was sourced unconditionally. Changes: - lib/functions/compilation/kernel.sh, uboot.sh: wrap the compile pass with do_with_compile_wrapper so the new compile_wrapper_pre/ _post hooks fire around it. - lib/functions/configuration/compilation-config.sh: dispatch compile_prepare_vars (early phase, before any make-quoted-params array is built) instead of inlining configure-ccache. Also clears stale CCACHE export from a previous artifact iteration so a second uboot/kernel call in the same shell can't reuse a now- disabled cache. Keeps a one-shot deprecation warning if any user-patch still sources the legacy lib.config. - lib/functions/configuration/main-config.sh: if USE_CCACHE=yes (or the ccache-remote extension is enabled) auto-enable the 'ccache' extension before initialize_extension_manager runs. Honours both EXT alias and the legacy space-separated ENABLE_EXTENSIONS form. - lib/functions/general/extensions.sh: extract extension_list_normalized helper used by the auto-enable shim. - lib/functions/compilation/ccache.sh: removed (logic moved to extensions/ccache.sh). Behaviour is preserved for USE_CCACHE=yes (default-on for kernel/uboot builds) and USE_CCACHE=no callers. ENABLE_EXTENSIONS=ccache-remote implicitly pulls in ccache, matching pre-refactor coupling. Assisted-by: Claude:claude-opus-4.7
1 parent 05a1f86 commit 6dd6a7c

7 files changed

Lines changed: 91 additions & 138 deletions

File tree

lib/functions/compilation/ccache.sh

Lines changed: 0 additions & 110 deletions
This file was deleted.

lib/functions/compilation/kernel.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,11 @@ function kernel_build() {
256256
cd "${kernel_work_dir}" || exit_with_error "Can't cd to kernel_work_dir: ${kernel_work_dir}"
257257

258258
display_alert "Building kernel" "${LINUXFAMILY} ${LINUXCONFIG} ${build_targets_build[*]}" "info"
259-
do_with_ccache_statistics \
259+
do_with_compile_wrapper \
260260
run_kernel_make_long_running "${install_make_params_quoted[@]@Q}" "${build_targets_build[@]}" # "V=1" # "-s" silent mode, "V=1" verbose mode
261261

262262
display_alert "Installing kernel" "${LINUXFAMILY} ${LINUXCONFIG} ${build_targets_install[*]}" "info"
263-
do_with_ccache_statistics \
263+
do_with_compile_wrapper \
264264
run_kernel_make_long_running "${install_make_params_quoted[@]@Q}" "${build_targets_install[@]}" # "V=1" # "-s" silent mode, "V=1" verbose mode
265265

266266
display_alert "Kernel built in" "$((SECONDS - ts)) seconds - ${version}-${LINUXFAMILY}" "info"

lib/functions/compilation/uboot.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ function compile_uboot_target() {
270270

271271
display_alert "${uboot_prefix}Compiling u-boot" "${version} ${target_make} with gcc '${gcc_version_main}'" "info"
272272
declare -g if_error_detail_message="${uboot_prefix}Failed to build u-boot ${version} ${target_make}"
273-
do_with_ccache_statistics run_host_command_logged_long_running \
273+
do_with_compile_wrapper run_host_command_logged_long_running \
274274
"env" "-i" "${uboot_make_envs[@]}" \
275275
pipetty make "$target_make" "$CTHREADS" "${cross_compile}"
276276

lib/functions/configuration/compilation-config.sh

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,38 @@
88
# https://github.com/armbian/build/
99

1010
function prepare_compilation_vars() {
11-
# moved from config: rpardini: ccache belongs in compilation, not config. I think.
12-
if [[ $USE_CCACHE == yes || ${PRIVATE_CCACHE} == yes ]]; then
13-
display_alert "using CCACHE" "USE_CCACHE or PRIVATE_CCACHE is set to yes" "warn"
14-
15-
CCACHE=ccache
16-
export PATH="/usr/lib/ccache:$PATH" # this actually needs export'ing
17-
# private ccache directory to avoid permission issues when using build script with "sudo"
18-
# see https://ccache.samba.org/manual.html#_sharing_a_cache for alternative solution
19-
[[ $PRIVATE_CCACHE == yes ]] && export CCACHE_DIR=$SRC/cache/ccache # actual export
20-
21-
# Set default umask for ccache to allow write access for all users (enables cache sharing)
22-
# CCACHE_UMASK=000 creates files with permissions 666 (rw-rw-rw-) and dirs with 777 (rwxrwxrwx)
23-
# Only set this for shared cache, not for private cache
24-
[[ -z "${CCACHE_UMASK}" && "${PRIVATE_CCACHE}" != "yes" ]] && export CCACHE_UMASK=000
25-
else
26-
CCACHE=""
11+
# Backend-agnostic dispatch point for compile-cache extensions
12+
# (ccache, sccache, …). Runs after every extension_prepare_config_*
13+
# (so values set late by other extensions — e.g. PRIVATE_CCACHE from
14+
# ccache-remote — are visible) and before run_*_make_internal builds
15+
# the env-i make arrays (so ${CCACHE} substitution and PATH prepend
16+
# propagate correctly). Order-independent by construction.
17+
#
18+
# Reset CCACHE to empty first so a previous USE_CCACHE=yes build in
19+
# the same shell (or a CCACHE exported in the user's environment)
20+
# does not silently leak the wrapper into a USE_CCACHE=no run. Any
21+
# enabled backend extension will assign CCACHE inside its hook.
22+
declare -g CCACHE=""
23+
call_extension_method "compile_prepare_vars" <<- 'COMPILE_PREPARE_VARS'
24+
*compile-cache env setup hook for ccache / sccache / similar backends*
25+
Called once early in default_build_start, after all extension
26+
prepare_config hooks have run and before kernel/u-boot/ATF/Crust
27+
make invocations begin. Implementations export the env vars their
28+
backend needs (CCACHE, CCACHE_DIR, CCACHE_UMASK, SCCACHE_DIR, …)
29+
so later array-building code captures them, and tweak PATH if a
30+
wrapper prefix directory is needed.
31+
COMPILE_PREPARE_VARS
32+
33+
# Framework-level limitation: USE_CCACHE / PRIVATE_CCACHE set in
34+
# userpatches/lib.config (sourced after initialize_extension_manager —
35+
# see main-config.sh:443 "too late to define hook functions or add
36+
# extensions in lib.config") cannot auto-enable the ccache extension.
37+
# Warn the user so they can migrate; we deliberately do not duplicate
38+
# the env-setup logic here — it lives in extensions/ccache.sh as the
39+
# sole owner of the ccache backend.
40+
if [[ ("${USE_CCACHE}" == "yes" || "${PRIVATE_CCACHE}" == "yes") && -z "${CCACHE}" ]]; then
41+
display_alert "USE_CCACHE / PRIVATE_CCACHE set, but no compile-cache extension is active" \
42+
"likely set in userpatches/lib.config (framework limit: too late to enable extensions there). Add 'ccache' to ENABLE_EXTENSIONS / EXT in your CLI, env, or config file sourced earlier." "warn"
2743
fi
2844

2945
# moved from config: this does not belong in configuration. it's a compilation thing.
@@ -36,7 +52,7 @@ function prepare_compilation_vars() {
3652
# If CPUTHREADS is defined and a valid positive integer allow user to override CTHREADS
3753
# This is useful for limiting Armbian build to a specific number of threads, e.g. for build servers
3854
if [[ "$CPUTHREADS" =~ ^[1-9][0-9]*$ ]]; then
39-
CTHREADS="-j$CPUTHREADS"
55+
CTHREADS="-j$CPUTHREADS"
4056
echo "Using user-defined thread count: $CTHREADS"
4157
fi
4258

lib/functions/configuration/main-config.sh

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function do_main_configuration() {
4747
unset VENDORSUPPORT,VENDORPRIVACY,VENDORBUGS,VENDORLOGO,ROOTPWD,MAINTAINER,MAINTAINERMAIL
4848
fi
4949

50-
[[ -z $VENDORCOLOR ]] && VENDORCOLOR="247;16;0" # RGB values for MOTD logo
50+
[[ -z $VENDORCOLOR ]] && VENDORCOLOR="247;16;0" # RGB values for MOTD logo
5151
[[ -z $VENDORURL ]] && VENDORURL="https://duckduckgo.com/"
5252
[[ -z $VENDORSUPPORT ]] && VENDORSUPPORT="https://community.armbian.com/"
5353
[[ -z $VENDORPRIVACY ]] && VENDORPRIVACY="https://duckduckgo.com/"
@@ -60,7 +60,7 @@ function do_main_configuration() {
6060
DEST_LANG="${DEST_LANG:-"en_US.UTF-8"}" # en_US.UTF-8 is default locale for target
6161
display_alert "DEST_LANG..." "DEST_LANG: ${DEST_LANG}" "debug"
6262

63-
declare -g USE_CCACHE="${USE_CCACHE:-no}" # stop using ccache as our worktree is more effective
63+
declare -g USE_CCACHE="${USE_CCACHE:-no}" # stop using ccache as our worktree is more effective
6464

6565
# Armbian config is central tool used in all builds. As its build externally, we have moved it to extension. Enable it here.
6666
enable_extension "armbian-config"
@@ -173,7 +173,7 @@ function do_main_configuration() {
173173

174174
# Support for LUKS / cryptroot
175175
if [[ $CRYPTROOT_ENABLE == yes ]]; then
176-
enable_extension "fs-cryptroot-support" # add the tooling needed, cryptsetup
176+
enable_extension "fs-cryptroot-support" # add the tooling needed, cryptsetup
177177
if [[ -z $CRYPTROOT_PASSPHRASE ]] && [[ -z $CRYPTROOT_AUTOUNLOCK ]]; then # a passphrase is mandatory if rootfs encryption is enabled, unless CRYPTROOT_AUTOUNLOCK is wanted
178178
exit_with_error "Root encryption is enabled but CRYPTROOT_PASSPHRASE or CRYPTROOT_AUTOUNLOCK is not set"
179179
fi
@@ -326,14 +326,48 @@ function do_main_configuration() {
326326
;;
327327
esac
328328

329-
# enable APA extension for Debian Unstable release
329+
# enable APA extension for Debian Unstable release
330330
# loong64 is not supported now
331331
# [ "$RELEASE" = "sid" ] && [ "$ARCH" != "loong64" ] && enable_extension "apa"
332332

333333
## Extensions: at this point we've sourced all the config files that will be used,
334334
## and (hopefully) not yet invoked any extension methods. So this is the perfect
335335
## place to initialize the extension manager. It will create functions
336336
## like the 'post_family_config' that is invoked below.
337+
338+
# Auto-enable the ccache compile-cache extension when:
339+
# - the legacy toggle is set (USE_CCACHE=yes / PRIVATE_CCACHE=yes), OR
340+
# - the ccache-remote extension is requested (it sets USE_CCACHE=yes
341+
# itself inside extension_prepare_config, too late for this
342+
# shim to react to the toggle directly, so we trigger on the
343+
# extension name instead).
344+
# Skip when a competing compile-cache extension (sccache, …) is already
345+
# requested. Mutex resolution lives inside the extensions themselves;
346+
# the regex below just prevents redundant auto-enabling.
347+
#
348+
# Placed immediately before initialize_extension_manager so all
349+
# in-do_main_configuration sources of USE_CCACHE / PRIVATE_CCACHE /
350+
# ENABLE_EXTENSIONS are visible. The extension-list normalisation
351+
# (EXT fallback + comma/whitespace handling) is shared with the
352+
# per-extension mutex checks via extension_list_normalized.
353+
#
354+
# Note: USE_CCACHE set in userpatches/lib.config is a framework-level
355+
# limitation — lib.config is sourced after initialize_extension_manager
356+
# (see line ~443 'too late to define hook functions or add extensions
357+
# in lib.config'). For that scenario prepare_compilation_vars emits a
358+
# warning at compile phase nudging users to migrate to
359+
# ENABLE_EXTENSIONS=ccache.
360+
local _ext_list
361+
_ext_list="$(extension_list_normalized)"
362+
if [[ "${USE_CCACHE}" == "yes" ||
363+
"${PRIVATE_CCACHE}" == "yes" ||
364+
"${_ext_list}" == *,ccache-remote,* ]]; then
365+
if [[ ! "${_ext_list}" == *,ccache,* && ! "${_ext_list}" == *,sccache,* ]]; then
366+
enable_extension "ccache"
367+
fi
368+
fi
369+
unset _ext_list
370+
337371
initialize_extension_manager
338372

339373
call_extension_method "post_family_config" "config_tweaks_post_family_config" <<- 'POST_FAMILY_CONFIG'

lib/functions/general/extensions.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ function extension_hook_opt_out() {
2424
fi
2525
}
2626

27+
# Normalize the active extension list for substring matching.
28+
# Mirrors initialize_extension_manager's resolution (ENABLE_EXTENSIONS:-EXT)
29+
# and accepts both comma- and whitespace-separated forms. Emits a canonical
30+
# `,name1,name2,…,` string suitable for `[[ "$list" == *,name,* ]]` checks.
31+
# Stdout: the normalized list. Useful in both the in-core auto-enable shim
32+
# and per-extension mutex checks between mutually-exclusive extensions.
33+
function extension_list_normalized() {
34+
local raw="${ENABLE_EXTENSIONS:-${EXT:-}}"
35+
local list=",${raw// /,},"
36+
while [[ "$list" == *,,* ]]; do list="${list//,,/,}"; done
37+
echo "$list"
38+
}
39+
2740
function extension_manager_declare_globals() {
2841
# global variables managing the state of the extension manager. treat as private.
2942
declare -g -A extension_function_info # maps a function name to a string with KEY=VALUEs information about the defining extension
@@ -590,7 +603,7 @@ function enable_extensions_with_hostdeps_builtin_and_user() {
590603
display_alert "Extension search" "Searching in directory: \"${ext_dir}\"" ""
591604
if [[ -d "${ext_dir}" ]]; then
592605
declare -a ext_list_dir=()
593-
mapfile -t ext_list_dir < <(find "${ext_dir}" -maxdepth 2 -type f -name "*.sh" -print0 | xargs -0 -r grep -l "${grep_args[@]}" 2>/dev/null || true)
606+
mapfile -t ext_list_dir < <(find "${ext_dir}" -maxdepth 2 -type f -name "*.sh" -print0 | xargs -0 -r grep -l "${grep_args[@]}" 2> /dev/null || true)
594607
display_alert "Extension search result" "Found ${#ext_list_dir[@]} extensions in \"${ext_dir}\"" ""
595608
extension_list+=("${ext_list_dir[@]}")
596609
else

lib/library-functions.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,9 @@ source "${SRC}"/lib/functions/compilation/atf.sh
285285
#set -o nounset ## set -u : exit the script if you try to use an uninitialised variable - one day will be enabled
286286
set -o errtrace # trace ERR through - enabled
287287
set -o errexit ## set -e : exit the script if any statement returns a non-true return value - enabled
288-
### lib/functions/compilation/ccache.sh
289-
# shellcheck source=lib/functions/compilation/ccache.sh
290-
source "${SRC}"/lib/functions/compilation/ccache.sh
288+
### lib/functions/compilation/compile-wrapper.sh
289+
# shellcheck source=lib/functions/compilation/compile-wrapper.sh
290+
source "${SRC}"/lib/functions/compilation/compile-wrapper.sh
291291

292292
# no errors tolerated. invoked before each sourced file to make sure.
293293
#set -o pipefail # trace ERR through pipes - will be enabled "soon"

0 commit comments

Comments
 (0)