From 44d28f9f8885d59502055dd077b566d7d3b7f663 Mon Sep 17 00:00:00 2001 From: Leonid Pliushch Date: Wed, 6 Mar 2019 16:46:53 +0200 Subject: [PATCH] build-package.sh: prevent parallel runs in the same environment Avoid corruption of the build environment if build-package.sh was executed in 2 parallel sessions. --- build-package.sh | 79 +++++++++++-------- scripts/build/termux_step_handle_arguments.sh | 5 +- scripts/build/termux_step_start_build.sh | 4 +- 3 files changed, 52 insertions(+), 36 deletions(-) diff --git a/build-package.sh b/build-package.sh index 25c2791c90..c7ae459e7b 100755 --- a/build-package.sh +++ b/build-package.sh @@ -3,6 +3,16 @@ set -e -o pipefail -u +# Lock file to prevent parallel running in the same environment. +TERMUX_BUILD_LOCK_FILE="/tmp/.termux-build.lck" +if [ ! -e "$TERMUX_BUILD_LOCK_FILE" ]; then + touch "$TERMUX_BUILD_LOCK_FILE" +fi + +# Special variable for internal use. It forces script to ignore +# lock file. +: "${TERMUX_BUILD_IGNORE_LOCK:=false}" + # Utility function to log an error message and exit with an error code. source scripts/build/termux_error_exit.sh @@ -128,35 +138,40 @@ source scripts/build/termux_step_create_debfile.sh # Finish the build. Not to be overridden by package scripts. source scripts/build/termux_step_finish_build.sh -termux_step_handle_arguments "$@" -termux_step_setup_variables -termux_step_handle_buildarch -termux_step_start_build -termux_step_extract_package -cd "$TERMUX_PKG_SRCDIR" -termux_step_post_extract_package -termux_step_handle_hostbuild -termux_step_setup_toolchain -termux_step_patch_package -termux_step_replace_guess_scripts -cd "$TERMUX_PKG_SRCDIR" -termux_step_pre_configure -cd "$TERMUX_PKG_BUILDDIR" -termux_step_configure -cd "$TERMUX_PKG_BUILDDIR" -termux_step_post_configure -cd "$TERMUX_PKG_BUILDDIR" -termux_step_make -cd "$TERMUX_PKG_BUILDDIR" -termux_step_make_install -cd "$TERMUX_PKG_BUILDDIR" -termux_step_post_make_install -cd "$TERMUX_PKG_MASSAGEDIR" -termux_step_extract_into_massagedir -cd "$TERMUX_PKG_MASSAGEDIR" -termux_step_massage -cd "$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX" -termux_step_post_massage -termux_step_create_datatar -termux_step_create_debfile -termux_step_finish_build +{ + if ! $TERMUX_BUILD_IGNORE_LOCK; then + flock -n 5 || termux_error_exit "Another build is already running within same environment." + fi + termux_step_handle_arguments "$@" + termux_step_setup_variables + termux_step_handle_buildarch + termux_step_start_build + termux_step_extract_package + cd "$TERMUX_PKG_SRCDIR" + termux_step_post_extract_package + termux_step_handle_hostbuild + termux_step_setup_toolchain + termux_step_patch_package + termux_step_replace_guess_scripts + cd "$TERMUX_PKG_SRCDIR" + termux_step_pre_configure + cd "$TERMUX_PKG_BUILDDIR" + termux_step_configure + cd "$TERMUX_PKG_BUILDDIR" + termux_step_post_configure + cd "$TERMUX_PKG_BUILDDIR" + termux_step_make + cd "$TERMUX_PKG_BUILDDIR" + termux_step_make_install + cd "$TERMUX_PKG_BUILDDIR" + termux_step_post_make_install + cd "$TERMUX_PKG_MASSAGEDIR" + termux_step_extract_into_massagedir + cd "$TERMUX_PKG_MASSAGEDIR" + termux_step_massage + cd "$TERMUX_PKG_MASSAGEDIR/$TERMUX_PREFIX" + termux_step_post_massage + termux_step_create_datatar + termux_step_create_debfile + termux_step_finish_build +} 5< "$TERMUX_BUILD_LOCK_FILE" diff --git a/scripts/build/termux_step_handle_arguments.sh b/scripts/build/termux_step_handle_arguments.sh index 92cbdfc4e3..9b1807a886 100644 --- a/scripts/build/termux_step_handle_arguments.sh +++ b/scripts/build/termux_step_handle_arguments.sh @@ -36,8 +36,9 @@ termux_step_handle_arguments() { # Handle 'all' arch: if [ -n "${TERMUX_ARCH+x}" ] && [ "${TERMUX_ARCH}" = 'all' ]; then for arch in 'aarch64' 'arm' 'i686' 'x86_64'; do - ./build-package.sh ${TERMUX_FORCE_BUILD+-f} -a $arch ${TERMUX_INSTALL_DEPS+-i} \ - ${TERMUX_DEBUG+-d} ${TERMUX_DEBDIR+-o $TERMUX_DEBDIR} "$1" + TERMUX_BUILD_IGNORE_LOCK=true ./build-package.sh ${TERMUX_FORCE_BUILD+-f} \ + -a $arch ${TERMUX_INSTALL_DEPS+-i} ${TERMUX_DEBUG+-d} \ + ${TERMUX_DEBDIR+-o $TERMUX_DEBDIR} "$1" done exit fi diff --git a/scripts/build/termux_step_start_build.sh b/scripts/build/termux_step_start_build.sh index 4d67e4e997..14eeb163ac 100644 --- a/scripts/build/termux_step_start_build.sh +++ b/scripts/build/termux_step_start_build.sh @@ -31,7 +31,7 @@ termux_step_start_build() { fi if ! termux_download_deb $PKG $DEP_ARCH $DEP_VERSION; then echo "Download of $PKG@$DEP_VERSION from $TERMUX_REPO_URL failed, building instead" - ./build-package.sh -a $TERMUX_ARCH -I "${PKG_DIR}" + TERMUX_BUILD_IGNORE_LOCK=true ./build-package.sh -a $TERMUX_ARCH -I "${PKG_DIR}" continue else if [ ! "$TERMUX_QUIET_BUILD" = true ]; then echo "extracting $PKG..."; fi @@ -64,7 +64,7 @@ termux_step_start_build() { fi echo "Building dependency $PKG if necessary..." # Built dependencies are put in the default TERMUX_DEBDIR instead of the specified one - ./build-package.sh -a $TERMUX_ARCH -s "${PKG_DIR}" + TERMUX_BUILD_IGNORE_LOCK=true ./build-package.sh -a $TERMUX_ARCH -s "${PKG_DIR}" done<<<$(./scripts/buildorder.py "$TERMUX_PKG_BUILDER_DIR" $TERMUX_PACKAGES_DIRECTORIES || echo "ERROR") fi