armbian-build/lib/functions/compilation/patch/drivers-harness.sh

164 lines
6.3 KiB
Bash
Raw Normal View History

function kernel_drivers_create_patches_hash_only() {
hash_only="yes" kernel_drivers_create_patches "${@}"
}
function kernel_drivers_create_patches() {
kernel_drivers_patch_hash="undetermined" # outer scope
kernel_drivers_patch_file="undetermined" # outer scope
declare hash_files # any changes in these files will trigger a cache miss; also any changes in misc .patch with "wireless" at start or "wifi" anywhere in the name
calculate_hash_for_files "${SRC}/lib/functions/compilation/patch/drivers_network.sh" "${SRC}/lib/functions/compilation/patch/drivers-harness.sh" "${SRC}"/patch/misc/wireless*.patch "${SRC}"/patch/misc/*wifi*.patch
declare cache_key_base="${KERNEL_MAJOR_MINOR}_${LINUXFAMILY}"
declare cache_key="${cache_key_base}_${hash_files}"
display_alert "Cache key base:" "$cache_key_base" "debug"
display_alert "Cache key:" "$cache_key" "debug"
declare cache_dir_base="${SRC}/cache/patch/kernel-drivers"
mkdir -p "${cache_dir_base}"
declare cache_target_file="${cache_dir_base}/${cache_key}.patch"
# outer scope variables:
kernel_drivers_patch_file="${cache_target_file}" # outer scope
kernel_drivers_patch_hash="${hash_files}" # outer scope
if [[ "${hash_only:-"no"}" == "yes" ]]; then
armbian-next: artifacts: firmware/rootfs and actual usage of artifacts during image build - artifacts: introduce `ARTIFACT_IGNORE_CACHE=yes` - artifacts: introduce `DONT_BUILD_ARTIFACTS`, list of artifacts that if not found cached, fail the build - kernel_package_source() is no more - a long dissertation about kernels, families, and the universe - artifacts: actually use rootfs artifact for image build - artifacts: detangle via artifact_base_dir - artifacts: rootfs: use folders in artifact_name; include cache_type - artifacts: some cleanups / validations - rootfs artifact; drop old rootfs cli - artifacts: new CLI shortcuts; remove old firmware CLI - artifacts: full firmware & usage - use firmware artifacts in image build and install its debs - kernel artifact sans legacy; use tmpdir for .deb target for all packages - legacy artifact versions is no more; pack/unpack now in common obtain; - artifacts: uboot: cleanup legacy renaming, use artifact version directly - artifacts: add firmware (small) artifact - deploy uboot to loop from artifact; allow tty to artifact; todos for cleaning - fixes, kernel dtb/headers conditional; remove `.git` from Makefile url; use mapfile for finding files to hash - completely remove KERNEL_HAS_WORKING_HEADERS_FULL_SOURCE and `kernel_package_callback_linux_headers_full_source()` - don't use realpath for artifact_file_relative - curb some warnings - fix: only install headers & dtbs if such exist - kernel .config hook modification hash versioning - OCI_TARGET_BASE vs per-artifact defaults; only deploy to remote from CLI with OTB - artifact revolver & installing into image - add artifact_map_packages and artifact_map_debs dicts - revolver accumulates all info - REPOSITORY_INSTALL is no more (for uboot/kernel, later others) - rename `VER` to `IMAGE_INSTALLED_KERNEL_VERSION`
2023-02-03 14:36:28 +00:00
display_alert "Hash-only kernel driver requested" "$kernel_drivers_patch_hash - returning" "debug"
return 0
fi
declare kernel_work_dir="${1}"
declare kernel_git_revision="${2}"
# If the target file exists, we can skip the patch creation.
if [[ -f "${cache_target_file}" ]]; then
# Make sure the file is larger than 512 bytes. Old versions of this code left small/empty files on failure.
if [[ $(stat -c%s "${cache_target_file}") -gt 512 ]]; then
display_alert "Using cached drivers patch file for ${LINUXFAMILY}-${KERNEL_MAJOR_MINOR}" "${cache_key}" "cachehit"
return
else
display_alert "Removing invalid/small cached drivers patch file for ${LINUXFAMILY}-${KERNEL_MAJOR_MINOR}" "${cache_key}" "warn"
run_host_command_logged rm -fv "${cache_target_file}"
fi
fi
display_alert "Creating patches for kernel drivers" "version: '${KERNEL_MAJOR_MINOR}' family: '${LINUXFAMILY}'" "info"
# if it does _not_ exist, fist clear the base, so no old patches are left over
run_host_command_logged rm -fv "${cache_dir_base}/${cache_key_base}*"
# since it does not exist, go create it. this requires working tree.
declare target_patch_file="${cache_target_file}"
display_alert "Preparing patch for drivers" "version: ${KERNEL_MAJOR_MINOR} kernel_work_dir: ${kernel_work_dir}" "debug"
kernel_drivers_prepare_harness "${kernel_work_dir}" "${kernel_git_revision}"
}
function kernel_drivers_prepare_harness() {
declare kernel_work_dir="${1}"
declare kernel_git_revision="${2}"
# outer scope variable: target_patch_file
declare -a drivers=(
driver_generic_bring_back_ipx
driver_rtl8152_rtl8153
driver_rtl8189ES
driver_rtl8189FS
driver_rtl8192EU
driver_rtl8811_rtl8812_rtl8814_rtl8821
driver_xradio_xr819
driver_rtl8811CU_rtl8821C
driver_rtl8188EU_rtl8188ETV
driver_rtl88x2bu
driver_rtl88x2cs
driver_rtl8822cs_bt
driver_rtl8723DS
driver_rtl8723DU
driver_rtl8822BS
driver_uwe5622_allwinner
)
# change cwd to the kernel working dir
cd "${kernel_work_dir}" || exit_with_error "Failed to change directory to ${kernel_work_dir}"
#run_host_command_logged git status
run_host_command_logged git reset --hard "${kernel_git_revision}"
# git: remove tracked files, but not those in .gitignore
run_host_command_logged git clean -fd # no -x here
for driver in "${drivers[@]}"; do
display_alert "Preparing driver" "${driver}" "info"
# reset variables used by each driver
declare version="${KERNEL_MAJOR_MINOR}"
declare kernel_work_dir="${1}"
declare kernel_git_revision="${2}"
# for compatibility with `master`-based code
declare kerneldir="${kernel_work_dir}"
declare EXTRAWIFI="yes" # forced! @TODO not really?
# change cwd to the kernel working dir
cd "${kernel_work_dir}" || exit_with_error "Failed to change directory to ${kernel_work_dir}"
# invoke the driver; non-armbian-next code.
"${driver}"
# recover from possible cwd changes in the driver code
cd "${kernel_work_dir}" || exit_with_error "Failed to change directory to ${kernel_work_dir}"
done
# git: check if there are modifications
if [[ -n "$(git status --porcelain)" ]]; then
display_alert "Drivers have modifications" "exporting patch into ${target_patch_file}" "info"
export_changes_as_patch_via_git_format_patch
else
exit_with_error "Applying drivers didn't produce changes."
fi
}
function export_changes_as_patch_via_git_format_patch() {
# git: add all modifications
run_host_command_logged git add .
declare -a common_envs=(
"HOME=${HOME}"
"PATH=${PATH}"
)
# git: commit the changes
declare -a git_params=(
"-c" "commit.gpgsign=false" # force gpgsign off; the user might have it enabled and it will fail.
)
declare -a commit_params=(
"--quiet" # otherwise too much output
-m "drivers for ${LINUXFAMILY} version ${KERNEL_MAJOR_MINOR}"
--author="${MAINTAINER} <${MAINTAINERMAIL}>"
)
declare -a commit_envs=(
"GIT_COMMITTER_NAME=${MAINTAINER}"
"GIT_COMMITTER_EMAIL=${MAINTAINERMAIL}"
)
run_host_command_logged env -i "${common_envs[@]@Q}" "${commit_envs[@]@Q}" git "${git_params[@]@Q}" commit "${commit_params[@]@Q}"
# export the commit as a patch
declare formatpatch_params=(
"-1" "--stdout"
"--unified=3" # force 3 lines of diff context
"--keep-subject" # do not add a prefix to the subject "[PATCH] "
# "--no-encode-email-headers" # do not encode email headers - @TODO does not exist under focal, disable
'--signature' "Armbian generated patch from drivers for kernel ${version} and family ${LINUXFAMILY}"
'--stat=120' # 'wider' stat output; default is 80
'--stat-graph-width=10' # shorten the diffgraph graph part, it's too long
"--zero-commit" # Output an all-zero hash in each patchs From header instead of the hash of the commit.
)
declare target_patch_file_tmp="${target_patch_file}.tmp"
# The redirect ">" is escaped here, so it's run inside the subshell, not in the current shell.
run_host_command_logged env -i "${common_envs[@]@Q}" git format-patch "${formatpatch_params[@]@Q}" ">" "${target_patch_file_tmp}"
# move the tmp to final, if it worked.
run_host_command_logged mv -v "${target_patch_file_tmp}" "${target_patch_file}"
}