Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 11, 2009, 10:22:55 PM (15 years ago)
Author:
landauf
Message:

Added a dynamic library loader (more or less a copy of Ogre::DynLibManager but with some adjustments for Orxonox). This allows us to load plugins at runtime. Plugin-libraries must be declared with the "PLUGIN" switch in ORXONOX_ADD_LIBRARY in CMake.

Location:
code/branches/libraries
Files:
4 added
7 edited

Legend:

Unmodified
Added
Removed
  • code/branches/libraries/cmake/BuildConfig.cmake

    r5625 r5626  
    172172# when building, don't use the install RPATH already
    173173# (but later on when installing)
    174 SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) 
     174SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
    175175
    176176# the RPATH to be used when installing
  • code/branches/libraries/cmake/TargetUtilities.cmake

    r5615 r5626  
    3535 #      NO_SOURCE_GROUPS:  Don't create msvc source groups
    3636 #      STATIC/SHARED:     Inherited from ADD_LIBRARY
     37 #      PLUGIN:            For dynamic plugin libraries
    3738 #      WIN32:             Inherited from ADD_EXECUTABLE
    3839 #      PCH_NO_DEFAULT:    Do not make precompiled header files default if
     
    8081  SET(_switches   FIND_HEADER_FILES  EXCLUDE_FROM_ALL  ORXONOX_EXTERNAL
    8182                  NO_DLL_INTERFACE   NO_SOURCE_GROUPS  ${_additional_switches}
    82                   PCH_NO_DEFAULT NO_INSTALL)
     83                  PCH_NO_DEFAULT     NO_INSTALL        PLUGIN)
    8384  SET(_list_names LINK_LIBRARIES  VERSION   SOURCE_FILES  DEFINE_SYMBOL
    8485                  TOLUA_FILES     PCH_FILE  PCH_EXCLUDE OUTPUT_NAME)
     
    147148  ENDIF()
    148149
     150  # PLUGIN A
     151  IF(_arg_PLUGIN)
     152    SET(_arg_PLUGIN MODULE)
     153    SET(_arg_SHARED)
     154    SET(_arg_STATIC)
     155  ENDIF()
     156
    149157  # Add the library/executable
    150158  IF("${_target_type}" STREQUAL "LIBRARY")
    151     ADD_LIBRARY(${_target_name} ${_arg_STATIC} ${_arg_SHARED}
     159    ADD_LIBRARY(${_target_name} ${_arg_STATIC} ${_arg_SHARED} ${_arg_PLUGIN}
    152160                ${_arg_EXCLUDE_FROM_ALL} ${_${_target_name}_files})
    153161  ELSE()
    154162    ADD_EXECUTABLE(${_target_name} ${_arg_WIN32} ${_arg_EXCLUDE_FROM_ALL}
    155163                   ${_${_target_name}_files})
     164  ENDIF()
     165
     166  # PLUGIN B
     167  IF (_arg_PLUGIN)
     168    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_PLUGIN_OUTPUT_DIRECTORY})
     169    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_PLUGIN_OUTPUT_DIRECTORY})
     170    ADD_PLUGIN(${_target_name})
    156171  ENDIF()
    157172
     
    174189
    175190  # OUTPUT_NAME
    176   IF(_arg_OUTPUT_NAME )
     191  IF(_arg_OUTPUT_NAME)
    177192    SET_TARGET_PROPERTIES(${_target_name} PROPERTIES OUTPUT_NAME  ${_arg_OUTPUT_NAME})
    178193  ENDIF()
     
    184199
    185200  IF(NOT _arg_STATIC AND NOT _arg_NO_INSTALL)
     201    SET(_library_destination ${ORXONOX_LIBRARY_INSTALL_PATH})
     202    IF (_arg_PLUGIN)
     203      SET(_library_destination ${ORXONOX_PLUGIN_INSTALL_PATH})
     204    ENDIF()
     205
    186206    INSTALL(TARGETS ${_target_name}
    187207      RUNTIME DESTINATION ${ORXONOX_RUNTIME_INSTALL_PATH}
    188       LIBRARY DESTINATION ${ORXONOX_LIBRARY_INSTALL_PATH}
     208      LIBRARY DESTINATION ${_library_destination}
    189209      #ARCHIVE DESTINATION ${ORXONOX_ARCHIVE_INSTALL_PATH}
    190210    )
     
    192212
    193213ENDFUNCTION(TU_ADD_TARGET)
     214
     215
     216# Creates a helper file with name <name_of_the_library>.plugin
     217# This helps finding dynamically loadable plugins at runtime
     218
     219FUNCTION(ADD_PLUGIN _name)
     220  # We use the properties to get the name because the librarys name may differ from
     221  # the targets name (for example orxonox <-> liborxonox)
     222
     223  GET_TARGET_PROPERTY(_target_loc ${_name} LOCATION)
     224  GET_FILENAME_COMPONENT(_target_name ${_target_loc} NAME_WE)
     225
     226  SET(_plugin_filename "${CMAKE_PLUGIN_OUTPUT_DIRECTORY}/${_target_name}.plugin")
     227
     228  FILE(WRITE ${_plugin_filename})
     229
     230  INSTALL(
     231    FILES ${_plugin_filename}
     232    DESTINATION ${ORXONOX_PLUGIN_INSTALL_PATH}
     233  )
     234ENDFUNCTION(ADD_PLUGIN)
  • code/branches/libraries/src/SpecialConfig.h.in

    r5625 r5626  
    8787    const char ORXONOX_LOG_DEV_PATH[]         = "@CMAKE_LOG_OUTPUT_DIRECTORY@/"    BOOST_PP_STRINGIZE(CMAKE_BUILD_TYPE);
    8888#else
     89    const char ORXONOX_PLUGIN_DEV_PATH[]      = "@CMAKE_PLUGIN_OUTPUT_DIRECTORY@";
    8990    const char ORXONOX_CONFIG_DEV_PATH[]      = "@CMAKE_CONFIG_OUTPUT_DIRECTORY@";
    9091    const char ORXONOX_LOG_DEV_PATH[]         = "@CMAKE_LOG_OUTPUT_DIRECTORY@";
  • code/branches/libraries/src/core/CMakeLists.txt

    r3370 r5626  
    2323  ConfigValueContainer.cc
    2424  Core.cc
     25  DynLib.cc
     26  DynLibManager.cc
    2527  Event.cc
    2628  Game.cc
  • code/branches/libraries/src/core/Core.cc

    r5625 r5626  
    4040#include <cstdlib>
    4141#include <cstdio>
     42#include <boost/version.hpp>
    4243#include <boost/filesystem.hpp>
    4344#include <OgreRenderWindow.h>
     
    6869#include "ConfigValueIncludes.h"
    6970#include "CoreIncludes.h"
     71#include "DynLibManager.h"
    7072#include "Factory.h"
    7173#include "GameMode.h"
     
    8082#include "input/InputManager.h"
    8183
     84// Boost 1.36 has some issues with deprecated functions that have been omitted
     85#if (BOOST_VERSION == 103600)
     86#  define BOOST_LEAF_FUNCTION filename
     87#else
     88#  define BOOST_LEAF_FUNCTION leaf
     89#endif
     90
    8291namespace orxonox
    8392{
     
    260269        this->setFixedPaths();
    261270
    262         // TODO: Load plugins
     271        // Create a new dynamic library manager
     272        this->dynLibManager_.reset(new DynLibManager());
     273
     274        // Load plugins
     275        try
     276        {
     277            // We search for helper files with the following extension
     278            std::string pluginextension = ".plugin";
     279            size_t pluginextensionlength = pluginextension.size();
     280
     281            // Search in the directory of our executable
     282            boost::filesystem::path searchpath = this->getRootPath() / ORXONOX_PLUGIN_INSTALL_PATH;
     283
     284            boost::filesystem::directory_iterator file(searchpath);
     285            boost::filesystem::directory_iterator end;
     286
     287            // Iterate through all files
     288            while (file != end)
     289            {
     290                std::string filename = file->BOOST_LEAF_FUNCTION();
     291
     292                // Check if the file ends with the exension in question
     293                if (filename.size() > pluginextensionlength)
     294                {
     295                    if (filename.substr(filename.size() - pluginextensionlength) == pluginextension)
     296                    {
     297                        // We've found a helper file - now load the library with the same name
     298                        std::string library = filename.substr(0, filename.size() - pluginextensionlength);
     299                        boost::filesystem::path librarypath = searchpath / library;
     300
     301                        this->dynLibManager_->load(librarypath.string());
     302                    }
     303                }
     304
     305                ++file;
     306            }
     307        }
     308        catch (const std::exception& e)
     309        {
     310            COUT(1) << "An error occurred while loading plugins: " << e.what() << std::endl;
     311        }
     312        catch (...)
     313        {
     314            COUT(1) << "An error occurred while loading plugins." << std::endl;
     315        }
    263316
    264317        // Parse command line arguments AFTER the plugins have been loaded (static code!)
  • code/branches/libraries/src/core/Core.h

    r5625 r5626  
    119119
    120120            // Mind the order for the destruction!
     121            scoped_ptr<DynLibManager>     dynLibManager_;
    121122            scoped_ptr<SignalHandler>     signalHandler_;
    122123            SimpleScopeGuard              identifierDestroyer_;
  • code/branches/libraries/src/core/CorePrereqs.h

    r3370 r5626  
    112112    class ConsoleCommand;
    113113    class Core;
     114    class DynLib;
     115    class DynLibManager;
    114116    struct Event;
    115117    class EventContainer;
Note: See TracChangeset for help on using the changeset viewer.