release build now installs assets to prefix
authorAndrew Karpow <andy@ndyk.de>
Tue, 11 Nov 2014 20:13:11 +0000 (21:13 +0100)
committerAndrew Karpow <andy@ndyk.de>
Tue, 11 Nov 2014 20:13:11 +0000 (21:13 +0100)
if build with dynamic assets

CMakeLists.txt
htdocs/mkdata.c [deleted file]
htdocs/mkdata.pl [deleted file]
src/config.h.in
src/ympd.c
tools/mkdata.c [new file with mode: 0644]
tools/mkdata.pl [new file with mode: 0755]

index 4fb845b..d32f0d9 100644 (file)
@@ -5,9 +5,14 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/")
 set(CPACK_PACKAGE_VERSION_MAJOR "1")
 set(CPACK_PACKAGE_VERSION_MINOR "2")
 set(CPACK_PACKAGE_VERSION_PATCH "3")
+if(CMAKE_BUILD_TYPE MATCHES RELEASE)
+    set(ASSETS_PATH "${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}/htdocs")
+else()
+    set(ASSETS_PATH "${PROJECT_SOURCE_DIR}/htdocs")
+endif()
 
 option(WITH_MPD_HOST_CHANGE "Let users of the web frontend change the MPD Host" ON)
-option(WITH_DYNAMIC_SERVING "Serve file of ./htdocs dynamically (development)" OFF)
+option(WITH_DYNAMIC_ASSETS "Serve assets dynamically (e.g for development/packaging)" OFF)
 option(WITH_IPV6 "enable IPv6 support" ON)
 
 find_package(LibMPDClient REQUIRED)
@@ -18,46 +23,50 @@ include_directories(${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR})
 
 include(CheckCSourceCompiles)
 
-set(CMAKE_C_FLAGS "-std=gnu99 -Wall") 
+set(CMAKE_C_FLAGS "-std=gnu99 -Wall")
 set(CMAKE_C_FLAGS_DEBUG "-ggdb -pedantic")
 if(WITH_IPV6)
-       set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS NS_ENABLE_IPV6)
+    set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS NS_ENABLE_IPV6)
 endif()
 
 file(GLOB RESOURCES 
-       RELATIVE ${PROJECT_SOURCE_DIR}
-       htdocs/js/*
-       htdocs/assets/*
-       htdocs/css/*.min.css
-       htdocs/fonts/*
-       htdocs/index.html
-)
-
-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_TARGET}
+    RELATIVE ${PROJECT_SOURCE_DIR}
+    htdocs/js/*
+    htdocs/assets/*
+    htdocs/css/*.min.css
+    htdocs/fonts/*
+    htdocs/index.html
 )
 
 set(SOURCES
     src/ympd.c
-    src/http_server.c
     src/mpd_client.c
     src/mongoose.c
     src/json_encode.c
-    assets.c
 )
 
+if(NOT WITH_DYNAMIC_ASSETS)
+    if(CMAKE_CROSSCOMPILING)
+        set(MKDATA_EXE ${PROJECT_SOURCE_DIR}/tools/mkdata.pl)
+    else()
+        set(MKDATA_EXE $<TARGET_FILE:mkdata>)
+        set(MKDATA_TARGET mkdata)
+        add_executable(mkdata tools/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_TARGET}
+    )
+    list(APPEND SOURCES src/http_server.c assets.c)
+endif()
+
 add_executable(ympd ${SOURCES})
 target_link_libraries(ympd ${LIBMPDCLIENT_LIBRARY} ${CMAKE_THREAD_LIBS_INIT})
 
 install(TARGETS ympd DESTINATION bin)
 install(FILES ympd.1 DESTINATION ${CMAKE_INSTALL_PREFIX}/share/man/man1)
+if(WITH_DYNAMIC_ASSETS)
+    install(DIRECTORY htdocs DESTINATION share/${PROJECT_NAME})
+endif()
diff --git a/htdocs/mkdata.c b/htdocs/mkdata.c
deleted file mode 100644 (file)
index d22c8e7..0000000
+++ /dev/null
@@ -1,94 +0,0 @@
-/* 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;
-}
diff --git a/htdocs/mkdata.pl b/htdocs/mkdata.pl
deleted file mode 100755 (executable)
index c1bea88..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-#!/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
index 202d63a..08a5529 100644 (file)
@@ -22,8 +22,8 @@
 #define YMPD_VERSION_MAJOR ${CPACK_PACKAGE_VERSION_MAJOR}
 #define YMPD_VERSION_MINOR ${CPACK_PACKAGE_VERSION_MINOR}
 #define YMPD_VERSION_PATCH ${CPACK_PACKAGE_VERSION_PATCH}
-#define SRC_PATH "${PROJECT_SOURCE_DIR}/htdocs"
+#define SRC_PATH "${ASSETS_PATH}"
 #cmakedefine WITH_MPD_HOST_CHANGE
-#cmakedefine WITH_DYNAMIC_SERVING
+#cmakedefine WITH_DYNAMIC_ASSETS
 #endif
 
index 794301b..e974479 100644 (file)
@@ -51,7 +51,7 @@ static int server_callback(struct mg_connection *c, enum mg_event ev) {
                 else
                     return MG_TRUE;
             } else
-#ifdef WITH_DYNAMIC_SERVING
+#ifdef WITH_DYNAMIC_ASSETS
                 return MG_FALSE;
 #else
                 return callback_http(c);
@@ -73,7 +73,7 @@ int main(int argc, char **argv)
 
     atexit(bye);
     error_msg = mg_set_option(server, "listening_port", "8080");
-#ifdef WITH_DYNAMIC_SERVING
+#ifdef WITH_DYNAMIC_ASSETS
     mg_set_option(server, "document_root", SRC_PATH);
 #endif
 
diff --git a/tools/mkdata.c b/tools/mkdata.c
new file mode 100644 (file)
index 0000000..d22c8e7
--- /dev/null
@@ -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;
+}
diff --git a/tools/mkdata.pl b/tools/mkdata.pl
new file mode 100755 (executable)
index 0000000..c1bea88
--- /dev/null
@@ -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