2009-10-02 01:21:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
KWSys - Kitware System Library
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2006-03-09 22:35:56 +03:00
|
|
|
|
2009-10-02 01:21:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2006-03-09 22:35:56 +03:00
|
|
|
|
2009-10-02 01:21:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2006-03-09 22:35:56 +03:00
|
|
|
#include "kwsysPrivate.h"
|
|
|
|
|
|
|
|
#include KWSYS_HEADER(DynamicLoader.hxx)
|
|
|
|
#include KWSYS_HEADER(ios/iostream)
|
2006-03-10 18:07:42 +03:00
|
|
|
#include KWSYS_HEADER(stl/string)
|
2006-03-09 22:35:56 +03:00
|
|
|
|
2008-09-16 01:53:28 +04:00
|
|
|
#if defined(__BEOS__) && !defined(__HAIKU__)
|
2006-12-05 01:26:41 +03:00
|
|
|
#include <be/kernel/OS.h> /* disable_debugger() API. */
|
|
|
|
#endif
|
|
|
|
|
2008-09-16 01:53:28 +04:00
|
|
|
#if defined(__HAIKU__)
|
|
|
|
#include <os/kernel/OS.h> /* disable_debugger() API. */
|
|
|
|
#endif
|
|
|
|
|
2006-03-09 22:35:56 +03:00
|
|
|
// Work-around CMake dependency scanning limitation. This must
|
|
|
|
// duplicate the above list of headers.
|
|
|
|
#if 0
|
|
|
|
# include "DynamicLoader.hxx.in"
|
|
|
|
# include "kwsys_ios_iostream.h.in"
|
2006-03-10 18:28:43 +03:00
|
|
|
# include "kwsys_stl_string.hxx.in"
|
2006-03-09 22:35:56 +03:00
|
|
|
#endif
|
|
|
|
|
2007-03-07 21:52:32 +03:00
|
|
|
// Include with <> instead of "" to avoid getting any in-source copy
|
|
|
|
// left on disk.
|
|
|
|
#include <testSystemTools.h>
|
2006-03-10 00:40:34 +03:00
|
|
|
|
2006-03-10 01:08:33 +03:00
|
|
|
kwsys_stl::string GetLibName(const char* lname)
|
|
|
|
{
|
|
|
|
// Construct proper name of lib
|
|
|
|
kwsys_stl::string slname;
|
2006-03-10 23:08:40 +03:00
|
|
|
slname = EXECUTABLE_OUTPUT_PATH;
|
|
|
|
#ifdef CMAKE_INTDIR
|
2006-03-10 23:42:33 +03:00
|
|
|
slname += "/";
|
2006-03-10 23:08:40 +03:00
|
|
|
slname += CMAKE_INTDIR;
|
|
|
|
#endif
|
2006-03-10 23:12:04 +03:00
|
|
|
slname += "/";
|
|
|
|
slname += kwsys::DynamicLoader::LibPrefix();
|
2006-03-10 01:08:33 +03:00
|
|
|
slname += lname;
|
|
|
|
slname += kwsys::DynamicLoader::LibExtension();
|
|
|
|
|
|
|
|
return slname;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* libname = Library name (proper prefix, proper extension)
|
2006-03-10 00:40:34 +03:00
|
|
|
* System = symbol to lookup in libname
|
|
|
|
* r1: should OpenLibrary succeed ?
|
|
|
|
* r2: should GetSymbolAddress succeed ?
|
|
|
|
* r3: should CloseLibrary succeed ?
|
|
|
|
*/
|
|
|
|
int TestDynamicLoader(const char* libname, const char* symbol, int r1, int r2, int r3)
|
2006-03-09 22:35:56 +03:00
|
|
|
{
|
2006-03-10 23:38:35 +03:00
|
|
|
kwsys_ios::cerr << "Testing: " << libname << kwsys_ios::endl;
|
2006-03-16 19:01:05 +03:00
|
|
|
kwsys::DynamicLoader::LibraryHandle l
|
|
|
|
= kwsys::DynamicLoader::OpenLibrary(libname);
|
2006-03-10 00:40:34 +03:00
|
|
|
// If result is incompatible with expectation just fails (xor):
|
|
|
|
if( (r1 && !l) || (!r1 && l) )
|
2006-03-09 22:35:56 +03:00
|
|
|
{
|
|
|
|
kwsys_ios::cerr
|
|
|
|
<< kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-03-16 19:01:05 +03:00
|
|
|
kwsys::DynamicLoader::SymbolPointer f
|
|
|
|
= kwsys::DynamicLoader::GetSymbolAddress(l, symbol);
|
2006-03-10 00:40:34 +03:00
|
|
|
if( (r2 && !f) || (!r2 && f) )
|
2006-03-09 22:35:56 +03:00
|
|
|
{
|
|
|
|
kwsys_ios::cerr
|
|
|
|
<< kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-03-13 22:39:50 +03:00
|
|
|
#ifndef __APPLE__
|
2006-03-10 00:40:34 +03:00
|
|
|
int s = kwsys::DynamicLoader::CloseLibrary(l);
|
|
|
|
if( (r3 && !s) || (!r3 && s) )
|
2006-03-09 22:35:56 +03:00
|
|
|
{
|
|
|
|
kwsys_ios::cerr
|
|
|
|
<< kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2006-03-13 23:57:40 +03:00
|
|
|
#else
|
|
|
|
(void)r3;
|
2006-03-13 22:39:50 +03:00
|
|
|
#endif
|
2006-03-09 22:35:56 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-03 18:47:06 +03:00
|
|
|
int testDynamicLoader(int argc, char *argv[])
|
2006-03-09 22:35:56 +03:00
|
|
|
{
|
2006-03-30 22:49:56 +04:00
|
|
|
#if defined(_WIN32)
|
|
|
|
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
|
2008-09-23 20:04:39 +04:00
|
|
|
#elif defined(__BEOS__) || defined(__HAIKU__)
|
2006-12-05 01:26:41 +03:00
|
|
|
disable_debugger(1);
|
2006-03-30 22:49:56 +04:00
|
|
|
#endif
|
2007-11-24 04:45:49 +03:00
|
|
|
int res = 0;
|
2006-03-13 18:27:42 +03:00
|
|
|
if( argc == 3 )
|
|
|
|
{
|
|
|
|
// User specify a libname and symbol to check.
|
|
|
|
res = TestDynamicLoader(argv[1], argv[2],1,1,1);
|
|
|
|
return res;
|
|
|
|
}
|
2007-11-24 04:45:49 +03:00
|
|
|
|
|
|
|
// dlopen() on Syllable before 11/22/2007 doesn't return 0 on error
|
|
|
|
#ifndef __SYLLABLE__
|
2006-03-10 00:40:34 +03:00
|
|
|
// Make sure that inexistant lib is giving correct result
|
2007-11-24 04:45:49 +03:00
|
|
|
res += TestDynamicLoader("azerty_", "foo_bar",0,0,0);
|
2006-03-10 00:40:34 +03:00
|
|
|
// Make sure that random binary file cannnot be assimilated as dylib
|
|
|
|
res += TestDynamicLoader(TEST_SYSTEMTOOLS_BIN_FILE, "wp",0,0,0);
|
2007-11-24 04:45:49 +03:00
|
|
|
#endif
|
|
|
|
|
2006-03-10 00:40:34 +03:00
|
|
|
#ifdef __linux__
|
|
|
|
// This one is actually fun to test, since dlopen is by default loaded...wonder why :)
|
|
|
|
res += TestDynamicLoader("foobar.lib", "dlopen",0,1,0);
|
|
|
|
res += TestDynamicLoader("libdl.so", "dlopen",1,1,1);
|
|
|
|
res += TestDynamicLoader("libdl.so", "TestDynamicLoader",1,0,1);
|
|
|
|
#endif
|
2006-03-10 01:08:33 +03:00
|
|
|
// Now try on the generated library
|
2007-03-03 18:47:06 +03:00
|
|
|
kwsys_stl::string libname = GetLibName(KWSYS_NAMESPACE_STRING "TestDynload");
|
2006-03-10 19:32:09 +03:00
|
|
|
res += TestDynamicLoader(libname.c_str(), "dummy",1,0,1);
|
2006-03-16 19:01:05 +03:00
|
|
|
res += TestDynamicLoader(libname.c_str(), "TestDynamicLoaderSymbolPointer",1,1,1);
|
|
|
|
res += TestDynamicLoader(libname.c_str(), "_TestDynamicLoaderSymbolPointer",1,0,1);
|
2006-03-10 19:32:09 +03:00
|
|
|
res += TestDynamicLoader(libname.c_str(), "TestDynamicLoaderData",1,1,1);
|
2006-03-10 19:57:43 +03:00
|
|
|
res += TestDynamicLoader(libname.c_str(), "_TestDynamicLoaderData",1,0,1);
|
2006-03-09 22:35:56 +03:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|