blob: 9de884923010a94d2f1a3def29c514906c2af7f2 [file] [log] [blame]
#!/bin/sh
# Copyright (C) 2024 MediaTek Inc. All rights reserved.
# Author: Weijie Gao <weijie.gao@mediatek.com>
# Top rules
# Declare substages of every stage
sub_stage_prepare="update_ab_info mk_ab_tmp pre_prepare apply_autobuild_script_patch modify_feeds_conf update_feeds \
mtk_feed_prepare install_feeds autobuild_prepare post_prepare"
sub_stage_build="update_ab_info pre_build do_build post_build"
sub_stage_release="pre_release do_release post_release"
sub_stage_sdk_release="pre_sdk_release do_sdk_release post_sdk_release"
sub_stage_menuconfig="do_menuconfig_update"
# Declare hooks of every substage
hooks_pre_prepare=
hooks_mtk_feed_prepare="remove_files_by_mtk_feed_list copy_mtk_feed_files apply_mtk_feed_base_patches \
apply_mtk_feed_feed_patches"
hooks_autobuild_prepare="remove_files_by_global_list copy_global_files apply_global_patches \
remove_files_by_platform_list copy_platform_files apply_platform_patches \
remove_files_by_wifi_list copy_wifi_files apply_wifi_patches \
remove_files_by_sku_list copy_sku_files apply_sku_patches \
remove_files_by_variant_list copy_variant_files apply_variant_patches \
prepare_wifi_driver prepare_openwrt_config platform_change_openwrt_config \
wifi_change_openwrt_config sku_change_openwrt_config variant_change_openwrt_config \
enable_openwrt_collect_debug_symbols make_defconfig update_target_info \
platform_change_kernel_config wifi_change_kernel_config sku_change_kernel_config \
variant_change_kernel_config enable_kernel_proc_config_gz"
hooks_post_prepare="prepare_stamp"
hooks_pre_build=
hooks_do_build="download_openwrt_packages build_openwrt"
hooks_post_build=
hooks_pre_release="update_target_info mk_ab_bin_release"
hooks_do_release="collect_openwrt_images collect_openwrt_configs collect_kernel_debug_symbols \
collect_userspace_debug_symbols collect_feeds_buildinfo"
hooks_post_release=
hooks_pre_sdk_release=
hooks_do_sdk_release="release_openwrt_sdk"
hooks_post_sdk_release=
# Global information
build_time=
internal_build=
mtk_feed_path=
kernel_ver=
target_name=
subtarget_name=
openwrt_bin_dir=
openwrt_config_file=
kernel_config_file=
# Generic function for modifying openwrt config, to be compliant with menuconfig
# $1: Config name
openwrt_config_disable() {
if test -z "${do_menuconfig}"; then
if kconfig_enabled "${openwrt_config_file}" "${1}"; then
kconfig_disable "${openwrt_config_file}" "${1}"
fi
fi
}
# $1: Config name
# $2: Value to be set for config (y if not specified)
openwrt_config_enable() {
if test -z "${do_menuconfig}"; then
if ! kconfig_enabled "${openwrt_config_file}" "${1}"; then
kconfig_enable "${openwrt_config_file}" "${1}" "${2}"
fi
fi
}
# $1: Config name
openwrt_config_enabled() {
kconfig_enabled "${openwrt_config_file}" "${1}"
}
# Generic function for modifying target's kernel config
# $1: Config name
kernel_config_disable() {
if kconfig_enabled "${kernel_config_file}" "${1}"; then
kconfig_disable "${kernel_config_file}" "${1}"
fi
}
# $1: Config name
# $2: Value to be set for config (y if not specified)
kernel_config_enable() {
if ! kconfig_enabled "${kernel_config_file}" "${1}"; then
kconfig_enable "${kernel_config_file}" "${1}" "${2}"
fi
}
# $1: Config name
kernel_config_enabled() {
kconfig_enabled "${kernel_config_file}" "${1}"
}
# Whether this is for internal build
update_ab_info() {
if test -z "${internal_build}"; then
if test -d "${openwrt_root}/../mtk-openwrt-feeds"; then
log_dbg "Internal repo build mode"
internal_build=1
fi
fi
}
# Create the ${ab_tmp} directory
mk_ab_tmp() {
exec_log "mkdir -p \"${ab_tmp}\""
}
# Modify scripts for specific features:
# 1. allow specify feed subdirectory for scanning
# 2. allow config merging and diff
apply_autobuild_script_patch() {
copy_files "${ab_root}/scripts/${openwrt_branch}/files"
apply_patches "${ab_root}/scripts/${openwrt_branch}/patches"
}
modify_feeds_conf() {
local rev_file_list=
local feed_rev=
local feed_url="https://git01.mediatek.com/openwrt/feeds/mtk-openwrt-feeds"
# Backup original feeds
exec_log "cp -f \"${openwrt_root}/feeds.conf.default\" \"${ab_tmp}/\""
# Modify feeds
openwrt_feeds_replace_url packages https://gerrit.mediatek.inc/openwrt/feeds/packages
openwrt_feeds_replace_url luci https://gerrit.mediatek.inc/openwrt/feeds/luci
openwrt_feeds_replace_url routing https://gerrit.mediatek.inc/openwrt/feeds/routing
openwrt_feeds_disable telephony
openwrt_feeds_change_src_git_type packages 0
openwrt_feeds_change_src_git_type luci 0
openwrt_feeds_change_src_git_type routing 0
# Add mtk-openwrt-feeds
[ -n "${ab_variant_dir}" ] && list_append rev_file_list "${ab_variant_dir}/feed_revision"
[ -n "${ab_sku_dir}" ] && list_append rev_file_list "${ab_sku_dir}/feed_revision"
[ -n "${ab_wifi_dir}" ] && list_append rev_file_list "${ab_wifi_dir}/feed_revision"
[ -n "${ab_platform_dir}" ] && list_append rev_file_list "${ab_platform_dir}/feed_revision"
list_append rev_file_list "${ab_root}/feed_revision"
for rev_file in ${rev_file_list}; do
if test -f "${rev_file}"; then
feed_rev=$(cat "${rev_file}")
break;
fi
done
[ -n "${feed_rev}" ] && feed_rev="^${feed_rev}"
if test -n "${internal_build}"; then
feed_url="${openwrt_root}/../mtk-openwrt-feeds"
fi
if test -n "${internal_build}" -a -z "${feed_rev}"; then
openwrt_feeds_add mtk_openwrt_feed src-link "${feed_url}" --subdir=feed
else
openwrt_feeds_add mtk_openwrt_feed src-git "${feed_url}${feed_rev}" --subdir=feed
fi
}
update_feeds() {
exec_log "${openwrt_root}/scripts/feeds update -a -r -s"
mtk_feed_path=${openwrt_root}/feeds/mtk_openwrt_feed
}
remove_files_by_mtk_feed_list() {
remove_files_from_list "${mtk_feed_path}/common/remove_list.txt"
remove_files_from_list "${mtk_feed_path}/${openwrt_branch}/remove_list.txt"
}
copy_mtk_feed_files() {
copy_files "${mtk_feed_path}/common/files"
copy_files "${mtk_feed_path}/tools" tools
copy_files "${mtk_feed_path}/${openwrt_branch}/files"
}
apply_mtk_feed_base_patches() {
apply_patches "${mtk_feed_path}/${openwrt_branch}/patches-base"
}
apply_mtk_feed_feed_patches() {
apply_patches "${mtk_feed_path}/${openwrt_branch}/patches-feeds"
}
install_feeds() {
exec_log "${openwrt_root}/scripts/feeds install -a"
}
remove_files_by_global_list() {
remove_files_from_list "${ab_global}/common/remove_list.txt"
remove_files_from_list "${ab_global}/${openwrt_branch}/remove_list.txt"
}
copy_global_files() {
copy_files "${ab_global}/common/files"
copy_files "${ab_global}/${openwrt_branch}/files"
}
apply_global_patches() {
apply_patches "${ab_global}/${openwrt_branch}/patches-base" || return 1
apply_patches "${ab_global}/${openwrt_branch}/patches-feeds" || return 1
apply_patches "${ab_global}/${openwrt_branch}/patches" || return 1
}
remove_files_by_platform_list() {
remove_files_from_list "${ab_platform_dir}/remove_list.txt"
remove_files_from_list "${ab_platform_dir}/${openwrt_branch}/remove_list.txt"
}
copy_platform_files() {
copy_files "${ab_platform_dir}/files"
copy_files "${ab_platform_dir}/${openwrt_branch}/files"
}
apply_platform_patches() {
apply_patches "${ab_platform_dir}/${openwrt_branch}/patches-base" || return 1
apply_patches "${ab_platform_dir}/${openwrt_branch}/patches-feeds" || return 1
apply_patches "${ab_platform_dir}/${openwrt_branch}/patches" || return 1
}
remove_files_by_wifi_list() {
if test -n "${ab_wifi_dir}"; then
remove_files_from_list "${ab_wifi_dir}/remove_list.txt"
remove_files_from_list "${ab_wifi_dir}/${openwrt_branch}/remove_list.txt"
fi
}
copy_wifi_files() {
if test -n "${ab_wifi_dir}"; then
copy_files "${ab_wifi_dir}/files"
copy_files "${ab_wifi_dir}/${openwrt_branch}/files"
fi
}
apply_wifi_patches() {
if test -n "${ab_wifi_dir}"; then
apply_patches "${ab_wifi_dir}/${openwrt_branch}/patches-base" || return 1
apply_patches "${ab_wifi_dir}/${openwrt_branch}/patches-feeds" || return 1
apply_patches "${ab_wifi_dir}/${openwrt_branch}/patches" || return 1
fi
}
remove_files_by_sku_list() {
if test -n "${ab_sku_dir}"; then
remove_files_from_list "${ab_sku_dir}/remove_list.txt"
remove_files_from_list "${ab_sku_dir}/${openwrt_branch}/remove_list.txt"
fi
}
copy_sku_files() {
if test -n "${ab_sku_dir}"; then
copy_files "${ab_sku_dir}/files"
copy_files "${ab_sku_dir}/${openwrt_branch}/files"
fi
}
apply_sku_patches() {
if test -n "${ab_sku_dir}"; then
apply_patches "${ab_sku_dir}/${openwrt_branch}/patches-base" || return 1
apply_patches "${ab_sku_dir}/${openwrt_branch}/patches-feeds" || return 1
apply_patches "${ab_sku_dir}/${openwrt_branch}/patches" || return 1
fi
}
remove_files_by_variant_list() {
if test -n "${ab_variant_dir}"; then
remove_files_from_list "${ab_sku_dir}/remove_list.txt"
remove_files_from_list "${ab_sku_dir}/${openwrt_branch}/remove_list.txt"
fi
}
copy_variant_files() {
if test -n "${ab_variant_dir}"; then
copy_files "${ab_variant_dir}/files"
copy_files "${ab_variant_dir}/${openwrt_branch}/files"
fi
}
apply_variant_patches() {
if test -n "${ab_variant_dir}"; then
apply_patches "${ab_variant_dir}/${openwrt_branch}/patches-base" || return 1
apply_patches "${ab_variant_dir}/${openwrt_branch}/patches-feeds" || return 1
apply_patches "${ab_variant_dir}/${openwrt_branch}/patches" || return 1
fi
}
# prepare_wifi_driver() { } Implemented by wifi inclusion
prepare_openwrt_config() {
local kconfig_files=
exec_log "make -C \"${openwrt_root}\" prepare-tmpinfo scripts/config/aconf"
openwrt_config_file="${openwrt_root}/.config"
kconfig_files="\"${ab_platform_dir}/${openwrt_branch}/defconfig\""
if test ${ab_branch_level} -ge 2; then
kconfig_files="${kconfig_files} \"${ab_wifi_dir}/${openwrt_branch}/defconfig\""
fi
if test ${ab_branch_level} -ge 3; then
kconfig_files="${kconfig_files} \"${ab_sku_dir}/${openwrt_branch}/defconfig\""
fi
if test ${ab_branch_level} -ge 4; then
kconfig_files="${kconfig_files} \"${ab_variant_dir}/${openwrt_branch}/defconfig\""
fi
exec_log "rm -f \"${openwrt_config_file}.old\""
exec_log "STAGING_DIR_HOST=\"${openwrt_root}/staging_dir/host\" \"${openwrt_root}/scripts/config/aconf\" -m -o \"${openwrt_config_file}\" -k \"${openwrt_root}/Config.in\" ${kconfig_files}"
# for debug purpose
exec_log "cp -f \"${openwrt_config_file}\" \"${ab_tmp}/${ab_branch}.config\""
exec_log "STAGING_DIR_HOST=\"${openwrt_root}/staging_dir/host\" \"${openwrt_root}/scripts/config/aconf\" -m -M -o \"${ab_tmp}/${ab_branch}_defconfig\" -k \"${openwrt_root}/Config.in\" ${kconfig_files}"
}
# {platform,wifi,sku,variant}_change_openwrt_config() { } Implemented by platform sub levels
enable_openwrt_collect_debug_symbols() {
openwrt_config_enable CONFIG_COLLECT_KERNEL_DEBUG
openwrt_config_enable CONFIG_DEBUG
openwrt_config_disable CONFIG_KERNEL_DEBUG_INFO_REDUCED
}
make_defconfig() {
if test -f "${ab_tmp}/.config.prev" -a -f "${openwrt_root}/.config"; then
if ! cmp -s "${ab_tmp}/.config.prev" "${openwrt_root}/.config"; then
exec_log "make -C \"${openwrt_root}\" defconfig"
fi
fi
}
__target_info_updated=
update_target_info() {
[ -n "${__target_info_updated}" ] && return 0
target_name=$(openwrt_get_target_name)
subtarget_name=$(openwrt_get_subtarget_name)
if test -z "${target_name}"; then
log_err "Failed to get OpenWrt's target name"
return 1
fi
log_info "Target name: ${target_name}"
[ -n "${subtarget_name}" ] && log_info "Subtarget name: ${subtarget_name}"
openwrt_bin_dir=$(openwrt_get_bin_dir)
if test -z "${openwrt_bin_dir}"; then
log_err "Failed to get OpenWrt's bin directory"
return 1
fi
log_dbg "Target bin dir: ${openwrt_bin_dir}"
kernel_ver=$(openwrt_get_target_kernel_version ${target_name})
if test -z "${kernel_ver}"; then
log_err "Failed to get OpenWrt's target kernel version"
return 1
else
log_info "Target kernel version: ${kernel_ver}"
fi
if test -n ${subtarget_name}; then
if test -f "${openwrt_root}/target/linux/${target_name}/${subtarget_name}/config-${kernel_ver}"; then
kernel_config_file="${openwrt_root}/target/linux/${target_name}/${subtarget_name}/config-${kernel_ver}"
fi
fi
if test -z ${kernel_config_file}; then
if test -f "${openwrt_root}/target/linux/${target_name}/config-${kernel_ver}"; then
kernel_config_file="${openwrt_root}/target/linux/${target_name}/config-${kernel_ver}"
fi
fi
if test -z ${kernel_config_file}; then
log_err "Unable to find target's kernel config file"
return 1
fi
log_dbg "Target kernel config file: ${kernel_config_file}"
__target_info_updated=1
}
# {platform,wifi,sku,variant}_change_kernel_config() { } Implemented by platform sub levels
enable_kernel_proc_config_gz() {
kernel_config_enable CONFIG_IKCONFIG
kernel_config_enable CONFIG_IKCONFIG_PROC
}
prepare_stamp() {
if test -n "${do_menuconfig}"; then
touch "${ab_tmp}/.stamp.menuconfig"
else
rm -f "${ab_tmp}/.stamp.menuconfig"
fi
echo -n "${ab_branch}" > "${ab_tmp}/branch_name"
echo -n "${ab_cmdline}" > "${ab_tmp}/cmdline"
}
download_openwrt_packages() {
if test x"${internal_build}" = x"1"; then
if test -d "${openwrt_root}/../dl"; then
if ! test -d "${openwrt_root}/dl" -o -L "${openwrt_root}/dl"; then
exec_log "ln -sf ../dl \"${openwrt_root}/dl\""
fi
fi
fi
exec_log "make -C \"${openwrt_root}\" V=1 -j\$((\$(nproc) + 1)) download"
}
build_openwrt() {
local ret=
local verbose=1
if test x"${debug_set}" = x"yes"; then
verbose=s
fi
build_time=$(date +%Y%m%d%H%M%S)
exec_log "make -C \"${openwrt_root}\" V=${verbose} -j\$((\$(nproc) + 1))"
ret=$?
if test ${ret} != 0; then
log_warn "Build failed with error code ${ret}."
log_warn "Restart single-threaded building for debugging purpose."
exec_log "make -C \"${openwrt_root}\" V=s -j1"
ret=$?
if test ${ret} != 0; then
log_err "Debug build failed with error code ${ret}."
return 1
fi
fi
log_info "OpenWrt built successfully"
}
# Create the ${ab_bin_release} directory
mk_ab_bin_release() {
exec_log "mkdir -p \"${ab_bin_release}\""
}
collect_openwrt_images() {
local file_count=0
local files=
if [ -z "${build_time}" ]; then
build_time=$(date +%Y%m%d%H%M%S)
fi
files=$(find "${openwrt_bin_dir}" -maxdepth 1 -name '*.bin' -o -name "*.img" -o -name '*.itb' -o -name '*.gz')
for file in ${files}; do
local file_no_ext=${file%.*}
local file_name=${file_no_ext##*/}
local file_ext=${file##*.}
exec_log "cp -rf \"${file}\" \"${ab_bin_release}/${file_name}-${build_time}.${file_ext}\""
((file_count++))
done
log_info "Total ${file_count} image files copied."
}
collect_openwrt_configs() {
local linux_dir=$(openwrt_get_target_kernel_linux_build_dir ${target_name})
exec_log "cp -f \"${openwrt_root}/.config\" \"${ab_bin_release}/openwrt.config\""
if test -z "${linux_dir}"; then
log_warn "Failed to get OpenWrt's linux kernel build directory"
else
local kernel_config_data="${linux_dir}/kernel/config_data"
if [ -f "${kernel_config_data}" ]; then
exec_log "cp -f \"${kernel_config_data}\" \"${ab_bin_release}/kernel.config\""
fi
fi
}
collect_kernel_debug_symbols() {
if [ -f "${openwrt_bin_dir}/kernel-debug.tar.zst" ]; then
exec_log "cp -f \"${openwrt_bin_dir}/kernel-debug.tar.zst\" \"${ab_bin_release}/\""
fi
}
collect_userspace_debug_symbols() {
local staging_dir_root=$(openwrt_get_staging_dir_root)
log_dbg "Staging dir root: ${staging_dir_root}"
if test -d "${staging_dir_root}"; then
staging_dir_root_prefix=$(dirname "${staging_dir_root}")
staging_dir_root_name=$(basename "${staging_dir_root}")
if test -x "${openwrt_root}/staging_dir/host/bin/zstd"; then
exec_log "tar -c -C \"${staging_dir_root_prefix}\" \"${staging_dir_root_name}\" | \"${openwrt_root}/staging_dir/host/bin/zstd\" -T0 -f -o \"${ab_bin_release}/rootfs-debug.tar.zst\""
else
exec_log "tar -jcf \"${ab_bin_release}/rootfs-debug.tar.bz2\" -C \"${staging_dir_root_prefix}\" \"${staging_dir_root_name}\""
fi
fi
}
collect_feeds_buildinfo() {
if [ -f "${openwrt_bin_dir}/feeds.buildinfo" ]; then
exec_log "cp -f \"${openwrt_bin_dir}/feeds.buildinfo\" \"${ab_bin_release}/\""
fi
}
do_menuconfig_update() {
local kconfig_files=
local final_file=
openwrt_config_file="${openwrt_root}/.config"
if test ${ab_branch_level} -eq 1; then
final_file="${ab_platform_dir}/${openwrt_branch}/defconfig"
fi
if test ${ab_branch_level} -ge 2; then
kconfig_files="\"${ab_platform_dir}/${openwrt_branch}/defconfig\""
final_file="${ab_wifi_dir}/${openwrt_branch}/defconfig"
fi
if test ${ab_branch_level} -ge 3; then
kconfig_files="${kconfig_files} \"${final_file}\""
final_file="${ab_sku_dir}/${openwrt_branch}/defconfig"
fi
if test ${ab_branch_level} -ge 4; then
kconfig_files="${kconfig_files} \"${final_file}\""
final_file="${ab_variant_dir}/${openwrt_branch}/defconfig"
fi
if test x"${sync_config_set}" == x"yes"; then
exec_log "rm -f \"${openwrt_config_file}.old\""
exec_log "STAGING_DIR_HOST=\"${openwrt_root}/staging_dir/host\" \"${openwrt_root}/scripts/config/aconf\" -m -o \"${openwrt_config_file}\" -k \"${openwrt_root}/Config.in\" ${kconfig_files} \"${final_file}\""
fi
exec_log "make -C \"${openwrt_root}\" menuconfig scripts/config/aconf"
if test ${ab_branch_level} -eq 1; then
exec_log "STAGING_DIR_HOST=\"${openwrt_root}/staging_dir/host\" \"${openwrt_root}/scripts/config/aconf\" -m -M -o \"${ab_platform_dir}/${openwrt_branch}/defconfig\" -k \"${openwrt_root}/Config.in\" \"${openwrt_config_file}\""
return
fi
exec_log "STAGING_DIR_HOST=\"${openwrt_root}/staging_dir/host\" \"${openwrt_root}/scripts/config/aconf\" -d -o \"${final_file}\" -k \"${openwrt_root}/Config.in\" -n \"${openwrt_config_file}\" ${kconfig_files}"
}