From 638384c10ff1b7b1233bec1d62db3e82cffeaafc Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 19 Jun 2017 13:42:40 +0200 Subject: Add '-s' option to hexdump script to dump arrays using string literals. If the compiler supports string literals long enough, this will speed up compilation and reduce compiler memory use by a lot. Enough so that we don't need to use the .incbin asm directive. Gcc and Clang both support long string literals, so use the '-s' option for generated font files by default in the Makefile. --- scripts/hexdump.c | 71 +++++++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 39 deletions(-) (limited to 'scripts') diff --git a/scripts/hexdump.c b/scripts/hexdump.c index c75e97bc..18f6ac15 100644 --- a/scripts/hexdump.c +++ b/scripts/hexdump.c @@ -4,23 +4,31 @@ #include #include +static int zero, string; + static int hexdump(FILE *fo, FILE *fi) { int c, n; + if (string) + fprintf(fo, "\""); + n = 0; c = fgetc(fi); while (c != -1) { - n += fprintf(fo, "%d,", c); + n += fprintf(fo, string ? "\\x%02x" : "%d,", c); if (n > 72) { - fprintf(fo, "\n"); + fprintf(fo, string ? "\"\n\"" : "\n"); n = 0; } c = fgetc(fi); } + if (string) + fprintf(fo, "\"\n"); + return n; } @@ -32,38 +40,36 @@ main(int argc, char **argv) char filename[256]; char *basename; char *p; - int i, size; - int zero; + int i, optind, size; if (argc < 3) { - fprintf(stderr, "usage: hexdump [-0] output.c input.dat\n"); + fprintf(stderr, "usage: hexdump [-0] [-s] output.c input.dat\n"); return 1; } zero = 0; - if (!strcmp(argv[1], "-0")) + string = 0; + optind = 1; + + if (!strcmp(argv[optind], "-0")) { + ++optind; zero = 1; + } - fo = fopen(argv[zero+1], "wb"); - if (!fo) - { - fprintf(stderr, "hexdump: could not open output file '%s'\n", argv[zero+1]); - return 1; + if (!strcmp(argv[optind], "-s")) { + ++optind; + string = 1; } - if (!zero) + fo = fopen(argv[optind], "wb"); + if (!fo) { - fprintf(fo, "#ifndef __STRICT_ANSI__\n"); - fprintf(fo, "#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)\n"); - fprintf(fo, "#if !defined(__ICC) && !defined(__ANDROID__)\n"); - fprintf(fo, "#define HAVE_INCBIN\n"); - fprintf(fo, "#endif\n"); - fprintf(fo, "#endif\n"); - fprintf(fo, "#endif\n"); + fprintf(stderr, "hexdump: could not open output file '%s'\n", argv[optind]); + return 1; } - for (i = zero+2; i < argc; i++) + for (i = optind+1; i < argc; i++) { fi = fopen(argv[i], "rb"); if (!fi) @@ -100,31 +106,18 @@ main(int argc, char **argv) size = ftell(fi); fseek(fi, 0, SEEK_SET); - if (!zero) - { - fprintf(fo, "\n#ifdef HAVE_INCBIN\n"); - fprintf(fo, "const int fz_%s_size = %d;\n", filename, size); - fprintf(fo, "extern const char fz_%s[];\n", filename); - fprintf(fo, "asm(\".section .rodata\");\n"); - fprintf(fo, "asm(\".global fz_%s\");\n", filename); - fprintf(fo, "asm(\".type fz_%s STT_OBJECT\");\n", filename); - fprintf(fo, "asm(\".size fz_%s, %d\");\n", filename, size); - fprintf(fo, "asm(\".balign 64\");\n"); - fprintf(fo, "asm(\"fz_%s:\");\n", filename); - fprintf(fo, "asm(\".incbin \\\"%s\\\"\");\n", argv[i]); - fprintf(fo, "#else\n"); - } - fprintf(fo, "const int fz_%s_size = %d;\n", filename, size); - fprintf(fo, "const char fz_%s[] = {\n", filename); + fprintf(fo, "const int fz_%s_size = %d;\n", filename, size + zero); + fprintf(fo, "const char fz_%s[] =", filename); + fprintf(fo, string ? "\n" : " {\n"); hexdump(fo, fi); if (!zero) { - fprintf(fo, "};\n"); - fprintf(fo, "#endif\n"); + fprintf(fo, string ? ";\n" : "};\n"); } else { - fprintf(fo, "0};\n"); /* zero-terminate so we can hexdump text files into C strings */ + /* zero-terminate so we can hexdump text files into C strings */ + fprintf(fo, string ? ";\n" : "0};\n"); } fclose(fi); -- cgit v1.2.3