Skip to content

Commit

Permalink
Fixed some Windows/MinGW compile issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Jennifer Buehler committed May 8, 2016
1 parent 44dbc35 commit 5aa49c3
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 37 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ set (CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/CMakeMacros/")

find_package(Qhull REQUIRED)
find_package(SoQt4 REQUIRED)
find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)


Expand All @@ -61,7 +62,7 @@ endif(DYNAMICS_ENGINE STREQUAL BULLET_DYNAMICS)


SET( QT_USE_QT3SUPPORT TRUE )
find_package(Qt4 COMPONENTS QtCore REQUIRED)
find_package(Qt4 COMPONENTS QtCore QtSql REQUIRED)

include (${QT_USE_FILE})

Expand Down Expand Up @@ -731,7 +732,8 @@ include_directories(
## Build ##
############

add_definitions(${GRASPIT_DEFINES} -g)
add_definitions(${GRASPIT_DEFINES} ${SOQT_CXXFLAGS} ${LAPACK_LINKER_FLAGS} -g)
link_directories(${SOQT_LINK_DIRS} ${QT_LIBRARY_DIR})

set (GRASPIT_LIBRARY_SOURCES
${GRASPIT_CORE_SOURCES}
Expand Down
2 changes: 1 addition & 1 deletion CMakeMacros/FindQhull.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ set(QHULL_HEADER "${QHULL_HEADER}" CACHE INTERNAL "QHull header" FORCE )


find_file(QHULL_A_HEADER
NAMES qhull_a.h
NAMES libqhull/qhull_a.h qhull_a.h
HINTS "${QHULL_ROOT}" "$ENV{QHULL_ROOT}" "${QHULL_INCLUDE_DIR}"
PATHS "$ENV{PROGRAMFILES}/QHull" "$ENV{PROGRAMW6432}/QHull"
PATH_SUFFIXES qhull include)
Expand Down
37 changes: 30 additions & 7 deletions CMakeMacros/FindSoQt4.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
# SOQT_LIBRARY_DEBUG - the debug version
# SOQT_LIBRARY - a default library, with priority debug.

# function to convert from an msys-style path (/c/...) to
# the required CMake path style on Windows (C:/...)
macro(msys_to_cmake_path MsysPath ResultingPath)
#message("GOT PATH: '${MsysPath}'")
string(REGEX REPLACE "^/([a-zA-Z])/" "\\1:/" ${ResultingPath} "${MsysPath}")
string(REGEX REPLACE ";/([a-zA-Z])/" ";\\1:/" ${ResultingPath} "${${ResultingPath}}")
#message("REPL PATH: '${${ResultingPath}}'")
endmacro()



# --- First, try to find relevant headers with find_path and find_library,
Expand Down Expand Up @@ -91,31 +100,36 @@ endif (SOQT_LIBRARY_DEBUG)

# use soqt-config
find_program(SOQT_CONFIG_EXECUTABLE NAMES soqt-config DOC "soqt-config executable")
find_program(BASH_EXECUTABLE NAMES bash DOC "Bash shell")
mark_as_advanced(SOQT_CONFIG_EXECUTABLE)
mark_as_advanced(BASH_EXECUTABLE)

if(SOQT_CONFIG_EXECUTABLE)
if(SOQT_CONFIG_EXECUTABLE AND BASH_EXECUTABLE)
set(SOQT_LIBRARY_FOUND 1)

execute_process(
COMMAND ${SOQT_CONFIG_EXECUTABLE} --cppflags
COMMAND ${BASH_EXECUTABLE} -l -c "${SOQT_CONFIG_EXECUTABLE} --cppflags"
OUTPUT_VARIABLE _soqtconfig_cppflags
RESULT_VARIABLE _soqtconfig_failed)
string(REGEX REPLACE "[\r\n]" " " _soqtconfig_cppflags "${_soqtconfig_cppflags}")
execute_process(
COMMAND ${SOQT_CONFIG_EXECUTABLE} --includedir
COMMAND ${BASH_EXECUTABLE} -l -c "${SOQT_CONFIG_EXECUTABLE} --includedir"
OUTPUT_VARIABLE _soqtconfig_includedir
RESULT_VARIABLE _soqtconfig_failed)
string(REGEX REPLACE "[\r\n]" " " _soqtconfig_includedir "${_soqtconfig_includedir}")
execute_process(
COMMAND ${SOQT_CONFIG_EXECUTABLE} --ldflags
COMMAND ${BASH_EXECUTABLE} -l -c "${SOQT_CONFIG_EXECUTABLE} --ldflags"
OUTPUT_VARIABLE _soqtconfig_ldflags
RESULT_VARIABLE _soqtconfig_failed)
string(REGEX REPLACE "[\r\n]" " " _soqtconfig_ldflags "${_soqtconfig_ldflags}")
execute_process(
COMMAND ${SOQT_CONFIG_EXECUTABLE} --libs
COMMAND ${BASH_EXECUTABLE} -l -c "${SOQT_CONFIG_EXECUTABLE} --libs"
OUTPUT_VARIABLE _soqtconfig_libs
RESULT_VARIABLE _soqtconfig_failed)

message(STATUS "Result of soqt_config: ${_soqtconfig_libs}")
message(STATUS "soqt_config failed? ${_soqtconfig_failed}")

string(REGEX MATCHALL "(^| )-L([./+-_\\a-zA-Z]*)" _soqtconfig_ldirs "${_soqtconfig_ldflags}")
string(REGEX REPLACE "(^| )-L" "" _soqtconfig_ldirs "${_soqtconfig_ldirs}")

Expand All @@ -142,6 +156,15 @@ if(SOQT_CONFIG_EXECUTABLE)
string(REGEX REPLACE "(^| )-l([./+-_\\a-zA-Z]*)" " " _soqtconfig_ldflags "${_soqtconfig_ldflags}")
string(REGEX REPLACE "(^| )-L([./+-_\\a-zA-Z]*)" " " _soqtconfig_ldflags "${_soqtconfig_ldflags}")



# for MinGW/MSYS have to convert the paths to windows-style
if(MINGW OR MSYS)
msys_to_cmake_path("${_soqtconfig_includedir}" _soqtconfig_includedir)
msys_to_cmake_path("${_soqtconfig_ldirs}" _soqtconfig_ldirs)
endif(MINGW OR MSYS)


separate_arguments(_soqtconfig_includedir)

set( SOQT_CXXFLAGS "${_soqtconfig_cppflags}" )
Expand All @@ -151,7 +174,7 @@ if(SOQT_CONFIG_EXECUTABLE)
set( SOQT_LIBRARY ${_soqtconfig_libs})
set( SOQT_LIBRARY_RELEASE ${SOQT_LIBRARY})
set( SOQT_LIBRARY_DEBUG ${SOQT_LIBRARY})
else(SOQT_CONFIG_EXECUTABLE)
else(SOQT_CONFIG_EXECUTABLE AND BASH_EXECUTABLE)
# soqt include files in local directory
if( MSVC )
set(SOQT_LIBRARY_FOUND 1)
Expand All @@ -172,7 +195,7 @@ else(SOQT_CONFIG_EXECUTABLE)
else( MSVC )
set(SOQT_LIBRARY_FOUND 0)
endif( MSVC )
endif(SOQT_CONFIG_EXECUTABLE)
endif(SOQT_CONFIG_EXECUTABLE AND BASH_EXECUTABLE)

MARK_AS_ADVANCED(
SOQT_LIBRARY_FOUND
Expand Down
2 changes: 1 addition & 1 deletion include/graspitGUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class GraspItGUI
void exitMainLoop();
};

#ifdef WIN32
#if defined(WIN32) && !defined(__MINGW32__)
#ifdef GRASPIT_EXPORTS
#define GRASPIT_API __declspec(dllexport)
#else
Expand Down
30 changes: 28 additions & 2 deletions include/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,32 @@
#ifndef __PLUGIN_H__
#include <string>


// set GRASPIT_USE_WIN_DYNLIB in order to use windows libraries (LoadLibraray etc.)
// instead of the DL library (dlopen etc). This flag is needed in plugin.cpp also.
#ifdef _WIN32
#define GRASPIT_USE_WIN_DYNLIB
#endif

#ifdef GRASPIT_USE_WIN_DYNLIB
#include <windows.h>
// typedef HMODULE PLUGIN_DYNLIB_HANDLE;
typedef HINSTANCE PLUGIN_DYNLIB_HANDLE;
#define PLUGIN_DYNLIB_ERROR plugin_dynlib_error

#define PLUGIN_API_ENTRY
#define PLUGIN_API_CALL __cdecl // __stdcall
#define PLUGIN_CALLBACK __cdecl // __stdcall

#else
typedef void* PLUGIN_DYNLIB_HANDLE;
#define PLUGIN_API_ENTRY
#define PLUGIN_API_CALL
#define PLUGIN_CALLBACK
#endif



//! Defines a plugin that can can be loaded dynamically and used with GraspIt
class Plugin
{
Expand All @@ -45,7 +71,7 @@ class PluginCreator
{
public:
typedef Plugin* (*CreatePluginFctn)();
typedef std::string (*GetTypeFctn)();
typedef PLUGIN_API_ENTRY std::string (PLUGIN_API_CALL *GetTypeFctn)();
private:
void* mLibraryHandle;
CreatePluginFctn mCreatePluginFctn;
Expand Down Expand Up @@ -97,7 +123,7 @@ I.E.
Both the createPlugin and getType functions must be declared using these macros.
*/
#ifdef WIN32
#if defined(WIN32) && !defined(__MINGW32__)
#define PLUGIN_API __declspec(dllexport)
#else
#define PLUGIN_API
Expand Down
10 changes: 5 additions & 5 deletions src/matvec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,8 @@ operator>>(std::istream &is, vec3 &v)
std::ostream &
operator<<(std::ostream &os, const vec3 &v)
{
#ifdef WIN32
int oldFlags = os.setf(std::ios_base::showpos);
#if defined(WIN32) && !defined(__MINGW32__)
int oldFlags = os.setf(std::ios_base::showpos);
#else
#ifdef GCC22
int oldFlags = os.setf(ios::showpos);
Expand Down Expand Up @@ -501,7 +501,7 @@ operator>>(std::istream &is, position &p)
std::ostream &
operator<<(std::ostream &os, const position &p)
{
#ifdef WIN32
#if defined(WIN32) && !defined(__MINGW32__)
int oldFlags = os.setf(std::ios_base::showpos);
#else
#ifdef GCC22
Expand Down Expand Up @@ -536,7 +536,7 @@ operator>>(std::istream &is, mat3 &m)
std::ostream &
operator<<(std::ostream &os, const mat3 &m)
{
#ifdef WIN32
#if defined(WIN32) && !defined(__MINGW32__)
int oldFlags = os.setf(std::ios_base::showpos);
#else
#ifdef GCC22
Expand Down Expand Up @@ -578,7 +578,7 @@ operator>>(std::istream &is, Quaternion &q)
std::ostream&
operator<<(std::ostream &os, const Quaternion &q)
{
#ifdef WIN32
#if defined(WIN32) && !defined(__MINGW32__)
int oldFlags = os.setf(std::ios_base::showpos);
#else
#ifdef GCC22
Expand Down
68 changes: 49 additions & 19 deletions src/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,54 @@
//
//######################################################################

#ifdef WIN32
#include "dlfcn-win32.h"
#define LIBRARY_SUFFIX ".dll"
#else
extern "C"{
#include <dlfcn.h>
}
#define LIBRARY_SUFFIX ".so"
#endif

#include <QFile>

#include "plugin.h"
#include "mytools.h"
#include "debug.h"

#ifdef GRASPIT_USE_WIN_DYNLIB
#include <windows.h>

#define PLUGIN_DYNLIB_OPEN LoadLibrary
#define PLUGIN_DYNLIB_CLOSE FreeLibrary
#define PLUGIN_DYNLIB_IMPORT GetProcAddress
#define LIBRARY_SUFFIX ".dll"

static char* plugin_dynlib_error(void)
{
static char buf[32];
DWORD dw = GetLastError();
if (dw == 0) return NULL;
sprintf(buf,"error 0x%x", (unsigned int)dw);
return buf;
}

#define PLUGIN_DYNLIB_ERROR plugin_dynlib_error

#else // GRASPIT_USE_WIN_DYNLIB
// extern "C"{ // it seems extern C is not needed (any more?)
#include <dlfcn.h>
// }
#define PLUGIN_DYNLIB_OPEN(path) dlopen(path, RTLD_NOW | RTLD_GLOBAL)
#define PLUGIN_DYNLIB_CLOSE dlclose
#define PLUGIN_DYNLIB_IMPORT dlsym

#define PLUGIN_DYNLIB_ERROR dlerror

#define LIBRARY_SUFFIX ".so"
#endif // GRASPIT_USE_WIN_DYNLIB








PluginCreator::~PluginCreator()
{
dlclose(mLibraryHandle);
PLUGIN_DYNLIB_CLOSE(mLibraryHandle);
}


Expand Down Expand Up @@ -114,8 +143,8 @@ PluginCreator* PluginCreator::loadFromLibrary(std::string libName)
}

//look for the library file and load it
void* handle = dlopen(filename.toAscii().constData(), RTLD_NOW | RTLD_GLOBAL);
char *errstr = dlerror();
void* handle = PLUGIN_DYNLIB_OPEN(filename.toAscii().constData());
char *errstr = PLUGIN_DYNLIB_ERROR();
if (!handle) {
DBGA("Failed to open dynamic library " << filename.toAscii().constData() );
if (errstr) DBGA("Error: " << errstr);
Expand All @@ -128,20 +157,21 @@ PluginCreator* PluginCreator::loadFromLibrary(std::string libName)
//see also discussion here:
// http://www.trilithium.com/johan/2004/12/problem-with-dlsym/
//maybe in the future a better solution can be found...
PluginCreator::CreatePluginFctn createPluginFctn;
*(void **)(&createPluginFctn) = dlsym(handle,"createPlugin");
if (dlerror()) {
void * _createPluginFctn = PLUGIN_DYNLIB_IMPORT(handle,"createPlugin");
if (PLUGIN_DYNLIB_ERROR()) {
DBGA("Could not load symbol createPlugin from library " << filename.toAscii().constData());
return NULL;
}
PluginCreator::CreatePluginFctn createPluginFctn = reinterpret_cast<PluginCreator::CreatePluginFctn>(_createPluginFctn);

//read the type of plugin
PluginCreator::GetTypeFctn getTypeFctn;
*(void **)(&getTypeFctn) = dlsym(handle,"getType");
if (dlerror()) {
void * _getTypeFctn = PLUGIN_DYNLIB_IMPORT(handle,"getType");
if (PLUGIN_DYNLIB_ERROR()) {
DBGA("Could not load symbol getType from library " << filename.toAscii().constData());
return NULL;
}
PluginCreator::GetTypeFctn getTypeFctn = reinterpret_cast<PluginCreator::GetTypeFctn>(_getTypeFctn);

std::cout << "Function name " << (*getTypeFctn)() <<std::endl;
std::string type = (*getTypeFctn)();
if (type.empty()) {
Expand Down

0 comments on commit 5aa49c3

Please sign in to comment.