foamHasLibrary.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2020 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Application
27  foamHasLibrary
28 
29 Group
30  grpMiscUtilities
31 
32 Description
33  Test if given libraries can be loaded.
34 
35 Usage
36  \b foamHasLibrary [OPTION] lib...
37 
38  Options:
39  - \par -or
40  Success if any of the libraries can be loaded.
41  Does not short-circuit.
42 
43  - \par -detail
44  Additional detail (meaning may change).
45 
46  - \par -verbose
47  Additional verbosity
48 
49 Note
50  No normal output.
51 
52 \*---------------------------------------------------------------------------*/
53 
54 #include "argList.H"
55 #include "profiling.H"
56 
57 using namespace Foam;
58 
59 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60 
61 int main(int argc, char *argv[])
62 {
63  argList::addNote("Test if given libraries can be loaded");
64 
65  profiling::disable(); // No profiling output
68  argList::removeOption("case");
69  argList::removeOption("noFunctionObjects");
71  (
72  "or",
73  "Success if any of the libraries can be loaded\n"
74  "(does not short-circuit)"
75  );
77  (
78  "detail",
79  "Additional detail"
80  );
82  (
83  "verbose",
84  "Additional verbosity"
85  );
86 
87  argList::addArgument("lib...");
88  argList::noMandatoryArgs(); // Arguments are optional
89 
90  argList args(argc, argv, false, true);
91 
92  // Force dlOpen of FOAM_DLOPEN_LIBS (principally for Windows applications)
93  #include "foamDlOpenLibs.H"
94 
95  const bool testOr = args.found("or");
96  const bool detail = args.found("detail");
97  const bool verbose = args.found("verbose");
98 
99  label ngood = 0;
100  label nbad = 0;
101 
102  dlLibraryTable& libs = args.libs();
103 
104  wordHashSet loaded;
105 
106  for (int argi = 1; argi < args.size(); ++argi)
107  {
108  const fileName libName(fileName::validate(args[argi]));
109 
110  if (libName.empty())
111  {
112  continue;
113  }
114 
115  // InfoErr << "Check " << libName << nl;
116 
117  // Could have libs.findLibrary(...)
118  // if we really expect many duplicates
119 
120  const void* ptr = libs.open(libName, false);
121 
122  if (!ptr)
123  {
124  ++nbad;
125  }
126  else
127  {
128  ++ngood;
129 
130  if (verbose)
131  {
132  const word addr(Foam::name(ptr));
133 
134  if (loaded.insert(addr))
135  {
136  InfoErr << "Can load " << libName << nl;
137  }
138  else
139  {
140  InfoErr << "Already loaded " << libName << nl;
141  }
142  }
143  }
144  }
145 
146  if (detail)
147  {
148  InfoErr << libs.info();
149  }
150 
151  return (nbad == 0 || (testOr && ngood > 0)) ? 0 : 1;
152 }
153 
154 
155 // ************************************************************************* //
Foam::argList::noBanner
static void noBanner()
Disable emitting the banner information.
Definition: argList.C:442
profiling.H
Foam::dlLibraryTable
A table of dynamically loaded libraries.
Definition: dlLibraryTable.H:57
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::fileName::validate
static fileName validate(const std::string &s, const bool doClean=true)
Definition: fileName.C:58
Foam::argList::addNote
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:413
Foam::argList
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:123
Foam::InfoErr
messageStream InfoErr
Information stream (uses stderr - output is on the master only)
Foam::HashSet< word >
Foam::argList::noMandatoryArgs
static void noMandatoryArgs()
Flag command arguments as being optional (non-mandatory)
Definition: argList.C:430
Foam::argList::libs
const dlLibraryTable & libs() const
Access to the loaded libraries.
Definition: argListI.H:115
Foam::dlLibraryTable::info
InfoProxy< dlLibraryTable > info() const
Return info proxy.
Definition: dlLibraryTable.H:182
foamDlOpenLibs.H
Foam::argList::addArgument
static void addArgument(const string &argName, const string &usage="")
Append a (mandatory) argument to validArgs.
Definition: argList.C:302
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
argList.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::profiling::disable
static void disable()
Disallow profiling by forcing the InfoSwitch off.
Definition: profiling.C:118
Foam::dlLibraryTable::open
bool open(bool verbose=true)
Definition: dlLibraryTable.C:227
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:325
Foam::argList::size
label size() const noexcept
The number of arguments.
Definition: argListI.H:127
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:181
Foam::argList::noParallel
static void noParallel()
Remove the parallel options.
Definition: argList.C:491
args
Foam::argList args(argc, argv)
Foam::argList::removeOption
static void removeOption(const word &optName)
Remove option from validOptions and from optionUsage.
Definition: argList.C:422
Foam::argList::found
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:157