diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2017-06-19 13:42:40 +0200 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2017-06-22 15:46:56 +0100 |
commit | 638384c10ff1b7b1233bec1d62db3e82cffeaafc (patch) | |
tree | 7b95a78ccddbe4209834631bcc168cbff54f7768 | |
parent | e709f64df191609e83a497cae94ff59bcf32a239 (diff) | |
download | mupdf-638384c10ff1b7b1233bec1d62db3e82cffeaafc.tar.xz |
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.
-rw-r--r-- | Makefile | 10 | ||||
-rw-r--r-- | scripts/hexdump.c | 71 |
2 files changed, 37 insertions, 44 deletions
@@ -197,15 +197,15 @@ FONT_GEN := $(FONT_GEN_DROID) $(FONT_GEN_NOTO) $(FONT_GEN_HAN) $(FONT_GEN_URW) $ FONT_OBJ := $(FONT_GEN:%.c=$(OUT)/%.o) generated/%.c : resources/fonts/droid/%.ttf $(HEXDUMP_EXE) | generated - $(QUIET_GEN) $(HEXDUMP_EXE) $@ $< + $(QUIET_GEN) $(HEXDUMP_EXE) -s $@ $< generated/%.c : resources/fonts/noto/%.ttf $(HEXDUMP_EXE) | generated - $(QUIET_GEN) $(HEXDUMP_EXE) $@ $< + $(QUIET_GEN) $(HEXDUMP_EXE) -s $@ $< generated/%.c : resources/fonts/han/%.otf $(HEXDUMP_EXE) | generated - $(QUIET_GEN) $(HEXDUMP_EXE) $@ $< + $(QUIET_GEN) $(HEXDUMP_EXE) -s $@ $< generated/%.c : resources/fonts/urw/%.cff $(HEXDUMP_EXE) | generated - $(QUIET_GEN) $(HEXDUMP_EXE) $@ $< + $(QUIET_GEN) $(HEXDUMP_EXE) -s $@ $< generated/%.c : resources/fonts/sil/%.cff $(HEXDUMP_EXE) | generated - $(QUIET_GEN) $(HEXDUMP_EXE) $@ $< + $(QUIET_GEN) $(HEXDUMP_EXE) -s $@ $< $(FONT_OBJ) : $(FONT_GEN) $(FONT_GEN_DROID) : $(FONT_BIN_DROID) 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 <stdlib.h> #include <string.h> +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); |