ant-build-system.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016, 2018 Ricardo Wurmus <rekado@elephly.net>
  3. ;;; Copyright © 2019 Björn Höfling <bjoern.hoefling@bjoernhoefling.de>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix build ant-build-system)
  20. #:use-module ((guix build gnu-build-system) #:prefix gnu:)
  21. #:use-module (guix build utils)
  22. #:use-module (sxml simple)
  23. #:use-module (ice-9 match)
  24. #:use-module (ice-9 ftw)
  25. #:use-module (srfi srfi-1)
  26. #:use-module (srfi srfi-26)
  27. #:export (%standard-phases
  28. ant-build))
  29. ;; Commentary:
  30. ;;
  31. ;; Builder-side code of the standard build procedure for Java packages using
  32. ;; Ant.
  33. ;;
  34. ;; Code:
  35. (define* (default-build.xml jar-name prefix #:optional
  36. (source-dir ".") (test-dir "./test") (main-class #f)
  37. (test-include '("**/*Test.java"))
  38. (test-exclude '("**/Abstract*Test.java")))
  39. "Create a simple build.xml with standard targets for Ant."
  40. (call-with-output-file "build.xml"
  41. (lambda (port)
  42. (sxml->xml
  43. `(project (@ (basedir ".")
  44. (name ,jar-name))
  45. (property (@ (name "classes.dir")
  46. (value "${basedir}/build/classes")))
  47. (property (@ (name "manifest.dir")
  48. (value "${basedir}/build/manifest")))
  49. (property (@ (name "manifest.file")
  50. (value "${manifest.dir}/MANIFEST.MF")))
  51. (property (@ (name "jar.dir")
  52. (value "${basedir}/build/jar")))
  53. (property (@ (name "dist.dir")
  54. (value ,prefix)))
  55. (property (@ (name "test.home")
  56. (value ,test-dir)))
  57. (property (@ (name "test.classes.dir")
  58. (value "${basedir}/build/test-classes")))
  59. ;; respect the CLASSPATH environment variable
  60. (property (@ (name "build.sysclasspath")
  61. (value "first")))
  62. (property (@ (environment "env")))
  63. (path (@ (id "classpath"))
  64. (pathelement (@ (location "${env.CLASSPATH}"))))
  65. (target (@ (name "manifest"))
  66. (mkdir (@ (dir "${manifest.dir}")))
  67. (manifest (@ (file "${manifest.file}"))
  68. ,(if main-class
  69. `(attribute (@ (name "Main-Class")
  70. (value ,main-class)))
  71. "")))
  72. (target (@ (name "compile"))
  73. (mkdir (@ (dir "${classes.dir}")))
  74. (javac (@ (includeantruntime "false")
  75. (srcdir ,source-dir)
  76. (destdir "${classes.dir}")
  77. (classpath (@ (refid "classpath"))))))
  78. (target (@ (name "compile-tests"))
  79. (mkdir (@ (dir "${test.classes.dir}")))
  80. (javac (@ (includeantruntime "false")
  81. (srcdir ,test-dir)
  82. (destdir "${test.classes.dir}"))
  83. (classpath
  84. (pathelement (@ (path "${env.CLASSPATH}")))
  85. (pathelement (@ (location "${classes.dir}")))
  86. (pathelement (@ (location "${test.classes.dir}"))))))
  87. (target (@ (name "check")
  88. (depends "compile-tests"))
  89. (mkdir (@ (dir "${test.home}/test-reports")))
  90. (junit (@ (printsummary "true")
  91. (showoutput "true")
  92. (fork "yes")
  93. (haltonfailure "yes"))
  94. (classpath
  95. (pathelement (@ (path "${env.CLASSPATH}")))
  96. (pathelement (@ (location "${test.home}/resources")))
  97. (pathelement (@ (location "${classes.dir}")))
  98. (pathelement (@ (location "${test.classes.dir}"))))
  99. (formatter (@ (type "plain")
  100. (usefile "true")))
  101. (batchtest (@ (fork "yes")
  102. (todir "${test.home}/test-reports"))
  103. (fileset (@ (dir "${test.home}/java"))
  104. ,@(map (lambda (file)
  105. `(include (@ (name ,file))))
  106. test-include)
  107. ,@(map (lambda (file)
  108. `(exclude (@ (name ,file))))
  109. test-exclude)))))
  110. (target (@ (name "jar")
  111. (depends "compile, manifest"))
  112. (mkdir (@ (dir "${jar.dir}")))
  113. (jar (@ (destfile ,(string-append "${jar.dir}/" jar-name))
  114. (manifest "${manifest.file}")
  115. (basedir "${classes.dir}"))))
  116. (target (@ (name "install"))
  117. (copy (@ (todir "${dist.dir}"))
  118. (fileset (@ (dir "${jar.dir}"))
  119. (include (@ (name "**/*.jar")))))))
  120. port)))
  121. (utime "build.xml" 0 0)
  122. #t)
  123. (define (generate-classpath inputs)
  124. "Return a colon-separated string of full paths to jar files found among the
  125. INPUTS."
  126. (string-join
  127. (apply append (map (match-lambda
  128. ((_ . dir)
  129. (find-files dir "\\.jar$")))
  130. inputs)) ":"))
  131. (define* (unpack #:key source #:allow-other-keys)
  132. "Unpack the jar archive SOURCE. When SOURCE is not a jar archive fall back
  133. to the default GNU unpack strategy."
  134. (if (string-suffix? ".jar" source)
  135. (begin
  136. (mkdir "src")
  137. (with-directory-excursion "src"
  138. (invoke "jar" "-xf" source))
  139. #t)
  140. ;; Use GNU unpack strategy for things that aren't jar archives.
  141. ((assq-ref gnu:%standard-phases 'unpack) #:source source)))
  142. (define* (configure #:key inputs outputs (jar-name #f)
  143. (source-dir "src")
  144. (test-dir "src/test")
  145. (main-class #f)
  146. (test-include '("**/*Test.java"))
  147. (test-exclude '("**/Abstract*.java")) #:allow-other-keys)
  148. (when jar-name
  149. (default-build.xml jar-name
  150. (string-append (assoc-ref outputs "out")
  151. "/share/java")
  152. source-dir test-dir main-class test-include test-exclude))
  153. (setenv "JAVA_HOME" (assoc-ref inputs "jdk"))
  154. (setenv "CLASSPATH" (generate-classpath inputs))
  155. #t)
  156. (define* (build #:key (make-flags '()) (build-target "jar")
  157. #:allow-other-keys)
  158. (apply invoke `("ant" ,build-target ,@make-flags)))
  159. (define (regular-jar-file-predicate file stat)
  160. "Predicate returning true if FILE is ending on '.jar'
  161. and STAT indicates it is a regular file."
  162. (and ((file-name-predicate "\\.jar$") file stat)
  163. (eq? 'regular (stat:type stat))))
  164. (define* (generate-jar-indices #:key outputs #:allow-other-keys)
  165. "Generate file \"META-INF/INDEX.LIST\". This file does not use word wraps
  166. and is preferred over \"META-INF/MANIFEST.MF\", which does use word wraps,
  167. by Java when resolving dependencies. So we make sure to create it so that
  168. grafting works - and so that the garbage collector doesn't collect
  169. dependencies of this jar file."
  170. (define (generate-index jar)
  171. (invoke "jar" "-i" jar))
  172. (for-each (match-lambda
  173. ((output . directory)
  174. (for-each generate-index
  175. (find-files
  176. directory
  177. regular-jar-file-predicate))))
  178. outputs)
  179. #t)
  180. (define* (strip-jar-timestamps #:key outputs
  181. #:allow-other-keys)
  182. "Unpack all jar archives, reset the timestamp of all contained files, and
  183. repack them. This is necessary to ensure that archives are reproducible."
  184. (define (repack-archive jar)
  185. (format #t "repacking ~a\n" jar)
  186. (let* ((dir (mkdtemp "jar-contents.XXXXXX"))
  187. (manifest (string-append dir "/META-INF/MANIFEST.MF")))
  188. (with-directory-excursion dir
  189. (invoke "jar" "xf" jar))
  190. (delete-file jar)
  191. ;; XXX: copied from (gnu build install)
  192. (for-each (lambda (file)
  193. (let ((s (lstat file)))
  194. (unless (eq? (stat:type s) 'symlink)
  195. (utime file 0 0 0 0))))
  196. (find-files dir #:directories? #t))
  197. ;; The jar tool will always set the timestamp on the manifest file
  198. ;; and the containing directory to the current time, even when we
  199. ;; reuse an existing manifest file. To avoid this we use "zip"
  200. ;; instead of "jar". It is important that the manifest appears
  201. ;; first.
  202. (with-directory-excursion dir
  203. (let* ((files (find-files "." ".*" #:directories? #t))
  204. ;; To ensure that the reference scanner can detect all
  205. ;; store references in the jars we disable compression
  206. ;; with the "-0" option.
  207. (command (if (file-exists? manifest)
  208. `("zip" "-0" "-X" ,jar ,manifest ,@files)
  209. `("zip" "-0" "-X" ,jar ,@files))))
  210. (apply invoke command)))
  211. (utime jar 0 0)
  212. #t))
  213. (for-each (match-lambda
  214. ((output . directory)
  215. (for-each repack-archive
  216. (find-files directory regular-jar-file-predicate))))
  217. outputs)
  218. #t)
  219. (define* (check #:key target (make-flags '()) (tests? (not target))
  220. (test-target "check")
  221. #:allow-other-keys)
  222. (if tests?
  223. (apply invoke `("ant" ,test-target ,@make-flags))
  224. (format #t "test suite not run~%"))
  225. #t)
  226. (define* (install #:key (make-flags '()) #:allow-other-keys)
  227. (apply invoke `("ant" "install" ,@make-flags)))
  228. (define %standard-phases
  229. (modify-phases gnu:%standard-phases
  230. (replace 'unpack unpack)
  231. (delete 'bootstrap)
  232. (replace 'configure configure)
  233. (replace 'build build)
  234. (replace 'check check)
  235. (replace 'install install)
  236. (add-after 'install 'reorder-jar-content
  237. strip-jar-timestamps)
  238. (add-after 'reorder-jar-content 'generate-jar-indices generate-jar-indices)
  239. (add-after 'generate-jar-indices 'strip-jar-timestamps
  240. strip-jar-timestamps)))
  241. (define* (ant-build #:key inputs (phases %standard-phases)
  242. #:allow-other-keys #:rest args)
  243. "Build the given Java package, applying all of PHASES in order."
  244. (apply gnu:gnu-build #:inputs inputs #:phases phases args))
  245. ;;; ant-build-system.scm ends here