CVS resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / kwsys / testDynamicLoader.cxx
blobd3abc7702f7cdc5bc91ae8f1f00dbb842f02cb36
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__)
21 #include <be/kernel/OS.h> /* disable_debugger() API. */
22 #endif
24 // Work-around CMake dependency scanning limitation. This must
25 // duplicate the above list of headers.
26 #if 0
27 # include "DynamicLoader.hxx.in"
28 # include "kwsys_ios_iostream.h.in"
29 # include "kwsys_stl_string.hxx.in"
30 #endif
32 // Include with <> instead of "" to avoid getting any in-source copy
33 // left on disk.
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;
41 #ifdef CMAKE_INTDIR
42 slname += "/";
43 slname += CMAKE_INTDIR;
44 #endif
45 slname += "/";
46 slname += kwsys::DynamicLoader::LibPrefix();
47 slname += lname;
48 slname += kwsys::DynamicLoader::LibExtension();
50 return slname;
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) )
67 kwsys_ios::cerr
68 << kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
69 return 1;
71 kwsys::DynamicLoader::SymbolPointer f
72 = kwsys::DynamicLoader::GetSymbolAddress(l, symbol);
73 if( (r2 && !f) || (!r2 && f) )
75 kwsys_ios::cerr
76 << kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
77 return 1;
79 #ifndef __APPLE__
80 int s = kwsys::DynamicLoader::CloseLibrary(l);
81 if( (r3 && !s) || (!r3 && s) )
83 kwsys_ios::cerr
84 << kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
85 return 1;
87 #else
88 (void)r3;
89 #endif
90 return 0;
93 int testDynamicLoader(int argc, char *argv[])
95 #if defined(_WIN32)
96 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
97 #elif defined(__BEOS__)
98 disable_debugger(1);
99 #endif
100 int res = 0;
101 if( argc == 3 )
103 // User specify a libname and symbol to check.
104 res = TestDynamicLoader(argv[1], argv[2],1,1,1);
105 return res;
108 // dlopen() on Syllable before 11/22/2007 doesn't return 0 on error
109 #ifndef __SYLLABLE__
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);
114 #endif
116 #ifdef __linux__
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);
121 #endif
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);
130 return res;