changeset 47:7097a91b08a8

Tools: add mlk-bcc tool, closes #596
author David Demelier <markand@malikania.fr>
date Wed, 07 Dec 2016 20:51:19 +0100
parents b0593a3e2ca8
children 3be179ba3226
files CMakeLists.txt cmake/MalikaniaFunctions.cmake tools/CMakeLists.txt tools/bcc/CMakeLists.txt tools/bcc/main.cpp
diffstat 5 files changed, 160 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Sun Dec 04 21:26:18 2016 +0100
+++ b/CMakeLists.txt	Wed Dec 07 20:51:19 2016 +0100
@@ -47,6 +47,7 @@
 find_package(OpenSSL REQUIRED)
 find_package(ZIP REQUIRED)
 
+add_subdirectory(tools)
 add_subdirectory(extern)
 add_subdirectory(database)
 add_subdirectory(docs)
--- a/cmake/MalikaniaFunctions.cmake	Sun Dec 04 21:26:18 2016 +0100
+++ b/cmake/MalikaniaFunctions.cmake	Wed Dec 07 20:51:19 2016 +0100
@@ -21,6 +21,29 @@
 # ---------------------------------------------------------
 # The following macros are available:
 #
+# malikania_build_assets
+# ----------------------
+#
+# malikania_build_assets(inputs outputs)
+#
+# Create binary data as C++ arrays for inclusion in source code. Static assets
+# increases the executable size so use this when really needed.
+#
+# The macro iterates over the input data which can be any files and generates
+# outputs file in the form CMAKE_CURRENT_BINARY_DIR/assets/basename.cpp.
+#
+# The macro removes the extension from the filename and create a static array
+# with that name, thus you need to specify a filename that is compatible with
+# C++ identifiers!
+#
+# Correct:
+#   - myfile.png
+#   - startup_logo.png
+#
+# Incorrect:
+#   - useful-code.ogg
+#   - archive.tar.xz
+#
 # malikania_define_executable
 # ---------------------------
 #
@@ -41,6 +64,7 @@
 # malikania_create_library(
 #    TARGET            The target name
 #    SOURCES           The sources
+#    ASSETS            (Optional) Additional assets files
 #    FLAGS             (Optional) List of flags
 #    PRIVATE_INCLUDES  (Optional) List of includes only for building the library
 #    PUBLIC_INCLUDES   (Optional) List of public includes to share with the library users
@@ -121,6 +145,23 @@
     endforeach ()
 endfunction()
 
+macro(malikania_build_assets inputs outputs)
+    file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/assets)
+
+    foreach (in ${inputs})
+        get_filename_component(basename ${in} NAME_WE)
+        set(out ${CMAKE_CURRENT_BINARY_DIR}/assets/${basename}.cpp)
+        add_custom_command(
+            OUTPUT ${out}
+            COMMENT "Generating binary data from ${in}"
+            DEPENDS ${in}
+            COMMAND
+                $<TARGET_FILE:mlk-bcc> ${basename} ${in} ${out}
+        )
+        list(APPEND ${outputs} ${out})
+    endforeach ()
+endmacro()
+
 function(malikania_define_executable)
     set(singleArgs TARGET)
     set(multiArgs SOURCES FLAGS INCLUDES LIBRARIES)
@@ -138,13 +179,17 @@
 
 function(malikania_create_library)
     set(singleArgs TARGET)
-    set(multiArgs SOURCES FLAGS PRIVATE_INCLUDES PUBLIC_INCLUDES LIBRARIES)
+    set(multiArgs ASSETS SOURCES FLAGS PRIVATE_INCLUDES PUBLIC_INCLUDES LIBRARIES)
     set(mandatory TARGET SOURCES)
 
     cmake_parse_arguments(LIB "" "${singleArgs}" "${multiArgs}" ${ARGN})
     check_args(LIB ${mandatory})
 
-    add_library(${LIB_TARGET} SHARED ${LIB_SOURCES})
+    # Enable assets for libraries.
+    malikania_build_assets("${LIB_ASSETS}" assets)
+
+    add_library(${LIB_TARGET} SHARED ${LIB_SOURCES} ${assets})
+    target_include_directories(${LIB_TARGET} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/assets)
 
     # Remove lib suffix to avoid conflict with client and libclient targets
     set_target_properties(
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/CMakeLists.txt	Wed Dec 07 20:51:19 2016 +0100
@@ -0,0 +1,19 @@
+#
+# CMakeLists.txt -- CMake build system for malikania
+#
+# Copyright (c) 2013-2016 Malikania Authors
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+add_subdirectory(bcc)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/bcc/CMakeLists.txt	Wed Dec 07 20:51:19 2016 +0100
@@ -0,0 +1,20 @@
+#
+# CMakeLists.txt -- CMake build system for malikania
+#
+# Copyright (c) 2013-2016 Malikania Authors
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+project(mlk-bcc)
+add_executable(mlk-bcc main.cpp)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/bcc/main.cpp	Wed Dec 07 20:51:19 2016 +0100
@@ -0,0 +1,73 @@
+/*
+ * main.cpp -- create binary data from assets
+ *
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <cerrno>
+#include <cstring>
+#include <fstream>
+#include <iomanip>
+#include <iostream>
+#include <iterator>
+
+int main(int argc, char** argv)
+{
+    -- argc;
+    ++ argv;
+
+    if (argc < 3) {
+        std::cerr << "usage mlk-bcc variable input output" << std::endl;
+        return 1;
+    }
+
+    std::ifstream input(argv[1], std::ifstream::in | std::ifstream::binary);
+
+    if (!input) {
+        std::cerr << argv[1] << ": " << std::strerror(errno) << std::endl;
+        return 1;
+    }
+
+    std::ofstream output(argv[2], std::ofstream::out | std::ofstream::trunc);
+
+    if (!output) {
+        std::cerr << argv[2] << ": " << std::strerror(errno) << std::endl;
+        return 1;
+    }
+
+    std::istreambuf_iterator<char> it(input);
+    std::istreambuf_iterator<char> end;
+
+    output << "const char " << argv[0] << "[] = {\n";
+
+    for (int i = 0; it != end; ) {
+        if (i == 0) {
+            output << "    ";
+        }
+
+        output << std::setw(4) << static_cast<int>(*it++);
+
+        if (it != end) {
+            output << ",";
+
+            if (i++ == 11) {
+                i = 0;
+                output << "\n";
+            }
+        }
+    }
+
+    output << "\n};\n";
+}