| 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) 1984, Taiichi Yuasa and Masami Hagiya |
| 6 |
;;;; Copyright (c) 1990, Giuseppe Attardi |
| 7 |
;;;; Copyright (c) 2001, Juan Jose Garcia Ripoll |
| 8 |
;;;; Copyright (c) 2023, Daniel Kochmański |
| 9 |
;;;; |
| 10 |
;;;; See the file 'LICENSE' for the copyright details. |
| 11 |
;;;; |
| 12 |
|
| 13 |
;;;; CMPBACKEND-CXX -- backend for compiling to C99/C++ and then invoking the |
| 14 |
;;;; external compiler. |
| 15 |
|
| 16 |
(in-package "COMPILER") |
| 17 |
|
| 18 |
|
| 19 |
;;; External tool wrappers |
| 20 |
(defun safe-mkstemp (template) |
| 21 |
;; We do several things here. One is to check for success in MKSTEMP, |
| 22 |
;; the other one is to ensure that the output of this function _always_ |
| 23 |
;; carries a file type -- this solves a problem with filesystems where |
| 24 |
;; mkstemp may introduce one or more dots in the name causing several |
| 25 |
;; functions below to ignore parts of the name. Note that this forces |
| 26 |
;; us to have two files per temp: one with and one without extension. |
| 27 |
(let ((base (ext:mkstemp template))) |
| 28 |
(unless base |
| 29 |
(error "Unable to create temporary file~%~ |
| 30 |
~AXXXXXX |
| 31 |
Make sure you have enough free space in disk, check permissions or set~%~ |
| 32 |
the environment variable TMPDIR to a different value." template)) |
| 33 |
(let ((output (make-pathname :name |
| 34 |
(concatenate 'string (pathname-name base) |
| 35 |
(or (pathname-type base) "")) |
| 36 |
:type "tmp" |
| 37 |
:defaults base))) |
| 38 |
(if (and (not (probe-file output)) |
| 39 |
(si:copy-file base output)) |
| 40 |
(setf base (list (truename output) (truename base))) |
| 41 |
(progn (delete-file base) |
| 42 |
(setf base nil)))) |
| 43 |
base)) |
| 44 |
|
| 45 |
#+msvc |
| 46 |
(defun delete-msvc-generated-files (output-pathname) |
| 47 |
(loop for i in '("implib" "exp" "ilk" ) |
| 48 |
for full = (make-pathname :type i :defaults output-pathname) |
| 49 |
for truename = (probe-file full) |
| 50 |
when truename |
| 51 |
do (cmp-delete-file truename))) |
| 52 |
|
| 53 |
#+msvc |
| 54 |
(defun embed-manifest-file (o-file &optional (type :dll)) |
| 55 |
(let* ((real-file (probe-file o-file))) |
| 56 |
(when real-file |
| 57 |
(let* ((manifest-namestring (concatenate 'string (namestring o-file) |
| 58 |
".manifest")) |
| 59 |
(resource-code (ecase type |
| 60 |
((:dll :shared-library :fasl :fas) 2) |
| 61 |
((:program) 1))) |
| 62 |
(resource-option (format nil "-outputresource:~A;~D" |
| 63 |
(namestring real-file) |
| 64 |
resource-code)) |
| 65 |
(manifest (probe-file manifest-namestring))) |
| 66 |
(when manifest |
| 67 |
(safe-run-program "mt" |
| 68 |
(list "-nologo" |
| 69 |
"-manifest" |
| 70 |
manifest-namestring |
| 71 |
resource-option)) |
| 72 |
(delete-file manifest)))))) |
| 73 |
|
| 74 |
(defun cmp-delete-file (file) |
| 75 |
(cond ((null *delete-files*)) |
| 76 |
((ext:getenv "ECL_PRESERVE_FILES")) |
| 77 |
((null (probe-file file))) |
| 78 |
(*debug-compiler* |
| 79 |
(cmpprogress "~%Postponing deletion of ~A" file) |
| 80 |
(push file *files-to-be-deleted*)) |
| 81 |
(t |
| 82 |
(delete-file file)))) |
| 83 |
|
| 84 |
(push #'(lambda () (mapc #'delete-file *files-to-be-deleted*)) |
| 85 |
si::*exit-hooks*) |
| 86 |
|
| 87 |
#-mingw32 |
| 88 |
(defmacro fix-for-mingw (directory-namestring) |
| 89 |
directory-namestring) |
| 90 |
|
| 91 |
#+mingw32 |
| 92 |
(defun fix-for-mingw (directory-namestring) |
| 93 |
(let ((x (string-right-trim '(#\\ #\/) directory-namestring))) |
| 94 |
(if (zerop (length x)) "/" x))) |
| 95 |
|
| 96 |
(defun get-deprecated-user-ld-flags () |
| 97 |
(let ((flags (split-program-options *user-ld-flags*))) |
| 98 |
(when flags |
| 99 |
(cmpwarn "The variable ~s is deprecated, please use ~s and ~s instead." |
| 100 |
'*user-ld-flags* '*user-linker-flags* '*user-linker-libs*)) |
| 101 |
flags)) |
| 102 |
|
| 103 |
#+msvc |
| 104 |
(defun linker-cc (o-pathname object-files &key |
| 105 |
(type :program) |
| 106 |
(ld-flags (split-program-options (if #-dlopen nil #+dlopen (eq type :program) |
| 107 |
*ld-program-flags* |
| 108 |
*ld-flags*))) |
| 109 |
(ld-libs (split-program-options *ld-libs*))) |
| 110 |
(safe-run-program |
| 111 |
*ld* |
| 112 |
`(,(concatenate 'string "-Fe" (brief-namestring o-pathname)) |
| 113 |
,@(split-program-options *ld-rpath*) |
| 114 |
,@(split-program-options *user-linker-flags*) |
| 115 |
,@object-files |
| 116 |
,@ld-flags |
| 117 |
,@(split-program-options *user-linker-libs*) |
| 118 |
,@(get-deprecated-user-ld-flags) |
| 119 |
,@ld-libs |
| 120 |
,(if (eq type :program) |
| 121 |
(concatenate 'string "/IMPLIB:prog" (file-namestring o-pathname) ".lib") |
| 122 |
"") |
| 123 |
,(concatenate 'string "/LIBPATH:" |
| 124 |
(ecl-library-directory)))) |
| 125 |
(embed-manifest-file o-pathname type) |
| 126 |
(delete-msvc-generated-files o-pathname)) |
| 127 |
|
| 128 |
#-msvc |
| 129 |
(defun linker-cc (o-pathname object-files &key |
| 130 |
(type :program) |
| 131 |
(ld-flags (split-program-options (if #-dlopen nil #+dlopen (eq type :program) |
| 132 |
*ld-program-flags* |
| 133 |
*ld-flags*))) |
| 134 |
(ld-libs (split-program-options *ld-libs*))) |
| 135 |
(declare (ignore type)) |
| 136 |
(safe-run-program |
| 137 |
*ld* |
| 138 |
`("-o" ,(brief-namestring o-pathname) |
| 139 |
,(concatenate 'string "-L" (fix-for-mingw (ecl-library-directory))) |
| 140 |
,@(split-program-options *user-linker-flags*) |
| 141 |
,@ld-flags |
| 142 |
,@object-files |
| 143 |
,@(and *ld-rpath* (list *ld-rpath*)) |
| 144 |
,@(split-program-options *user-linker-libs*) |
| 145 |
,@(get-deprecated-user-ld-flags) |
| 146 |
,@ld-libs))) |
| 147 |
|
| 148 |
(defun linker-ar (output-name o-name ld-libs) |
| 149 |
#-msvc |
| 150 |
(static-lib-ar (namestring output-name) |
| 151 |
(list* (brief-namestring o-name) ld-libs)) |
| 152 |
#+msvc |
| 153 |
(unwind-protect |
| 154 |
(progn |
| 155 |
(with-open-file (f "static_lib.tmp" :direction :output |
| 156 |
:if-does-not-exist :create :if-exists :supersede) |
| 157 |
(format f "/OUT:~A ~A ~{~&\"~A\"~}" |
| 158 |
output-name o-name ld-libs)) |
| 159 |
(safe-run-program "link" '("-lib" "-nologo" "@static_lib.tmp"))) |
| 160 |
(when (probe-file "static_lib.tmp") |
| 161 |
(cmp-delete-file "static_lib.tmp")))) |
| 162 |
|
| 163 |
(defun static-lib-ar (lib object-files) |
| 164 |
(let ((lib (brief-namestring lib))) |
| 165 |
(when (probe-file lib) |
| 166 |
(delete-file lib)) |
| 167 |
(safe-run-program *ar* (list* "cr" lib (mapcar #'brief-namestring object-files))) |
| 168 |
(safe-run-program *ranlib* (list lib)))) |
| 169 |
|
| 170 |
(defun compiler-cc (c-pathname o-pathname) |
| 171 |
(safe-run-program |
| 172 |
*cc* |
| 173 |
`("-I." |
| 174 |
,@(precompiled-header-flags) |
| 175 |
,(concatenate 'string "-I" (fix-for-mingw (ecl-include-directory))) |
| 176 |
,@(split-program-options *cc-flags*) |
| 177 |
,@(and (>= (cmp-env-optimization 'speed) 2) |
| 178 |
(split-program-options *cc-optimize*)) |
| 179 |
"-c" |
| 180 |
,(brief-namestring c-pathname) |
| 181 |
#-msvc |
| 182 |
,@(list "-o" (brief-namestring o-pathname)) |
| 183 |
#+msvc |
| 184 |
,(concatenate 'string "-Fo" (brief-namestring o-pathname)) |
| 185 |
,@(split-program-options *user-cc-flags*)))) |
| 186 |
;;; Since the SUN4 assembler loops with big files, you might want to use this: |
| 187 |
;;; (format nil "~A ~@[~*-O1~] -S -I. -I~A -w ~A ; as -o ~A ~A" |
| 188 |
;;; *cc* (>= *speed* 2) |
| 189 |
;;; *include-directory* |
| 190 |
;;; (namestring c-pathname) |
| 191 |
;;; (namestring o-pathname) |
| 192 |
;;; (namestring s-pathname)) |
| 193 |
|
| 194 |
|
| 195 |
(defun need-to-dump-precompiled-header () |
| 196 |
(let* ((config *precompiled-header-cc-config*) |
| 197 |
(need-to-dump (or (null config) |
| 198 |
(not (eq (svref config 0) *cc*)) |
| 199 |
(not (eq (svref config 1) (ecl-include-directory))) |
| 200 |
(not (eq (svref config 2) *cc-flags*)) |
| 201 |
(not (eq (svref config 3) *cc-optimize*)) |
| 202 |
(not (eq (svref config 4) *user-cc-flags*))))) |
| 203 |
(when need-to-dump |
| 204 |
(setf *precompiled-header-cc-config* |
| 205 |
(vector *cc* (ecl-include-directory) *cc-flags* |
| 206 |
*cc-optimize* *user-cc-flags*))) |
| 207 |
need-to-dump)) |
| 208 |
|
| 209 |
(defun precompiled-header-flags () |
| 210 |
(when *use-precompiled-headers* |
| 211 |
(when (need-to-dump-precompiled-header) |
| 212 |
(handler-case |
| 213 |
(dump-precompiled-header) |
| 214 |
(error (err) |
| 215 |
(setf *use-precompiled-headers* nil |
| 216 |
*precompiled-header-flags* nil |
| 217 |
*precompiled-header-cc-config* nil) |
| 218 |
(cmpnote "Disabling precompiled header files due to error:~% ~A" err)))) |
| 219 |
*precompiled-header-flags*)) |
| 220 |
|
| 221 |
#+msvc |
| 222 |
(defun dump-precompiled-header () |
| 223 |
;; The way precompiled headers work on msvc is not compatible with |
| 224 |
;; what we want to use them for. The msvc compiler creates a |
| 225 |
;; precompiled header file out of ordinary source files by |
| 226 |
;; processing them up to a certain point at which all needed headers |
| 227 |
;; are included. This creates both a precompiled header and a object |
| 228 |
;; file. The object file created by this compilation must be |
| 229 |
;; included in all binaries which are linked together from other |
| 230 |
;; source files compiled using the precompiled header. Thus, we |
| 231 |
;; would need to include the first object file created in a session |
| 232 |
;; in all further object files if we wanted to support that. |
| 233 |
(error "Precompiled headers are not supported for msvc.")) |
| 234 |
|
| 235 |
#-msvc |
| 236 |
(defun dump-precompiled-header () |
| 237 |
(let* ((input-file (make-pathname |
| 238 |
:directory (append (pathname-directory (ecl-include-directory)) |
| 239 |
'("ecl")) |
| 240 |
:defaults (ecl-include-directory) |
| 241 |
:name "ecl-cmp" |
| 242 |
:type "h")) |
| 243 |
(output-dir (merge-pathnames |
| 244 |
(format nil "ecl-include~4,'0x/" (random #xffff)) |
| 245 |
(translate-logical-pathname "TMP:"))) |
| 246 |
(output-file (compile-file-pathname |
| 247 |
(make-pathname :name "ecl-cmp" :defaults output-dir) |
| 248 |
:type :precompiled-header))) |
| 249 |
(ensure-directories-exist output-dir) |
| 250 |
(push output-dir *files-to-be-deleted*) |
| 251 |
(safe-run-program |
| 252 |
*cc* |
| 253 |
`("-x" "c-header" |
| 254 |
,(fix-for-mingw (namestring input-file)) |
| 255 |
,(concatenate 'string "-I" (fix-for-mingw (ecl-include-directory))) |
| 256 |
,@(split-program-options *cc-flags*) |
| 257 |
,@(split-program-options *cc-optimize*) |
| 258 |
"-o" |
| 259 |
,(fix-for-mingw (namestring output-file)) |
| 260 |
,@(split-program-options *user-cc-flags*))) |
| 261 |
(push output-file *files-to-be-deleted*) |
| 262 |
(setf *precompiled-header-flags* |
| 263 |
(list (concatenate 'string "-I" (namestring output-dir)) |
| 264 |
"-include" |
| 265 |
(concatenate 'string (namestring output-dir) "ecl-cmp.h"))))) |
| 266 |
|
| 267 |
|
| 268 |
;;; Collecting necessary information |
| 269 |
|
| 270 |
(defun ecl-include-directory () |
| 271 |
"Finds the directory in which the header files were installed." |
| 272 |
(cond ((and *ecl-include-directory* |
| 273 |
(probe-file (merge-pathnames "ecl/config.h" *ecl-include-directory*))) |
| 274 |
*ecl-include-directory*) |
| 275 |
((probe-file "SYS:ecl;config.h") |
| 276 |
(setf *ecl-include-directory* (namestring (translate-logical-pathname "SYS:")))) |
| 277 |
((error "Unable to find include directory")))) |
| 278 |
|
| 279 |
(defun ecl-library-directory () |
| 280 |
"Finds the directory in which the ECL core library was installed." |
| 281 |
(cond ((and *ecl-library-directory* |
| 282 |
(probe-file (merge-pathnames (compile-file-pathname "ecl" :type |
| 283 |
#+dlopen :shared-library |
| 284 |
#-dlopen :static-library) |
| 285 |
*ecl-library-directory*))) |
| 286 |
*ecl-library-directory*) |
| 287 |
((probe-file "SYS:BUILD-STAMP") |
| 288 |
(setf *ecl-library-directory* (namestring (translate-logical-pathname "SYS:")))) |
| 289 |
((error "Unable to find library directory")))) |
| 290 |
|
| 291 |
(defun guess-kind (pathname) |
| 292 |
"Given a file name, guess whether it is an object file, a library, a program |
| 293 |
or a loadable module." |
| 294 |
(let ((record (assoc (pathname-type pathname) |
| 295 |
'((#.+object-file-extension+ :object) |
| 296 |
("o" :object) |
| 297 |
("obj" :object) |
| 298 |
("c" :c) |
| 299 |
(#.+static-library-extension+ :static-library) |
| 300 |
("lib" :static-library) |
| 301 |
("a" :static-library) |
| 302 |
(#.+shared-library-extension+ :shared-library) |
| 303 |
("dylib" :shared-library) |
| 304 |
("dll" :shared-library) |
| 305 |
("so" :shared-library) |
| 306 |
("fas" :fasl)) |
| 307 |
:test #'string-equal))) |
| 308 |
(if record |
| 309 |
(second record) |
| 310 |
(progn |
| 311 |
(warn "File ~s is of no known file type. Assuming it is an object file." |
| 312 |
pathname) |
| 313 |
:object)))) |
| 314 |
|
| 315 |
(defun guess-ld-libs (pathname &key (kind (guess-kind pathname))) |
| 316 |
"Given a file name, return the compiler command line argument to link this file in." |
| 317 |
(case kind |
| 318 |
((:object :c) |
| 319 |
(brief-namestring pathname)) |
| 320 |
((:fasl :fas) |
| 321 |
nil) |
| 322 |
((:static-library :lib) |
| 323 |
(brief-namestring pathname)) |
| 324 |
((:shared-library :dll) |
| 325 |
(brief-namestring pathname)) |
| 326 |
((:program) |
| 327 |
nil) |
| 328 |
(otherwise |
| 329 |
(error "C::BUILDER cannot accept files of kind ~s" kind)))) |
| 330 |
|
| 331 |
(defun system-ld-flag (library) |
| 332 |
"Given a symbol, try to find a library that matches it, either by looking in the |
| 333 |
filesystem or in the database of ASDF modules." |
| 334 |
(let ((asdf #+asdf (find-package "ASDF")) |
| 335 |
system) |
| 336 |
(labels ((asdfsym (x) (find-symbol (string x) asdf)) |
| 337 |
(asdfcall (fun &rest rest) (apply (asdfsym fun) rest)) |
| 338 |
(system-output (system type) |
| 339 |
(let ((build (make-instance (asdfsym :build-op) :type type))) |
| 340 |
(first (asdfcall :output-files build system)))) |
| 341 |
(existing-system-output (system type) |
| 342 |
(let ((o (system-output system type))) |
| 343 |
(and o (setf o (probe-file o)) (namestring o)))) |
| 344 |
(find-archive (system) |
| 345 |
(or (existing-system-output system :library) |
| 346 |
(existing-system-output system :shared-library))) |
| 347 |
(fallback () |
| 348 |
(translate-logical-pathname |
| 349 |
(merge-pathnames |
| 350 |
"SYS:" |
| 351 |
(compile-file-pathname (string-downcase library) |
| 352 |
:type :library))))) |
| 353 |
(or |
| 354 |
#-ecl-min |
| 355 |
(and asdf |
| 356 |
(setf system (asdfcall :find-system library nil)) |
| 357 |
(find-archive system)) |
| 358 |
(fallback))))) |
| 359 |
|
| 360 |
|
| 361 |
;;; Target-specific invocations. |
| 362 |
|
| 363 |
#+dlopen |
| 364 |
(defun shared-cc (o-pathname object-files) |
| 365 |
(let ((ld-flags (split-program-options *ld-shared-flags*)) |
| 366 |
(ld-libs (split-program-options *ld-libs*))) |
| 367 |
#+msvc |
| 368 |
(setf ld-flags |
| 369 |
(let ((implib (brief-namestring |
| 370 |
(compile-file-pathname o-pathname :type :lib)))) |
| 371 |
;; MSVC linker options are added at the end, after the |
| 372 |
;; /link flag, because they are not processed by the |
| 373 |
;; compiler, but by the linker |
| 374 |
(append ld-flags |
| 375 |
(list (concatenate 'string "/LIBPATH:" |
| 376 |
(ecl-library-directory)) |
| 377 |
(concatenate 'string "/IMPLIB:" implib))))) |
| 378 |
#+mingw32 |
| 379 |
(setf ld-flags (list* "-shared" ld-flags)) |
| 380 |
(linker-cc o-pathname object-files :type :dll |
| 381 |
:ld-flags ld-flags :ld-libs ld-libs))) |
| 382 |
|
| 383 |
#+dlopen |
| 384 |
(defun bundle-cc (o-pathname init-name object-files) |
| 385 |
(declare (ignore init-name)) |
| 386 |
(let ((ld-flags (split-program-options *ld-bundle-flags*)) |
| 387 |
(ld-libs (split-program-options *ld-libs*))) |
| 388 |
#+msvc |
| 389 |
(setf ld-flags |
| 390 |
(let ((implib (brief-namestring |
| 391 |
(compile-file-pathname o-pathname :type :import-library)))) |
| 392 |
;; MSVC linker options are added at the end, after the |
| 393 |
;; /link flag, because they are not processed by the |
| 394 |
;; compiler, but by the linker |
| 395 |
(append ld-flags |
| 396 |
(list |
| 397 |
;; Not needed because we use ECL_DLLEXPORT |
| 398 |
;; (concatenate 'string "/EXPORT:" init-name) |
| 399 |
(concatenate 'string "/LIBPATH:" |
| 400 |
(ecl-library-directory)) |
| 401 |
(concatenate 'string "/IMPLIB:" implib))))) |
| 402 |
#+mingw32 |
| 403 |
(setf ld-flags (list* "-shared" "-Wl,--export-all-symbols" ld-flags)) |
| 404 |
(linker-cc o-pathname object-files :type :fasl |
| 405 |
:ld-flags ld-flags :ld-libs ld-libs))) |
| 406 |
|
| 407 |
(defconstant +lisp-program-header+ " |
| 408 |
#include <ecl/ecl.h> |
| 409 |
|
| 410 |
#ifdef __cplusplus |
| 411 |
#define ECL_CPP_TAG \"C\" |
| 412 |
#else |
| 413 |
#define ECL_CPP_TAG |
| 414 |
#endif |
| 415 |
|
| 416 |
~:{ extern ECL_CPP_TAG void ~A(cl_object);~%~} |
| 417 |
|
| 418 |
") |
| 419 |
|
| 420 |
;; |
| 421 |
;; This format string contains the structure of the code that initializes |
| 422 |
;; a program, a library, a module, etc. Basically, it processes a codeblock |
| 423 |
;; just like in a normal compiled file, but then adds all the codeblocks of |
| 424 |
;; its corresponding modules. |
| 425 |
;; |
| 426 |
(defconstant +lisp-program-init+ " |
| 427 |
#ifdef __cplusplus |
| 428 |
extern \"C\" |
| 429 |
#endif |
| 430 |
|
| 431 |
ECL_DLLEXPORT |
| 432 |
void ~A(cl_object cblock) |
| 433 |
{ |
| 434 |
/* |
| 435 |
* This function is first invoked with a pointer to a Cblock |
| 436 |
* structure, so that the function initializes it, and then |
| 437 |
* it is invoked with OBJNULL, to force initialization. |
| 438 |
*/ |
| 439 |
static cl_object Cblock = OBJNULL; |
| 440 |
if (cblock != OBJNULL) { |
| 441 |
Cblock = cblock; |
| 442 |
#ifndef ECL_DYNAMIC_VV |
| 443 |
cblock->cblock.data = NULL; |
| 444 |
#endif |
| 445 |
cblock->cblock.data_size = 0; |
| 446 |
return; |
| 447 |
} |
| 448 |
Cblock->cblock.data_text = (const cl_object *)\"~A\"; |
| 449 |
~A |
| 450 |
{ |
| 451 |
/* |
| 452 |
* At this point Cblock contains the cblock of the parent. |
| 453 |
* Notice how the modules are linked to the parent forming a |
| 454 |
* circular chain. This disables the garbage collection of |
| 455 |
* the library until _ALL_ functions in all modules are unlinked. |
| 456 |
*/ |
| 457 |
cl_object current = OBJNULL, next = Cblock; |
| 458 |
~:{ |
| 459 |
current = ecl_make_codeblock(); |
| 460 |
current->cblock.next = next; |
| 461 |
next = current; |
| 462 |
ecl_init_module(current, ~A); |
| 463 |
~} |
| 464 |
Cblock->cblock.next = current; |
| 465 |
} |
| 466 |
~A |
| 467 |
} |
| 468 |
") |
| 469 |
|
| 470 |
(defconstant +lisp-init-wrapper+ " |
| 471 |
#ifdef __cplusplus |
| 472 |
extern \"C\" |
| 473 |
#endif |
| 474 |
|
| 475 |
ECL_DLLEXPORT |
| 476 |
void ~A(cl_object cblock) |
| 477 |
{ |
| 478 |
/* This is a wrapper around the randomized init function name. */ |
| 479 |
~A(cblock); |
| 480 |
} |
| 481 |
") |
| 482 |
|
| 483 |
(defconstant +lisp-program-main+ " |
| 484 |
extern int |
| 485 |
main(int argc, char **argv) |
| 486 |
{ |
| 487 |
cl_boot(argc, argv); |
| 488 |
ECL_CATCH_ALL_BEGIN(ecl_process_env()) { |
| 489 |
~A |
| 490 |
ecl_init_module(OBJNULL, ~A); |
| 491 |
~A |
| 492 |
} ECL_CATCH_ALL_END; |
| 493 |
si_exit(0); |
| 494 |
} |
| 495 |
") |
| 496 |
|
| 497 |
(defconstant +lisp-library-main+ " |
| 498 |
extern int |
| 499 |
~A(int argc, char **argv) |
| 500 |
{ |
| 501 |
cl_boot(argc, argv); |
| 502 |
ECL_CATCH_ALL_BEGIN(ecl_process_env()) { |
| 503 |
~A |
| 504 |
ecl_init_module(OBJNULL, ~A); |
| 505 |
~A |
| 506 |
} ECL_CATCH_ALL_END; |
| 507 |
return 0; |
| 508 |
} |
| 509 |
") |
| 510 |
|
| 511 |
#+:win32 |
| 512 |
(defconstant +lisp-program-winmain+ " |
| 513 |
#include <windows.h> |
| 514 |
int |
| 515 |
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) |
| 516 |
{ |
| 517 |
char **argv; |
| 518 |
int argc; |
| 519 |
ecl_get_commandline_args(&argc, &argv); |
| 520 |
cl_boot(argc, argv); |
| 521 |
ECL_CATCH_ALL_BEGIN(ecl_process_env()) { |
| 522 |
~A |
| 523 |
ecl_init_module(OBJNULL, ~A); |
| 524 |
~A |
| 525 |
} ECL_CATCH_ALL_END; |
| 526 |
si_exit(0); |
| 527 |
for (int i = 0; i < argc; i++) { |
| 528 |
LocalFree(argv[i]); |
| 529 |
} |
| 530 |
LocalFree(argv); |
| 531 |
} |
| 532 |
") |
| 533 |
|
| 534 |
|
| 535 |
;;; Code assembly |
| 536 |
|
| 537 |
(defun compiler-pass/assemble-cxx (input-file output-file |
| 538 |
&key |
| 539 |
(c-file nil) |
| 540 |
(h-file nil) |
| 541 |
(data-file nil) |
| 542 |
(system-p nil) |
| 543 |
&allow-other-keys) |
| 544 |
(let* ((cpath (compile-file-pathname output-file :output-file c-file :type :c)) |
| 545 |
(hpath (compile-file-pathname output-file :output-file h-file :type :h)) |
| 546 |
(dpath (compile-file-pathname output-file :output-file data-file :type :data)) |
| 547 |
(opath (compile-file-pathname output-file :type :object)) |
| 548 |
(to-delete (nconc (unless c-file (list cpath)) |
| 549 |
(unless h-file (list hpath)) |
| 550 |
(unless data-file (list dpath)) |
| 551 |
(unless system-p (list opath)))) |
| 552 |
(init-name (compute-init-name output-file :kind (if system-p :object :fasl)))) |
| 553 |
(compiler-pass/generate-cxx cpath hpath dpath init-name input-file) |
| 554 |
(if system-p |
| 555 |
(compiler-cc cpath opath) |
| 556 |
(progn |
| 557 |
(compiler-cc cpath opath) |
| 558 |
(bundle-cc (brief-namestring output-file) |
| 559 |
init-name |
| 560 |
(list (brief-namestring opath))))) |
| 561 |
(mapc 'cmp-delete-file to-delete))) |
| 562 |
|
| 563 |
|
| 564 |
;;; The builder. |
| 565 |
|
| 566 |
(defun builder (target output-name |
| 567 |
&key |
| 568 |
lisp-files ld-flags ld-libs |
| 569 |
(init-name nil) |
| 570 |
(main-name nil) |
| 571 |
(prologue-code "") |
| 572 |
(epilogue-code (when (eq target :program) '(SI::TOP-LEVEL T))) |
| 573 |
#+:win32 (system :console) |
| 574 |
&aux |
| 575 |
(*suppress-compiler-messages* (or *suppress-compiler-messages* |
| 576 |
(not *compile-verbose*))) |
| 577 |
(target (normalize-build-target-name target)) |
| 578 |
(output-name (if (or (symbolp output-name) (stringp output-name)) |
| 579 |
(compile-file-pathname output-name :type target) |
| 580 |
output-name)) |
| 581 |
;; wrap-name is the init function name defined by a programmer |
| 582 |
(wrap-name init-name)) |
| 583 |
;; init-name should always be unique |
| 584 |
(setf init-name (compute-init-name output-name :kind target)) |
| 585 |
(cond ((null wrap-name) nil) |
| 586 |
((equal init-name wrap-name) ; fixup for ASDF |
| 587 |
(cmpwarn "Parameter `init-name' is the same as the result of an internal function `compute-init-name'. Ignoring.") |
| 588 |
(setf wrap-name nil)) |
| 589 |
((null (member target '(:static-library :shared-library))) |
| 590 |
(cmpwarn "Supplying `init-name' is valid only for libraries. Ignoring."))) |
| 591 |
(unless main-name |
| 592 |
(setf main-name (compute-init-name output-name :kind target :prefix "main_"))) |
| 593 |
|
| 594 |
|
| 595 |
;; |
| 596 |
;; The epilogue-code can be either a string made of C code, or a |
| 597 |
;; lisp form. In the latter case we add some additional C code to |
| 598 |
;; clean up, and the lisp form is stored in a text representation, |
| 599 |
;; to avoid using the compiler. |
| 600 |
;; |
| 601 |
(cond ((null epilogue-code) |
| 602 |
(setf epilogue-code "")) |
| 603 |
((stringp epilogue-code) |
| 604 |
nil) |
| 605 |
(t |
| 606 |
(with-standard-io-syntax |
| 607 |
(setq epilogue-code |
| 608 |
(with-output-to-string (stream) |
| 609 |
(princ "{ const char *lisp_code = " stream) |
| 610 |
(wt-filtered-data (write-to-string epilogue-code) stream) |
| 611 |
(princ "; |
| 612 |
cl_object output; |
| 613 |
si_select_package(ecl_make_constant_base_string(\"CL-USER\", 7)); |
| 614 |
output = si_safe_eval(2, ecl_read_from_cstring(lisp_code), ECL_NIL); |
| 615 |
}" stream) |
| 616 |
))))) |
| 617 |
(cond ((null prologue-code) |
| 618 |
(setf prologue-code "")) |
| 619 |
((stringp prologue-code) |
| 620 |
) |
| 621 |
(t |
| 622 |
(with-standard-io-syntax |
| 623 |
(setq prologue-code |
| 624 |
(with-output-to-string (stream) |
| 625 |
(princ "{ const char *lisp_code = " stream) |
| 626 |
(wt-filtered-data (write-to-string prologue-code) stream) |
| 627 |
(princ "; |
| 628 |
cl_object output; |
| 629 |
si_select_package(ecl_make_constant_base_string(\"CL-USER\", 7)); |
| 630 |
output = si_safe_eval(2, ecl_read_from_cstring(lisp_code), ECL_NIL); |
| 631 |
}" stream) |
| 632 |
))))) |
| 633 |
;; |
| 634 |
;; When a module is built out of several object files, we have to |
| 635 |
;; create an additional object file that initializes those ones. |
| 636 |
;; This routine is responsible for creating this file. |
| 637 |
;; |
| 638 |
;; To avoid name clashes, this object file will have a temporary |
| 639 |
;; file name (tmp-name). |
| 640 |
;; |
| 641 |
(let* ((tmp-names (safe-mkstemp #P"TMP:ECLINIT")) |
| 642 |
(tmp-name (first tmp-names)) |
| 643 |
(c-name (brief-namestring |
| 644 |
(compile-file-pathname tmp-name :type :c))) |
| 645 |
(o-name (brief-namestring |
| 646 |
(compile-file-pathname tmp-name :type :object))) |
| 647 |
submodules |
| 648 |
c-file) |
| 649 |
(dolist (item (reverse lisp-files)) |
| 650 |
(let* ((path (etypecase item |
| 651 |
(symbol (system-ld-flag item)) |
| 652 |
(pathname item) |
| 653 |
(string (parse-namestring item)))) |
| 654 |
(kind (guess-kind path))) |
| 655 |
|
| 656 |
;; Shared and static libraries may be linked in a program or |
| 657 |
;; fasl, but if we try to create a `static-library' from two |
| 658 |
;; static libraries we will end with broken binary because |
| 659 |
;; `ar' works fine only with object files. See #274. |
| 660 |
(unless (member kind `(,@(unless (eql target :static-library) |
| 661 |
'(:shared-library :static-library)) |
| 662 |
:object :c)) |
| 663 |
(error "C::BUILDER does not accept a file ~s of kind ~s for target ~s" item kind target)) |
| 664 |
(let ((init-fn (guess-init-name path kind)) |
| 665 |
(guessed-libs (guess-ld-libs path))) |
| 666 |
;; We should give a warning that we cannot link this module in |
| 667 |
(when guessed-libs |
| 668 |
(push guessed-libs ld-libs)) |
| 669 |
(when init-fn |
| 670 |
(push (list init-fn path) submodules))))) |
| 671 |
(setq c-file (open c-name :direction :output :external-format :default)) |
| 672 |
(format c-file +lisp-program-header+ submodules) |
| 673 |
|
| 674 |
(let ((init-tag (init-name-tag init-name :kind target))) |
| 675 |
(ecase target |
| 676 |
(:program |
| 677 |
(format c-file +lisp-program-init+ init-name init-tag "" submodules "") |
| 678 |
;; we don't need wrapper in the program, we have main for that |
| 679 |
;(format c-file +lisp-init-wrapper+ wrap-name init-name) |
| 680 |
(format c-file |
| 681 |
#+:win32 (ecase system |
| 682 |
(:console +lisp-program-main+) |
| 683 |
(:windows +lisp-program-winmain+)) |
| 684 |
#-:win32 +lisp-program-main+ |
| 685 |
prologue-code init-name epilogue-code) |
| 686 |
(close c-file) |
| 687 |
(compiler-cc c-name o-name) |
| 688 |
(linker-cc output-name (append ld-flags (list (namestring o-name)) |
| 689 |
ld-libs))) |
| 690 |
(:static-library |
| 691 |
(format c-file +lisp-program-init+ |
| 692 |
init-name init-tag prologue-code submodules epilogue-code) |
| 693 |
(when wrap-name |
| 694 |
(format c-file +lisp-init-wrapper+ wrap-name init-name)) |
| 695 |
(format c-file +lisp-library-main+ |
| 696 |
main-name prologue-code init-name epilogue-code) |
| 697 |
(close c-file) |
| 698 |
(compiler-cc c-name o-name) |
| 699 |
(when (probe-file output-name) (delete-file output-name)) |
| 700 |
(linker-ar output-name o-name ld-libs)) |
| 701 |
#+dlopen |
| 702 |
(:shared-library |
| 703 |
(format c-file +lisp-program-init+ |
| 704 |
init-name init-tag prologue-code submodules epilogue-code) |
| 705 |
(when wrap-name |
| 706 |
(format c-file +lisp-init-wrapper+ wrap-name init-name)) |
| 707 |
(format c-file +lisp-library-main+ |
| 708 |
main-name prologue-code init-name epilogue-code) |
| 709 |
(close c-file) |
| 710 |
(compiler-cc c-name o-name) |
| 711 |
(shared-cc output-name (append ld-flags (list o-name) |
| 712 |
ld-libs))) |
| 713 |
#+dlopen |
| 714 |
(:fasl |
| 715 |
(format c-file +lisp-program-init+ init-name init-tag prologue-code |
| 716 |
submodules epilogue-code) |
| 717 |
;; we don't need wrapper in the fasl, we scan for init function name |
| 718 |
;(format c-file +lisp-init-wrapper+ wrap-name init-name) |
| 719 |
(close c-file) |
| 720 |
(compiler-cc c-name o-name) |
| 721 |
(bundle-cc output-name init-name (append ld-flags (list o-name) |
| 722 |
ld-libs)))) |
| 723 |
(mapc 'cmp-delete-file tmp-names) |
| 724 |
(cmp-delete-file c-name) |
| 725 |
(cmp-delete-file o-name) |
| 726 |
output-name))) |
| 727 |
|
| 728 |
(defun build-fasl (&rest args) |
| 729 |
(apply #'builder :fasl args)) |
| 730 |
|
| 731 |
(defun build-program (&rest args) |
| 732 |
(apply #'builder :program args)) |
| 733 |
|
| 734 |
(defun build-static-library (&rest args) |
| 735 |
(apply #'builder :static-library args)) |
| 736 |
|
| 737 |
(defun build-shared-library (&rest args) |
| 738 |
#-dlopen |
| 739 |
(error "Dynamically loadable libraries not supported in this system.") |
| 740 |
#+dlopen |
| 741 |
(apply #'builder :shared-library args)) |