1

Because Clojure is built on top of the JVM, I would guess I could use GCJ to compile it directly to machine code, but I can't find any information about it.

When it's possible, how could I then "strip" it to it's bare essentials, so the startup time and memory footprint is as small as possible.

I'd really like to write small terminal programs following the UNIX principles with Clojure

incurious
  • 13
  • 4
  • You don't and you can't. **However**, you could use a different LISP implementation that can compile to native code. – Elliott Frisch Jul 20 '15 at 23:25
  • In case it's helpful: SBCL compiles to native code (often very fast), and I believe that ECL compiles to C. – Mars Jul 21 '15 at 04:22
  • [This discussion](https://groups.google.com/forum/#!topic/clojure/f_zn0v0wr3s) is relevant. – Mars Jul 21 '15 at 04:25

2 Answers2

3

In theory, you could AOT-compile Clojure source code to Java bytecode, and then use gcj to compile this byte code to native machine code. In practice, I would not expect this to work; gcj has severe limitations that prevent its use for most modern Java applications. Specifically, its support for language features stops at Java 1.2 (Clojure requires Java 1.6), and it has incomplete implementations of the standard class libraries required by the Clojure runtime. In the wild, I've seen Clojure applications crash and burn when accidentally launched using the gcj runtime instead of the OpenJDK or Oracle JREs.

Alex
  • 13,811
  • 1
  • 37
  • 50
1

The clojure compiler takes in source "expressions", usually from a file, and produces JVM bytecode. This bytecode is then compiled JIT style to machine code by the JVM. the clojure compiler and GCJ are both compilers, though for different languages, so it does not make sense to talk about chaining one after the other. The clojure compiler does not produce java source code, it directly produces bytecode.

Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • [According to the project page](https://gcc.gnu.org/java/) GCJ can compile bytecode to machine code. That's why I'm confused – incurious Jul 21 '15 at 01:13