|
| 1 | +#include <Windows.h> |
| 2 | +#include <msi.h> |
| 3 | +#include <msiquery.h> |
| 4 | + |
| 5 | +#include <filesystem> |
| 6 | + |
| 7 | +std::wstring get_error_message(const std::error_code& error) { |
| 8 | + auto message = error.message(); |
| 9 | + auto length = MultiByteToWideChar(CP_THREAD_ACP, 0, message.c_str(), message.size(), nullptr, 0); |
| 10 | + std::wstring result(length, L'\0'); |
| 11 | + MultiByteToWideChar(CP_THREAD_ACP, 0, message.c_str(), message.size(), &result[0], length); |
| 12 | + return result; |
| 13 | +} |
| 14 | + |
| 15 | +namespace msi { |
| 16 | +void log(MSIHANDLE hInstall, UINT eMessageType, std::wstring message) noexcept { |
| 17 | + PMSIHANDLE record = MsiCreateRecord(0); |
| 18 | + (void)MsiRecordSetStringW(record, 0, message.c_str()); |
| 19 | + (void)MsiProcessMessage(hInstall, INSTALLMESSAGE(eMessageType), record); |
| 20 | +} |
| 21 | + |
| 22 | +void log_last_error(MSIHANDLE hInstall) noexcept { |
| 23 | + PMSIHANDLE record = MsiGetLastErrorRecord(); |
| 24 | + (void)MsiProcessMessage(hInstall, INSTALLMESSAGE_ERROR, record); |
| 25 | +} |
| 26 | + |
| 27 | +std::wstring get_property(MSIHANDLE hInstall, std::wstring name, UINT &result) noexcept { |
| 28 | + DWORD size = 0; |
| 29 | + |
| 30 | + result = MsiGetPropertyW(hInstall, name.c_str(), L"", &size); |
| 31 | + switch (result) { |
| 32 | + case ERROR_MORE_DATA: |
| 33 | + break; |
| 34 | + case ERROR_SUCCESS: |
| 35 | + result = ERROR_INSTALL_FAILURE; |
| 36 | + return nullptr; |
| 37 | + default: |
| 38 | + log_last_error(hInstall); |
| 39 | + return nullptr; |
| 40 | + } |
| 41 | + |
| 42 | + std::vector<WCHAR> buffer; |
| 43 | + buffer.resize(size + 1); |
| 44 | + |
| 45 | + size = buffer.capacity(); |
| 46 | + result = MsiGetPropertyW(hInstall, name.c_str(), buffer.data(), &size); |
| 47 | + switch (result) { |
| 48 | + case ERROR_SUCCESS: |
| 49 | + break; |
| 50 | + default: |
| 51 | + log_last_error(hInstall); |
| 52 | + return nullptr; |
| 53 | + } |
| 54 | + |
| 55 | + return {buffer.data(), buffer.capacity()}; |
| 56 | +} |
| 57 | +} |
| 58 | + |
| 59 | +extern "C" _declspec(dllexport) UINT __stdcall CopyClangResources(MSIHANDLE hInstall) { |
| 60 | + UINT result = ERROR_SUCCESS; |
| 61 | + |
| 62 | + // Get the runtime resource directory path |
| 63 | + auto resourceDirectory = msi::get_property(hInstall, L"CustomActionData", result); |
| 64 | + if (result != ERROR_SUCCESS) { |
| 65 | + return result; |
| 66 | + } |
| 67 | + msi::log(hInstall, INSTALLMESSAGE_INFO, L"Swift runtime resources: " + resourceDirectory); |
| 68 | + |
| 69 | + // Get the source and destination paths |
| 70 | + std::filesystem::path resourcePath(resourceDirectory); |
| 71 | + if (resourcePath.has_filename()) { |
| 72 | + resourcePath = resourcePath.parent_path(); |
| 73 | + } |
| 74 | + auto sourcePath = resourcePath.parent_path() / "clang"; |
| 75 | + auto destinationPath = resourcePath / "clang"; |
| 76 | + |
| 77 | + msi::log(hInstall, INSTALLMESSAGE_INFO, L"Clang resources: " + sourcePath.wstring()); |
| 78 | + msi::log(hInstall, INSTALLMESSAGE_INFO, L"Clang resources for Swift: " + destinationPath.wstring()); |
| 79 | + |
| 80 | + // Verify that Clang resources contain exactly one top-level directory |
| 81 | + { |
| 82 | + int directoryCount = 0, fileCount = 0; |
| 83 | + for (const auto& entry : std::filesystem::directory_iterator(sourcePath)) { |
| 84 | + if (entry.is_directory()) { |
| 85 | + directoryCount++; |
| 86 | + sourcePath = entry.path(); |
| 87 | + } else { |
| 88 | + fileCount++; |
| 89 | + } |
| 90 | + } |
| 91 | + if (directoryCount != 1 || fileCount != 0) { |
| 92 | + msi::log(hInstall, INSTALLMESSAGE_ERROR | MB_OK, L"lib\\clang must contain exactly one directory."); |
| 93 | + return ERROR_FILE_CORRUPT; |
| 94 | + } |
| 95 | + } |
| 96 | + msi::log(hInstall, INSTALLMESSAGE_INFO, L"Detected Clang version: " + sourcePath.filename().wstring()); |
| 97 | + |
| 98 | + // Copy the contents of the single directory directly to the destination path |
| 99 | + std::error_code error; |
| 100 | + auto options = std::filesystem::copy_options::overwrite_existing | std::filesystem::copy_options::recursive; |
| 101 | + |
| 102 | + std::filesystem::copy(sourcePath, destinationPath, options, error); |
| 103 | + if (error) { |
| 104 | + std::wstring errorMessage = L"Error during directory copy: " + get_error_message(error); |
| 105 | + msi::log(hInstall, INSTALLMESSAGE_ERROR | MB_OK, errorMessage); |
| 106 | + return ERROR_DISK_OPERATION_FAILED; |
| 107 | + } |
| 108 | + |
| 109 | + return result; |
| 110 | +} |
| 111 | + |
| 112 | +extern "C" _declspec(dllexport) UINT __stdcall RemoveClangResources(MSIHANDLE hInstall) { |
| 113 | + UINT result = ERROR_SUCCESS; |
| 114 | + |
| 115 | + // Get the directory path for removal |
| 116 | + auto resourceDirectory = msi::get_property(hInstall, L"_usr_lib_swift", result); |
| 117 | + if (result != ERROR_SUCCESS) { |
| 118 | + return result; |
| 119 | + } |
| 120 | + msi::log(hInstall, INSTALLMESSAGE_INFO, L"Swift runtime resources: " + resourceDirectory); |
| 121 | + |
| 122 | + std::filesystem::path resourcePath(resourceDirectory); |
| 123 | + if (resourcePath.has_filename()) { |
| 124 | + resourcePath = resourcePath.parent_path(); |
| 125 | + } |
| 126 | + auto directoryPath = resourcePath / "clang"; |
| 127 | + msi::log(hInstall, INSTALLMESSAGE_INFO, L"Clang resources for Swift: " + directoryPath.wstring()); |
| 128 | + |
| 129 | + // Remove the directory and its contents |
| 130 | + std::error_code error; |
| 131 | + auto count = std::filesystem::remove_all(directoryPath, error); |
| 132 | + if (error) { |
| 133 | + std::wstring errorMessage = L"Error during directory removal: " + get_error_message(error); |
| 134 | + msi::log(hInstall, INSTALLMESSAGE_ERROR | MB_OK, errorMessage); |
| 135 | + return ERROR_DISK_OPERATION_FAILED; |
| 136 | + } else if (count == 0) { |
| 137 | + msi::log(hInstall, INSTALLMESSAGE_INFO, L"Clang resources for Swift doesn't exist!"); |
| 138 | + } |
| 139 | + |
| 140 | + return result; |
| 141 | +} |
| 142 | + |
| 143 | +BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { |
| 144 | + switch (ul_reason_for_call) { |
| 145 | + case DLL_PROCESS_ATTACH: |
| 146 | + case DLL_PROCESS_DETACH: |
| 147 | + case DLL_THREAD_ATTACH: |
| 148 | + case DLL_THREAD_DETACH: |
| 149 | + break; |
| 150 | + } |
| 151 | + return TRUE; |
| 152 | +} |
0 commit comments