Skip to content

Commit

Permalink
Support kernel compilation with Clang/LLVM
Browse files Browse the repository at this point in the history
  • Loading branch information
Randl committed Aug 14, 2024
1 parent 5224c5b commit 921a7cf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/functions/compilation/kernel-make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ function run_kernel_make_internal() {
# Add the distcc envs, if any.
common_make_envs+=("${DISTCC_EXTRA_ENVS[@]}")

if [[ "${KERNEL_COMPILER}" == "clang" ]]; then
llvm_flag="LLVM=1"
cc_name="CC"
extra_warnings="-Wno-error=unused-command-line-argument -Wno-error=unknown-warning-option" # downgrade errors to warnings
else
cc_name="CROSS_COMPILE"
extra_warnings=""
fi
common_make_params_quoted=(
# @TODO: introduce O=path/to/binaries, so sources and bins are not in the same dir; this has high impact in headers packaging though.

Expand All @@ -45,8 +53,8 @@ function run_kernel_make_internal() {
"ARCH=${ARCHITECTURE}" # Key param. Everything depends on this.
"LOCALVERSION=-${BRANCH}-${LINUXFAMILY}" # Change the internal kernel version to include the family. Changing this causes recompiles # @TODO change hack at .config; that might handles mtime better

"CROSS_COMPILE=${CCACHE} ${DISTCC_CROSS_COMPILE_PREFIX[@]} ${KERNEL_COMPILER}" # added as prefix to every compiler invocation by make
"KCFLAGS=-fdiagnostics-color=always -Wno-error=misleading-indentation ${KERNEL_EXTRA_CFLAGS:-""}" # Force GCC colored messages, downgrade misleading indentation to warning
"${cc_name}=${CCACHE} ${DISTCC_CROSS_COMPILE_PREFIX[@]} ${KERNEL_COMPILER}" # added as prefix to every compiler invocation by make
"KCFLAGS=-fdiagnostics-color=always -Wno-error=misleading-indentation ${extra_warnings} ${KERNEL_EXTRA_CFLAGS:-""}" # Force GCC colored messages, downgrade misleading indentation to warning

"SOURCE_DATE_EPOCH=${kernel_base_revision_ts}" # https://reproducible-builds.org/docs/source-date-epoch/ and https://www.kernel.org/doc/html/latest/kbuild/reproducible-builds.html
"KBUILD_BUILD_TIMESTAMP=${kernel_base_revision_date}" # https://www.kernel.org/doc/html/latest/kbuild/kbuild.html#kbuild-build-timestamp
Expand All @@ -55,6 +63,9 @@ function run_kernel_make_internal() {

"KGZIP=pigz" "KBZIP2=pbzip2" # Parallel compression, use explicit parallel compressors https://lore.kernel.org/lkml/[email protected]/ # @TODO: what about XZ?
)
if [[ -n "${llvm_flag}" ]]; then
common_make_params_quoted+=("${llvm_flag}")
fi

# last statement, so it passes the result to calling function. "env -i" is used for empty env
full_command=("${KERNEL_MAKE_RUNNER:-run_host_command_logged}" "env" "-i" "${common_make_envs[@]}"
Expand Down Expand Up @@ -87,6 +98,11 @@ function kernel_determine_toolchain() {
[[ -z $toolchain ]] && exit_with_error "Could not find required toolchain" "${KERNEL_COMPILER}gcc $KERNEL_USE_GCC"
fi

kernel_compiler_version="$(eval env PATH="${toolchain}:${PATH}" "${KERNEL_COMPILER}gcc" -dumpfullversion -dumpversion)"
display_alert "Compiler version" "${KERNEL_COMPILER}gcc ${kernel_compiler_version}" "info"
if [[ "${KERNEL_COMPILER}" == "clang" ]]; then
KERNEL_COMPILER_FULL="${KERNEL_COMPILER}"
else
KERNEL_COMPILER_FULL="${KERNEL_COMPILER}gcc"
fi
kernel_compiler_version="$(eval env PATH="${toolchain}:${PATH}" "${KERNEL_COMPILER_FULL}" -dumpfullversion -dumpversion)"
display_alert "Compiler version" "${KERNEL_COMPILER_FULL} ${kernel_compiler_version}" "info"
}
6 changes: 6 additions & 0 deletions lib/functions/host/prepare-host.sh
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ function adaptative_prepare_host_dependencies() {
host_dependencies+=(libc6-amd64-cross) # Support for running x86 binaries (under qemu on other arches)
fi

if [[ "${KERNEL_COMPILER}" == "clang" ]]; then
host_dependencies+=("clang")
host_dependencies+=("llvm")
host_dependencies+=("lld")
fi

declare -g EXTRA_BUILD_DEPS=""
call_extension_method "add_host_dependencies" <<- 'ADD_HOST_DEPENDENCIES'
*run before installing host dependencies*
Expand Down

0 comments on commit 921a7cf

Please sign in to comment.