| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll |
| 6 |
;;;; |
| 7 |
;;;; See file 'LICENSE' for the copyright details. |
| 8 |
|
| 9 |
;;;; CMPFEATURES.LSP -- Gather a list of features from the compiler |
| 10 |
|
| 11 |
(in-package "COMPILER") |
| 12 |
|
| 13 |
(defun run-and-collect (command args &optional file) |
| 14 |
(handler-case |
| 15 |
(let ((output-stream (si:run-program-inner command args :default t)) |
| 16 |
lines) |
| 17 |
#+msvc |
| 18 |
(si::stream-external-format-set |
| 19 |
output-stream |
| 20 |
(list (si::windows-codepage-encoding) :crlf)) |
| 21 |
(setf lines (collect-lines output-stream)) |
| 22 |
(cond ((null file) |
| 23 |
lines) |
| 24 |
((probe-file file) |
| 25 |
(with-open-file (s file :direction :input) |
| 26 |
(collect-lines s))) |
| 27 |
(t |
| 28 |
(warn "Unable to find file ~A" file)))) |
| 29 |
(error (c) |
| 30 |
(format t "~&;;; Unable to execute program ~S~&;;; Condition~&;;; ~A" |
| 31 |
command c)))) |
| 32 |
|
| 33 |
(defun split-words (string) |
| 34 |
(loop with output = '() |
| 35 |
with word = '() |
| 36 |
for i from 0 below (length string) |
| 37 |
for c = (elt string i) |
| 38 |
do (if (member c '(#\Space #\Tab #\Newline)) |
| 39 |
(when word |
| 40 |
(push (make-array (length word) :element-type 'base-char |
| 41 |
:initial-contents (nreverse word)) |
| 42 |
output) |
| 43 |
(setf word nil)) |
| 44 |
(push c word)) |
| 45 |
finally (return (push (make-array (length word) :element-type 'base-char |
| 46 |
:initial-contents (nreverse word)) |
| 47 |
output)))) |
| 48 |
|
| 49 |
(defconstant +known-keywords+ |
| 50 |
'("sparc*" "x86*" "*-bit" "32*" "64*" "*32" "*64" |
| 51 |
"elf" "coff" "mach-o" |
| 52 |
"universal" |
| 53 |
"gcc" "icc")) |
| 54 |
|
| 55 |
(defun known-keyword (string &optional (patterns +known-keywords+)) |
| 56 |
(loop with base = (make-pathname :directory nil :name (string-upcase string)) |
| 57 |
for p in patterns |
| 58 |
for pattern-path = (make-pathname :directory nil :name (string-upcase p)) |
| 59 |
thereis (pathname-match-p base pattern-path))) |
| 60 |
|
| 61 |
(defun gather-keywords (strings patterns) |
| 62 |
(declare (ignore patterns)) |
| 63 |
(let ((strings (reduce #'append (mapcar #'split-words strings)))) |
| 64 |
(mapcar (lambda (s) |
| 65 |
(intern (string-upcase s) (find-package :keyword))) |
| 66 |
(remove-if-not #'known-keyword strings)))) |
| 67 |
|
| 68 |
(defun compiler-defines (macros) |
| 69 |
"Test for existence of a set of C preprocessor macros for the compiler flags |
| 70 |
we are currently using with ECL." |
| 71 |
(let* ((f (ext:mkstemp "tmp:foo")) |
| 72 |
(fc (make-pathname :type "c" :defaults f)) |
| 73 |
(fs (make-pathname :type "i" :defaults f))) |
| 74 |
(with-open-file (s fc :direction :output :if-exists :overwrite |
| 75 |
:if-does-not-exist :create) |
| 76 |
(loop for i from 0 |
| 77 |
for (macro . rest) in macros |
| 78 |
do (format s "~%#ifdef ~A~%ECLVALUE ~4,'0D ~A~%#endif" |
| 79 |
macro i macro))) |
| 80 |
(loop with list = (mapcar #'list (mapcar #'first macros)) |
| 81 |
with lines = (run-and-collect c::*cc* |
| 82 |
(append (c::split-program-options c::*cc-flags*) |
| 83 |
#+msvc |
| 84 |
(list "-P" (namestring fc) |
| 85 |
"-Fi" (namestring fs)) |
| 86 |
#-msvc |
| 87 |
(list "-E" (namestring fc) |
| 88 |
"-o" (namestring fs))) |
| 89 |
fs) |
| 90 |
for l in lines |
| 91 |
when (eql (search "ECLVALUE" l) 0) |
| 92 |
do (let* ((number (parse-integer (subseq l 9 13))) |
| 93 |
(defines (subseq l 14))) |
| 94 |
(setf (elt list number) |
| 95 |
(elt macros number))) |
| 96 |
finally (progn |
| 97 |
;;(and (probe-file f) (delete-file f)) |
| 98 |
;;(and (probe-file fc) (delete-file fc)) |
| 99 |
;;(and (probe-file fs) (delete-file fs)) |
| 100 |
(return list))))) |
| 101 |
|
| 102 |
(defconstant +compiler-macros+ |
| 103 |
'(;; Compiler names |
| 104 |
("__INTEL_COMPILER" :intel-compiler) |
| 105 |
("__GNUC__" :gcc-compiler) |
| 106 |
("__SUNPRO_CC" :sun-c++-compiler) |
| 107 |
("__SUNPRO_C" :sun-c-compiler) |
| 108 |
("__xlc__" :ibm-c-compiler) |
| 109 |
("__xlC__" :ibm-c++-compiler) |
| 110 |
("_MSC_VER" :msvc-compiler) |
| 111 |
|
| 112 |
;; Processor features |
| 113 |
("__MMX__" :mmx) |
| 114 |
("__SSE__" :sse) |
| 115 |
("__SSE2__" :sse2) |
| 116 |
("__ELF__" :elf) |
| 117 |
("__i386" :i386) |
| 118 |
("__i386__" :i386) |
| 119 |
("__amd64" :amd64) |
| 120 |
("__x86_64__" :x86-64) |
| 121 |
("__X86_64__" :x86-64) |
| 122 |
("_WIN64" :x86-64) |
| 123 |
("__LP64__" :lp64) |
| 124 |
("_LP64" :lp64) |
| 125 |
("__ILP32__" :ilp32) |
| 126 |
("_ILP32" :ilp32) |
| 127 |
("__powerpc" :powerpc) |
| 128 |
("__PPC" :ppc) |
| 129 |
("__PPC__" :ppc) |
| 130 |
("__PPC64__" :ppc64) |
| 131 |
("_PPC64_" :ppc64) |
| 132 |
)) |
| 133 |
|
| 134 |
(defun run-and-collect-keywords (&rest args) |
| 135 |
(gather-keywords (apply #'run-and-collect args) +known-keywords+)) |
| 136 |
|
| 137 |
(defun gather-system-features (&key (executable |
| 138 |
#+(or windows cygwin mingw32) "sys:ecl_min.exe" |
| 139 |
#-(or windows cygwin mingw32) "sys:ecl_min")) |
| 140 |
(let* ((ecl-binary (namestring (truename executable))) |
| 141 |
(executable-features |
| 142 |
#-windows |
| 143 |
(run-and-collect-keywords "file" (list ecl-binary))) |
| 144 |
(compiler-version (run-and-collect-keywords c::*cc* |
| 145 |
(if (search "xlc" c::*cc*) |
| 146 |
'("-qversion") |
| 147 |
'("--version")))) |
| 148 |
(compiler-features (reduce #'append |
| 149 |
(mapcar #'rest |
| 150 |
(compiler-defines +compiler-macros+))))) |
| 151 |
(delete-duplicates (nconc executable-features |
| 152 |
compiler-version |
| 153 |
compiler-features) |
| 154 |
:test #'string-equal))) |
| 155 |
|
| 156 |
(defun update-compiler-features (&rest args) |
| 157 |
(setf *compiler-features* (apply #'gather-system-features args))) |