vmtester: Use loader library

This commit is contained in:
Paweł Bylica 2018-07-05 14:51:16 +02:00
parent 721f2b3083
commit 8331ca15de
No known key found for this signature in database
GPG Key ID: 7A0C037434FE77EF
2 changed files with 22 additions and 73 deletions

View File

@ -4,8 +4,8 @@
include(GNUInstallDirs) include(GNUInstallDirs)
hunter_add_package(Boost COMPONENTS program_options filesystem system) hunter_add_package(Boost COMPONENTS program_options)
find_package(Boost CONFIG REQUIRED program_options filesystem system) find_package(Boost CONFIG REQUIRED program_options)
add_executable(evmc-vmtester vmtester.hpp vmtester.cpp tests.cpp) add_executable(evmc-vmtester vmtester.hpp vmtester.cpp tests.cpp)
set_target_properties(evmc-vmtester PROPERTIES RUNTIME_OUTPUT_DIRECTORY ..) set_target_properties(evmc-vmtester PROPERTIES RUNTIME_OUTPUT_DIRECTORY ..)
@ -14,11 +14,9 @@ target_link_libraries(
evmc-vmtester evmc-vmtester
PRIVATE PRIVATE
evmc evmc
loader
GTest::gtest GTest::gtest
Boost::program_options Boost::program_options
Boost::filesystem
Boost::system
${CMAKE_DL_LIBS}
) )
install(TARGETS evmc-vmtester RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) install(TARGETS evmc-vmtester RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

View File

@ -4,23 +4,18 @@
#include "vmtester.hpp" #include "vmtester.hpp"
#include <boost/dll.hpp> #include <evmc/loader.h>
#include <boost/dll/alias.hpp>
#include <boost/function.hpp>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
namespace fs = boost::filesystem;
namespace dll = boost::dll;
namespace opts = boost::program_options; namespace opts = boost::program_options;
extern "C" using evmc_create_fn = evmc_instance * ();
namespace namespace
{ {
boost::function<evmc_create_fn> create_fn; evmc_create_fn create_fn;
std::unique_ptr<evmc_instance, evmc_destroy_fn> create_vm() std::unique_ptr<evmc_instance, evmc_destroy_fn> create_vm()
{ {
@ -35,43 +30,11 @@ evmc_instance* get_vm_instance()
return vm.get(); return vm.get();
} }
std::vector<std::string> get_vm_names(const fs::path path)
{
std::vector<std::string> 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[]) int main(int argc, char* argv[])
{ {
try try
{ {
fs::path vm_path; std::string vm_path;
opts::options_description desc("EVMC VM Tester Options"); opts::options_description desc("EVMC VM Tester Options");
auto add_option = desc.add_options(); auto add_option = desc.add_options();
@ -97,34 +60,22 @@ int main(int argc, char* argv[])
opts::notify(variables_map); opts::notify(variables_map);
std::cout << "Testing " << vm_path.filename().string() << "\n" std::cout << "Testing " << vm_path << "\n";
<< "Path: " << vm_path.string() << "\n"; evmc_loader_error_code ec;
create_fn = evmc_load(vm_path.c_str(), &ec);
for (auto&& name : get_vm_names(vm_path)) switch (ec)
{ {
try case EVMC_LOADER_SUCCESS:
{ break;
const std::string create_fn_name = "evmc_create_" + name; case EVMC_LOADER_CANNOT_OPEN:
std::cout << "Seeking `" << create_fn_name << "`... "; std::cerr << "Cannot open " << vm_path << "\n";
create_fn = dll::import<evmc_create_fn>(vm_path, create_fn_name); return static_cast<int>(ec);
std::cout << "found.\n"; case EVMC_LOADER_SYMBOL_NOT_FOUND:
break; std::cerr << "EVMC create function not found in " << vm_path << "\n";
} return static_cast<int>(ec);
catch (boost::system::system_error& err) default:
{ std::cerr << "Unexpected error in evmc_load(): " << ec << "\n";
using namespace boost::system; return static_cast<int>(ec);
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;
} }
std::cout << std::endl; std::cout << std::endl;