Commit 11d695064b8f7b18ee0a9e72e078d6b5267c6b1f

Thomas de Grivel 2023-02-14T12:59:21

refactor config and add locking

diff --git a/bin/shpkg b/bin/shpkg
index da76848..e9878d6 100755
--- a/bin/shpkg
+++ b/bin/shpkg
@@ -12,6 +12,8 @@ SHPKG_SHARE_DIR="${SHPKG_DIR}/share/shpkg"
 
 . ${SHPKG_SHARE_DIR}/shpkg.subr
 
+shpkg_lock shpkg.lock
+
 # Usage
 
 usage () {
@@ -51,6 +53,7 @@ Miscellaneous operations :
 # CLI
 
 main () {
+    verbose 2 shpkg_config
     if [ "x$1" = "xbuild" ]; then
         shift
         verbose 2 shpkg_build "$@"
@@ -129,4 +132,4 @@ main () {
     fi
 }
 
-shpkg_log shpkg shpkg_lock shpkg main "$@"
+shpkg_log shpkg main "$@"
diff --git a/bin/shpkg_bootstrap b/bin/shpkg_bootstrap
index 2d98ec7..cb42e1f 100755
--- a/bin/shpkg_bootstrap
+++ b/bin/shpkg_bootstrap
@@ -1,4 +1,5 @@
 #!/bin/sh
+
 set -e
 
 usage () {
diff --git a/bin/shpkg_ci b/bin/shpkg_ci
index e78db0c..0aa4695 100755
--- a/bin/shpkg_ci
+++ b/bin/shpkg_ci
@@ -1,19 +1,31 @@
 #!/bin/sh
 
+set -e
+
 . ~/shpkg/etc/shpkg/profile
+
 . ~/shpkg/share/shpkg/shpkg.subr
 
-logs2html () {
-    for F in ~/"www/log/${SHPKG_TARGET}/${SHPKG_REPO}".*.log; do
-        if ! [ -f "$F.html" ]; then
+shpkg_lock shpkg_ci.lock
+
+logs2html () (
+    cd ~/"www/log/${SHPKG_TARGET}/txt"
+    for F in "${SHPKG_REPO}".*.log; do
+        if [ "$F" -nt "../html/$F.html" ]; then
             verbose 1 ansi2html "$F"
         fi
     done
-}
+    verbose 1 mv ~/www/log/"${SHPKG_TARGET}"/txt/*.html ~/www/log/"${SHPKG_TARGET}"/html/ || true
+)
 
 main () {
-    if ! [ -d ~/"www/log/${SHPKG_TARGET}" ]; then
-        verbose 1 mkdir -p ~/"www/log/${SHPKG_TARGET}"
+    verbose 2 shpkg_config
+    SHPKG_CI_STATUS=0
+    if ! [ -d ~/"www/log/${SHPKG_TARGET}/txt" ]; then
+        verbose 1 mkdir -p ~/"www/log/${SHPKG_TARGET}/txt"
+    fi
+    if ! [ -d ~/"www/log/${SHPKG_TARGET}/html" ]; then
+        verbose 1 mkdir -p ~/"www/log/${SHPKG_TARGET}/html"
     fi
     for SHPKG_REPO; do
         SHPKG_REPO_DIR="$(shpkg_dir "${SHPKG_REPO}")"
@@ -21,20 +33,37 @@ main () {
             err 1 "shpkg_ci: unknown repo: $SHPKG_REPO"
         fi
         shpkg_tag_log "${SHPKG_REPO}.ci.log"
+        # first bootstrap
         if ! [ -d ~/"shpkg_${SHPKG_REPO}" ]; then
-            verbose 2 shpkg_bootstrap ~/"shpkg_${SHPKG_REPO}"
+            ( verbose 2 shpkg_bootstrap ~/"shpkg_${SHPKG_REPO}"; )
+        else
+            # check upgrade
+            ( . ~/"shpkg_${SHPKG_REPO}/etc/shpkg/profile"
+              if ! verbose 2 shpkg upgrade "${SHPKG_REPO}"; then
+                  SHPKG_CI_STATUS=10
+              fi; )
+            if verbose 1 cp -p ~/"shpkg_${SHPKG_REPO}/var/log/shpkg/tags/${SHPKG_REPO}".*.log ~/"www/log/${SHPKG_TARGET}/txt"; then
+                verbose 2 logs2html
+            fi
         fi
+        ( verbose 2 shpkg_bootstrap -f ~/"shpkg_${SHPKG_REPO}"; )
         ( . ~/"shpkg_${SHPKG_REPO}/etc/shpkg/profile"
-          verbose 2 shpkg upgrade "${SHPKG_REPO}"; )
-        verbose 1 cp ~/"shpkg_${SHPKG_REPO}/var/log/shpkg/tags/${SHPKG_REPO}".*.log ~/"www/log/${SHPKG_TARGET}/"
-        verbose 2 logs2html
-        verbose 2 shpkg_bootstrap -f ~/"shpkg_${SHPKG_REPO}"
-        ( . ~/"shpkg_${SHPKG_REPO}/etc/shpkg/profile"
-          verbose 2 shpkg install "${SHPKG_REPO}"; )
-        verbose 1 cp ~/"shpkg_${SHPKG_REPO}/var/log/shpkg/tags/${SHPKG_REPO}".*.log ~/"www/log/${SHPKG_TARGET}"
-        verbose 1 cp ~/"shpkg/var/log/shpkg/tags/${SHPKG_REPO}.ci.log" ~/"www/log/${SHPKG_TARGET}"
-        verbose 2 logs2html
+          if ! verbose 2 shpkg install "${SHPKG_REPO}"; then
+              SHPKG_CI_STATUS=10
+          fi; )
+        SHPKG_CI_COPY=No
+        if verbose 1 cp -p ~/"shpkg_${SHPKG_REPO}/var/log/shpkg/tags/${SHPKG_REPO}".*.log ~/"www/log/${SHPKG_TARGET}/txt"; then
+            SHPKG_CI_COPY=Yes
+        fi
+        if verbose 1 cp -p ~/"shpkg/var/log/shpkg/tags/${SHPKG_REPO}.ci.log" ~/"www/log/${SHPKG_TARGET}/txt"; then
+            SHPKG_CI_COPY=Yes
+        fi
+        if [ "x${SHPKG_CI_COPY}" = "xYes" ]; then
+            verbose 2 logs2html
+        fi
     done
 }
 
-shpkg_log shpkg_ci shpkg_lock shpkg_ci main "$@"
+shpkg_log shpkg_ci main "$@"
+
+exit "${SHPKG_CI_STATUS}"
diff --git a/share/shpkg/shpkg.subr b/share/shpkg/shpkg.subr
index de15040..d56f97c 100644
--- a/share/shpkg/shpkg.subr
+++ b/share/shpkg/shpkg.subr
@@ -1,8 +1,6 @@
 #!/bin/sh
 # Copyright 2023 Thomas de Grivel <thodg@kmx.io>
 
-set -e
-
 # Config
 
 SHPKG_DATE="$(date +%F_%T)"
@@ -99,7 +97,7 @@ shpkg_start_logging () {
 shpkg_tag_log () {
     SHPKG_LOG_TAG="$1"
     SHPKG_LOG_TAG_PATH="${SHPKG_LOG_TAGS_DIR}/${SHPKG_LOG_TAG}"
-    verbose -n 2 "logging to ${SHPKG_LOG_TAG_PATH}"
+    verbose -n 2 "Logging to ${SHPKG_LOG_TAG_PATH}"
     if [ -f "${SHPKG_LOG_TAG_PATH}.tmp" ]; then
         verbose 1 rm "${SHPKG_LOG_TAG_PATH}.tmp"
     fi
@@ -119,86 +117,74 @@ shpkg_lock () {
     if ! [ -d "${SHPKG_LOCK_DIR}" ]; then
         verbose 1 mkdir -p "${SHPKG_LOCK_DIR}"
     fi
-    ( verbose 1 cd "${SHPKG_LOCK_DIR}" && 
-          while ! mkdir "${SHPKG_LOCK_NAME}" 2>/dev/null; do
-                sleep 1
-          done
-      cd "${SHPKG_LOCK_NAME}" &&
-          ( "$@"; )
-      cd .. &&
-          verbose -n 2 "Unlocking ${SHPKG_LOCK}" &&
-          rmdir "${SHPKG_LOCK_NAME}"; )
-}
-
-# C compiler
-
-if [ "x${CC}" = "x" ]; then
-    if which cc >/dev/null; then
-        CC=cc
-    elif which gcc >/dev/null; then
-        CC=gcc
-    elif which egcc >/dev/null; then
-        CC=egcc
-    else
-        err 1 "C compiler not found."
-    fi
-fi
-SHPKG_TARGET="$(LC_ALL=C.UTF-8 ${CC} -v 2>&1 | grep '^Target: ' | head -n 1 | cut -c 9-)"
-export CPPFLAGS="${CPPFLAGS} -I${SHPKG_INCLUDE_DIR}"
-export LDFLAGS="${LDFLAGS} -L${SHPKG_LIB_DIR}"
-export PKG_CONFIG_PATH="${SHPKG_LIB_DIR}/pkgconfig"
-
-SHPKG_TARGET_DIR="${SHPKG_DIR}/target/${SHPKG_TARGET}"
-
-SHPKG_FAKE_DIR="${SHPKG_TARGET_DIR}/fake"
-SHPKG_OBJ_DIR="${SHPKG_TARGET_DIR}/obj"
-SHPKG_PACKAGE_DIR="${SHPKG_TARGET_DIR}/packages"
-SHPKG_TAG_DIR="${SHPKG_TARGET_DIR}/tag"
-
-# OS detection
-
-SHPKG_OS="$(uname)"
-if [ "x${SHPKG_OS}" = "xLinux" ]; then
-    SHPKG_OS="${SHPKG_OS}/$(uname -a | grep -io -e Debian -e gentoo -e Ubuntu)"
-fi
-
-SHPKG_OS_DIR="${SHPKG_SHARE_DIR}/${SHPKG_OS}"
-
-# SHA256
-
-if which sha256 >/dev/null 2>&1; then
-    SHPKG_SHA256=sha256
-elif which sha256sum >/dev/null 2>&1; then
-    SHPKG_SHA256=sha256sum
-else
-    err 1 "sha256 executable not found"
-fi
-
-shpkg_sha256 () {
-    sha256 "$@"
-}
-
-shpkg_sha256sum () {
-    for F; do
-        echo -n "SHA256 ($F) = "
-        sha256sum "$F" | cut -d ' ' -f 1
+    while ! { set -C; 2>/dev/null >"${SHPKG_LOCK}"; }; do
+        sleep 1
     done
+    trap "rm -f '${SHPKG_LOCK}'" EXIT
 }
 
-# Setup
+# Config
 
-if ! [ -d "${SHPKG_FAKE_DIR}" ]; then
-    verbose 1 mkdir -p "${SHPKG_FAKE_DIR}"
-fi
-if ! [ -d "${SHPKG_PACKAGE_DIR}" ]; then
-    verbose 1 mkdir -p "${SHPKG_PACKAGE_DIR}"
-fi
-if ! [ -d "${SHPKG_SRC_DIR}" ]; then
-    verbose 1 mkdir -p "${SHPKG_SRC_DIR}"
-fi
-if ! [ -d "${SHPKG_VAR_DB_DIR}" ]; then
-    verbose 1 mkdir -p "${SHPKG_VAR_DB_DIR}"
-fi
+shpkg_config () {
+    # C compiler
+    if [ "x${CC}" = "x" ]; then
+        if which cc >/dev/null; then
+            CC=cc
+        elif which gcc >/dev/null; then
+            CC=gcc
+        elif which egcc >/dev/null; then
+            CC=egcc
+        else
+            err 1 "C compiler not found."
+        fi
+    fi
+    # C build variables
+    export CPPFLAGS="${CPPFLAGS} -I${SHPKG_INCLUDE_DIR}"
+    export LDFLAGS="${LDFLAGS} -L${SHPKG_LIB_DIR}"
+    export PKG_CONFIG_PATH="${SHPKG_LIB_DIR}/pkgconfig"
+    # Target architecture
+    SHPKG_TARGET="$(LC_ALL=C.UTF-8 ${CC} -v 2>&1 | grep '^Target: ' | head -n 1 | cut -c 9-)"
+    SHPKG_TARGET_DIR="${SHPKG_DIR}/target/${SHPKG_TARGET}"
+    SHPKG_FAKE_DIR="${SHPKG_TARGET_DIR}/fake"
+    SHPKG_OBJ_DIR="${SHPKG_TARGET_DIR}/obj"
+    SHPKG_PACKAGE_DIR="${SHPKG_TARGET_DIR}/packages"
+    SHPKG_TAG_DIR="${SHPKG_TARGET_DIR}/tag"
+    # OS detection
+    SHPKG_OS="$(uname)"
+    if [ "x${SHPKG_OS}" = "xLinux" ]; then
+        SHPKG_OS="${SHPKG_OS}/$(uname -a | grep -io -e Debian -e gentoo -e Ubuntu)"
+    fi
+    SHPKG_OS_DIR="${SHPKG_SHARE_DIR}/${SHPKG_OS}"
+    # Number of CPU cores
+    if which nproc >/dev/null 2>&1; then
+        SHPKG_NCPU="$(nproc --all)"
+    elif which sysctl >/dev/null 2>&1; then
+        SHPKG_NCPU="$(sysctl -n hw.ncpu)"
+    else
+        SHPKG_NCPU=1
+    fi
+    # SHA256
+    if which sha256 >/dev/null 2>&1; then
+        SHPKG_SHA256=sha256
+    elif which sha256sum >/dev/null 2>&1; then
+        SHPKG_SHA256=sha256sum
+    else
+        err 1 "sha256 executable not found"
+    fi
+    # Directories
+    if ! [ -d "${SHPKG_FAKE_DIR}" ]; then
+        verbose 1 mkdir -p "${SHPKG_FAKE_DIR}"
+    fi
+    if ! [ -d "${SHPKG_PACKAGE_DIR}" ]; then
+        verbose 1 mkdir -p "${SHPKG_PACKAGE_DIR}"
+    fi
+    if ! [ -d "${SHPKG_SRC_DIR}" ]; then
+        verbose 1 mkdir -p "${SHPKG_SRC_DIR}"
+    fi
+    if ! [ -d "${SHPKG_VAR_DB_DIR}" ]; then
+        verbose 1 mkdir -p "${SHPKG_VAR_DB_DIR}"
+    fi
+}
 
 # Repo index
 
@@ -232,6 +218,19 @@ shpkg_src_parent_dir () {
     dirname "$(shpkg_dir "${SHPKG_REPO}")"
 }
 
+# SHA256
+
+shpkg_sha256 () {
+    sha256 "$@"
+}
+
+shpkg_sha256sum () {
+    for F; do
+        echo -n "SHA256 ($F) = "
+        sha256sum "$F" | cut -d ' ' -f 1
+    done
+}
+
 # Source repository
 
 shpkg_checkout () {
@@ -476,7 +475,7 @@ shpkg_fake () {
 }
 
 shpkg_fake_dir () {
-    echo "${SHPKG_FAKE_DIR}/$(shpkg_dir "$1")/${SHPKG_PREFIX}"
+    echo "${SHPKG_FAKE_DIR}/$(shpkg_dir "$1")${SHPKG_PREFIX}"
 }
 
 shpkg_package () {