Skip to content

Commit

Permalink
Add DLL-relative path to search path for plugins.
Browse files Browse the repository at this point in the history
  • Loading branch information
abellgithub committed Jul 16, 2020
1 parent 06dc4cc commit 4b643c8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
30 changes: 30 additions & 0 deletions pdal/PDALUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
#include <pdal/Options.hpp>
#include <pdal/util/FileUtils.hpp>

#ifndef _WIN32
#include <dlfcn.h>
#endif

using namespace std;

namespace pdal
Expand Down Expand Up @@ -401,5 +405,31 @@ double computeHausdorff(PointViewPtr srcView, PointViewPtr candView)
return (std::max)(maxDistSrcToCand, maxDistCandToSrc);
}


std::string dllDir()
{
std::string s;

#ifdef WIN32
HMODULE hm = NULL;

if (GetModuleHandleEx(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)&dllDir, &hm))
{
wchar_t path[MAX_PATH];
DWORD cnt = GetModuleFileName(hm, path, sizeof(path));
if (cnt > 0 && cnt < MAX_PATH)
s = path;
}
#else
Dl_info info;
if (dladdr((const void *)dllDir, &info))
s = info.dli_fname;
#endif
return FileUtils::getDirectory(s);
}

} // namespace Utils
} // namespace pdal
1 change: 1 addition & 0 deletions pdal/PDALUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ inline void writeProgress(int fd, const std::string& type,
#endif
}

std::string dllDir();
std::string PDAL_DLL toJSON(const MetadataNode& m);
void PDAL_DLL toJSON(const MetadataNode& m, std::ostream& o);
std::istream PDAL_DLL *openFile(const std::string& path, bool asBinary = true);
Expand Down
15 changes: 10 additions & 5 deletions pdal/PluginDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
****************************************************************************/

#include <pdal/PluginDirectory.hpp>
#include <pdal/util/Algorithm.hpp>
#include <pdal/util/FileUtils.hpp>
#include <pdal/pdal_config.hpp>

Expand All @@ -53,14 +54,18 @@ StringList pluginSearchPaths()
searchPaths = Utils::split2(envOverride, Utils::pathListSeparator);
else
{
StringList standardPaths { ".", "./lib", "../lib", "./bin", "../bin" };
for (std::string& s : standardPaths)
StringList possiblePaths { ".", "./lib", "../lib", "./bin", "../bin", Utils::dllDir(),
Config::pluginInstallPath() };

for (std::string s : possiblePaths)
{
if (FileUtils::toAbsolutePath(s) !=
FileUtils::toAbsolutePath(Config::pluginInstallPath()))
s = FileUtils::toCanonicalPath(s);
if (s.size() && !Utils::contains(searchPaths, s))
{
searchPaths.push_back(s);
std::cerr << "Search path = " << s << "!\n";
}
}
searchPaths.push_back(Config::pluginInstallPath());
}
return searchPaths;
}
Expand Down
6 changes: 2 additions & 4 deletions pdal/util/FileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,7 @@ std::string getcwd()
}


/***
// Non-boost alternative. Requires file existence.
std::string toAbsolutePath(const std::string& filename)
std::string toCanonicalPath(const std::string& filename)
{
std::string result;

Expand All @@ -299,7 +297,7 @@ std::string toAbsolutePath(const std::string& filename)
#endif
return result;
}
***/


// if the filename is an absolute path, just return it
// otherwise, make it absolute (relative to current working dir) and return that
Expand Down
10 changes: 10 additions & 0 deletions pdal/util/FileUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ namespace FileUtils
*/
PDAL_DLL bool isDirectory(const std::string& path);

/**
Return the path with all ".", ".." and symbolic links removed.
The file must exist.
\param filename Name of file to convert to canonical path.
\return Canonical version of provided filename, or empty string.
*/
PDAL_DLL std::string toCanonicalPath(const std::string& filename);


/**
If the filename is an absolute path, just return it otherwise,
make it absolute (relative to current working dir) and return it.
Expand Down

0 comments on commit 4b643c8

Please sign in to comment.