Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load library version #1074

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 69 additions & 12 deletions scripts/mklib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@

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
echo "--description --options"
exit 0
fi
MANUAL='Compile one or more *.cpp files into a shared library.

Usage:

plumed mklib [options] files1.cpp [file2.cpp ...]

Options:
-h, --help
Print this help and exit
-o LIBNAME, --out LIBNAME
Name of the output library. If missing, the name
of the first input file will me used, with its
suffix properly adjusted.
-n No-clobber mode, similar to `mv -n`.
Does not overwrite an existing library.
If the library exists when the command is started,
skip also the compilation phase.

Environment variables:
PLUMED_MKLIB_CFLAGS Extra compile time flags to be used
PLUMED_MKLIB_LDFLAGS Extra link time flags (and libraries) to be used
'

OPTIONS="--description --options -h --help -o --out -n"

if [ $# == 0 ]
then
Expand All @@ -21,17 +38,53 @@ fi

source "$PLUMED_ROOT"/src/config/compile_options.sh

lib="${1%%.cpp}".$soext
prefix=""
lib=""
no_clobber=""
files=() # empty array
for opt
do
prefixopt="$prefix$opt"
prefix=""
case "$prefixopt" in
(-o|--out) prefix="--out=";;
(-h|--help) echo "$MANUAL" ; exit ;;
(--options) echo "$OPTIONS" ; exit ;;
(--out=*) lib="${prefixopt#--out=}";;
(-n) no_clobber=yes;;
(-*)
echo "ERROR: Unknown option $opt. Use --help for help."
exit 1 ;;
(*)
files+=("${prefixopt}");;
esac
done

if [ ${#files[@]} = 0 ] ; then
echo ERROR
echo "pass at least one file"
exit 1
fi

if [ -z "$lib" ]
then
firstfile="${files[0]}"
lib="${firstfile%%.cpp}".$soext
fi

recompile=no
for file
for file in "${files[@]}"
do
if ! test $lib -nt $file ;
then
recompile=yes
fi
done

if test -z "$no_clobber" ; then
recompile=yes
fi

if test $recompile = no ; then
echo "$lib is already up to date"
exit 0
Expand All @@ -53,7 +106,7 @@ tmpdir=$(mktemp -d "plumed_mklib.XXXXXX")

toRemove="${toRemove} $tmpdir"

for file
for file in "${files[@]}"
do

if [[ "$file" != *.cpp ]] ;
Expand Down Expand Up @@ -114,5 +167,9 @@ fi

eval "$link_command" "$PLUMED_MKLIB_LDFLAGS" $objs -o "$tmpdir/$lib"

# || true is necessary with recent coreutils
mv -n "$tmpdir/$lib" $lib || true
if test -n "$no_clobber" ; then
# || true is necessary with recent coreutils
mv -n "$tmpdir/$lib" $lib || true
else
mv "$tmpdir/$lib" $lib
fi
11 changes: 7 additions & 4 deletions src/core/PlumedMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1255,14 +1255,16 @@
extension=libName.substr(n+1);
if(n!=std::string::npos && n<libName.length())
base=libName.substr(0,n);

if(extension=="cpp") {
libName="./"+base+"."+config::getVersionLong()+"."+config::getSoExt();
// full path command, including environment setup
// this will work even if plumed is not in the execution path or if it has been
// installed with a name different from "plumed"
std::string cmd=config::getEnvCommand()+" \""+config::getPlumedRoot()+"\"/scripts/mklib.sh "+libName;
std::string cmd=config::getEnvCommand()+" \""+config::getPlumedRoot()+"\"/scripts/mklib.sh -n -o "+libName+" "+fileName;

if(std::getenv("PLUMED_LOAD_ACTION_DEBUG")) log<<"Executing: "<<cmd;
else log<<"Compiling: "<<libName;
else log<<"Compiling: "<<fileName<<" to "<<libName;

if(comm.Get_size()>0) log<<" (only on master node)";
log<<"\n";
Expand All @@ -1276,13 +1278,14 @@
// the library is already there, even if running simultaneously).
// It however decreases the system load if many threads are used.
auto s=section.startStop(cmd);
int ret=std::system(cmd.c_str());

Check failure

Code scanning / CodeQL

Uncontrolled data used in OS command Critical

This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
This argument to an OS command is derived from
user input (an environment variable)
, dangerously concatenated into
call to operator+
, and then passed to system(__command).
if(ret!=0) plumed_error() <<"An error happened while executing command "<<cmd<<"\n";
}
comm.Barrier();
base="./"+base;
} else {
libName=base+"."+config::getSoExt();
}
libName=base+"."+config::getSoExt();

// If we have multiple threads (each holding a Plumed object), each of them
// will load the library, but each of them will only see actions registered
// from the owned library
Expand Down
Loading