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-2021 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
26Application
27 foamHasLibrary
28
29Group
30 grpMiscUtilities
31
32Description
33 Test if given libraries can be loaded.
34
35Usage
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
49Note
50 No normal output.
51
52\*---------------------------------------------------------------------------*/
53
54#include "argList.H"
55#include "profiling.H"
56
57using namespace Foam;
58
59// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
60
61int main(int argc, char *argv[])
62{
63 argList::addNote("Test if given libraries can be loaded");
64
65 profiling::disable(); // No profiling output
66 argList::noBanner();
67 argList::noParallel();
68 argList::removeOption("case");
69 argList::removeOption("noFunctionObjects");
70 argList::addBoolOption
71 (
72 "or",
73 "Success if any of the libraries can be loaded\n"
74 "(does not short-circuit)"
75 );
76 argList::addBoolOption
77 (
78 "detail",
79 "Additional detail"
80 );
81 argList::addVerboseOption
82 (
83 "Additional verbosity"
84 );
85
86 argList::addArgument("lib...");
87 argList::noMandatoryArgs(); // Arguments are optional
88
89 argList args(argc, argv, false, true);
90
91 // Force dlOpen of FOAM_DLOPEN_LIBS (principally for Windows applications)
92 #include "foamDlOpenLibs.H"
93
94 const bool testOr = args.found("or");
95 const bool detail = args.found("detail");
96
97 label ngood = 0;
98 label nbad = 0;
99
100 dlLibraryTable& libs = args.libs();
101
102 wordHashSet loaded;
103
104 for (int argi = 1; argi < args.size(); ++argi)
105 {
106 const auto libName = args.get<fileName>(argi); // with validate
107
108 if (libName.empty())
109 {
110 continue;
111 }
112
113 // InfoErr << "Check " << libName << nl;
114
115 // Could have libs.findLibrary(...)
116 // if we really expect many duplicates
117
118 const void* ptr = libs.open(libName, false);
119
120 if (!ptr)
121 {
122 ++nbad;
123 }
124 else
125 {
126 ++ngood;
127
128 if (args.verbose())
129 {
130 const word addr(Foam::name(ptr));
131
132 if (loaded.insert(addr))
133 {
134 InfoErr << "Can load " << libName << nl;
135 }
136 else
137 {
138 InfoErr << "Already loaded " << libName << nl;
139 }
140 }
141 }
142 }
143
144 if (detail)
145 {
146 InfoErr << libs.info();
147 }
148
149 return (nbad == 0 || (testOr && ngood > 0)) ? 0 : 1;
150}
151
152
153// ************************************************************************* //
bool insert(const Key &key)
Insert a new entry, not overwriting existing entries.
Definition: HashSet.H:191
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:124
T get(const label index) const
Get a value from the argument at index.
Definition: argListI.H:278
int verbose() const noexcept
Return the verbose flag.
Definition: argListI.H:128
label size() const noexcept
The number of arguments.
Definition: argListI.H:146
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:178
dlLibraryTable & libs() const noexcept
Mutable access to the loaded dynamic libraries.
Definition: argListI.H:140
A table of dynamically loaded libraries.
bool open(bool verbose=true)
InfoProxy< dlLibraryTable > info() const
Return info proxy.
A class for handling file names.
Definition: fileName.H:76
A class for handling words, derived from Foam::string.
Definition: word.H:68
Namespace for OpenFOAM.
messageStream InfoErr
Information stream (stderr output on master, null elsewhere)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
Foam::argList args(argc, argv)