Commit 2dcbc09b authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: add foamHasLibrary to test for availability of libraries (#1735)

- For some cases it can be helpful to test if additional libraries can
  be properly resolved.

  This can be useful in scripts to test for additional capability:

      if foamHasLibrary geometricVoF
      then ...
      fi

  But also directly from the command-line to help resolve configuration
  issues:

      foamHasLibrary -verbose petscFoam

  Could not load "petscFoam"
  libpetsc.so.3.13: cannot open shared object file: No such file or directory
parent 420be3f9
foamHasLibrary.C
EXE = $(FOAM_APPBIN)/foamHasLibrary
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application
foamHasLibrary
Group
grpMiscUtilities
Description
Test if given libraries can be loaded.
Usage
\b foamHasLibrary [OPTION] lib...
Options:
- \par -or
Success if any of the libraries can be loaded.
Does not short-circuit.
- \par -verbose
Additional verbosity
Note
No normal output.
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "profiling.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
{
argList::addNote("Test if given libraries can be loaded");
profiling::disable(); // No profiling output
argList::noBanner();
argList::noParallel();
argList::removeOption("case");
argList::removeOption("noFunctionObjects");
argList::addBoolOption
(
"or",
"Success if any of the libraries can be loaded\n"
"(does not short-circuit)"
);
argList::addBoolOption
(
"verbose",
"Additional verbosity"
);
argList::addArgument("lib...");
argList::noMandatoryArgs(); // Arguments are optional
argList args(argc, argv, false, true);
// Force dlOpen of FOAM_DLOPEN_LIBS (principally for Windows applications)
#include "foamDlOpenLibs.H"
const bool testOr = args.found("or");
const bool verbose = args.found("verbose");
label ngood = 0;
label nbad = 0;
dlLibraryTable& libs = args.libs();
wordHashSet loaded;
for (int argi = 1; argi < args.size(); ++argi)
  • should I also use int instead of label in for-loops, or other places you prefer?

  • Generally we use label on for-loops, since we normally iterate over List or other containers that all use label for their size(). With command-line arguments it is not so clear. The system will always use int for argc (the arg count) so it feels more natural to also use that when looping over command-line arguments, even although they have been filtered and repackaged into our own List, which uses label for its sizing...

    So for this case, could go either way (int or label). It will be impossible to populate the list with more than 32-bit anyhow and an implicit conversion from int32 to int64 in the loop (when compiled with 64 bit) is not exactly a performance killer here.

Please register or sign in to reply
{
const fileName libName(fileName::validate(args[argi]));
if (libName.empty())
{
continue;
}
// InfoErr << "Check " << libName << nl;
Please register or sign in to reply
// Could have libs.findLibrary(...)
// if we really expect many duplicates
const void* ptr = libs.open(libName, false);
if (!ptr)
{
++nbad;
}
else
{
++ngood;
if (verbose)
{
const word addr(Foam::name(ptr));
if (loaded.insert(addr))
{
InfoErr << "Can load " << libName << nl;
}
else
{
InfoErr << "Already loaded " << libName << nl;
}
}
}
}
const bool okay
(
testOr
? (ngood > 0 || nbad == 0)
: nbad == 0
);
return okay ? 0 : 1;
}
// ************************************************************************* //
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment