Skip to content

Commit

Permalink
One-pass plugin directories.
Browse files Browse the repository at this point in the history
  • Loading branch information
abellgithub committed Mar 2, 2018
1 parent 63bcead commit e9022a8
Show file tree
Hide file tree
Showing 9 changed files with 439 additions and 341 deletions.
27 changes: 13 additions & 14 deletions apps/pdal.cpp
Expand Up @@ -69,7 +69,7 @@ class App
void outputVersion();
void outputHelp(const ProgramArgs& args);
void outputDrivers();
void outputCommands();
void outputCommands(const std::string& leader);
void outputOptions();
void outputOptions(const std::string& stageName,std::ostream& strm);
void addArgs(ProgramArgs& args);
Expand Down Expand Up @@ -110,20 +110,20 @@ void App::outputHelp(const ProgramArgs& args)

m_out << "The following commands are available:" << std::endl;

StageFactory f(false);
StringList loaded_kernels = PluginManager<Kernel>::names();

for (auto name : loaded_kernels)
m_out << " - " << name << std::endl;
// Load all kernels so that we can report the names.
StageFactory f;
PluginManager<Kernel>::loadAll();
outputCommands(" -");
m_out << std::endl;
m_out << "See http://pdal.io/apps/ for more detail" << std::endl;
}


void App::outputDrivers()
{
// Force plugin loading.
StageFactory f(false);

StageFactory f;
PluginManager<Stage>::loadAll();
StringList stages = PluginManager<Stage>::names();

if (!m_showJSON)
Expand Down Expand Up @@ -178,13 +178,12 @@ void App::outputDrivers()
}


void App::outputCommands()
void App::outputCommands(const std::string& leader)
{
StageFactory f(false);
std::vector<std::string> loaded_kernels;
loaded_kernels = PluginManager<Kernel>::names();
for (auto name : loaded_kernels)
m_out << name << std::endl;
StageFactory f;
PluginManager<Kernel>::loadAll();
for (auto name : PluginManager<Kernel>::names())
m_out << leader << name << std::endl;
}


Expand Down
138 changes: 138 additions & 0 deletions pdal/PluginDirectory.cpp
@@ -0,0 +1,138 @@
/******************************************************************************
* Copyright (c) 2018, Hobu Inc. (info@hobu.co)
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/

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

namespace pdal
{

namespace
{

#if defined(__APPLE__) && defined(__MACH__)
const std::string dynamicLibraryExtension("dylib");
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__) || defined(__GNU__)
const std::string dynamicLibraryExtension("so");
#elif defined _WIN32
const std::string dynamicLibraryExtension("dll");
#endif

StringList pluginSearchPaths()
{
StringList searchPaths;
std::string envOverride;

Utils::getenv("PDAL_DRIVER_PATH", envOverride);

if (!envOverride.empty())
searchPaths = Utils::split2(envOverride, ':');
else
{
StringList standardPaths { ".", "./lib", "../lib", "./bin", "../bin" };
for (std::string& s : standardPaths)
{
if (FileUtils::toAbsolutePath(s) !=
FileUtils::toAbsolutePath(Config::pluginInstallPath()))
searchPaths.push_back(s);
}
searchPaths.push_back(Config::pluginInstallPath());
}
return searchPaths;
}

// libpdal_plugin_{stagetype}_{name}.{extension}
// For example, libpdal_plugin_writer_text.so or
// libpdal_plugin_filter_color.dylib
std::string validPlugin(const std::string& path, const StringList& types)
{
auto typeValid = [&types](std::string& type)
{
for (auto t: types)
if (type == t)
return true;
return false;
};

std::string file = FileUtils::getFilename(path);
StringList parts = Utils::split(file, '_');
if (parts.size() != 4 || parts[0] != "libpdal" || parts[1] != "plugin")
return std::string();
StringList subparts = Utils::split(parts[3], '.');

if (subparts.size() != 2 || subparts[1] != dynamicLibraryExtension)
return std::string();

std::string type = parts[2];
std::string plugin = subparts[0];

if (!typeValid(type))
return std::string();
return type + "s." + plugin;
}

} // unnamed namespace;


PluginDirectory::PluginDirectory()
{
for (const auto& dir : pluginSearchPaths())
{
StringList files = FileUtils::directoryList(dir);
for (auto& file : files)
{
file = FileUtils::toAbsolutePath(file);

std::string plugin;
plugin = validPlugin(file, {"kernel"});
if (plugin.size())
{
m_kernels.insert(std::make_pair(plugin, file));
continue;
}
plugin = validPlugin(file, {"reader", "writer", "filter"});
if (plugin.size())
m_drivers.insert(std::make_pair(plugin, file));
}
}
}

StringList PluginDirectory::test_pluginSearchPaths()
{
return pluginSearchPaths();
}

} // namespace pdal

64 changes: 64 additions & 0 deletions pdal/PluginDirectory.hpp
@@ -0,0 +1,64 @@
/******************************************************************************
* Copyright (c) 2018, Hobu Inc. (info@hobu.co)
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/

#pragma once

#include <pdal/Log.hpp>
#include <pdal/Stage.hpp>
#include <pdal/Kernel.hpp>

#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>

namespace pdal
{

class PluginDirectory
{
FRIEND_TEST(PluginManagerTest, SearchPaths);
public:
PluginDirectory();

std::map<std::string, std::string> m_kernels;
std::map<std::string, std::string> m_drivers;

private:
static StringList test_pluginSearchPaths();
};

} // namespace pdal

0 comments on commit e9022a8

Please sign in to comment.