mescc.scm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ;;; GNU Mes --- Maxwell Equations of Software
  2. ;;; Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Mes.
  5. ;;;
  6. ;;; GNU Mes is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Mes is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (mescc)
  19. #:use-module (srfi srfi-1)
  20. #:use-module (ice-9 getopt-long)
  21. #:use-module (mes guile)
  22. #:use-module (mes misc)
  23. #:use-module (mescc mescc)
  24. #:export (mescc:main))
  25. (cond-expand
  26. (mes
  27. (define (set-port-encoding! port encoding) #t)
  28. (mes-use-module (mes guile))
  29. (mes-use-module (mes misc))
  30. (mes-use-module (mes getopt-long))
  31. (mes-use-module (mes display))
  32. (mes-use-module (mescc mescc))
  33. )
  34. (guile
  35. (define-macro (mes-use-module . rest) #t)))
  36. (define %host-arch (or (getenv "%arch") %arch))
  37. (define %host-kernel (or (getenv "%kernel") "linux")) ;; FIXME
  38. (define %prefix (or (getenv "%prefix") "mes"))
  39. (define %includedir (or (getenv "%includedir") "include"))
  40. (define %libdir (or (getenv "%libdir") "."))
  41. (define %version (or (getenv "%version") "0.0"))
  42. (define %numbered-arch? (and=> (getenv "%numbered_arch") (lambda (x) (equal? x "true"))))
  43. (when (and=> (getenv "V") (lambda (v) (and (= (string-length v) 1) (> (string->number v) 1))))
  44. (format (current-error-port) "mescc[~a]...\n" %scheme))
  45. (define (unclump-single o)
  46. (cond ((string-prefix? "--" o) (list o))
  47. ((and (string-prefix? "-" o)
  48. (> (string-length o) 2)
  49. (not (eq? (string-ref o 2) #\space)))
  50. (list (substring o 0 2)
  51. (substring o 2)))
  52. (else (list o))))
  53. (define (parse-opts args)
  54. (let* ((option-spec
  55. '((align (value #t))
  56. (arch (value #t))
  57. (assemble (single-char #\c))
  58. (base-address (value #t))
  59. (compile (single-char #\S))
  60. (define (single-char #\D) (value #t))
  61. (debug-info (single-char #\g))
  62. (dumpmachine)
  63. (print-libgcc-file-name)
  64. (fno-builtin)
  65. (fno-stack-protector)
  66. (help (single-char #\h))
  67. (include (single-char #\I) (value #t))
  68. (library-dir (single-char #\L) (value #t))
  69. (library (single-char #\l) (value #t))
  70. (machine (single-char #\m) (value #t))
  71. (nodefaultlibs)
  72. (nostartfiles)
  73. (nostdinc)
  74. (nostdlib)
  75. (numbered-arch?)
  76. (preprocess (single-char #\E))
  77. (static)
  78. (std (value #t))
  79. (output (single-char #\o) (value #t))
  80. (optimize (single-char #\O) (value #t))
  81. (version (single-char #\V))
  82. (verbose (single-char #\v))
  83. (write (single-char #\w) (value #t))
  84. (language (single-char #\x) (value #t))))
  85. (options (getopt-long args option-spec))
  86. (help? (option-ref options 'help #f))
  87. (files (option-ref options '() '()))
  88. (dumpmachine? (option-ref options 'dumpmachine #f))
  89. (print-libgcc-file-name? (option-ref options 'print-libgcc-file-name #f))
  90. (version? (option-ref options 'version #f))
  91. (usage? (and (not dumpmachine?) (not print-libgcc-file-name?) (not help?) (not version?) (null? files))))
  92. (cond (version? (format #t "mescc (GNU Mes) ~a\n" %version) (exit 0))
  93. (else
  94. (and (or help? usage?)
  95. (format (or (and usage? (current-error-port)) (current-output-port)) "\
  96. Usage: mescc [OPTION]... FILE...
  97. C99 compiler in Scheme for bootstrapping the GNU system.
  98. Options:
  99. --align=SYMBOL align SYMBOL {functions,globals,none} [functions]
  100. --arch=ARCH compile for ARCH [~a]
  101. --kernel=ARCH compile for KERNEL [~a]
  102. -dumpmachine display the compiler's target machine
  103. --base-address=ADRRESS
  104. use BaseAddress ADDRESS [0x1000000]
  105. --numbered-arch mescc-tools use numbered arch
  106. -D DEFINE[=VALUE] define DEFINE [VALUE=1]
  107. -E preprocess only; do not compile, assemble or link
  108. -g add debug info [GDB, objdump] TODO: hex2 footer
  109. -h, --help display this help and exit
  110. -I DIR append DIR to include path
  111. -L DIR append DIR to library path
  112. -l LIBNAME link with LIBNAME
  113. -m BITS compile for BITS bits [32]
  114. -nodefaultlibs do not use libc.o nor libmescc.a when linking
  115. -nostartfiles do not use crt1.o when linking
  116. -nostdlib do not use crt1.o or libc.o or libmescc.a when linking
  117. -o FILE write output to FILE
  118. -O LEVEL use optimizing LEVEL
  119. -S preprocess and compile only; do not assemble or link
  120. --std=STANDARD assume that the input sources are for STANDARD
  121. -V,--version display version and exit
  122. -w,--write=TYPE dump Nyacc AST using TYPE {pretty-print,write}
  123. -x LANGUAGE specify LANGUAGE of the following input files
  124. Ignored for GCC compatibility
  125. -fno-builtin
  126. -fno-stack-protector
  127. -no-pie
  128. -nostdinc
  129. -static
  130. Environment variables:
  131. MES=BINARY run on mes-executable BINARY {mes,guile}
  132. MES_DEBUG=LEVEL show debug output with verbosity LEVEL {0..5}
  133. NYACC_TRACE=1 show Nyacc progress
  134. Report bugs to: bug-mes@gnu.org
  135. GNU Mes home page: <http://gnu.org/software/mes/>
  136. General help using GNU software: <http://gnu.org/gethelp/>
  137. " %host-arch %host-kernel)
  138. (exit (or (and usage? 2) 0)))
  139. options))))
  140. (define (mescc:main args)
  141. (let* ((single-dash-options '("-dumpmachine"
  142. "-fno-builtin"
  143. "-fno-stack-protector"
  144. "-no-pie"
  145. "-nodefaultlibs"
  146. "-nostartfiles"
  147. "-nostdinc"
  148. "-nostdlib"
  149. "-static"
  150. "-std"))
  151. (args (map (lambda (o)
  152. (if (member o single-dash-options) (string-append "-" o)
  153. o))
  154. args))
  155. (args (append-map unclump-single args))
  156. (options (parse-opts args))
  157. (options (acons 'prefix %prefix options))
  158. (options (acons 'includedir %includedir options))
  159. (options (acons 'libdir %libdir options))
  160. (arch (option-ref options 'arch %host-arch))
  161. (options (if arch (acons 'arch arch options) options))
  162. (kernel (option-ref options 'kernel %host-kernel))
  163. (options (acons 'kernel kernel options))
  164. (numbered-arch? (option-ref options 'numbered-arch? %numbered-arch?))
  165. (options (acons 'numbered-arch? numbered-arch? options))
  166. (dumpmachine? (option-ref options 'dumpmachine #f))
  167. (preprocess? (option-ref options 'preprocess #f))
  168. (print-libgcc-file-name? (option-ref options 'print-libgcc-file-name #f))
  169. (compile? (option-ref options 'compile #f))
  170. (assemble? (option-ref options 'assemble #f))
  171. (verbose? (count-opt options 'verbose)))
  172. (when verbose?
  173. (setenv "NYACC_TRACE" "yes")
  174. (when (> verbose? 1)
  175. (format (current-error-port) "options=~s\n" options)))
  176. (cond (dumpmachine? (display (mescc:get-host options)))
  177. (print-libgcc-file-name? (display "-lmescc\n"))
  178. (preprocess? (mescc:preprocess options))
  179. (compile? (mescc:compile options))
  180. (assemble? (mescc:assemble options))
  181. (else (mescc:link options)))))
  182. (define main mescc:main)