diff --git a/applications/utilities/miscellaneous/foamHasLibrary/Make/files b/applications/utilities/miscellaneous/foamHasLibrary/Make/files new file mode 100644 index 0000000000..9bc0ae1e38 --- /dev/null +++ b/applications/utilities/miscellaneous/foamHasLibrary/Make/files @@ -0,0 +1,3 @@ +foamHasLibrary.C + +EXE = $(FOAM_APPBIN)/foamHasLibrary diff --git a/applications/utilities/miscellaneous/foamHasLibrary/Make/options b/applications/utilities/miscellaneous/foamHasLibrary/Make/options new file mode 100644 index 0000000000..4c3dd783cb --- /dev/null +++ b/applications/utilities/miscellaneous/foamHasLibrary/Make/options @@ -0,0 +1,3 @@ +EXE_INC = + +EXE_LIBS = diff --git a/applications/utilities/miscellaneous/foamHasLibrary/foamHasLibrary.C b/applications/utilities/miscellaneous/foamHasLibrary/foamHasLibrary.C new file mode 100644 index 0000000000..931fc3677e --- /dev/null +++ b/applications/utilities/miscellaneous/foamHasLibrary/foamHasLibrary.C @@ -0,0 +1,148 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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 . + +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) + { + const fileName libName(fileName::validate(args[argi])); + + if (libName.empty()) + { + continue; + } + + // InfoErr << "Check " << libName << nl; + + // 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; +} + + +// ************************************************************************* //