Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "hal/Extensions.h" |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 7 | #include <cstdio> |
| 8 | #include <string_view> |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 11 | #include <fmt/format.h> |
| 12 | #include <wpi/SmallVector.h> |
| 13 | #include <wpi/StringExtras.h> |
| 14 | #include <wpi/fs.h> |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 15 | #include <wpi/spinlock.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 16 | |
| 17 | #if defined(WIN32) || defined(_WIN32) |
| 18 | #include <windows.h> |
| 19 | #else |
| 20 | #include <dlfcn.h> |
| 21 | #endif |
| 22 | |
| 23 | #if defined(WIN32) || defined(_WIN32) |
| 24 | #define DELIM ';' |
| 25 | #define HTYPE HMODULE |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 26 | #define DLOPEN(a) LoadLibraryA(a) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 27 | #define DLSYM GetProcAddress |
| 28 | #define DLCLOSE FreeLibrary |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 29 | #define DLERROR fmt::format("error #{}", GetLastError()) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 30 | #else |
| 31 | #define DELIM ':' |
| 32 | #define HTYPE void* |
| 33 | #define PREFIX "lib" |
| 34 | #define DLOPEN(a) dlopen(a, RTLD_LAZY) |
| 35 | #define DLSYM dlsym |
| 36 | #define DLCLOSE dlclose |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 37 | #define DLERROR dlerror() |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 38 | #endif |
| 39 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 40 | static wpi::recursive_spinlock gExtensionRegistryMutex; |
| 41 | static std::vector<std::pair<const char*, void*>> gExtensionRegistry; |
| 42 | static std::vector<std::pair<void*, void (*)(void*, const char*, void*)>> |
| 43 | gExtensionListeners; |
| 44 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 45 | namespace hal::init { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 46 | void InitializeExtensions() {} |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 47 | } // namespace hal::init |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 48 | |
| 49 | static bool& GetShowNotFoundMessage() { |
| 50 | static bool showMsg = true; |
| 51 | return showMsg; |
| 52 | } |
| 53 | |
| 54 | extern "C" { |
| 55 | |
| 56 | int HAL_LoadOneExtension(const char* library) { |
| 57 | int rc = 1; // It is expected and reasonable not to find an extra simulation |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 58 | fmt::print("HAL Extensions: Attempting to load: {}\n", |
| 59 | fs::path{library}.stem().string()); |
| 60 | std::fflush(stdout); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 61 | HTYPE handle = DLOPEN(library); |
| 62 | #if !defined(WIN32) && !defined(_WIN32) |
| 63 | if (!handle) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 64 | #if defined(__APPLE__) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 65 | auto libraryName = fmt::format("lib{}.dylib", library); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 66 | #else |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 67 | auto libraryName = fmt::format("lib{}.so", library); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 68 | #endif |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 69 | fmt::print("HAL Extensions: Load failed: {}\nTrying modified name: {}\n", |
| 70 | DLERROR, fs::path{libraryName}.stem().string()); |
| 71 | std::fflush(stdout); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 72 | handle = DLOPEN(libraryName.c_str()); |
| 73 | } |
| 74 | #endif |
| 75 | if (!handle) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 76 | fmt::print("HAL Extensions: Failed to load library: {}\n", DLERROR); |
| 77 | std::fflush(stdout); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 78 | return rc; |
| 79 | } |
| 80 | |
| 81 | auto init = reinterpret_cast<halsim_extension_init_func_t*>( |
| 82 | DLSYM(handle, "HALSIM_InitExtension")); |
| 83 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 84 | if (init) { |
| 85 | rc = (*init)(); |
| 86 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 87 | |
| 88 | if (rc != 0) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 89 | std::puts("HAL Extensions: Failed to load extension"); |
| 90 | std::fflush(stdout); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 91 | DLCLOSE(handle); |
| 92 | } else { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 93 | std::puts("HAL Extensions: Successfully loaded extension"); |
| 94 | std::fflush(stdout); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 95 | } |
| 96 | return rc; |
| 97 | } |
| 98 | |
| 99 | int HAL_LoadExtensions(void) { |
| 100 | int rc = 1; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 101 | wpi::SmallVector<std::string_view, 2> libraries; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 102 | const char* e = std::getenv("HALSIM_EXTENSIONS"); |
| 103 | if (!e) { |
| 104 | if (GetShowNotFoundMessage()) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 105 | std::puts("HAL Extensions: No extensions found"); |
| 106 | std::fflush(stdout); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 107 | } |
| 108 | return rc; |
| 109 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 110 | wpi::split(e, libraries, DELIM, -1, false); |
| 111 | for (auto& library : libraries) { |
| 112 | rc = HAL_LoadOneExtension(std::string(library).c_str()); |
| 113 | if (rc < 0) { |
| 114 | break; |
| 115 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 116 | } |
| 117 | return rc; |
| 118 | } |
| 119 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 120 | void HAL_RegisterExtension(const char* name, void* data) { |
| 121 | std::scoped_lock lock(gExtensionRegistryMutex); |
| 122 | gExtensionRegistry.emplace_back(name, data); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 123 | for (auto&& listener : gExtensionListeners) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 124 | listener.second(listener.first, name, data); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 125 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void HAL_RegisterExtensionListener(void* param, |
| 129 | void (*func)(void*, const char* name, |
| 130 | void* data)) { |
| 131 | std::scoped_lock lock(gExtensionRegistryMutex); |
| 132 | gExtensionListeners.emplace_back(param, func); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 133 | for (auto&& extension : gExtensionRegistry) { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 134 | func(param, extension.first, extension.second); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 135 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 138 | void HAL_SetShowExtensionsNotFoundMessages(HAL_Bool showMessage) { |
| 139 | GetShowNotFoundMessage() = showMessage; |
| 140 | } |
| 141 | |
| 142 | } // extern "C" |