From e9f01baa33e5a118847200f8faa7ef005879bbff Mon Sep 17 00:00:00 2001 From: Daniele Date: Wed, 22 Nov 2023 14:58:27 +0100 Subject: [PATCH 1/3] mklib deletes tmp files also in case of failure --- scripts/mklib.sh | 65 ++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/scripts/mklib.sh b/scripts/mklib.sh index 059ed69795..592c684b9e 100755 --- a/scripts/mklib.sh +++ b/scripts/mklib.sh @@ -1,77 +1,82 @@ #! /usr/bin/env bash -if [ "$1" = --description ] ; then - echo "compile a .cpp file into a shared library" +if [ "$1" = --description ]; then + echo "compile one or more *.cpp files into a shared library" exit 0 fi -if [ "$1" = --options ] ; then +if [ "$1" = --options ]; then echo "--description --options" exit 0 fi -source "$PLUMED_ROOT"/src/config/compile_options.sh - -if [ $# == 0 ] -then +if [ $# == 0 ]; then echo "ERROR" echo "type 'plumed mklib file1.cpp [file2.cpp etc]'" exit 1 fi +source "$PLUMED_ROOT"/src/config/compile_options.sh + lib="${1%%.cpp}".$soext rm -f "$lib" +toRemove="" +remover() { + #if compilation fails or crashes the trap will delete the temporary files + for f in $toRemove; do + rm -f "${f}" + done +} +trap "remover" EXIT objs="" -for file -do +for file; do - if [[ "$file" != *.cpp ]] ; - then + if [[ "$file" != *.cpp ]]; then echo "ERROR" echo "type 'plumed mklib file1.cpp [file2.cpp etc]'" exit 1 fi obj="${file%%.cpp}".o - - if [ ! -f "$file" ] - then + + if [ ! -f "$file" ]; then echo "ERROR: I cannot find file $file" exit 1 fi #adding a simple tmpfile, to preprocess "in place" the input file, #this assumes the user has write permission in the current directory #which should be true since we are going to compile and output something here - tmpfile=$(mktemp ${file%.cpp}.XXXXXX).cpp + tmpfile=$(mktemp "${file%.cpp}.XXXXXX") + mv "${tmpfile}" "${tmpfile}.cpp" + tmpfile=${tmpfile}.cpp cp "${file}" "${tmpfile}" - + toRemove="${toRemove} ${tmpfile} ${tmpfile}.bak" + if grep -q '^#include "\(bias\|colvar\|function\|sasa\|vatom\)\/ActionRegister.h"' "${tmpfile}"; then - >&2 echo 'WARNING: using a legacy ActionRegister.h include path, please use <<#include "core/ActionRegister.h">>' - sed -i.bak 's%^#include ".*/ActionRegister.h"%#include "core/ActionRegister.h"%g' "${tmpfile}" + echo >&2 'WARNING: using a legacy ActionRegister.h include path, please use <<#include "core/ActionRegister.h">>' + sed -i.bak 's%^#include ".*/ActionRegister.h"%#include "core/ActionRegister.h"%g' "${tmpfile}" fi - + if grep -q '^#include "\(cltools\)\/CLToolRegister.h"' "${tmpfile}"; then - >&2 echo 'WARNING: using a legacy CLToolRegister.h include path, please use <<#include "core/CLToolRegister.h">>' - sed -i.bak 's%^#include ".*/CLToolRegister.h"%#include "core/CLToolRegister.h"%g' "${tmpfile}" + echo >&2 'WARNING: using a legacy CLToolRegister.h include path, please use <<#include "core/CLToolRegister.h">>' + sed -i.bak 's%^#include ".*/CLToolRegister.h"%#include "core/CLToolRegister.h"%g' "${tmpfile}" fi - - rm -f "$obj" - eval "$compile" "$PLUMED_MKLIB_CFLAGS" -o "$obj" "$tmpfile" || { + rm -f "$obj" + eval "$compile" "$PLUMED_MKLIB_CFLAGS" "$tmpfile" -o "$obj" || { echo "ERROR: compiling $file" exit 1 } - rm -f ${tmpfile} ${tmpfile}.bak ${tmpfile%.cpp} objs="$objs $obj" - done -if test "$PLUMED_IS_INSTALLED" = yes ; then - eval "$link_installed" "$PLUMED_MKLIB_LDFLAGS" -o "$lib" "$objs" -else - eval "$link_uninstalled" "$PLUMED_MKLIB_LDFLAGS" -o "$lib" "$objs" +link_command="$link_uninstalled" + +if test "$PLUMED_IS_INSTALLED" = yes; then + link_command="$link_installed" fi +eval "$link_command" "$PLUMED_MKLIB_LDFLAGS" $objs -o "$lib" From 3a2685d434824cd6dfec8a9bf12cfe7726f0783d Mon Sep 17 00:00:00 2001 From: Daniele Date: Wed, 22 Nov 2023 15:15:22 +0100 Subject: [PATCH 2/3] added a few line explaining the new PLUMED_MKLIB_*FLAGS variables --- scripts/mklib.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/mklib.sh b/scripts/mklib.sh index 592c684b9e..ac4d4d1efe 100755 --- a/scripts/mklib.sh +++ b/scripts/mklib.sh @@ -2,6 +2,8 @@ if [ "$1" = --description ]; then echo "compile one or more *.cpp files into a shared library" + echo " you can create and export the variable PLUMED_MKLIB_CFLAGS with some extra compile time flags to be used" + echo " you can create and export the variable PLUMED_MKLIB_LDFLAGS with some extra link time flags (and libraries) to be used" exit 0 fi From 1ff0a97f19c92f5e2bbe4d16fc011118002eddcf Mon Sep 17 00:00:00 2001 From: Daniele Date: Mon, 27 Nov 2023 17:20:05 +0100 Subject: [PATCH 3/3] Reverted to old formattation for mklib.sh --- scripts/mklib.sh | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/scripts/mklib.sh b/scripts/mklib.sh index ac4d4d1efe..290a1d8cc8 100755 --- a/scripts/mklib.sh +++ b/scripts/mklib.sh @@ -1,18 +1,19 @@ #! /usr/bin/env bash -if [ "$1" = --description ]; then +if [ "$1" = --description ] ; then echo "compile one or more *.cpp files into a shared library" echo " you can create and export the variable PLUMED_MKLIB_CFLAGS with some extra compile time flags to be used" echo " you can create and export the variable PLUMED_MKLIB_LDFLAGS with some extra link time flags (and libraries) to be used" exit 0 fi -if [ "$1" = --options ]; then +if [ "$1" = --options ] ; then echo "--description --options" exit 0 fi -if [ $# == 0 ]; then +if [ $# == 0 ] +then echo "ERROR" echo "type 'plumed mklib file1.cpp [file2.cpp etc]'" exit 1 @@ -33,17 +34,20 @@ remover() { trap "remover" EXIT objs="" -for file; do +for file +do - if [[ "$file" != *.cpp ]]; then + if [[ "$file" != *.cpp ]] ; + then echo "ERROR" echo "type 'plumed mklib file1.cpp [file2.cpp etc]'" exit 1 fi obj="${file%%.cpp}".o - - if [ ! -f "$file" ]; then + + if [ ! -f "$file" ] + then echo "ERROR: I cannot find file $file" exit 1 fi @@ -57,22 +61,24 @@ for file; do toRemove="${toRemove} ${tmpfile} ${tmpfile}.bak" if grep -q '^#include "\(bias\|colvar\|function\|sasa\|vatom\)\/ActionRegister.h"' "${tmpfile}"; then - echo >&2 'WARNING: using a legacy ActionRegister.h include path, please use <<#include "core/ActionRegister.h">>' - sed -i.bak 's%^#include ".*/ActionRegister.h"%#include "core/ActionRegister.h"%g' "${tmpfile}" + >&2 echo 'WARNING: using a legacy ActionRegister.h include path, please use <<#include "core/ActionRegister.h">>' + sed -i.bak 's%^#include ".*/ActionRegister.h"%#include "core/ActionRegister.h"%g' "${tmpfile}" fi - + if grep -q '^#include "\(cltools\)\/CLToolRegister.h"' "${tmpfile}"; then - echo >&2 'WARNING: using a legacy CLToolRegister.h include path, please use <<#include "core/CLToolRegister.h">>' - sed -i.bak 's%^#include ".*/CLToolRegister.h"%#include "core/CLToolRegister.h"%g' "${tmpfile}" + >&2 echo 'WARNING: using a legacy CLToolRegister.h include path, please use <<#include "core/CLToolRegister.h">>' + sed -i.bak 's%^#include ".*/CLToolRegister.h"%#include "core/CLToolRegister.h"%g' "${tmpfile}" fi - + rm -f "$obj" - eval "$compile" "$PLUMED_MKLIB_CFLAGS" "$tmpfile" -o "$obj" || { + + eval "$compile" "$PLUMED_MKLIB_CFLAGS" -o "$obj" "$tmpfile" || { echo "ERROR: compiling $file" exit 1 } objs="$objs $obj" + done link_command="$link_uninstalled"