Skip to content

Commit

Permalink
Add stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
kartik-s committed Sep 13, 2024
1 parent a6968c2 commit a0e6e7d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
73 changes: 72 additions & 1 deletion lib/generate-bindings.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
(asdf:load-system :sbcl-librarian)
(handler-bind ((deprecation-condition #'continue))
(asdf:load-system :swank))
(in-package #:sbcl-librarian)

(defpackage #:sbcl-librarian/lib
(:use #:cl #:sbcl-librarian)
(:shadowing-import-from #:sbcl-librarian
#:warning #:error #:assert))

(in-package #:sbcl-librarian/lib)

(define-aggregate-library libsbcl-librarian (:function-linkage "LIBSBCL_LIBRARIAN_API")
diagnostics
Expand All @@ -12,6 +18,71 @@
handles
loader)

;;; BEGIN HACKS

;; Turn off SIGFPE handling by masking all floating point modes,
;; becuase libyaml signals SIGFPE... The core saves the state of the
;; modes so it remains off on startup.
(sb-int::set-floating-point-modes :traps '())

;;; We want the "ordinary" Ctrl+C behavior on Windows, i.e. whatever the
;;; application depending on libsbcl_librarian.dll happens to choose.
#+win32
(progn
(sb-ext:unlock-package :sb-win32)
(setf (symbol-function 'sb-win32::initialize-console-control-handler) (constantly nil)))

;;; END HACKS

;;; hacks to the dynamic library search path on macOS -- the goal here
;;; is to ensure we look in the conda lib dir
(defun write-python-header (library stream &optional (omit-init-call nil) (library-path nil))
(let ((name (sbcl-librarian::library-c-name library)))
(format stream "#~%")
(format stream "# THIS FILE IS AUTOGENERATED~%")
(format stream "#~%")
(format stream "# isort: skip_file~%")
(format stream "# mypy: ignore-errors~%")
(format stream "# ruff: noqa~%~%")
(format stream "import os~%")
(format stream "import platform~%")
(format stream "import signal~%")
(format stream "from ctypes import *~%")
(format stream "from ctypes.util import find_library~%")
(format stream "from pathlib import Path~%~%")
(format stream "import sbcl_librarian.wrapper~%")
(format stream "from sbcl_librarian.errors import lisp_err_t~%~%")

(format stream "def find_~a():~%" name)
(format stream " if platform.system() == 'Windows' or platform.system() == 'Linux':~%")
(format stream " return find_library('~a')~%" name)
(format stream " elif platform.system() == 'Darwin':~%")
(format stream " # cf. https://github.com/ContinuumIO/anaconda-issues/issues/1716~%")
(format stream " fallback_path = os.environ.get('DYLD_FALLBACK_LIBRARY_PATH', '')~%")
(format stream " conda_path = os.environ.get('CONDA_PREFIX')~%")
(format stream " try:~%")
(format stream " os.environ['DYLD_FALLBACK_LIBRARY_PATH'] = (conda_path+'/lib:'+fallback_path) if conda_path else fallback_path~%")
(format stream " return find_library('~a')~%" name)
(format stream " finally:~%")
(format stream " os.environ['DYLD_FALLBACK_LIBRARY_PATH'] = fallback_path~%")
(format stream " else:~%")
(format stream " raise Exception(f'Unexpected platform {platform.system()}')~%~%")

(format stream "try:~%")
(format stream " libpath = Path(find_~a()).resolve()~%" name)
(format stream "except Exception as e:~%")
(format stream " raise Exception('Unable to locate ~a') from e~%~%" name)

(format stream "_int_handler = signal.getsignal(signal.SIGINT)~%")
(format stream "_term_handler = signal.getsignal(signal.SIGTERM)~%")
(format stream "if platform.system() != \"Windows\":
_chld_handler = signal.getsignal(signal.SIGCHLD)~%")
(format stream "~a = CDLL(str(libpath), mode=RTLD_GLOBAL)~%~%" name)
(format stream "if platform.system() != \"Windows\":
signal.signal(signal.SIGCHLD, _chld_handler)~%~%")
(format stream "signal.signal(signal.SIGTERM, _term_handler)~%~%")
(format stream "signal.signal(signal.SIGINT, _int_handler)~%~%")))

(build-bindings libsbcl-librarian "." :omit-init-function t)
(build-python-bindings libsbcl-librarian "." :omit-init-call t)
(build-core-and-die libsbcl-librarian ".")
6 changes: 4 additions & 2 deletions src/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
#:library-c-name
#:callable-exports

#:loader
#:handles
#:diagnostics
#:environment
#:errors
#:handles
#:loader

#:error
#:warning
Expand Down

0 comments on commit a0e6e7d

Please sign in to comment.