xref: /netbsd-src/external/bsd/pcc/dist/pcc/driver/prog_cpp.c (revision 411dcbec990c8aa9c57d3bd2f4bcacadec0b1ab5)
1 /*	Id	*/
2 /*	$NetBSD: prog_cpp.c,v 1.1.1.1 2016/02/09 20:29:13 plunky Exp $	*/
3 
4 /*-
5  * Copyright (c) 2014 Iain Hibbert.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #include "driver.h"
27 
JUNK(void)28 static void JUNK(void)
29 {
30 	// __LITTLE_ENDIAN__
31 	// __BIG_ENDIAN__
32 
33 	/* Machine specific macros */
34 	switch (targmach) {
35 	case mach_amd64:
36 		list_add(l, "-D__amd64__");
37 		list_add(l, "-D__amd64");
38 		list_add(l, "-D__x86_64__");
39 		list_add(l, "-D__x86_64");
40 		list_add(l, "-D__LP64__");
41 		list_add(l, "-D_LP64");
42 		break;
43 
44 	case mach_arm:
45 		list_add(l, "-D__arm__");
46 		break;
47 
48 	case mach_i386:
49 		list_add(l, "-D__i386__");
50 		list_add(l, "-D__i386");
51 		break;
52 
53 	case mach_nova:
54 		list_add(l, "-D__nova__");
55 		break;
56 
57 	case mach_m16c:
58 		list_add(l, "-D__m16c__");
59 		break;
60 
61 	case mach_mips:
62 		list_add(l, "-D__mips__");
63 		break;
64 
65 	case mach_pdp10:
66 		list_add(l, "-D__pdp10__");
67 		break;
68 
69 	case mach_pdp11:
70 		list_add(l, "-D__pdp11__");
71 		break;
72 
73 	case mach_powerpc:
74 		list_add(l, "-D__powerpc__");
75 		list_add(l, "-D__ppc__");
76 		break;
77 
78 	case mach_sparc:
79 		list_add(l, "-D__sparc__");
80 		list_add(l, "-D__sparc");
81 		break;
82 
83 	case mach_sparc64:
84 		list_add(l, "-D__sparc64__");
85 		list_add(l, "-D__sparc_v9__");
86 		list_add(l, "-D__sparcv9");
87 		list_add(l, "-D__sparc__");
88 		list_add(l, "-D__sparc");
89 		list_add(l, "-D__LP64__");
90 		list_add(l, "-D_LP64");
91 		break;
92 
93 	case mach_vax:
94 		list_add(l, "-D__vax__");
95 		break;
96 	}
97 }
98 
99 static void
cpp_add_stddef(struct list * l)100 cpp_add_stddef(struct list *l)
101 {
102 
103 	/* STDC */
104 	list_add(l, "-D__STDC__=1");
105 	list_add(l, "-D__STDC_HOSTED__=%d", opt.hosted ? 1 : 0);
106 	list_add(l, "-D__STDC_VERSION_=199901L");
107 	list_add(l, "-D__STDC_ISO_10646__=200009L");
108 
109 	/* PCC specific */
110 	list_add(l, "-D__PCC__=%d", PCC_MAJOR);
111 	list_add(l, "-D__PCC_MINOR__=%d", PCC_MINOR);
112 	list_add(l, "-D__PCC_MINORMINOR__=%d", PCC_MINORMINOR);
113 	list_add(l, "-D__VERSION__=\"%s\"", VERSSTR);
114 
115 	/* GNU C compat */
116 	if (opt.gnu89 || opt.gnu99) {
117 		list_add(l, "-D__GNUC__=4");
118 		list_add(l, "-D__GNUC_MINOR__=3");
119 		list_add(l, "-D__GNUC_PATCHLEVEL__=1");
120 		list_add(l, "-D__GNUC_%s_INLINE__", opt.gnu89 ? "GNU" : "STDC");
121 	}
122 
123 	/* language specific */
124 	if (strcmp(infile->next, "I")
125 		list_add(l, "-D__cplusplus");
126 	if (strcmp(infile->next, "f")
127 		list_add(l, "-D__FORTRAN__");
128 	if (strcmp(infile->next, "s")
129 		list_add(l, "-D__ASSEMBLER__");
130 
131 	/* runtime options */
132 	if (opt.optim > 0)
133 		list_add(l, "-D__OPTIMIZE__");
134 	if (opt.ssp > 0)
135 		list_add(l, "-D__SSP__");
136 	if (opt.pthread)
137 		list_add(l, "-D_PTHREADS");
138 	if (opt.uchar)
139 		list_add(l, "-D__CHAR_UNSIGNED__");
140 
141 	/* target specific definitions */
142 	list_add_array(l, target_defs);
143 }
144 
145 static int
146 cpp_exec(const char *infile, const char *outfile)
147 {
148 	list_t *l;
149 	const char *file;
150 	int rv;
151 
152 	if (prog_cpp == NULL)
153 		error("No preprocessor is defined");
154 
155 	l = list_alloc();
156 
157 	list_add_file(l, prog_cpp, &progdirs, X_OK);
158 
159 	for (int i = 0; i < opt.C; i++)
160 		list_add(l, "-C");
161 	if (opt.M)
162 		list_add(l, "-M");
163 	if (opt.P)
164 		list_add(l, "-P");
165 	if (opt.traditional)
166 		list_add(l, "-t");
167 	if (opt.verbose)
168 		list_add(l, "-v");
169 
170 	Wflag_add(l, W_CPP);
171 
172 	if (opt.stddefs)
173 		cpp_add_stddef(l);
174 
175 	list_add_list(l, opt.DIU);
176 
177 	list_add_list(l, opt.Wp);
178 
179 	if (infile != NULL)
180 		list_add(l, infile);
181 	else if (outfile != NULL)
182 		list_add(l, "-");
183 
184 	if (outfile != NULL)
185 		list_add(l, outfile);
186 
187 	rv = list_exec(l);
188 	list_free(l);
189 	return rv;
190 }
191