From a3401596593a12f7da6ab718ed74f1ab11d2b702 Mon Sep 17 00:00:00 2001
From: Alexander Schwinn <al.schwinn@gsi.de>
Date: Thu, 9 Feb 2023 10:02:13 +0100
Subject: [PATCH] Automate modbus installation

---
 build_and_install.sh                          |  2 +-
 modbus/.gitignore                             |  1 +
 modbus/build.sh                               | 47 +++++++++++++++++
 modbus/install.sh                             | 52 +++++++++++++++++++
 silecs-communication-cpp/Makefile.dep         |  2 +-
 .../squash_modbus_lib.mri                     |  2 +-
 6 files changed, 103 insertions(+), 3 deletions(-)
 create mode 100644 modbus/.gitignore
 create mode 100755 modbus/build.sh
 create mode 100755 modbus/install.sh

diff --git a/build_and_install.sh b/build_and_install.sh
index 591744b..44b24f8 100755
--- a/build_and_install.sh
+++ b/build_and_install.sh
@@ -35,7 +35,7 @@ fi
 # make install
 
 # List of silecs packages which can be released
-PACKAGES="silecs-codegen silecs-model snap7 silecs-communication-cpp silecs-diagnostic-cpp silecs_cli silecs-cli-client"
+PACKAGES="silecs-codegen silecs-model modbus snap7 silecs-communication-cpp silecs-diagnostic-cpp silecs_cli silecs-cli-client"
 
 SCRIPT=$(readlink -f "$0")
 SCRIPTPATH=$(dirname "$SCRIPT")     # path where this script is located in
diff --git a/modbus/.gitignore b/modbus/.gitignore
new file mode 100644
index 0000000..07bbcf8
--- /dev/null
+++ b/modbus/.gitignore
@@ -0,0 +1 @@
+/libmodbus-*/
diff --git a/modbus/build.sh b/modbus/build.sh
new file mode 100755
index 0000000..707014e
--- /dev/null
+++ b/modbus/build.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+set -e
+
+# First argument has to be the desired modbus version
+MODBUS_VERSION=$1
+
+# Second argument is the install location
+RELEASE_DIR=$2
+
+SCRIPT=$(readlink -f "$0")
+SCRIPTPATH=$(dirname "$SCRIPT")     # path where this script is located in
+MODBUS_FOLDER=libmodbus-${MODBUS_VERSION}
+MODBUS_PATH=${SCRIPTPATH}/${MODBUS_FOLDER}
+
+echo ${MODBUS_PATH}
+# If there is no modbus folder yet, we download the required version and build it
+if [ -d ${MODBUS_PATH} ]; then
+  echo "Some modbus version is already available. No need to download/build it"
+  #TODO Check if the version matches
+else
+  
+  archiveFile=${MODBUS_FOLDER}.tar.gz
+  archiveFilePath=${SCRIPTPATH}/${archiveFile}
+
+  if [ ! -f "$archiveFilePath" ]
+  then
+     echo "Downloading modbus"
+     cd ${SCRIPTPATH}
+     wget https://github.com/stephane/libmodbus/releases/download/v${MODBUS_VERSION}/libmodbus-${MODBUS_VERSION}.tar.gz  
+  fi
+
+  if [ ! -d ${MODBUS_PATH} ]
+  then
+     echo "Extracting modbus"
+     tar -vxzf ${archiveFilePath}
+     rm ${archiveFilePath}
+  fi
+
+  echo "Building and installing modbus"
+  
+  cd ${MODBUS_FOLDER}
+  ./configure --enable-static --prefix=${RELEASE_DIR}
+  make 
+  make install
+fi
+
+
diff --git a/modbus/install.sh b/modbus/install.sh
new file mode 100755
index 0000000..f105c6d
--- /dev/null
+++ b/modbus/install.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+set -e
+
+RELEASE_DIR_BASE=$1
+SILECS_VERSION=$2
+
+MODBUS_VERSION_FULL=3.1.10
+MODBUS_VERSION_MAJOR=`echo ${MODBUS_VERSION_FULL} | cut -d. -f1`
+MODBUS_VERSION_MINOR=`echo ${MODBUS_VERSION_FULL} | cut -d. -f2`
+
+SCRIPT=$(readlink -f "$0")
+SCRIPTPATH=$(dirname "$SCRIPT")     # path where this script is located in
+
+RELEASE_DIR=${RELEASE_DIR_BASE}/modbus/${MODBUS_VERSION_FULL}
+if [ -d ${RELEASE_DIR} ]; then 
+    echo "Error: ${RELEASE_DIR} already exists ...skipping"
+    exit 1
+fi
+
+# Download and build modbus if required
+${SCRIPTPATH}/build.sh ${MODBUS_VERSION_FULL} ${RELEASE_DIR}
+
+# Make sure everybody can read all installed files
+chmod a+r -R ${RELEASE_DIR}
+chmod a+x -R ${RELEASE_DIR}
+
+# Make all files write-protected to prevent overwriting an old version by accident
+chmod a-w -R ${RELEASE_DIR}
+    
+# Update link to latest version
+LATEST_LINK=${RELEASE_DIR_BASE}/modbus/latest
+if [ -L ${LATEST_LINK} ]; then
+    rm ${LATEST_LINK}
+fi
+ln -fvs ${RELEASE_DIR_BASE}/modbus/${MODBUS_VERSION_FULL} ${LATEST_LINK}
+
+# Update links to major version
+MAJOR_LINK=${RELEASE_DIR_BASE}/modbus/${MODBUS_VERSION_MAJOR}
+if [ -L ${MAJOR_LINK} ]; then
+    rm ${MAJOR_LINK}
+fi
+ln -fvs ${RELEASE_DIR_BASE}/modbus/${MODBUS_VERSION_FULL} ${MAJOR_LINK}
+
+# Update links to major.minor version
+MAJOR_MINOR_LINK=${RELEASE_DIR_BASE}/modbus/${MODBUS_VERSION_MAJOR}.${MODBUS_VERSION_MINOR}
+if [ -L ${MAJOR_MINOR_LINK} ]; then
+    rm ${MAJOR_MINOR_LINK}
+fi
+ln -fvs ${RELEASE_DIR_BASE}/modbus/${MODBUS_VERSION_FULL} ${MAJOR_MINOR_LINK}
+
+
+
diff --git a/silecs-communication-cpp/Makefile.dep b/silecs-communication-cpp/Makefile.dep
index 1716312..f8eca7b 100644
--- a/silecs-communication-cpp/Makefile.dep
+++ b/silecs-communication-cpp/Makefile.dep
@@ -5,7 +5,7 @@ SNAP7_BASE ?= ../snap7/snap7-full
 BOOST_HOME ?= /opt/gsi/3rdparty/boost/$(BOOST_VERSION)
 
 ifdef MODBUS_SUPPORT_ENABLED
-	MODBUS_VERSION = 3.1.7
+	MODBUS_VERSION = latest
 	MODBUS_BASE=/common/usr/cscofe/silecs/modbus/$(MODBUS_VERSION)
 	DEPENDENT_COMPILER_OPTIONS += -I$(MODBUS_BASE)/include
 endif
diff --git a/silecs-communication-cpp/squash_modbus_lib.mri b/silecs-communication-cpp/squash_modbus_lib.mri
index 145b315..68d0d6d 100644
--- a/silecs-communication-cpp/squash_modbus_lib.mri
+++ b/silecs-communication-cpp/squash_modbus_lib.mri
@@ -1,5 +1,5 @@
 create build/lib/x86_64/libsquashed.a
 addlib build/lib/x86_64/libsilecs-comm.a
-addlib /common/usr/cscofe/silecs/modbus/3.1.7/lib/libmodbus.a
+addlib /common/usr/cscofe/silecs/modbus/latest/lib/libmodbus.a
 save
 end
-- 
GitLab