Skip to content

Commit

Permalink
added mkdata.pl generator for cross-compiling, fixes #31
Browse files Browse the repository at this point in the history
  • Loading branch information
notandy committed Oct 19, 2014
1 parent 912140c commit 2b5a4c6
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
15 changes: 10 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ file(GLOB RESOURCES
htdocs/index.html
)

add_executable(mkdata htdocs/mkdata.c)
set(MKDATA_EXE $<TARGET_FILE:mkdata>)
if(CMAKE_CROSSCOMPILING)
set(MKDATA_EXE ${PROJECT_SOURCE_DIR}/htdocs/mkdata.pl)
else()
set(MKDATA_EXE $<TARGET_FILE:mkdata>)
set(MKDATA_TARGET mkdata)
add_executable(mkdata htdocs/mkdata.c)
endif()

add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/assets.c
COMMAND ${MKDATA_EXE} ${RESOURCES} > ${PROJECT_BINARY_DIR}/assets.c
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
DEPENDS ${RESOURCES} mkdata
COMMAND ${MKDATA_EXE} ${RESOURCES} > ${PROJECT_BINARY_DIR}/assets.c
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
DEPENDS ${RESOURCES} ${MKDATA_TARGET}
)

set(SOURCES
Expand Down
65 changes: 65 additions & 0 deletions htdocs/mkdata.pl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2b5a4c6

Please sign in to comment.