Subject: Re: [PATCH 03/11] gnu: Add sajson.

From: Liliana Marie Prikler <[email-masked]>
Date: Tue, 04 Oct 2022 09:22:55 +0200

Am Montag, dem 03.10.2022 um 00:19 +0000 schrieb David Elsing:
> * gnu/packages/cpp.scm (sajson): New variable.
> ---
>  gnu/packages/cpp.scm                          | 63
> +++++++++++++++++++
>  .../patches/sajson-parse_flags-linking.patch  | 45 +++++++++++++
>  2 files changed, 108 insertions(+)
>  create mode 100644 gnu/packages/patches/sajson-parse_flags-
> linking.patch
> 
> diff --git a/gnu/packages/cpp.scm b/gnu/packages/cpp.scm
> index 65df317100..7bfa3d6cdf 100644
> --- a/gnu/packages/cpp.scm
> +++ b/gnu/packages/cpp.scm
> @@ -57,6 +57,7 @@ (define-module (gnu packages cpp)
>    #:use-module (guix build-system gnu)
>    #:use-module (guix build-system meson)
>    #:use-module (guix build-system python)
> +  #:use-module (guix build-system scons)
>    #:use-module (guix modules)
>    #:use-module (guix gexp)
>    #:use-module (gnu packages)
> @@ -1957,3 +1958,65 @@ (define-public pocketfft
>  which is an reimplementation of FFTPack with several advantages,
> such as
>  multidimensional algorithms, different datatypes and vector
> instructions.")
>        (license license:bsd-3))))
> +
> +(define-public sajson
> +  (let ((commit "ec644013e34f9984a3cc9ba568cab97a391db9cd")
> (revision "0"))
> +    (package
> +      (name "sajson")
> +      (version (git-version "0.0.0" revision commit))
Note that the swift FFI is advertised as 1.0 and there has been a
blogpost claiming it to be 1.0 in 2013.
> +      (source (origin
> +                (method git-fetch)
> +                (uri (git-reference
> +                      (url "https://github.com/chadaustin/sajson")
> +                      (commit commit)))
> +                (file-name (git-file-name name version))
> +                (patches
> +                 (search-patches "sajson-parse_flags-
> linking.patch"))
> +                (sha256
> +                 (base32
> +                 
> "0fjag27w7gvkc5pdhq3ad7yc09rabpzahndw1sgsg04ipznidmmq"))
> +                (modules '((guix build utils)))
> +                (snippet '(delete-file-recursively "third-party"))))
> +      (build-system scons-build-system)
> +      (arguments
> +       (list
> +        #:phases
> +        #~(modify-phases %standard-phases
> +            (add-after 'unpack 'disable-other-builds
> +              (lambda _
> +                (substitute* "SConstruct"
> +                  (("for name, tools in builds:")
> +                   "for name, tools in [('opt', [gcc, opt])]:"))
> +                #t))
> +            (add-after 'unpack 'use-external-unittest-cpp
> +              (lambda* (#:key inputs #:allow-other-keys)
> +                (substitute* "SConscript"
> +                  (("unittestpp_env\\.Library") "_dummy = ")
> +                  (("test_env = env.Clone\\(tools=\\[unittestpp,
> sajson\\]\\)")
> +                   (string-append
> +                    "test_env = env.Clone(tools=[sajson])\n"
> +                    "test_env.Append(CPPPATH='"
> +                    (search-input-directory inputs
> "/include/UnitTest++")
> +                    "', LIBPATH='"
> +                    (string-append #$(this-package-native-input
> "unittest-cpp")
> +                                   "/lib")
> +                    "', LIBS=['UnitTest++'])")))
> +                #t))
> +            (replace 'check
> +              (lambda* (#:key tests? #:allow-other-keys)
> +                (when tests?
> +                  (invoke "build/opt/test")
> +                  (invoke "build/opt/test_unsorted"))
> +                #t))
> +            (replace 'install
> +              (lambda _
> +                (let ((out (string-append #$output "/include")))
> +                  (install-file "include/sajson.h" out)
> +                  (install-file "include/sajson_ostream.h" out))
> +                #t)))))
You don't need the trailing #ts
> +      (native-inputs (list unittest-cpp))
> +      (home-page "https://github.com/chadaustin/sajson")
> +      (synopsis "Lightweight, extremely high-performance JSON parser
> for C++11")
> +      (description "sajson is a high-performance, in-place JSON
> parser
> +header-only library.")
Cut down on the ads.
> +      (license license:expat))))
> diff --git a/gnu/packages/patches/sajson-parse_flags-linking.patch
> b/gnu/packages/patches/sajson-parse_flags-linking.patch
> new file mode 100644
> index 0000000000..878706dc79
> --- /dev/null
> +++ b/gnu/packages/patches/sajson-parse_flags-linking.patch
> @@ -0,0 +1,45 @@
> +This patch is from the upstream pull request
> +https://github.com/chadaustin/sajson/pull/54.
> +It fixes linking with GCC.
> +
> +diff --git a/include/sajson.h b/include/sajson.h
> +index 8b4e05a..1bd045b 100644
> +--- a/include/sajson.h
> ++++ b/include/sajson.h
> +@@ -138,12 +138,17 @@ constexpr inline size_t make_element(tag t,
> size_t value) {
> + // header. This trick courtesy of Rich Geldreich's Purple JSON
> parser.
> + template <typename unused = void>
> + struct globals_struct {
> ++    static const unsigned char parse_flags[256];
> ++};
> ++typedef globals_struct<> globals;
> ++
> + // clang-format off
> + 
> +     // bit 0 (1) - set if: plain ASCII string character
> +     // bit 1 (2) - set if: whitespace
> +     // bit 4 (0x10) - set if: 0-9 e E .
> +-    constexpr static const uint8_t parse_flags[256] = {
> ++    template <typename unused>
> ++    const unsigned char globals_struct<unused>::parse_flags[256] =
> {
> +      // 0    1    2    3    4    5    6    7      8    9    A   
> B    C    D    E    F
> +         0,   0,   0,   0,   0,   0,   0,   0,     0,   2,   2,  
> 0,   0,   2,   0,   0, // 0
> +         0,   0,   0,   0,   0,   0,   0,   0,     0,   0,   0,  
> 0,   0,   0,   0,   0, // 1
> +@@ -162,15 +167,13 @@ struct globals_struct {
> +     };
> + 
> + // clang-format on
> +-};
> +-typedef globals_struct<> globals;
> + 
> +-constexpr inline bool is_plain_string_character(char c) {
> ++inline bool is_plain_string_character(char c) {
> +     // return c >= 0x20 && c <= 0x7f && c != 0x22 && c != 0x5c;
> +     return (globals::parse_flags[static_cast<unsigned char>(c)] &
> 1) != 0;
> + }
> + 
> +-constexpr inline bool is_whitespace(char c) {
> ++inline bool is_whitespace(char c) {
> +     // return c == '\r' || c == '\n' || c == '\t' || c == ' ';
> +     return (globals::parse_flags[static_cast<unsigned char>(c)] &
> 2) != 0;
> + }
Since this fixes an error that occurs when building with GCC 10, the
patch name should reflect that, e.g. "sajson-build-with-gcc10" (whether
you include or do not include a dash between gcc and 10 doesn't seem to
matter).

Cheers