2020-12-23 04:10:22 +08:00
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
/* with -D C2STR: convert tccdefs.h to C-strings */
|
|
|
|
|
2023-09-07 04:21:15 +08:00
|
|
|
#if C2STR
|
|
|
|
|
2020-12-23 04:10:22 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/* replace native host macros by compile-time versions */
|
|
|
|
const char *platform_macros[] = {
|
|
|
|
"__i386__", "TCC_TARGET_I386",
|
|
|
|
"__x86_64__", "TCC_TARGET_X86_64",
|
|
|
|
"_WIN32", "TCC_TARGET_PE",
|
|
|
|
"__arm__", "TCC_TARGET_ARM",
|
|
|
|
"__ARM_EABI__", "TCC_ARM_EABI",
|
|
|
|
"__aarch64__", "TCC_TARGET_ARM64",
|
|
|
|
"__riscv", "TCC_TARGET_RISCV64",
|
|
|
|
"__APPLE__", "TCC_TARGET_MACHO",
|
|
|
|
"__FreeBSD__", "TARGETOS_FreeBSD",
|
|
|
|
"__FreeBSD_kernel__", "TARGETOS_FreeBSD_kernel",
|
|
|
|
"__OpenBSD__", "TARGETOS_OpenBSD",
|
|
|
|
"__NetBSD__", "TARGETOS_NetBSD",
|
|
|
|
"__linux__", "TARGETOS_Linux",
|
2022-05-18 04:28:32 +08:00
|
|
|
"__ANDROID__", "TARGETOS_ANDROID",
|
|
|
|
|
2020-12-23 04:10:22 +08:00
|
|
|
"__SIZEOF_POINTER__", "PTR_SIZE",
|
|
|
|
"__SIZEOF_LONG__", "LONG_SIZE",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
int isid(int c)
|
|
|
|
{
|
|
|
|
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|
|
|
|
|| (c >= '0' && c <= '9') || c == '_';
|
|
|
|
}
|
|
|
|
|
|
|
|
int isspc(int c)
|
|
|
|
{
|
|
|
|
return (unsigned char)c <= ' ' && c != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
char l[1000], l2[1000], *p, *q, *p0;
|
|
|
|
FILE *fp, *op;
|
|
|
|
int c, e, f, s, cmt, cmt_n;
|
|
|
|
const char *r;
|
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
fp = fopen(argv[1], "rb");
|
|
|
|
op = fopen(argv[2], "wb");
|
|
|
|
if (!fp || !op) {
|
|
|
|
fprintf(stderr, "c2str: file error\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmt = cmt_n = 0;
|
|
|
|
for (;;) {
|
|
|
|
p = l;
|
|
|
|
append:
|
|
|
|
if (fgets(p, sizeof l - (p - l), fp)) {
|
|
|
|
p = strchr(p, 0);
|
|
|
|
while (p > l && isspc(p[-1]))
|
|
|
|
--p;
|
|
|
|
*p = 0;
|
|
|
|
} else if (p == l)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* check for continuation */
|
|
|
|
if (p > l && p[-1] == '\\') {
|
|
|
|
p[-1] = ' ';
|
|
|
|
goto append;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* count & skip leading spaces */
|
|
|
|
p = l, q = l2, f = 0;
|
|
|
|
while (*p && isspc(*p))
|
|
|
|
++p, ++f;
|
|
|
|
|
|
|
|
/* handle comments */
|
|
|
|
if (p[0] == '/' && cmt == 0) {
|
|
|
|
if (p[1] == '*')
|
|
|
|
cmt = 2;
|
|
|
|
if (p[1] == '/')
|
|
|
|
cmt = 1;
|
|
|
|
}
|
|
|
|
if (cmt) {
|
|
|
|
fprintf(op, "%s", l);
|
|
|
|
if (++cmt_n == 1)
|
|
|
|
fprintf(op, " (converted, do not edit this file)");
|
|
|
|
fprintf(op, "\n");
|
|
|
|
if (cmt == 1)
|
|
|
|
cmt = 0;
|
|
|
|
if (cmt == 2) {
|
|
|
|
p = strchr(l, 0);
|
|
|
|
if (p >= l + 2 && p[-1] == '/' && p[-2] == '*')
|
|
|
|
cmt = 0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (f < 4) {
|
|
|
|
do {
|
|
|
|
/* replace machine/os macros by compile-time counterparts */
|
|
|
|
for (e = f = 0; (r = platform_macros[f]); f += 2) {
|
|
|
|
c = strlen(r);
|
|
|
|
/* remove 'defined' */
|
|
|
|
//e = memcmp(p, "defined ", 8) ? 0 : 8;
|
|
|
|
if (0 == memcmp(p + e, r, c)) {
|
|
|
|
p += e + c;
|
|
|
|
q = strchr(strcpy(q, platform_macros[f + 1]), 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
if (r)
|
|
|
|
continue;
|
|
|
|
} while (!!(*q++ = *p++));
|
|
|
|
/* output as is */
|
|
|
|
fprintf(op, "%s\n", l2);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
s = e = f = 0, p0 = p;
|
|
|
|
for (;;) {
|
|
|
|
c = *p++;
|
|
|
|
|
|
|
|
if (isspc(c)) {
|
|
|
|
s = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == '/' && (p[0] == '/' || p[0] == '*'))
|
|
|
|
c = 0; /* trailing comment detected */
|
|
|
|
else if (s && q > l2
|
|
|
|
&& ((isid(q[-1]) && isid(c))
|
|
|
|
// keep space after macro name
|
|
|
|
|| (q >= l2 + 2
|
|
|
|
&& l2[0] == '#'
|
|
|
|
&& l2[1] == 'd'
|
|
|
|
&& f < 2 && !e
|
|
|
|
)))
|
|
|
|
*q++ = ' ', ++f;
|
|
|
|
s = 0;
|
|
|
|
|
|
|
|
if (c == '(')
|
|
|
|
++e;
|
|
|
|
if (c == ')')
|
|
|
|
--e;
|
|
|
|
if (c == '\\' || c == '\"')
|
|
|
|
*q++ = '\\';
|
|
|
|
*q++ = c;
|
|
|
|
if (c == 0)
|
|
|
|
break;
|
|
|
|
p0 = p;
|
|
|
|
}
|
|
|
|
/* output with quotes */
|
|
|
|
fprintf(op, " \"%s\\n\"%s\n", l2, p0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
fclose(op);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
/* get some information from the host compiler for configure */
|
|
|
|
|
2023-09-07 04:21:15 +08:00
|
|
|
#elif 1
|
|
|
|
|
2013-02-14 13:53:07 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2019-06-25 20:43:24 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <fcntl.h>
|
2023-09-07 04:21:15 +08:00
|
|
|
#include <io.h>
|
|
|
|
int _CRT_glob = 0;
|
2019-06-25 20:43:24 +08:00
|
|
|
#endif
|
|
|
|
|
2013-02-14 22:39:35 +08:00
|
|
|
/* Define architecture */
|
2017-02-18 16:55:34 +08:00
|
|
|
#if defined(__i386__) || defined _M_IX86
|
2013-02-14 22:39:35 +08:00
|
|
|
# define TRIPLET_ARCH "i386"
|
2017-02-18 16:55:34 +08:00
|
|
|
#elif defined(__x86_64__) || defined _M_AMD64
|
2013-02-14 22:39:35 +08:00
|
|
|
# define TRIPLET_ARCH "x86_64"
|
|
|
|
#elif defined(__arm__)
|
|
|
|
# define TRIPLET_ARCH "arm"
|
2015-02-14 02:58:31 +08:00
|
|
|
#elif defined(__aarch64__)
|
|
|
|
# define TRIPLET_ARCH "aarch64"
|
2019-06-22 12:13:10 +08:00
|
|
|
#elif defined(__riscv) && defined(__LP64__)
|
|
|
|
# define TRIPLET_ARCH "riscv64"
|
2013-02-14 22:39:35 +08:00
|
|
|
#else
|
|
|
|
# define TRIPLET_ARCH "unknown"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Define OS */
|
|
|
|
#if defined (__linux__)
|
|
|
|
# define TRIPLET_OS "linux"
|
|
|
|
#elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
|
|
|
|
# define TRIPLET_OS "kfreebsd"
|
2021-01-17 15:49:37 +08:00
|
|
|
#elif defined(__NetBSD__)
|
|
|
|
# define TRIPLET_OS "netbsd"
|
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
# define TRIPLET_OS "openbsd"
|
2020-06-21 16:58:36 +08:00
|
|
|
#elif defined(_WIN32)
|
2017-02-18 16:55:34 +08:00
|
|
|
# define TRIPLET_OS "win32"
|
2020-06-21 16:58:36 +08:00
|
|
|
#elif defined(__APPLE__)
|
|
|
|
# define TRIPLET_OS "darwin"
|
2013-02-14 22:39:35 +08:00
|
|
|
#elif !defined (__GNU__)
|
|
|
|
# define TRIPLET_OS "unknown"
|
|
|
|
#endif
|
|
|
|
|
2022-05-18 04:28:32 +08:00
|
|
|
#if defined __ANDROID__
|
|
|
|
# define ABI_PREFIX "android"
|
|
|
|
#else
|
|
|
|
# define ABI_PREFIX "gnu"
|
|
|
|
#endif
|
|
|
|
|
2013-02-14 22:39:35 +08:00
|
|
|
/* Define calling convention and ABI */
|
2013-02-14 23:40:16 +08:00
|
|
|
#if defined (__ARM_EABI__)
|
|
|
|
# if defined (__ARM_PCS_VFP)
|
2022-05-18 04:28:32 +08:00
|
|
|
# define TRIPLET_ABI ABI_PREFIX"eabihf"
|
2013-02-14 23:40:16 +08:00
|
|
|
# else
|
2022-05-18 04:28:32 +08:00
|
|
|
# define TRIPLET_ABI ABI_PREFIX"eabi"
|
2013-02-14 23:40:16 +08:00
|
|
|
# endif
|
|
|
|
#else
|
2022-05-18 04:28:32 +08:00
|
|
|
# define TRIPLET_ABI ABI_PREFIX
|
2013-02-14 23:40:16 +08:00
|
|
|
#endif
|
2013-02-14 22:39:35 +08:00
|
|
|
|
2017-02-18 16:55:34 +08:00
|
|
|
#if defined _WIN32
|
|
|
|
# define TRIPLET TRIPLET_ARCH "-" TRIPLET_OS
|
|
|
|
#elif defined __GNU__
|
2013-02-14 22:39:35 +08:00
|
|
|
# define TRIPLET TRIPLET_ARCH "-" TRIPLET_ABI
|
|
|
|
#else
|
|
|
|
# define TRIPLET TRIPLET_ARCH "-" TRIPLET_OS "-" TRIPLET_ABI
|
|
|
|
#endif
|
|
|
|
|
2013-02-14 13:53:07 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2019-06-25 20:43:24 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
_setmode(_fileno(stdout), _O_BINARY); /* don't translate \n to \r\n */
|
|
|
|
#endif
|
2013-02-14 13:53:07 +08:00
|
|
|
switch(argc == 2 ? argv[1][0] : 0) {
|
2020-06-27 23:22:04 +08:00
|
|
|
case 'b'://igendian
|
2013-02-14 22:39:35 +08:00
|
|
|
{
|
|
|
|
volatile unsigned foo = 0x01234567;
|
|
|
|
puts(*(unsigned char*)&foo == 0x67 ? "no" : "yes");
|
2013-02-14 13:53:07 +08:00
|
|
|
break;
|
2013-02-14 22:39:35 +08:00
|
|
|
}
|
2020-06-21 16:58:36 +08:00
|
|
|
#if defined(__clang__)
|
2020-06-27 23:22:04 +08:00
|
|
|
case 'm'://inor
|
2020-06-21 15:53:52 +08:00
|
|
|
printf("%d\n", __clang_minor__);
|
|
|
|
break;
|
2020-06-27 23:22:04 +08:00
|
|
|
case 'v'://ersion
|
2020-06-21 15:53:52 +08:00
|
|
|
printf("%d\n", __clang_major__);
|
|
|
|
break;
|
2020-06-21 16:58:36 +08:00
|
|
|
#elif defined(__TINYC__)
|
2020-06-27 23:22:04 +08:00
|
|
|
case 'v'://ersion
|
2017-02-18 16:55:34 +08:00
|
|
|
puts("0");
|
|
|
|
break;
|
2020-06-27 23:22:04 +08:00
|
|
|
case 'm'://inor
|
2017-02-18 16:55:34 +08:00
|
|
|
printf("%d\n", __TINYC__);
|
|
|
|
break;
|
2020-06-27 23:22:04 +08:00
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
case 'v'://ersion
|
|
|
|
puts("0");
|
|
|
|
break;
|
|
|
|
case 'm'://inor
|
|
|
|
printf("%d\n", _MSC_VER);
|
|
|
|
break;
|
2020-06-21 16:58:36 +08:00
|
|
|
#elif defined(__GNUC__) && defined(__GNUC_MINOR__)
|
|
|
|
/* GNU comes last as other compilers may add 'GNU' compatibility */
|
2020-06-27 23:22:04 +08:00
|
|
|
case 'm'://inor
|
2020-06-21 16:52:04 +08:00
|
|
|
printf("%d\n", __GNUC_MINOR__);
|
|
|
|
break;
|
2020-06-27 23:22:04 +08:00
|
|
|
case 'v'://ersion
|
2020-06-21 16:52:04 +08:00
|
|
|
printf("%d\n", __GNUC__);
|
|
|
|
break;
|
2013-02-14 22:39:35 +08:00
|
|
|
#else
|
2020-06-27 23:22:04 +08:00
|
|
|
case 'm'://inor
|
|
|
|
case 'v'://ersion
|
2013-02-14 13:53:07 +08:00
|
|
|
puts("0");
|
|
|
|
break;
|
|
|
|
#endif
|
2020-06-27 23:22:04 +08:00
|
|
|
case 't'://riplet
|
2013-02-14 22:39:35 +08:00
|
|
|
puts(TRIPLET);
|
2013-02-14 13:53:07 +08:00
|
|
|
break;
|
2020-06-27 23:22:04 +08:00
|
|
|
case 'c'://ompiler
|
2020-06-22 16:12:18 +08:00
|
|
|
#if defined(__clang__)
|
|
|
|
puts("clang");
|
|
|
|
#elif defined(__TINYC__)
|
|
|
|
puts("tcc");
|
2020-06-27 23:22:04 +08:00
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
puts("msvc");
|
2020-06-22 16:19:09 +08:00
|
|
|
#elif defined(__GNUC__)
|
2020-06-22 16:12:18 +08:00
|
|
|
puts("gcc");
|
|
|
|
#else
|
|
|
|
puts("unknown");
|
|
|
|
#endif
|
|
|
|
break;
|
2017-02-18 16:55:34 +08:00
|
|
|
default:
|
2013-02-14 13:53:07 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2020-12-23 04:10:22 +08:00
|
|
|
|
|
|
|
/* ----------------------------------------------------------------------- */
|
|
|
|
#endif
|