Compiling.xhtml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  3. <head>
  4. <title>Compiling to byte-code</title>
  5. <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
  6. <link rel="stylesheet" type="text/css" href="kawa.css"/>
  7. <script src="kawa-ebook.js" type="text/javascript"/>
  8. <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
  9. <link rel="prev" href="Overall-Index.xhtml" title="Index"/>
  10. <link rel="next" href="Syntax.xhtml" title="Syntax"/>
  11. </head>
  12. <body>
  13. <header/>
  14. <section class="sect1" title="Compiling to byte-code" epub:type="subchapter" id="Compiling">
  15. <div class="titlepage">
  16. <div>
  17. <div>
  18. <h2 class="title" style="clear: both">Compiling to byte-code</h2>
  19. </div>
  20. </div>
  21. </div>
  22. <p>All Scheme functions and source files are invisibly compiled
  23. into internal Java byte-codes.
  24. (A traditional interpreter is used for macro-expansion.
  25. Kawa used to also interpret “simple” expressions in interactive mode,
  26. but always compiling makes things more consistent, and allows for
  27. better stack traces on errors.)
  28. </p>
  29. <p>To save speed when loading large Scheme source files, you probably
  30. want to pre-compile them and save them on your local disk.
  31. There are two ways to do this.
  32. </p>
  33. <p>You can compile a Scheme source file to a single archive file.
  34. You do this using the <code class="literal">compile-file</code> function.
  35. The result is a single file that you can move around and <code class="literal">load</code>
  36. just like the <code class="literal">.scm</code> source file. You just specify the name
  37. of the archive file to the <code class="literal">load</code> procedure.
  38. Currently, the archive is a "zip" archive and has extension ".zip";
  39. a future release will probably use "Java Archive" (jar) files.
  40. The advantage of compiling to an archive is that it is simple
  41. and transparent.
  42. </p>
  43. <p>Alternatively, you can compile a Scheme source file to a
  44. collection of ‘<code class="literal">.class</code>’ files.
  45. You then use the standard Java class loading mechanism to load the code.
  46. The compiled class files do have to be installed somewhere
  47. in the <code class="literal">CLASSPATH</code>.
  48. </p>
  49. <section class="sect2" title="Compiling to a set of .class files" epub:type="division" id="Files-compilation">
  50. <div class="titlepage">
  51. <div>
  52. <div>
  53. <h3 class="title">Compiling to a set of .class files</h3>
  54. </div>
  55. </div>
  56. </div>
  57. <p>Invoking ‘<code class="literal">kawa</code>’ (or ‘<code class="literal">java kawa.repl</code>’) with
  58. the ‘<code class="literal">-C</code>’ flag will compile
  59. a ‘<code class="literal">.scm</code>’ source file into one or more ‘<code class="literal">.class</code>’ files:
  60. </p>
  61. <pre class="screen">kawa --main -C myprog.scm
  62. </pre>
  63. <p>You run it as follows:
  64. </p>
  65. <pre class="screen">kawa [-d <em class="replaceable"><code>outdirectory</code></em>] [-P <em class="replaceable"><code>prefix</code></em>] [-T <em class="replaceable"><code>topname</code></em>] [--main | --applet | --servlet] -C <em class="replaceable"><code>infile</code></em> ...
  66. </pre>
  67. <p>Note the ‘<code class="literal">-C</code>’ must come last, because ‘<code class="literal">Kawa</code>’ processes the
  68. arguments and options in order,
  69. </p>
  70. <p>Here:
  71. </p>
  72. <div class="variablelist" epub:type="list">
  73. <dl class="variablelist">
  74. <dt class="term"><code class="literal">-C <em class="replaceable"><code>infile</code></em> ...</code>
  75. </dt>
  76. <dd>
  77. <p>The Scheme source files we want to compile.
  78. </p>
  79. </dd>
  80. <dt class="term"><code class="literal">-d <em class="replaceable"><code>outdirectory</code></em></code>
  81. </dt>
  82. <dd>
  83. <p>The directory under which the resulting ‘<code class="literal">.class</code>’ files will be.
  84. The default is the current directory.
  85. </p>
  86. </dd>
  87. <dt class="term"><code class="literal">-P <em class="replaceable"><code>prefix</code></em></code>
  88. </dt>
  89. <dd>
  90. <p>A string to prepend to the generated class names.
  91. The default is the empty string.
  92. </p>
  93. </dd>
  94. <dt class="term"><code class="literal">-T <em class="replaceable"><code>topname</code></em></code>
  95. </dt>
  96. <dd>
  97. <p>The name of the "top" class - i.e. the one that contains the code
  98. for the top-level expressions and definitions.
  99. The default is generated from the <em class="replaceable"><code>infile</code></em> and <em class="replaceable"><code>prefix</code></em>.
  100. </p>
  101. </dd>
  102. <dt class="term"><code class="literal">--main</code>
  103. </dt>
  104. <dd>
  105. <p>Generate a <code class="literal">main</code> method so that the resulting "top" class can
  106. be used as a stand-alone application. See <a class="link" href="Compiling.xhtml#Application-compilation" title="Compiling to a standalone application">Application compilation</a>.
  107. </p>
  108. </dd>
  109. <dt class="term"><code class="literal">--applet</code>
  110. </dt>
  111. <dd>
  112. <p>The resulting class inherits from <code class="literal">java.applet.Applet</code>,
  113. and can be used as an applet. See <a class="link" href="Compiling.xhtml#Applet-compilation" title="Compiling to an applet">Applet compilation</a>.
  114. </p>
  115. </dd>
  116. <dt class="term"><code class="literal">--servlet</code>
  117. </dt>
  118. <dd>
  119. <p>The resulting class implements <code class="literal">javax.servlet.http.HttpServlet</code>,
  120. and can be used as a servlet in a servlet container like Tomcat.
  121. </p>
  122. </dd>
  123. </dl>
  124. </div>
  125. <p>When you actually want to load the classes, the <em class="replaceable"><code>outdirectory</code></em>
  126. must be in your ‘<code class="literal">CLASSPATH</code>’.
  127. You can use the <code class="literal">require</code> syntax or the <code class="literal">load</code> function to load the code,
  128. by specifying the top-level class, either as a file name
  129. (relative to <em class="replaceable"><code>outdirectory</code></em>) or as a class name.
  130. E.g. if you did:
  131. </p>
  132. <pre class="screen">kawa -d /usr/local/share/java -P my.lib. -T foo -C foosrc.scm
  133. </pre>
  134. <p>you can use either:
  135. </p>
  136. <pre class="screen">(require my.lib.foo)
  137. </pre>
  138. <p>or:
  139. </p>
  140. <pre class="screen">(load "my.lib.foo")
  141. </pre>
  142. <p>Using <code class="literal">require</code> is preferred as it imports the definitions
  143. from <code class="literal">my.lib.foo</code> into the compile-time environment,
  144. while <code class="literal">load</code> only imports the definitions into the run-time environment.
  145. </p>
  146. <p>If you are compiling a Scheme source file (say ‘<code class="literal">foosrc.scm</code>’)
  147. that uses macros defined in some other file (say ‘<code class="literal">macs.scm</code>’),
  148. you need to make sure the definitions are visible to the compiler.
  149. One way to do that is with the ‘<code class="literal">-f</code>’:
  150. </p>
  151. <pre class="screen">kawa -f macs.scm -C foosrc.scm
  152. </pre>
  153. <p>Many of the options <a class="link" href="Options.xhtml" title="Command-line arguments">described earlier</a> are
  154. relevant when compiling. Commonly used options include language selection,
  155. the <code class="literal">--warn-xxx</code> options, and <code class="literal">--full-tailcalls</code>.
  156. </p>
  157. </section>
  158. <section class="sect2" title="Compiling to an archive file" epub:type="division" id="Archive-compilation">
  159. <div class="titlepage">
  160. <div>
  161. <div>
  162. <h3 class="title">Compiling to an archive file</h3>
  163. </div>
  164. </div>
  165. </div>
  166. <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667879904288" class="indexterm"/> <code class="function">compile-file</code> <em class="replaceable"><code>source-file</code></em> <em class="replaceable"><code>compiled-archive</code></em></p>
  167. <div class="blockquote">
  168. <blockquote class="blockquote">
  169. <p>Compile the <em class="replaceable"><code>source-file</code></em>, producing a <code class="literal">.zip</code> archive
  170. <em class="replaceable"><code>compiled-file</code></em>.
  171. </p>
  172. <p>For example, to byte-compile a file ‘<code class="literal">foo.scm</code>’ do:
  173. </p>
  174. <pre class="screen">(compile-file "foo.scm" "foo")
  175. </pre>
  176. <p>This will create ‘<code class="literal">foo.zip</code>’, which contains
  177. byte-compiled JVM <code class="literal">.class</code> files.
  178. You can move this file around, without worrying about class paths.
  179. To load the compiled file, you can later <code class="literal">load</code> the
  180. named file, as in either <code class="literal">(load "foo")</code> or <code class="literal">(load "foo.zip")</code>.
  181. This should have the same effect as
  182. loading ‘<code class="literal">foo.scm</code>’, except you will get the faster byte-compiled versions.
  183. </p>
  184. </blockquote>
  185. </div>
  186. </section>
  187. <section class="sect2" title="Compiling using Ant" epub:type="division" id="Compiling-using-Ant">
  188. <div class="titlepage">
  189. <div>
  190. <div>
  191. <h3 class="title">Compiling using Ant</h3>
  192. </div>
  193. </div>
  194. </div>
  195. <a id="idm139667879893216" class="indexterm"/>
  196. <p>Many Java projects use <a class="ulink" href="http://ant.apache.org" target="_top">Ant</a>
  197. for building Java projects. Kawa includes a <code class="literal">&lt;kawac&gt;</code>
  198. Ant task that simplifies compiling Kawa source files to classes.
  199. See the <code class="literal">build.xml</code> in the Kawa source distribution for examples.
  200. See the <a class="ulink" href="ant-kawac.html" target="_top"><code class="literal">kawac</code> task documentation</a> for details.
  201. </p>
  202. </section>
  203. <section class="sect2" title="Compiling to a standalone application" epub:type="division" id="Application-compilation">
  204. <div class="titlepage">
  205. <div>
  206. <div>
  207. <h3 class="title">Compiling to a standalone application</h3>
  208. </div>
  209. </div>
  210. </div>
  211. <p>A Java application is a Java class with a special method
  212. (whose name is <code class="literal">main</code>). The application can be invoked directly
  213. by naming it in the Java command.
  214. If you want to generate an application from a Scheme program,
  215. create a Scheme source file with the definitions you need, plus
  216. the top-level actions that you want the application to execute.
  217. </p>
  218. <p>For example, assuming your Scheme file is
  219. <code class="literal">MyProgram.scm</code>, you have two ways at your disposal to
  220. compile this Scheme program to a standalone application:
  221. </p>
  222. <div class="orderedlist" epub:type="list">
  223. <ol class="orderedlist" type="1">
  224. <li class="listitem" epub:type="list-item">
  225. <p>Compile
  226. in the regular way described in the previous section, but add the
  227. <code class="literal">--main</code> option.
  228. </p>
  229. <pre class="screen">kawa --main -C MyProgram.scm
  230. </pre>
  231. <p>The <code class="literal">--main</code> option will compile all Scheme programs
  232. received in arguments to standalone applications.
  233. </p>
  234. </li>
  235. <li class="listitem" epub:type="list-item">
  236. <p>Compile
  237. in the regular way decribed in the previous section, but add the
  238. <code class="literal">main: #t</code> module compile option to your module.
  239. </p>
  240. <pre class="screen">;; MyProgram.scm
  241. (module-name &lt;myprogram&gt;)
  242. (module-compile-options main: #t)
  243. </pre>
  244. <pre class="screen">kawa -C MyProgram.scm
  245. </pre>
  246. <p>This way you can compile multiple Scheme programs at once, and
  247. still control which one(s) will compile to standalone application(s).
  248. </p>
  249. </li>
  250. </ol>
  251. </div>
  252. <p>Both methods will create a <code class="literal">MyProgram.class</code> which you can either
  253. <code class="literal">load</code> (as described in the previous section), or invoke as an application:
  254. </p>
  255. <pre class="screen">java MyProgram [<em class="replaceable"><code>args</code></em>]
  256. </pre>
  257. <p>Your Scheme program can access the command-line arguments <em class="replaceable"><code>args</code></em>
  258. by using the global variable ‘<code class="literal">command-line-arguments</code>’,
  259. or the R6RS function ‘<code class="literal">command-line</code>’.
  260. </p>
  261. <p>If there is no explicit <code class="literal">module-export</code> in a module compiled
  262. with <code class="literal">--main</code> then no names are exported. (The default
  263. otherwise is for all names to be exported.)
  264. </p>
  265. </section>
  266. <section class="sect2" title="Compiling to an applet" epub:type="division" id="Applet-compilation">
  267. <div class="titlepage">
  268. <div>
  269. <div>
  270. <h3 class="title">Compiling to an applet</h3>
  271. </div>
  272. </div>
  273. </div>
  274. <p>An applet is a Java class that inherits from <code class="literal">java.applet.Applet</code>.
  275. The applet can be downloaded and run in a Java-capable web-browser.
  276. To generate an applet from a Scheme program, write the Scheme
  277. program with appropriate definitions of the functions ‘<code class="literal">init</code>’,
  278. ‘<code class="literal">start</code>’, ‘<code class="literal">stop</code>’ and ‘<code class="literal">destroy</code>’. You must declare these
  279. as zero-argument functions with a <code class="literal">&lt;void&gt;</code> return-type.
  280. </p>
  281. <p>Here is an example, based on the scribble applet in Flanagan’s
  282. "Java Examples in a Nutshell" (O’Reilly, 1997):
  283. </p>
  284. <pre class="screen">(define-private last-x 0)
  285. (define-private last-y 0)
  286. (define (init) :: void
  287. (let ((applet (this)))
  288. (applet:addMouseListener
  289. (object (java.awt.event.MouseAdapter)
  290. ((mousePressed e)
  291. (set! last-x (e:getX))
  292. (set! last-y (e:getY)))))
  293. (applet:addMouseMotionListener
  294. (object (java.awt.event.MouseMotionAdapter)
  295. ((mouseDragged e)
  296. (let ((g (applet:getGraphics))
  297. (x (e:getX))
  298. (y (e:getY)))
  299. (g:drawLine last-x last-y x y)
  300. (set! last-x x)
  301. (set! last-y y)))))))
  302. (define (start) :: void (format #t "called start.~%~!"))
  303. (define (stop) :: void (format #t "called stop.~%~!"))
  304. (define (destroy) :: void (format #t "called destroy.~%~!"))
  305. </pre>
  306. <p>You compile the program with the ‘<code class="literal">--applet</code>’ flag in addition to the
  307. normal ‘<code class="literal">-C</code>’ flag:
  308. </p>
  309. <pre class="screen">java kawa.repl --applet -C scribble.scm
  310. </pre>
  311. <p>You can then create a ‘<code class="literal">.jar</code>’ archive containing your applet:
  312. </p>
  313. <pre class="screen">jar cf scribble.jar scribble*.class
  314. </pre>
  315. <p>Finally, you create an ‘<code class="literal">.html</code>’ page referencing your applet
  316. and its support <code class="literal">jar</code>s:
  317. </p>
  318. <pre class="screen">&lt;html&gt;&lt;head&gt;&lt;title&gt;Scribble testapp&lt;/title&gt;&lt;/head&gt;
  319. &lt;body&gt;&lt;h1&gt;Scribble testapp&lt;/h1&gt;
  320. You can scribble here:
  321. &lt;br&gt;
  322. &lt;applet code="scribble.class" archive="scribble.jar, kawa-2.92_invoke.jar" width=200 height=200&gt;
  323. Sorry, Java is needed.&lt;/applet&gt;
  324. &lt;/body&gt;&lt;/html&gt;
  325. </pre>
  326. <p>The problem with using Kawa to write applets is that the Kawa <code class="literal">.jar</code>
  327. file is quite big, and may take a while to download over a network connection.
  328. Some possible solutions:
  329. </p>
  330. <div class="itemizedlist" epub:type="list">
  331. <ul class="itemizedlist" style="list-style-type: disc; ">
  332. <li class="listitem" epub:type="list-item">
  333. <p>Try to strip out of the Kawa <code class="literal">.jar</code> any classes your
  334. applet doesn’t need.
  335. </p>
  336. </li>
  337. <li class="listitem" epub:type="list-item">
  338. <p>Java 2 provides a mechanism to install a <a class="ulink" href="http://java.sun.com/docs/books/tutorial/ext/basics/download.html" target="_top">download extension</a>.
  339. </p>
  340. </li>
  341. <li class="listitem" epub:type="list-item">
  342. <p>Consider some alternative to applets, such as
  343. <a class="ulink" href="http://java.sun.com/products/javawebstart/" target="_top">Java Web Start</a>.
  344. </p>
  345. </li>
  346. </ul>
  347. </div>
  348. </section>
  349. <section class="sect2" title="Compiling to a native executable" epub:type="division" id="Compiling-to-executable">
  350. <div class="titlepage">
  351. <div>
  352. <div>
  353. <h3 class="title">Compiling to a native executable</h3>
  354. </div>
  355. </div>
  356. </div>
  357. <p>In the past it was possible to compile a Scheme program to native code using GCJ.
  358. However, using GCJ with Kawa is no longer supported,
  359. as GCJ is no longer being actively maintained.
  360. </p>
  361. </section>
  362. </section>
  363. <footer>
  364. <div class="navfooter">
  365. <ul>
  366. <li>
  367. <b class="toc">
  368. <a href="Compiling.xhtml#Files-compilation">Compiling to a set of .class files</a>
  369. </b>
  370. </li>
  371. <li>
  372. <b class="toc">
  373. <a href="Compiling.xhtml#Archive-compilation">Compiling to an archive file</a>
  374. </b>
  375. </li>
  376. <li>
  377. <b class="toc">
  378. <a href="Compiling.xhtml#Compiling-using-Ant">Compiling using Ant</a>
  379. </b>
  380. </li>
  381. <li>
  382. <b class="toc">
  383. <a href="Compiling.xhtml#Application-compilation">Compiling to a standalone application</a>
  384. </b>
  385. </li>
  386. <li>
  387. <b class="toc">
  388. <a href="Compiling.xhtml#Applet-compilation">Compiling to an applet</a>
  389. </b>
  390. </li>
  391. <li>
  392. <b class="toc">
  393. <a href="Compiling.xhtml#Compiling-to-executable">Compiling to a native executable</a>
  394. </b>
  395. </li>
  396. </ul>
  397. <p>
  398. Up: <a accesskey="u" href="Running.xhtml">How to start up and run Kawa</a></p>
  399. <p>
  400. Previous: <a accesskey="p" href="Exiting.xhtml">Exiting Kawa</a></p>
  401. </div>
  402. </footer>
  403. </body>
  404. </html>