2017-10-18 02:27:55 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-10-09 22:25:02 -07:00
|
|
|
/* Copyright (c) 2017-2019 FIRST. All Rights Reserved. */
|
2017-10-18 02:27:55 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/Extensions.h"
|
2017-10-18 02:27:55 -05:00
|
|
|
|
2019-10-09 22:25:02 -07:00
|
|
|
#include <wpi/Path.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/SmallString.h>
|
|
|
|
|
#include <wpi/StringRef.h>
|
2019-10-09 22:25:02 -07:00
|
|
|
#include <wpi/raw_ostream.h>
|
2017-10-18 02:27:55 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "hal/HAL.h"
|
2017-10-18 02:27:55 -05:00
|
|
|
|
|
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#else
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
|
|
|
|
#define DELIM ';'
|
|
|
|
|
#define HTYPE HMODULE
|
|
|
|
|
#define DLOPEN(a) LoadLibrary(a)
|
|
|
|
|
#define DLSYM GetProcAddress
|
|
|
|
|
#define DLCLOSE FreeLibrary
|
|
|
|
|
#else
|
|
|
|
|
#define DELIM ':'
|
|
|
|
|
#define HTYPE void*
|
|
|
|
|
#define PREFIX "lib"
|
|
|
|
|
#define DLOPEN(a) dlopen(a, RTLD_LAZY)
|
|
|
|
|
#define DLSYM dlsym
|
|
|
|
|
#define DLCLOSE dlclose
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-12-10 19:38:53 -08:00
|
|
|
namespace hal {
|
|
|
|
|
namespace init {
|
|
|
|
|
void InitializeExtensions() {}
|
|
|
|
|
} // namespace init
|
|
|
|
|
} // namespace hal
|
|
|
|
|
|
2019-12-06 20:55:36 -08:00
|
|
|
static bool& GetShowNotFoundMessage() {
|
|
|
|
|
static bool showMsg = true;
|
|
|
|
|
return showMsg;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 02:27:55 -05:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
int HAL_LoadOneExtension(const char* library) {
|
|
|
|
|
int rc = 1; // It is expected and reasonable not to find an extra simulation
|
2019-10-09 22:25:02 -07:00
|
|
|
wpi::outs() << "HAL Extensions: Attempting to load: "
|
|
|
|
|
<< wpi::sys::path::stem(library) << "\n";
|
|
|
|
|
wpi::outs().flush();
|
2017-10-18 02:27:55 -05:00
|
|
|
HTYPE handle = DLOPEN(library);
|
|
|
|
|
#if !defined(WIN32) && !defined(_WIN32)
|
|
|
|
|
if (!handle) {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallString<128> libraryName("lib");
|
2017-10-18 02:27:55 -05:00
|
|
|
libraryName += library;
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
libraryName += ".dylib";
|
|
|
|
|
#else
|
|
|
|
|
libraryName += ".so";
|
|
|
|
|
#endif
|
2019-10-09 22:25:02 -07:00
|
|
|
wpi::outs() << "HAL Extensions: Trying modified name: "
|
|
|
|
|
<< wpi::sys::path::stem(libraryName);
|
|
|
|
|
wpi::outs().flush();
|
2017-10-18 02:27:55 -05:00
|
|
|
handle = DLOPEN(libraryName.c_str());
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2019-10-09 22:25:02 -07:00
|
|
|
if (!handle) {
|
|
|
|
|
wpi::outs() << "HAL Extensions: Failed to load library\n";
|
|
|
|
|
wpi::outs().flush();
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
2017-10-18 02:27:55 -05:00
|
|
|
|
|
|
|
|
auto init = reinterpret_cast<halsim_extension_init_func_t*>(
|
|
|
|
|
DLSYM(handle, "HALSIM_InitExtension"));
|
|
|
|
|
|
|
|
|
|
if (init) rc = (*init)();
|
|
|
|
|
|
2019-10-09 22:25:02 -07:00
|
|
|
if (rc != 0) {
|
|
|
|
|
wpi::outs() << "HAL Extensions: Failed to load extension\n";
|
|
|
|
|
wpi::outs().flush();
|
|
|
|
|
DLCLOSE(handle);
|
|
|
|
|
} else {
|
|
|
|
|
wpi::outs() << "HAL Extensions: Successfully loaded extension\n";
|
|
|
|
|
wpi::outs().flush();
|
|
|
|
|
}
|
2017-10-18 02:27:55 -05:00
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int HAL_LoadExtensions(void) {
|
|
|
|
|
int rc = 1;
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallVector<wpi::StringRef, 2> libraries;
|
2017-10-18 02:27:55 -05:00
|
|
|
const char* e = std::getenv("HALSIM_EXTENSIONS");
|
2019-10-09 22:25:02 -07:00
|
|
|
if (!e) {
|
2019-12-06 20:55:36 -08:00
|
|
|
if (GetShowNotFoundMessage()) {
|
|
|
|
|
wpi::outs() << "HAL Extensions: No extensions found\n";
|
|
|
|
|
wpi::outs().flush();
|
|
|
|
|
}
|
2019-10-09 22:25:02 -07:00
|
|
|
return rc;
|
|
|
|
|
}
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::StringRef env{e};
|
2017-10-18 02:27:55 -05:00
|
|
|
env.split(libraries, DELIM, -1, false);
|
|
|
|
|
for (auto& libref : libraries) {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallString<128> library(libref);
|
2017-10-18 02:27:55 -05:00
|
|
|
rc = HAL_LoadOneExtension(library.c_str());
|
|
|
|
|
if (rc < 0) break;
|
|
|
|
|
}
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 20:55:36 -08:00
|
|
|
void HAL_SetShowExtensionsNotFoundMessages(HAL_Bool showMessage) {
|
|
|
|
|
GetShowNotFoundMessage() = showMessage;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 02:27:55 -05:00
|
|
|
} // extern "C"
|