mirror of
https://github.com/SuperBFG7/ympd
synced 2025-10-26 19:37:41 +00:00
release build now installs assets to prefix
if build with dynamic assets
This commit is contained in:
94
tools/mkdata.c
Normal file
94
tools/mkdata.c
Normal file
@@ -0,0 +1,94 @@
|
||||
/* This program is used to embed arbitrary data into a C binary. It takes
|
||||
* a list of files as an input, and produces a .c data file that contains
|
||||
* contents of all these files as collection of char arrays.
|
||||
*
|
||||
* Usage: ./mkdata <this_file> <file1> [file2, ...] > embedded_data.c
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
const char* header =
|
||||
"#include <stddef.h>\n"
|
||||
"#include <string.h>\n"
|
||||
"#include <sys/types.h>\n"
|
||||
"#include \"src/http_server.h\"\n"
|
||||
"\n"
|
||||
"static const struct embedded_file embedded_files[] = {\n";
|
||||
|
||||
const char* footer =
|
||||
" {NULL, NULL, NULL, 0}\n"
|
||||
"};\n"
|
||||
"\n"
|
||||
"const struct embedded_file *find_embedded_file(const char *name) {\n"
|
||||
" const struct embedded_file *p;\n"
|
||||
" for (p = embedded_files; p->name != NULL; p++)\n"
|
||||
" if (!strcmp(p->name, name))\n"
|
||||
" return p;\n"
|
||||
" return NULL;\n"
|
||||
"}\n";
|
||||
|
||||
static const char* get_mime(char* filename)
|
||||
{
|
||||
const char *extension = strrchr(filename, '.');
|
||||
if(!strcmp(extension, ".js"))
|
||||
return "application/javascript";
|
||||
if(!strcmp(extension, ".css"))
|
||||
return "text/css";
|
||||
if(!strcmp(extension, ".ico"))
|
||||
return "image/vnd.microsoft.icon";
|
||||
if(!strcmp(extension, ".woff"))
|
||||
return "application/font-woff";
|
||||
if(!strcmp(extension, ".ttf"))
|
||||
return "application/x-font-ttf";
|
||||
if(!strcmp(extension, ".eot"))
|
||||
return "application/octet-stream";
|
||||
if(!strcmp(extension, ".svg"))
|
||||
return "image/svg+xml";
|
||||
if(!strcmp(extension, ".html"))
|
||||
return "text/html";
|
||||
return "text/plain";
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i, j, buf;
|
||||
FILE *fd;
|
||||
|
||||
if(argc <= 1)
|
||||
err(EXIT_FAILURE, "Usage: ./%s <this_file> <file1> [file2, ...] > embedded_data.c", argv[0]);
|
||||
|
||||
for(i = 1; i < argc; i++)
|
||||
{
|
||||
fd = fopen(argv[i], "r");
|
||||
if(!fd)
|
||||
err(EXIT_FAILURE, "%s", argv[i]);
|
||||
|
||||
printf("static const unsigned char v%d[] = {", i);
|
||||
|
||||
j = 0;
|
||||
while((buf = fgetc(fd)) != EOF)
|
||||
{
|
||||
if(!(j % 12))
|
||||
putchar('\n');
|
||||
|
||||
printf(" %#04x, ", buf);
|
||||
j++;
|
||||
}
|
||||
printf(" 0x00\n};\n\n");
|
||||
fclose(fd);
|
||||
}
|
||||
fputs(header, stdout);
|
||||
|
||||
for(i = 1; i < argc; i++)
|
||||
{
|
||||
printf(" {\"%s\", v%d, \"%s\", sizeof(v%d) - 1}, \n",
|
||||
argv[i]+6, i, get_mime(argv[i]), i);
|
||||
}
|
||||
|
||||
fputs(footer, stdout);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
65
tools/mkdata.pl
Executable file
65
tools/mkdata.pl
Executable file
@@ -0,0 +1,65 @@
|
||||
#!/usr/bin/perl
|
||||
# This program is used to embed arbitrary data into a C binary. It takes
|
||||
# a list of files as an input, and produces a .c data file that contains
|
||||
# contents of all these files as collection of char arrays.
|
||||
#
|
||||
# Usage: perl <this_file> <file1> [file2, ...] > embedded_data.c
|
||||
|
||||
use File::Basename;
|
||||
|
||||
%mimetypes = (
|
||||
js => 'application/javascript',
|
||||
css => 'text/css',
|
||||
ico => 'image/vnd.microsoft.icon',
|
||||
woff => 'application/font-woff',
|
||||
ttf => 'application/x-font-ttf',
|
||||
eot => 'application/octet-stream',
|
||||
svg => 'image/svg+xml',
|
||||
html => 'text/html'
|
||||
);
|
||||
|
||||
foreach my $i (0 .. $#ARGV) {
|
||||
open FD, '<:raw', $ARGV[$i] or die "Cannot open $ARGV[$i]: $!\n";
|
||||
printf("static const unsigned char v%d[] = {", $i);
|
||||
my $byte;
|
||||
my $j = 0;
|
||||
while (read(FD, $byte, 1)) {
|
||||
if (($j % 12) == 0) {
|
||||
print "\n";
|
||||
}
|
||||
printf ' %#04x,', ord($byte);
|
||||
$j++;
|
||||
}
|
||||
print " 0x00\n};\n";
|
||||
close FD;
|
||||
}
|
||||
|
||||
print <<EOS;
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include "src/http_server.h"
|
||||
|
||||
static const struct embedded_file embedded_files[] = {
|
||||
EOS
|
||||
|
||||
foreach my $i (0 .. $#ARGV) {
|
||||
my ($ext) = $ARGV[$i] =~ /([^.]+)$/;
|
||||
my $mime = $mimetypes{$ext};
|
||||
$ARGV[$i] =~ s/htdocs//;
|
||||
print " {\"$ARGV[$i]\", v$i, \"$mime\", sizeof(v$i) - 1},\n";
|
||||
}
|
||||
|
||||
print <<EOS;
|
||||
{NULL, NULL, NULL, 0}
|
||||
};
|
||||
|
||||
const struct embedded_file *find_embedded_file(const char *name) {
|
||||
const struct embedded_file *p;
|
||||
for (p = embedded_files; p->name != NULL; p++)
|
||||
if (!strcmp(p->name, name))
|
||||
return p;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
EOS
|
||||
Reference in New Issue
Block a user