From bd0cd1ab1b6c626ee8c5734fef94ac07b188e297 Mon Sep 17 00:00:00 2001
From: Steve Lhomme <robux4@ycbcr.xyz>
Date: Thu, 26 Sep 2024 10:24:33 +0200
Subject: [PATCH] CI: don't use prebuilt if the buildsystem scripts were
 modified

---
 buildsystem/get-contrib-rebuild.sh | 34 ++++++++++++++++++++++++++++++
 buildsystem/gitlab/.gitlab-ci.yml  |  8 ++++---
 2 files changed, 39 insertions(+), 3 deletions(-)
 create mode 100755 buildsystem/get-contrib-rebuild.sh

diff --git a/buildsystem/get-contrib-rebuild.sh b/buildsystem/get-contrib-rebuild.sh
new file mode 100755
index 0000000..b0fb50a
--- /dev/null
+++ b/buildsystem/get-contrib-rebuild.sh
@@ -0,0 +1,34 @@
+#!/usr/bin/env bash
+# Returns 0 if the prebuilt contrib can be used
+set -e
+
+# Print error message and terminate script with status 1
+# Arguments:
+#   Message to print
+abort_err()
+{
+    echo "ERROR: $1" >&2
+    exit 1
+}
+
+command -v "git" >/dev/null 2>&1 || abort_err "Git was not found!"
+
+# VLC source root directory
+LIBVLCJNI_SRC_ROOT_DIR=$(git rev-parse --show-toplevel)
+
+[ -n "${LIBVLCJNI_SRC_ROOT_DIR}" ] || abort_err "This script must be run in the libvlcjni Git repo and git must be available"
+[ -f "${LIBVLCJNI_SRC_ROOT_DIR}/libvlc/jni/Android.mk" ] || abort_err "This script must be run in the libvlcjni Git repository"
+
+REFERENCE_BRANCH="$1"
+[ -n "${REFERENCE_BRANCH}" ] || abort_err "Missing reference branch argument (origin/master ?)"
+
+# Check if files were changed in the buildsystem
+ALL_CHANGES=$(git diff --name-only "${REFERENCE_BRANCH}")
+BUILDSYSTEM_CHANGED=$(echo "${ALL_CHANGES}" | grep ^buildsystem/)
+
+if [ -n "${BUILDSYSTEM_CHANGED}" ]; then
+    # The buildsystem has been modified, we need to rebuild contribs
+    exit 1
+fi
+
+exit 0
diff --git a/buildsystem/gitlab/.gitlab-ci.yml b/buildsystem/gitlab/.gitlab-ci.yml
index ad566e0..599fcec 100644
--- a/buildsystem/gitlab/.gitlab-ci.yml
+++ b/buildsystem/gitlab/.gitlab-ci.yml
@@ -21,9 +21,11 @@ stages:
     script: |
         rm -rf vlc
         ./buildsystem/get-vlc.sh
-        export VLC_CONTRIB_SHA="$(cd vlc && extras/ci/get-contrib-sha.sh android-${ARCH})"
-        export VLC_PREBUILT_CONTRIBS_URL="https://artifacts.videolan.org/vlc/android-${ARCH}/vlc-contrib-${TRIPLET}-${VLC_CONTRIB_SHA}.tar.bz2"
-        if vlc/extras/ci/check-url.sh "$VLC_PREBUILT_CONTRIBS_URL"; then CONTRIB_FLAGS="--with-prebuilt-contribs"; fi
+        if [ ! buildsystem/get-contrib-rebuild.sh "${CI_DEFAULT_BRANCH}" ]; then
+          export VLC_CONTRIB_SHA="$(cd vlc && extras/ci/get-contrib-sha.sh android-${ARCH})"
+          export VLC_PREBUILT_CONTRIBS_URL="https://artifacts.videolan.org/vlc/android-${ARCH}/vlc-contrib-${TRIPLET}-${VLC_CONTRIB_SHA}.tar.bz2"
+          if vlc/extras/ci/check-url.sh "$VLC_PREBUILT_CONTRIBS_URL"; then CONTRIB_FLAGS="--with-prebuilt-contribs"; fi
+        fi
         ./buildsystem/compile-libvlc.sh -a ${ARCH} $CONTRIB_FLAGS
     rules:
         # Explicitely refuse to build anything that would also trigger
-- 
GitLab