Subject: bug#49823: closed (Re: bug#49823: [PATCH] gnu: Add jsonnet.)

From: [email-masked] (GNU bug Tracking System)
Date: Thu, 12 Aug 2021 07:55:02 +0000

Your bug report

#49823: [PATCH] gnu: Add jsonnet.

which was filed against the guix-patches package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
49823: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=49823
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#49823: [PATCH] gnu: Add jsonnet.
From: Ludovic Courtès <[email-masked]>
To: Vivien Kraus <[email-masked]>
Cc: [email-masked], Maxime Devos <[email-masked]>, Leo Prikler <[email-masked]>
Date: Thu, 12 Aug 2021 09:53:53 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi Vivien,

Vivien Kraus <[email-masked]> skribis:

> From 8ce461001ed14c3267d839334e4853feaed69b50 Mon Sep 17 00:00:00 2001
> From: Vivien Kraus <[email-masked]>
> Date: Mon, 2 Aug 2021 16:07:08 +0200
> Subject: [PATCH] gnu: Add jsonnet.
>
> * gnu/packages/cpp.scm (jsonnet): New variable.

Applied with the tweak below.

Thank you, and thanks for taking the time for the extra work!

Ludo’.

diff --git a/gnu/packages/cpp.scm b/gnu/packages/cpp.scm
index 870c02dac3..8c8109fb83 100644
--- a/gnu/packages/cpp.scm
+++ b/gnu/packages/cpp.scm
@@ -1232,16 +1232,13 @@ of reading and writing XML.")
             (rename-file "third_party/md5" ".md5")
             (delete-file-recursively "third_party")
             (delete-file-recursively "doc/third_party")
-            (substitute*
-                '("core/vm.cpp")
+            (substitute* '("core/vm.cpp")
               (("#include \"json.hpp\"") "#include <nlohmann/json.hpp>"))
             (mkdir "third_party")
             (rename-file ".md5" "third_party/md5")))))
     (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)))

Subject: [PATCH] gnu: Add jsonnet.
From: Vivien Kraus <[email-masked]>
To: [email-masked]
Date: Mon, 02 Aug 2021 17:01:07 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello,

Here is jsonnet. I don’t fully understand what it does, but thanks to
leoprikler, I know it is a dependency to package GraalJS, an
interpreter for JavaScript on the Java Virtual Machine.

For the sake of having "no dependencies", it bundles a custom
implementation of MD5. According to a comment in the associated
license, it is taken from the implementation of bzflag, but it does not
seem to use the same as of today.

I decided to use nettle, and add a few lines of C++ to shape it into
the required interface.

Best regards,

Vivien
From 7cfbec67974d03c6e7dd56e4e2ade95f38faad6a Mon Sep 17 00:00:00 2001
From: Vivien Kraus <[email-masked]>
Date: Mon, 2 Aug 2021 16:07:08 +0200
Subject: [PATCH] gnu: Add jsonnet.

* gnu/packages/cpp.scm (jsonnet): New variable.
---
 gnu/packages/cpp.scm | 103 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 103 insertions(+)

diff --git a/gnu/packages/cpp.scm b/gnu/packages/cpp.scm
index 42e9d50687..4e51c4a0cc 100644
--- a/gnu/packages/cpp.scm
+++ b/gnu/packages/cpp.scm
@@ -63,6 +63,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 +1212,105 @@ 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")
+           (call-with-output-file "third_party/md5/CMakeLists.txt"
+             (lambda (port)
+               (format port "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})
+")))
+           (call-with-output-file "third_party/md5/md5.h"
+             (lambda (port)
+               (format port "#ifndef BZF_MD5_H
+#define BZF_MD5_H
+#include <string>
+
+// Return the hexadecimal digest.
+std::string md5 (const std::string str);
+
+#endif
+")))
+           (call-with-output-file "third_party/md5/md5.cpp"
+             (lambda (port)
+               (format port "#include \"md5.h\"
+#include <nettle/md5.h>
+#include <nettle/base16.h>
+#include <string>
+#include <vector>
+
+#define OUTPUT_LENGTH BASE16_ENCODE_LENGTH (MD5_DIGEST_SIZE)
+
+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;
+}
+")))))))
+    (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 "The data templating language")
+    (description "A data templating language for app and tool developers:
+- Generate config data
+- Side-effect free
+- Organize, simplify, unify
+- Manage sprawling config
+
+A simple extension of JSON
+- Open source (Apache 2.0)
+- Familiar syntax
+- Reformatter, linter
+- Editor & IDE integrations
+- Formally specified
+")
+    (license license:asl2.0)))
-- 
2.32.0