Skip to content

Commit

Permalink
-fixed dynamic libarary loading with path names
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12146 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Jun 20, 2012
1 parent ce37188 commit aef4431
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 38 deletions.
14 changes: 9 additions & 5 deletions Compiler/Script/CevalScript.mo
Expand Up @@ -798,7 +798,7 @@ algorithm
plotCmd,tmpPlotFile,call,str_1,mp,pathstr,name,cname,fileNamePrefix_s,errMsg,errorStr,uniqueStr,interpolation,
title,xLabel,yLabel,filename2,varNameStr,xml_filename,xml_contents,visvar_str,pwd,omhome,omlib,omcpath,os,
platform,usercflags,senddata,res,workdir,gcc,confcmd,touch_file,uname,filenameprefix,compileDir,from,to,
legendStr, gridStr, logXStr, logYStr, x1Str, x2Str, y1Str, y2Str,scriptFile,logFile;
legendStr, gridStr, logXStr, logYStr, x1Str, x2Str, y1Str, y2Str,scriptFile,logFile, simflags2;
list<Values.Value> vals;
Absyn.Path path,p1,classpath,className;
SCode.Program scodeP,sp;
Expand Down Expand Up @@ -1338,17 +1338,21 @@ algorithm
(cache,simValue,st);

case (cache,env,"simulate",vals as Values.CODE(Absyn.C_TYPENAME(className))::_,st_1,msg)
equation

equation
System.realtimeTick(RT_CLOCK_SIMULATE_TOTAL);
(cache,st,compileDir,executable,method_str,outputFormat_str,_,simflags,resultValues) = buildModel(cache,env,vals,st_1,msg);

cit = winCitation();
ifcpp=Util.equal(Config.simCodeTarget(),"Cpp");
executable1=Util.if_(ifcpp,Settings.getInstallationDirectoryPath() +& "/Simulation",executable);
compileDir=Util.if_(ifcpp,Settings.getInstallationDirectoryPath() +& "/bin/" ,compileDir);
simflags2=Util.if_(ifcpp,stringAppendList({compileDir," ","./"}),"");
executable1=Util.if_(ifcpp,"Simulation",executable);
executableSuffixedExe = stringAppend(executable1, System.getExeExt());
// sim_call = stringAppendList({"sh -c ",cit,"ulimit -t 60; ",cit,pwd,pd,executableSuffixedExe,cit," > output.log 2>&1",cit});
sim_call = stringAppendList({cit,compileDir,executableSuffixedExe,cit," ",simflags," > output.log 2>&1"});
System.realtimeTick(RT_CLOCK_SIMULATE_SIMULATION);
sim_call = stringAppendList({cit,compileDir,executableSuffixedExe,cit," ",simflags," ",simflags2," > output.log 2>&1"});
print("\n Simulation call: " +& sim_call +& "\n" );
System.realtimeTick(RT_CLOCK_SIMULATE_SIMULATION);
SimulationResults.close() "Windows cannot handle reading and writing to the same file from different processes like any real OS :(";
0 = System.systemCall(sim_call);

Expand Down
5 changes: 4 additions & 1 deletion SimulationRuntime/cpp/Source/CMakeLists.txt
Expand Up @@ -36,7 +36,7 @@ SET(USE_SUNDIALS ON)
SET(Boost_USE_STATIC_LIBS OFF)
SET(Boost_USE_MULTITHREADED ON)
SET(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.45.0 COMPONENTS serialization REQUIRED)
find_package(Boost 1.45.0 COMPONENTS filesystem system serialization REQUIRED)
# Lapack and Blas
find_package( BLAS )
find_package( LAPACK )
Expand Down Expand Up @@ -158,12 +158,15 @@ GET_FILENAME_COMPONENT(libModelicaName ${libModelica} NAME)
GET_TARGET_PROPERTY(libSystem System LOCATION)
GET_FILENAME_COMPONENT(libSystemName ${libSystem} NAME)

GET_TARGET_PROPERTY(libSolver Solver LOCATION)
GET_FILENAME_COMPONENT(libSolverName ${libSolver} NAME)

set (EULER_LIB ${libEulerName})
set (SETTINGSFACTORY_LIB ${libSetFactoryName})
set (MODELICASYSTEM_LIB ${libModelicaName})
set (NEWTON_LIB ${libNewtonName})
set (SYSTEM_LIB ${libSystemName})
set (SOLVER_LIB ${libSolverName})
# configure a header file to pass some library names
# to the source code which are used to dynamic load and instantiate

Expand Down
19 changes: 10 additions & 9 deletions SimulationRuntime/cpp/Source/LibrariesConfig.h.in
@@ -1,9 +1,10 @@

#define EULER_LIB "@EULER_LIB@"
#define CVODE_LIB "@CVODE_LIB@"
#define IDAS_LIB "@IDAS_LIB@"
#define IDA_LIB "@IDA_LIB@"
#define SETTINGSFACTORY_LIB "@SETTINGSFACTORY_LIB@"
#define MODELICASYSTEM_LIB "@MODELICASYSTEM_LIB@"
#define NEWTON_LIB "@NEWTON_LIB@"
#define SYSTEM_LIB "@SYSTEM_LIB@"

#define EULER_LIB "@EULER_LIB@"
#define CVODE_LIB "@CVODE_LIB@"
#define IDAS_LIB "@IDAS_LIB@"
#define IDA_LIB "@IDA_LIB@"
#define SETTINGSFACTORY_LIB "@SETTINGSFACTORY_LIB@"
#define MODELICASYSTEM_LIB "@MODELICASYSTEM_LIB@"
#define NEWTON_LIB "@NEWTON_LIB@"
#define SYSTEM_LIB "@SYSTEM_LIB@"
#define SOLVER_LIB "@SOLVER_LIB@"
Expand Up @@ -17,8 +17,11 @@ SettingsFactory::~SettingsFactory(void)


}
tuple<boost::shared_ptr<IGlobalSettings>,boost::shared_ptr<ISolverSettings> > SettingsFactory::create()
tuple<boost::shared_ptr<IGlobalSettings>,boost::shared_ptr<ISolverSettings> > SettingsFactory::create(fs::path libraries_path)
{



cout<<"Read Settings..."<<std::endl;

//load global settings or use default settings
Expand All @@ -27,6 +30,7 @@ tuple<boost::shared_ptr<IGlobalSettings>,boost::shared_ptr<ISolverSettings> > Se
std::string solver_dll;
//Load solver dll


if(_global_settings->getSelectedSolver().compare("Euler")==0)
solver_dll.assign(EULER_LIB);
else if(_global_settings->getSelectedSolver().compare("Idas")==0)
Expand All @@ -43,8 +47,18 @@ tuple<boost::shared_ptr<IGlobalSettings>,boost::shared_ptr<ISolverSettings> > Se
_global_settings->getSelectedSolver().append("Settings.xml"));
type_map types;

if(!load_single_library(types,solver_dll))
throw std::invalid_argument(solver_dll + " library could not be loaded");
fs::path solver_name(solver_dll);
fs::path solver_path = libraries_path;
solver_path/=solver_name;
fs::path solver_default_name(SOLVER_LIB);
fs::path solver_default_path = libraries_path;
solver_default_path/=solver_default_name;
cout << "loading solver" << std::endl;
if(!load_single_library(types,solver_default_path.c_str()))
throw std::invalid_argument(solver_default_path.native() + " library could not be loaded");

if(!load_single_library(types,solver_path.c_str()))
throw std::invalid_argument(solver_path.native() + " library could not be loaded");
//get solver factory
std::map<std::string, factory<ISolverSettings, IGlobalSettings* > >::iterator iter;
std::map<std::string, factory<ISolverSettings, IGlobalSettings* > >& factories(types.get());
Expand Down
Expand Up @@ -6,8 +6,8 @@
class /*BOOST_EXTENSION_SETTINGSFACTORY_DECL*/ SettingsFactory : public ISettingsFactory
{
public:
/*DLL_EXPORT*/ SettingsFactory(void);
/*DLL_EXPORT*/ tuple<boost::shared_ptr<IGlobalSettings>,boost::shared_ptr<ISolverSettings> > create();
/*DLL_EXPORT*/ SettingsFactory();
/*DLL_EXPORT*/ tuple<boost::shared_ptr<IGlobalSettings>,boost::shared_ptr<ISolverSettings> > create(fs::path libraries_path);
/*DLL_EXPORT*/ ~SettingsFactory(void);
private:
boost::shared_ptr<IGlobalSettings> _global_settings;
Expand Down
Expand Up @@ -21,15 +21,16 @@ using namespace std;
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/shared_ptr.hpp>

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>

#include "Utils/extension/shared_library.hpp"
#include "Utils/extension/extension.hpp"
#include "Utils/extension/extension.hpp"
#include "Utils/extension/factory.hpp"
#include "Utils/extension/convenience.hpp"
using namespace boost::extensions;

namespace fs = boost::filesystem;
using std::ios;
using boost::tuple;
using boost::shared_ptr;
Expand Down
Expand Up @@ -8,7 +8,7 @@ class ISettingsFactory
public:
ISettingsFactory() {};
virtual ~ISettingsFactory(void) {};
virtual tuple<boost::shared_ptr<IGlobalSettings>,boost::shared_ptr<ISolverSettings> > create() =0;
virtual tuple<boost::shared_ptr<IGlobalSettings>,boost::shared_ptr<ISolverSettings> > create(fs::path libraries_path) =0;


};
20 changes: 14 additions & 6 deletions SimulationRuntime/cpp/Source/SimManager/Configuration.cpp
Expand Up @@ -2,11 +2,15 @@
#include "Configuration.h"
#include <boost/algorithm/string.hpp>
#include "LibrariesConfig.h"
Configuration::Configuration(void)
Configuration::Configuration(fs::path libraries_path)
:_libraries_path(libraries_path)
{
type_map types;
std::string settings_name(SETTINGSFACTORY_LIB);
if(!load_single_library(types, settings_name))

fs::path settings_name(SETTINGSFACTORY_LIB);
fs::path settings_path = libraries_path;
settings_path/=settings_name;
if(!load_single_library(types,settings_path.c_str()))
throw std::invalid_argument("Settings factory library could not be loaded");
std::map<std::string, factory<ISettingsFactory> >::iterator iter;
std::map<std::string, factory<ISettingsFactory> >& factories(types.get());
Expand All @@ -17,7 +21,7 @@ Configuration::Configuration(void)
throw std::invalid_argument("No such settings library");
}
_settings_factory = boost::shared_ptr<ISettingsFactory>(iter->second.create());
tie(_global_settings,_solver_settings) =_settings_factory->create();
tie(_global_settings,_solver_settings) =_settings_factory->create(libraries_path);


}
Expand Down Expand Up @@ -52,8 +56,12 @@ IDAESolver* Configuration::createSolver(IDAESystem* system)
else
throw std::invalid_argument("Selected Solver is not available");

if(!load_single_library(types, solver_dll))
throw std::invalid_argument(solver_dll + "library could not be loaded");
fs::path solver_name(solver_dll);
fs::path solver_path = _libraries_path;
solver_path/=solver_name;

if(!load_single_library(types,solver_path.c_str()))
throw std::invalid_argument(solver_path.native() + "library could not be loaded");
std::map<std::string, factory<IDAESolver,IDAESystem*, ISolverSettings*> >::iterator iter;
std::map<std::string, factory<IDAESolver,IDAESystem*, ISolverSettings*> >& factories(types.get());
iter = factories.find(solver);
Expand Down
4 changes: 3 additions & 1 deletion SimulationRuntime/cpp/Source/SimManager/Configuration.h
@@ -1,4 +1,5 @@
#pragma once
#include <boost/filesystem/path.hpp>
#include "SettingsFactory/Interfaces/IGlobalSettings.h"
#include "Solver/Interfaces/ISolverSettings.h"
#include "Solver/Interfaces/IDAESolver.h"
Expand All @@ -8,7 +9,7 @@
class Configuration
{
public:
Configuration(void);
Configuration(fs::path libraries_path);
~Configuration(void);
IDAESolver* createSolver(IDAESystem* system);
IGlobalSettings* getGlobalSettings();
Expand All @@ -19,4 +20,5 @@ class Configuration
boost::shared_ptr<ISolverSettings> _solver_settings;
boost::shared_ptr<IGlobalSettings> _global_settings;
boost::shared_ptr<IDAESolver> _solver;
fs::path _libraries_path;
};
28 changes: 23 additions & 5 deletions SimulationRuntime/cpp/Source/SimManager/Main.cpp
Expand Up @@ -3,6 +3,7 @@
#include "Configuration.h"
#include "System/Interfaces/ISystemProperties.h"
#include "LibrariesConfig.h"
namespace fs = boost::filesystem;

#if defined(_MSC_VER) || defined(__MINGW32__)
#include <tchar.h>
Expand All @@ -11,18 +12,35 @@ int _tmain(int argc, _TCHAR* argv[])
int main(int argc, const char* argv[])
#endif
{


if(argc < 3)
throw std::invalid_argument("No runtime library path and Modelica system library path defined");

fs::path libraries_path = fs::path( argv[1] ) ;
fs::path modelica_path = fs::path( argv[2] ) ;

std::cout << libraries_path << " end" << std::endl;
try
{

Configuration config;
Configuration config(libraries_path);

IGlobalSettings* global_settings = config.getGlobalSettings();
//Load Modelica sytem library
type_map types;
std::string modelica_name(MODELICASYSTEM_LIB);
if(!load_single_library(types, modelica_name))

fs::path modelica_system_name(MODELICASYSTEM_LIB);
fs::path modelica_system_path = modelica_path;
modelica_system_path/=modelica_system_name;

fs::path default_system_name(SYSTEM_LIB);
fs::path default_system_path = libraries_path;
default_system_path/=default_system_name;

if(!load_single_library(types, default_system_path.c_str()))
throw std::invalid_argument("System default library could not be loaded");


if(!load_single_library(types, modelica_system_path.c_str()))
throw std::invalid_argument("ModelicaSystem library could not be loaded");
std::map<std::string, factory<IDAESystem,IGlobalSettings&> >::iterator iter;
std::map<std::string, factory<IDAESystem,IGlobalSettings&> >& factories(types.get());
Expand Down
5 changes: 4 additions & 1 deletion SimulationRuntime/cpp/Source/SimManager/stdafx.h
Expand Up @@ -17,6 +17,8 @@
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include "boost/tuple/tuple.hpp"
#include <boost/shared_ptr.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include "Utils/extension/extension.hpp"
#include "Utils/extension/shared_library.hpp"
#include "Utils/extension/convenience.hpp"
Expand All @@ -25,4 +27,5 @@ using boost::tuple;
using boost::tie;
using namespace boost::numeric;
using namespace std;
using boost::shared_ptr;
using boost::shared_ptr;
namespace fs = boost::filesystem;
Expand Up @@ -6,7 +6,7 @@ add_library(System SHARED AlgLoopDefaultImplementation AlgLoopSolverFactory.cpp


install (TARGETS System DESTINATION bin)
install (FILES SystemDefaultImplementation.h AlgloopDefaultImplementation.h EventHandling.h DESTINATION include/omc/cpp/System/Implementation)
install (FILES SystemDefaultImplementation.h AlgLoopDefaultImplementation.h EventHandling.h DESTINATION include/omc/cpp/System/Implementation)
install (FILES ../Interfaces/IAlgLoop.h
../Interfaces/IAlgLoopSolver.h
../Interfaces/IAlgLoopSolverFactory.h
Expand Down
Expand Up @@ -9,7 +9,7 @@ target_link_libraries (Modelica System ModelicaExternalC)

GET_TARGET_PROPERTY(libModelicaSystem Modelica LOCATION)
GET_FILENAME_COMPONENT(libModelicaSystemName ${libModelicaSystem} NAME)
set (BOOST_LIBS ${Boost_LIBRARIES})
#set (BOOST_LIBS ${Boost})
set (BOOS_INCLUDE ${Boost_INCLUDE_DIRS})
set (MODELICA_SYSTEM_LIB ${libModelicaSystemName})
configure_file (
Expand Down

0 comments on commit aef4431

Please sign in to comment.