Subject: [PATCH] build: clojure-build-system: Support compiling Java sources.
From: Reily Siegel <[email-masked]>
To: [email-masked]
Date: Sat, 15 Jan 2022 20:31:02 -0500
List-Id: <debbugs-submit.debbugs.gnu.org>

* guix/build/clojure-build-system.scm (compile-java): New variable.
* guix/build/clojure-build-system.scm (build): Copy classes compiled from Java
and optionally Java sources to the final jar.
* guix/build/clojure-build-system.scm (%standard-phases): Add compile-java
phase before build.
* guix/build/clojure-utils.scm (%java-source-dirs): New variable.
* guix/build/clojure-utils.scm (%java-compile-dir): New variable.
* guix/build-system/clojure.scm (clojure-build): Include %java-source-dirs and
%java-compile-dir.
* guix/build-system/clojure.scm (builder): Include %java-source-dirs and
%java-compile-dir.
---
 guix/build-system/clojure.scm       |  6 ++++-
 guix/build/clojure-build-system.scm | 36 +++++++++++++++++++++++------
 guix/build/clojure-utils.scm        | 10 ++++++++
 3 files changed, 44 insertions(+), 8 deletions(-)

diff --git a/guix/build-system/clojure.scm b/guix/build-system/clojure.scm
index 634854cf1b..2a0713d297 100644
--- a/guix/build-system/clojure.scm
+++ b/guix/build-system/clojure.scm
@@ -107,8 +107,10 @@ (define* (clojure-build name inputs
                         #:key
                         source
                         (source-dirs `',%source-dirs)
+                        (java-source-dirs `',%java-source-dirs)
                         (test-dirs `',%test-dirs)
                         (compile-dir %compile-dir)
+                        (java-compile-dir %java-compile-dir)
 
                         (jar-names `',(package-name->jar-names name))
                         (main-class %main-class)
@@ -142,9 +144,11 @@ (define builder
                          #:source #+source
 
                          #:source-dirs #$source-dirs
+                         #:java-source-dirs #$java-source-dirs
                          #:test-dirs #$test-dirs
                          #:compile-dir #$compile-dir
-
+                         #:java-compile-dir #$java-compile-dir
+                         
                          #:jar-names #$jar-names
                          #:main-class #$main-class
                          #:omit-source? #$omit-source?
diff --git a/guix/build/clojure-build-system.scm b/guix/build/clojure-build-system.scm
index dd01f95de8..22e90581a5 100644
--- a/guix/build/clojure-build-system.scm
+++ b/guix/build/clojure-build-system.scm
@@ -34,8 +34,24 @@ (define-module (guix build clojure-build-system)
 ;;
 ;; Code:
 
+(define* (compile-java #:key
+                       java-source-dirs java-compile-dir
+                       #:allow-other-keys)
+  "Compile java sources for use in clojure-build-system."
+  (let ((java-files (append-map (lambda (dir)
+                                  (find-files dir "\\.java$"))
+                                java-source-dirs)))
+    (mkdir-p java-compile-dir)
+    (when (not (eq? java-files '()))
+      (apply invoke
+             "javac"
+             "-verbose"
+             "-d" java-compile-dir
+             java-files))))
+
 (define* (build #:key
-                source-dirs compile-dir
+                source-dirs java-source-dirs
+                compile-dir java-compile-dir
                 jar-names main-class omit-source?
                 aot-include aot-exclude
                 #:allow-other-keys)
@@ -46,19 +62,24 @@ (define* (build #:key
                                            #:all-list libs)))
     (mkdir-p compile-dir)
     (eval-with-clojure `(run! compile ',libs*)
-                       source-dirs)
+                       (cons*  compile-dir
+                               java-compile-dir
+                               source-dirs))
     (let ((source-dir-files-alist (map (lambda (dir)
                                          (cons dir (find-files* dir)))
-                                       source-dirs))
+                                       (append source-dirs
+                                           java-source-dirs)))
           ;; workaround transitive compilation in Clojure
           (classes (filter (lambda (class)
                              (any (cut compiled-from? class <>)
                                   libs*))
                            (find-files* compile-dir))))
-      (for-each (cut create-jar <> (cons (cons compile-dir classes)
-                                         (if omit-source?
-                                             '()
-                                             source-dir-files-alist))
+      (for-each (cut create-jar <> (cons* (cons compile-dir classes)
+                                          (cons java-compile-dir
+                                                (find-files* java-compile-dir))
+                                          (if omit-source?
+                                              '()
+                                              source-dir-files-alist))
                      #:main-class main-class)
                 jar-names)
       #t)))
@@ -94,6 +115,7 @@ (define-with-docs install
 (define-with-docs %standard-phases
   "Standard build phases for clojure-build-system."
   (modify-phases %standard-phases@ant
+    (add-before 'build 'compile-java compile-java)
     (replace 'build build)
     (replace 'check check)
     (replace 'install install)
diff --git a/guix/build/clojure-utils.scm b/guix/build/clojure-utils.scm
index 8817cab52a..c5322141d3 100644
--- a/guix/build/clojure-utils.scm
+++ b/guix/build/clojure-utils.scm
@@ -32,8 +32,10 @@ (define-module (guix build clojure-utils)
             install-doc
 
             %source-dirs
+            %java-source-dirs
             %test-dirs
             %compile-dir
+            %java-compile-dir
             package-name->jar-names
             %main-class
             %omit-source?
@@ -101,6 +103,10 @@ (define-with-docs %source-dirs
   "A default list of source directories."
   '("src/"))
 
+(define-with-docs %java-source-dirs
+  "A default list of java source directories."
+  '())
+
 (define-with-docs %test-dirs
   "A default list of test directories."
   '("test/"))
@@ -109,6 +115,10 @@ (define-with-docs %compile-dir
   "Default directory for holding class files."
   "classes/")
 
+(define-with-docs %java-compile-dir
+  "Default directory for holding java class files."
+  "java-classes/")
+
 (define (package-name->jar-names name)
   "Given NAME, a package name like \"foo-0.9.1b\",
 return the list of default jar names: (\"foo-0.9.1b.jar\" \"foo.jar\")."
-- 
2.34.0