From aee7ef0f132aac810c787df62ec6e8a6182c95e5 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Mon, 16 Jul 2018 11:21:09 -0500 Subject: [PATCH 1/3] Move LoadPlugin contents to cpp. No change other than this, but this should improve debug experience. --- src/libfunctionality/CMakeLists.txt | 6 +++--- .../{LoadPlugin.cpp => LoadPluginInternal.h} | 18 +++++------------- .../{LoadPluginLibdl.h => LoadPluginLibdl.cpp} | 9 +++------ .../{LoadPluginWin32.h => LoadPluginWin32.cpp} | 9 +++------ 4 files changed, 14 insertions(+), 28 deletions(-) rename src/libfunctionality/{LoadPlugin.cpp => LoadPluginInternal.h} (81%) rename src/libfunctionality/{LoadPluginLibdl.h => LoadPluginLibdl.cpp} (92%) rename src/libfunctionality/{LoadPluginWin32.h => LoadPluginWin32.cpp} (86%) diff --git a/src/libfunctionality/CMakeLists.txt b/src/libfunctionality/CMakeLists.txt index e75270b..e84f252 100644 --- a/src/libfunctionality/CMakeLists.txt +++ b/src/libfunctionality/CMakeLists.txt @@ -46,20 +46,20 @@ set(API set(SOURCE DynamicLoadConfig.h.in - LoadPlugin.cpp + LoadPluginInternal.h PluginHandle.cpp) if(LIBFUNC_DL_WIN32) list(APPEND SOURCE LibraryHandleWin32.cpp LibraryHandleWin32.h - LoadPluginWin32.h) + LoadPluginWin32.cpp) endif() if(LIBFUNC_DL_LIBDL) list(APPEND SOURCE LibraryHandleLibdl.cpp LibraryHandleLibdl.h - LoadPluginLibdl.h) + LoadPluginLibdl.cpp) endif() ### diff --git a/src/libfunctionality/LoadPlugin.cpp b/src/libfunctionality/LoadPluginInternal.h similarity index 81% rename from src/libfunctionality/LoadPlugin.cpp rename to src/libfunctionality/LoadPluginInternal.h index 2fa52bc..a18517c 100644 --- a/src/libfunctionality/LoadPlugin.cpp +++ b/src/libfunctionality/LoadPluginInternal.h @@ -1,5 +1,5 @@ /** @file - @brief Implementation + @brief Internal header @date 2014 @@ -24,6 +24,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +#ifndef INCLUDED_LoadPluginInternal_h_GUID_AEC00CE9_5EC3_4D93_6BFF_ABB175D5E66E +#define INCLUDED_LoadPluginInternal_h_GUID_AEC00CE9_5EC3_4D93_6BFF_ABB175D5E66E + // Internal Includes #include #include @@ -47,15 +50,4 @@ typedef libfunc_ep_return_t (*entry_point_t)(void *); /// @} } // end of namespace libfunc -// PLATFORM_SPECIFIC CODE -#ifdef LIBFUNC_DL_SUPPORT - -#ifdef LIBFUNC_DL_WIN32 -#include "LoadPluginWin32.h" -#endif - -#ifdef LIBFUNC_DL_LIBDL -#include "LoadPluginLibdl.h" -#endif - -#endif +#endif // INCLUDED_LoadPluginInternal_h_GUID_AEC00CE9_5EC3_4D93_6BFF_ABB175D5E66E diff --git a/src/libfunctionality/LoadPluginLibdl.h b/src/libfunctionality/LoadPluginLibdl.cpp similarity index 92% rename from src/libfunctionality/LoadPluginLibdl.h rename to src/libfunctionality/LoadPluginLibdl.cpp index c6e2be4..3ffd782 100644 --- a/src/libfunctionality/LoadPluginLibdl.h +++ b/src/libfunctionality/LoadPluginLibdl.cpp @@ -1,5 +1,5 @@ /** @file - @brief Header + @brief Implementation using libdl (linux, etc.) @date 2014 @@ -24,11 +24,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef INCLUDED_LoadPluginLibdl_h_GUID_AEC00CE9_5EC3_4D93_6BFF_ABB175D5E66E -#define INCLUDED_LoadPluginLibdl_h_GUID_AEC00CE9_5EC3_4D93_6BFF_ABB175D5E66E - // Internal Includes #include "LibraryHandleLibdl.h" +#include "LoadPluginInternal.h" // Library/third-party includes // - none @@ -113,5 +111,4 @@ PluginHandle loadPluginByName(std::string const &n, void *opaque) { return PluginHandle(lib); } -} // end of namespace libfunc -#endif // INCLUDED_LoadPluginLibdl_h_GUID_AEC00CE9_5EC3_4D93_6BFF_ABB175D5E66E +} // end of namespace libfunc \ No newline at end of file diff --git a/src/libfunctionality/LoadPluginWin32.h b/src/libfunctionality/LoadPluginWin32.cpp similarity index 86% rename from src/libfunctionality/LoadPluginWin32.h rename to src/libfunctionality/LoadPluginWin32.cpp index 3787c03..124e877 100644 --- a/src/libfunctionality/LoadPluginWin32.h +++ b/src/libfunctionality/LoadPluginWin32.cpp @@ -1,5 +1,5 @@ /** @file - @brief Header + @brief Implementation for win32 @date 2014 @@ -24,11 +24,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef INCLUDED_LoadPluginWin32_h_GUID_F3039817_F263_43B5_B117_AD894AAF1A83 -#define INCLUDED_LoadPluginWin32_h_GUID_F3039817_F263_43B5_B117_AD894AAF1A83 - // Internal Includes #include "LibraryHandleWin32.h" +#include "LoadPluginInternal.h" // Library/third-party includes // - none @@ -75,5 +73,4 @@ PluginHandle loadPluginByName(std::string const &n, void *opaque) { return PluginHandle(lib); } -} // end of namespace libfunc -#endif // INCLUDED_LoadPluginWin32_h_GUID_F3039817_F263_43B5_B117_AD894AAF1A83 +} // end of namespace libfunc \ No newline at end of file From 1facd651942f1996941e049821bba5bed8709f20 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Mon, 16 Jul 2018 11:31:01 -0500 Subject: [PATCH 2/3] Guard implementations with ifdef so all platforms use same files. --- src/libfunctionality/CMakeLists.txt | 26 +++++++++++---------- src/libfunctionality/LibraryHandleLibdl.cpp | 5 +++- src/libfunctionality/LibraryHandleLibdl.h | 3 +++ src/libfunctionality/LibraryHandleWin32.cpp | 3 +++ src/libfunctionality/LibraryHandleWin32.h | 4 +++- src/libfunctionality/LoadPluginLibdl.cpp | 6 ++++- src/libfunctionality/LoadPluginWin32.cpp | 5 +++- 7 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/libfunctionality/CMakeLists.txt b/src/libfunctionality/CMakeLists.txt index e84f252..dd72068 100644 --- a/src/libfunctionality/CMakeLists.txt +++ b/src/libfunctionality/CMakeLists.txt @@ -49,18 +49,20 @@ set(SOURCE LoadPluginInternal.h PluginHandle.cpp) -if(LIBFUNC_DL_WIN32) - list(APPEND SOURCE - LibraryHandleWin32.cpp - LibraryHandleWin32.h - LoadPluginWin32.cpp) -endif() -if(LIBFUNC_DL_LIBDL) - list(APPEND SOURCE - LibraryHandleLibdl.cpp - LibraryHandleLibdl.h - LoadPluginLibdl.cpp) -endif() + +### Implementations - guarded with ifdefs + +# Win32 +list(APPEND SOURCE + LibraryHandleWin32.cpp + LibraryHandleWin32.h + LoadPluginWin32.cpp) + +# libdl +list(APPEND SOURCE + LibraryHandleLibdl.cpp + LibraryHandleLibdl.h + LoadPluginLibdl.cpp) ### # "Interface" target for plugins to use - they don't need to link, just need the includes. diff --git a/src/libfunctionality/LibraryHandleLibdl.cpp b/src/libfunctionality/LibraryHandleLibdl.cpp index c0b449c..aef0eab 100644 --- a/src/libfunctionality/LibraryHandleLibdl.cpp +++ b/src/libfunctionality/LibraryHandleLibdl.cpp @@ -25,8 +25,9 @@ // limitations under the License. // Internal Includes -#include "LibraryHandleLibdl.h" #include +#ifdef LIBFUNC_DL_LIBDL +#include "LibraryHandleLibdl.h" #include // Library/third-party includes @@ -53,3 +54,5 @@ LibraryHandle RAIILoadLibrary(std::string const &name) { } } // end of namespace libfunc + +#endif // LIBFUNC_DL_LIBDL \ No newline at end of file diff --git a/src/libfunctionality/LibraryHandleLibdl.h b/src/libfunctionality/LibraryHandleLibdl.h index 6b0ab14..5084399 100644 --- a/src/libfunctionality/LibraryHandleLibdl.h +++ b/src/libfunctionality/LibraryHandleLibdl.h @@ -28,6 +28,8 @@ #define INCLUDED_LibraryHandleLibdl_h_GUID_BA547FB8_A564_4B94_F2AD_58BA772462E4 // Internal Includes +#include +#ifdef LIBFUNC_DL_LIBDL #include // Library/third-party includes @@ -40,4 +42,5 @@ namespace libfunc { LibraryHandle RAIILoadLibrary(std::string const &name); } // end of namespace libfunc +#endif // LIBFUNC_DL_LIBDL #endif // INCLUDED_LibraryHandleLibdl_h_GUID_BA547FB8_A564_4B94_F2AD_58BA772462E4 diff --git a/src/libfunctionality/LibraryHandleWin32.cpp b/src/libfunctionality/LibraryHandleWin32.cpp index e2a041a..74e0fa1 100644 --- a/src/libfunctionality/LibraryHandleWin32.cpp +++ b/src/libfunctionality/LibraryHandleWin32.cpp @@ -25,6 +25,8 @@ // limitations under the License. // Internal Includes +#include +#ifdef LIBFUNC_DL_WIN32 #include "LibraryHandleWin32.h" #include @@ -82,3 +84,4 @@ LibraryHandle RAIILoadLibrary(std::string const &name) { } } // end of namespace libfunc +#endif // LIBFUNC_DL_WIN32 \ No newline at end of file diff --git a/src/libfunctionality/LibraryHandleWin32.h b/src/libfunctionality/LibraryHandleWin32.h index e6d126a..0b67038 100644 --- a/src/libfunctionality/LibraryHandleWin32.h +++ b/src/libfunctionality/LibraryHandleWin32.h @@ -28,6 +28,8 @@ #define INCLUDED_LibraryHandleWin32_h_GUID_767832A3_0B91_43A0_B49F_3818CB69C3A7 // Internal Includes +#include +#ifdef LIBFUNC_DL_WIN32 #include // Library/third-party includes @@ -47,5 +49,5 @@ inline HMODULE GetHMODULE(LibraryHandle const &h) { } } // end of namespace libfunc - +#endif // LIBFUNC_DL_WIN32 #endif // INCLUDED_LibraryHandleWin32_h_GUID_767832A3_0B91_43A0_B49F_3818CB69C3A7 diff --git a/src/libfunctionality/LoadPluginLibdl.cpp b/src/libfunctionality/LoadPluginLibdl.cpp index 3ffd782..da32dd6 100644 --- a/src/libfunctionality/LoadPluginLibdl.cpp +++ b/src/libfunctionality/LoadPluginLibdl.cpp @@ -25,6 +25,8 @@ // limitations under the License. // Internal Includes +#include +#ifdef LIBFUNC_DL_LIBDL #include "LibraryHandleLibdl.h" #include "LoadPluginInternal.h" @@ -111,4 +113,6 @@ PluginHandle loadPluginByName(std::string const &n, void *opaque) { return PluginHandle(lib); } -} // end of namespace libfunc \ No newline at end of file +} // end of namespace libfunc + +#endif // LIBFUNC_DL_LIBDL \ No newline at end of file diff --git a/src/libfunctionality/LoadPluginWin32.cpp b/src/libfunctionality/LoadPluginWin32.cpp index 124e877..3636a9a 100644 --- a/src/libfunctionality/LoadPluginWin32.cpp +++ b/src/libfunctionality/LoadPluginWin32.cpp @@ -25,6 +25,8 @@ // limitations under the License. // Internal Includes +#include +#ifdef LIBFUNC_DL_WIN32 #include "LibraryHandleWin32.h" #include "LoadPluginInternal.h" @@ -73,4 +75,5 @@ PluginHandle loadPluginByName(std::string const &n, void *opaque) { return PluginHandle(lib); } -} // end of namespace libfunc \ No newline at end of file +} // end of namespace libfunc +#endif // LIBFUNC_DL_WIN32 \ No newline at end of file From f9a766f7322f324e222b8fefbe5f3b5778f74443 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Mon, 16 Jul 2018 13:22:51 -0500 Subject: [PATCH 3/3] Add binary location files. Based on the files of similar names in osvr::util. Goal is to use when searching for plugin, but not yet used. --- src/libfunctionality/BinaryLocation.cpp | 114 ++++++++++++++++++ src/libfunctionality/BinaryLocation.h | 43 +++++++ src/libfunctionality/CMakeLists.txt | 3 + src/libfunctionality/PlatformDefinitions.h | 128 +++++++++++++++++++++ 4 files changed, 288 insertions(+) create mode 100644 src/libfunctionality/BinaryLocation.cpp create mode 100644 src/libfunctionality/BinaryLocation.h create mode 100644 src/libfunctionality/PlatformDefinitions.h diff --git a/src/libfunctionality/BinaryLocation.cpp b/src/libfunctionality/BinaryLocation.cpp new file mode 100644 index 0000000..5fbfa23 --- /dev/null +++ b/src/libfunctionality/BinaryLocation.cpp @@ -0,0 +1,114 @@ +/** @file + @brief Implementation + + @date 2015 + + @author + Sensics, Inc. + +*/ + +// Copyright 2015 Sensics, Inc. +// Copyright 2018 Collabora +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Internal Includes +#include "BinaryLocation.h" +#include "PlatformDefinitions.h" + +// Library/third-party includes +#ifdef LIBFUNC_WINDOWS +#define WIN32_LEAN_AND_MEAN +#include +#endif // LIBFUNC_WINDOWS + +#ifdef LIBFUNC_MACOSX +#include +#endif // LIBFUNC_MACOSX + +#ifndef LIBFUNC_WINDOWS +#define LIBFUNC_HAVE_REALPATH +#endif // !LIBFUNC_WINDOWS + +#ifdef LIBFUNC_HAVE_REALPATH +#include +#include +#endif + +// Standard includes +// - none + +namespace libfunc { +#if 0 + namespace fs = boost::filesystem; + + static inline std::string getCanonical(const char path[]) { + return fs::canonical(path).generic_string(); + } +#endif +#ifdef LIBFUNC_HAVE_REALPATH + static inline std::string getCanonical(const char path[]) { + char * temp = realpath(path, NULL); + std::string ret; + if (temp) { + ret = std::string(temp); + free(temp); + } + return ret; + } +#endif + +#if defined(LIBFUNC_WINDOWS) + std::string getBinaryLocation() { + char buf[512] = {0}; + DWORD len = GetModuleFileNameA(nullptr, buf, sizeof(buf)); + std::string ret; + if (0 != len) { + ret.assign(buf, len); + } + return ret; + } +#elif defined(LIBFUNC_LINUX) || defined(LIBFUNC_ANDROID) + std::string getBinaryLocation() { + return getCanonical("/proc/self/exe"); + } +#elif defined(LIBFUNC_NETBSD) + std::string getBinaryLocation() { + return getCanonical("/proc/curproc/exe"); + } +#elif defined(LIBFUNC_FREEBSD) + std::string getBinaryLocation() { + if (fs::exists("proc/curproc/file")) { + return getCanonical("/proc/curproc/file"); + } + return std::string{}; + /// @todo sysctl CTL_KERN KERN_PROC KERN_PROC_PATHNAME -1 + } +#elif defined(LIBFUNC_MACOSX) + std::string getBinaryLocation() { + char buf[1024] = {0}; + uint32_t len = sizeof(buf); + if (0 == _NSGetExecutablePath(buf, &len)) { + return getCanonical(buf); + } + return std::string{}; + } +#else +#error "getBinaryLocation() not yet implemented for this platform!" + std::string getBinaryLocation() { return std::string{}; } + +/// @todo Solaris: getexecname() +#endif + +} // namespace libfunc diff --git a/src/libfunctionality/BinaryLocation.h b/src/libfunctionality/BinaryLocation.h new file mode 100644 index 0000000..590b79d --- /dev/null +++ b/src/libfunctionality/BinaryLocation.h @@ -0,0 +1,43 @@ +/** @file + @brief Header + + @date 2015 + + @author + Sensics, Inc. + +*/ + +// Copyright 2015 Sensics, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef INCLUDED_BinaryLocation_h_GUID_B0594B7B_B7B8_489B_DF0D_5B3D1998D3AD +#define INCLUDED_BinaryLocation_h_GUID_B0594B7B_B7B8_489B_DF0D_5B3D1998D3AD + +// Internal Includes +// - none + +// Library/third-party includes +// - none + +// Standard includes +#include + +namespace libfunc { + /// Get this binary's location, often as a step in finding the "root" and + /// search paths. + std::string getBinaryLocation(); +} // namespace libfunc + +#endif // INCLUDED_BinaryLocation_h_GUID_B0594B7B_B7B8_489B_DF0D_5B3D1998D3AD diff --git a/src/libfunctionality/CMakeLists.txt b/src/libfunctionality/CMakeLists.txt index dd72068..41e2fd5 100644 --- a/src/libfunctionality/CMakeLists.txt +++ b/src/libfunctionality/CMakeLists.txt @@ -45,8 +45,11 @@ set(API "${HEADER_LOCATION}/SharedPtr.h") set(SOURCE + BinaryLocation.cpp + BinaryLocation.h DynamicLoadConfig.h.in LoadPluginInternal.h + PlatformDefinitions.h PluginHandle.cpp) diff --git a/src/libfunctionality/PlatformDefinitions.h b/src/libfunctionality/PlatformDefinitions.h new file mode 100644 index 0000000..71a6183 --- /dev/null +++ b/src/libfunctionality/PlatformDefinitions.h @@ -0,0 +1,128 @@ +/** @file + @brief Auto-configured header + + If this filename ends in `.h`, don't edit it: your edits will + be lost next time this file is regenerated! + + Must be c-safe! + + @date 2014 + + @author + Sensics, Inc. + +*/ + +/* +// Copyright 2014 Sensics, Inc. +// Copyright 2018 Collabora +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +*/ + +#ifndef INCLUDED_PlatformConfig_h_GUID_0D10E644_8114_4294_A839_699F39E1F0E1 +#define INCLUDED_PlatformConfig_h_GUID_0D10E644_8114_4294_A839_699F39E1F0E1 + +/** + * \name Operating system tests. + * + * Prefer testing for specific compiler or platform features instead of relying + * on these variables. + * + * Multiple OS defines may be enabled at the same time. + */ +//@{ +#if defined(_AIX) +#define LIBFUNC_AIX 1 +#endif + +#if defined(__ANDROID__) +#define LIBFUNC_ANDROID 1 +#endif + +#if defined(__bsdi__) +#define LIBFUNC_BSDOS 1 +#endif + +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(BSD) +#define LIBFUNC_FREEBSD 1 +#endif + +#if defined(hpux) || defined(_hpux) || defined(__hpux) +#define LIBFUNC_HPUX 1 +#endif + +#if defined(sgi) || defined(__sgi) +#define LIBFUNC_IRIX 1 +#endif + +#if defined(linux) || defined(__linux__) +#define LIBFUNC_LINUX 1 +#endif + +#if defined(__FreeBSD_kernel__) && defined(__GLIBC__) +#define LIBFUNC_KFREEBSD 1 +#endif + +#if defined(__NetBSD__) +#define LIBFUNC_NETBSD 1 +#endif + +#if defined(__OpenBSD__) +#define LIBFUNC_OPENBSD 1 +#endif + +#if defined(sco) && defined(_UNIXWARE7) +#define LIBFUNC_UNIXWARE 1 +#endif + +#if defined(sun) || defined(__sun) +#if defined(__SVR4) || defined(__svr4__) +#define LIBFUNC_SOLARIS 1 +# else +#define LIBFUNC_SUNOS 1 +#endif +#endif + +#if defined(__osf) || defined(__osf__) +#define LIBFUNC_TRU64 1 +#endif + +#if defined(ultrix) || defined(__ultrix) || defined(__ultrix__) || (defined(unix) && defined(vax)) +#define LIBFUNC_ULTRIX 1 +#endif + +#if defined(__CYGWIN__) +#define LIBFUNC_CYGWIN 1 +#endif + +#if (defined(__APPLE__) && defined(__MACH__)) +#define LIBFUNC_MACOSX 1 +#endif + +#if defined(_WIN32) || defined(WIN32) || defined(__NT__) || defined(__WIN32__) +#define LIBFUNC_WINDOWS 1 +#if defined(_WIN64) +#define LIBFUNC_WIN64 1 +#else +#define LIBFUNC_WIN32 1 +#endif // _WIN64 +#endif // LIBFUNC_WINDOWS + +#if defined(_XBOX) +#define LIBFUNC_XBOX 1 +#endif +//@} + +#endif +