3 MES=${MES-$(dirname $0)/mes}
4 PREFIX=${PREFIX-@PREFIX@}
5 MES_PREFIX=${MES_PREFIX-$PREFIX}
6 if [ "$MES_PREFIX" = @PREFIX""@ ]
8 MES_PREFIX=$(cd $(dirname $0)/.. && pwd)
10 MES_MODULEDIR=${MES_MODULEDIR-$MES_PREFIX/"module"}
13 MES_MODULEDIR=${MES_MODULEDIR-$MES_PREFIX/share/mes/"module"}
16 echo '()' | cat $MES_MODULEDIR/mes/base-0.mes $0 /dev/stdin | $MES $MES_FLAGS -- "$@"
21 ;;; Mes --- Maxwell Equations of Software
22 ;;; Copyright © 2016,2017,2018 Jan Nieuwenhuizen <janneke@gnu.org>
24 ;;; This file is part of Mes.
26 ;;; Mes is free software; you can redistribute it and/or modify it
27 ;;; under the terms of the GNU General Public License as published by
28 ;;; the Free Software Foundation; either version 3 of the License, or (at
29 ;;; your option) any later version.
31 ;;; Mes is distributed in the hope that it will be useful, but
32 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
33 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 ;;; GNU General Public License for more details.
36 ;;; You should have received a copy of the GNU General Public License
37 ;;; along with Mes. If not, see <http://www.gnu.org/licenses/>.
41 ;;; mescc.mes is a proof-of-concept simplistic C compiler and linker
46 ;;(mes-use-module (language c compiler))
49 (mes-use-module (mes guile))
50 (mes-use-module (mes getopt-long))
51 (mes-use-module (mes pretty-print))
52 (mes-use-module (language c99 info))
53 (mes-use-module (language c99 compiler))
54 (mes-use-module (mes display))
55 (mes-use-module (mes elf))
56 (mes-use-module (mes M1))
57 (mes-use-module (srfi srfi-1))
58 (mes-use-module (srfi srfi-26))
60 (format (current-error-port) "mescc.mes...\n")
62 (define %prefix (if (string-prefix? "@PREFIX" "@PREFIX@") (or (getenv "MES_PREFIX") "") "@PREFIX@"))
64 (define (parse-opts args)
66 '((c (single-char #\c))
67 (define (single-char #\D) (value #t))
70 (help (single-char #\h))
71 (include (single-char #\I) (value #t))
72 (o (single-char #\o) (value #t))
73 (version (single-char #\V))))
74 (options (getopt-long args option-spec))
75 (help? (option-ref options 'help #f))
76 (files (option-ref options '() '()))
77 (usage? (and (not help?) (null? files)))
78 (version? (option-ref options 'version #f)))
81 (format (current-output-port) "mescc.scm (mes) ~a\n" %version))
82 (and (or help? usage?)
83 (format (or (and usage? (current-error-port)) (current-output-port)) "\
84 Usage: mescc.mes [OPTION]... FILE...
85 -c compile and assemble, but do not link
86 -D DEFINE define DEFINE
87 -E preprocess only; do not compile, assemble or link
88 -g add debug info [GDB, objdump] TODO: hex2 footer
89 -h, --help display this help and exit
90 -I DIR append DIR to include path
91 -o FILE write output to FILE
92 -v, --version display version and exit
94 (exit (or (and usage? 2) 0)))
97 (define (read-object file)
98 (let ((char (with-input-from-file file read-char)))
99 (if (eq? char #\#) (error "hex2 format not supported:" file)))
100 (with-input-from-file file read))
102 (define (main:ast->info file)
103 (let ((ast (with-input-from-file file read)))
104 (with-input-from-file file
106 (c99-ast->info ast)))))
108 (define (source->ast defines includes)
110 (with-input-from-file file
112 (write (c99-input->ast #:defines defines #:includes includes))))))
114 (define (source->info defines includes)
116 (with-input-from-file file
118 ((c99-input->info #:defines defines #:includes includes))))))
121 (or (string-suffix? ".E" o)
122 (string-suffix? ".mes-E" o)))
125 (or (string-suffix? ".o" o)
126 (string-suffix? ".mes-o" o)))
129 (let* ((args (cons* (car args) (cdr (member "--" args))))
130 (options (parse-opts args))
131 (files (option-ref options '() '()))
133 (preprocess? (option-ref options 'E #f))
134 (compile? (option-ref options 'c #f))
135 (debug-info? (option-ref options 'g #f))
136 (asts (filter ast? files))
137 (objects (filter object? files))
138 (sources (filter (cut string-suffix? ".c" <>) files))
139 (base (substring file (1+ (or (string-rindex file #\/) -1)) (- (string-length file) 2)))
140 (out (option-ref options 'o (cond (compile? (string-append base ".o"))
141 (preprocess? (string-append base ".E"))
143 (multi-opt (lambda (option) (lambda (o) (and (eq? (car o) option) (cdr o)))))
144 (defines (reverse (filter-map (multi-opt 'define) options)))
145 (includes (reverse (filter-map (multi-opt 'include) options))))
146 (when (getenv "MES_DEBUG") (format (current-error-port) "options=~s\n" options)
147 (format (current-error-port) "output: ~a\n" out))
148 (if (and (pair? sources) (pair? objects)) (error "cannot mix source and object files:" files))
149 (with-output-to-port (open-output-file out (if (and (not compile?)
150 (not preprocess?)) S_IRWXU))
152 (cond ((pair? objects) (let ((objects (map read-object objects)))
153 (if compile? (objects->M1 objects)
154 (objects->elf objects))))
155 ((pair? asts) (let* ((infos (map main:ast->info asts))
156 (objects (map info->object infos)))
157 (if compile? (objects->M1 objects)
158 (objects->elf objects))))
159 ((pair? sources) (if preprocess? (map (source->ast defines includes) sources)
160 (let* ((infos (map (source->info defines includes) sources))
161 (objects (map info->object infos)))
162 (if compile? (objects->M1 objects)
163 (objects->elf objects))))))))))
165 (main (command-line))