Subject: [bug#56604] [PATCH 5/8] gnu: clojure: Update to 1.11.1.This patch updates Clojure to 1.11.1. It also adds the 'reset-class-timestamps

From: r0man <[email-masked]>
Date: Sat, 16 Jul 2022 20:24:18 +0200

This patch updates Clojure to 1.11.1. It also adds the 'reset-class-timestamps
phase to the Clojure build system.

This phase makes sure the timestamp of compiled class files is set to a later
point in time than the timestamp of the corresponding Clojure source files. If
the timestamps of the class and source files are the same, the Clojure
compiler will compile the sources again which can lead to issues. This problem
has been discussed here [1]. The suggested solution was to keep/adjust the
timestamps of the class files.

[1] https://www.mail-archive.com/[email-masked]/msg99928.html
---
 gnu/packages/clojure.scm            | 93 ++++++++++++++++++++++-------
 guix/build/clojure-build-system.scm | 53 +++++++++++++++-
 2 files changed, 123 insertions(+), 23 deletions(-)

diff --git a/gnu/packages/clojure.scm b/gnu/packages/clojure.scm
index fa201f3110..d51923acdb 100644
--- a/gnu/packages/clojure.scm
+++ b/gnu/packages/clojure.scm
@@ -43,36 +43,44 @@ (define-public clojure
          ;; The libraries below are needed to run the tests.
          (libraries
           `(("core-specs-alpha-src"
-             ,(lib "core.specs.alpha/archive/core.specs.alpha-"
-                   "0.1.24"
-                   "0v2a0svf1ar2y42ajxwsjr7zmm5j7pp2zwrd2jh3k7xzd1p9x1fv"))
+             ,(lib "core.specs.alpha/archive/v"
+                   "0.2.62"
+                   "0v6nhghsigpzm8y7dykfm318q5dvk5l8sykmn1hr0qgs1jsjqh9j"))
             ("data-generators-src"
              ,(lib "data.generators/archive/data.generators-"
-                   "0.1.2"
-                   "0kki093jp4ckwxzfnw8ylflrfqs8b1i1wi9iapmwcsy328dmgzp1"))
+                   "1.0.0"
+                   "0s3hf1njvs68b8igasikvzagzqxl0gbri7w2qhzsypkhfh60v2cp"))
+            ("java-classpath-src"
+             ,(lib "java.classpath/archive/java.classpath-"
+                   "1.0.0"
+                   "178zajjsc9phk5l61r8w9hcpk0wgc9a811pl7kjgvn7rg4l7fh7j"))
             ("spec-alpha-src"
-             ,(lib "spec.alpha/archive/spec.alpha-"
-                   "0.1.143"
-                   "00alf0347licdn773w2jarpllyrbl52qz4d8mw61anjksacxylzz"))
+             ,(lib "spec.alpha/archive/v"
+                   "0.3.218"
+                   "0h5nd9xlind1a2vmllr2yfhnirgj2pm5dndgqzrly78l5iwcc3wa"))
             ("test-check-src"
-             ,(lib "test.check/archive/test.check-"
-                   "0.9.0"
-                   "0p0mnyhr442bzkz0s4k5ra3i6l5lc7kp6ajaqkkyh4c2k5yck1md"))
+             ,(lib "test.check/archive/v"
+                   "1.1.1"
+                   "0kx8l79mhpnn94rpsgc7nac7gb222g7a47mzrycj8crfc54wf0c1"))
             ("test-generative-src"
              ,(lib "test.generative/archive/test.generative-"
-                   "0.5.2"
-                   "1pjafy1i7yblc7ixmcpfq1lfbyf3jaljvkgrajn70sws9xs7a9f8"))
+                   "1.0.0"
+                   "0yy2vc38s4j5n94jdcjx1v7l2gdq0lywam31id1jh07sx37lv5il"))
             ("tools-namespace-src"
              ,(lib "tools.namespace/archive/tools.namespace-"
-                   "0.2.11"
-                   "10baak8v0hnwz2hr33bavshm7y49mmn9zsyyms1dwjz45p5ymhy0"))))
+                   "1.0.0"
+                   "1ifpk93m33rj2xm1qnnninlsdvm1liqmsp9igr63pjjwwwjw1cnn"))
+            ("tools-reader-src"
+             ,(lib "tools.reader/archive/tools.reader-"
+                   "1.3.2"
+                   "1n4dhg61iyypnjbxmihhqjb7lfpc0lzfvlk4jd8w0yr6za414f3a"))))
          (library-names (match libraries
                           (((library-name _) ...)
                            library-name))))
 
     (package
       (name "clojure")
-      (version "1.10.0")
+      (version "1.11.1")
       (source (let ((name+version (string-append name "-" version)))
                 (origin
                   (method git-fetch)
@@ -81,18 +89,23 @@ (define-public clojure
                         (commit name+version)))
                   (file-name (string-append name+version "-checkout"))
                   (sha256
-                   (base32 "1kcyv2836acs27vi75hvf3r773ahv2nlh9b3j9xa9m9sdanz1h83")))))
+                   (base32 "1xbab21rm9zvhmw1i2h5lqm7612vrdkxprq0rgb2i3sbgsxcdsn4")))))
       (build-system ant-build-system)
       (inputs
        `(("jre" ,icedtea)))
       (arguments
        `(#:imported-modules ((guix build clojure-utils)
+                             (guix build clojure-build-system)
                              (guix build guile-build-system)
                              ,@%ant-build-system-modules)
          #:modules ((guix build ant-build-system)
+                    ((guix build clojure-build-system) #:prefix clj:)
                     (guix build clojure-utils)
                     (guix build java-utils)
                     (guix build utils)
+                    (guix build syscalls)
+                    (ice-9 match)
+                    (ice-9 regex)
                     (srfi srfi-26))
          #:test-target "test"
          #:phases
@@ -106,22 +119,60 @@ (define (extract-library name)
                            "--extract"
                            "--verbose"
                            "--file" (assoc-ref inputs name)
-                           "--strip-components=1"))
-                 (copy-recursively (string-append name "/src/main/clojure/")
-                                   "src/clj/"))
+                           "--strip-components=1")))
                (for-each extract-library ',library-names)
+               (copy-recursively "core-specs-alpha-src/src/main/clojure"
+                                 "src/clj/")
+               (copy-recursively "spec-alpha-src/src/main/clojure"
+                                 "src/clj/")
                #t))
            (add-after 'unpack-library-sources 'fix-manifest-classpath
              (lambda _
                (substitute* "build.xml"
                  (("<attribute name=\"Class-Path\" value=\".\"/>") ""))
                #t))
+           (add-after 'unpack-library-sources 'clojure-spec-skip-macros
+             ;; Disable spec macro instrumentation when compiling clojure.spec
+             ;; See: https://clojure.atlassian.net/browse/CLJ-2254
+             (lambda _
+               (substitute* "build.xml"
+                 (("<sysproperty key=\"java.awt.headless\" value=\"true\"/>")
+                  ,(string-join
+                    '("<sysproperty key=\"java.awt.headless\" value=\"true\"/>"
+                      "<sysproperty key=\"clojure.spec.skip-macros\" value=\"true\"/>\n")
+                    "\n")))
+               #t))
+           (add-after 'unpack-library-sources 'clojure-spec-compile
+             ;; Compile and include clojure.spec.alpha & clojure.core.specs.alpha
+             (lambda _
+               (substitute* "build.xml"
+                 (("<arg value=\"clojure.math\"/>")
+                  ,(string-join
+                    '("<arg value=\"clojure.math\"/>"
+                      "<arg value=\"clojure.spec.alpha\"/>"
+                      "<arg value=\"clojure.spec.gen.alpha\"/>"
+                      "<arg value=\"clojure.spec.test.alpha\"/>"
+                      "<arg value=\"clojure.core.specs.alpha\"/>"))))
+               #t))
+           (add-before 'build 'maven-classpath-properties
+             (lambda _
+               (define (make-classpath libraries)
+                 (string-join (map (lambda (library)
+                                     (string-append library "/src/main/clojure"))
+                                   libraries) ":"))
+               (with-output-to-file "maven-classpath.properties"
+                 (lambda ()
+                   (let ((classpath (make-classpath ',library-names)))
+                     (display (string-append "maven.compile.classpath=" classpath "\n"))
+                     (display (string-append "maven.test.classpath=" classpath "\n")))))
+               #t))
            (add-after 'build 'build-javadoc ant-build-javadoc)
            (replace 'install (install-jars "./"))
            (add-after 'install-license-files 'install-doc
              (cut install-doc #:doc-dirs '("doc/clojure/") <...>))
            (add-after 'install-doc 'install-javadoc
-             (install-javadoc "target/javadoc/")))))
+             (install-javadoc "target/javadoc/"))
+           (add-after 'reset-gzip-timestamps 'reset-class-timestamps clj:reset-class-timestamps))))
       (native-inputs libraries)
       (home-page "https://clojure.org/")
       (synopsis "Lisp dialect running on the JVM")
diff --git a/guix/build/clojure-build-system.scm b/guix/build/clojure-build-system.scm
index 7d494078ea..82440e3b7a 100644
--- a/guix/build/clojure-build-system.scm
+++ b/guix/build/clojure-build-system.scm
@@ -22,11 +22,15 @@ (define-module (guix build clojure-build-system)
                           ant-build))
   #:use-module (guix build clojure-utils)
   #:use-module (guix build java-utils)
+  #:use-module (guix build syscalls)
   #:use-module (guix build utils)
+  #:use-module (ice-9 match)
+  #:use-module (ice-9 regex)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-26)
   #:export (%standard-phases
-            clojure-build))
+            clojure-build
+            reset-class-timestamps))
 
 ;; Commentary:
 ;;
@@ -108,6 +112,50 @@ (define* (check #:key
                   jar-names)))
   #t)
 
+(define (regular-jar-file-predicate file stat)
+  "Predicate returning true if FILE is ending on '.jar'
+and STAT indicates it is a regular file."
+    (and ((file-name-predicate "\\.jar$") file stat)
+         (eq? 'regular (stat:type stat))))
+
+(define* (reset-class-timestamps #:key outputs #:allow-other-keys)
+  "Unpack all jar archives, reset the timestamp of all contained class files,
+and repack them.  This is necessary to ensure that archives are reproducible."
+  (define (repack-archive jar)
+    (format #t "resetting class timestamps and repacking ~a\n" jar)
+    (let* ((early-1980 315619200) ; 1980-01-02 UTC
+           (dir (mkdtemp! "jar-contents.XXXXXX"))
+           (manifest (string-append dir "/META-INF/MANIFEST.MF")))
+      (with-directory-excursion dir
+        (invoke "jar" "xf" jar))
+      (delete-file jar)
+      (for-each (lambda (file)
+                  (let ((s (lstat file)))
+                    (unless (eq? (stat:type s) 'symlink)
+                      (when (string-match "^(.*)\\.class$" file)
+                        (utime file early-1980 early-1980)))))
+                (find-files dir #:directories? #t))
+      ;; The jar tool will always set the timestamp on the manifest file
+      ;; and the containing directory to the current time, even when we
+      ;; reuse an existing manifest file.  To avoid this we use "zip"
+      ;; instead of "jar".  It is important that the manifest appears
+      ;; first.
+      (with-directory-excursion dir
+        (let* ((files (find-files "." ".*" #:directories? #t))
+               ;; To ensure that the reference scanner can detect all
+               ;; store references in the jars we disable compression
+               ;; with the "-0" option.
+               (command (if (file-exists? manifest)
+                            `("zip" "-0" "-X" ,jar ,manifest ,@files)
+                            `("zip" "-0" "-X" ,jar ,@files))))
+          (apply invoke command)))
+      (utime jar 0 0)))
+  (for-each (match-lambda
+              ((output . directory)
+               (for-each repack-archive
+                         (find-files directory regular-jar-file-predicate))))
+            outputs))
+
 (define-with-docs install
   "Standard 'install' phase for clojure-build-system."
   (install-jars "./"))
@@ -119,7 +167,8 @@ (define-with-docs %standard-phases
     (replace 'build build)
     (replace 'check check)
     (replace 'install install)
-    (add-after 'install-license-files 'install-doc install-doc)))
+    (add-after 'install-license-files 'install-doc install-doc)
+    (add-after 'reset-gzip-timestamps 'reset-class-timestamps reset-class-timestamps)))
 
 (define* (clojure-build #:key
                         inputs
-- 
2.36.1