Skip to content
This repository
Browse code

Merge pull request #886 from hyPiRion/run-javac-in-subprocess

Make javac run in a subprocess. Fixes #809.
  • Loading branch information...
commit 3a1f84f993dfcab8388a6b6c0c91febb107768ee 2 parents b4558ec + d38ecd3
Phil Hagelberg authored

Showing 1 changed file with 41 additions and 15 deletions. Show diff stats Hide diff stats

  1. 56  src/leiningen/javac.clj
56  src/leiningen/javac.clj
... ...
@@ -1,7 +1,10 @@
1 1
 (ns leiningen.javac
2 2
   "Compile Java source files."
3 3
   (:require [leiningen.classpath :as classpath]
4  
-            [leiningen.core.main :as main]
  4
+            [leiningen.core
  5
+             [main :as main]
  6
+             [eval :as eval]
  7
+             [project :as project]]
5 8
             [clojure.java.io :as io])
6 9
   (:import java.io.File
7 10
            javax.tools.ToolProvider))
@@ -66,27 +69,50 @@
66 69
             "-d" (:compile-path project)]
67 70
            files)))
68 71
 
  72
+;; Pure java projects will not have Clojure on the classpath. As such, we need
  73
+;; to add it if it's not already there.
  74
+(def subprocess-profile
  75
+  {:dependencies [^:displace
  76
+                  ['org.clojure/clojure (clojure-version)]]
  77
+   :eval-in :subprocess})
  78
+
  79
+(defn- subprocess-form
  80
+  "Creates a form for running javac in a subprocess."
  81
+  [compile-path files javac-opts]
  82
+  `(let [abort# (fn [& msg#]
  83
+                  (.println java.lang.System/err (apply str msg#))
  84
+                  (java.lang.System/exit 1))]
  85
+     (if-let [compiler# (javax.tools.ToolProvider/getSystemJavaCompiler)]
  86
+       (do
  87
+         (println "Compiling" ~(count files) "source files to" ~compile-path)
  88
+         (.mkdirs (clojure.java.io/file ~compile-path))
  89
+         (when-not (zero?
  90
+                    (.run compiler# nil nil nil
  91
+                          (into-array java.lang.String ~javac-opts)))
  92
+           (abort# "Compilation of Java sources(lein javac) failed.")))
  93
+       (abort# "lein-javac: system java compiler not found; "
  94
+               "Be sure to use java from a JDK\nrather than a JRE by"
  95
+               " either modifying PATH or setting JAVA_CMD."))))
  96
+
69 97
 ;; We can't really control what is printed here. We're just going to
70 98
 ;; allow `.run` to attach in, out, and err to the standard streams. This
71 99
 ;; should have the effect of compile errors being printed. javac doesn't
72 100
 ;; actually output any compilation info unless it has to (for an error)
73 101
 ;; or you make it do so with `-verbose`.
74  
-(defn- run-javac-task
75  
-  "Run javac to compile all source files in the project."
  102
+(defn- run-javac-subprocess
  103
+  "Run javac to compile all source files in the project. The compilation is run
  104
+  in a subprocess to avoid it from adding the leiningen standalone to the
  105
+  classpath, as leiningen adds itself to the classpath through the
  106
+  bootclasspath."
76 107
   [project args]
77 108
   (let [compile-path (:compile-path project)
78  
-        files (stale-java-sources (:java-source-paths project) compile-path)]
  109
+        files (stale-java-sources (:java-source-paths project) compile-path)
  110
+        javac-opts (vec (javac-options project files args))
  111
+        form (subprocess-form compile-path files javac-opts)]
79 112
     (when (seq files)
80  
-      (if-let [compiler (ToolProvider/getSystemJavaCompiler)]
81  
-        (do
82  
-          (main/info "Compiling" (count files) "source files to" compile-path)
83  
-          (.mkdirs (io/file compile-path))
84  
-          (when-not (zero? (.run compiler nil nil nil
85  
-                                 (javac-options project files args)))
86  
-            (main/abort "Compilation of Java sources (lein javac) failed.")))
87  
-        (main/abort "lein-javac: system java compiler not found;"
88  
-                    "Be sure to use java from a JDK\nrather than a JRE by"
89  
-                    "either modifying PATH or setting JAVA_CMD.")))))
  113
+      (eval/eval-in
  114
+       (project/merge-profiles project [subprocess-profile])
  115
+       form))))
90 116
 
91 117
 (defn javac
92 118
   "Compile Java source files.
@@ -99,4 +125,4 @@ Like the compile and deps tasks, this should be invoked automatically when
99 125
 needed and shouldn't ever need to be run by hand. By default it is called before
100 126
 compilation of Clojure source; change :prep-tasks to alter this."
101 127
   [project & args]
102  
-  (run-javac-task project args))
  128
+  (run-javac-subprocess project args))

0 notes on commit 3a1f84f

Please sign in to comment.
Something went wrong with that request. Please try again.