1 /* ----------------------------------------------------------------------- */
4 /* with -D C2STR: convert tccdefs.h to C-strings */
9 /* replace native host macros by compile-time versions */
10 const char *platform_macros
[] = {
11 "__i386__", "TCC_TARGET_I386",
12 "__x86_64__", "TCC_TARGET_X86_64",
13 "_WIN32", "TCC_TARGET_PE",
14 "__arm__", "TCC_TARGET_ARM",
15 "__ARM_EABI__", "TCC_ARM_EABI",
16 "__aarch64__", "TCC_TARGET_ARM64",
17 "__riscv", "TCC_TARGET_RISCV64",
18 "__APPLE__", "TCC_TARGET_MACHO",
19 "__FreeBSD__", "TARGETOS_FreeBSD",
20 "__FreeBSD_kernel__", "TARGETOS_FreeBSD_kernel",
21 "__OpenBSD__", "TARGETOS_OpenBSD",
22 "__NetBSD__", "TARGETOS_NetBSD",
23 "__linux__", "TARGETOS_Linux",
24 "__SIZEOF_POINTER__", "PTR_SIZE",
25 "__SIZEOF_LONG__", "LONG_SIZE",
31 return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z')
32 || (c
>= '0' && c
<= '9') || c
== '_';
37 return (unsigned char)c
<= ' ' && c
!= 0;
40 int main(int argc
, char **argv
)
42 char l
[1000], l2
[1000], *p
, *q
, *p0
;
44 int c
, e
, f
, s
, cmt
, cmt_n
;
50 fp
= fopen(argv
[1], "rb");
51 op
= fopen(argv
[2], "wb");
53 fprintf(stderr
, "c2str: file error\n");
61 if (fgets(p
, sizeof l
- (p
- l
), fp
)) {
63 while (p
> l
&& isspc(p
[-1]))
69 /* check for continuation */
70 if (p
> l
&& p
[-1] == '\\') {
75 /* count & skip leading spaces */
77 while (*p
&& isspc(*p
))
81 if (p
[0] == '/' && cmt
== 0) {
90 fprintf(op
, " (converted, do not edit this file)");
96 if (p
>= l
+ 2 && p
[-1] == '/' && p
[-2] == '*')
104 /* replace machine/os macros by compile-time counterparts */
105 for (e
= f
= 0; (r
= platform_macros
[f
]); f
+= 2) {
107 /* remove 'defined' */
108 //e = memcmp(p, "defined ", 8) ? 0 : 8;
109 if (0 == memcmp(p
+ e
, r
, c
)) {
111 q
= strchr(strcpy(q
, platform_macros
[f
+ 1]), 0);
118 } while (!!(*q
++ = *p
++));
120 fprintf(op
, "%s\n", l2
);
124 s
= e
= f
= 0, p0
= p
;
132 if (c
== '/' && (p
[0] == '/' || p
[0] == '*'))
133 c
= 0; /* trailing comment detected */
135 && ((isid(q
[-1]) && isid(c
))
136 // keep space after macro name
149 if (c
== '\\' || c
== '\"')
156 /* output with quotes */
157 fprintf(op
, " \"%s\\n\"%s\n", l2
, p0
);
166 /* ----------------------------------------------------------------------- */
169 /* get some information from the host compiler for configure */
177 /* Define architecture */
178 #if defined(__i386__) || defined _M_IX86
179 # define TRIPLET_ARCH "i386"
180 #elif defined(__x86_64__) || defined _M_AMD64
181 # define TRIPLET_ARCH "x86_64"
182 #elif defined(__arm__)
183 # define TRIPLET_ARCH "arm"
184 #elif defined(__aarch64__)
185 # define TRIPLET_ARCH "aarch64"
186 #elif defined(__riscv) && defined(__LP64__)
187 # define TRIPLET_ARCH "riscv64"
189 # define TRIPLET_ARCH "unknown"
193 #if defined (__linux__)
194 # define TRIPLET_OS "linux"
195 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
196 # define TRIPLET_OS "kfreebsd"
197 #elif defined(__NetBSD__)
198 # define TRIPLET_OS "netbsd"
199 #elif defined(__OpenBSD__)
200 # define TRIPLET_OS "openbsd"
201 #elif defined(_WIN32)
202 # define TRIPLET_OS "win32"
203 #elif defined(__APPLE__)
204 # define TRIPLET_OS "darwin"
205 #elif !defined (__GNU__)
206 # define TRIPLET_OS "unknown"
209 /* Define calling convention and ABI */
210 #if defined (__ARM_EABI__)
211 # if defined (__ARM_PCS_VFP)
212 # define TRIPLET_ABI "gnueabihf"
214 # define TRIPLET_ABI "gnueabi"
217 # define TRIPLET_ABI "gnu"
221 # define TRIPLET TRIPLET_ARCH "-" TRIPLET_OS
222 #elif defined __GNU__
223 # define TRIPLET TRIPLET_ARCH "-" TRIPLET_ABI
225 # define TRIPLET TRIPLET_ARCH "-" TRIPLET_OS "-" TRIPLET_ABI
232 int main(int argc
, char *argv
[])
235 _setmode(_fileno(stdout
), _O_BINARY
); /* don't translate \n to \r\n */
237 switch(argc
== 2 ? argv
[1][0] : 0) {
240 volatile unsigned foo
= 0x01234567;
241 puts(*(unsigned char*)&foo
== 0x67 ? "no" : "yes");
244 #if defined(__clang__)
246 printf("%d\n", __clang_minor__
);
249 printf("%d\n", __clang_major__
);
251 #elif defined(__TINYC__)
256 printf("%d\n", __TINYC__
);
258 #elif defined(_MSC_VER)
263 printf("%d\n", _MSC_VER
);
265 #elif defined(__GNUC__) && defined(__GNUC_MINOR__)
266 /* GNU comes last as other compilers may add 'GNU' compatibility */
268 printf("%d\n", __GNUC_MINOR__
);
271 printf("%d\n", __GNUC__
);
283 #if defined(__clang__)
285 #elif defined(__TINYC__)
287 #elif defined(_MSC_VER)
289 #elif defined(__GNUC__)
301 /* ----------------------------------------------------------------------- */