Skip to content

Commit 921a7cf

Browse files
committed
Support kernel compilation with Clang/LLVM
1 parent 5224c5b commit 921a7cf

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/functions/compilation/kernel-make.sh

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ function run_kernel_make_internal() {
3737
# Add the distcc envs, if any.
3838
common_make_envs+=("${DISTCC_EXTRA_ENVS[@]}")
3939

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

@@ -45,8 +53,8 @@ function run_kernel_make_internal() {
4553
"ARCH=${ARCHITECTURE}" # Key param. Everything depends on this.
4654
"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
4755

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

5159
"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
5260
"KBUILD_BUILD_TIMESTAMP=${kernel_base_revision_date}" # https://www.kernel.org/doc/html/latest/kbuild/kbuild.html#kbuild-build-timestamp
@@ -55,6 +63,9 @@ function run_kernel_make_internal() {
5563

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

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

90-
kernel_compiler_version="$(eval env PATH="${toolchain}:${PATH}" "${KERNEL_COMPILER}gcc" -dumpfullversion -dumpversion)"
91-
display_alert "Compiler version" "${KERNEL_COMPILER}gcc ${kernel_compiler_version}" "info"
101+
if [[ "${KERNEL_COMPILER}" == "clang" ]]; then
102+
KERNEL_COMPILER_FULL="${KERNEL_COMPILER}"
103+
else
104+
KERNEL_COMPILER_FULL="${KERNEL_COMPILER}gcc"
105+
fi
106+
kernel_compiler_version="$(eval env PATH="${toolchain}:${PATH}" "${KERNEL_COMPILER_FULL}" -dumpfullversion -dumpversion)"
107+
display_alert "Compiler version" "${KERNEL_COMPILER_FULL} ${kernel_compiler_version}" "info"
92108
}

lib/functions/host/prepare-host.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ function adaptative_prepare_host_dependencies() {
342342
host_dependencies+=(libc6-amd64-cross) # Support for running x86 binaries (under qemu on other arches)
343343
fi
344344

345+
if [[ "${KERNEL_COMPILER}" == "clang" ]]; then
346+
host_dependencies+=("clang")
347+
host_dependencies+=("llvm")
348+
host_dependencies+=("lld")
349+
fi
350+
345351
declare -g EXTRA_BUILD_DEPS=""
346352
call_extension_method "add_host_dependencies" <<- 'ADD_HOST_DEPENDENCIES'
347353
*run before installing host dependencies*

0 commit comments

Comments
 (0)