Skip to content

Commit

Permalink
Pass <path>/importc.h/../include to the C preprocessor as an includ…
Browse files Browse the repository at this point in the history
…e path.
  • Loading branch information
just-harry committed Apr 23, 2024
1 parent 5bb51be commit f70502b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 3 additions & 1 deletion compiler/src/dmd/cpreprocess.d
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ DArray!ubyte preprocess(FileName csrcfile, ref const Loc loc, ref OutBuffer defi
//printf("preprocess %s\n", csrcfile.toChars());
version (runPreprocessor)
{
const includePath = FileName.combine(toDString(importc_h), "../include");
const command = global.params.cpp ? toDString(global.params.cpp) : cppCommand();
DArray!ubyte text;
int status = runPreprocessor(loc, command, csrcfile.toString(), importc_h, global.params.cppswitches, global.params.v.verbose, global.errorSink, defines, text);
int status = runPreprocessor(loc, command, csrcfile.toString(), importc_h, includePath, global.params.cppswitches, global.params.v.verbose, global.errorSink, defines, text);
FileName.free(includePath.ptr);
if (status)
fatal();
return text;
Expand Down
26 changes: 25 additions & 1 deletion compiler/src/dmd/link.d
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ public int runProgram(const char[] exefile, const char*[] runargs, bool verbose,
* cpp = name of C preprocessor program
* filename = C source file name
* importc_h = filename of importc.h
* includePath = path passed to the preprocessor as an include path
* cppswitches = array of switches to pass to C preprocessor
* verbose = print progress to eSink
* eSink = for verbose messages and error messages
Expand All @@ -1059,7 +1060,7 @@ public int runProgram(const char[] exefile, const char*[] runargs, bool verbose,
* Returns:
* error status, 0 for success
*/
public int runPreprocessor(ref const Loc loc, const(char)[] cpp, const(char)[] filename, const(char)* importc_h, ref Array!(const(char)*) cppswitches,
public int runPreprocessor(ref const Loc loc, const(char)[] cpp, const(char)[] filename, const(char)* importc_h, const(char)[] includePath, ref Array!(const(char)*) cppswitches,
bool verbose, ErrorSink eSink, ref OutBuffer defines, out DArray!ubyte text)
{
//printf("runPreprocessor() cpp: %.*s filename: %.*s\n", cast(int)cpp.length, cpp.ptr, cast(int)filename.length, filename.ptr);
Expand Down Expand Up @@ -1111,6 +1112,14 @@ public int runPreprocessor(ref const Loc loc, const(char)[] cpp, const(char)[] f
buf.printf(" /P /Zc:preprocessor /PD /nologo /utf-8 %.*s /FI%s /Fi%.*s",
cast(int)filename.length, filename.ptr, importc_h, cast(int)output.length, output.ptr);

/* Append the include path, if it was provided
*/
if (includePath)
{
buf.write(" /I");
buf.write(includePath);
}

/* Append preprocessor switches to command line
*/
foreach (a; cppswitches)
Expand Down Expand Up @@ -1266,6 +1275,14 @@ public int runPreprocessor(ref const Loc loc, const(char)[] cpp, const(char)[] f
buf.printf(" %.*s -HI%s -ED -o%.*s",
cast(int)filename.length, filename.ptr, importc_h, cast(int)output.length, output.ptr);

/* Append the include path, if it was provided
*/
if (includePath)
{
buf.write(" -I");
buf.write(includePath);
}

/* Append preprocessor switches to command line
*/
foreach (a; cppswitches)
Expand Down Expand Up @@ -1387,6 +1404,13 @@ public int runPreprocessor(ref const Loc loc, const(char)[] cpp, const(char)[] f
// need to redefine some macros in importc.h
argv.push("-Wno-builtin-macro-redefined");

// append the include path, if it was provided
if (includePath)
{
argv.push("-I");
argv.push(includePath);
}

if (target.os == Target.OS.OSX)
{
argv.push("-fno-blocks"); // disable clang blocks extension
Expand Down

0 comments on commit f70502b

Please sign in to comment.