Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 954348e9 authored by Jackeagle's avatar Jackeagle
Browse files

build: harden kernel repo bootstrap in envsetup

parent c96eb1b2
Loading
Loading
Loading
Loading
+32 −14
Original line number Diff line number Diff line
@@ -942,6 +942,9 @@ function build_kernel() {
    local target_kernel_source="$(_get_build_var_cached TARGET_KERNEL_PLATFORM_SOURCE)"

    local KERNEL_BUILD_TOP="${ANDROID_BUILD_TOP}/out-kernel/${target_kernel_source}"
    local kernel_repo_dir="${KERNEL_BUILD_TOP}/.repo"
    local android_build_top_real="$(cd "${ANDROID_BUILD_TOP}" && pwd -P)"
    local kernel_repo_bootstrap_root="${KERNEL_REPO_BOOTSTRAP_ROOT:-/tmp}"

    local target_kernel_manifest=$(echo android_kernel_${target_kernel_source}_manifest | tr / _)
    local repo_init_args=("-b" "${lineage_version}")
@@ -955,11 +958,10 @@ function build_kernel() {
        repo_init_args+=("-g" "default,-darwin,-muppets,muppets_${LINEAGE_BUILD}")
    fi

    # Make sure we have the kernel source folder structure in place.
    # Check for manifests.git specifically — a previous failed run may have
    # left .repo/ with only a TRACE_FILE, causing the old .repo/ check to
    # skip setup entirely while repo still walks up to the parent Android tree.
    if [ ! -d "${KERNEL_BUILD_TOP}/.repo/manifests.git" ]; then
    # Make sure we have a complete standalone repo client in the kernel tree.
    # Checking only manifests.git is not sufficient: partial/stale .repo state
    # can make repo walk up and attach to the parent Android checkout instead.
    if [ ! -d "${kernel_repo_dir}/manifests.git" ] || [ ! -f "${kernel_repo_dir}/repo/main.py" ]; then
        echo "Kernel source ${KERNEL_BUILD_TOP} is missing, preparing folder structure"

        # Run repo init from a temp dir OUTSIDE the Android tree.
@@ -967,12 +969,19 @@ function build_kernel() {
        # the Android .repo checkout instead of creating a new one.
        # By initializing outside, repo has no parent .repo to find.
        # We then move the result into the kernel dir.
        local _tmpdir=$(mktemp -d)
        mkdir -p "${kernel_repo_bootstrap_root}"
        local _tmpdir=$(mktemp -d -p "${kernel_repo_bootstrap_root}" kernel-repo.XXXXXX)
        local _tmpdir_real="$(cd "${_tmpdir}" && pwd -P)"
        if [[ "${_tmpdir_real}" == "${android_build_top_real}" || "${_tmpdir_real}" == "${android_build_top_real}/"* ]]; then
            echo "Kernel source bootstrap dir ${_tmpdir_real} is inside ${android_build_top_real}; refusing to run repo init there"
            rm -rf "${_tmpdir}"
            return 1
        fi
        mkdir -p "${_tmpdir}/.repo"
        cp -R "${ANDROID_BUILD_TOP}/.repo/repo" "${_tmpdir}/.repo/repo"

        pushd "${_tmpdir}" > /dev/null
        yes | repo init -u "https://github.com/LineageOS/${target_kernel_manifest}.git" ${repo_init_args[@]} || [ $? -eq 141 ]
        yes | repo init -u "https://github.com/LineageOS/${target_kernel_manifest}.git" "${repo_init_args[@]}" || [ $? -eq 141 ]
        if [ $? -ne 0 ]; then
            echo "Kernel source repo init failed"
            popd > /dev/null
@@ -984,16 +993,21 @@ function build_kernel() {
        # Move the initialized .repo to kernel dir (all internal paths
        # are relative so the move is safe)
        mkdir -p "${KERNEL_BUILD_TOP}"
        rm -rf "${KERNEL_BUILD_TOP}/.repo"
        mv "${_tmpdir}/.repo" "${KERNEL_BUILD_TOP}/.repo"
        rm -rf "${kernel_repo_dir}"
        mv "${_tmpdir}/.repo" "${kernel_repo_dir}"
        rm -rf "${_tmpdir}"

        if [ ! -d "${kernel_repo_dir}/manifests.git" ] || [ ! -f "${kernel_repo_dir}/repo/main.py" ]; then
            echo "Kernel source repo bootstrap is incomplete in ${kernel_repo_dir}"
            return 1
        fi

        # Allow custom .repo/project-objects dir
        if [ -n "${KERNEL_REPO_PROJECT_OBJECTS_DIR}" ]; then
            if [ ! -d "${KERNEL_REPO_PROJECT_OBJECTS_DIR}" ]; then
                mkdir "${KERNEL_REPO_PROJECT_OBJECTS_DIR}"
            fi
            ln -sf "${KERNEL_REPO_PROJECT_OBJECTS_DIR}" "${KERNEL_BUILD_TOP}/.repo/project-objects"
            ln -sf "${KERNEL_REPO_PROJECT_OBJECTS_DIR}" "${kernel_repo_dir}/project-objects"
        fi

        # Allow custom .repo/projects dir
@@ -1001,7 +1015,7 @@ function build_kernel() {
            if [ ! -d "${KERNEL_REPO_PROJECTS_DIR}" ]; then
                mkdir "${KERNEL_REPO_PROJECTS_DIR}"
            fi
            ln -sf "${KERNEL_REPO_PROJECTS_DIR}" "${KERNEL_BUILD_TOP}/.repo/projects"
            ln -sf "${KERNEL_REPO_PROJECTS_DIR}" "${kernel_repo_dir}/projects"
        fi

        # Mark as out dir to prevent build system from scanning it
@@ -1010,11 +1024,15 @@ function build_kernel() {

    # Init, sync, remove previous build output & build kernel
    pushd "${KERNEL_BUILD_TOP}" > /dev/null
    if [ ! -d "${kernel_repo_dir}/manifests.git" ] || [ ! -f "${kernel_repo_dir}/repo/main.py" ]; then
        echo "Kernel source repo checkout in ${KERNEL_BUILD_TOP} is incomplete; refusing to run repo from parent source tree"
        popd > /dev/null
        return 1
    fi
    if [[ "${SKIP_KERNEL_SYNC}" != "true" && "${SKIP_KERNEL_SYNC}" != "1" ]]; then
        echo "Syncing ${KERNEL_BUILD_TOP}"
        # On subsequent runs manifests.git exists so repo finds the
        # kernel checkout without walking up to the Android tree.
        yes | repo init -u https://github.com/LineageOS/${target_kernel_manifest}.git ${repo_init_args[@]} || [ $? -eq 141 ]
        # Run only when the kernel tree has its own valid repo launcher.
        yes | repo init -u "https://github.com/LineageOS/${target_kernel_manifest}.git" "${repo_init_args[@]}" || [ $? -eq 141 ]
        if [ $? -ne 0 ]; then
            echo "Kernel source repo init failed"
            popd > /dev/null