Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Bugzilla issue 24111 - [ImportC] fatal error C1034: stdio.h: no include path set #16248

Merged
merged 2 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions compiler/src/dmd/link.d
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,57 @@
// Convert command to wchar
wchar[1024] scratch = void;
auto smbuf = SmallBuffer!wchar(scratch.length, scratch[]);

// INCLUDE
static VSOptions vsopt; // cache, as this can be expensive
static Strings includePaths;
if (includePaths.length == 0)
{
if (!vsopt.VSInstallDir)
vsopt.initialize();

if (auto vcincludedir = vsopt.getVCIncludeDir()) {
includePaths.push(vcincludedir);
} else {
return DArray!ubyte();

Check warning on line 1183 in compiler/src/dmd/link.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/link.d#L1183

Added line #L1183 was not covered by tests
}
if (auto sdkincludedir = vsopt.getSDKIncludePath()) {
includePaths.push(FileName.combine(sdkincludedir, "ucrt"));
includePaths.push(FileName.combine(sdkincludedir, "shared"));
includePaths.push(FileName.combine(sdkincludedir, "um"));
includePaths.push(FileName.combine(sdkincludedir, "winrt"));
includePaths.push(FileName.combine(sdkincludedir, "cppwinrt"));
} else {
includePaths = Strings.init;
return DArray!ubyte();

Check warning on line 1193 in compiler/src/dmd/link.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/link.d#L1192-L1193

Added lines #L1192 - L1193 were not covered by tests
}
}

// Get current environment variable and rollback
auto oldIncludePathLen = GetEnvironmentVariableW("INCLUDE"w.ptr, null, 0);
wchar* oldIncludePaths = cast(wchar*)mem.xmalloc(oldIncludePathLen * wchar.sizeof);
oldIncludePathLen = GetEnvironmentVariableW("INCLUDE"w.ptr, oldIncludePaths, oldIncludePathLen);
scope (exit)
{
SetEnvironmentVariableW("INCLUDE"w.ptr, oldIncludePaths);
mem.xfree(oldIncludePaths);
}

// Make new environment variable
OutBuffer envbuf;
foreach (inc; includePaths[])
{
if (FileName.exists(inc) == 2)
{
envbuf.write(cast(const ubyte[])toWStringz(inc[0..strlen(inc)], smbuf));
envbuf.writewchar(';');
}
}
envbuf.write(cast(const ubyte[])oldIncludePaths[0..oldIncludePathLen]);
envbuf.writewchar('\0');
// Temporarily set INCLUDE environment variable
SetEnvironmentVariableW("INCLUDE"w.ptr, cast(LPCWSTR)envbuf.buf);

auto szCommand = toWStringz(buf.peekChars()[0 .. buf.length], smbuf);

int exitCode = runProcessCollectStdout(szCommand.ptr, buffer[], &sink);
Expand Down
1 change: 1 addition & 0 deletions compiler/test/dshell/extra-files/issue24111.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <stdio.h>
15 changes: 15 additions & 0 deletions compiler/test/dshell/issue24111.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import dshell;

int main()
{
version (Windows)
{
auto cmd = "$DMD -m$MODEL -c $EXTRA_FILES" ~ SEP ~ "issue24111.c";
run(cmd);

import std.process: environment;
environment.remove("INCLUDE");
run(cmd);
}
return 0;
}
Loading