Skip to content

Various windows fixes #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
814479a
Fix rvalue error for std::string in DynamicLibraryManager::isSharedLi…
fsfod Jan 15, 2024
2268634
Fix error from window.h declaring min macro, disabled with NOMINMAX
fsfod Jan 15, 2024
9d037ba
Fix Dtor_Comdat to clang::Dtor_Comdat in maybeMangleDeclName
fsfod Jan 15, 2024
f3ad010
Fix isa compile error in Compatibility.h
fsfod Jan 15, 2024
b5f7150
Fix rvalue errors from LookupLibrary returning const std::string
fsfod Jan 15, 2024
306708f
Move include of dlfcn.h to non windows only in CppInterOp.cpp
fsfod Jan 15, 2024
466bf4c
Replace dlsym with llvm::sys::DynamicLibrary::SearchForAddressOfSymbo…
fsfod Jan 15, 2024
67248b4
Use llvm::sys::fs::real_path in NormalizePath instead of realpath
fsfod Jan 15, 2024
67d8567
Move dlfcn.h include to be guarded on LLVM_ON_UNIX in Paths.cpp
fsfod Jan 16, 2024
2c72340
Make GetSystemLibraryPaths a nop on windows since PATH was already ad…
fsfod Jan 16, 2024
b0684dd
Make DyLibHandle non const so we can pass it to llvm API
fsfod Jan 16, 2024
652abef
Add windows implementations of DLOpen\DLClose using llvm's DynamicLib…
fsfod Jan 16, 2024
0264765
Don't try link dl on windows for clangCppInterOp
fsfod Jan 16, 2024
4f0ae2d
Implement IsDLL in the same style as identify_magic
fsfod Jan 17, 2024
11c6fe4
Only declare Popen in Paths.cpp for LLVM_ON_UNIX
fsfod Jan 17, 2024
6875276
Fix unit tests failing because NormalizePath was not using SmallStrin…
fsfod Jan 17, 2024
b300623
Define STDOUT_FILENO to 1 for windows if not already defined
fsfod Jan 18, 2024
15490eb
Fix git apply patch command in README.md
fsfod Jan 18, 2024
745590f
Only link pthread for unit tests on non windows platforms
fsfod Jan 18, 2024
e91f9c0
Fix gtest library link paths on windows
fsfod Jan 18, 2024
abc6f6f
Fix TestSharedLib not exporting ret_zero on windows
fsfod Jan 18, 2024
31db0ca
Don't use BUILD_SHARED_LIB for windows
fsfod Jan 19, 2024
59c5f1d
Set an build config to run tests since VS is multi-config generator
fsfod Jan 19, 2024
c8c6928
Don't build gtest with different runtime library type/build config o…
fsfod Jan 19, 2024
d8e7b95
Just build gtest using the current config on all platforms
fsfod Jan 19, 2024
cc2dfc4
Make the DynamicLibraryManagerTests pass
fsfod Jan 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -666,15 +666,15 @@ jobs:
-DUSE_REPL=OFF `
-DCling_DIR="$env:LLVM_BUILD_DIR\tools\cling" `
-DLLVM_DIR="$env:LLVM_BUILD_DIR" `
-DClang_DIR="$env:LLVM_BUILD_DIR" -DBUILD_SHARED_LIBS=ON -DCODE_COVERAGE=${{ env.CODE_COVERAGE }} -DCMAKE_INSTALL_PREFIX="$env:CPPINTEROP_DIR" ..\
-DClang_DIR="$env:LLVM_BUILD_DIR" -DCODE_COVERAGE=${{ env.CODE_COVERAGE }} -DCMAKE_INSTALL_PREFIX="$env:CPPINTEROP_DIR" ..\
}
else
{
cmake -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} `
-DUSE_CLING=OFF `
-DUSE_REPL=ON `
-DLLVM_DIR="$env:LLVM_BUILD_DIR\lib\cmake\llvm" `
-DClang_DIR="$env:LLVM_BUILD_DIR\lib\cmake\clang" -DBUILD_SHARED_LIBS=ON -DCODE_COVERAGE=${{ env.CODE_COVERAGE }} -DCMAKE_INSTALL_PREFIX="$env:CPPINTEROP_DIR" ..\
-DClang_DIR="$env:LLVM_BUILD_DIR\lib\cmake\clang" -DCODE_COVERAGE=${{ env.CODE_COVERAGE }} -DCMAKE_INSTALL_PREFIX="$env:CPPINTEROP_DIR" ..\
}
cmake --build . --target check-cppinterop --parallel ${{ env.ncpus }}
cd ..
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ cd llvm-project
```
Get the following patches required for development work. To apply these patches on Linux and MacOS execute the following command
```
git apply -v ..\patches\llvm\clang${{ matrix.clang-runtime }}-*.patch
git apply -v .../patches/llvm/clang17-*.patch
```
and
```
Expand Down
10 changes: 7 additions & 3 deletions cmake/modules/GoogleTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if(MSVC)
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=${_gtest_byproduct_binary_dir}/lib/
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO:PATH=${_gtest_byproduct_binary_dir}/lib/
-Dgtest_force_shared_crt=ON
BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --config Release)
BUILD_COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --config $<CONFIG>)
elseif(APPLE)
set(EXTRA_GTEST_OPTS -DCMAKE_OSX_SYSROOT=${CMAKE_OSX_SYSROOT})
endif()
Expand All @@ -32,7 +32,7 @@ ExternalProject_Add(
# -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE:PATH=ReleaseLibs
# -Dgtest_force_shared_crt=ON
CMAKE_ARGS -G ${CMAKE_GENERATOR}
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_BUILD_TYPE=$<CONFIG>
-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
Expand All @@ -59,7 +59,11 @@ file(MAKE_DIRECTORY ${GTEST_INCLUDE_DIR} ${GMOCK_INCLUDE_DIR})

# Libraries
ExternalProject_Get_Property(googletest binary_dir)
set(_G_LIBRARY_PATH ${binary_dir}/lib/)
if(WIN32)
set(_G_LIBRARY_PATH ${_gtest_byproduct_binary_dir}/lib)
else()
set(_G_LIBRARY_PATH ${binary_dir}/lib/)
endif()

# Use gmock_main instead of gtest_main because it initializes gtest as well.
# Note: The libraries are listed in reverse order of their dependancies.
Expand Down
2 changes: 1 addition & 1 deletion include/clang/Interpreter/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ namespace Cpp {

/// Looks up the library if access is enabled.
///\returns the path to the library.
const std::string LookupLibrary(const char *lib_name);
std::string LookupLibrary(const char* lib_name);

/// Finds \c lib_stem considering the list of search paths and loads it by
/// calling dlopen.
Expand Down
5 changes: 4 additions & 1 deletion lib/Interpreter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ set(link_libs
clangFrontend
clangLex
clangSema
dl
)

if(NOT WIN32)
list(APPEND link_libs dl)
endif()

# Get rid of libLLVM-X.so which is appended to the list of static libraries.
if (LLVM_LINK_LLVM_DYLIB)
set(new_libs ${link_libs})
Expand Down
5 changes: 3 additions & 2 deletions lib/Interpreter/Compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "llvm/Config/llvm-config.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Path.h"

#ifdef USE_CLING
Expand Down Expand Up @@ -169,8 +170,8 @@ inline void maybeMangleDeclName(const clang::GlobalDecl& GD,

#if defined(_WIN32)
// MicrosoftMangle.cpp:954 calls llvm_unreachable when mangling Dtor_Comdat
if (isa<clang::CXXDestructorDecl>(GD.getDecl()) &&
GD.getDtorType() == Dtor_Comdat) {
if (llvm::isa<clang::CXXDestructorDecl>(GD.getDecl()) &&
GD.getDtorType() == clang::Dtor_Comdat) {
if (const clang::IdentifierInfo* II = D->getIdentifier())
RawStr << II->getName();
} else
Expand Down
10 changes: 7 additions & 3 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_os_ostream.h"

#include <dlfcn.h>
#include <sstream>
#include <string>

// Stream redirect.
#ifdef WIN32
#include <io.h>
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
#else
#include <dlfcn.h>
#include <unistd.h>
#endif // WIN32

Expand Down Expand Up @@ -1084,7 +1087,8 @@ namespace Cpp {
auto GD = GlobalDecl(VD);
std::string mangledName;
compat::maybeMangleDeclName(GD, mangledName);
auto address = dlsym(/*whole_process=*/0, mangledName.c_str());
void* address = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
mangledName.c_str());
auto &I = getInterp();
if (!address)
address = I.getAddressOfGlobal(GD);
Expand Down Expand Up @@ -2584,7 +2588,7 @@ namespace Cpp {
return compat::convertTo<intptr_t>(V);
}

const std::string LookupLibrary(const char *lib_name) {
std::string LookupLibrary(const char* lib_name) {
return getInterp().getDynamicLibraryManager()->lookupLibrary(lib_name);
}

Expand Down
30 changes: 25 additions & 5 deletions lib/Interpreter/DynamicLibraryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"

#if defined(_WIN32)
#include "llvm/BinaryFormat/COFF.h"
#include "llvm/Support/Endian.h"
#endif

#include <fstream>
#include <system_error>
#include <sys/stat.h>
Expand Down Expand Up @@ -429,6 +434,21 @@ namespace Cpp {
}
}

#if defined(_WIN32)
static bool IsDLL(llvm::StringRef headers) {
using namespace llvm::support::endian;

uint32_t headeroffset = read32le(headers.data() + 0x3c);
auto peheader = headers.substr(headeroffset, 24);
if (peheader.size() != 24) {
return false;
}
// Read Characteristics from the coff header
uint32_t characteristics = read16le(peheader.data() + 22);
return (characteristics & llvm::COFF::IMAGE_FILE_DLL) != 0;
}
#endif

bool DynamicLibraryManager::isSharedLibrary(StringRef libFullPath,
bool* exists /*=0*/) {
using namespace llvm;
Expand All @@ -449,7 +469,8 @@ namespace Cpp {
// do its work.
// TODO: Fix the code upstream and consider going back to calling the
// convenience function after a future LLVM upgrade.
std::ifstream in(libFullPath.str(), std::ios::binary);
std::string path = libFullPath.str();
std::ifstream in(path, std::ios::binary);
char header[1024] = {0};
in.read(header, sizeof(header));
if (in.fail()) {
Expand All @@ -474,10 +495,9 @@ namespace Cpp {
(Magic == file_magic::elf_shared_object)
#endif
#elif defined(_WIN32)
// We should only include dll libraries without including executables,
// object code and others...
(Magic == file_magic::pecoff_executable &&
platform::IsDLL(libFullPath.str()))
// We should only include dll libraries without including executables,
// object code and others...
(Magic == file_magic::pecoff_executable && IsDLL(headerStr))
#else
# error "Unsupported platform."
#endif
Expand Down
2 changes: 1 addition & 1 deletion lib/Interpreter/DynamicLibraryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class DynamicLibraryManager {
using SearchPathInfos = llvm::SmallVector<SearchPathInfo, 32>;

private:
typedef const void* DyLibHandle;
typedef void* DyLibHandle;
typedef llvm::DenseMap<DyLibHandle, std::string> DyLibs;
///\brief DynamicLibraries loaded by this Interpreter.
///
Expand Down
2 changes: 2 additions & 0 deletions lib/Interpreter/DynamicLibraryManagerSymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#endif // __APPLE__

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
#include <libloaderapi.h> // For GetModuleFileNameA
#include <memoryapi.h> // For VirtualQuery
Expand Down
Loading