1 /*=========================================================================
3 Program: KWSys - Kitware System Library
4 Module: $RCSfile: testDynamicLoader.cxx,v $
6 Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
13 =========================================================================*/
14 #include "kwsysPrivate.h"
16 #include KWSYS_HEADER(DynamicLoader.hxx)
17 #include KWSYS_HEADER(ios/iostream)
18 #include KWSYS_HEADER(stl/string)
21 #include <be/kernel/OS.h> /* disable_debugger() API. */
24 // Work-around CMake dependency scanning limitation. This must
25 // duplicate the above list of headers.
27 # include "DynamicLoader.hxx.in"
28 # include "kwsys_ios_iostream.h.in"
29 # include "kwsys_stl_string.hxx.in"
32 // Include with <> instead of "" to avoid getting any in-source copy
34 #include <testSystemTools.h>
36 kwsys_stl::string
GetLibName(const char* lname
)
38 // Construct proper name of lib
39 kwsys_stl::string slname
;
40 slname
= EXECUTABLE_OUTPUT_PATH
;
43 slname
+= CMAKE_INTDIR
;
46 slname
+= kwsys::DynamicLoader::LibPrefix();
48 slname
+= kwsys::DynamicLoader::LibExtension();
53 /* libname = Library name (proper prefix, proper extension)
54 * System = symbol to lookup in libname
55 * r1: should OpenLibrary succeed ?
56 * r2: should GetSymbolAddress succeed ?
57 * r3: should CloseLibrary succeed ?
59 int TestDynamicLoader(const char* libname
, const char* symbol
, int r1
, int r2
, int r3
)
61 kwsys_ios::cerr
<< "Testing: " << libname
<< kwsys_ios::endl
;
62 kwsys::DynamicLoader::LibraryHandle l
63 = kwsys::DynamicLoader::OpenLibrary(libname
);
64 // If result is incompatible with expectation just fails (xor):
65 if( (r1
&& !l
) || (!r1
&& l
) )
68 << kwsys::DynamicLoader::LastError() << kwsys_ios::endl
;
71 kwsys::DynamicLoader::SymbolPointer f
72 = kwsys::DynamicLoader::GetSymbolAddress(l
, symbol
);
73 if( (r2
&& !f
) || (!r2
&& f
) )
76 << kwsys::DynamicLoader::LastError() << kwsys_ios::endl
;
80 int s
= kwsys::DynamicLoader::CloseLibrary(l
);
81 if( (r3
&& !s
) || (!r3
&& s
) )
84 << kwsys::DynamicLoader::LastError() << kwsys_ios::endl
;
93 int testDynamicLoader(int argc
, char *argv
[])
96 SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOGPFAULTERRORBOX
);
97 #elif defined(__BEOS__)
103 // User specify a libname and symbol to check.
104 res
= TestDynamicLoader(argv
[1], argv
[2],1,1,1);
108 // dlopen() on Syllable before 11/22/2007 doesn't return 0 on error
110 // Make sure that inexistant lib is giving correct result
111 res
+= TestDynamicLoader("azerty_", "foo_bar",0,0,0);
112 // Make sure that random binary file cannnot be assimilated as dylib
113 res
+= TestDynamicLoader(TEST_SYSTEMTOOLS_BIN_FILE
, "wp",0,0,0);
117 // This one is actually fun to test, since dlopen is by default loaded...wonder why :)
118 res
+= TestDynamicLoader("foobar.lib", "dlopen",0,1,0);
119 res
+= TestDynamicLoader("libdl.so", "dlopen",1,1,1);
120 res
+= TestDynamicLoader("libdl.so", "TestDynamicLoader",1,0,1);
122 // Now try on the generated library
123 kwsys_stl::string libname
= GetLibName(KWSYS_NAMESPACE_STRING
"TestDynload");
124 res
+= TestDynamicLoader(libname
.c_str(), "dummy",1,0,1);
125 res
+= TestDynamicLoader(libname
.c_str(), "TestDynamicLoaderSymbolPointer",1,1,1);
126 res
+= TestDynamicLoader(libname
.c_str(), "_TestDynamicLoaderSymbolPointer",1,0,1);
127 res
+= TestDynamicLoader(libname
.c_str(), "TestDynamicLoaderData",1,1,1);
128 res
+= TestDynamicLoader(libname
.c_str(), "_TestDynamicLoaderData",1,0,1);