4 GUILEDIR=${GUILEDIR-@GUILEDIR@}
5 [ "$GODIR" = @"GODIR"@ ] && GODIR=$(dirname $0)
6 [ "$GUILEDIR" = @"GUILEDIR"@ ] && GUILEDIR=$(dirname $0)
7 export GUILE_AUTO_COMPILE=${GUILE_AUTO_COMPILE-0}
8 exec ${GUILE-guile} -L $GUILEDIR -C $GODIR -e '(mescc)' -s "$0" "$@"
11 ;;; Mes --- The Maxwell Equations of Software
12 ;;; Copyright © 2016,2017 Jan Nieuwenhuizen <janneke@gnu.org>
14 ;;; This file is part of GNU Guix.
16 ;;; Mes is free software; you can redistribute it and/or modify it
17 ;;; under the terms of the GNU General Public License as published by
18 ;;; the Free Software Foundation; either version 3 of the License, or (at
19 ;;; your option) any later version.
21 ;;; Mes is distributed in the hope that it will be useful, but
22 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;;; GNU General Public License for more details.
26 ;;; You should have received a copy of the GNU General Public License
27 ;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
29 ;; The Maxwell Equations of Software -- John McCarthy page 13
30 ;; http://www.softwarepreservation.org/projects/LISP/book/LISP%201.5%20Programmers%20Manual.pdf
34 GUILE='~/src/guile-1.8/build/pre-inst-guile --debug -q' guile/mescc.scm
37 (define-module (mescc)
38 #:use-module (language c99 info)
39 #:use-module (language c99 compiler)
40 #:use-module (mes elf)
42 #:use-module (ice-9 getopt-long)
43 #:use-module (ice-9 pretty-print)
44 #:use-module (srfi srfi-1)
45 #:use-module (srfi srfi-26)
48 (define %prefix (if (string-prefix? "@PREFIX" "@PREFIX@") (or (getenv "PREFIX") "") "@PREFIX@"))
49 (module-define! (resolve-module '(language c99 compiler)) '%prefix %prefix)
51 (define (parse-opts args)
53 '((c (single-char #\c))
54 (D (single-char #\D) (value #t))
57 (help (single-char #\h))
58 (I (single-char #\I) (value #t))
59 (o (single-char #\o) (value #t))
60 (version (single-char #\V) (value #t))))
61 (options (getopt-long args option-spec))
62 (help? (option-ref options 'help #f))
63 (files (option-ref options '() '()))
64 (usage? (and (not help?) (null? files)))
65 (version? (option-ref options 'version #f)))
68 (format (current-output-port) "mescc.scm (mes) ~a\n" %version))
69 (and (or help? usage?)
70 (format (or (and usage? (current-error-port)) (current-output-port)) "\
71 Usage: mescc.scm [OPTION]... FILE...
72 -c compile and assemble, but do not link
73 -D DEFINE define DEFINE
74 -E preprocess only; do not compile, assemble or link
75 -g add debug info [GDB, objdump] TODO: hex2 footer
76 -h, --help display this help and exit
77 -I DIR append DIR to include path
78 -o FILE write output to FILE
79 -v, --version display version and exit
81 (exit (or (and usage? 2) 0)))
84 (define (read-object file)
85 (let ((char (with-input-from-file file read-char)))
86 (if (eq? char #\#) (error "hex2 format not supported:" file)))
87 (with-input-from-file file read))
89 (define (main:ast->info file)
90 (let ((ast (with-input-from-file file read)))
91 (with-input-from-file file
93 (c99-ast->info ast)))))
95 (define (source->ast defines includes)
97 (with-input-from-file file
99 (pretty-print (c99-input->ast #:defines defines #:includes includes))))))
101 (define (source->info defines includes)
103 (with-input-from-file file
105 ((c99-input->info #:defines defines #:includes includes))))))
108 (or (string-suffix? ".E" o)
109 (string-suffix? ".guile-E" o)))
112 (or (string-suffix? ".o" o)
113 (string-suffix? ".guile-o" o)))
116 (let* ((options (parse-opts args))
117 (files (option-ref options '() '()))
119 (preprocess? (option-ref options 'E #f))
120 (compile? (option-ref options 'c #f))
121 (debug-info? (option-ref options 'g #f))
122 (asts (filter ast? files))
123 (objects (filter object? files))
124 (sources (filter (cut string-suffix? ".c" <>) files))
125 (base (substring file (1+ (or (string-rindex file #\/) -1)) (- (string-length file) 2)))
126 (out (option-ref options 'o (cond (compile? (string-append base ".o"))
127 (preprocess? (string-append base ".E"))
129 (multi-opt (lambda (option) (lambda (o) (and (eq? (car o) option) (cdr o)))))
130 (defines (reverse (filter-map (multi-opt 'D) options)))
131 (includes (reverse (filter-map (multi-opt 'I) options))))
132 (when (getenv "MES_DEBUG") (format (current-error-port) "options=~s\n" options)
133 (format (current-error-port) "output: ~a\n" out))
134 (if (and (pair? sources) (pair? objects)) (error "cannot mix source and object files:" files))
135 (with-output-to-file out
137 (if (and (not compile?)
138 (not preprocess?)) (set-port-encoding! (current-output-port) "ISO-8859-1"))
139 (cond ((pair? objects) (let ((objects (map read-object objects)))
140 (if compile? (objects->M1 objects)
141 (objects->elf objects))))
142 ((pair? asts) (let* ((infos (map main:ast->info asts))
143 (objects (map info->object infos)))
144 (if compile? (objects->M1 objects)
145 (objects->elf objects))))
146 ((pair? sources) (if preprocess? (map (source->ast defines includes) sources)
147 (let* ((infos (map (source->info defines includes) sources))
148 (objects (map info->object infos)))
149 (if compile? (objects->M1 objects)
150 (objects->elf objects))))))))
151 (if (and (not compile?)