From 8331ca15deaeb8abc32ece054f5ea00ecee037bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 5 Jul 2018 14:51:16 +0200 Subject: [PATCH] vmtester: Use loader library --- test/vmtester/CMakeLists.txt | 8 ++-- test/vmtester/vmtester.cpp | 87 ++++++++---------------------------- 2 files changed, 22 insertions(+), 73 deletions(-) diff --git a/test/vmtester/CMakeLists.txt b/test/vmtester/CMakeLists.txt index fbff90d..b586a2d 100644 --- a/test/vmtester/CMakeLists.txt +++ b/test/vmtester/CMakeLists.txt @@ -4,8 +4,8 @@ include(GNUInstallDirs) -hunter_add_package(Boost COMPONENTS program_options filesystem system) -find_package(Boost CONFIG REQUIRED program_options filesystem system) +hunter_add_package(Boost COMPONENTS program_options) +find_package(Boost CONFIG REQUIRED program_options) add_executable(evmc-vmtester vmtester.hpp vmtester.cpp tests.cpp) set_target_properties(evmc-vmtester PROPERTIES RUNTIME_OUTPUT_DIRECTORY ..) @@ -14,11 +14,9 @@ target_link_libraries( evmc-vmtester PRIVATE evmc + loader GTest::gtest Boost::program_options - Boost::filesystem - Boost::system - ${CMAKE_DL_LIBS} ) install(TARGETS evmc-vmtester RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/test/vmtester/vmtester.cpp b/test/vmtester/vmtester.cpp index 3c5821c..ea7ba2d 100644 --- a/test/vmtester/vmtester.cpp +++ b/test/vmtester/vmtester.cpp @@ -4,23 +4,18 @@ #include "vmtester.hpp" -#include -#include -#include +#include + #include #include #include -namespace fs = boost::filesystem; -namespace dll = boost::dll; namespace opts = boost::program_options; -extern "C" using evmc_create_fn = evmc_instance * (); - namespace { -boost::function create_fn; +evmc_create_fn create_fn; std::unique_ptr create_vm() { @@ -35,43 +30,11 @@ evmc_instance* get_vm_instance() return vm.get(); } -std::vector get_vm_names(const fs::path path) -{ - std::vector names; - - // Get the filename without extension. - auto name = path.stem().string(); - - // Skip the optional library name prefix. - const std::string lib_name_prefix{"lib"}; - if (name.find(lib_name_prefix) == 0) - name = name.substr(lib_name_prefix.size()); - - size_t hyphen_pos = 0; - const std::string hyphen{"-"}; - if ((hyphen_pos = name.find(hyphen)) != std::string::npos) - { - // Replace the hyphen with underscore. - name.replace(hyphen_pos, hyphen.size(), "_"); - names.emplace_back(name); - - // Also add the name without the hyphen-separated prefix. - names.emplace_back(name.substr(hyphen_pos + hyphen.size())); - } - else - { - // Add the filename as the name. - names.emplace_back(std::move(name)); - } - - return names; -} - int main(int argc, char* argv[]) { try { - fs::path vm_path; + std::string vm_path; opts::options_description desc("EVMC VM Tester Options"); auto add_option = desc.add_options(); @@ -97,34 +60,22 @@ int main(int argc, char* argv[]) opts::notify(variables_map); - std::cout << "Testing " << vm_path.filename().string() << "\n" - << "Path: " << vm_path.string() << "\n"; - - for (auto&& name : get_vm_names(vm_path)) + std::cout << "Testing " << vm_path << "\n"; + evmc_loader_error_code ec; + create_fn = evmc_load(vm_path.c_str(), &ec); + switch (ec) { - try - { - const std::string create_fn_name = "evmc_create_" + name; - std::cout << "Seeking `" << create_fn_name << "`... "; - create_fn = dll::import(vm_path, create_fn_name); - std::cout << "found.\n"; - break; - } - catch (boost::system::system_error& err) - { - using namespace boost::system; - const error_code windows_error{127, system_category()}; - constexpr auto posix_error = errc::invalid_seek; - if (err.code() != posix_error && err.code() != windows_error) - throw; // Error other than "symbol not found". - std::cout << "not found.\n"; - } - } - - if (!create_fn) - { - std::cerr << "EVMC create function not found in " << vm_path.string() << "\n"; - return 2; + case EVMC_LOADER_SUCCESS: + break; + case EVMC_LOADER_CANNOT_OPEN: + std::cerr << "Cannot open " << vm_path << "\n"; + return static_cast(ec); + case EVMC_LOADER_SYMBOL_NOT_FOUND: + std::cerr << "EVMC create function not found in " << vm_path << "\n"; + return static_cast(ec); + default: + std::cerr << "Unexpected error in evmc_load(): " << ec << "\n"; + return static_cast(ec); } std::cout << std::endl;