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)
20 #if defined(__BEOS__) && !defined(__HAIKU__)
21 #include <be/kernel/OS.h> /* disable_debugger() API. */
24 #if defined(__HAIKU__)
25 #include <os/kernel/OS.h> /* disable_debugger() API. */
28 // Work-around CMake dependency scanning limitation. This must
29 // duplicate the above list of headers.
31 # include "DynamicLoader.hxx.in"
32 # include "kwsys_ios_iostream.h.in"
33 # include "kwsys_stl_string.hxx.in"
36 // Include with <> instead of "" to avoid getting any in-source copy
38 #include <testSystemTools.h>
40 kwsys_stl::string
GetLibName(const char* lname
)
42 // Construct proper name of lib
43 kwsys_stl::string slname
;
44 slname
= EXECUTABLE_OUTPUT_PATH
;
47 slname
+= CMAKE_INTDIR
;
50 slname
+= kwsys::DynamicLoader::LibPrefix();
52 slname
+= kwsys::DynamicLoader::LibExtension();
57 /* libname = Library name (proper prefix, proper extension)
58 * System = symbol to lookup in libname
59 * r1: should OpenLibrary succeed ?
60 * r2: should GetSymbolAddress succeed ?
61 * r3: should CloseLibrary succeed ?
63 int TestDynamicLoader(const char* libname
, const char* symbol
, int r1
, int r2
, int r3
)
65 kwsys_ios::cerr
<< "Testing: " << libname
<< kwsys_ios::endl
;
66 kwsys::DynamicLoader::LibraryHandle l
67 = kwsys::DynamicLoader::OpenLibrary(libname
);
68 // If result is incompatible with expectation just fails (xor):
69 if( (r1
&& !l
) || (!r1
&& l
) )
72 << kwsys::DynamicLoader::LastError() << kwsys_ios::endl
;
75 kwsys::DynamicLoader::SymbolPointer f
76 = kwsys::DynamicLoader::GetSymbolAddress(l
, symbol
);
77 if( (r2
&& !f
) || (!r2
&& f
) )
80 << kwsys::DynamicLoader::LastError() << kwsys_ios::endl
;
84 int s
= kwsys::DynamicLoader::CloseLibrary(l
);
85 if( (r3
&& !s
) || (!r3
&& s
) )
88 << kwsys::DynamicLoader::LastError() << kwsys_ios::endl
;
97 int testDynamicLoader(int argc
, char *argv
[])
100 SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOGPFAULTERRORBOX
);
101 #elif defined(__BEOS__) || defined(__HAIKU__)
107 // User specify a libname and symbol to check.
108 res
= TestDynamicLoader(argv
[1], argv
[2],1,1,1);
112 // dlopen() on Syllable before 11/22/2007 doesn't return 0 on error
114 // Make sure that inexistant lib is giving correct result
115 res
+= TestDynamicLoader("azerty_", "foo_bar",0,0,0);
116 // Make sure that random binary file cannnot be assimilated as dylib
117 res
+= TestDynamicLoader(TEST_SYSTEMTOOLS_BIN_FILE
, "wp",0,0,0);
121 // This one is actually fun to test, since dlopen is by default loaded...wonder why :)
122 res
+= TestDynamicLoader("foobar.lib", "dlopen",0,1,0);
123 res
+= TestDynamicLoader("libdl.so", "dlopen",1,1,1);
124 res
+= TestDynamicLoader("libdl.so", "TestDynamicLoader",1,0,1);
126 // Now try on the generated library
127 kwsys_stl::string libname
= GetLibName(KWSYS_NAMESPACE_STRING
"TestDynload");
128 res
+= TestDynamicLoader(libname
.c_str(), "dummy",1,0,1);
129 res
+= TestDynamicLoader(libname
.c_str(), "TestDynamicLoaderSymbolPointer",1,1,1);
130 res
+= TestDynamicLoader(libname
.c_str(), "_TestDynamicLoaderSymbolPointer",1,0,1);
131 res
+= TestDynamicLoader(libname
.c_str(), "TestDynamicLoaderData",1,1,1);
132 res
+= TestDynamicLoader(libname
.c_str(), "_TestDynamicLoaderData",1,0,1);