From 88d24816aa57e084be090303fda1e5840b703aa1 Mon Sep 17 00:00:00 2001
From: Vivien Kraus <vivien@planete-kraus.eu>
Date: Mon, 2 Aug 2021 16:07:08 +0200
Subject: [PATCH] gnu: Add jsonnet.
* gnu/packages/cpp.scm (jsonnet): New variable.
---
Makefile.am | 5 +-
.../aux-files/jsonnet-md5/CMakeLists.txt | 5 ++
gnu/packages/aux-files/jsonnet-md5/md5.cpp | 25 +++++++++
gnu/packages/aux-files/jsonnet-md5/md5.h | 8 +++
gnu/packages/cpp.scm | 53 +++++++++++++++++++
5 files changed, 95 insertions(+), 1 deletion(-)
create mode 100644 gnu/packages/aux-files/jsonnet-md5/CMakeLists.txt
create mode 100644 gnu/packages/aux-files/jsonnet-md5/md5.cpp
create mode 100644 gnu/packages/aux-files/jsonnet-md5/md5.h
@@ -390,7 +390,10 @@ AUX_FILES = \
gnu/packages/aux-files/linux-libre/4.4-i686.conf \
gnu/packages/aux-files/linux-libre/4.4-x86_64.conf \
gnu/packages/aux-files/pack-audit.c \
- gnu/packages/aux-files/run-in-namespace.c
+ gnu/packages/aux-files/run-in-namespace.c \
+ gnu/packages/aux-files/jsonnet-md5/CMakeLists.txt \
+ gnu/packages/aux-files/jsonnet-md5/md5.cpp \
+ gnu/packages/aux-files/jsonnet-md5/md5.h
# Templates, examples.
EXAMPLES = \
new file mode 100644
@@ -0,0 +1,5 @@
+add_library(md5 STATIC md5.cpp md5.h)
+find_package(PkgConfig REQUIRED)
+pkg_check_modules(NETTLE REQUIRED nettle)
+target_link_libraries(md5 ${NETTLE_LIBRARIES})
+target_include_directories(md5 PUBLIC ${NETTLE_INCLUDE_DIRS})
new file mode 100644
@@ -0,0 +1,25 @@
+#include "md5.h"
+#include <nettle/md5.h>
+#include <nettle/base16.h>
+#include <string>
+#include <vector>
+
+std::string
+md5 (const std::string str)
+{
+ // Convert str to a byte array
+ std::vector<uint8_t> input (str.begin (), str.end ());
+
+ // Compute the digest
+ struct md5_ctx nettle_ctx;
+ md5_init (&nettle_ctx);
+ md5_update (&nettle_ctx, input.size (), input.data ());
+ uint8_t digest[MD5_DIGEST_SIZE];
+ md5_digest (&nettle_ctx, MD5_DIGEST_SIZE, digest);
+
+ // Encode it to base16
+ std::vector<char> encoded (BASE16_ENCODE_LENGTH (MD5_DIGEST_SIZE));
+ base16_encode_update (encoded.data (), MD5_DIGEST_SIZE, digest);
+ std::string final_digest (encoded.begin (), encoded.end ());
+ return final_digest;
+}
new file mode 100644
@@ -0,0 +1,8 @@
+#ifndef BZF_MD5_H
+#define BZF_MD5_H
+#include <string>
+
+// Return the hexadecimal digest.
+std::string md5 (const std::string str);
+
+#endif
@@ -45,6 +45,7 @@
#:use-module (guix build-system gnu)
#:use-module (guix build-system python)
#:use-module (guix modules)
+ #:use-module (guix gexp)
#:use-module (gnu packages)
#:use-module (gnu packages autotools)
#:use-module (gnu packages boost)
@@ -63,6 +64,7 @@
#:use-module (gnu packages llvm)
#:use-module (gnu packages logging)
#:use-module (gnu packages maths)
+ #:use-module (gnu packages nettle)
#:use-module (gnu packages onc-rpc)
#:use-module (gnu packages perl)
#:use-module (gnu packages pkg-config)
@@ -1211,3 +1213,54 @@ of reading and writing XML.")
;; incompatible with the GPL v2. Refer to the file named FLOSSE for the
;; details.
(license license:gpl2+)))
+
+(define-public jsonnet
+ (package
+ (name "jsonnet")
+ (version "0.17.0")
+ (source
+ (origin
+ (method git-fetch)
+ (uri (git-reference
+ (url "https://github.com/google/jsonnet")
+ (commit (string-append "v" version))))
+ (file-name (git-file-name name version))
+ (sha256
+ (base32 "1ddz14699v5lqx3dh0mb7hfffr6fk5zhmzn3z8yxkqqvriqnciim"))
+ (modules '((guix build utils)))
+ (snippet
+ #~(begin
+ (delete-file-recursively "third_party")
+ (delete-file-recursively "doc/third_party")
+ (substitute*
+ '("core/vm.cpp")
+ (("#include \"json.hpp\"") "#include <nlohmann/json.hpp>"))
+ (mkdir-p "third_party/md5")
+ (copy-file #+(local-file
+ (search-auxiliary-file "jsonnet-md5/CMakeLists.txt"))
+ "third_party/md5/CMakeLists.txt")
+ (copy-file #+(local-file
+ (search-auxiliary-file "jsonnet-md5/md5.h"))
+ "third_party/md5/md5.h")
+ (copy-file #+(local-file
+ (search-auxiliary-file "jsonnet-md5/md5.cpp"))
+ "third_party/md5/md5.cpp")))))
+ (build-system cmake-build-system)
+ (arguments
+ `(#:configure-flags '("-DUSE_SYSTEM_GTEST=ON" "-DUSE_SYSTEM_JSON=ON")))
+ (propagated-inputs
+ '())
+ (native-inputs
+ `(("googletest" ,googletest)
+ ("pkg-config" ,pkg-config)))
+ (inputs
+ `(("json-modern-cxx" ,json-modern-cxx)
+ ;; jsonnet uses a md5 implementation claiming to be from
+ ;; https://www.bzflag.org/, but they don’t use it anymore. We
+ ;; replace it with md5 from nettle.
+ ("nettle" ,nettle)))
+ (home-page "https://jsonnet.org/")
+ (synopsis "Data templating language")
+ (description "Jsonnet is a templating language extending JSON
+syntax with variables, conditions, functions and more.")
+ (license license:asl2.0)))
--
2.32.0