Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / kwsys / testDynamicLoader.cxx
blobc1695994758f300176c39a28021f816cc31126d2
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. */
22 #endif
24 #if defined(__HAIKU__)
25 #include <os/kernel/OS.h> /* disable_debugger() API. */
26 #endif
28 // Work-around CMake dependency scanning limitation. This must
29 // duplicate the above list of headers.
30 #if 0
31 # include "DynamicLoader.hxx.in"
32 # include "kwsys_ios_iostream.h.in"
33 # include "kwsys_stl_string.hxx.in"
34 #endif
36 // Include with <> instead of "" to avoid getting any in-source copy
37 // left on disk.
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;
45 #ifdef CMAKE_INTDIR
46 slname += "/";
47 slname += CMAKE_INTDIR;
48 #endif
49 slname += "/";
50 slname += kwsys::DynamicLoader::LibPrefix();
51 slname += lname;
52 slname += kwsys::DynamicLoader::LibExtension();
54 return slname;
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) )
71 kwsys_ios::cerr
72 << kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
73 return 1;
75 kwsys::DynamicLoader::SymbolPointer f
76 = kwsys::DynamicLoader::GetSymbolAddress(l, symbol);
77 if( (r2 && !f) || (!r2 && f) )
79 kwsys_ios::cerr
80 << kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
81 return 1;
83 #ifndef __APPLE__
84 int s = kwsys::DynamicLoader::CloseLibrary(l);
85 if( (r3 && !s) || (!r3 && s) )
87 kwsys_ios::cerr
88 << kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
89 return 1;
91 #else
92 (void)r3;
93 #endif
94 return 0;
97 int testDynamicLoader(int argc, char *argv[])
99 #if defined(_WIN32)
100 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
101 #elif defined(__BEOS__) || defined(__HAIKU__)
102 disable_debugger(1);
103 #endif
104 int res = 0;
105 if( argc == 3 )
107 // User specify a libname and symbol to check.
108 res = TestDynamicLoader(argv[1], argv[2],1,1,1);
109 return res;
112 // dlopen() on Syllable before 11/22/2007 doesn't return 0 on error
113 #ifndef __SYLLABLE__
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);
118 #endif
120 #ifdef __linux__
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);
125 #endif
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);
134 return res;