Skip to content

Commit 2b5a4c6

Browse files
committed
added mkdata.pl generator for cross-compiling, fixes #31
1 parent 912140c commit 2b5a4c6

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

CMakeLists.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@ file(GLOB RESOURCES
3232
htdocs/index.html
3333
)
3434

35-
add_executable(mkdata htdocs/mkdata.c)
36-
set(MKDATA_EXE $<TARGET_FILE:mkdata>)
35+
if(CMAKE_CROSSCOMPILING)
36+
set(MKDATA_EXE ${PROJECT_SOURCE_DIR}/htdocs/mkdata.pl)
37+
else()
38+
set(MKDATA_EXE $<TARGET_FILE:mkdata>)
39+
set(MKDATA_TARGET mkdata)
40+
add_executable(mkdata htdocs/mkdata.c)
41+
endif()
3742

3843
add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/assets.c
39-
COMMAND ${MKDATA_EXE} ${RESOURCES} > ${PROJECT_BINARY_DIR}/assets.c
40-
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
41-
DEPENDS ${RESOURCES} mkdata
44+
COMMAND ${MKDATA_EXE} ${RESOURCES} > ${PROJECT_BINARY_DIR}/assets.c
45+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
46+
DEPENDS ${RESOURCES} ${MKDATA_TARGET}
4247
)
4348

4449
set(SOURCES

htdocs/mkdata.pl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/perl
2+
# This program is used to embed arbitrary data into a C binary. It takes
3+
# a list of files as an input, and produces a .c data file that contains
4+
# contents of all these files as collection of char arrays.
5+
#
6+
# Usage: perl <this_file> <file1> [file2, ...] > embedded_data.c
7+
8+
use File::Basename;
9+
10+
%mimetypes = (
11+
js => 'application/javascript',
12+
css => 'text/css',
13+
ico => 'image/vnd.microsoft.icon',
14+
woff => 'application/font-woff',
15+
ttf => 'application/x-font-ttf',
16+
eot => 'application/octet-stream',
17+
svg => 'image/svg+xml',
18+
html => 'text/html'
19+
);
20+
21+
foreach my $i (0 .. $#ARGV) {
22+
open FD, '<:raw', $ARGV[$i] or die "Cannot open $ARGV[$i]: $!\n";
23+
printf("static const unsigned char v%d[] = {", $i);
24+
my $byte;
25+
my $j = 0;
26+
while (read(FD, $byte, 1)) {
27+
if (($j % 12) == 0) {
28+
print "\n";
29+
}
30+
printf ' %#04x,', ord($byte);
31+
$j++;
32+
}
33+
print " 0x00\n};\n";
34+
close FD;
35+
}
36+
37+
print <<EOS;
38+
#include <stddef.h>
39+
#include <string.h>
40+
#include <sys/types.h>
41+
#include "src/http_server.h"
42+
43+
static const struct embedded_file embedded_files[] = {
44+
EOS
45+
46+
foreach my $i (0 .. $#ARGV) {
47+
my ($ext) = $ARGV[$i] =~ /([^.]+)$/;
48+
my $mime = $mimetypes{$ext};
49+
$ARGV[$i] =~ s/htdocs//;
50+
print " {\"$ARGV[$i]\", v$i, \"$mime\", sizeof(v$i) - 1},\n";
51+
}
52+
53+
print <<EOS;
54+
{NULL, NULL, NULL, 0}
55+
};
56+
57+
const struct embedded_file *find_embedded_file(const char *name) {
58+
const struct embedded_file *p;
59+
for (p = embedded_files; p->name != NULL; p++)
60+
if (!strcmp(p->name, name))
61+
return p;
62+
return NULL;
63+
}
64+
65+
EOS

0 commit comments

Comments
 (0)