Commit 43bfe1b6 authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

#8. Adding intitial radixdl package to suport dynamic library interface.

parent 29e12094
Pipeline #10149 failed with stage
in 48 seconds
......@@ -19,7 +19,7 @@ linux_gcc_testing:
- module load cmake
- which cmake
- module load valgrind
- cmake -D COVERAGE_EXTRA_FLAGS="-s ${CI_PROJECT_DIR}/googletest -d" -D MEMCHECK_COMMAND=$(which valgrind) -DBUILDNAME=$(uname -s)-GCC-4.8.5-Debug-${CI_BUILD_REF_NAME} -DCMAKE_BUILD_TYPE=DEBUG -Dradix_ENABLE_COVERAGE_TESTING=ON -Dradix_ENABLE_TESTS=ON -Dradix_ENABLE_radixams=ON -Dradix_ENABLE_radixio=ON -Dradix_ENABLE_radixmath=ON -Dradix_ENABLE_radixpara=ON -Dradix_ENABLE_radixgeometry=ON -Dradix_ENABLE_radixglls=ON -Dradix_ENABLE_radixcommand=ON -Dradix_ENABLE_googletest=ON ..
- cmake -D COVERAGE_EXTRA_FLAGS="-s ${CI_PROJECT_DIR}/googletest -d" -D MEMCHECK_COMMAND=$(which valgrind) -DBUILDNAME=$(uname -s)-GCC-4.8.5-Debug-${CI_BUILD_REF_NAME} -DCMAKE_BUILD_TYPE=DEBUG -Dradix_ENABLE_COVERAGE_TESTING=ON -Dradix_ENABLE_TESTS=ON -Dradix_ENABLE_radixams=ON -Dradix_ENABLE_radixio=ON -Dradix_ENABLE_radixmath=ON -Dradix_ENABLE_radixpara=ON -Dradix_ENABLE_radixgeometry=ON -Dradix_ENABLE_radixglls=ON -Dradix_ENABLE_radixcommand=ON -Dradix_ENABLE_googletest=ON -Dradix_ENABLE_radixdl=ON ..
- ctest -D ExperimentalStart -D ExperimentalBuild -D ExperimentalTest -DExperimentalMemCheck
linux_openmpi_testing:
......@@ -38,7 +38,7 @@ linux_openmpi_testing:
- cd build_mpi
- module load cmake openmpi/1.8.1
- which cmake
- cmake -DBUILDNAME=$(uname -s)-MPI-1.8.1-Debug-${CI_BUILD_REF_NAME} -DTPL_ENABLE_MPI=ON -DMPI_BASEDIR=`dirname $(dirname $(which mpicc))` -DCMAKE_BUILD_TYPE=DEBUG -Dradix_ENABLE_TESTS=ON -Dradix_ENABLE_radixpara=ON -Dradix_ENABLE_googletest=ON ..
- cmake -DBUILDNAME=$(uname -s)-MPI-1.8.1-Debug-${CI_BUILD_REF_NAME} -DTPL_ENABLE_MPI=ON -DMPI_BASEDIR=`dirname $(dirname $(which mpicc))` -DCMAKE_BUILD_TYPE=DEBUG -Dradix_ENABLE_TESTS=ON -Dradix_ENABLE_radixpara=ON -Dradix_ENABLE_googletest=ON -Dradix_ENABLE_radixdl=ON ..
- ctest -D ExperimentalStart -D ExperimentalBuild -D ExperimentalTest
windows_mingw_testing:
......@@ -57,7 +57,7 @@ windows_mingw_testing:
- git clone https://github.com/lefebvre/googletest googletest
- mkdir build
- cd build
- cmake -DTPL_LAPACK_LIBRARIES=c:/vendors/mingw-w64/lapack/3.6.1/lib/liblapack.a\\\;c:/vendors/mingw-w64/lapack/3.6.1/lib/libblas.a\\\;-lgfortran -DBUILDNAME=Windows-MinGW-GCC-4.8.5-Debug-%CI_BUILD_REF_NAME% -DCMAKE_BUILD_TYPE=DEBUG -Dradix_ENABLE_COVERAGE_TESTING=ON -Dradix_ENABLE_TESTS=ON -Dradix_ENABLE_radixams=ON -Dradix_ENABLE_radixio=ON -Dradix_ENABLE_radixmath=ON -Dradix_ENABLE_radixpara=ON -Dradix_ENABLE_radixgeometry=ON -Dradix_ENABLE_radixglls=ON -Dradix_ENABLE_radixcommand=ON -Dradix_ENABLE_googletest=ON -G "MinGW Makefiles" ..
- cmake -DTPL_LAPACK_LIBRARIES=c:/vendors/mingw-w64/lapack/3.6.1/lib/liblapack.a\\\;c:/vendors/mingw-w64/lapack/3.6.1/lib/libblas.a\\\;-lgfortran -DBUILDNAME=Windows-MinGW-GCC-4.8.5-Debug-%CI_BUILD_REF_NAME% -DCMAKE_BUILD_TYPE=DEBUG -Dradix_ENABLE_COVERAGE_TESTING=ON -Dradix_ENABLE_TESTS=ON -Dradix_ENABLE_radixams=ON -Dradix_ENABLE_radixio=ON -Dradix_ENABLE_radixmath=ON -Dradix_ENABLE_radixpara=ON -Dradix_ENABLE_radixgeometry=ON -Dradix_ENABLE_radixglls=ON -Dradix_ENABLE_radixcommand=ON -Dradix_ENABLE_googletest=ON -Dradix_ENABLE_radixdl=ON -G "MinGW Makefiles" ..
- ctest -D ExperimentalStart -D ExperimentalBuild -D ExperimentalTest
.windows_intel_testing:
......@@ -76,5 +76,5 @@ windows_mingw_testing:
- git clone https://github.com/lefebvre/googletest googletest
- mkdir build
- cd build
- cmake -DTPL_LAPACK_LIBRARIES=c:/vendors/intel/lapack/3.5.0/lib/lapack.lib -DTPL_BLAS_LIBRARIES=c:/vendors/intel/lapack/3.5.0/lib/blas.lib -DBUILDNAME=Windows-Intel-15-Release-%CI_BUILD_REF_NAME% -DCMAKE_C_COMPILER=icl -DCMAKE_CXX_COMPILER=icl -DCMAKE_BUILD_TYPE=RELEASE -Dradix_ENABLE_TESTS=ON -Dradix_ENABLE_ALL_OPTIONAL_PACKAGES=ON -Dradix_ENABLE_SECONDARY_TESTED_CODE=ON -Dradix_ENABLE_radixplot=OFF -Dradix_ENABLE_radixwidgets=OFF -G "NMake Makefiles" ..
- cmake -DTPL_LAPACK_LIBRARIES=c:/vendors/intel/lapack/3.5.0/lib/lapack.lib -DTPL_BLAS_LIBRARIES=c:/vendors/intel/lapack/3.5.0/lib/blas.lib -DBUILDNAME=Windows-Intel-15-Release-%CI_BUILD_REF_NAME% -DCMAKE_C_COMPILER=icl -DCMAKE_CXX_COMPILER=icl -DCMAKE_BUILD_TYPE=RELEASE -Dradix_ENABLE_TESTS=ON -Dradix_ENABLE_ALL_OPTIONAL_PACKAGES=ON -Dradix_ENABLE_SECONDARY_TESTED_CODE=ON -Dradix_ENABLE_radixplot=OFF -Dradix_ENABLE_radixwidgets=OFF -Dradix_ENABLE_radixdl=ON -G "NMake Makefiles" ..
- ctest -D ExperimentalStart -D ExperimentalBuild -D ExperimentalTest
TRIBITS_PACKAGE_DEFINE_DEPENDENCIES(
SUBPACKAGES_DIRS_CLASSIFICATIONS_OPTREQS
bug radixbug SS OPTIONAL
dl radixdl SS OPTIONAL
pattern radixpattern SS OPTIONAL
algorithm radixalgorithm SS OPTIONAL
para radixpara SS OPTIONAL
......
TRIBITS_SUBPACKAGE(dl)
TRIBITS_CONFIGURE_FILE(visibility.hh)
SET(SOURCE
library.cc
)
SET(HEADERS
function.hh
library.hh
)
#
# Add library
TRIBITS_ADD_LIBRARY(radixdl
SOURCES ${SOURCE}
NOINSTALLHEADERS ${HEADERS}
SHARED
DEPLIBS ${CMAKE_DL_LIBS}
)
#
# Add testing directory
TRIBITS_ADD_TEST_DIRECTORIES(tests)
INSTALL(FILES ${HEADERS} DESTINATION "include/radixdl/")
TRIBITS_SUBPACKAGE_POSTPROCESS()
#ifndef SEMBA_DYNAMICLIBRARY_UTILS_H_
#define SEMBA_DYNAMICLIBRARY_UTILS_H_
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef _WIN32
#define SEMBA_DL_MAIN() \
BOOL APIENTRY DllMain(HANDLE hModule, \
DWORD dwReason, \
LPVOID lpReserved) { \
return TRUE; \
}
#else
#define SEMBA_DL_MAIN()
#endif
#if defined(_WIN32) || defined(__CYGWIN__)
# define SEMBA_DL_IMPORT __declspec(dllimport)
# define SEMBA_DL_EXPORT __declspec(dllexport)
#else
# if defined(__GNUC__) && __GNUC__ >= 4
# define SEMBA_DL_IMPORT __attribute__ ((visibility ("default")))
# define SEMBA_DL_EXPORT __attribute__ ((visibility ("default")))
# elif defined(__GNUC__) && __GNUC__ >= 2
# define SEMBA_DL_IMPORT __declspec(dllimport)
# define SEMBA_DL_EXPORT __declspec(dllexport)
# else
# define SEMBA_DL_IMPORT
# define SEMBA_DL_EXPORT
# endif
#endif
#if defined(_WIN32) && !defined(__GNUC__)
#define SEMBA_DL_CALL __cdecl
#else
#define SEMBA_DL_CALL
#endif
#endif /* SEMBA_DYNAMICLIBRARY_UTILS_H_ */
TRIBITS_PACKAGE_DEFINE_DEPENDENCIES(
LIB_REQUIRED_PACKAGES
LIB_OPTIONAL_PACKAGES
TEST_REQUIRED_PACKAGES testframework
TEST_OPTIONAL_PACKAGES
LIB_REQUIRED_TPLS
LIB_OPTIONAL_TPLS
TEST_REQUIRED_TPLS
TEST_OPTIONAL_TPLS
)
#ifndef RADIX_RADIXDL_VISIBILITY_HH_
#define RADIX_RADIXDL_VISIBILITY_HH_
#cmakedefine BUILD_SHARED_LIBS @BUILD_SHARED_LIBS@
#if defined _WIN32 || defined __CYGWIN__
#ifdef BUILD_SHARED_LIBS
#define RADIX_PUBLIC __declspec(dllexport)
#else
#define RADIX_PUBLIC __declspec(dllimport)
#endif
#define RADIX_LOCAL
#else
#define RADIX_PUBLIC __attribute__ ((visibility ("default")))
#define RADIX_LOCAL __attribute__ ((visibility ("hidden")))
#endif
#endif /* RADIX_RADIXDL_VISIBILITY_HH_*/
#ifndef RADIX_RADIXDL_FUNCTION_HH_
#define RADIX_RADIXDL_FUNCTION_HH_
#ifdef _WIN32
#include <windows.h>
#endif
namespace radix {
template <typename return_type, typename ...ARGS>
class DLFunction {
public:
#ifdef _WIN32
Function(FARPROC = nullptr);
#else
DLFunction(void* = nullptr);
#endif
virtual ~DLFunction();
return_type operator()(ARGS...);
private:
#ifdef _WIN32
FARPROC mFunction;
#else
void* mFunction;
#endif
};
} // namespace radix
#include "radixdl/function.i.hh"
#endif /* RADIX_RADIXDL_FUNCTION_HH_ */
#include "radixdl/function.hh"
namespace radix {
#ifdef _WIN32
template<typename return_type, typename ... ARGS>
Function<return_type, ARGS...>::Function(FARPROC function) {
mFunction = function;
}
#else
template<typename return_type, typename ... ARGS>
DLFunction<return_type, ARGS...>::DLFunction(void* function) {
mFunction = function;
}
#endif
template<typename return_type, typename ... ARGS>
DLFunction<return_type, ARGS...>::~DLFunction() {
}
template<typename return_type, typename ... ARGS>
return_type DLFunction<return_type, ARGS...>::operator()(ARGS... args) {
if (mFunction == nullptr) {
return return_type();
}
#ifdef _WIN32
return (*mFunction)(args...);
#else
union {
void* addr;
return_type (*func)(ARGS...);
} aux;
aux.addr = mFunction;
return (*aux.func)(args...);
#endif
}
} //namespace radix
#include "radixbug/bug.hh"
#include "radixdl/library.hh"
namespace radix {
DynamicLibrary::DynamicLibrary(const std::string& filename) {
radix_tagged_line("Starting DynamicLibrary(" << filename << ")");
mFileName = filename;
mHandle = nullptr;
}
DynamicLibrary::~DynamicLibrary() {
radix_tagged_line("Destructing/Closing DynamicLibrary('" << mFileName << "')");
close();
}
bool DynamicLibrary::is_open() const {
return (mHandle != nullptr);
}
bool DynamicLibrary::symbol_exists(const std::string& sym) {
radix_tagged_line("DynamicLibrary symbol_exists(" << sym << ")");
if (mHandle == nullptr) {
radix_tagged_line("\tno library handle.");
return false;
}
#ifdef _WIN32
return (GetProcAddress(mHandle, sym.c_str()) != nullptr);
#else
return (dlsym(mHandle, sym.c_str()) != nullptr);
#endif
}
bool DynamicLibrary::open() {
radix_tagged_line("Opening DynamicLibrary('" << mFileName << "')");
if (mHandle == nullptr) {
#ifdef _WIN32
mHandle = LoadLibrary(mFileName.c_str());
#else
mHandle = dlopen(mFileName.c_str(), RTLD_NOW);
#endif
}
return is_open();
}
bool DynamicLibrary::close() {
if (mHandle != nullptr) {
#ifdef _WIN32
return FreeLibrary(mHandle);
#else
return (dlclose(mHandle) == 0);
#endif
}
return true;
}
} //namespace radix
#ifndef RADIX_RADIXDL_LIBRARY_HH_
#define RADIX_RADIXDL_LIBRARY_HH_
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include <string>
#include "radixdl/function.hh"
namespace radix {
class DynamicLibrary {
public:
DynamicLibrary(const std::string&);
virtual ~DynamicLibrary();
bool is_open() const;
bool symbol_exists(const std::string&);
bool open();
bool close();
template <typename return_type = void, typename ...ARGS>
DLFunction<return_type, ARGS...> symbol(const std::string&);
private:
std::string mFileName;
#ifdef _WIN32
HINSTANCE mHandle;
#else
void* mHandle;
#endif
}; // class DLibrary
template<typename return_type, typename ... ARGS>
DLFunction<return_type, ARGS...> DynamicLibrary::symbol(
const std::string& sym) {
if (mHandle == NULL) {
return DLFunction<return_type, ARGS...>();
}
#ifdef _WIN32
return DLFunction<return_type, ARGS...>(
GetProcAddress(mHandle, sym.c_str()));
#else
return DLFunction<return_type, ARGS...>(
dlsym(mHandle, sym.c_str()));
#endif
}
} // namespace
#endif /* RADIX_RADIXDL_LIBRARY_HH_ */
SET(FAKE_SOURCE
fake_lib/libfunction.cc
fake_lib/libfunction.hh
fake_lib/libobj.cc
fake_lib/libobj.hh
)
TRIBITS_ADD_LIBRARY(
fakelib
SOURCES ${FAKE_SOURCE}
TESTONLY
NO_INSTALL_LIB_OR_HEADERS
)
TRIBITS_ADD_EXECUTABLE_AND_TEST(test_radixdl
SOURCES test_dll.cc)
#include "radixdl/visibility.hh"
#include "radixdl/tests/fake_lib/libfunction.hh"
namespace radix
{
extern "C" {
int RADIX_PUBLIC fake_global_function(int a)
{
return a * 8;
}
}
} // namespace
#ifndef FAKE_LIB_LIBFUNCTION_HH_
#define FAKE_LIB_LIBFUNCTION_HH_
#include "radixdl/visibility.hh"
namespace radix
{
extern "C" {
int RADIX_PUBLIC fake_global_function(int a);
}
}
#endif /** FAKE_LIB_LIBFUNCTION_HH_ */
#include "radixdl/tests/fake_lib/libobj.hh"
namespace radix
{
int FakeObj::fake_object_function(int a)
{
return 2 * a;
}
} // namespace
#ifndef FAKE_LIB_LIBOBJ_HH_
#define FAKE_LIB_LIBOBJ_HH_
namespace radix
{
class FakeObj{
int fake_object_function(int a);
};
} //namespace
#endif /** FAKE_LIB_LIBOBJ_HH_ */
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include "radixdl/library.hh"
#include "radixdl/function.hh"
using namespace radix;
int main(int argc, char** argv)
{
DynamicLibrary library("libfakelib.so");
bool openResult = library.open();
std::cout << "Open result: "<< std::boolalpha << openResult << std::endl;
bool funcExists = library.symbol_exists("fake_global_function");
std::cout << "Function('fake_global_function') exists: "
<< std::boolalpha << funcExists << std::endl;
DLFunction<int, int> func = library.symbol<int,int>("fake_global_function");
int funcResult = func(2);
std::cout << "Function('fake_global_function') correct result: "
<< std::boolalpha << (funcResult == 16) << " -- " << funcResult << std::endl;
if(openResult && funcExists && funcResult) return 0;
return 1;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment