Compare commits

...

3 Commits

Author SHA1 Message Date
Marko Budiselic
94428bc56f Fix Win compile and add init.ps1 example 2023-04-08 14:57:12 +02:00
Marko Budiselic
fb3029f33d Fix Apple 2023-04-08 13:51:37 +02:00
Marko Budiselic
d3eaff2adb Move SharedLibraryHandle to utils 2023-04-08 11:39:46 +00:00
5 changed files with 100 additions and 21 deletions

12
init.ps1 Normal file
View File

@ -0,0 +1,12 @@
# TODO(gitbuda): All paths compiler paths are hardcoded -> CMake will handle.
$compiler="C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.30.30705\bin\Hostx64\x64\cl"
$stdlibinclude="C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.30.30705\include"
$toolsinclude="C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\ucrt"
$uminclude="C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\um"
$sharedinclude="C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\shared"
$stdlib="C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.30.30705\lib\x64"
$umlib="C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\um\x64"
$ucrtlib="C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\ucrt\x64"
& $compiler -I"$stdlibinclude" -I"$toolsinclude" -I"$uminclude" -I"$sharedinclude" -I"src" /std:c++20 /EHsc tests\manual\shared_library_handle.cpp /link /LIBPATH:"$stdlib" /LIBPATH:"$umlib" /LIBPATH:"$ucrtlib"

View File

@ -1,4 +1,4 @@
// Copyright 2022 Memgraph Ltd.
// Copyright 2023 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
@ -26,6 +26,7 @@
#include "query/procedure/mg_procedure_impl.hpp"
#include "utils/memory.hpp"
#include "utils/rw_lock.hpp"
#include "utils/shared_library_handle.hpp"
class CypherMainVisitorTest;
@ -129,28 +130,10 @@ class ModuleRegistry final {
const std::filesystem::path &InternalModuleDir() const noexcept;
private:
class SharedLibraryHandle {
public:
SharedLibraryHandle(const std::string &shared_library, int mode) : handle_{dlopen(shared_library.c_str(), mode)} {}
SharedLibraryHandle(const SharedLibraryHandle &) = delete;
SharedLibraryHandle(SharedLibraryHandle &&) = delete;
SharedLibraryHandle operator=(const SharedLibraryHandle &) = delete;
SharedLibraryHandle operator=(SharedLibraryHandle &&) = delete;
~SharedLibraryHandle() {
if (handle_) {
dlclose(handle_);
}
}
private:
void *handle_;
};
#if __has_feature(address_sanitizer)
// This is why we need RTLD_NODELETE and we must not use RTLD_DEEPBIND with
// ASAN: https://github.com/google/sanitizers/issues/89
SharedLibraryHandle libstd_handle{"libstdc++.so.6", RTLD_NOW | RTLD_LOCAL | RTLD_NODELETE};
utils::LinuxDLHandle libstd_handle{"libstdc++.so.6", RTLD_NOW | RTLD_LOCAL | RTLD_NODELETE};
#else
// The reason behind opening share library during runtime is to avoid issues
// with loading symbols from stdlib. We have encounter issues with locale
@ -161,7 +144,7 @@ class ModuleRegistry final {
// mentioned library will be first performed in the already existing binded
// libraries and then the global namespace.
// RTLD_DEEPBIND => https://linux.die.net/man/3/dlopen
SharedLibraryHandle libstd_handle{"libstdc++.so.6", RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND};
utils::LinuxDLHandle libstd_handle{"libstdc++.so.6", RTLD_NOW | RTLD_LOCAL | RTLD_DEEPBIND};
#endif
std::vector<std::filesystem::path> modules_dirs_;
std::filesystem::path internal_module_dir_;

View File

@ -0,0 +1,46 @@
// Copyright 2023 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
// License, and you may not use this file except in compliance with the Business Source License.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
#include <string>
// https://stackoverflow.com/questions/142508/how-do-i-check-os-with-a-preprocessor-directive
// TODO(gitbuda): Deal with the flags in both: OS case and ASAN case.
namespace memgraph::utils {
#if defined(__linux__) || defined(__APPLE__)
#include <dlfcn.h>
class LinuxDLHandle {
public:
LinuxDLHandle(const std::string &shared_library, int mode) : handle_{dlopen(shared_library.c_str(), mode)} {}
LinuxDLHandle(const LinuxDLHandle &) = delete;
LinuxDLHandle(LinuxDLHandle &&) = delete;
LinuxDLHandle operator=(const LinuxDLHandle &) = delete;
LinuxDLHandle operator=(LinuxDLHandle &&) = delete;
~LinuxDLHandle() {
if (handle_) {
dlclose(handle_);
}
}
private:
void *handle_;
};
#endif
#ifdef _WIN32
#include <windows.h>
class WindowsDLLHandle {};
#endif
} // namespace memgraph::utils

View File

@ -55,3 +55,5 @@ target_link_libraries(${test_prefix}ssl_client mg-communication)
add_manual_test(ssl_server.cpp)
target_link_libraries(${test_prefix}ssl_server mg-communication)
add_manual_test(shared_library_handle.cpp)

View File

@ -0,0 +1,36 @@
// Copyright 2023 Memgraph Ltd.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
// License, and you may not use this file except in compliance with the Business Source License.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
#include <iostream>
#include "utils/shared_library_handle.hpp"
// APPLE CLANG 14: clang++ -I../../src -std=c++20 -o a.out shared_library_handle.cpp
using namespace memgraph::utils;
int main(int argc, char *argv[]) {
#if defined(__APPLE__)
std::cout << "Apple" << std::endl;
LinuxDLHandle handle("test", 0);
#elif defined(__linux__)
std::cout << "Linux" << std::endl;
LinuxDLHandle handle("test", 0);
#elif defined(_WIN32)
std::cout << "Windows" << std::endl;
WindowsDLLHandle handle;
#else
std::cout << "Unsupportd platform" << std::endl;
#endif
return 0;
}