/
cpreprocess.d
141 lines (128 loc) · 4.15 KB
/
cpreprocess.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Run the C preprocessor on a C source file.
*
* Specification: C11
*
* Copyright: Copyright (C) 2022-2024 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/cpreprocess.d, _cpreprocess.d)
* Documentation: https://dlang.org/phobos/dmd_cpreprocess.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/cpreprocess.d
*/
module dmd.cpreprocess;
import core.stdc.stdio;
import core.stdc.stdlib;
import dmd.errors;
import dmd.globals;
import dmd.link;
import dmd.location;
import dmd.target;
import dmd.vsoptions;
import dmd.common.outbuffer;
import dmd.root.array;
import dmd.root.filename;
import dmd.root.string;
// Use default for other versions
version (Posix) version = runPreprocessor;
version (Windows) version = runPreprocessor;
/***************************************
* Preprocess C file.
* Params:
* csrcfile = C file to be preprocessed, with .c or .h extension
* loc = The source location where preprocess is requested from
* ifile = set to true if an output file was written
* defines = buffer to append any `#define` and `#undef` lines encountered to
* Result:
* filename of output
*/
extern (C++)
FileName preprocess(FileName csrcfile, ref const Loc loc, out bool ifile, OutBuffer* defines)
{
/* Look for "importc.h" by searching along import path.
* It should be in the same place as "object.d"
*/
const(char)* importc_h;
foreach (entry; (global.path ? (*global.path)[] : null))
{
auto f = FileName.combine(entry, "importc.h");
if (FileName.exists(f) == 1)
{
importc_h = FileName.toAbsolute(f);
break;
}
FileName.free(f);
}
if (importc_h)
{
if (global.params.v.verbose)
message("include %s", importc_h);
}
else
{
error(loc, "cannot find \"importc.h\" along import path");
fatal();
}
//printf("preprocess %s\n", csrcfile.toChars());
version (runPreprocessor)
{
/*
To get sppn.exe: http://ftp.digitalmars.com/sppn.zip
To get the dmc C headers, dmc will need to be installed:
http://ftp.digitalmars.com/Digital_Mars_C++/Patch/dm857c.zip
*/
const name = FileName.name(csrcfile.toString());
const ext = FileName.ext(name);
assert(ext);
const ifilename = FileName.addExt(name[0 .. name.length - (ext.length + 1)], i_ext);
const command = global.params.cpp ? toDString(global.params.cpp) : cppCommand();
auto status = runPreprocessor(command, csrcfile.toString(), importc_h, global.params.cppswitches, ifilename, defines);
if (status)
{
error(loc, "C preprocess command %.*s failed for file %s, exit status %d\n",
cast(int)command.length, command.ptr, csrcfile.toChars(), status);
fatal();
}
//printf("C preprocess succeeded %s\n", ifilename.ptr);
ifile = true;
return FileName(ifilename);
}
else
return csrcfile; // no-op
}
private const(char)[] cppCommand()
{
if (auto p = getenv("CPPCMD"))
return toDString(p);
version (Windows)
{
if (target.objectFormat() == Target.ObjectFormat.coff)
{
VSOptions vsopt;
vsopt.initialize();
auto path = vsopt.compilerPath(target.isX86_64);
return toDString(path);
}
if (target.objectFormat() == Target.ObjectFormat.omf)
{
return "sppn.exe";
}
// Perhaps we are cross-compiling.
return "cpp";
}
else version (OpenBSD)
{
// On OpenBSD, we need to use the actual binary /usr/libexec/cpp
// rather than the shell script wrapper /usr/bin/cpp ...
// Turns out the shell script doesn't really understand -o
return "/usr/libexec/cpp";
}
else version (OSX)
{
return "clang";
}
else
{
return "cpp";
}
}