Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 2 | /* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "hal/Extensions.h" |
| 9 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 10 | #include <wpi/Path.h> |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 11 | #include <wpi/SmallString.h> |
| 12 | #include <wpi/StringRef.h> |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 13 | #include <wpi/raw_ostream.h> |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 14 | |
| 15 | #include "hal/HAL.h" |
| 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 |
| 26 | #define DLOPEN(a) LoadLibrary(a) |
| 27 | #define DLSYM GetProcAddress |
| 28 | #define DLCLOSE FreeLibrary |
| 29 | #else |
| 30 | #define DELIM ':' |
| 31 | #define HTYPE void* |
| 32 | #define PREFIX "lib" |
| 33 | #define DLOPEN(a) dlopen(a, RTLD_LAZY) |
| 34 | #define DLSYM dlsym |
| 35 | #define DLCLOSE dlclose |
| 36 | #endif |
| 37 | |
| 38 | namespace hal { |
| 39 | namespace init { |
| 40 | void InitializeExtensions() {} |
| 41 | } // namespace init |
| 42 | } // namespace hal |
| 43 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 44 | static bool& GetShowNotFoundMessage() { |
| 45 | static bool showMsg = true; |
| 46 | return showMsg; |
| 47 | } |
| 48 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 49 | extern "C" { |
| 50 | |
| 51 | int HAL_LoadOneExtension(const char* library) { |
| 52 | int rc = 1; // It is expected and reasonable not to find an extra simulation |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 53 | wpi::outs() << "HAL Extensions: Attempting to load: " |
| 54 | << wpi::sys::path::stem(library) << "\n"; |
| 55 | wpi::outs().flush(); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 56 | HTYPE handle = DLOPEN(library); |
| 57 | #if !defined(WIN32) && !defined(_WIN32) |
| 58 | if (!handle) { |
| 59 | wpi::SmallString<128> libraryName("lib"); |
| 60 | libraryName += library; |
| 61 | #if defined(__APPLE__) |
| 62 | libraryName += ".dylib"; |
| 63 | #else |
| 64 | libraryName += ".so"; |
| 65 | #endif |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 66 | wpi::outs() << "HAL Extensions: Trying modified name: " |
| 67 | << wpi::sys::path::stem(libraryName); |
| 68 | wpi::outs().flush(); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 69 | handle = DLOPEN(libraryName.c_str()); |
| 70 | } |
| 71 | #endif |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 72 | if (!handle) { |
| 73 | wpi::outs() << "HAL Extensions: Failed to load library\n"; |
| 74 | wpi::outs().flush(); |
| 75 | return rc; |
| 76 | } |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 77 | |
| 78 | auto init = reinterpret_cast<halsim_extension_init_func_t*>( |
| 79 | DLSYM(handle, "HALSIM_InitExtension")); |
| 80 | |
| 81 | if (init) rc = (*init)(); |
| 82 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 83 | if (rc != 0) { |
| 84 | wpi::outs() << "HAL Extensions: Failed to load extension\n"; |
| 85 | wpi::outs().flush(); |
| 86 | DLCLOSE(handle); |
| 87 | } else { |
| 88 | wpi::outs() << "HAL Extensions: Successfully loaded extension\n"; |
| 89 | wpi::outs().flush(); |
| 90 | } |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 91 | return rc; |
| 92 | } |
| 93 | |
| 94 | int HAL_LoadExtensions(void) { |
| 95 | int rc = 1; |
| 96 | wpi::SmallVector<wpi::StringRef, 2> libraries; |
| 97 | const char* e = std::getenv("HALSIM_EXTENSIONS"); |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 98 | if (!e) { |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 99 | if (GetShowNotFoundMessage()) { |
| 100 | wpi::outs() << "HAL Extensions: No extensions found\n"; |
| 101 | wpi::outs().flush(); |
| 102 | } |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame] | 103 | return rc; |
| 104 | } |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 105 | wpi::StringRef env{e}; |
| 106 | env.split(libraries, DELIM, -1, false); |
| 107 | for (auto& libref : libraries) { |
| 108 | wpi::SmallString<128> library(libref); |
| 109 | rc = HAL_LoadOneExtension(library.c_str()); |
| 110 | if (rc < 0) break; |
| 111 | } |
| 112 | return rc; |
| 113 | } |
| 114 | |
James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 115 | void HAL_SetShowExtensionsNotFoundMessages(HAL_Bool showMessage) { |
| 116 | GetShowNotFoundMessage() = showMessage; |
| 117 | } |
| 118 | |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 119 | } // extern "C" |