Skip to content

Commit 10c804b

Browse files
committed
runtime: adjust the program name reference
`__progname` is not available on Windows, and is provided by libbsd on Linux. This provides a replacement for the functional aspect of the symbol on Windows.
1 parent ecaa041 commit 10c804b

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Runtimes/Core/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ target_include_directories(swiftRuntime PRIVATE
110110

111111
target_link_libraries(swiftRuntime PRIVATE
112112
$<$<PLATFORM_ID:Windows>:User32>
113+
$<$<PLATFORM_ID:Windows>:ShLwApi>
113114
swiftShims
114115
swiftDemangling)
115116

stdlib/public/runtime/LibPrespecialized.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
#define HAS_OS_FEATURE 1
3434
#endif
3535

36+
#if defined(_WIN32)
37+
#define WIN32_LEAN_AND_MEAN
38+
#define NOMINMAX
39+
#include <ShlWapi.h>
40+
#include <Windows.h>
41+
#endif
42+
3643
using namespace swift;
3744

3845
static bool prespecializedLoggingEnabled = false;
@@ -67,10 +74,42 @@ static bool environmentProcessListContainsProcess(const char *list,
6774
}
6875

6976
static bool isThisProcessEnabled(const LibPrespecializedData<InProcess> *data) {
77+
#if defined(_WIN32)
78+
DWORD dwSize = MAX_PATH;
79+
DWORD dwResult;
80+
std::unique_ptr<WCHAR[]> pwszBuffer(new WCHAR[dwSize]);
81+
while (true) {
82+
dwResult = GetModuleFileNameW(nullptr, pwszBuffer.get(), dwSize);
83+
if (dwResult == 0)
84+
return true;
85+
if (dwResult < dwSize)
86+
break;
87+
if (dwResult == dwSize && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
88+
pwszBuffer.reset(new WCHAR[dwSize <<= 1]);
89+
}
90+
91+
PCWSTR pwszBaseName = PathFindFileNameW(pwszBuffer.get());
92+
93+
DWORD cchLength =
94+
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS | WC_NO_BEST_FIT_CHARS,
95+
pwszBaseName, -1, nullptr, 0, nullptr, nullptr);
96+
if (cchLength == 0)
97+
return true;
98+
99+
std::unique_ptr<char[]> pszBaseName{new char[cchLength]};
100+
cchLength = WideCharToMultiByte(
101+
CP_UTF8, WC_ERR_INVALID_CHARS | WC_NO_BEST_FIT_CHARS, pwszBaseName, -1,
102+
pszBaseName.get(), cchLength, nullptr, nullptr);
103+
if (cchLength == 0)
104+
return true;
105+
106+
const char *__progname = pszBaseName.get();
107+
#else
70108
extern const char *__progname;
71109

72110
if (!__progname)
73111
return true;
112+
#endif
74113

75114
auto envEnabledProcesses =
76115
runtime::environment::SWIFT_DEBUG_LIB_PRESPECIALIZED_ENABLED_PROCESSES();

0 commit comments

Comments
 (0)