Skip to content

Commit 4c9d06e

Browse files
committed
WiX: add custom action to remove Clang resources
1 parent 5d2b3e3 commit 4c9d06e

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

platforms/Windows/CustomActions/CustomActions.cc

+29-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ void log_last_error(MSIHANDLE hInstall) noexcept {
1616
(void)MsiProcessMessage(hInstall, INSTALLMESSAGE_ERROR, record);
1717
}
1818

19-
std::wstring get_action_data(MSIHANDLE hInstall, UINT &result) noexcept {
19+
std::wstring get_property(MSIHANDLE hInstall, std::wstring name, UINT &result) noexcept {
2020
DWORD size = 0;
2121

22-
result = MsiGetPropertyW(hInstall, L"CustomActionData", L"", &size);
22+
result = MsiGetPropertyW(hInstall, name.c_str(), L"", &size);
2323
switch (result) {
2424
case ERROR_MORE_DATA:
2525
break;
@@ -35,7 +35,7 @@ std::wstring get_action_data(MSIHANDLE hInstall, UINT &result) noexcept {
3535
buffer.resize(size + 1);
3636

3737
size = buffer.capacity();
38-
result = MsiGetPropertyW(hInstall, L"CustomActionData", buffer.data(), &size);
38+
result = MsiGetPropertyW(hInstall, name.c_str(), buffer.data(), &size);
3939
switch (result) {
4040
case ERROR_SUCCESS:
4141
break;
@@ -52,7 +52,7 @@ extern "C" _declspec(dllexport) UINT __stdcall CopyClangResources(MSIHANDLE hIns
5252
UINT result = ERROR_SUCCESS;
5353

5454
// Get the runtime resource directory path
55-
auto resourceDirectory = msi::get_action_data(hInstall, result);
55+
auto resourceDirectory = msi::get_property(hInstall, L"CustomActionData", result);
5656
if (result != ERROR_SUCCESS) {
5757
return result;
5858
}
@@ -101,6 +101,31 @@ extern "C" _declspec(dllexport) UINT __stdcall CopyClangResources(MSIHANDLE hIns
101101
return result;
102102
}
103103

104+
extern "C" _declspec(dllexport) UINT __stdcall RemoveClangResources(MSIHANDLE hInstall) {
105+
UINT result = ERROR_SUCCESS;
106+
107+
// Get the directory path for removal
108+
auto directory = msi::get_property(hInstall, L"_usr_lib_swift_clang", result);
109+
if (result != ERROR_SUCCESS) {
110+
return result;
111+
}
112+
std::filesystem::path directoryPath(directory);
113+
msi::log(hInstall, INSTALLMESSAGE_INFO, L"Clang resources for Swift: " + directoryPath.wstring());
114+
115+
// Remove the directory and its contents
116+
std::error_code error;
117+
auto count = std::filesystem::remove_all(directoryPath, error);
118+
if (error) {
119+
std::string errorMessage = "Error during directory removal: " + error.message();
120+
msi::log(hInstall, INSTALLMESSAGE_ERROR | MB_OK, {errorMessage.begin(), errorMessage.end()});
121+
return ERROR_DISK_OPERATION_FAILED;
122+
} else if (count == 0) {
123+
msi::log(hInstall, INSTALLMESSAGE_INFO, L"Clang resources for Swift doesn't exist!");
124+
}
125+
126+
return result;
127+
}
128+
104129
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
105130
switch (ul_reason_for_call) {
106131
case DLL_PROCESS_ATTACH:

platforms/Windows/toolchain-amd64.wxs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs" xmlns:ui="http://wixtoolset.org/schemas/v4/wxs/ui">
1+
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs" xmlns:ui="http://wixtoolset.org/schemas/v4/wxs/ui">
22
<Package
33
Language="1033"
44
Manufacturer="swift.org"
@@ -72,6 +72,8 @@
7272
</Directory>
7373
</Directory>
7474
<Directory Id="_usr_lib_swift" Name="swift">
75+
<Directory Id="_usr_lib_swift_clang" Name="clang">
76+
</Directory>
7577
<Directory Id="_usr_lib_swift_swiftToCxx" Name="swiftToCxx">
7678
</Directory>
7779
<Directory Id="_usr_lib_swift_migrator" Name="migrator">
@@ -589,10 +591,15 @@
589591
Execute="deferred"
590592
Impersonate="no"
591593
Return="check" />
594+
<CustomAction Id="RemoveClangResources"
595+
BinaryRef="CustomActions"
596+
DllEntry="RemoveClangResources"
597+
Impersonate="no" />
592598

593599
<InstallExecuteSequence>
594600
<Custom Action="CopyClangResources.SetProperty" Before="CopyClangResources" />
595601
<Custom Action="CopyClangResources" After="InstallFiles" Condition="NOT REMOVE" />
602+
<Custom Action="RemoveClangResources" Before="RemoveFiles" Condition='REMOVE="ALL"' />
596603
</InstallExecuteSequence>
597604
</Package>
598605
</Wix>

platforms/Windows/toolchain-arm64.wxs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs" xmlns:ui="http://wixtoolset.org/schemas/v4/wxs/ui">
1+
<Wix xmlns="http://wixtoolset.org/schemas/v4/wxs" xmlns:ui="http://wixtoolset.org/schemas/v4/wxs/ui">
22
<Package
33
Language="1033"
44
Manufacturer="swift.org"
@@ -72,6 +72,8 @@
7272
</Directory>
7373
</Directory>
7474
<Directory Id="_usr_lib_swift" Name="swift">
75+
<Directory Id="_usr_lib_swift_clang" Name="clang">
76+
</Directory>
7577
<Directory Id="_usr_lib_swift_swiftToCxx" Name="swiftToCxx">
7678
</Directory>
7779
<Directory Id="_usr_lib_swift_migrator" Name="migrator">
@@ -589,10 +591,15 @@
589591
Execute="deferred"
590592
Impersonate="no"
591593
Return="check" />
594+
<CustomAction Id="RemoveClangResources"
595+
BinaryRef="CustomActions"
596+
DllEntry="RemoveClangResources"
597+
Impersonate="no" />
592598

593599
<InstallExecuteSequence>
594600
<Custom Action="CopyClangResources.SetProperty" Before="CopyClangResources" />
595601
<Custom Action="CopyClangResources" After="InstallFiles" Condition="NOT REMOVE" />
602+
<Custom Action="RemoveClangResources" Before="RemoveFiles" Condition='REMOVE="ALL"' />
596603
</InstallExecuteSequence>
597604
</Package>
598605
</Wix>

0 commit comments

Comments
 (0)