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