Skip to content

Commit f0f81f7

Browse files
committed
runtime: annotate _environ DLL storage, exclude in WinRT
`_environ` is meant to be DLL imported when linking against the C runtime dynamically (`/MD` or `/MDd`). However, when building for the Windows Runtime environment, we cannot support the use of `_environ` and thus disable the support for that. `_WINRT_DLL` identifies the combination of `/ZW` and `/LD` or `/LDd`. Windows Runtime builds cannot be executables (and obviously not static libraries as they are not executable), and thus we properly disable `ENVIRON` in all Windows Runtime builds.
1 parent c139bb4 commit f0f81f7

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

stdlib/public/runtime/EnvironmentVariables.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,19 @@ OnceToken_t swift::runtime::environment::initializeToken;
122122
extern "C" char **environ;
123123
#define ENVIRON environ
124124
#elif defined(_WIN32)
125+
// `_environ` is DLL-imported unless we are linking against the static C runtime
126+
// (via `/MT` or `/MTd`).
127+
#if defined(_DLL)
128+
extern "C" __declspec(dllimport) char **_environ;
129+
#else
125130
extern "C" char **_environ;
131+
#endif
132+
// `_environ` is unavailable in the Windows Runtime environment.
133+
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/environ-wenviron?view=msvc-160
134+
#if !defined(_WINRT_DLL)
126135
#define ENVIRON _environ
127136
#endif
137+
#endif
128138

129139
#ifdef ENVIRON
130140
void swift::runtime::environment::initialize(void *context) {

0 commit comments

Comments
 (0)