2010-03-21 23:53:50 +00:00
|
|
|
//
|
2011-02-25 00:35:29 +00:00
|
|
|
// Copyright 2010-2011 Ettus Research LLC
|
2018-02-19 23:30:32 +00:00
|
|
|
// Copyright 2018 Ettus Research, a National Instruments Company
|
2010-03-21 23:53:50 +00:00
|
|
|
//
|
2018-02-19 23:30:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2010-03-21 23:53:50 +00:00
|
|
|
//
|
|
|
|
|
|
2015-01-28 00:07:43 +00:00
|
|
|
#include <uhd/utils/paths.hpp>
|
2010-03-27 08:02:58 +00:00
|
|
|
#include <uhd/utils/static.hpp>
|
2011-02-25 00:35:29 +00:00
|
|
|
#include <uhd/exception.hpp>
|
2010-03-21 23:53:50 +00:00
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
#include <iostream>
|
2010-08-09 18:24:08 +00:00
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
2010-03-21 23:53:50 +00:00
|
|
|
|
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* Module Load Function
|
|
|
|
|
**********************************************************************/
|
2011-02-22 03:03:13 +00:00
|
|
|
#ifdef HAVE_DLOPEN
|
2010-03-21 23:53:50 +00:00
|
|
|
#include <dlfcn.h>
|
|
|
|
|
static void load_module(const std::string &file_name){
|
|
|
|
|
if (dlopen(file_name.c_str(), RTLD_LAZY) == NULL){
|
2011-02-25 00:35:29 +00:00
|
|
|
throw uhd::os_error(str(
|
2010-03-21 23:53:50 +00:00
|
|
|
boost::format("dlopen failed to load \"%s\"") % file_name
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-22 03:03:13 +00:00
|
|
|
#endif /* HAVE_DLOPEN */
|
2010-03-21 23:53:50 +00:00
|
|
|
|
|
|
|
|
|
2011-02-22 03:03:13 +00:00
|
|
|
#ifdef HAVE_LOAD_LIBRARY
|
|
|
|
|
#include <windows.h>
|
2010-03-21 23:53:50 +00:00
|
|
|
static void load_module(const std::string &file_name){
|
|
|
|
|
if (LoadLibrary(file_name.c_str()) == NULL){
|
2011-02-25 00:35:29 +00:00
|
|
|
throw uhd::os_error(str(
|
2010-03-21 23:53:50 +00:00
|
|
|
boost::format("LoadLibrary failed to load \"%s\"") % file_name
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-22 03:03:13 +00:00
|
|
|
#endif /* HAVE_LOAD_LIBRARY */
|
2010-03-21 23:53:50 +00:00
|
|
|
|
|
|
|
|
|
2011-02-22 03:03:13 +00:00
|
|
|
#ifdef HAVE_LOAD_MODULES_DUMMY
|
2010-03-21 23:53:50 +00:00
|
|
|
static void load_module(const std::string &file_name){
|
2011-02-25 00:35:29 +00:00
|
|
|
throw uhd::not_implemented_error(str(
|
2010-03-21 23:53:50 +00:00
|
|
|
boost::format("Module loading not supported: Cannot load \"%s\"") % file_name
|
|
|
|
|
));
|
|
|
|
|
}
|
2011-02-22 03:03:13 +00:00
|
|
|
#endif /* HAVE_LOAD_MODULES_DUMMY */
|
2010-03-21 23:53:50 +00:00
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* Load Modules
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
/*!
|
|
|
|
|
* Load all modules in a given path.
|
|
|
|
|
* This will recurse into sub-directories.
|
|
|
|
|
* Does not throw, prints to std error.
|
|
|
|
|
* \param path the filesystem path
|
|
|
|
|
*/
|
2010-08-09 18:24:08 +00:00
|
|
|
static void load_module_path(const fs::path &path){
|
2010-03-21 23:53:50 +00:00
|
|
|
if (not fs::exists(path)){
|
2011-03-16 01:21:59 +00:00
|
|
|
//std::cerr << boost::format("Module path \"%s\" not found.") % path.string() << std::endl;
|
2010-03-21 23:53:50 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//try to load the files in this path
|
|
|
|
|
if (fs::is_directory(path)){
|
|
|
|
|
for(
|
|
|
|
|
fs::directory_iterator dir_itr(path);
|
|
|
|
|
dir_itr != fs::directory_iterator();
|
|
|
|
|
++dir_itr
|
|
|
|
|
){
|
2010-08-09 18:24:08 +00:00
|
|
|
load_module_path(dir_itr->path());
|
2010-03-21 23:53:50 +00:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//its not a directory, try to load it
|
|
|
|
|
try{
|
2011-03-16 01:21:59 +00:00
|
|
|
load_module(path.string());
|
2010-03-21 23:53:50 +00:00
|
|
|
}
|
|
|
|
|
catch(const std::exception &err){
|
|
|
|
|
std::cerr << boost::format("Error: %s") % err.what() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-29 19:36:17 +00:00
|
|
|
/*!
|
2010-08-09 18:24:08 +00:00
|
|
|
* Load all the modules given in the module paths.
|
2010-03-21 23:53:50 +00:00
|
|
|
*/
|
2010-03-27 08:02:58 +00:00
|
|
|
UHD_STATIC_BLOCK(load_modules){
|
2017-02-10 07:19:55 +00:00
|
|
|
for(const fs::path &path: uhd::get_module_paths()){
|
2010-08-09 18:24:08 +00:00
|
|
|
load_module_path(path);
|
2010-03-21 23:53:50 +00:00
|
|
|
}
|
|
|
|
|
}
|