~ chicken-core (chicken-5) 35abf17111d131b5cc2d4f68e86ac2d25df3e028


commit 35abf17111d131b5cc2d4f68e86ac2d25df3e028
Author:     Evan Hanson <evhan@foldling.org>
AuthorDate: Sat Jul 27 19:46:43 2019 +1200
Commit:     felix <felix@call-with-current-continuation.org>
CommitDate: Thu Aug 1 14:50:48 2019 +0200

    Handle import libraries with static linkage egg property
    
    For fully-static extensions, we need to both emit import libraries (so
    they can be installed to support batch compilation) and compile module
    registration code into the resulting objects (so the modules work when
    imported in eval from statically-compiled programs).
    
    This isn't possible with the current options, so add a flag and make the
    `compile-module-registration` setting tri-valued: yes, no, and unset (to
    keep the default behaviour). The egg toolchain then uses this flag to
    compile static objects as usual while also emitting import libraries
    when the static-linkage egg property is used.
    
    Signed-off-by: felix <felix@call-with-current-continuation.org>

diff --git a/NEWS b/NEWS
index bfb2536d..1139155a 100644
--- a/NEWS
+++ b/NEWS
@@ -29,6 +29,11 @@
   - Inline files no longer refer to unexported foreign stub functions
     (fixes #1440, thanks to "megane").
 
+- Tools
+  - The new "-module-registration" options causes module registration
+    code to always be included in the program, even when it has also
+    been emitted as a separate file (for example with "-J").
+
 
 5.1.0
 
diff --git a/batch-driver.scm b/batch-driver.scm
index a7d791fd..ac871a8b 100644
--- a/batch-driver.scm
+++ b/batch-driver.scm
@@ -181,8 +181,10 @@
   (initialize-compiler)
   (set! explicit-use-flag (memq 'explicit-use options))
   (set! emit-debug-info (memq 'debug-info options))
-  (set! enable-module-registration
-    (not (memq 'no-module-registration options)))
+  (when (memq 'module-registration options)
+    (set! compile-module-registration 'yes))
+  (when (memq 'no-module-registration options)
+    (set! compile-module-registration 'no))
   (when (memq 'static options)
     (set! static-extensions #t)
     (register-feature! 'chicken-compile-static))
@@ -199,10 +201,11 @@
 			     '()
 			     `((uses ,@default-units)))
 			 (if (and static-extensions
-				  enable-module-registration
 				  (not dynamic)
 				  (not unit)
-				  (not explicit-use-flag))
+				  (not explicit-use-flag)
+				  (or (not compile-module-registration)
+				      (eq? compile-module-registration 'yes)))
 			     '((uses eval-modules))
 			     '())))
 		     ,@(if explicit-use-flag
diff --git a/c-platform.scm b/c-platform.scm
index f5206e91..dff89665 100644
--- a/c-platform.scm
+++ b/c-platform.scm
@@ -102,7 +102,8 @@
     no-bound-checks no-procedure-checks-for-usual-bindings no-compiler-syntax
     no-parentheses-synonyms no-symbol-escape r5rs-syntax emit-all-import-libraries
     strict-types clustering lfa2 debug-info
-    regenerate-import-libraries setup-mode no-module-registration))
+    regenerate-import-libraries setup-mode
+    module-registration no-module-registration))
 
 (define valid-compiler-options-with-argument
   '(debug emit-link-file
diff --git a/chicken.mdoc b/chicken.mdoc
index 4e885999..d6324203 100644
--- a/chicken.mdoc
+++ b/chicken.mdoc
@@ -102,12 +102,15 @@ Macros are made available at run-time.
 Write compile-time module information into separate file.
 .It Fl emit-all-import-libraries
 Emit import-libraries for all defined modules.
-.It Fl no-module-registration
-Do not generate module registration code.
 .It Fl no-compiler-syntax
 Disable expansion of compiler-macros.
 .It Fl module Ar NAME
 Wrap compiled code in module of the given name.
+.It Fl module-registration
+Always generate module registration code, even when import libraries are emitted.
+.It Fl no-module-registration
+Do not generate module registration code. Overrides
+.Fl module-registration .
 .El
 .Pp
 Translation options:
diff --git a/core.scm b/core.scm
index 13198e52..bd36448b 100644
--- a/core.scm
+++ b/core.scm
@@ -290,7 +290,7 @@
      all-import-libraries preserve-unchanged-import-libraries
      bootstrap-mode compiler-syntax-enabled
      emit-closure-info emit-profile enable-inline-files explicit-use-flag
-     first-analysis no-bound-checks enable-module-registration
+     first-analysis no-bound-checks compile-module-registration
      optimize-leaf-routines standalone-executable undefine-shadowed-macros
      verbose-mode local-definitions enable-specialization block-compilation
      inline-locally inline-substitutions-enabled strict-variable-types
@@ -402,7 +402,7 @@
 (define import-libraries '())
 (define all-import-libraries #f)
 (define preserve-unchanged-import-libraries #t)
-(define enable-module-registration #t)
+(define compile-module-registration #f) ; 'no | 'yes
 (define standalone-executable #t)
 (define local-definitions #f)
 (define inline-locally #f)
@@ -1020,21 +1020,20 @@
 						       (print-error-message ex (current-error-port))
 						       (exit 1))
 						   (##sys#finalize-module (##sys#current-module)))
-						 (cond ((or (assq name import-libraries) all-import-libraries)
-							=> (lambda (il)
-							     (emit-import-lib name il)
-							     ;; Remove from list to avoid error
-							     (when (pair? il)
-							       (set! import-libraries
-								 (delete il import-libraries equal?)))
-							     (values (reverse xs) '())))
-						       ((not enable-module-registration)
-							(values (reverse xs) '()))
-						       (else
-							(values
-							 (reverse xs)
-							 (##sys#compiled-module-registration
-							  (##sys#current-module))))))
+						 (let ((il (or (assq name import-libraries) all-import-libraries)))
+						   (when il
+						     (emit-import-lib name il)
+						     ;; Remove from list to avoid error
+						     (when (pair? il)
+						       (set! import-libraries
+							 (delete il import-libraries equal?))))
+						   (values
+						    (reverse xs)
+						    (if (or (eq? compile-module-registration 'yes)
+							    (and (not il) ; default behaviour
+								 (not compile-module-registration)))
+							(##sys#compiled-module-registration (##sys#current-module))
+							'()))))
 						(else
 						 (loop
 						  (cdr body)
diff --git a/csc.mdoc b/csc.mdoc
index 4b6f733d..61d17c37 100644
--- a/csc.mdoc
+++ b/csc.mdoc
@@ -107,12 +107,15 @@ Macros are made available at run-time.
 Write compile-time module information into separate file.
 .It Fl J , Fl emit-all-import-libraries
 Emit import-libraries for all defined modules.
-.It Fl N , Fl no-module-registration
-Do not generate module registration code.
 .It Fl no-compiler-syntax
 Disable expansion of compiler-macros.
 .It Fl m Ar NAME , Fl module Ar NAME
 Wrap compiled code in module of the given name.
+.It Fl M , Fl module-registration
+Always generate module registration code, even when import libraries are emitted.
+.It Fl N , Fl no-module-registration
+Do not generate module registration code. Overrides
+.Fl module-registration .
 .El
 .Pp
 Translation options:
diff --git a/csc.scm b/csc.scm
index 3a3b7ab7..d4ce7fa8 100644
--- a/csc.scm
+++ b/csc.scm
@@ -151,7 +151,7 @@
     -analyze-only -keep-shadowed-macros -inline-global -ignore-repository
     -no-symbol-escape -no-parentheses-synonyms -r5rs-syntax
     -no-argc-checks -no-bound-checks -no-procedure-checks -no-compiler-syntax
-    -emit-all-import-libraries -no-elevation -no-module-registration
+    -emit-all-import-libraries -no-elevation -module-registration -no-module-registration
     -no-procedure-checks-for-usual-bindings -regenerate-import-libraries
     -specialize -strict-types -clustering -lfa2 -debug-info
     -no-procedure-checks-for-toplevel-bindings))
@@ -178,6 +178,7 @@
     (|-K| "-keyword-style")
     (|-X| "-extend")
     (|-J| "-emit-all-import-libraries")
+    (|-M| "-module-registration")
     (|-N| "-no-module-registration")
     (-x "-explicit-use")
     (-u "-unsafe")
@@ -185,6 +186,7 @@
     (-b "-block")
     (-types "-consult-types-file")))
 
+;; TODO is this up-to-date?
 (define short-options
   (string->list "PHhsfiENxubvwAOeWkctgSJM") )
 
@@ -365,9 +367,11 @@ Usage: #{csc} [OPTION ...] [FILENAME ...]
     -j -emit-import-library MODULE write compile-time module information into
                                     separate file
     -J -emit-all-import-libraries  emit import-libraries for all defined modules
-    -no-module-registration        do not generate module registration code
     -no-compiler-syntax            disable expansion of compiler-macros
     -m -module NAME                wrap compiled code in a module
+    -M -module-registration        always generate module registration code
+    -N -no-module-registration     never generate module registration code
+                                    (overrides `-M')
 
   Translation options:
 
diff --git a/egg-compile.scm b/egg-compile.scm
index e4e4869e..0fd9c242 100644
--- a/egg-compile.scm
+++ b/egg-compile.scm
@@ -475,12 +475,13 @@
                                    '())
                                (if (memq 'static link) 
                                    ;; if compiling both static + dynamic, override
-                                   ;; types-file: + inline-file: properties to
+                                   ;; modules/types-file/inline-file properties to
                                    ;; avoid generating things twice:
                                    (list (apply compile-static-extension
                                                 (if (memq 'dynamic link)
                                                     (cons (car data)
-                                                          (append '(types-file: #f
+                                                          (append '(modules: #f
+                                                                    types-file: #f
                                                                     inline-file: #f)
                                                                   (cdr data)))
                                                     data)))
@@ -559,7 +560,7 @@
                                    source-dependencies
                                    source (options '())
                                    predefined-types eggfile
-                                   link-objects
+                                   link-objects modules
                                    custom types-file inline-file)
          srcdir platform)
   (let* ((cmd (qs* (or (custom-cmd custom srcdir platform)
@@ -594,7 +595,11 @@
                         platform)))
          (targets (append (list out3 lfile)
                           (maybe types-file tfile)
-                          (maybe inline-file ifile)))
+                          (maybe inline-file ifile)
+                          (map (lambda (m)
+                                 (qs* (prefix srcdir (conc m ".import.scm"))
+                                      platform))
+                               (or modules '()))))
          (src (qs* (or source (conc name ".scm")) platform)))
     (when custom
       (prepare-custom-command cmd platform))
@@ -606,6 +611,7 @@
            " : " cmd
            (if keep-generated-files " -k" "")
            " -regenerate-import-libraries"
+           (if modules " -J" "") " -M"
            " -setup-mode -static -I " srcdir 
            " -emit-link-file " lfile
            (if (eq? mode 'host) " -host" "")
diff --git a/support.scm b/support.scm
index 1d078ca0..53dcbf99 100644
--- a/support.scm
+++ b/support.scm
@@ -1726,9 +1726,11 @@ Usage: chicken FILENAME [OPTION ...]
     -emit-import-library MODULE  write compile-time module information into
                                   separate file
     -emit-all-import-libraries   emit import-libraries for all defined modules
-    -no-module-registration      do not generate module registration code
     -no-compiler-syntax          disable expansion of compiler-macros
     -module NAME                 wrap compiled code in a module
+    -module-registration         always generate module registration code
+    -no-module-registration      never generate module registration code
+                                  (overrides `-module-registration')
 
   Translation options:
 
Trap