;;;
;;; gauche.cgen.dyncomp - On-the-fly C code generation & dynlink
;;;
;;;   Copyright (c) 2026  Shiro Kawai  <shiro@acm.org>
;;;
;;;   Redistribution and use in source and binary forms, with or without
;;;   modification, are permitted provided that the following conditions
;;;   are met:
;;;
;;;   1. Redistributions of source code must retain the above copyright
;;;      notice, this list of conditions and the following disclaimer.
;;;
;;;   2. Redistributions in binary form must reproduce the above copyright
;;;      notice, this list of conditions and the following disclaimer in the
;;;      documentation and/or other materials provided with the distribution.
;;;
;;;   3. Neither the name of the authors nor the names of its contributors
;;;      may be used to endorse or promote products derived from this
;;;      software without specific prior written permission.
;;;
;;;   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;;;   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;;;   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;;;   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;;;   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;;;   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
;;;   TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
;;;   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
;;;   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;;   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;;   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;

(define-module gauche.cgen.dyncomp
  (use gauche.cgen)
  (use gauche.package.compile)
  (use gauche.config)
  (use file.util)
  (export cgen-dynamic-load))
(select-module gauche.cgen.dyncomp)

;; This module provides a convenient way to generate C code, compile, link,
;; and dynamically load on-the-fly.

;; Entry point
;;   `unit` is a <cgen-unit>
;;   `tmpdir` is the parent of the temporary work directory created here.
;;
;; Unit's c-file slot is overridden with the temporary C file name
;; generated by cgen-dynamic-load.

(define (cgen-dynamic-load unit :optional (tmpdir (sys-tmpdir)))
  (let* ([workdir (sys-mkdtemp (build-path tmpdir (cgen-safe-name (~ unit'name))))]
         [c-file  ($ build-path workdir
                     $ string-append (cgen-safe-name (~ unit'name)) ".c")]
         [o-file  (path-swap-extension c-file (gauche-config "--object-suffix"))]
         [so-file (path-swap-extension c-file (gauche-config "--so-suffix"))])
    (set! (~ unit'c-file) c-file)

    ;; Generate C file, compile, and link.
    (cgen-emit-c unit)
    (gauche-package-compile (sys-basename c-file)
                            :srcdir   workdir
                            :output   o-file)
    (gauche-package-link so-file (list o-file))

    (let1 dlo (dynamic-load (path-sans-extension so-file)
                            :init-function (cgen-unit-init-name unit))
      ;; Arrange cleanup
      (sys-unlink c-file)
      (sys-unlink o-file)
      (sys-unlink-eventually dlo 1))))
