2018-07-22 12:24:36 +00:00
|
|
|
;;; GNU Mes --- Maxwell Equations of Software
|
2020-08-23 07:52:01 +00:00
|
|
|
;;; Copyright © 2016,2017,2018,2019,2020 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
2018-05-25 06:05:02 +00:00
|
|
|
;;;
|
2018-07-22 12:24:36 +00:00
|
|
|
;;; This file is part of GNU Mes.
|
2018-05-25 06:05:02 +00:00
|
|
|
;;;
|
2018-07-22 12:24:36 +00:00
|
|
|
;;; GNU Mes is free software; you can redistribute it and/or modify it
|
2018-05-25 06:05:02 +00:00
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
2018-07-22 12:24:36 +00:00
|
|
|
;;; GNU Mes is distributed in the hope that it will be useful, but
|
2018-05-25 06:05:02 +00:00
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
2018-07-22 12:24:36 +00:00
|
|
|
;;; along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
|
2018-05-25 06:05:02 +00:00
|
|
|
|
|
|
|
(define-module (mescc mescc)
|
|
|
|
#:use-module (srfi srfi-1)
|
|
|
|
#:use-module (srfi srfi-26)
|
|
|
|
#:use-module (ice-9 pretty-print)
|
|
|
|
#:use-module (ice-9 getopt-long)
|
|
|
|
#:use-module (mes misc)
|
|
|
|
|
2020-11-25 17:33:51 +00:00
|
|
|
#:use-module (mescc info)
|
2020-06-06 17:51:45 +00:00
|
|
|
#:use-module (mescc armv4 info)
|
2018-08-14 10:35:24 +00:00
|
|
|
#:use-module (mescc i386 info)
|
2018-08-14 18:32:56 +00:00
|
|
|
#:use-module (mescc x86_64 info)
|
2018-05-25 06:05:02 +00:00
|
|
|
#:use-module (mescc preprocess)
|
|
|
|
#:use-module (mescc compile)
|
|
|
|
#:use-module (mescc M1)
|
2019-07-27 07:51:21 +00:00
|
|
|
#:export (count-opt
|
|
|
|
mescc:preprocess
|
2019-05-22 15:55:20 +00:00
|
|
|
mescc:get-host
|
2018-05-25 06:05:02 +00:00
|
|
|
mescc:compile
|
|
|
|
mescc:assemble
|
2019-07-27 07:51:21 +00:00
|
|
|
mescc:link
|
|
|
|
multi-opt))
|
2018-05-25 06:05:02 +00:00
|
|
|
|
2018-08-14 08:58:02 +00:00
|
|
|
(define GUILE-with-output-to-file with-output-to-file)
|
|
|
|
(define (with-output-to-file file-name thunk)
|
|
|
|
(if (equal? file-name "-") (thunk)
|
|
|
|
(GUILE-with-output-to-file file-name thunk)))
|
|
|
|
|
2018-05-25 06:05:02 +00:00
|
|
|
(define (mescc:preprocess options)
|
2018-08-15 17:11:54 +00:00
|
|
|
(let* ((pretty-print/write (string->symbol (option-ref options 'write (if guile? "pretty-print" "write"))))
|
2018-05-25 06:05:02 +00:00
|
|
|
(pretty-print/write (if (eq? pretty-print/write 'pretty-print) pretty-print write))
|
|
|
|
(files (option-ref options '() '("a.c")))
|
|
|
|
(input-file-name (car files))
|
2019-07-08 17:42:31 +00:00
|
|
|
(input-base (basename input-file-name))
|
2018-05-25 06:05:02 +00:00
|
|
|
(ast-file-name (cond ((and (option-ref options 'preprocess #f)
|
|
|
|
(option-ref options 'output #f)))
|
2019-07-08 17:42:31 +00:00
|
|
|
(else (replace-suffix input-base ".E"))))
|
2018-08-15 17:11:54 +00:00
|
|
|
(dir (dirname input-file-name))
|
|
|
|
(defines (reverse (filter-map (multi-opt 'define) options)))
|
|
|
|
(includes (reverse (filter-map (multi-opt 'include) options)))
|
2019-11-24 10:14:14 +00:00
|
|
|
(includes (cons (option-ref options 'includedir #f) includes))
|
2018-08-15 17:11:54 +00:00
|
|
|
(includes (cons dir includes))
|
2018-08-14 18:32:56 +00:00
|
|
|
(prefix (option-ref options 'prefix ""))
|
|
|
|
(machine (option-ref options 'machine "32"))
|
2019-07-26 20:44:04 +00:00
|
|
|
(arch (arch-get options))
|
2020-11-25 17:33:51 +00:00
|
|
|
(defines (append (arch-get-defines options) defines))
|
2019-07-27 07:51:21 +00:00
|
|
|
(verbose? (count-opt options 'verbose)))
|
2018-05-25 06:05:02 +00:00
|
|
|
(with-output-to-file ast-file-name
|
2019-07-27 07:51:21 +00:00
|
|
|
(lambda _ (for-each (cut c->ast prefix defines includes arch pretty-print/write verbose? <>) files)))))
|
2018-05-25 06:05:02 +00:00
|
|
|
|
2019-07-27 07:51:21 +00:00
|
|
|
(define (c->ast prefix defines includes arch write verbose? file-name)
|
2018-05-25 06:05:02 +00:00
|
|
|
(with-input-from-file file-name
|
2019-07-27 07:51:21 +00:00
|
|
|
(cut write (c99-input->ast #:prefix prefix #:defines defines #:includes includes #:arch arch #:verbose? verbose?))))
|
2018-05-25 06:05:02 +00:00
|
|
|
|
|
|
|
(define (mescc:compile options)
|
|
|
|
(let* ((files (option-ref options '() '("a.c")))
|
|
|
|
(input-file-name (car files))
|
2019-07-08 17:42:31 +00:00
|
|
|
(input-base (basename input-file-name))
|
2018-05-25 06:05:02 +00:00
|
|
|
(M1-file-name (cond ((and (option-ref options 'compile #f)
|
|
|
|
(option-ref options 'output #f)))
|
2019-12-06 20:17:13 +00:00
|
|
|
((string-suffix? ".S" input-file-name) input-file-name)
|
2019-07-08 17:42:31 +00:00
|
|
|
(else (replace-suffix input-base ".s"))))
|
2018-05-25 06:05:02 +00:00
|
|
|
(infos (map (cut file->info options <>) files))
|
2019-07-27 07:51:21 +00:00
|
|
|
(verbose? (count-opt options 'verbose))
|
2018-10-06 13:56:14 +00:00
|
|
|
(align? (option-ref options 'align #f)))
|
2018-05-25 06:05:02 +00:00
|
|
|
(when verbose?
|
|
|
|
(stderr "dumping: ~a\n" M1-file-name))
|
|
|
|
(with-output-to-file M1-file-name
|
2019-07-27 07:51:21 +00:00
|
|
|
(cut infos->M1 M1-file-name infos #:align? align? #:verbose? verbose?))
|
2018-05-25 06:05:02 +00:00
|
|
|
M1-file-name))
|
|
|
|
|
|
|
|
(define (file->info options file-name)
|
|
|
|
(cond ((.c? file-name) (c->info options file-name))
|
|
|
|
((.E? file-name) (E->info options file-name))))
|
|
|
|
|
|
|
|
(define (c->info options file-name)
|
2019-11-24 10:14:14 +00:00
|
|
|
(let* ((dir (dirname file-name))
|
|
|
|
(defines (reverse (filter-map (multi-opt 'define) options)))
|
2018-08-15 17:11:54 +00:00
|
|
|
(includes (reverse (filter-map (multi-opt 'include) options)))
|
2019-11-24 10:14:14 +00:00
|
|
|
(includes (cons (option-ref options 'includedir #f) includes))
|
2018-08-15 17:11:54 +00:00
|
|
|
(includes (cons dir includes))
|
2018-08-14 18:32:56 +00:00
|
|
|
(prefix (option-ref options 'prefix ""))
|
2020-11-25 17:33:51 +00:00
|
|
|
(defines (append (arch-get-defines options) defines))
|
2019-07-27 07:51:21 +00:00
|
|
|
(arch (arch-get options))
|
|
|
|
(verbose? (count-opt options 'verbose)))
|
2018-05-25 06:05:02 +00:00
|
|
|
(with-input-from-file file-name
|
2019-07-27 07:51:21 +00:00
|
|
|
(cut c99-input->info (arch-get-info options) #:prefix prefix #:defines defines #:includes includes #:arch arch #:verbose? verbose?))))
|
2018-05-25 06:05:02 +00:00
|
|
|
|
|
|
|
(define (E->info options file-name)
|
2019-07-27 07:51:21 +00:00
|
|
|
(let ((ast (with-input-from-file file-name read))
|
|
|
|
(verbose? (count-opt options 'verbose)))
|
|
|
|
(c99-ast->info (arch-get-info options) ast #:verbose? verbose?)))
|
2018-05-25 06:05:02 +00:00
|
|
|
|
|
|
|
(define (mescc:assemble options)
|
|
|
|
(let* ((files (option-ref options '() '("a.c")))
|
|
|
|
(input-file-name (car files))
|
2019-07-08 17:42:31 +00:00
|
|
|
(input-base (basename input-file-name))
|
2018-05-25 06:05:02 +00:00
|
|
|
(hex2-file-name (cond ((and (option-ref options 'assemble #f)
|
|
|
|
(option-ref options 'output #f)))
|
2019-07-08 17:42:31 +00:00
|
|
|
(else (replace-suffix input-base ".o"))))
|
2019-06-13 12:19:59 +00:00
|
|
|
(s-files (filter .s? files))
|
2018-05-25 06:05:02 +00:00
|
|
|
(hex2-files M1->hex2 ) ;; FIXME
|
|
|
|
(source-files (filter (disjoin .c? .E?) files))
|
|
|
|
(infos (map (cut file->info options <>) source-files)))
|
2019-06-13 12:19:59 +00:00
|
|
|
(if (and (pair? s-files) (pair? infos))
|
|
|
|
(error "mixing source and object not supported:" source-files s-files))
|
|
|
|
(when (pair? s-files)
|
|
|
|
(M1->hex2 options s-files))
|
2018-05-25 06:05:02 +00:00
|
|
|
(when (pair? infos)
|
|
|
|
(infos->hex2 options hex2-file-name infos))
|
|
|
|
hex2-file-name))
|
|
|
|
|
|
|
|
(define (mescc:link options)
|
|
|
|
(let* ((files (option-ref options '() '("a.c")))
|
|
|
|
(source-files (filter (disjoin .c? .E?) files))
|
2019-06-13 12:19:59 +00:00
|
|
|
(s-files (filter .s? files))
|
2018-05-25 06:05:02 +00:00
|
|
|
(o-files (filter .o? files))
|
|
|
|
(input-file-name (car files))
|
|
|
|
(hex2-file-name (if (or (string-suffix? ".hex2" input-file-name)
|
|
|
|
(string-suffix? ".o" input-file-name)) input-file-name
|
|
|
|
(replace-suffix input-file-name ".o")))
|
|
|
|
(infos (map (cut file->info options <>) source-files))
|
2019-06-13 12:19:59 +00:00
|
|
|
(s-files (filter .s? files))
|
2018-05-25 06:05:02 +00:00
|
|
|
(hex2-files (filter .o? files))
|
2019-06-13 12:19:59 +00:00
|
|
|
(hex2-files (if (null? s-files) hex2-files
|
|
|
|
(append hex2-files (list (M1->hex2 options s-files)))))
|
2018-05-25 06:05:02 +00:00
|
|
|
(hex2-files (if (null? infos) hex2-files
|
|
|
|
(append hex2-files
|
|
|
|
(list (infos->hex2 options hex2-file-name infos)))))
|
2019-05-05 18:25:22 +00:00
|
|
|
(default-libraries (if (or (option-ref options 'nodefaultlibs #f)
|
Introduce libmescc.a; Put division by integer in there; split syscalls' errno off.
* build-aux/configure-lib.sh (libmescc_SOURCES): Add lib/mes/div.c,
lib/linux/*/syscall-internal.c.
* build-aux/build-lib.sh: Add libmescc.a.
* build-aux/build-mes.sh: On gcc, add "-lmescc".
* build-aux/test-c.sh: Add "-lmescc".
* build-aux/check.sh.in: Add mescc to LIBS.
* module/mescc/mescc.scm (mescc:link): Add "mescc".
* module/mescc.scm (mescc:main): Update documentation of "-nodefaultlibs"
and "-nostdlib".
* lib/mes/div.c (ldiv): Rename to...
(__mesabi_ldiv): ...this. Avoid assert.
(__mesabi_div0): Avoid assert.
(__aeabi_idivmod): New procedure.
(__aeabi_idiv): New procedure.
(__aeabi_uidivmod): New procedure.
(__aeabi_uidiv): New procedure.
* lib/linux/x86-mes-gcc/syscall.c (__sys_call, __sys_call1, __sys_call2,
__sys_call3, __sys_call4): Move to...
* lib/linux/x86-mes-gcc/syscall-internal.c: ...here.
(__raise): New procedure.
* lib/linux/x86-mes-mescc/syscall.c (__sys_call, __sys_call1, __sys_call2,
__sys_call3, __sys_call4): Move to...
* lib/linux/x86-mes-mescc/syscall-internal.c: ...here.
(__raise): New procedure.
* lib/linux/arm-mes-gcc/syscall.c: New file.
* lib/linux/arm-mes-gcc/syscall-internal.c: New file.
* lib/linux/arm-mes-mescc/syscall.c: New file.
* lib/linux/arm-mes-mescc/syscall-internal.c: New file.
* lib/gnu/syscall.c (__syscall, __syscall2, __syscall_get, __syscall_put):
Move to...
* lib/gnu/syscall-internal.c: ...here.
2020-06-01 21:47:49 +00:00
|
|
|
(option-ref options 'nostdlib #f))
|
|
|
|
'()
|
|
|
|
'("mescc" "c")))
|
2018-05-25 06:05:02 +00:00
|
|
|
(libraries (filter-map (multi-opt 'library) options))
|
2019-05-05 18:25:22 +00:00
|
|
|
(libraries (delete-duplicates (append libraries default-libraries)))
|
2019-06-08 13:36:22 +00:00
|
|
|
(hex2-libraries (map (cut find-library options ".a" <>) libraries))
|
2018-05-25 06:05:02 +00:00
|
|
|
(hex2-files (append hex2-files hex2-libraries))
|
2019-06-13 12:19:59 +00:00
|
|
|
(s-files (append s-files (map (cut find-library options ".s" <>) libraries)))
|
2018-05-25 06:05:02 +00:00
|
|
|
(debug-info? (option-ref options 'debug-info #f))
|
2019-12-06 20:17:13 +00:00
|
|
|
(s-files (if (string-suffix? ".S" input-file-name) s-files
|
|
|
|
(cons (replace-suffix input-file-name ".s") s-files)))
|
2018-05-25 06:05:02 +00:00
|
|
|
(elf-footer (and debug-info?
|
2019-06-13 12:19:59 +00:00
|
|
|
(or (M1->blood-elf options s-files)
|
2018-05-25 06:05:02 +00:00
|
|
|
(exit 1)))))
|
|
|
|
(or (hex2->elf options hex2-files #:elf-footer elf-footer)
|
|
|
|
(exit 1))))
|
|
|
|
|
|
|
|
(define (infos->hex2 options hex2-file-name infos)
|
|
|
|
(let* ((input-file-name (car (option-ref options '() '("a.c"))))
|
2019-06-13 12:19:59 +00:00
|
|
|
(M1-file-name (replace-suffix hex2-file-name ".s"))
|
2018-05-25 06:05:02 +00:00
|
|
|
(options (acons 'compile #t options)) ; ugh
|
|
|
|
(options (acons 'output hex2-file-name options))
|
2019-07-27 07:51:21 +00:00
|
|
|
(verbose? (count-opt options 'verbose))
|
2018-10-06 13:56:14 +00:00
|
|
|
(align? (option-ref options 'align #f)))
|
2018-05-25 06:05:02 +00:00
|
|
|
(when verbose?
|
|
|
|
(stderr "dumping: ~a\n" M1-file-name))
|
|
|
|
(with-output-to-file M1-file-name
|
2018-10-06 13:56:14 +00:00
|
|
|
(cut infos->M1 M1-file-name infos #:align? align?))
|
2018-05-25 06:05:02 +00:00
|
|
|
(or (M1->hex2 options (list M1-file-name))
|
|
|
|
(exit 1))))
|
|
|
|
|
|
|
|
(define (M1->hex2 options M1-files)
|
|
|
|
(let* ((input-file-name (car (option-ref options '() '("a.c"))))
|
2019-07-08 17:42:31 +00:00
|
|
|
(input-base (basename input-file-name))
|
2018-05-25 06:05:02 +00:00
|
|
|
(M1-file-name (car M1-files))
|
|
|
|
(hex2-file-name (cond ((and (option-ref options 'assemble #f)
|
|
|
|
(option-ref options 'output #f)))
|
|
|
|
((option-ref options 'assemble #f)
|
2019-07-08 17:42:31 +00:00
|
|
|
(replace-suffix input-base ".o"))
|
2018-05-25 06:05:02 +00:00
|
|
|
(else (replace-suffix M1-file-name ".o"))))
|
2019-07-27 07:51:21 +00:00
|
|
|
(verbose? (count-opt options 'verbose))
|
2018-05-25 06:05:02 +00:00
|
|
|
(M1 (or (getenv "M1") "M1"))
|
|
|
|
(command `(,M1
|
|
|
|
"--LittleEndian"
|
2019-12-07 13:39:19 +00:00
|
|
|
,@(arch-get-architecture options)
|
2019-05-22 15:55:20 +00:00
|
|
|
"-f" ,(arch-find options (arch-get-m1-macros options))
|
2018-05-25 06:05:02 +00:00
|
|
|
,@(append-map (cut list "-f" <>) M1-files)
|
|
|
|
"-o" ,hex2-file-name)))
|
2019-07-27 07:51:21 +00:00
|
|
|
(when (and verbose? (> verbose? 1))
|
2018-05-25 06:05:02 +00:00
|
|
|
(stderr "~a\n" (string-join command)))
|
2018-07-09 07:25:38 +00:00
|
|
|
(and (zero? (apply assert-system* command))
|
2018-05-25 06:05:02 +00:00
|
|
|
hex2-file-name)))
|
|
|
|
|
|
|
|
(define* (hex2->elf options hex2-files #:key elf-footer)
|
|
|
|
(let* ((input-file-name (car (option-ref options '() '("a.c"))))
|
|
|
|
(elf-file-name (cond ((option-ref options 'output #f))
|
2019-05-29 14:49:22 +00:00
|
|
|
(else "a.out")))
|
2019-07-27 07:51:21 +00:00
|
|
|
(verbose? (count-opt options 'verbose))
|
2018-05-25 06:05:02 +00:00
|
|
|
(hex2 (or (getenv "HEX2") "hex2"))
|
2018-08-14 18:32:56 +00:00
|
|
|
(base-address (option-ref options 'base-address "0x1000000"))
|
2019-05-22 15:55:20 +00:00
|
|
|
(machine (arch-get-machine options))
|
2019-12-11 07:16:46 +00:00
|
|
|
(elf-footer
|
|
|
|
(or elf-footer
|
|
|
|
(kernel-find
|
|
|
|
options
|
|
|
|
(string-append "elf" machine "-footer-single-main.hex2"))))
|
2019-05-05 18:25:22 +00:00
|
|
|
(start-files (if (or (option-ref options 'nostartfiles #f)
|
|
|
|
(option-ref options 'nostdlib #f)) '()
|
|
|
|
`("-f" ,(arch-find options "crt1.o"))))
|
2018-05-25 06:05:02 +00:00
|
|
|
(command `(,hex2
|
|
|
|
"--LittleEndian"
|
2019-12-07 13:39:19 +00:00
|
|
|
,@(arch-get-architecture options)
|
2018-08-14 18:32:56 +00:00
|
|
|
"--BaseAddress" ,base-address
|
2019-12-11 07:16:46 +00:00
|
|
|
"-f" ,(kernel-find
|
|
|
|
options
|
|
|
|
(string-append "elf" machine "-header.hex2"))
|
2019-05-05 18:25:22 +00:00
|
|
|
,@start-files
|
2018-05-25 06:05:02 +00:00
|
|
|
,@(append-map (cut list "-f" <>) hex2-files)
|
|
|
|
"-f" ,elf-footer
|
|
|
|
"--exec_enable"
|
|
|
|
"-o" ,elf-file-name)))
|
2019-07-27 07:51:21 +00:00
|
|
|
(when (and verbose? (> verbose? 1))
|
2018-05-29 18:07:46 +00:00
|
|
|
(stderr "~a\n" (string-join command)))
|
2018-07-09 07:25:38 +00:00
|
|
|
(and (zero? (apply assert-system* command))
|
2018-05-25 06:05:02 +00:00
|
|
|
elf-file-name)))
|
|
|
|
|
|
|
|
(define (M1->blood-elf options M1-files)
|
|
|
|
(let* ((M1-file-name (car M1-files))
|
|
|
|
(M1-blood-elf-footer (string-append M1-file-name ".blood-elf"))
|
|
|
|
(hex2-file-name (replace-suffix M1-file-name ".o"))
|
|
|
|
(blood-elf-footer (string-append hex2-file-name ".blood-elf"))
|
2019-07-27 07:51:21 +00:00
|
|
|
(verbose? (count-opt options 'verbose))
|
2018-05-25 06:05:02 +00:00
|
|
|
(blood-elf (or (getenv "BLOOD_ELF") "blood-elf"))
|
|
|
|
(command `(,blood-elf
|
2019-05-22 15:55:20 +00:00
|
|
|
"-f" ,(arch-find options (arch-get-m1-macros options))
|
2018-05-25 06:05:02 +00:00
|
|
|
,@(append-map (cut list "-f" <>) M1-files)
|
|
|
|
"-o" ,M1-blood-elf-footer)))
|
2019-07-27 07:51:21 +00:00
|
|
|
(when (and verbose? (> verbose? 1))
|
2020-08-23 07:52:01 +00:00
|
|
|
(format (current-error-port) "~a\n" (string-join command)))
|
2018-07-09 07:25:38 +00:00
|
|
|
(and (zero? (apply assert-system* command))
|
2018-05-25 06:05:02 +00:00
|
|
|
(let* ((options (acons 'compile #t options)) ; ugh
|
|
|
|
(options (acons 'output blood-elf-footer options)))
|
|
|
|
(M1->hex2 options (list M1-blood-elf-footer))))))
|
|
|
|
|
|
|
|
(define (replace-suffix file-name suffix)
|
|
|
|
(let* ((parts (string-split file-name #\.))
|
2020-08-23 07:52:01 +00:00
|
|
|
(base (if (pair? (cdr parts)) (drop-right parts 1) (list file-name)))
|
|
|
|
(old-suffix (if (pair? (cdr parts)) (last parts) ""))
|
2019-05-22 15:55:20 +00:00
|
|
|
(program-prefix (cond ((string-prefix? "arm-mes-" old-suffix) ".arm-mes-")
|
|
|
|
((string-prefix? "x86-mes-" old-suffix) ".x86-mes-")
|
2018-11-08 13:46:46 +00:00
|
|
|
((string-prefix? "x86_64-mes-" old-suffix) ".x86_64-mes-")
|
|
|
|
(else "."))))
|
|
|
|
(if (string-null? suffix)
|
|
|
|
(if (string-null? program-prefix) (string-join base ".")
|
|
|
|
(string-append (string-drop program-prefix 1) (string-join base ".")))
|
|
|
|
(string-append (string-join base ".") program-prefix (string-drop suffix 1)))))
|
2018-05-25 06:05:02 +00:00
|
|
|
|
2018-05-29 18:07:46 +00:00
|
|
|
(define (find-library options ext o)
|
|
|
|
(arch-find options (string-append "lib" o ext)))
|
|
|
|
|
2019-12-11 07:16:46 +00:00
|
|
|
(define* (arch-find options file-name #:key kernel)
|
2018-07-25 06:14:13 +00:00
|
|
|
(let* ((srcdest (or (getenv "srcdest") ""))
|
|
|
|
(srcdir-lib (string-append srcdest "lib"))
|
2020-08-30 13:00:26 +00:00
|
|
|
(srcdir-mescc-lib (string-append srcdest "mescc-lib"))
|
|
|
|
(libdir (option-ref options 'libdir "lib"))
|
|
|
|
(libdir-mescc (string-append
|
|
|
|
(dirname (option-ref options 'libdir "lib"))
|
|
|
|
"/mescc-lib"))
|
2019-05-22 15:55:20 +00:00
|
|
|
(arch (string-append (arch-get options) "-mes"))
|
2020-08-30 13:00:26 +00:00
|
|
|
(path (append (if (getenv "MES_UNINSTALLED")
|
|
|
|
(list srcdir-mescc-lib
|
|
|
|
srcdir-lib
|
|
|
|
libdir-mescc)
|
|
|
|
'())
|
|
|
|
(list libdir)
|
|
|
|
(or (and=> (getenv "LIBRARY_PATH")
|
|
|
|
(cut string-split <> #\:)) '())
|
|
|
|
(filter-map (multi-opt 'library-dir) options)))
|
2018-08-14 18:32:56 +00:00
|
|
|
(arch-file-name (string-append arch "/" file-name))
|
2019-12-11 07:16:46 +00:00
|
|
|
(arch-file-name (if kernel (string-append kernel "/" arch-file-name)
|
|
|
|
arch-file-name))
|
2019-07-27 07:51:21 +00:00
|
|
|
(verbose? (count-opt options 'verbose)))
|
2018-11-08 13:46:46 +00:00
|
|
|
(let ((file (search-path path arch-file-name)))
|
2019-07-27 07:51:21 +00:00
|
|
|
(when (and verbose? (> verbose? 1))
|
2018-11-08 13:46:46 +00:00
|
|
|
(stderr "arch-find=~s\n" arch-file-name)
|
|
|
|
(stderr " path=~s\n" path)
|
|
|
|
(stderr " => ~s\n" file))
|
|
|
|
(or file
|
|
|
|
(error (format #f "mescc: file not found: ~s" arch-file-name))))))
|
2018-05-29 18:07:46 +00:00
|
|
|
|
2019-12-11 07:16:46 +00:00
|
|
|
(define (kernel-find options file-name)
|
|
|
|
(let ((kernel (option-ref options 'kernel "linux")))
|
|
|
|
(or (arch-find options file-name #:kernel kernel)
|
|
|
|
(arch-find options file-name))))
|
|
|
|
|
2018-07-07 15:31:06 +00:00
|
|
|
(define (assert-system* . args)
|
|
|
|
(let ((status (apply system* args)))
|
2018-07-09 07:25:38 +00:00
|
|
|
(when (not (zero? status))
|
2018-07-07 15:31:06 +00:00
|
|
|
(stderr "mescc: failed: ~a\n" (string-join args))
|
2018-10-16 18:40:35 +00:00
|
|
|
(exit (status:exit-val status)))
|
2018-07-09 07:25:38 +00:00
|
|
|
status))
|
2018-05-29 18:07:46 +00:00
|
|
|
|
2019-05-22 15:55:20 +00:00
|
|
|
(define (arch-get options)
|
|
|
|
(let* ((machine (option-ref options 'machine #f))
|
|
|
|
(arch (option-ref options 'arch #f)))
|
|
|
|
(if machine (cond ((member arch '("x86" "x86_64")) (cond ((equal? machine "32") "x86")
|
|
|
|
((equal? machine "64") "x86_64")))
|
2020-06-01 22:03:03 +00:00
|
|
|
((equal? arch "arm") (cond ((equal? machine "32") "arm")
|
|
|
|
((equal? machine "arm") "arm"))))
|
2019-05-22 15:55:20 +00:00
|
|
|
arch)))
|
|
|
|
|
|
|
|
(define (mescc:get-host options)
|
|
|
|
(let ((cpu (arch-get options))
|
2019-12-11 07:16:46 +00:00
|
|
|
(kernel (option-ref options 'kernel "linux")))
|
2019-05-22 15:55:20 +00:00
|
|
|
(string-join (list cpu kernel "mes") "-")))
|
|
|
|
|
|
|
|
(define (arch-get-info options)
|
|
|
|
(let ((arch (arch-get options)))
|
|
|
|
(cond ((equal? arch "arm") (armv4-info))
|
|
|
|
((equal? arch "x86") (x86-info))
|
|
|
|
((equal? arch "x86_64") (x86_64-info)))))
|
|
|
|
|
2020-11-25 17:33:51 +00:00
|
|
|
(define (arch-get-defines options)
|
|
|
|
(let* ((arch (arch-get options))
|
|
|
|
(info (arch-get-info options))
|
|
|
|
(types (.types info)))
|
|
|
|
(define (sizeof type)
|
|
|
|
(type:size (assoc-ref types type)))
|
|
|
|
(let ((int (sizeof "int"))
|
|
|
|
(long (sizeof "long"))
|
|
|
|
(long-long (sizeof "long long")))
|
|
|
|
(cons (cond ((equal? arch "arm")
|
|
|
|
"__arm__=1")
|
|
|
|
((equal? arch "x86")
|
|
|
|
"__i386__=1")
|
|
|
|
((equal? arch "x86_64")
|
|
|
|
"__x86_64__=1"))
|
|
|
|
`(,(string-append "__SIZEOF_INT__=" (number->string int))
|
|
|
|
,(string-append "__SIZEOF_LONG__=" (number->string long))
|
|
|
|
,@(if (< long-long 8) '() ;C99: long long must be >= 8
|
|
|
|
'("__SIZEOF_LONG_LONG__=8")))))))
|
2019-05-22 15:55:20 +00:00
|
|
|
|
|
|
|
(define (arch-get-machine options)
|
|
|
|
(let* ((machine (option-ref options 'machine #f))
|
|
|
|
(arch (option-ref options 'arch #f)))
|
|
|
|
(or machine
|
|
|
|
(if (member arch '("x86_64")) "64"
|
|
|
|
"32"))))
|
|
|
|
|
|
|
|
(define (arch-get-m1-macros options)
|
|
|
|
(let ((arch (arch-get options)))
|
|
|
|
(cond ((equal? arch "arm") "arm.M1")
|
|
|
|
((equal? arch "x86") "x86.M1")
|
|
|
|
((equal? arch "x86_64") "x86_64.M1"))))
|
|
|
|
|
|
|
|
(define (arch-get-architecture options)
|
2019-12-07 13:39:19 +00:00
|
|
|
(let* ((arch (arch-get options))
|
|
|
|
(numbered-arch? (option-ref options 'numbered-arch? #f))
|
|
|
|
(flag (if numbered-arch? "--Architecture" "--architecture")))
|
|
|
|
(list flag
|
|
|
|
(cond ((equal? arch "arm") (if numbered-arch? "40" "armv7l"))
|
|
|
|
((equal? arch "x86") (if numbered-arch? "1" "x86"))
|
|
|
|
((equal? arch "x86_64") (if numbered-arch? "2" "amd64"))))))
|
2019-05-22 15:55:20 +00:00
|
|
|
|
2018-05-25 06:05:02 +00:00
|
|
|
(define (multi-opt option-name) (lambda (o) (and (eq? (car o) option-name) (cdr o))))
|
2019-07-27 07:51:21 +00:00
|
|
|
(define (count-opt options option-name)
|
|
|
|
(let ((lst (filter-map (multi-opt option-name) options)))
|
|
|
|
(and (pair? lst) (length lst))))
|
2018-05-25 06:05:02 +00:00
|
|
|
|
|
|
|
(define (.c? o) (or (string-suffix? ".c" o)
|
|
|
|
(string-suffix? ".M2" o)))
|
2018-09-30 10:15:11 +00:00
|
|
|
(define (.E? o) (or (string-suffix? ".E" o)
|
|
|
|
(string-suffix? ".mes-E" o)
|
2019-05-22 15:55:20 +00:00
|
|
|
(string-suffix? ".arm-mes-E" o)
|
2018-09-30 10:15:11 +00:00
|
|
|
(string-suffix? ".x86-mes-E" o)
|
|
|
|
(string-suffix? ".x86_64-mes-E" o)))
|
2019-06-13 12:19:59 +00:00
|
|
|
(define (.s? o) (or (string-suffix? ".s" o)
|
|
|
|
(string-suffix? ".S" o)
|
2018-05-25 06:05:02 +00:00
|
|
|
(string-suffix? ".mes-S" o)
|
2019-05-22 15:55:20 +00:00
|
|
|
(string-suffix? ".arm-mes-S" o)
|
2018-09-30 10:15:11 +00:00
|
|
|
(string-suffix? ".x86-mes-S" o)
|
|
|
|
(string-suffix? ".x86_64-mes-S" o)
|
2018-05-25 06:05:02 +00:00
|
|
|
(string-suffix? ".M1" o)))
|
|
|
|
(define (.o? o) (or (string-suffix? ".o" o)
|
|
|
|
(string-suffix? ".mes-o" o)
|
2019-05-22 15:55:20 +00:00
|
|
|
(string-suffix? ".arm-mes-o" o)
|
2018-09-30 10:15:11 +00:00
|
|
|
(string-suffix? ".x86-mes-o" o)
|
|
|
|
(string-suffix? ".x86_64-mes-o" o)
|
2018-05-25 06:05:02 +00:00
|
|
|
(string-suffix? ".hex2" o)))
|