~ chicken-core (chicken-5) d4b6dab350b7db7efe0911d3599be277483d3175


commit d4b6dab350b7db7efe0911d3599be277483d3175
Author:     Evan Hanson <evhan@foldling.org>
AuthorDate: Wed Jan 28 19:35:20 2015 +1300
Commit:     Evan Hanson <evhan@foldling.org>
CommitDate: Sun Jul 19 21:12:54 2015 +1200

    Convert the ports unit into a module
    
    Makes ports.scm into a proper module (named "chicken.ports") and removes its
    static, "primitive" import library in favor of one that is dynamically
    generated during the build.
    
    Also removes a now-unnecessary compatibility hack during bootstrapping
    in irregex-core.scm.

diff --git a/README b/README
index ac865653..2d7f1cf8 100644
--- a/README
+++ b/README
@@ -284,6 +284,7 @@
 	|   |-- chicken
 	|   |   `-- 7
 	|   |       |-- chicken.import.so
+	|   |       |-- chicken.ports.import.so
 	|   |       |-- csi.import.so
 	|   |       |-- data-structures.import.so
 	|   |       |-- extras.import.so
@@ -292,7 +293,6 @@
 	|   |       |-- irregex.import.so
 	|   |       |-- lolevel.import.so
 	|   |       |-- modules.db
-	|   |       |-- ports.import.so
 	|   |       |-- posix.import.so
 	|   |       |-- setup-api.import.so
 	|   |       |-- setup-api.so
@@ -305,8 +305,8 @@
 	|   |-- libchicken.a
 	|   |-- libchicken.dll.a                      (Windows)
 	|   |-- libchicken.dylib                      (Macintosh)
-	|   |-- libchicken.so -> libchicken.so.6      (Unix)
-	|   `-- libchicken.so.6			      (Unix)
+	|   |-- libchicken.so -> libchicken.so.7      (Unix)
+	|   `-- libchicken.so.7			      (Unix)
 	`-- share
 	    |-- chicken
 	    |   |-- doc
diff --git a/chicken-install.scm b/chicken-install.scm
index eb9e005a..6d5d266b 100644
--- a/chicken-install.scm
+++ b/chicken-install.scm
@@ -29,11 +29,11 @@
 
 (module main ()
 
-  (import scheme chicken posix data-structures utils irregex ports extras
-          files)
+  (import scheme chicken posix data-structures utils irregex extras files)
   (import setup-download setup-api)
 
   (import foreign)
+  (import chicken.ports)
 
   (include "mini-srfi-1.scm")
 
@@ -42,11 +42,11 @@
     '("setup-api.so" "setup-api.import.so"
       "setup-download.so" "setup-download.import.so"
       "chicken.import.so"
+      "chicken.ports.import.so"
       "lolevel.import.so"
       "srfi-1.import.so"
       "srfi-4.import.so"
       "data-structures.import.so"
-      "ports.import.so"
       "files.import.so"
       "posix.import.so"
       "extras.import.so"
diff --git a/chicken-status.scm b/chicken-status.scm
index 3c714272..85eb1a11 100644
--- a/chicken-status.scm
+++ b/chicken-status.scm
@@ -30,8 +30,8 @@
 (module main ()
   
   (import scheme chicken foreign)
-  (import posix data-structures utils ports irregex
-	  files setup-api extras)
+  (import posix data-structures utils irregex files setup-api extras)
+  (import chicken.ports)
 
   (include "mini-srfi-1.scm")
 
diff --git a/chicken-uninstall.scm b/chicken-uninstall.scm
index 2bc26166..989b9054 100644
--- a/chicken-uninstall.scm
+++ b/chicken-uninstall.scm
@@ -33,7 +33,8 @@
   
   (import scheme chicken foreign)
   (import setup-api)
-  (import posix data-structures utils ports irregex files)
+  (import posix data-structures utils irregex files)
+  (import chicken.ports)
 
   (include "mini-srfi-1.scm")
 
diff --git a/csi.scm b/csi.scm
index b5231b11..a3bd2903 100644
--- a/csi.scm
+++ b/csi.scm
@@ -54,6 +54,8 @@ EOF
 	describe dump tty-input? history-list history-count
 	history-add history-ref history-clear history-show) )
 
+(import chicken.ports)
+
 
 ;;; Parameters:
 
diff --git a/defaults.make b/defaults.make
index 46c07051..670b3bce 100644
--- a/defaults.make
+++ b/defaults.make
@@ -265,6 +265,13 @@ endif
 
 CHICKEN_PROGRAM_OPTIONS += $(if $(PROFILE_OBJECTS),-profile)
 
+# import libraries
+
+PRIMITIVE_IMPORT_LIBRARIES = chicken lolevel srfi-4 data-structures files
+PRIMITIVE_IMPORT_LIBRARIES += posix extras tcp foreign utils csi irregex
+PRIMITIVE_IMPORT_LIBRARIES += setup-api setup-download
+DYNAMIC_IMPORT_LIBRARIES = ports
+
 # targets
 
 CHICKEN_PROGRAM = $(PROGRAM_PREFIX)chicken$(PROGRAM_SUFFIX)
@@ -275,8 +282,8 @@ CHICKEN_INSTALL_PROGRAM = $(PROGRAM_PREFIX)chicken-install$(PROGRAM_SUFFIX)
 CHICKEN_UNINSTALL_PROGRAM = $(PROGRAM_PREFIX)chicken-uninstall$(PROGRAM_SUFFIX)
 CHICKEN_STATUS_PROGRAM = $(PROGRAM_PREFIX)chicken-status$(PROGRAM_SUFFIX)
 CHICKEN_BUG_PROGRAM = $(PROGRAM_PREFIX)chicken-bug$(PROGRAM_SUFFIX)
-IMPORT_LIBRARIES = chicken lolevel srfi-4 data-structures ports files posix extras tcp foreign utils csi irregex
-IMPORT_LIBRARIES += setup-api setup-download
+IMPORT_LIBRARIES = $(PRIMITIVE_IMPORT_LIBRARIES) \
+		   $(foreach lib,$(DYNAMIC_IMPORT_LIBRARIES),chicken.$(lib))
 
 ifdef STATICBUILD
 CHICKEN_STATIC_EXECUTABLE = $(CHICKEN_PROGRAM)$(EXE)
diff --git a/distribution/manifest b/distribution/manifest
index 25c3a8b4..2b172543 100644
--- a/distribution/manifest
+++ b/distribution/manifest
@@ -236,12 +236,12 @@ modules.c
 chicken-syntax.scm
 chicken-syntax.c
 common-declarations.scm
-ports.import.scm
-ports.import.c
 files.import.scm
 files.import.c
 chicken.import.scm
 chicken.import.c
+chicken.ports.import.scm
+chicken.ports.import.c
 foreign.import.scm
 foreign.import.c
 lolevel.import.scm
diff --git a/irregex-core.scm b/irregex-core.scm
index 9d09a486..c4dbea2e 100644
--- a/irregex-core.scm
+++ b/irregex-core.scm
@@ -93,72 +93,67 @@
 (cond-expand
   (chicken-bootstrap
    (begin
-     (define-syntax internal
-       (er-macro-transformer
-	(lambda (x r c)
-	  `(,(with-input-from-string (cadr x) read) ,@(cddr x)))))
      ;; make-irregex defined elsewhere
      (define (irregex? x)
-       (internal "##sys#structure?" x 'regexp))
+       (##sys#structure? x 'regexp))
      (define (irregex-dfa x)
-       (internal "##sys#check-structure" x 'regexp 'irregex-dfa)
-       (internal "##sys#slot" x 1))
+       (##sys#check-structure x 'regexp 'irregex-dfa)
+       (##sys#slot x 1))
      (define (irregex-dfa/search x)
-       (internal "##sys#check-structure" x 'regexp 'irregex-dfa/search)
-       (internal "##sys#slot" x 2))
+       (##sys#check-structure x 'regexp 'irregex-dfa/search)
+       (##sys#slot x 2))
      (define (irregex-nfa x)
-       (internal "##sys#check-structure" x 'regexp 'irregex-nfa)
-       (internal "##sys#slot" x 3))
+       (##sys#check-structure x 'regexp 'irregex-nfa)
+       (##sys#slot x 3))
      (define (irregex-flags x)
-       (internal "##sys#check-structure" x 'regexp 'irregex-flags)
-       (internal "##sys#slot" x 4))
+       (##sys#check-structure x 'regexp 'irregex-flags)
+       (##sys#slot x 4))
      (define (irregex-num-submatches x)
-       (internal "##sys#check-structure" x 'regexp 'irregex-num-submatches)
-       (internal "##sys#slot" x 5))
+       (##sys#check-structure x 'regexp 'irregex-num-submatches)
+       (##sys#slot x 5))
      (define (irregex-lengths x)
-       (internal "##sys#check-structure" x 'regexp 'irregex-lengths)
-       (internal "##sys#slot" x 6))
+       (##sys#check-structure x 'regexp 'irregex-lengths)
+       (##sys#slot x 6))
      (define (irregex-names x)
-       (internal "##sys#check-structure" x 'regexp 'irregex-names)
-       (internal "##sys#slot" x 7))
+       (##sys#check-structure x 'regexp 'irregex-names)
+       (##sys#slot x 7))
      ;; make-irregex-match defined elsewhere
      (define (irregex-new-matches irx)
        (make-irregex-match (irregex-num-submatches irx) (irregex-names irx)))
      (define (irregex-reset-matches! m)
-       (let ((v (internal "##sys#slot" m 1)))
+       (let ((v (##sys#slot m 1)))
 	 (vector-fill! v #f)
 	 m))
      (define (irregex-copy-matches m)
-       (and (internal "##sys#structure?" m 'regexp-match)
-	    (internal
-	     "##sys#make-structure"
+       (and (##sys#structure? m 'regexp-match)
+	    (##sys#make-structure
 	     'regexp-match
-	     (vector-copy (internal "##sys#slot" m 1))
-	     (internal "##sys#slot" m 2)
-	     (internal "##sys#slot" m 3)
-	     (internal "##sys#slot" m 4))))
+	     (vector-copy (##sys#slot m 1))
+	     (##sys#slot m 2)
+	     (##sys#slot m 3)
+	     (##sys#slot m 4))))
      (define (irregex-match-data? obj)
-       (internal "##sys#structure?" obj 'regexp-match))
+       (##sys#structure? obj 'regexp-match))
      (define (irregex-match-num-submatches m)
-       (internal "##sys#check-structure" m 'regexp-match 'irregex-match-num-submatches)
-       (- (fx/ (internal "##sys#size" (internal "##sys#slot" m 1)) 4) 2))
+       (##sys#check-structure m 'regexp-match 'irregex-match-num-submatches)
+       (- (fx/ (##sys#size (##sys#slot m 1)) 4) 2))
      (define (irregex-match-chunker m)
-       (internal "##sys#slot" m 3))
+       (##sys#slot m 3))
      (define (irregex-match-names m)
-       (internal "##sys#check-structure" m 'regexp-match 'irregex-match-names)
-       (internal "##sys#slot" m 2))
+       (##sys#check-structure m 'regexp-match 'irregex-match-names)
+       (##sys#slot m 2))
      (define (irregex-match-chunker-set! m str)
-       (internal "##sys#setslot" m 3 str))
+       (##sys#setslot m 3 str))
      (define-inline (%irregex-match-start-chunk m n)
-       (internal "##sys#slot" (internal "##sys#slot" m 1) (* n 4)))
+       (##sys#slot (##sys#slot m 1) (* n 4)))
      (define-inline (%irregex-match-start-index m n)
-       (internal "##sys#slot" (internal "##sys#slot" m 1) (+ 1 (* n 4))))
+       (##sys#slot (##sys#slot m 1) (+ 1 (* n 4))))
      (define-inline (%irregex-match-end-chunk m n)
-       (internal "##sys#slot" (internal "##sys#slot" m 1) (+ 2 (* n 4))))
+       (##sys#slot (##sys#slot m 1) (+ 2 (* n 4))))
      (define (%irregex-match-end-index m n)
-       (internal "##sys#slot" (internal "##sys#slot" m 1) (+ 3 (* n 4))))
-     (define (%irregex-match-fail m) (internal "##sys#slot" m 4))
-     (define (%irregex-match-fail-set! m x) (internal "##sys#setslot" m 4 x))
+       (##sys#slot (##sys#slot m 1) (+ 3 (* n 4))))
+     (define (%irregex-match-fail m) (##sys#slot m 4))
+     (define (%irregex-match-fail-set! m x) (##sys#setslot m 4 x))
      (define-record-printer (regexp-match m out)
        (let ((n (irregex-match-num-submatches m)))
 	 (display "#<regexp-match (" out)
@@ -167,11 +162,11 @@
 	 (when (or (eq? n 0) (fx> n 1)) (display "es" out))
 	 (display ")>" out)))
      (define-inline (irregex-match-valid-numeric-index? m n)
-       (let ((v (internal "##sys#slot" m 1)))
-	 (and (>= n 0) (< (* n 4) (- (internal "##sys#size" v) 4)))))
+       (let ((v (##sys#slot m 1)))
+	 (and (>= n 0) (< (* n 4) (- (##sys#size v) 4)))))
      (define-inline (irregex-match-matched-numeric-index? m n)
-       (let ((v (internal "##sys#slot" m 1)))
-         (and (internal "##sys#slot" v (+ 1 (* n 4)))
+       (let ((v (##sys#slot m 1)))
+         (and (##sys#slot v (+ 1 (* n 4)))
               #t)))))
   (else
    (begin
diff --git a/modules.scm b/modules.scm
index f3c449a1..d012a041 100644
--- a/modules.scm
+++ b/modules.scm
@@ -913,6 +913,7 @@
   (##sys#register-primitive-module 'r5rs-null '() r4rs-syntax))
 
 (##sys#register-module-alias 'r5rs 'scheme)
+(##sys#register-module-alias 'ports 'chicken.ports)
 
 (register-feature! 'module-environments)
 
diff --git a/ports.import.scm b/ports.import.scm
deleted file mode 100644
index 1050de24..00000000
--- a/ports.import.scm
+++ /dev/null
@@ -1,44 +0,0 @@
-;;;; ports.import.scm - import library for "ports" module
-;
-; Copyright (c) 2008-2015, The CHICKEN Team
-; All rights reserved.
-;
-; Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
-; conditions are met:
-;
-;   Redistributions of source code must retain the above copyright notice, this list of conditions and the following
-;     disclaimer. 
-;   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. 
-;   Neither the name of the author 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 HOLDERS 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.
-
-
-(##sys#register-primitive-module
- 'ports
- '(call-with-input-string
-   call-with-output-string
-   copy-port
-   make-input-port
-   make-output-port
-   port-for-each
-   port-map
-   port-fold
-   make-broadcast-port
-   make-concatenated-port
-   with-error-output-to-port
-   with-input-from-port
-   with-input-from-string
-   with-output-to-port
-   with-output-to-string
-   with-error-output-to-port))
diff --git a/ports.scm b/ports.scm
index eee171c3..8a5cfdcb 100644
--- a/ports.scm
+++ b/ports.scm
@@ -36,6 +36,26 @@
   (unit ports)
   (uses extras))
 
+(module chicken.ports
+  (call-with-input-string
+   call-with-output-string
+   copy-port
+   make-input-port
+   make-output-port
+   port-for-each
+   port-map
+   port-fold
+   make-broadcast-port
+   make-concatenated-port
+   with-error-output-to-port
+   with-input-from-port
+   with-input-from-string
+   with-output-to-port
+   with-output-to-string)
+
+(import scheme chicken)
+(import extras)
+
 (include "common-declarations.scm")
 
 (register-feature! 'ports)
@@ -266,3 +286,5 @@
 	   (port (##sys#make-port #f class "(custom)" 'custom)) )
       (##sys#set-port-data! port data) 
       port) ) )
+
+)
diff --git a/posixunix.scm b/posixunix.scm
index 6f7ec5b4..86e6bcda 100644
--- a/posixunix.scm
+++ b/posixunix.scm
@@ -43,6 +43,8 @@
 
 (include "posix-common.scm")
 
+(import chicken.ports)
+
 
 (declare
   (foreign-declare #<<EOF
diff --git a/posixwin.scm b/posixwin.scm
index 8ca06381..35c1b63a 100644
--- a/posixwin.scm
+++ b/posixwin.scm
@@ -663,6 +663,8 @@ EOF
 
 (include "posix-common.scm")
 
+(import chicken.ports)
+
 
 ;;; Lo-level I/O:
 
diff --git a/rules.make b/rules.make
index cef3bb93..31240b9d 100644
--- a/rules.make
+++ b/rules.make
@@ -489,6 +489,11 @@ define declare-emitted-import-lib-dependency
 $(1).import.scm: $(1).c
 endef
 
+define declare-emitted-chicken-import-lib-dependency
+.SECONDARY: chicken.$(1).import.scm
+chicken.$(1).import.scm: $(1).c
+endef
+
 define declare-emitted-compiler-import-lib-dependency
 .SECONDARY: chicken.compiler.$(1).import.scm
 chicken.compiler.$(1).import.scm: $(1).c
@@ -497,6 +502,9 @@ endef
 $(foreach lib, $(SETUP_API_OBJECTS_1),\
           $(eval $(call declare-emitted-import-lib-dependency,$(lib))))
 
+$(foreach lib, $(DYNAMIC_IMPORT_LIBRARIES),\
+          $(eval $(call declare-emitted-chicken-import-lib-dependency,$(lib))))
+
 $(foreach lib, $(filter-out chicken,$(COMPILER_OBJECTS_1)),\
           $(eval $(call declare-emitted-compiler-import-lib-dependency,$(lib))))
 
@@ -526,12 +534,38 @@ core.c: core.scm mini-srfi-1.scm \
 optimizer.c: optimizer.scm mini-srfi-1.scm \
 		chicken.compiler.support.import.scm
 scrutinizer.c: scrutinizer.scm mini-srfi-1.scm \
+		chicken.compiler.support.import.scm \
+		chicken.ports.import.scm
+lfa2.c: lfa2.scm mini-srfi-1.scm \
 		chicken.compiler.support.import.scm
-lfa2.c: lfa2.scm chicken.compiler.support.import.scm mini-srfi-1.scm
 compiler-syntax.c: compiler-syntax.scm mini-srfi-1.scm \
 		chicken.compiler.support.import.scm \
 		chicken.compiler.core.import.scm
-support.c: support.scm mini-srfi-1.scm
+support.c: support.scm mini-srfi-1.scm \
+		chicken.ports.import.scm
+csi.c: csi.scm \
+		chicken.ports.import.scm
+chicken-status.c: chicken-status.scm \
+		chicken.ports.import.scm \
+		setup-api.import.scm
+chicken-install.c: chicken-install.scm \
+		chicken.ports.import.scm \
+		setup-api.import.scm \
+		setup-download.import.scm
+chicken-uninstall.c: chicken-uninstall.scm \
+		chicken.ports.import.scm \
+		setup-api.import.scm
+setup-api.c: setup-api.scm \
+		chicken.ports.import.scm
+setup-download.c: setup-download.scm \
+		chicken.ports.import.scm \
+		setup-api.import.scm
+posixunix.c: posixunix.scm \
+		chicken.ports.import.scm
+posixwin.c: posixwin.scm \
+		chicken.ports.import.scm
+tcp.c: tcp.scm \
+		chicken.ports.import.scm
 
 define profile-flags
 $(if $(filter $(basename $(1)),$(PROFILE_OBJECTS)),-profile)
@@ -555,9 +589,6 @@ posixwin.c: $(SRCDIR)posixwin.scm $(SRCDIR)posix-common.scm $(SRCDIR)common-decl
 	$(bootstrap-lib) 
 irregex.c: $(SRCDIR)irregex.scm $(SRCDIR)irregex-core.scm $(SRCDIR)irregex-utils.scm $(SRCDIR)common-declarations.scm
 	$(bootstrap-lib)
-#
-# The ones below just depend on their matching .scm file and common-declarations
-#
 chicken-syntax.c: $(SRCDIR)chicken-syntax.scm $(SRCDIR)common-declarations.scm $(SRCDIR)mini-srfi-1.scm
 	$(bootstrap-lib)
 chicken-ffi-syntax.c: $(SRCDIR)chicken-ffi-syntax.scm $(SRCDIR)common-declarations.scm $(SRCDIR)mini-srfi-1.scm
@@ -565,7 +596,7 @@ chicken-ffi-syntax.c: $(SRCDIR)chicken-ffi-syntax.scm $(SRCDIR)common-declaratio
 data-structures.c: $(SRCDIR)data-structures.scm $(SRCDIR)common-declarations.scm
 	$(bootstrap-lib)
 ports.c: $(SRCDIR)ports.scm $(SRCDIR)common-declarations.scm
-	$(bootstrap-lib)
+	$(bootstrap-lib) -emit-import-library chicken.ports
 files.c: $(SRCDIR)files.scm $(SRCDIR)common-declarations.scm
 	$(bootstrap-lib)
 lolevel.c: $(SRCDIR)lolevel.scm $(SRCDIR)common-declarations.scm
@@ -609,7 +640,7 @@ csi.c: $(SRCDIR)csi.scm $(SRCDIR)banner.scm
 	$(CHICKEN) $< $(CHICKEN_PROGRAM_OPTIONS) -output-file $@
 chicken-profile.c: $(SRCDIR)chicken-profile.scm $(SRCDIR)mini-srfi-1.scm
 	$(CHICKEN) $< $(CHICKEN_PROGRAM_OPTIONS) -output-file $@ 
-chicken-install.c: $(SRCDIR)chicken-install.scm setup-download.c setup-api.c $(SRCDIR)mini-srfi-1.scm
+chicken-install.c: $(SRCDIR)chicken-install.scm $(SRCDIR)mini-srfi-1.scm
 	$(CHICKEN) $< $(CHICKEN_PROGRAM_OPTIONS) -output-file $@ 
 chicken-uninstall.c: $(SRCDIR)chicken-uninstall.scm $(SRCDIR)mini-srfi-1.scm
 	$(CHICKEN) $< $(CHICKEN_PROGRAM_OPTIONS) -output-file $@ 
@@ -623,7 +654,7 @@ chicken-bug.c: $(SRCDIR)chicken-bug.scm
 setup-api.c: $(SRCDIR)setup-api.scm $(SRCDIR)mini-srfi-1.scm
 	$(CHICKEN) $< $(CHICKEN_DYNAMIC_OPTIONS) -emit-import-library setup-api \
 	  -output-file $@ 
-setup-download.c: $(SRCDIR)setup-download.scm setup-api.c $(SRCDIR)mini-srfi-1.scm
+setup-download.c: $(SRCDIR)setup-download.scm $(SRCDIR)mini-srfi-1.scm
 	$(CHICKEN) $< $(CHICKEN_DYNAMIC_OPTIONS) -emit-import-library setup-download \
 	  -output-file $@ 
 
@@ -658,9 +689,9 @@ clean:
 	  $(LIBCHICKEN_SO_FILE) \
 	  $(PRIMARY_LIBCHICKEN) \
 	  lib$(PROGRAM_PREFIX)chicken$(PROGRAM_SUFFIX)$(A) \
-	  $(PROGRAM_IMPORT_LIBRARIES) \
 	  $(IMPORT_LIBRARIES:=.import.so) $(LIBCHICKEN_IMPORT_LIBRARY) \
-	  $(SETUP_API_OBJECTS_1:=.so) $(SETUP_API_OBJECTS_1:=.import.so)
+	  $(SETUP_API_OBJECTS_1:=.so) $(SETUP_API_OBJECTS_1:=.import.so) \
+	  $(foreach lib,$(DYNAMIC_IMPORT_LIBRARIES),chicken.$(lib).import.scm)
 ifdef USES_SONAME
 	$(REMOVE_COMMAND) $(REMOVE_COMMAND_OPTIONS) lib$(PROGRAM_PREFIX)chicken$(PROGRAM_SUFFIX).so.$(BINARYVERSION)
 endif
diff --git a/scrutinizer.scm b/scrutinizer.scm
index 231bb77c..f1569ee3 100644
--- a/scrutinizer.scm
+++ b/scrutinizer.scm
@@ -33,8 +33,9 @@
     (scrutinize load-type-database emit-type-file
      validate-type check-and-validate-type install-specializations)
 
-(import chicken scheme data-structures extras ports files
-	chicken.compiler.support)
+(import chicken scheme data-structures extras files
+	chicken.compiler.support
+	chicken.ports)
 
 (include "tweaks")
 (include "mini-srfi-1.scm")
diff --git a/setup-api.scm b/setup-api.scm
index aa85f018..5823f4c6 100644
--- a/setup-api.scm
+++ b/setup-api.scm
@@ -66,8 +66,8 @@
      setup-error-handling)
   
   (import scheme chicken foreign
-	  irregex utils posix ports extras data-structures
-	  files)
+	  irregex utils posix extras data-structures files
+	  chicken.ports)
 
 (include "mini-srfi-1.scm")
 
diff --git a/support.scm b/support.scm
index 4fb07862..2dc27c53 100644
--- a/support.scm
+++ b/support.scm
@@ -75,7 +75,8 @@
      ;; in a lot of other places.
      number-type unsafe)
 
-(import chicken scheme foreign data-structures files extras ports)
+(import chicken scheme foreign data-structures files extras
+	chicken.ports)
 
 (include "tweaks")
 (include "mini-srfi-1.scm")
diff --git a/tcp.scm b/tcp.scm
index 2ee36b57..e12a7521 100644
--- a/tcp.scm
+++ b/tcp.scm
@@ -142,6 +142,8 @@ EOF
 
 (include "common-declarations.scm")
 
+(import chicken.ports)
+
 (register-feature! 'tcp)
 
 (define-foreign-type sockaddr* (pointer "struct sockaddr"))
diff --git a/tests/pp-test.scm b/tests/pp-test.scm
index 0af80e43..1c65a57c 100644
--- a/tests/pp-test.scm
+++ b/tests/pp-test.scm
@@ -1,5 +1,6 @@
 ;;;; pp-test.scm
 
+(use (only ports with-output-to-string))
 
 (define (pp->string thing)
   (with-output-to-string (cut pp thing)))
diff --git a/tests/r7rs-tests.scm b/tests/r7rs-tests.scm
index 4c98869e..da337179 100644
--- a/tests/r7rs-tests.scm
+++ b/tests/r7rs-tests.scm
@@ -1,5 +1,7 @@
 ;; R7RS Tests
 
+(use (only ports with-input-from-string with-output-to-string))
+
 ;; Copied from R4RS tests
 (define cur-section '())
 
diff --git a/tests/reader-tests.scm b/tests/reader-tests.scm
index 894e8461..6fbaddf4 100644
--- a/tests/reader-tests.scm
+++ b/tests/reader-tests.scm
@@ -1,7 +1,8 @@
 ;;;; reader-tests.scm
 
 
-(use utils)
+(use (only ports with-input-from-string with-output-to-string)
+     (only utils read-all))
 
 
 (set-sharp-read-syntax! #\& (lambda (p) (read p) (values)))
diff --git a/tests/runtests.sh b/tests/runtests.sh
index f4dc1a8d..63301f90 100755
--- a/tests/runtests.sh
+++ b/tests/runtests.sh
@@ -38,7 +38,7 @@ mkdir -p test-repository
 for x in setup-api.so setup-api.import.so setup-download.so \
       setup-download.import.so chicken.import.so lolevel.import.so \
       srfi-4.import.so data-structures.import.so \
-      ports.import.so files.import.so posix.import.so \
+      chicken.ports.import.so files.import.so posix.import.so \
       extras.import.so \
       irregex.import.so tcp.import.so \
       foreign.import.so \
diff --git a/tests/test-irregex.scm b/tests/test-irregex.scm
index d2754218..57b65596 100644
--- a/tests/test-irregex.scm
+++ b/tests/test-irregex.scm
@@ -1,7 +1,7 @@
 ;;;: test-irregex.scm
 
 
-(use extras irregex)
+(use irregex ports)
 
 (include "test.scm")
 
diff --git a/types.db b/types.db
index 16ff9a3e..201fda0d 100644
--- a/types.db
+++ b/types.db
@@ -1790,24 +1790,24 @@
 
 ;; ports
 
-(call-with-input-string (#(procedure #:enforce) call-with-input-string (string (procedure (input-port) . *)) . *))
-(call-with-output-string (#(procedure #:enforce) call-with-output-string ((procedure (output-port) . *)) string))
-(copy-port (#(procedure #:enforce) copy-port (* * #!optional (procedure (*) *) (procedure (* output-port) *)) undefined)) 
-(make-input-port (#(procedure #:clean #:enforce) make-input-port ((procedure () (or char eof)) (procedure () *) (procedure () . *) #!optional * * * *) input-port))
-(make-output-port (#(procedure #:clean #:enforce) make-output-port ((procedure (string) . *) (procedure () . *) #!optional (procedure () . *)) output-port))
-(port-for-each (#(procedure #:enforce) port-for-each ((procedure (*) *) (procedure () . *)) undefined))
-
-(port-map
- (forall (a b) (#(procedure #:enforce) port-map ((procedure (a) b) (procedure () a)) (list-of b))))
-
-(port-fold (#(procedure #:enforce) port-fold ((procedure (* *) *) * (procedure () *)) *))
-(make-broadcast-port (#(procedure #:clean #:enforce) make-broadcast-port (#!rest output-port) output-port))
-(make-concatenated-port (#(procedure #:clean #:enforce) make-concatenated-port (port #!rest input-port) input-port))
-(with-error-output-to-port (#(procedure #:enforce) with-error-output-to-port (output-port (procedure () . *)) . *))
-(with-input-from-port (#(procedure #:enforce) with-input-from-port (input-port (procedure () . *)) . *))
-(with-input-from-string (#(procedure #:enforce) with-input-from-string (string (procedure () . *)) . *))
-(with-output-to-port (#(procedure #:enforce) with-output-to-port (output-port (procedure () . *)) . *))
-(with-output-to-string (#(procedure #:enforce) with-output-to-string ((procedure () . *)) . *))
+(chicken.ports#call-with-input-string (#(procedure #:enforce) chicken.ports#call-with-input-string (string (procedure (input-port) . *)) . *))
+(chicken.ports#call-with-output-string (#(procedure #:enforce) chicken.ports#call-with-output-string ((procedure (output-port) . *)) string))
+(chicken.ports#copy-port (#(procedure #:enforce) chicken.ports#copy-port (* * #!optional (procedure (*) *) (procedure (* output-port) *)) undefined))
+(chicken.ports#make-input-port (#(procedure #:clean #:enforce) chicken.ports#make-input-port ((procedure () (or char eof)) (procedure () *) (procedure () . *) #!optional * * * *) input-port))
+(chicken.ports#make-output-port (#(procedure #:clean #:enforce) chicken.ports#make-output-port ((procedure (string) . *) (procedure () . *) #!optional (procedure () . *)) output-port))
+(chicken.ports#port-for-each (#(procedure #:enforce) chicken.ports#port-for-each ((procedure (*) *) (procedure () . *)) undefined))
+
+(chicken.ports#port-map
+ (forall (a b) (#(procedure #:enforce) chicken.ports#port-map ((procedure (a) b) (procedure () a)) (list-of b))))
+
+(chicken.ports#port-fold (#(procedure #:enforce) chicken.ports#port-fold ((procedure (* *) *) * (procedure () *)) *))
+(chicken.ports#make-broadcast-port (#(procedure #:clean #:enforce) chicken.ports#make-broadcast-port (#!rest output-port) output-port))
+(chicken.ports#make-concatenated-port (#(procedure #:clean #:enforce) chicken.ports#make-concatenated-port (port #!rest input-port) input-port))
+(chicken.ports#with-error-output-to-port (#(procedure #:enforce) chicken.ports#with-error-output-to-port (output-port (procedure () . *)) . *))
+(chicken.ports#with-input-from-port (#(procedure #:enforce) chicken.ports#with-input-from-port (input-port (procedure () . *)) . *))
+(chicken.ports#with-input-from-string (#(procedure #:enforce) chicken.ports#with-input-from-string (string (procedure () . *)) . *))
+(chicken.ports#with-output-to-port (#(procedure #:enforce) chicken.ports#with-output-to-port (output-port (procedure () . *)) . *))
+(chicken.ports#with-output-to-string (#(procedure #:enforce) chicken.ports#with-output-to-string ((procedure () . *)) . *))
 
 
 ;; posix
Trap